@@ -121,7 +121,14 @@ export class Binary {
121
121
console . log ( `Downloading release from ${ getCompressedBinaryUrl ( ) } ` ) ;
122
122
try {
123
123
const res = await axios ( { url : getCompressedBinaryUrl ( ) , responseType : 'stream' } ) ;
124
- await pipeline ( res . data , createGunzip ( ) , this . extract ( ) ) ;
124
+ // An array to collect a promises from nested pipeline that extracts tar content to a file.
125
+ // The tar file to actual file on disk streaming is kicked off by asynchronous events
126
+ // of extract step. So top level pipeline may complete before streaming is completed.
127
+ // We capture a Promise from that process to await it before proceeding,
128
+ // so that we don't call spawnSync prematurely before content streaming completes.
129
+ const extractPromiseCollector : Array < Promise < void > > = [ ] ;
130
+ await pipeline ( res . data , createGunzip ( ) , this . extract ( extractPromiseCollector ) ) ;
131
+ await Promise . all ( extractPromiseCollector ) ;
125
132
126
133
console . log ( 'amplify has been installed!' ) ;
127
134
spawnSync ( this . binaryPath , [ 'version' ] , { cwd : process . cwd ( ) , stdio : 'inherit' } ) ;
@@ -151,29 +158,23 @@ export class Binary {
151
158
*
152
159
* @returns tar.Extract
153
160
*/
154
- private extract ( ) : tar . Extract {
161
+ private extract ( extractPromiseCollector : Array < Promise < void > > ) : tar . Extract {
155
162
const extract = tar . extract ( ) ;
156
- const chunks : Uint8Array [ ] = [ ] ;
157
163
extract . on ( 'entry' , ( header , extractStream , next ) => {
158
164
if ( header . type === 'file' ) {
159
- extractStream . on ( 'data' , ( chunk ) => {
160
- chunks . push ( chunk ) ;
165
+ const fileWriteStream = fs . createWriteStream ( this . binaryPath , {
166
+ mode : 0o755 ,
161
167
} ) ;
168
+ // pipe tar entry to file stream
169
+ // and collect a promise so that top level process can await it
170
+ extractPromiseCollector . push ( pipeline ( extractStream , fileWriteStream ) ) ;
162
171
}
163
172
extractStream . on ( 'end' , ( ) => {
164
173
next ( ) ;
165
174
} ) ;
166
175
167
176
extractStream . resume ( ) ;
168
177
} ) ;
169
- extract . on ( 'finish' , ( ) => {
170
- if ( chunks . length ) {
171
- const data = Buffer . concat ( chunks ) ;
172
- fs . writeFileSync ( this . binaryPath , data , {
173
- mode : 0o755 ,
174
- } ) ;
175
- }
176
- } ) ;
177
178
return extract ;
178
179
}
179
180
}
0 commit comments