1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.clearCache = exports.getDownloadURL = void 0;
|
7 | const os_1 = __importDefault(require("os"));
|
8 | const path_1 = __importDefault(require("path"));
|
9 | const semver_1 = __importDefault(require("semver"));
|
10 | const version_list_1 = require("./version-list");
|
11 | Object.defineProperty(exports, "clearCache", { enumerable: true, get: function () { return version_list_1.clearCache; } });
|
12 | const linux_distro_1 = require("./linux-distro");
|
13 | const util_1 = require("util");
|
14 | const debug_1 = __importDefault(require("debug"));
|
15 | const debug = debug_1.default('mongodb-download-url');
|
16 | function getPriority(values, candidate) {
|
17 | for (const { value, priority } of values) {
|
18 | if (value === candidate) {
|
19 | return priority;
|
20 | }
|
21 | }
|
22 | return 0;
|
23 | }
|
24 | function maximizer(values, evaluator) {
|
25 | let max = -Infinity;
|
26 | let maximizer;
|
27 | for (const v of values) {
|
28 | const result = evaluator(v);
|
29 | if (result > max) {
|
30 | max = result;
|
31 | maximizer = v;
|
32 | }
|
33 | }
|
34 | return maximizer;
|
35 | }
|
36 | function parseArch(arch) {
|
37 | if (['i686', 'i386', 'x86', 'ia32'].includes(arch)) {
|
38 | return ['i686', 'i386', 'x86', 'ia32'];
|
39 | }
|
40 | if (['x86_64', 'x64'].includes(arch)) {
|
41 | return ['x86_64', 'x64'];
|
42 | }
|
43 | if (['arm64', 'aarch64'].includes(arch)) {
|
44 | return ['arm64', 'aarch64'];
|
45 | }
|
46 | if (['ppc64', 'ppc64le'].includes(arch)) {
|
47 | return ['ppc64', 'ppc64le'];
|
48 | }
|
49 | return [arch];
|
50 | }
|
51 | async function parseTarget(distro, platform, archs, version) {
|
52 | if (platform === 'linux') {
|
53 | const results = [];
|
54 | if (distro) {
|
55 | results.push({ value: distro, priority: 1000 });
|
56 | if (archs.includes('x86_64')) {
|
57 | if (distro === 'amzn64' || distro === 'amazon1') {
|
58 | results.push({ value: 'amazon', priority: 900 });
|
59 | }
|
60 | if (distro === 'amazon' || distro === 'amazon1') {
|
61 | results.push({ value: 'amzn64', priority: 900 });
|
62 | }
|
63 | }
|
64 | }
|
65 | if (archs.includes('x86_64')) {
|
66 | results.push({ value: 'linux_x86_64', priority: 1 });
|
67 | }
|
68 | else if (archs.includes('i686')) {
|
69 | results.push({ value: 'linux_i686', priority: 1 });
|
70 | }
|
71 | let distroResultsErr;
|
72 | try {
|
73 | results.push(...await linux_distro_1.getCurrentLinuxDistro());
|
74 | }
|
75 | catch (err) {
|
76 | distroResultsErr = err;
|
77 | }
|
78 | if (distro === undefined &&
|
79 | distroResultsErr &&
|
80 | (version === '*' ||
|
81 | version === 'latest-alpha' ||
|
82 | semver_1.default.gte(version, '4.0.0'))) {
|
83 | throw distroResultsErr;
|
84 | }
|
85 | return results;
|
86 | }
|
87 | else if (platform === 'sunos') {
|
88 | return [{ value: 'sunos5', priority: 1 }];
|
89 | }
|
90 | else if (['win32', 'windows'].includes(platform)) {
|
91 | if (archs.includes('i686')) {
|
92 | return [
|
93 | { value: 'windows', priority: 1 },
|
94 | { value: 'windows_i686', priority: 10 }
|
95 | ];
|
96 | }
|
97 | else {
|
98 | return [
|
99 | { value: 'windows', priority: 1 },
|
100 | { value: 'windows_x86_64', priority: 10 },
|
101 | { value: 'windows_x86_64-2008plus', priority: 10 },
|
102 | { value: 'windows_x86_64-2008plus-ssl', priority: 100 },
|
103 | { value: 'windows_x86_64-2012plus', priority: 100 }
|
104 | ];
|
105 | }
|
106 | }
|
107 | else if (['darwin', 'osx', 'macos'].includes(platform)) {
|
108 | return [
|
109 | { value: 'osx', priority: 1 },
|
110 | { value: 'osx-ssl', priority: 10 },
|
111 | { value: 'darwin', priority: 1 },
|
112 | { value: 'macos', priority: 1 }
|
113 | ];
|
114 | }
|
115 | return [{ value: platform, priority: 1 }];
|
116 | }
|
117 | async function resolve(opts) {
|
118 | var _a, _b, _c, _d;
|
119 | let download;
|
120 | if (opts.version === 'latest-alpha' && opts.enterprise) {
|
121 | const targets = opts.target.map(({ value }) => value);
|
122 | let url, target;
|
123 | if (targets.includes('macos')) {
|
124 | url = 'https://downloads.mongodb.com/osx/mongodb-macos-x86_64-enterprise-latest.tgz';
|
125 | target = 'macos';
|
126 | }
|
127 | else if (targets.includes('linux_x86_64')) {
|
128 | target = maximizer(opts.target, candidate => candidate.priority).value;
|
129 | url = `https://downloads.mongodb.com/linux/mongodb-linux-x86_64-enterprise-${target}-latest.tgz`;
|
130 | }
|
131 | else if (targets.includes('windows_x86_64')) {
|
132 | target = 'windows';
|
133 | url = 'https://downloads.mongodb.com/windows/mongodb-windows-x86_64-enterprise-latest.zip';
|
134 | }
|
135 | if (url) {
|
136 | download = {
|
137 | target,
|
138 | edition: 'enterprise',
|
139 | arch: 'x86_64',
|
140 | archive: {
|
141 | url,
|
142 | sha1: '',
|
143 | sha256: '',
|
144 | debug_symbols: ''
|
145 | }
|
146 | };
|
147 | }
|
148 | }
|
149 | let version;
|
150 | if (!download) {
|
151 | version = await version_list_1.getVersion(opts);
|
152 | if (!version) {
|
153 | throw new Error(`Could not find version matching ${util_1.inspect(opts)}`);
|
154 | }
|
155 | const bestDownload = maximizer(version.downloads.map((candidate) => {
|
156 | if (opts.enterprise) {
|
157 | if (candidate.edition !== 'enterprise') {
|
158 | return { value: candidate, priority: 0 };
|
159 | }
|
160 | }
|
161 | else {
|
162 | if (candidate.edition !== 'targeted' && candidate.edition !== 'base') {
|
163 | return { value: candidate, priority: 0 };
|
164 | }
|
165 | }
|
166 | if (!opts.arch.includes(candidate.arch)) {
|
167 | return { value: candidate, priority: 0 };
|
168 | }
|
169 | const targetPriority = getPriority(opts.target, candidate.target);
|
170 | return { value: candidate, priority: targetPriority };
|
171 | }), (candidate) => candidate.priority);
|
172 | if (bestDownload.priority > 0) {
|
173 | download = bestDownload.value;
|
174 | }
|
175 | }
|
176 | if (!download) {
|
177 | throw new Error(`Could not find download URL for version ${version === null || version === void 0 ? void 0 : version.version} ${util_1.inspect(opts)}`);
|
178 | }
|
179 | debug('fully resolved', JSON.stringify(opts, null, 2), download);
|
180 | const wantsCryptd = opts.cryptd && download.target;
|
181 | let { url } = (_a = (wantsCryptd ? download.cryptd : null)) !== null && _a !== void 0 ? _a : download.archive;
|
182 | if (wantsCryptd) {
|
183 | url = url.replace('mongodb-shell-windows', 'mongodb-cryptd-windows');
|
184 | }
|
185 | return {
|
186 | ...opts,
|
187 | name: 'mongodb',
|
188 | url: url,
|
189 | arch: download.arch,
|
190 | distro: download.target,
|
191 | platform: download.target,
|
192 | filenamePlatform: download.target,
|
193 | version: (_b = version === null || version === void 0 ? void 0 : version.version) !== null && _b !== void 0 ? _b : '*',
|
194 | artifact: path_1.default.basename(url),
|
195 | debug: false,
|
196 | enterprise: download.edition === 'enterprise',
|
197 | branch: 'master',
|
198 | bits: ['i386', 'i686'].includes(download.arch) ? '32' : '64',
|
199 | ext: (_d = (_c = url.match(/\.([^.]+)$/)) === null || _c === void 0 ? void 0 : _c[1]) !== null && _d !== void 0 ? _d : 'tgz'
|
200 | };
|
201 | }
|
202 | async function options(opts = {}) {
|
203 | if (typeof opts === 'string') {
|
204 | opts = {
|
205 | version: opts
|
206 | };
|
207 | }
|
208 | else {
|
209 | opts = { ...opts };
|
210 | }
|
211 | if (opts.bits && !opts.arch) {
|
212 | opts.arch = +opts.bits === 32 ? 'ia32' : 'x64';
|
213 | }
|
214 | if (!opts.arch) {
|
215 | opts.arch = os_1.default.arch();
|
216 | }
|
217 | if (!opts.platform) {
|
218 | opts.platform = os_1.default.platform();
|
219 | }
|
220 | if (!opts.version) {
|
221 | opts.version = process.env.MONGODB_VERSION || 'stable';
|
222 | }
|
223 | if (opts.version === 'stable' || opts.version === 'latest' || opts.version === '*') {
|
224 | opts.version = '*';
|
225 | opts.productionOnly = true;
|
226 | }
|
227 | else if (opts.version === 'unstable') {
|
228 | opts.version = '*';
|
229 | opts.productionOnly = false;
|
230 | }
|
231 | const processedOptions = {
|
232 | ...opts,
|
233 | arch: parseArch(opts.arch),
|
234 | target: [],
|
235 | enterprise: !!opts.enterprise,
|
236 | cryptd: !!opts.cryptd,
|
237 | version: opts.version
|
238 | };
|
239 | processedOptions.target = await parseTarget(opts.distro, opts.platform, processedOptions.arch, processedOptions.version);
|
240 | return processedOptions;
|
241 | }
|
242 | async function getDownloadURL(opts) {
|
243 | const parsedOptions = await options(opts);
|
244 | debug('Building URL for options `%j`', parsedOptions);
|
245 | return await resolve(parsedOptions);
|
246 | }
|
247 | exports.getDownloadURL = getDownloadURL;
|
248 | exports.default = getDownloadURL;
|
249 |
|
\ | No newline at end of file |