nginx-binaries
Version:
A downloader for nginx and njs standalone binaries.
52 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDownloader = void 0;
const OS = require("node:os");
const path = require("node:path");
const repoIndex_1 = require("./repoIndex");
const archName_1 = require("./internal/archName");
const cacheDir_1 = require("./internal/cacheDir");
const downloadFile_1 = require("./internal/downloadFile");
const utils_1 = require("./internal/utils");
const defaultQuery = {
arch: (0, archName_1.normalizeArch)(OS.arch()),
os: OS.platform(),
variant: '',
};
const createDownloader = (name) => (0, utils_1.bindAll)({
cacheMaxAge: 8 * 60,
repoUrl: 'https://jirutka.github.io/nginx-binaries',
timeout: 10000,
get cacheDir() {
// Replace accessors with plain value (lazy initialization).
return (0, utils_1.replaceProperty)(this, 'cacheDir', (0, cacheDir_1.getCacheDir)('nginx-binaries'));
},
set cacheDir(dirpath) {
this.cacheDir && (this.cacheDir = dirpath);
},
async download(query, destFilePath) {
const [entry,] = await this.search(query);
if (!entry) {
throw RangeError(`No ${name} binary found for ${(0, repoIndex_1.formatQuery)({ ...defaultQuery, ...query })}`);
}
destFilePath ?? (destFilePath = path.join(this.cacheDir, entry.filename));
const url = `${this.repoUrl}/${entry.filename}`;
return await (0, downloadFile_1.downloadFile)(url, entry.checksum, destFilePath, { timeout: this.timeout });
},
async search(query) {
const index = await (0, repoIndex_1.getIndex)(this.repoUrl, this.cacheDir, this.timeout, this.cacheMaxAge);
// TODO: Move to getIndex()
if (index.formatVersion !== repoIndex_1.FORMAT_VERSION) {
throw Error(`Index format version mismatch, clean cache and update nginx-binaries package`);
}
return (0, repoIndex_1.queryIndex)(index, name, { ...defaultQuery, ...query });
},
async variants(query) {
return [...new Set((await this.search(query ?? {})).map(x => x.variant))];
},
async versions(query) {
return [...new Set((await this.search(query ?? {})).map(x => x.version))];
},
});
exports.createDownloader = createDownloader;
//# sourceMappingURL=downloader.js.map