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 2 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
38 changes: 38 additions & 0 deletions packages/manager/src/lib/fetch.ts
@@ -0,0 +1,38 @@
import http from "node:http";
import https from "node:https";
import nodeFetch, { RequestInfo, RequestInit } from "node-fetch";

export * from "node-fetch";

const httpAgent = new http.Agent({
keepAlive: true,
});
const httpsAgent = new https.Agent({
keepAlive: true,
});

const options: RequestInit = {
agent: function (parsedURL) {
if (parsedURL.protocol == "http:") {
return httpAgent;
} else {
return httpsAgent;
}
},
};

/**
* Wrapper around node-fetch that passes an user-agent with the keepAlive option
* enabled
*/
export default function fetch(
url: URL | RequestInfo,
init?: RequestInit,
): ReturnType<typeof nodeFetch> {
const opts = {
...options,
...init,
};

return nodeFetch(url, opts);
}
@@ -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