import http from 'http' ; import https , { AgentOptions } from 'https' ; import nodeFetch , { Headers as _Headers } from 'node-fetch' ; import { createFetch } from './base' ; export * from './base' ; export function createNodeFetch ( ) { const useKeepAlive = JSON . parse ( process . env . FETCH_KEEP_ALIVE || 'false' ) ; if ( ! useKeepAlive ) { return nodeFetch ; } const agentOpts = { keepAlive : true } ; const httpAgent = new http . Agent ( agentOpts ) ; const httpsAgent = new https . Agent ( agentOpts ) ; const nodeFetchOptions = { agent ( parsedURL ) { return parsedURL . protocol === 'http:' ? httpAgent : httpsAgent ; } } ; return function nodeFetchWithKeepAlive ( input , init ? ) { return ( nodeFetch as any ) ( input , { ... nodeFetchOptions , ... init } ) ; } ; } export const fetch = globalThis . fetch || createNodeFetch ( ) ; export const Headers = globalThis . Headers || _Headers ; export const $fetch = createFetch ( { fetch , Headers } ) ;