license import { Agent as HTTPAgent } from 'http' ; import { Agent as HTTPSAgent } from 'https' ; import { parse } from 'url' ; import { Options } from './' ; export const pool = new Map < string , HTTPAgent > ( ) ; export type HttpAnyAgent = HTTPAgent | HTTPSAgent ; private param { string } uri param { Options } reqOpts returns { HttpAnyAgent | undefined } export function getAgent ( uri , reqOpts ) { const isHttp = uri . startsWith ( 'http://' ) ; const proxy = reqOpts . proxy || process . env . HTTP_PROXY || process . env . http_proxy || process . env . HTTPS_PROXY || process . env . https_proxy ; const poolOptions = Object . assign ( { } , reqOpts . pool ) ; if ( proxy ) { const Agent = isHttp ? require ( 'http-proxy-agent' ) : require ( 'https-proxy-agent' ) ; const proxyOpts = { ... parse ( proxy ) , ... poolOptions } ; return new Agent ( proxyOpts ) ; } let key = isHttp ? 'http' : 'https' ; if ( reqOpts . forever ) { key += ':forever' ; if ( ! pool . has ( key ) ) { const Agent = isHttp ? HTTPAgent : HTTPSAgent ; pool . set ( key , new Agent ( { ... poolOptions , keepAlive : true } ) ) ; } } return pool . get ( key ) ; }