From 76de968e22e4fd0e6a39dc04b8beffc6f739792d Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Wed, 3 May 2023 09:41:53 -1000 Subject: [PATCH 1/2] refactor: `src/lib/fetch.ts` --- packages/manager/src/lib/fetch.ts | 66 +++++++++++++++++-------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/packages/manager/src/lib/fetch.ts b/packages/manager/src/lib/fetch.ts index cb38d2c53f..64d6ad2b99 100644 --- a/packages/manager/src/lib/fetch.ts +++ b/packages/manager/src/lib/fetch.ts @@ -1,38 +1,46 @@ -import http from "node:http"; -import https from "node:https"; -import nodeFetch, { RequestInfo, RequestInit } from "node-fetch"; +// This temporary wrapper around `node-fetch` fixes an issue where quick +// consecutive network requests causes failed requests. +// +// See https://github.com/node-fetch/node-fetch/issues/1735 for more details. +// +// TODO: Remove this wrapper and replace all imports with `node-fetch` if https://github.com/node-fetch/node-fetch/pull/1736 is merged. + +import * as http from "node:http"; +import * as https from "node:https"; +import baseFetch from "node-fetch"; export * from "node-fetch"; -const httpAgent = new http.Agent({ - keepAlive: true, -}); -const httpsAgent = new https.Agent({ - keepAlive: true, -}); +/** + * The default HTTP Agent with `keepAlive: true` used in `fetch()` requests. + */ +const DEFAULT_HTTP_AGENT = new http.Agent({ keepAlive: true }); -const options: RequestInit = { - agent: function (parsedURL) { - if (parsedURL.protocol == "http:") { - return httpAgent; - } else { - return httpsAgent; - } - }, -}; +/** + * The default HTTPS Agent with `keepAlive: true` used in `fetch()` requests. + */ +const DEFAULT_HTTPS_AGENT = new https.Agent({ keepAlive: true }); /** - * Wrapper around node-fetch that passes an user-agent with the keepAlive option - * enabled + * Patched `fetch()` from `node-fetch` that fixes a bug where quick consecutive + * network requests cause failed requests. + * + * Use this `fetch()` in place of `node-fetch`'s `fetch()`. + * + * @remarks + * `fetch()` is patched by setting an HTTP/HTTPS Agent with `keepAlive: true`. + * If you need to assign an Agent, be sure to retain the `keepAlive: true` + * option. */ -export default function fetch( - url: URL | RequestInfo, - init?: RequestInit, -): ReturnType { - const opts = { - ...options, +const fetch: typeof baseFetch = (url, init) => { + return baseFetch(url, { + agent: (parsedURL) => { + return parsedURL.protocol === "http:" + ? DEFAULT_HTTP_AGENT + : DEFAULT_HTTPS_AGENT; + }, ...init, - }; + }); +}; - return nodeFetch(url, opts); -} +export default fetch; From 5a62eff5b8c33dd6fbcefd5c30529e4e068b46a8 Mon Sep 17 00:00:00 2001 From: Baptiste Morelle Date: Thu, 4 May 2023 10:15:12 +0200 Subject: [PATCH 2/2] fixing a small typo --- packages/manager/src/lib/fetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/manager/src/lib/fetch.ts b/packages/manager/src/lib/fetch.ts index 64d6ad2b99..091ee8e66f 100644 --- a/packages/manager/src/lib/fetch.ts +++ b/packages/manager/src/lib/fetch.ts @@ -1,5 +1,5 @@ // This temporary wrapper around `node-fetch` fixes an issue where quick -// consecutive network requests causes failed requests. +// consecutive network requests cause failed requests. // // See https://github.com/node-fetch/node-fetch/issues/1735 for more details. //