@@ -70,6 +70,7 @@ export interface ProxyAgentParams {
7070 isAdditionalFetchSupportEnabled : ( ) => boolean ,
7171 addCertificatesV1 : ( ) => boolean ,
7272 addCertificatesV2 : ( ) => boolean ,
73+ loadSystemCertificatesFromNode : ( ) => boolean | undefined ;
7374 loadAdditionalCertificates ( ) : Promise < string [ ] > ;
7475 lookupProxyAuthorization ?: LookupProxyAuthorization ;
7576 log : Log ;
@@ -522,17 +523,18 @@ function patchTlsConnect(params: ProxyAgentParams, original: typeof tls.connect)
522523 if ( ! options . secureContext ) {
523524 options . secureContext = tls . createSecureContext ( options ) ;
524525 }
525- if ( ! _certificates ) {
526+ const certificates = _certs . get ( ! ! params . loadSystemCertificatesFromNode ( ) ) ?. result ;
527+ if ( ! certificates ) {
526528 params . log . trace ( 'ProxyResolver#tls.connect waiting for existing socket connect' ) ;
527529 options . socket . once ( 'connect' , ( ) => {
528530 params . log . trace ( 'ProxyResolver#tls.connect got existing socket connect - adding certs' ) ;
529- for ( const cert of _certificates || [ ] ) {
531+ for ( const cert of _certs . get ( ! ! params . loadSystemCertificatesFromNode ( ) ) ?. result || [ ] ) {
530532 options ! . secureContext ! . context . addCACert ( cert ) ;
531533 }
532534 } ) ;
533535 } else {
534536 params . log . trace ( 'ProxyResolver#tls.connect existing socket already connected - adding certs' ) ;
535- for ( const cert of _certificates ) {
537+ for ( const cert of certificates ) {
536538 options ! . secureContext ! . context . addCACert ( cert ) ;
537539 }
538540 }
@@ -877,28 +879,41 @@ function addCertificatesToOptionsV1(params: ProxyAgentParams, addCertificatesV1:
877879 }
878880}
879881
880- let _certificatesPromise : Promise < string [ ] > | undefined ;
881- let _certificates : string [ ] | undefined ;
882+ const _certs = new Map < boolean , { promise : Promise < string [ ] > ; result : string [ ] | undefined } > ( ) ;
882883export async function getOrLoadAdditionalCertificates ( params : ProxyAgentParams ) {
883- if ( ! _certificatesPromise ) {
884- _certificatesPromise = ( async ( ) => {
885- return _certificates = await params . loadAdditionalCertificates ( ) ;
886- } ) ( ) ;
884+ const loadFromNode = ! ! params . loadSystemCertificatesFromNode ( ) ;
885+ if ( ! _certs . has ( loadFromNode ) ) {
886+ const cert : { promise : Promise < string [ ] > ; result : string [ ] | undefined } = {
887+ promise : ( async ( ) => {
888+ const result = await params . loadAdditionalCertificates ( ) ;
889+ return cert ! . result = result ; // need to await before accessing cert.
890+ } ) ( ) ,
891+ result : undefined
892+ } ;
893+ _certs . set ( loadFromNode , cert ) ;
887894 }
888- return _certificatesPromise ;
895+ return _certs . get ( loadFromNode ) ! . promise ;
889896}
890897
891898export interface CertificateParams {
899+ loadSystemCertificatesFromNode : ( ) => boolean | undefined ;
892900 log : Log ;
893901}
894902
895903let _systemCertificatesPromise : Promise < string [ ] > | undefined ;
896904export async function loadSystemCertificates ( params : CertificateParams ) {
905+ if ( ! ! params . loadSystemCertificatesFromNode ?.( ) ) { // Checking if function exists for backward compatibility.
906+ const start = Date . now ( ) ;
907+ const systemCerts = tls . getCACertificates ( 'system' ) ;
908+ params . log . debug ( `ProxyResolver#loadSystemCertificates from Node.js count (${ Date . now ( ) - start } ms)` , systemCerts . length ) ;
909+ return systemCerts ;
910+ }
897911 if ( ! _systemCertificatesPromise ) {
898912 _systemCertificatesPromise = ( async ( ) => {
899913 try {
914+ const start = Date . now ( ) ;
900915 const certs = await readSystemCertificates ( ) ;
901- params . log . debug ( ' ProxyResolver#loadSystemCertificates count' , certs . length ) ;
916+ params . log . debug ( ` ProxyResolver#loadSystemCertificates count ( ${ Date . now ( ) - start } ms)` , certs . length ) ;
902917 const now = Date . now ( ) ;
903918 const filtered = certs
904919 . filter ( cert => {
@@ -923,8 +938,7 @@ export async function loadSystemCertificates(params: CertificateParams) {
923938}
924939
925940export function resetCaches ( ) {
926- _certificatesPromise = undefined ;
927- _certificates = undefined ;
941+ _certs . clear ( ) ;
928942 _systemCertificatesPromise = undefined ;
929943}
930944
0 commit comments