UNPKG

4.86 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.clearCache = exports.getVersion = void 0;
7const zlib_1 = __importDefault(require("zlib"));
8const fs_1 = require("fs");
9const util_1 = require("util");
10const path_1 = __importDefault(require("path"));
11const os_1 = __importDefault(require("os"));
12const node_fetch_1 = __importDefault(require("node-fetch"));
13const semver_1 = __importDefault(require("semver"));
14const debug_1 = __importDefault(require("debug"));
15const debug = debug_1.default('mongodb-download-url:version-list');
16const gunzip = util_1.promisify(zlib_1.default.gunzip);
17const gzip = util_1.promisify(zlib_1.default.gzip);
18function defaultCachePath() {
19 return path_1.default.join(os_1.default.tmpdir(), '.mongodb-full.json.gz');
20}
21let fullJSON;
22let fullJSONFetchTime = 0;
23async function getFullJSON(opts) {
24 var _a, _b, _c;
25 const versionListUrl = (_a = opts.versionListUrl) !== null && _a !== void 0 ? _a : 'https://downloads.mongodb.org/full.json';
26 const cachePath = (_b = opts.cachePath) !== null && _b !== void 0 ? _b : defaultCachePath();
27 const cacheTimeMs = (_c = opts.cacheTimeMs) !== null && _c !== void 0 ? _c : 24 * 3600 * 1000;
28 let tryWriteCache = cacheTimeMs > 0;
29 const inMemoryCopyUpToDate = () => fullJSONFetchTime >= new Date().getTime() - cacheTimeMs;
30 try {
31 if ((!fullJSON || !inMemoryCopyUpToDate()) && cacheTimeMs > 0) {
32 debug('trying to load versions from cache', cachePath);
33 const fh = await fs_1.promises.open(cachePath, 'r');
34 try {
35 const stat = await fh.stat();
36 if (process.getuid && (stat.uid !== process.getuid() || (stat.mode & 0o022) !== 0)) {
37 tryWriteCache = false;
38 debug('cannot use cache because it is not a file or we do not own it');
39 throw new Error();
40 }
41 if (stat.mtime.getTime() < new Date().getTime() - cacheTimeMs) {
42 debug('cache is outdated');
43 throw new Error();
44 }
45 debug('cache up-to-date');
46 tryWriteCache = false;
47 fullJSON = JSON.parse((await gunzip(await fh.readFile())).toString());
48 fullJSONFetchTime = new Date().getTime();
49 }
50 finally {
51 await fh.close();
52 }
53 }
54 }
55 catch (_d) { }
56 if (!fullJSON || !inMemoryCopyUpToDate()) {
57 debug('trying to load versions from source', versionListUrl);
58 const response = await node_fetch_1.default(versionListUrl);
59 if (!response.ok) {
60 throw new Error(`Could not get mongodb versions from ${versionListUrl}: ${response.statusText}`);
61 }
62 fullJSON = await response.json();
63 fullJSONFetchTime = new Date().getTime();
64 if (tryWriteCache) {
65 const partialFilePath = cachePath + `.partial.${process.pid}`;
66 await fs_1.promises.mkdir(path_1.default.dirname(cachePath), { recursive: true });
67 try {
68 const compressed = await gzip(JSON.stringify(fullJSON), { level: 9 });
69 await fs_1.promises.writeFile(partialFilePath, compressed, { mode: 0o644, flag: 'wx' });
70 await fs_1.promises.rename(partialFilePath, cachePath);
71 debug('wrote cache', cachePath);
72 }
73 catch (_e) {
74 try {
75 await fs_1.promises.unlink(partialFilePath);
76 }
77 catch (_f) { }
78 }
79 }
80 }
81 return fullJSON;
82}
83async function getVersion(opts) {
84 const fullJSON = await getFullJSON(opts);
85 let versions = fullJSON.versions;
86 versions = versions.filter((info) => info.downloads.length > 0);
87 if (opts.productionOnly) {
88 versions = versions.filter((info) => info.production_release);
89 }
90 if (opts.version && opts.version !== '*') {
91 versions = versions.filter((info) => semver_1.default.satisfies(info.version, opts.version));
92 }
93 versions = versions.sort((a, b) => semver_1.default.rcompare(a.version, b.version));
94 return versions[0];
95}
96exports.getVersion = getVersion;
97async function clearCache(cachePath) {
98 debug('clearing cache');
99 fullJSON = undefined;
100 fullJSONFetchTime = 0;
101 if (cachePath !== '') {
102 try {
103 await fs_1.promises.unlink(cachePath !== null && cachePath !== void 0 ? cachePath : defaultCachePath());
104 }
105 catch (err) {
106 if (err.code === 'ENOENT')
107 return;
108 throw err;
109 }
110 }
111}
112exports.clearCache = clearCache;
113//# sourceMappingURL=version-list.js.map
\No newline at end of file