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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http-connect): try wrapping fetch so that it's passed and agent t… #960

Merged
merged 5 commits into from May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/manager/src/auth/PrismicAuthManager.ts
Expand Up @@ -5,7 +5,7 @@ import * as os from "node:os";
import * as http from "node:http";

import * as h3 from "h3";
import fetch from "node-fetch";
import fetch from "../lib/fetch";
import cookie from "cookie";
import cors from "cors";
import getPort from "get-port";
Expand Down
2 changes: 1 addition & 1 deletion packages/manager/src/lib/checkIsURLAccessible.ts
@@ -1,4 +1,4 @@
import fetch from "node-fetch";
import fetch from "./fetch";

export const checkIsURLAccessible = async (url: string): Promise<boolean> => {
const res = await fetch(url);
Expand Down
46 changes: 46 additions & 0 deletions packages/manager/src/lib/fetch.ts
@@ -0,0 +1,46 @@
// This temporary wrapper around `node-fetch` fixes an issue where quick
// consecutive network requests cause 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";

/**
* The default HTTP Agent with `keepAlive: true` used in `fetch()` requests.
*/
const DEFAULT_HTTP_AGENT = new http.Agent({ keepAlive: true });

/**
* The default HTTPS Agent with `keepAlive: true` used in `fetch()` requests.
*/
const DEFAULT_HTTPS_AGENT = new https.Agent({ keepAlive: true });

/**
* 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.
*/
const fetch: typeof baseFetch = (url, init) => {
return baseFetch(url, {
agent: (parsedURL) => {
return parsedURL.protocol === "http:"
? DEFAULT_HTTP_AGENT
: DEFAULT_HTTPS_AGENT;
},
...init,
});
};

export default fetch;
@@ -1,5 +1,5 @@
import * as t from "io-ts";
import fetch from "node-fetch";
import fetch from "./fetch";
import pLimit from "p-limit";

import { decode } from "./decode";
Expand Down
2 changes: 1 addition & 1 deletion packages/manager/src/lib/fetchNPMPackageVersions.ts
@@ -1,5 +1,5 @@
import * as t from "io-ts";
import fetch from "node-fetch";
import fetch from "./fetch";

import { decode } from "./decode";

Expand Down
@@ -1,5 +1,5 @@
import * as t from "io-ts";
import fetch from "node-fetch";
import fetch from "../../lib/fetch";
import * as prismicCustomTypesClient from "@prismicio/custom-types-client";
import { CustomType } from "@prismicio/types-internal/lib/customtypes";
import {
Expand Down
@@ -1,5 +1,5 @@
import * as t from "io-ts";
import fetch, { Response } from "node-fetch";
import fetch, { Response } from "../../lib/fetch";
import { fold } from "fp-ts/Either";

import { decode } from "../../lib/decode";
Expand Down
@@ -1,6 +1,6 @@
import * as t from "io-ts";
import { fileTypeFromBuffer } from "file-type";
import fetch, { FormData, Blob, Response } from "node-fetch";
import fetch, { FormData, Blob, Response } from "../../lib/fetch";
// puppeteer is lazy-loaded in captureSliceSimulatorScreenshot
import type { BrowserContext, Viewport } from "puppeteer";

Expand Down
@@ -1,6 +1,6 @@
import * as t from "io-ts";
import { HookError } from "@slicemachine/plugin-kit";
import fetch from "node-fetch";
import fetch from "../../lib/fetch";

import { DecodeError } from "../../lib/DecodeError";
import { assertPluginsInitialized } from "../../lib/assertPluginsInitialized";
Expand Down
2 changes: 1 addition & 1 deletion packages/manager/src/managers/slices/SlicesManager.ts
@@ -1,5 +1,5 @@
import * as t from "io-ts";
import fetch from "node-fetch";
import fetch from "../../lib/fetch";
import * as prismicCustomTypesClient from "@prismicio/custom-types-client";
import { SharedSliceContent } from "@prismicio/types-internal/lib/content";
import {
Expand Down