@@ -217,7 +217,7 @@ Please double check that your authentication token is correct. Due to security r
217
217
218
218
async downloadToBuffer ( url : URL , options : DownloadOptions ) : Promise < Buffer > {
219
219
return await options . cancellationToken . createPromise < Buffer > ( ( resolve , reject , onCancel ) => {
220
- let result : Buffer | null = null
220
+ const responseChunks : Buffer [ ] = [ ]
221
221
const requestOptions = {
222
222
headers : options . headers || undefined ,
223
223
// because PrivateGitHubProvider requires HttpExecutor.prepareRedirectUrlOptions logic, so, we need to redirect manually
@@ -233,46 +233,23 @@ Please double check that your authentication token is correct. Due to security r
233
233
onCancel,
234
234
callback : error => {
235
235
if ( error == null ) {
236
- resolve ( result ! )
236
+ resolve ( Buffer . concat ( responseChunks ) )
237
237
} else {
238
238
reject ( error )
239
239
}
240
240
} ,
241
241
responseHandler : ( response , callback ) => {
242
- const contentLength = safeGetHeader ( response , "content-length" )
243
- let position = - 1
244
- if ( contentLength != null ) {
245
- const size = parseInt ( contentLength , 10 )
246
- if ( size > 0 ) {
247
- if ( size > 524288000 ) {
248
- callback ( new Error ( "Maximum allowed size is 500 MB" ) )
249
- return
250
- }
251
-
252
- result = Buffer . alloc ( size )
253
- position = 0
254
- }
255
- }
242
+ let receivedLength = 0
256
243
response . on ( "data" , ( chunk : Buffer ) => {
257
- if ( position !== - 1 ) {
258
- chunk . copy ( result ! , position )
259
- position += chunk . length
260
- } else if ( result == null ) {
261
- result = chunk
262
- } else {
263
- if ( result . length > 524288000 ) {
264
- callback ( new Error ( "Maximum allowed size is 500 MB" ) )
265
- return
266
- }
267
- result = Buffer . concat ( [ result , chunk ] )
244
+ receivedLength += chunk . length
245
+ if ( receivedLength > 524288000 ) {
246
+ callback ( new Error ( "Maximum allowed size is 500 MB" ) )
247
+ return
268
248
}
249
+ responseChunks . push ( chunk )
269
250
} )
270
251
response . on ( "end" , ( ) => {
271
- if ( result != null && position !== - 1 && position !== result . length ) {
272
- callback ( new Error ( `Received data length ${ position } is not equal to expected ${ result . length } ` ) )
273
- } else {
274
- callback ( null )
275
- }
252
+ callback ( null )
276
253
} )
277
254
} ,
278
255
} ,
0 commit comments