Skip to content

Commit e19a738

Browse files
committed
refactor: add sugested improvements for stream pipe handling
1 parent 0a47eed commit e19a738

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { HttpsProxyAgent } from 'https-proxy-agent';
1414
import { promisify } from 'util';
1515
import resolveConfig, { envToBool } from './resolve-config';
1616
import debug from 'debug';
17+
import dedent from 'dedent';
1718

1819
const log = debug('MongoMS:MongoBinaryDownload');
1920

@@ -237,17 +238,26 @@ export default class MongoBinaryDownload {
237238
})
238239
);
239240
}
240-
241241
stream.on('end', () => next());
242242
stream.resume();
243243
});
244244

245245
return new Promise((resolve, reject) => {
246-
extract.on('finish', () => resolve());
247-
extract.on('error', (e) => reject(e));
248246
fs.createReadStream(mongoDBArchive)
247+
.on('error', (err) => {
248+
reject('Unable to open tarball ' + mongoDBArchive + ': ' + err);
249+
})
249250
.pipe(createUnzip())
250-
.pipe(extract);
251+
.on('error', (err) => {
252+
reject('Error during unzip for ' + mongoDBArchive + ': ' + err);
253+
})
254+
.pipe(extract)
255+
.on('error', (err) => {
256+
reject('Error during untar for ' + mongoDBArchive + ': ' + err);
257+
})
258+
.on('finish', (result) => {
259+
resolve(result);
260+
});
251261
});
252262
}
253263

@@ -304,10 +314,10 @@ export default class MongoBinaryDownload {
304314
if (response.statusCode != 200) {
305315
if (response.statusCode === 403) {
306316
reject(
307-
new Error(
308-
"Status Code is 403 (MongoDB's 404)\n" +
309-
'This means that the requested version-platform combination dosnt exist'
310-
)
317+
new Error(dedent`
318+
Status Code is 403 (MongoDB's 404)\n
319+
This means that the requested version-platform combination dosnt exist
320+
`)
311321
);
312322
return;
313323
}
@@ -375,8 +385,7 @@ export default class MongoBinaryDownload {
375385

376386
const crReturn = this.platform === 'win32' ? '\x1b[0G' : '\r';
377387
process.stdout.write(
378-
`Downloading MongoDB ${this.version}: ${percentComplete} % (${mbComplete}mb ` +
379-
`/ ${this.dlProgress.totalMb}mb)${crReturn}`
388+
`Downloading MongoDB ${this.version}: ${percentComplete} % (${mbComplete}mb / ${this.dlProgress.totalMb}mb)${crReturn}`
380389
);
381390
}
382391

0 commit comments

Comments
 (0)