Avanzado
Retrievers personalizados
Cómo extender AbstractXmlRetriever o AbstractBaseRetriever para descargar otros tipos de recursos XML y ajustar límites.
Además de XsdRetriever y XsltRetriever, puedes crear tus propios retrievers para cualquier formato XML con referencias externas (por ejemplo, WSDL, Schematron o vocabularios propios) o para recursos que requieran lógica de validación distinta.
Extender AbstractXmlRetriever
Para recursos XML que referencian otros documentos mediante atributos en un namespace específico, extiende AbstractXmlRetriever e implementa searchNamespace() y searchElements().
src/wsdl_retriever.ts
import type { SearchElement } from '@nodecfdi/xml-resource-retriever/types';
import { AbstractXmlRetriever } from '@nodecfdi/xml-resource-retriever';
export class WsdlRetriever extends AbstractXmlRetriever {
protected searchNamespace(): string {
return 'http://schemas.xmlsoap.org/wsdl/';
}
protected searchElements(): SearchElement[] {
return [{ element: 'import', attribute: 'location' }];
}
}
Ajustar límites de recursión y concurrencia
AbstractXmlRetriever expone tres métodos protegidos que puedes sobrescribir para ajustar los límites por operación retrieve():
| Método | Valor por defecto | Descripción |
|---|---|---|
maxDepth() | 50 | Profundidad máxima de anidamiento al seguir recursos enlazados. |
maxResources() | 500 | Número máximo total de recursos descargados en una sola llamada a retrieve(). |
maxConcurrentDownloads() | 6 | Número máximo de descargas simultáneas (siempre se garantiza al menos 1). |
src/limited_xsd_retriever.ts
import { XsdRetriever } from '@nodecfdi/xml-resource-retriever';
export class LimitedXsdRetriever extends XsdRetriever {
protected override maxDepth(): number {
return 15;
}
protected override maxResources(): number {
return 100;
}
protected override maxConcurrentDownloads(): number {
return 3;
}
}
Extender AbstractBaseRetriever
Si necesitas descargar recursos que no siguen el flujo estándar de parseo XML con searchNamespace() / searchElements(), extiende directamente AbstractBaseRetriever.
Deberás implementar:
retrieve(url: string): Promise<string>: lógica de descarga y gestión del historial usandoclearHistory(),addToHistory(),hasInHistory(),getFromHistory()yhistorySize().checkIsValidDownloadedFile(source: string, localPath: string): Promise<void>: validación ejecutada automáticamente al final dedownload(url).
src/custom_retriever.ts
import { stat, unlink } from 'node:fs/promises';
import { AbstractBaseRetriever } from '@nodecfdi/xml-resource-retriever';
export class SimpleFileRetriever extends AbstractBaseRetriever {
public constructor(basePath: string) {
super(basePath);
}
public async retrieve(url: string): Promise<string> {
this.clearHistory();
const localPath = await this.download(url);
this.addToHistory(url, localPath);
return localPath;
}
protected async checkIsValidDownloadedFile(source: string, localPath: string): Promise<void> {
const info = await stat(localPath);
if (info.size > 0) {
return;
}
await unlink(localPath);
throw new Error(`El recurso ${source} está vacío`);
}
}
