Skip to content

Commit c012c41

Browse files
jmrogTimothyGu
authored andcommitted
Reject when stream accumulation fails (node-fetch#415)
Fixes: node-fetch#414
1 parent 4c4f2f2 commit c012c41

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/body.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,13 @@ function consumeBody() {
257257
}
258258

259259
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+
}
261267
});
262268
});
263269
}

test/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,25 @@ describe('node-fetch', () => {
14271427
});
14281428
});
14291429

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+
});
14301449
});
14311450

14321451
describe('Headers', function () {

0 commit comments

Comments
 (0)