import { readFileSync } from 'fs' ; import http from 'http' ; import https from 'https' ; import { PeerCertificate } from 'tls' ; import { ConfigType } from '../' ; export type HttpAgent = http . Agent | https . Agent ; interface AgentOptions { rejectUnauthorized ? : boolean ; checkServerIdentity ? : ( ( host : string , cert : PeerCertificate ) => Error | undefined ) | undefined ; } class EnterpriseSearchHttpAgent { public httpAgent : HttpAgent = new http . Agent ( ) ; getHttpAgent ( ) { return this . httpAgent ; } initializeHttpAgent ( config ) { if ( ! config . host ) return ; try { const parsedHost = new URL ( config . host ) ; if ( parsedHost . protocol === 'https:' ) { this . httpAgent = new https . Agent ( { ca : this . loadCertificateAuthorities ( config . ssl . certificateAuthorities ) , ... this . getAgentOptions ( config . ssl . verificationMode ) , } ) ; } } catch { } } loadCertificateAuthorities ( certificates ) { if ( ! certificates ) return [ ] ; const paths = Array . isArray ( certificates ) ? certificates : [ certificates ] ; return paths . map ( ( path ) => readFileSync ( path , 'utf8' ) ) ; } getAgentOptions ( verificationMode ) { const agentOptions = { } ; switch ( verificationMode ) { case 'none' : agentOptions . rejectUnauthorized = false ; break ; case 'certificate' : agentOptions . rejectUnauthorized = true ; agentOptions . checkServerIdentity = ( ) => undefined ; break ; case 'full' : default : agentOptions . rejectUnauthorized = true ; break ; } return agentOptions ; } } export const entSearchHttpAgent = new EnterpriseSearchHttpAgent ( ) ;