Skip to content

Commit

Permalink
fix(MongoBinaryDownload): handle Status Codes other than 200 (#273)
Browse files Browse the repository at this point in the history
* Handle Status Codes other than 200
Add some TSDoc

fixes #226

* Remove Debug log
  • Loading branch information
hasezoey committed Mar 3, 2020
1 parent dde00c9 commit 3d68233
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
88 changes: 54 additions & 34 deletions packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts
Expand Up @@ -31,6 +31,9 @@ interface HttpDownloadOptions {
agent: HttpsProxyAgent | undefined;
}

/**
* Download and extract the "mongod" binary
*/
export default class MongoBinaryDownload {
dlProgress: DownloadProgressT;
_downloadingUrl?: string;
Expand Down Expand Up @@ -160,7 +163,7 @@ export default class MongoBinaryDownload {

const downloadLocation = path.resolve(this.downloadDir, filename);
const tempDownloadLocation = path.resolve(this.downloadDir, `${filename}.downloading`);
log(`Downloading${proxy ? ` via proxy ${proxy}` : ''}:`, downloadUrl);
log(`Downloading${proxy ? ` via proxy ${proxy}` : ''}: "${downloadUrl}"`);
const downloadedFile = await this.httpDownload(
downloadOptions,
downloadLocation,
Expand Down Expand Up @@ -225,45 +228,62 @@ export default class MongoBinaryDownload {
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(tempDownloadLocation);

const req = https.get(httpOptions, (response: any) => {
this.dlProgress.current = 0;
this.dlProgress.length = parseInt(response.headers['content-length'], 10);
this.dlProgress.totalMb = Math.round((this.dlProgress.length / 1048576) * 10) / 10;

response.pipe(fileStream);

fileStream.on('finish', async () => {
if (
this.dlProgress.current < this.dlProgress.length &&
!httpOptions.path.endsWith('.md5')
) {
const downloadUrl =
this._downloadingUrl || `https://${httpOptions.hostname}/${httpOptions.path}`;
reject(
new Error(
`Too small (${this.dlProgress.current} bytes) mongod binary downloaded from ${downloadUrl}`
)
);
https
.get(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 dosnt exist'
)
);
return;
}
reject(new Error('Status Code isnt 200!'));
return;
}

fileStream.close();
await promisify(fs.rename)(tempDownloadLocation, downloadLocation);
log(`renamed ${tempDownloadLocation} to ${downloadLocation}`);

resolve(downloadLocation);
});

response.on('data', (chunk: any) => {
this.printDownloadProgress(chunk);
});

req.on('error', (e: Error) => {
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;

response.pipe(fileStream);

fileStream.on('finish', async () => {
if (
this.dlProgress.current < this.dlProgress.length &&
!httpOptions.path.endsWith('.md5')
) {
const downloadUrl =
this._downloadingUrl || `https://${httpOptions.hostname}/${httpOptions.path}`;
reject(
new Error(
`Too small (${this.dlProgress.current} bytes) mongod binary downloaded from ${downloadUrl}`
)
);
return;
}

fileStream.close();
await promisify(fs.rename)(tempDownloadLocation, downloadLocation);
log(`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 ${httpOptions.path}!`, e.message);
reject(e);
});
});
});
}

Expand Down
Expand Up @@ -12,6 +12,9 @@ export interface MongoBinaryDownloadUrlOpts {
os?: AnyOS;
}

/**
* Download URL generator
*/
export default class MongoBinaryDownloadUrl {
platform: string;
arch: string;
Expand Down

0 comments on commit 3d68233

Please sign in to comment.