Skip to content

Commit 77df9c3

Browse files
committedApr 13, 2021
Prevent uncaught ParseErrors on initial successful response
Fixes #1527
1 parent f455c7c commit 77df9c3

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
 

‎source/core/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
330330
const typedError = error as RequestError;
331331

332332
void (async () => {
333-
if (response && !response.rawBody) {
333+
// Node.js parser is really weird.
334+
// It emits post-request Parse Errors on the same instance as previous request. WTF.
335+
// Therefore we need to check if it has been destroyed as well.
336+
if (response && !response.rawBody && !this._request?.destroyed) {
334337
// @types/node has incorrect typings. `setEncoding` accepts `null` as well.
335338
response.setEncoding(this.readableEncoding!);
336339

‎test/error.ts

+25
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,31 @@ test('no uncaught parse errors', async t => {
254254
await close();
255255
});
256256

257+
test('no uncaught parse errors #2', async t => {
258+
const server = net.createServer();
259+
260+
const listen = promisify(server.listen.bind(server));
261+
const close = promisify(server.close.bind(server));
262+
263+
await listen();
264+
265+
server.on('connection', socket => {
266+
socket.resume();
267+
socket.write([
268+
'HTTP/1.1 200 OK',
269+
'content-length: 1',
270+
'',
271+
'0a'
272+
].join('\r\n'));
273+
});
274+
275+
await t.throwsAsync(got(`http://localhost:${(server.address() as net.AddressInfo).port}`), {
276+
message: /^Parse Error/
277+
});
278+
279+
await close();
280+
});
281+
257282
// Fails randomly on Node 10:
258283
// Blocked by https://github.com/istanbuljs/nyc/issues/619
259284
// eslint-disable-next-line ava/no-skip-test

0 commit comments

Comments
 (0)
Please sign in to comment.