From 06a2d3d7d8d4fcc6898b6364d1a18ca1d407092b Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Thu, 3 Jun 2021 01:37:38 +0200 Subject: [PATCH] Fix a Node.js 16 bug that hangs Got streams --- source/core/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index 553a89a4f..88b886620 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -326,7 +326,10 @@ export default class Request extends Duplex implements RequestEvents { // Node.js parser is really weird. // It emits post-request Parse Errors on the same instance as previous request. WTF. // Therefore we need to check if it has been destroyed as well. - if (response && !response.rawBody && !this._request?.socket?.destroyed) { + // + // Furthermore, Node.js 16 `response.destroy()` doesn't immediately destroy the socket, + // but makes the response unreadable. So we additionally need to check `response.readable`. + if (response?.readable && !response.rawBody && !this._request?.socket?.destroyed) { // @types/node has incorrect typings. `setEncoding` accepts `null` as well. response.setEncoding(this.readableEncoding!);