22/* eslint-disable class-methods-use-this */
33
44import os from 'os' ;
5+ import url from 'url' ;
56import path from 'path' ;
67import fs from 'fs-extra' ;
7- import request from 'request-promise' ;
88import md5File from 'md5-file' ;
99import https from 'https' ;
10+ import HttpsProxyAgent from 'https-proxy-agent' ;
1011import decompress from 'decompress' ;
1112import MongoBinaryDownloadUrl from './MongoBinaryDownloadUrl' ;
1213
@@ -64,7 +65,7 @@ export default class MongoBinaryDownload {
6465 return mongodPath ;
6566 }
6667
67- const mongoDBArchive = await this . download ( ) ;
68+ const mongoDBArchive = await this . startDownload ( ) ;
6869 await this . extract ( mongoDBArchive ) ;
6970 fs . unlinkSync ( mongoDBArchive ) ;
7071
@@ -75,34 +76,66 @@ export default class MongoBinaryDownload {
7576 throw new Error ( `Cannot find downloaded mongod binary by path ${ mongodPath } ` ) ;
7677 }
7778
78- async download (): Promise< string > {
79+ async startDownload (): Promise< string > {
7980 const mbdUrl = new MongoBinaryDownloadUrl ( {
8081 platform : this . platform ,
8182 arch : this . arch ,
8283 version : this . version ,
8384 } ) ;
8485
8586 await fs . ensureDir ( this . downloadDir ) ;
86- const url = await mbdUrl . getDownloadUrl ( ) ;
87- const archName = await mbdUrl . getArchiveName ( ) ;
88- const downloadLocation = path . resolve ( this . downloadDir , archName ) ;
89- console . log ( 'Downloading MongoDB:' , url ) ;
90- const tempDownloadLocation = path . resolve ( this . downloadDir , `${ archName } .downloading` ) ;
91- const mongoDBArchive = await this . httpDownload ( url , downloadLocation , tempDownloadLocation ) ;
92- const md5Remote = await this . downloadMD5 ( `${ url } .md5` ) ;
87+
88+ const downloadUrl = await mbdUrl . getDownloadUrl ( ) ;
89+ const mongoDBArchive = await this . download ( downloadUrl ) ;
90+
91+ const mongoDBArchiveMd5 = await this . download ( `${ downloadUrl } .md5` ) ;
92+ await this . checkMd5 ( mongoDBArchiveMd5 , mongoDBArchive ) ;
93+
94+ return mongoDBArchive ;
95+ }
96+
97+ async checkMd5(mongoDBArchiveMd5: string, mongoDBArchive: string) {
98+ const signatureContent = ( await fs . readFile ( mongoDBArchiveMd5 ) ) . toString ( 'UTF-8' ) ;
99+ const md5Remote = signatureContent . match ( / ( .* ?) \s / ) [ 1 ] ;
93100 const md5Local = md5File . sync ( mongoDBArchive ) ;
94101 if ( md5Remote !== md5Local ) {
95102 throw new Error ( 'MongoBinaryDownload: md5 check is failed' ) ;
96103 }
97- return mongoDBArchive ;
98104 }
99105
100- async downloadMD5(md5url: string): Promise< string > {
101- const signatureContent = await request ( md5url ) ;
102- this . debug ( `getDownloadMD5Hash content: ${ signatureContent } ` ) ;
103- const signature = signatureContent . match ( / ( .* ?) \s / ) [ 1 ] ;
104- this . debug ( `getDownloadMD5Hash extracted signature: ${ signature } ` ) ;
105- return signature ;
106+ async download(downloadUrl: string) {
107+ const proxy =
108+ process . env [ 'yarn_https-proxy' ] ||
109+ process . env . yarn_proxy ||
110+ process . env [ 'npm_config_https-proxy' ] ||
111+ process . env . npm_config_proxy ||
112+ process . env . https_proxy ||
113+ process . env . http_proxy ;
114+
115+ const urlObject = url . parse ( downloadUrl ) ;
116+
117+ const downloadOptions = {
118+ hostname : urlObject . hostname ,
119+ port : urlObject . port || 443 ,
120+ path : urlObject . path ,
121+ method : 'GET' ,
122+ agent : proxy ? new HttpsProxyAgent ( proxy ) : undefined ,
123+ } ;
124+
125+ const filename = ( urlObject . pathname || '' ) . split ( '/' ) . pop ( ) ;
126+ if ( ! filename ) {
127+ throw new Error ( `MongoBinaryDownload: missing filename for url ${ downloadUrl } ` ) ;
128+ }
129+
130+ const downloadLocation = path . resolve ( this . downloadDir , filename ) ;
131+ const tempDownloadLocation = path . resolve ( this . downloadDir , `${ filename } .downloading` ) ;
132+ console . log ( `Downloading${ proxy ? ` via proxy ${ proxy } ` : '' } :` , downloadUrl ) ;
133+ const downloadedFile = await this . httpDownload (
134+ downloadOptions ,
135+ downloadLocation ,
136+ tempDownloadLocation
137+ ) ;
138+ return downloadedFile ;
106139 }
107140
108141 async extract(mongoDBArchive: string): Promise< string > {
0 commit comments