-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathMongoBinaryDownload.ts
528 lines (434 loc) · 15.9 KB
/
MongoBinaryDownload.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import os from 'os';
import { URL } from 'url';
import path from 'path';
import { promises as fspromises, createWriteStream, createReadStream, constants } from 'fs';
import md5File from 'md5-file';
import https from 'https';
import { createUnzip } from 'zlib';
import tar from 'tar-stream';
import yauzl from 'yauzl';
import MongoBinaryDownloadUrl from './MongoBinaryDownloadUrl';
import { HttpsProxyAgent } from 'https-proxy-agent';
import resolveConfig, { envToBool, ResolveConfigVariables } from './resolveConfig';
import debug from 'debug';
import { assertion, mkdir, pathExists } from './utils';
import { DryMongoBinary } from './DryMongoBinary';
import { MongoBinaryOpts } from './MongoBinary';
import { clearLine } from 'readline';
import { GenericMMSError, Md5CheckFailedError } from './errors';
const log = debug('MongoMS:MongoBinaryDownload');
export interface MongoBinaryDownloadProgress {
current: number;
length: number;
totalMb: number;
lastPrintedAt: number;
}
/**
* Download and extract the "mongod" binary
*/
export class MongoBinaryDownload {
dlProgress: MongoBinaryDownloadProgress;
_downloadingUrl?: string;
/**These options are kind of raw, they are not run through DryMongoBinary.generateOptions */
binaryOpts: Required<MongoBinaryOpts>;
// TODO: for an major version, remove the compat get/set
// the following get/set are to not break existing stuff
get checkMD5(): boolean {
return this.binaryOpts.checkMD5;
}
set checkMD5(val: boolean) {
this.binaryOpts.checkMD5 = val;
}
get downloadDir(): string {
return this.binaryOpts.downloadDir;
}
set downloadDir(val: string) {
this.binaryOpts.downloadDir = val;
}
get arch(): string {
return this.binaryOpts.arch;
}
set arch(val: string) {
this.binaryOpts.arch = val;
}
get version(): string {
return this.binaryOpts.version;
}
set version(val: string) {
this.binaryOpts.version = val;
}
get platform(): string {
return this.binaryOpts.platform;
}
set platform(val: string) {
this.binaryOpts.platform = val;
}
// end get/set backwards compat section
constructor(opts: MongoBinaryOpts) {
assertion(typeof opts.downloadDir === 'string', new Error('An DownloadDir must be specified!'));
const version = opts.version ?? resolveConfig(ResolveConfigVariables.VERSION);
assertion(
typeof version === 'string',
new Error('An MongoDB Binary version must be specified!')
);
// DryMongoBinary.generateOptions cannot be used here, because its async
this.binaryOpts = {
platform: opts.platform ?? os.platform(),
arch: opts.arch ?? os.arch(),
version: version,
downloadDir: opts.downloadDir,
checkMD5: opts.checkMD5 ?? envToBool(resolveConfig(ResolveConfigVariables.MD5_CHECK)),
systemBinary: opts.systemBinary ?? '',
os: opts.os ?? { os: 'unknown' },
};
this.dlProgress = {
current: 0,
length: 0,
totalMb: 0,
lastPrintedAt: 0,
};
}
/**
* Get the full path with filename
* @returns Absoulte Path with FileName
*/
protected async getPath(): Promise<string> {
const opts = await DryMongoBinary.generateOptions(this.binaryOpts);
return DryMongoBinary.combineBinaryName(
this.downloadDir,
await DryMongoBinary.getBinaryName(opts)
);
}
/**
* Get the path of the already downloaded "mongod" file
* otherwise download it and then return the path
*/
async getMongodPath(): Promise<string> {
log('getMongodPath');
const mongodPath = await this.getPath();
if (await pathExists(mongodPath)) {
log(`getMongodPath: mongod path "${mongodPath}" already exists, using this`);
return mongodPath;
}
const mongoDBArchive = await this.startDownload();
await this.extract(mongoDBArchive);
await fspromises.unlink(mongoDBArchive);
if (await pathExists(mongodPath)) {
return mongodPath;
}
throw new Error(`Cannot find downloaded mongod binary by path "${mongodPath}"`);
}
/**
* Download the MongoDB Archive and check it against an MD5
* @returns The MongoDB Archive location
*/
async startDownload(): Promise<string> {
log('startDownload');
const mbdUrl = new MongoBinaryDownloadUrl(this.binaryOpts);
await mkdir(this.downloadDir);
try {
await fspromises.access(this.downloadDir, constants.X_OK | constants.W_OK); // check that this process has permissions to create files & modify file contents & read file contents
} catch (err) {
console.error(
`Download Directory at "${this.downloadDir}" does not have sufficient permissions to be used by this process\n` +
'Needed Permissions: Write & Execute (-wx)\n'
);
throw err;
}
const downloadUrl = await mbdUrl.getDownloadUrl();
const mongoDBArchive = await this.download(downloadUrl);
await this.makeMD5check(`${downloadUrl}.md5`, mongoDBArchive);
return mongoDBArchive;
}
/**
* Download MD5 file and check it against the MongoDB Archive
* @param urlForReferenceMD5 URL to download the MD5
* @param mongoDBArchive The MongoDB Archive file location
*
* @returns {undefined} if "checkMD5" is falsey
* @returns {true} if the md5 check was successful
* @throws if the md5 check failed
*/
async makeMD5check(
urlForReferenceMD5: string,
mongoDBArchive: string
): Promise<boolean | undefined> {
log('makeMD5check: Checking MD5 of downloaded binary...');
if (!this.checkMD5) {
log('makeMD5check: checkMD5 is disabled');
return undefined;
}
const archiveMD5Path = await this.download(urlForReferenceMD5);
const signatureContent = (await fspromises.readFile(archiveMD5Path)).toString('utf-8');
const regexMatch = signatureContent.match(/^\s*([\w\d]+)\s*/i);
const md5SigRemote = regexMatch ? regexMatch[1] : null;
const md5SigLocal = md5File.sync(mongoDBArchive);
log(`makeMD5check: Local MD5: ${md5SigLocal}, Remote MD5: ${md5SigRemote}`);
if (md5SigRemote !== md5SigLocal) {
throw new Md5CheckFailedError(md5SigLocal, md5SigRemote || 'unknown');
}
await fspromises.unlink(archiveMD5Path);
return true;
}
/**
* Download file from downloadUrl
* @param downloadUrl URL to download a File
* @returns The Path to the downloaded archive file
*/
async download(downloadUrl: string): Promise<string> {
log('download');
const proxy =
process.env['yarn_https-proxy'] ||
process.env.yarn_proxy ||
process.env['npm_config_https-proxy'] ||
process.env.npm_config_proxy ||
process.env.https_proxy ||
process.env.http_proxy ||
process.env.HTTPS_PROXY ||
process.env.HTTP_PROXY;
const strictSsl = process.env.npm_config_strict_ssl === 'true';
const urlObject = new URL(downloadUrl);
urlObject.port = urlObject.port || '443';
const requestOptions: https.RequestOptions = {
method: 'GET',
rejectUnauthorized: strictSsl,
protocol: envToBool(resolveConfig(ResolveConfigVariables.USE_HTTP)) ? 'http:' : 'https:',
agent: proxy ? new HttpsProxyAgent(proxy) : undefined,
};
const filename = urlObject.pathname.split('/').pop();
if (!filename) {
throw new Error(`MongoBinaryDownload: missing filename for url "${downloadUrl}"`);
}
const downloadLocation = path.resolve(this.downloadDir, filename);
const tempDownloadLocation = path.resolve(this.downloadDir, `${filename}.downloading`);
log(`download: Downloading${proxy ? ` via proxy "${proxy}"` : ''}: "${downloadUrl}"`);
if (await pathExists(downloadLocation)) {
log('download: Already downloaded archive found, skipping download');
return downloadLocation;
}
this.assignDownloadingURL(urlObject);
const downloadedFile = await this.httpDownload(
urlObject,
requestOptions,
downloadLocation,
tempDownloadLocation
);
return downloadedFile;
}
/**
* Extract given Archive
* @param mongoDBArchive Archive location
* @returns extracted directory location
*/
async extract(mongoDBArchive: string): Promise<string> {
log('extract');
const mongodbFullPath = await this.getPath();
log(`extract: archive: "${mongoDBArchive}" final: "${mongodbFullPath}"`);
await mkdir(path.dirname(mongodbFullPath));
const filter = (file: string) => /(?:bin\/(?:mongod(?:\.exe)?))$/i.test(file);
if (/(.tar.gz|.tgz)$/.test(mongoDBArchive)) {
await this.extractTarGz(mongoDBArchive, mongodbFullPath, filter);
} else if (/.zip$/.test(mongoDBArchive)) {
await this.extractZip(mongoDBArchive, mongodbFullPath, filter);
} else {
throw new Error(
`MongoBinaryDownload: unsupported archive "${mongoDBArchive}" (downloaded from "${
this._downloadingUrl ?? 'unknown'
}"). Broken archive from MongoDB Provider?`
);
}
if (!(await pathExists(mongodbFullPath))) {
throw new Error(
`MongoBinaryDownload: missing mongod binary in "${mongoDBArchive}" (downloaded from "${
this._downloadingUrl ?? 'unknown'
}"). Broken archive from MongoDB Provider?`
);
}
return mongodbFullPath;
}
/**
* Extract a .tar.gz archive
* @param mongoDBArchive Archive location
* @param extractPath Directory to extract to
* @param filter Method to determine which files to extract
*/
async extractTarGz(
mongoDBArchive: string,
extractPath: string,
filter: (file: string) => boolean
): Promise<void> {
log('extractTarGz');
const extract = tar.extract();
extract.on('entry', (header, stream, next) => {
if (filter(header.name)) {
stream.pipe(
createWriteStream(extractPath, {
mode: 0o775,
})
);
}
stream.on('end', () => next());
stream.resume();
});
return new Promise((res, rej) => {
createReadStream(mongoDBArchive)
.on('error', (err) => {
rej(new GenericMMSError('Unable to open tarball ' + mongoDBArchive + ': ' + err));
})
.pipe(createUnzip())
.on('error', (err) => {
rej(new GenericMMSError('Error during unzip for ' + mongoDBArchive + ': ' + err));
})
.pipe(extract)
.on('error', (err) => {
rej(new GenericMMSError('Error during untar for ' + mongoDBArchive + ': ' + err));
})
.on('finish', res);
});
}
/**
* Extract a .zip archive
* @param mongoDBArchive Archive location
* @param extractPath Directory to extract to
* @param filter Method to determine which files to extract
*/
async extractZip(
mongoDBArchive: string,
extractPath: string,
filter: (file: string) => boolean
): Promise<void> {
log('extractZip');
return new Promise((resolve, reject) => {
yauzl.open(mongoDBArchive, { lazyEntries: true }, (e, zipfile) => {
if (e || !zipfile) {
return reject(e);
}
zipfile.readEntry();
zipfile.on('end', () => resolve());
zipfile.on('entry', (entry) => {
if (!filter(entry.fileName)) {
return zipfile.readEntry();
}
zipfile.openReadStream(entry, (e, r) => {
if (e || !r) {
return reject(e);
}
r.on('end', () => zipfile.readEntry());
r.pipe(
createWriteStream(extractPath, {
mode: 0o775,
})
);
});
});
});
});
}
/**
* Downlaod given httpOptions to tempDownloadLocation, then move it to downloadLocation
* @param httpOptions The httpOptions directly passed to https.get
* @param downloadLocation The location the File should be after the download
* @param tempDownloadLocation The location the File should be while downloading
*/
async httpDownload(
url: URL,
httpOptions: https.RequestOptions,
downloadLocation: string,
tempDownloadLocation: string
): Promise<string> {
log('httpDownload');
const downloadUrl = this.assignDownloadingURL(url);
return new Promise((resolve, reject) => {
log(`httpDownload: trying to download "${downloadUrl}"`);
https
.get(url, httpOptions, (response) => {
if (response.statusCode != 200) {
if (response.statusCode === 403) {
reject(
new Error(
"Status Code is 403 (MongoDB's 404)\n" +
"This means that the requested version-platform combination doesn't exist\n" +
` Used Url: "${downloadUrl}"\n` +
"Try to use different version 'new MongoMemoryServer({ binary: { version: 'X.Y.Z' } })'\n" +
'List of available versions can be found here:\n' +
' https://www.mongodb.org/dl/linux for Linux\n' +
' https://www.mongodb.org/dl/osx for OSX\n' +
' https://www.mongodb.org/dl/win32 for Windows'
)
);
return;
}
reject(new Error('Status Code isnt 200!'));
return;
}
if (typeof response.headers['content-length'] != 'string') {
reject(new Error('Response header "content-length" is empty!'));
return;
}
this.dlProgress.current = 0;
this.dlProgress.length = parseInt(response.headers['content-length'], 10);
this.dlProgress.totalMb = Math.round((this.dlProgress.length / 1048576) * 10) / 10;
const fileStream = createWriteStream(tempDownloadLocation);
response.pipe(fileStream);
fileStream.on('finish', async () => {
if (
this.dlProgress.current < this.dlProgress.length &&
!httpOptions.path?.endsWith('.md5')
) {
reject(
new Error(
`Too small (${this.dlProgress.current} bytes) mongod binary downloaded from ${downloadUrl}`
)
);
return;
}
this.printDownloadProgress({ length: 0 }, true);
fileStream.close();
await fspromises.rename(tempDownloadLocation, downloadLocation);
log(`httpDownload: moved "${tempDownloadLocation}" to "${downloadLocation}"`);
resolve(downloadLocation);
});
response.on('data', (chunk: any) => {
this.printDownloadProgress(chunk);
});
})
.on('error', (e: Error) => {
// log it without having debug enabled
console.error(`Couldnt download "${downloadUrl}"!`, e.message);
reject(e);
});
});
}
/**
* Print the Download Progress to STDOUT
* @param chunk A chunk to get the length
*/
printDownloadProgress(chunk: { length: number }, forcePrint: boolean = false): void {
this.dlProgress.current += chunk.length;
const now = Date.now();
if (now - this.dlProgress.lastPrintedAt < 2000 && !forcePrint) {
return;
}
this.dlProgress.lastPrintedAt = now;
const percentComplete =
Math.round(((100.0 * this.dlProgress.current) / this.dlProgress.length) * 10) / 10;
const mbComplete = Math.round((this.dlProgress.current / 1048576) * 10) / 10;
const crReturn = this.platform === 'win32' ? '\x1b[0G' : '\r';
const message = `Downloading MongoDB "${this.version}": ${percentComplete}% (${mbComplete}mb / ${this.dlProgress.totalMb}mb)${crReturn}`;
if (process.stdout.isTTY) {
// if TTY overwrite last line over and over until finished and clear line to avoid residual characters
clearLine(process.stdout, 0); // this is because "process.stdout.clearLine" does not exist anymore
process.stdout.write(message);
} else {
console.log(message);
}
}
/**
* Helper function to de-duplicate assigning "_downloadingUrl"
*/
assignDownloadingURL(url: URL): string {
this._downloadingUrl = url.href;
return this._downloadingUrl;
}
}
export default MongoBinaryDownload;