From d49b494b3ebf3ca32109ca410934a04430122359 Mon Sep 17 00:00:00 2001 From: David Edey Date: Fri, 21 Apr 2023 04:12:29 +0100 Subject: [PATCH] fix: Remove the default connection close header. Instead, we rely on the underlying http implementation in Node.js to handle this, as per the documentation at https://nodejs.org/api/http.html#new-agentoptions This fixes #1735 and likely replaces #1473 The original change introducing this provided no clear motivation for the override, and the implementation has since been changed to disable this header when an agent is provided, so I think there is sufficient evidence that removing this is the correct behaviour. https://github.com/node-fetch/node-fetch/commit/af21ae6c1c964cd40e48e974af56ea2e1361304f https://github.com/node-fetch/node-fetch/commit/7f68577de44c7d1efe7009b212f7c54a0b4a709b --- README.md | 3 ++- src/request.js | 4 ---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3f59dc1fe..badc2b197 100644 --- a/README.md +++ b/README.md @@ -539,7 +539,6 @@ If no values are set, the following request headers will be sent automatically: | ------------------- | ------------------------------------------------------ | | `Accept-Encoding` | `gzip, deflate, br` (when `options.compress === true`) | | `Accept` | `*/*` | -| `Connection` | `close` _(when no `options.agent` is present)_ | | `Content-Length` | _(automatically calculated, if possible)_ | | `Host` | _(host and port information from the target URI)_ | | `Transfer-Encoding` | `chunked` _(when `req.body` is a stream)_ | @@ -558,6 +557,8 @@ The `agent` option allows you to specify networking related options which are ou See [`http.Agent`](https://nodejs.org/api/http.html#http_new_agent_options) for more information. +If no agent is specified, the default agent provided by Node.js is used. Note that [this changed in Node.js 19](https://github.com/nodejs/node/blob/4267b92604ad78584244488e7f7508a690cb80d0/lib/_http_agent.js#L564) to have `keepalive` true by default. If you wish to enable `keepalive` in an earlier version of Node.js, you can override the agent as per the following code sample. + In addition, the `agent` option accepts a function that returns `http`(s)`.Agent` instance given current [URL](https://nodejs.org/api/url.html), this is useful during a redirection chain across HTTP and HTTPS protocol. ```js diff --git a/src/request.js b/src/request.js index 7a1a954fe..af2ebc8e9 100644 --- a/src/request.js +++ b/src/request.js @@ -288,10 +288,6 @@ export const getNodeRequestOptions = request => { agent = agent(parsedURL); } - if (!headers.has('Connection') && !agent) { - headers.set('Connection', 'close'); - } - // HTTP-network fetch step 4.2 // chunked encoding is handled by Node.js