-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathnode.ts
45 lines (37 loc) · 1.36 KB
/
node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import http from "node:http";
import https, { AgentOptions } from "node:https";
import nodeFetch, {
Headers as _Headers,
AbortController as _AbortController,
} from "node-fetch-native";
import { createFetch } from "./base";
export * from "./base";
export type * from "./types";
export function createNodeFetch() {
const useKeepAlive = JSON.parse(process.env.FETCH_KEEP_ALIVE || "false");
if (!useKeepAlive) {
return nodeFetch;
}
// https://github.com/node-fetch/node-fetch#custom-agent
const agentOptions: AgentOptions = { keepAlive: true };
const httpAgent = new http.Agent(agentOptions);
const httpsAgent = new https.Agent(agentOptions);
const nodeFetchOptions = {
agent(parsedURL: any) {
return parsedURL.protocol === "http:" ? httpAgent : httpsAgent;
},
};
return function nodeFetchWithKeepAlive(
input: RequestInfo,
init?: RequestInit
) {
return (nodeFetch as any)(input, { ...nodeFetchOptions, ...init });
};
}
export const fetch = globalThis.fetch
? (...args: Parameters<typeof globalThis.fetch>) => globalThis.fetch(...args)
: (createNodeFetch() as typeof globalThis.fetch);
export const Headers = globalThis.Headers || _Headers;
export const AbortController = globalThis.AbortController || _AbortController;
export const ofetch = createFetch({ fetch, Headers, AbortController });
export const $fetch = ofetch;