@@ -168,38 +168,57 @@ function cacheDefaultCACertificates() {
168168 return defaultCACertificates ;
169169}
170170
171- const certificateCache = { __proto__ : null } ;
172-
173- function getCACertificates ( options = { } ) {
174- if ( typeof options === 'string' ) {
175- options = { type : options } ;
176- } else if ( typeof options !== 'object' || options === null ) {
177- throw new ERR_INVALID_ARG_TYPE ( 'options' , [ 'string' , 'object' ] , options ) ;
178- }
179-
180- const {
181- type = 'default' ,
182- format = 'pem' ,
183- } = options ;
184-
185- validateString ( type , 'type' ) ;
186- validateOneOf ( format , 'format' , [ 'pem' , 'der' , 'x509' , 'string' , 'buffer' ] ) ;
187-
188- let effectiveFormat = format ;
189- if ( format === 'string' ) {
190- effectiveFormat = 'pem' ;
191- } else if ( format === 'buffer' ) {
192- effectiveFormat = 'der' ;
193- }
171+ function getCACertificates ( options = undefined ) {
172+ if ( typeof options === 'string' || options === undefined ) {
173+ const type = ( typeof options === 'string' ) ? options : 'default' ;
174+
175+ validateString ( type , 'type' ) ;
176+
177+ switch ( type ) {
178+ case 'default' : return cacheDefaultCACertificates ( ) ;
179+ case 'bundled' : return cacheBundledRootCertificates ( ) ;
180+ case 'system' : return cacheSystemCACertificates ( ) ;
181+ case 'extra' : return cacheExtraCACertificates ( ) ;
182+ default : throw new ERR_INVALID_ARG_VALUE ( 'type' , type ) ;
183+ }
184+ } else if ( typeof options === 'object' && options !== null ) {
185+ const {
186+ type = 'default' ,
187+ format = 'pem' ,
188+ } = options ;
189+
190+ validateString ( type , 'type' ) ;
191+ validateOneOf ( format , 'format' , [ 'pem' , 'der' , 'x509' , 'string' , 'buffer' ] ) ;
192+
193+ let effectiveFormat = format ;
194+ if ( format === 'string' ) {
195+ effectiveFormat = 'pem' ;
196+ } else if ( format === 'buffer' ) {
197+ effectiveFormat = 'der' ;
198+ }
194199
195- if ( certificateCache [ type ] ) {
196- const cachedCerts = certificateCache [ type ] ;
200+ let certs ;
201+ switch ( type ) {
202+ case 'default' : certs = cacheDefaultCACertificates ( ) ; break ;
203+ case 'bundled' : certs = cacheBundledRootCertificates ( ) ; break ;
204+ case 'system' : certs = cacheSystemCACertificates ( ) ; break ;
205+ case 'extra' : certs = cacheExtraCACertificates ( ) ; break ;
206+ default : throw new ERR_INVALID_ARG_VALUE ( 'type' , type ) ;
207+ }
197208
198209 if ( effectiveFormat === 'pem' ) {
199- return cachedCerts ;
210+ return certs . map ( ( cert ) => {
211+ if ( typeof cert === 'string' ) {
212+ return cert ;
213+ }
214+ return `-----BEGIN CERTIFICATE-----\n${ cert . toString ( 'base64' ) . match ( / .{ 1 , 64 } / g) . join ( '\n' ) } \n-----END CERTIFICATE-----` ;
215+ } ) ;
200216 }
201217
202- const buffers = cachedCerts . map ( ( cert ) => {
218+ const buffers = certs . map ( ( cert ) => {
219+ if ( Buffer . isBuffer ( cert ) ) {
220+ return cert ;
221+ }
203222 const base64 = cert . replace ( / (?: \s | - - - - - B E G I N C E R T I F I C A T E - - - - - | - - - - - E N D C E R T I F I C A T E - - - - - ) + / g, '' ) ;
204223 return Buffer . from ( base64 , 'base64' ) ;
205224 } ) ;
@@ -211,37 +230,7 @@ function getCACertificates(options = {}) {
211230 return buffers . map ( ( buf ) => new X509Certificate ( buf ) ) ;
212231 }
213232
214- let certs ;
215- switch ( type ) {
216- case 'default' : certs = cacheDefaultCACertificates ( ) ; break ;
217- case 'bundled' : certs = cacheBundledRootCertificates ( ) ; break ;
218- case 'system' : certs = cacheSystemCACertificates ( ) ; break ;
219- case 'extra' : certs = cacheExtraCACertificates ( ) ; break ;
220- default : throw new ERR_INVALID_ARG_VALUE ( 'type' , type ) ;
221- }
222-
223- const pemCerts = certs . map ( ( cert ) => {
224- if ( typeof cert === 'string' ) {
225- return cert ;
226- }
227- return `-----BEGIN CERTIFICATE-----\n${ cert . toString ( 'base64' ) . match ( / .{ 1 , 64 } / g) . join ( '\n' ) } \n-----END CERTIFICATE-----` ;
228- } ) ;
229- certificateCache [ type ] = pemCerts ;
230-
231- if ( effectiveFormat === 'pem' ) {
232- return pemCerts ;
233- }
234-
235- const derBuffers = pemCerts . map ( ( cert ) => {
236- const base64 = cert . replace ( / (?: \s | - - - - - B E G I N C E R T I F I C A T E - - - - - | - - - - - E N D C E R T I F I C A T E - - - - - ) + / g, '' ) ;
237- return Buffer . from ( base64 , 'base64' ) ;
238- } ) ;
239-
240- if ( effectiveFormat === 'der' ) {
241- return derBuffers ;
242- }
243-
244- return derBuffers . map ( ( buf ) => new X509Certificate ( buf ) ) ;
233+ throw new ERR_INVALID_ARG_TYPE ( 'options' , [ 'string' , 'object' ] , options ) ;
245234}
246235
247236exports . getCACertificates = getCACertificates ;
0 commit comments