79ed8e13b0
Fixes #27109 This PR adds a default `agent` as described in the [`node-fetch` docs](https://github.com/node-fetch/node-fetch#custom-agent). We should see about 2x perf according to some [benchmarks](https://github.com/Ethan-Arrowood/undici-fetch/blob/main/benchmarks.md#fetch).
23 lines
724 B
JavaScript
23 lines
724 B
JavaScript
import fetch, { Headers, Request, Response } from 'node-fetch'
|
|
import { Agent as HttpAgent } from 'http'
|
|
import { Agent as HttpsAgent } from 'https'
|
|
|
|
// Polyfill fetch() in the Node.js environment
|
|
if (!global.fetch) {
|
|
const httpAgent = new HttpAgent({ keepAlive: true })
|
|
const httpsAgent = new HttpsAgent({ keepAlive: true })
|
|
const agent = ({ protocol }) =>
|
|
protocol === 'http:' ? httpAgent : httpsAgent
|
|
const fetchWithAgent = (url, opts, ...rest) => {
|
|
if (!opts) {
|
|
opts = { agent }
|
|
} else if (!opts.agent) {
|
|
opts.agent = agent
|
|
}
|
|
return fetch(url, opts, ...rest)
|
|
}
|
|
global.fetch = fetchWithAgent
|
|
global.Headers = Headers
|
|
global.Request = Request
|
|
global.Response = Response
|
|
}
|