From 043a5fcfc82884155bcb2afba2a82333b2f29c02 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Sat, 7 May 2022 16:56:37 -0500 Subject: [PATCH 1/5] Fix leaking listeners (#1295) (#1474) Since 8eeeec1 it seems listeners have been leaking on keep-alive sockets. Apparently abort() has been deprecated since v17, using close() the onSocketClose listener is properly removed, and by creating a new onData function I'm able to remove the 'data' listener too. --- src/index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index f5f39844e..513b05225 100644 --- a/src/index.js +++ b/src/index.js @@ -387,13 +387,7 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) { } }; - socket.prependListener('close', onSocketClose); - - request.on('abort', () => { - socket.removeListener('close', onSocketClose); - }); - - socket.on('data', buf => { + const onData = buf => { properLastChunkReceived = Buffer.compare(buf.slice(-5), LAST_CHUNK) === 0; // Sometimes final 0-length chunk and end of message code are in separate packets @@ -405,6 +399,14 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) { } previousChunk = buf; + }; + + socket.prependListener('close', onSocketClose); + socket.on('data', onData); + + request.on('close', () => { + socket.removeListener('close', onSocketClose); + socket.removeListener('data', onData); }); }); } From 6ae9c7648157ecdf2bc5d3fce2ed61c2256d07e0 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Fri, 20 May 2022 05:55:15 -0700 Subject: [PATCH 2/5] docs(readme): response.clone() is not async (#1560) source code shows response is a sync call to a new Response constructor: https://github.com/node-fetch/node-fetch/blob/3944f24770b442e519eaff758adffc9ca0092bdb/src/response.js#L84-L101 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 37fd608f1..8ab2e804b 100644 --- a/README.md +++ b/README.md @@ -594,7 +594,7 @@ The recommended way to fix this problem is to resolve cloned response in paralle import fetch from 'node-fetch'; const response = await fetch('https://example.com'); -const r1 = await response.clone(); +const r1 = response.clone(); const results = await Promise.all([response.json(), r1.text()]); From 0f122b882461d0b212ba75dd92f4606e1cb12812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Fri, 27 May 2022 23:31:17 +0200 Subject: [PATCH 3/5] docs: fix formdata code example (#1562) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ab2e804b..07e0c51ba 100644 --- a/README.md +++ b/README.md @@ -428,7 +428,7 @@ import fetch, { FormData, File, fileFrom } from 'node-fetch' const httpbin = 'https://httpbin.org/post' const formData = new FormData() const binary = new Uint8Array([ 97, 98, 99 ]) -const abc = new File([binary], 'abc.txt'), { type: 'text/plain' }) +const abc = new File([binary], 'abc.txt', { type: 'text/plain' }) formData.set('greeting', 'Hello, world!') formData.set('file-upload', abc, 'new name.txt') From a92b5d5cf4457c2da95d8404b08cfd06a426a2fa Mon Sep 17 00:00:00 2001 From: Tom <53571657+tmclo@users.noreply.github.com> Date: Wed, 1 Jun 2022 20:43:48 +0100 Subject: [PATCH 4/5] fix: use space in accept-encoding values (#1572) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix for issue #1571 * Update README.md Co-authored-by: Jimmy Wärting --- README.md | 2 +- src/request.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 07e0c51ba..fce3d8f13 100644 --- a/README.md +++ b/README.md @@ -537,7 +537,7 @@ If no values are set, the following request headers will be sent automatically: | Header | Value | | ------------------- | ------------------------------------------------------ | -| `Accept-Encoding` | `gzip,deflate,br` _(when `options.compress === true`)_ | +| `Accept-Encoding` | `gzip, deflate, br` (when `options.compress === true`) | | `Accept` | `*/*` | | `Connection` | `close` _(when no `options.agent` is present)_ | | `Content-Length` | _(automatically calculated, if possible)_ | diff --git a/src/request.js b/src/request.js index a63f7e606..38f33df33 100644 --- a/src/request.js +++ b/src/request.js @@ -280,7 +280,7 @@ export const getNodeRequestOptions = request => { // HTTP-network-or-cache fetch step 2.15 if (request.compress && !headers.has('Accept-Encoding')) { - headers.set('Accept-Encoding', 'gzip,deflate,br'); + headers.set('Accept-Encoding', 'gzip, deflate, br'); } let {agent} = request; From 1c5ed6b981e6c5dd28bd50f5ab5418e5bd262b99 Mon Sep 17 00:00:00 2001 From: Krisi Ves Date: Thu, 9 Jun 2022 03:55:02 -0700 Subject: [PATCH 5/5] fix: undefined reference to response.body when aborted (#1578) --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 513b05225..82efc9888 100644 --- a/src/index.js +++ b/src/index.js @@ -110,7 +110,9 @@ export default async function fetch(url, options_) { }); fixResponseChunkedTransferBadEnding(request_, error => { - response.body.destroy(error); + if (response && response.body) { + response.body.destroy(error); + } }); /* c8 ignore next 18 */