@@ -31,6 +31,9 @@ interface HttpDownloadOptions {
3131 agent : HttpsProxyAgent | undefined ;
3232}
3333
34+ /**
35+ * Download and extract the "mongod" binary
36+ */
3437export default class MongoBinaryDownload {
3538 dlProgress : DownloadProgressT ;
3639 _downloadingUrl ?: string ;
@@ -160,7 +163,7 @@ export default class MongoBinaryDownload {
160163
161164 const downloadLocation = path . resolve ( this . downloadDir , filename ) ;
162165 const tempDownloadLocation = path . resolve ( this . downloadDir , `${ filename } .downloading` ) ;
163- log ( `Downloading${ proxy ? ` via proxy ${ proxy } ` : '' } :` , downloadUrl ) ;
166+ log ( `Downloading${ proxy ? ` via proxy ${ proxy } ` : '' } : " ${ downloadUrl } "` ) ;
164167 const downloadedFile = await this . httpDownload (
165168 downloadOptions ,
166169 downloadLocation ,
@@ -225,45 +228,62 @@ export default class MongoBinaryDownload {
225228 return new Promise ( ( resolve , reject ) => {
226229 const fileStream = fs . createWriteStream ( tempDownloadLocation ) ;
227230
228- const req = https . get ( httpOptions , ( response : any ) => {
229- this . dlProgress . current = 0 ;
230- this . dlProgress . length = parseInt ( response . headers [ 'content-length' ] , 10 ) ;
231- this . dlProgress . totalMb = Math . round ( ( this . dlProgress . length / 1048576 ) * 10 ) / 10 ;
232-
233- response . pipe ( fileStream ) ;
234-
235- fileStream . on ( 'finish' , async ( ) => {
236- if (
237- this . dlProgress . current < this . dlProgress . length &&
238- ! httpOptions . path . endsWith ( '.md5' )
239- ) {
240- const downloadUrl =
241- this . _downloadingUrl || `https://${ httpOptions . hostname } /${ httpOptions . path } ` ;
242- reject (
243- new Error (
244- `Too small (${ this . dlProgress . current } bytes) mongod binary downloaded from ${ downloadUrl } `
245- )
246- ) ;
231+ https
232+ . get ( httpOptions , ( response ) => {
233+ if ( response . statusCode != 200 ) {
234+ if ( response . statusCode === 403 ) {
235+ reject (
236+ new Error (
237+ "Status Code is 403 (MongoDB's 404)\n" +
238+ 'This means that the requested version-platform combination dosnt exist'
239+ )
240+ ) ;
241+ return ;
242+ }
243+ reject ( new Error ( 'Status Code isnt 200!' ) ) ;
247244 return ;
248245 }
249-
250- fileStream . close ( ) ;
251- await promisify ( fs . rename ) ( tempDownloadLocation , downloadLocation ) ;
252- log ( `renamed ${ tempDownloadLocation } to ${ downloadLocation } ` ) ;
253-
254- resolve ( downloadLocation ) ;
255- } ) ;
256-
257- response . on ( 'data' , ( chunk : any ) => {
258- this . printDownloadProgress ( chunk ) ;
259- } ) ;
260-
261- req . on ( 'error' , ( e : Error ) => {
246+ if ( typeof response . headers [ 'content-length' ] != 'string' ) {
247+ reject ( new Error ( 'Response header "content-length" is empty!' ) ) ;
248+ return ;
249+ }
250+ this . dlProgress . current = 0 ;
251+ this . dlProgress . length = parseInt ( response . headers [ 'content-length' ] , 10 ) ;
252+ this . dlProgress . totalMb = Math . round ( ( this . dlProgress . length / 1048576 ) * 10 ) / 10 ;
253+
254+ response . pipe ( fileStream ) ;
255+
256+ fileStream . on ( 'finish' , async ( ) => {
257+ if (
258+ this . dlProgress . current < this . dlProgress . length &&
259+ ! httpOptions . path . endsWith ( '.md5' )
260+ ) {
261+ const downloadUrl =
262+ this . _downloadingUrl || `https://${ httpOptions . hostname } /${ httpOptions . path } ` ;
263+ reject (
264+ new Error (
265+ `Too small (${ this . dlProgress . current } bytes) mongod binary downloaded from ${ downloadUrl } `
266+ )
267+ ) ;
268+ return ;
269+ }
270+
271+ fileStream . close ( ) ;
272+ await promisify ( fs . rename ) ( tempDownloadLocation , downloadLocation ) ;
273+ log ( `moved ${ tempDownloadLocation } to ${ downloadLocation } ` ) ;
274+
275+ resolve ( downloadLocation ) ;
276+ } ) ;
277+
278+ response . on ( 'data' , ( chunk : any ) => {
279+ this . printDownloadProgress ( chunk ) ;
280+ } ) ;
281+ } )
282+ . on ( 'error' , ( e : Error ) => {
262283 // log it without having debug enabled
263284 console . error ( `Couldnt download ${ httpOptions . path } !` , e . message ) ;
264285 reject ( e ) ;
265286 } ) ;
266- } ) ;
267287 } ) ;
268288 }
269289
0 commit comments