UNPKG

typesxml

Version:

Open source XML library written in TypeScript

40 lines 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FetchUrls = void 0; const node_child_process_1 = require("node:child_process"); const node_fs_1 = require("node:fs"); class FetchUrls { errorMessage; constructor() { this.errorMessage = ''; } getError() { return this.errorMessage; } fetch(url, outputPath) { this.errorMessage = ''; if ((0, node_fs_1.existsSync)(outputPath)) { (0, node_fs_1.unlinkSync)(outputPath); } const module = url.startsWith('https') ? 'https' : 'http'; const script = 'const m = require("' + module + '");' + 'const fs = require("fs");' + 'm.get(' + JSON.stringify(url) + ', res => {' + 'if (res.statusCode !== 200) { process.stderr.write("Received status code " + res.statusCode + " trying to fetch ' + url + '\\n"); process.exit(1); }' + 'const d = [];' + 'res.on("data", c => d.push(c));' + 'res.on("end", () => fs.writeFileSync(' + JSON.stringify(outputPath) + ', Buffer.concat(d).toString("utf-8"), "utf-8"));' + 'res.on("error", err => { process.stderr.write("Error trying to fetch ' + url + ': " + err.message + "\\n"); process.exit(1); });' + '});'; try { (0, node_child_process_1.execFileSync)(process.execPath, ['-e', script], { stdio: 'pipe' }); return true; } catch (err) { this.errorMessage = err.stderr?.toString().trim() ?? ''; return false; } } } exports.FetchUrls = FetchUrls; //# sourceMappingURL=fetchUrls.js.map