@@ -16,6 +16,7 @@ import { Readable } from 'stream';
16
16
import nv from '@pkgjs/nv' ;
17
17
import { fileURLToPath , URL } from 'url' ;
18
18
import { execFile } from 'child_process' ;
19
+ import { once } from 'events' ;
19
20
20
21
// Download and unpack a tarball containing the code for a specific Node.js version.
21
22
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
99
100
}
100
101
101
102
let tarballStream : Readable ;
102
- let tarballWritePromise : Promise < unknown > ;
103
+ let tarballWritePromise : Promise < unknown > | undefined ;
103
104
if ( hasCachedTarball ) {
104
105
tarballStream = createReadStream ( cachedTarballPath ) ;
105
106
} else {
@@ -135,14 +136,22 @@ async function getNodeSourceForVersion (range: string, dir: string, logger: Logg
135
136
// Streaming unpack. This will create the directory `${dir}/node-${version}`
136
137
// with the Node.js source tarball contents in it.
137
138
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' ) ;
143
153
} )
144
- ) ;
145
- await tarballWritePromise ;
154
+ ] ) ;
146
155
} catch ( err ) {
147
156
if ( retries > 0 ) {
148
157
logger . stepFailed ( err ) ;
0 commit comments