Reject when stream accumulation fails (#415)

Fixes: #414
This commit is contained in:
Jason Rogers 2018-03-22 21:38:03 -04:00 committed by Timothy Gu
parent 4c4f2f29e5
commit c012c4116b
2 changed files with 26 additions and 1 deletions

View File

@ -257,7 +257,13 @@ function consumeBody() {
}
clearTimeout(resTimeout);
resolve(Buffer.concat(accum));
try {
resolve(Buffer.concat(accum));
} catch (err) {
// handle streams that have accumulated too much data (issue #414)
reject(new FetchError(`Could not create Buffer from response body for ${this.url}: ${err.message}`, 'system', err));
}
});
});
}

View File

@ -1427,6 +1427,25 @@ describe('node-fetch', () => {
});
});
// issue #414
it('should reject if attempt to accumulate body stream throws', function () {
let body = resumer().queue('a=1').end();
body = body.pipe(new stream.PassThrough());
const res = new Response(body);
const bufferConcat = Buffer.concat;
const restoreBufferConcat = () => Buffer.concat = bufferConcat;
Buffer.concat = () => { throw new Error('embedded error'); };
const textPromise = res.text();
// Ensure that `Buffer.concat` is always restored:
textPromise.then(restoreBufferConcat, restoreBufferConcat);
return expect(textPromise).to.eventually.be.rejected
.and.be.an.instanceOf(FetchError)
.and.include({ type: 'system' })
.and.have.property('message').that.includes('Could not create Buffer')
.and.that.includes('embedded error');
});
});
describe('Headers', function () {