Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: packages/manager/src/lib/fetch.ts #961

Merged
merged 2 commits into from
May 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 37 additions & 29 deletions 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<typeof nodeFetch> {
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;