@dgac/nmb2b-client
Version:
EUROCONTROL Network Manager B2B SOAP client
45 lines (43 loc) • 1.46 kB
JavaScript
import { createDebugLogger } from "../debug.mjs";
import { getXSDCacheDirectory } from "./paths.mjs";
import { createDir, dirExists } from "../fs.mjs";
import { downloadAndExtractWSDL } from "./downloadAndExtractWSDL.mjs";
import { getWSDLDownloadURL } from "./getWSDLDownloadURL.mjs";
import { readdir } from "node:fs/promises";
import lockfile from "proper-lockfile";
//#region src/utils/xsd/index.ts
const debug = createDebugLogger("wsdl");
async function WSDLExists(config) {
const directory = getXSDCacheDirectory(config);
debug(`Checking if directory ${directory} exists`);
if (!await dirExists(directory)) return false;
return (await readdir(directory)).length > 0;
}
async function download(config) {
const outputDir = getXSDCacheDirectory(config);
if (!await dirExists(outputDir)) {
debug(`Creating directory ${outputDir}`);
await createDir(outputDir);
}
debug(`Acquiring lock for folder ${outputDir}`);
const releaseLock = await lockfile.lock(outputDir, { retries: 5 });
try {
debug(`Lock acquired. Testing WSDL existence ...`);
const hasWSDL = await WSDLExists(config);
if (!config.ignoreWSDLCache && hasWSDL) {
debug("WSDL found");
return;
}
const url = await getWSDLDownloadURL(config);
debug(`Downloading ${url}`);
await downloadAndExtractWSDL(url, {
security: config.security,
outputDir
});
} finally {
await releaseLock();
}
}
//#endregion
export { download };
//# sourceMappingURL=index.mjs.map