@@ -79,52 +79,55 @@ export const buffer = (targetPath: string, filePath: string): Promise<Buffer> =>
79
79
} ;
80
80
81
81
const extractAssets = async ( tarPath : string , match : RegExp , callback : ( path : string , data : Buffer ) => void ) : Promise < void > => {
82
- const buffer = await util . promisify ( fs . readFile ) ( tarPath ) ;
83
- return new Promise < void > ( async ( resolve , reject ) : Promise < void > => {
82
+ return new Promise < void > ( ( resolve , reject ) : void => {
84
83
const extractor = tarStream . extract ( ) ;
85
- extractor . once ( "error" , reject ) ;
84
+ const fail = ( error : Error ) => {
85
+ extractor . destroy ( ) ;
86
+ reject ( error ) ;
87
+ } ;
88
+ extractor . once ( "error" , fail ) ;
86
89
extractor . on ( "entry" , async ( header , stream , next ) => {
87
90
const name = header . name ;
88
91
if ( match . test ( name ) ) {
89
92
extractData ( stream ) . then ( ( data ) => {
90
93
callback ( name , data ) ;
91
94
next ( ) ;
92
- } ) . catch ( reject ) ;
93
- stream . resume ( ) ;
95
+ } ) . catch ( fail ) ;
94
96
} else {
95
97
stream . on ( "end" , ( ) => next ( ) ) ;
96
- stream . resume ( ) ;
98
+ stream . resume ( ) ; // Just drain it.
97
99
}
98
100
} ) ;
99
101
extractor . on ( "finish" , resolve ) ;
100
- extractor . write ( buffer ) ;
101
- extractor . end ( ) ;
102
+ fs . createReadStream ( tarPath ) . pipe ( extractor ) ;
102
103
} ) ;
103
104
} ;
104
105
105
106
const extractData = ( stream : NodeJS . ReadableStream ) : Promise < Buffer > => {
106
107
return new Promise ( ( resolve , reject ) : void => {
107
108
const fileData : Buffer [ ] = [ ] ;
108
- stream . on ( "data" , ( data ) => fileData . push ( data ) ) ;
109
- stream . on ( "end" , ( ) => resolve ( Buffer . concat ( fileData ) ) ) ;
110
109
stream . on ( "error" , reject ) ;
110
+ stream . on ( "end" , ( ) => resolve ( Buffer . concat ( fileData ) ) ) ;
111
+ stream . on ( "data" , ( data ) => fileData . push ( data ) ) ;
111
112
} ) ;
112
113
} ;
113
114
114
115
const extractTar = async ( tarPath : string , targetPath : string , options : IExtractOptions = { } , token : CancellationToken ) : Promise < void > => {
115
- const buffer = await util . promisify ( fs . readFile ) ( tarPath ) ;
116
- return new Promise < void > ( async ( resolve , reject ) : Promise < void > => {
116
+ return new Promise < void > ( ( resolve , reject ) : void => {
117
117
const sourcePathRegex = new RegExp ( options . sourcePath ? `^${ options . sourcePath } ` : "" ) ;
118
118
const extractor = tarStream . extract ( ) ;
119
- extractor . once ( "error" , reject ) ;
119
+ const fail = ( error : Error ) => {
120
+ extractor . destroy ( ) ;
121
+ reject ( error ) ;
122
+ } ;
123
+ extractor . once ( "error" , fail ) ;
120
124
extractor . on ( "entry" , async ( header , stream , next ) => {
121
- const rawName = path . normalize ( header . name ) ;
122
-
123
125
const nextEntry = ( ) : void => {
126
+ stream . on ( "end" , ( ) => next ( ) ) ;
124
127
stream . resume ( ) ;
125
- next ( ) ;
126
128
} ;
127
129
130
+ const rawName = path . normalize ( header . name ) ;
128
131
if ( token . isCancellationRequested || ! sourcePathRegex . test ( rawName ) ) {
129
132
return nextEntry ( ) ;
130
133
}
@@ -138,20 +141,18 @@ const extractTar = async (tarPath: string, targetPath: string, options: IExtract
138
141
const dirName = path . dirname ( fileName ) ;
139
142
const targetDirName = path . join ( targetPath , dirName ) ;
140
143
if ( targetDirName . indexOf ( targetPath ) !== 0 ) {
141
- return reject ( nls . localize ( "invalid file" , "Error extracting {0}. Invalid file." , fileName ) ) ;
144
+ return fail ( new Error ( nls . localize ( "invalid file" , "Error extracting {0}. Invalid file." , fileName ) ) ) ;
142
145
}
143
146
144
- return mkdirp ( targetDirName , undefined , token ) . then ( ( ) => {
145
- const fstream = fs . createWriteStream ( targetFileName , { mode : header . mode } ) ;
146
- fstream . once ( "close" , ( ) => next ( ) ) ;
147
- fstream . once ( "error" , reject ) ;
148
- stream . pipe ( fstream ) ;
149
- stream . resume ( ) ;
150
- } ) ;
147
+ await mkdirp ( targetDirName , undefined , token ) ;
148
+
149
+ const fstream = fs . createWriteStream ( targetFileName , { mode : header . mode } ) ;
150
+ fstream . once ( "close" , ( ) => next ( ) ) ;
151
+ fstream . once ( "error" , fail ) ;
152
+ stream . pipe ( fstream ) ;
151
153
} ) ;
152
154
extractor . once ( "finish" , resolve ) ;
153
- extractor . write ( buffer ) ;
154
- extractor . end ( ) ;
155
+ fs . createReadStream ( tarPath ) . pipe ( extractor ) ;
155
156
} ) ;
156
157
} ;
157
158
0 commit comments