File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -257,7 +257,13 @@ function consumeBody() {
257
257
}
258
258
259
259
clearTimeout ( resTimeout ) ;
260
- resolve ( Buffer . concat ( accum ) ) ;
260
+
261
+ try {
262
+ resolve ( Buffer . concat ( accum ) ) ;
263
+ } catch ( err ) {
264
+ // handle streams that have accumulated too much data (issue #414)
265
+ reject ( new FetchError ( `Could not create Buffer from response body for ${ this . url } : ${ err . message } ` , 'system' , err ) ) ;
266
+ }
261
267
} ) ;
262
268
} ) ;
263
269
}
Original file line number Diff line number Diff line change @@ -1427,6 +1427,25 @@ describe('node-fetch', () => {
1427
1427
} ) ;
1428
1428
} ) ;
1429
1429
1430
+ // issue #414
1431
+ it ( 'should reject if attempt to accumulate body stream throws' , function ( ) {
1432
+ let body = resumer ( ) . queue ( 'a=1' ) . end ( ) ;
1433
+ body = body . pipe ( new stream . PassThrough ( ) ) ;
1434
+ const res = new Response ( body ) ;
1435
+ const bufferConcat = Buffer . concat ;
1436
+ const restoreBufferConcat = ( ) => Buffer . concat = bufferConcat ;
1437
+ Buffer . concat = ( ) => { throw new Error ( 'embedded error' ) ; } ;
1438
+
1439
+ const textPromise = res . text ( ) ;
1440
+ // Ensure that `Buffer.concat` is always restored:
1441
+ textPromise . then ( restoreBufferConcat , restoreBufferConcat ) ;
1442
+
1443
+ return expect ( textPromise ) . to . eventually . be . rejected
1444
+ . and . be . an . instanceOf ( FetchError )
1445
+ . and . include ( { type : 'system' } )
1446
+ . and . have . property ( 'message' ) . that . includes ( 'Could not create Buffer' )
1447
+ . and . that . includes ( 'embedded error' ) ;
1448
+ } ) ;
1430
1449
} ) ;
1431
1450
1432
1451
describe ( 'Headers' , function ( ) {
You can’t perform that action at this time.
0 commit comments