@@ -184,38 +184,57 @@ function cacheDefaultCACertificates() {
184184 return defaultCACertificates ;
185185}
186186
187- const certificateCache = { __proto__ : null } ;
188-
189- function getCACertificates ( options = { } ) {
190- if ( typeof options === 'string' ) {
191- options = { type : options } ;
192- } else if ( typeof options !== 'object' || options === null ) {
193- throw new ERR_INVALID_ARG_TYPE ( 'options' , [ 'string' , 'object' ] , options ) ;
194- }
195-
196- const {
197- type = 'default' ,
198- format = 'pem' ,
199- } = options ;
200-
201- validateString ( type , 'type' ) ;
202- validateOneOf ( format , 'format' , [ 'pem' , 'der' , 'x509' , 'string' , 'buffer' ] ) ;
203-
204- let effectiveFormat = format ;
205- if ( format === 'string' ) {
206- effectiveFormat = 'pem' ;
207- } else if ( format === 'buffer' ) {
208- effectiveFormat = 'der' ;
209- }
187+ function getCACertificates ( options = undefined ) {
188+ if ( typeof options === 'string' || options === undefined ) {
189+ const type = ( typeof options === 'string' ) ? options : 'default' ;
190+
191+ validateString ( type , 'type' ) ;
192+
193+ switch ( type ) {
194+ case 'default' : return cacheDefaultCACertificates ( ) ;
195+ case 'bundled' : return cacheBundledRootCertificates ( ) ;
196+ case 'system' : return cacheSystemCACertificates ( ) ;
197+ case 'extra' : return cacheExtraCACertificates ( ) ;
198+ default : throw new ERR_INVALID_ARG_VALUE ( 'type' , type ) ;
199+ }
200+ } else if ( typeof options === 'object' && options !== null ) {
201+ const {
202+ type = 'default' ,
203+ format = 'pem' ,
204+ } = options ;
205+
206+ validateString ( type , 'type' ) ;
207+ validateOneOf ( format , 'format' , [ 'pem' , 'der' , 'x509' , 'string' , 'buffer' ] ) ;
208+
209+ let effectiveFormat = format ;
210+ if ( format === 'string' ) {
211+ effectiveFormat = 'pem' ;
212+ } else if ( format === 'buffer' ) {
213+ effectiveFormat = 'der' ;
214+ }
210215
211- if ( certificateCache [ type ] ) {
212- const cachedCerts = certificateCache [ type ] ;
216+ let certs ;
217+ switch ( type ) {
218+ case 'default' : certs = cacheDefaultCACertificates ( ) ; break ;
219+ case 'bundled' : certs = cacheBundledRootCertificates ( ) ; break ;
220+ case 'system' : certs = cacheSystemCACertificates ( ) ; break ;
221+ case 'extra' : certs = cacheExtraCACertificates ( ) ; break ;
222+ default : throw new ERR_INVALID_ARG_VALUE ( 'type' , type ) ;
223+ }
213224
214225 if ( effectiveFormat === 'pem' ) {
215- return cachedCerts ;
226+ return certs . map ( ( cert ) => {
227+ if ( typeof cert === 'string' ) {
228+ return cert ;
229+ }
230+ return `-----BEGIN CERTIFICATE-----\n${ cert . toString ( 'base64' ) . match ( / .{ 1 , 64 } / g) . join ( '\n' ) } \n-----END CERTIFICATE-----` ;
231+ } ) ;
216232 }
217233
218- const buffers = cachedCerts . map ( ( cert ) => {
234+ const buffers = certs . map ( ( cert ) => {
235+ if ( Buffer . isBuffer ( cert ) ) {
236+ return cert ;
237+ }
219238 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, '' ) ;
220239 return Buffer . from ( base64 , 'base64' ) ;
221240 } ) ;
@@ -227,37 +246,7 @@ function getCACertificates(options = {}) {
227246 return buffers . map ( ( buf ) => new X509Certificate ( buf ) ) ;
228247 }
229248
230- let certs ;
231- switch ( type ) {
232- case 'default' : certs = cacheDefaultCACertificates ( ) ; break ;
233- case 'bundled' : certs = cacheBundledRootCertificates ( ) ; break ;
234- case 'system' : certs = cacheSystemCACertificates ( ) ; break ;
235- case 'extra' : certs = cacheExtraCACertificates ( ) ; break ;
236- default : throw new ERR_INVALID_ARG_VALUE ( 'type' , type ) ;
237- }
238-
239- const pemCerts = certs . map ( ( cert ) => {
240- if ( typeof cert === 'string' ) {
241- return cert ;
242- }
243- return `-----BEGIN CERTIFICATE-----\n${ cert . toString ( 'base64' ) . match ( / .{ 1 , 64 } / g) . join ( '\n' ) } \n-----END CERTIFICATE-----` ;
244- } ) ;
245- certificateCache [ type ] = pemCerts ;
246-
247- if ( effectiveFormat === 'pem' ) {
248- return pemCerts ;
249- }
250-
251- const derBuffers = pemCerts . map ( ( cert ) => {
252- 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, '' ) ;
253- return Buffer . from ( base64 , 'base64' ) ;
254- } ) ;
255-
256- if ( effectiveFormat === 'der' ) {
257- return derBuffers ;
258- }
259-
260- return derBuffers . map ( ( buf ) => new X509Certificate ( buf ) ) ;
249+ throw new ERR_INVALID_ARG_TYPE ( 'options' , [ 'string' , 'object' ] , options ) ;
261250}
262251
263252exports . getCACertificates = getCACertificates ;
0 commit comments