Skip to content

Commit 3411ce8

Browse files
authored
chore: throw error if encountering beforeExit event during download MONGOSH-1595 (#48)
1 parent 92c0e50 commit 3411ce8

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Readable } from 'stream';
1616
import nv from '@pkgjs/nv';
1717
import { fileURLToPath, URL } from 'url';
1818
import { execFile } from 'child_process';
19+
import { once } from 'events';
1920

2021
// Download and unpack a tarball containing the code for a specific Node.js version.
2122
async function getNodeSourceForVersion (range: string, dir: string, logger: Logger, retries = 2): Promise<string> {
@@ -99,7 +100,7 @@ async function getNodeSourceForVersion (range: string, dir: string, logger: Logg
99100
}
100101

101102
let tarballStream: Readable;
102-
let tarballWritePromise: Promise<unknown>;
103+
let tarballWritePromise: Promise<unknown> | undefined;
103104
if (hasCachedTarball) {
104105
tarballStream = createReadStream(cachedTarballPath);
105106
} else {
@@ -135,14 +136,22 @@ async function getNodeSourceForVersion (range: string, dir: string, logger: Logg
135136
// Streaming unpack. This will create the directory `${dir}/node-${version}`
136137
// with the Node.js source tarball contents in it.
137138
try {
138-
await pipeline(
139-
tarballStream,
140-
zlib.createGunzip(),
141-
tar.x({
142-
cwd: dir
139+
await Promise.race([
140+
Promise.all([
141+
pipeline(
142+
tarballStream,
143+
zlib.createGunzip(),
144+
tar.x({
145+
cwd: dir
146+
})
147+
),
148+
tarballWritePromise
149+
]),
150+
// Unclear why this can happen, but it looks in CI like it does
151+
once(process, 'beforeExit').then(() => {
152+
throw new Error('premature exit from the event loop');
143153
})
144-
);
145-
await tarballWritePromise;
154+
]);
146155
} catch (err) {
147156
if (retries > 0) {
148157
logger.stepFailed(err);

0 commit comments

Comments
 (0)