@@ -168,6 +168,8 @@ function cacheDefaultCACertificates() {
168168 return defaultCACertificates ;
169169}
170170
171+ const certificateCache = { __proto__ : null } ;
172+
171173function getCACertificates ( options = { } ) {
172174 if ( typeof options === 'string' ) {
173175 options = { type : options } ;
@@ -177,11 +179,37 @@ function getCACertificates(options = {}) {
177179
178180 const {
179181 type = 'default' ,
180- format = 'string ' ,
182+ format = 'pem ' ,
181183 } = options ;
182184
183185 validateString ( type , 'type' ) ;
184- validateOneOf ( format , 'format' , [ 'string' , 'buffer' , 'x509' ] ) ;
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+ }
194+
195+ if ( certificateCache [ type ] ) {
196+ const cachedCerts = certificateCache [ type ] ;
197+
198+ if ( effectiveFormat === 'pem' ) {
199+ return cachedCerts ;
200+ }
201+
202+ const buffers = cachedCerts . map ( ( cert ) => {
203+ 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, '' ) ;
204+ return Buffer . from ( base64 , 'base64' ) ;
205+ } ) ;
206+
207+ if ( effectiveFormat === 'der' ) {
208+ return buffers ;
209+ }
210+
211+ return buffers . map ( ( buf ) => new X509Certificate ( buf ) ) ;
212+ }
185213
186214 let certs ;
187215 switch ( type ) {
@@ -192,32 +220,28 @@ function getCACertificates(options = {}) {
192220 default : throw new ERR_INVALID_ARG_VALUE ( 'type' , type ) ;
193221 }
194222
195- if ( format === 'string' ) {
196- // Return PEM strings directly
197- return certs . map ( ( cert ) => {
198- if ( typeof cert === 'string' ) return cert ;
199- if ( Buffer . isBuffer ( cert ) ) return cert . toString ( 'ascii' ) ;
200- throw new ERR_INVALID_ARG_VALUE ( 'cert' , cert ) ;
201- } ) ;
202- }
203-
204- const buffers = certs . map ( ( cert ) => {
205- if ( Buffer . isBuffer ( cert ) ) return cert ;
223+ const pemCerts = certs . map ( ( cert ) => {
206224 if ( typeof cert === 'string' ) {
207- const base64 = cert
208- . replace ( / - - - - - B E G I N C E R T I F I C A T E - - - - - / g, '' )
209- . replace ( / - - - - - E N D C E R T I F I C A T E - - - - - / g, '' )
210- . replace ( / \s + / g, '' ) ;
211- return Buffer . from ( base64 , 'base64' ) ;
225+ return cert ;
212226 }
213- throw new ERR_INVALID_ARG_VALUE ( 'cert' , cert ) ;
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' ) ;
214238 } ) ;
215239
216- if ( format === 'buffer ' ) {
217- return buffers ;
240+ if ( effectiveFormat === 'der ' ) {
241+ return derBuffers ;
218242 }
219243
220- return buffers . map ( ( buf ) => new X509Certificate ( buf ) ) ;
244+ return derBuffers . map ( ( buf ) => new X509Certificate ( buf ) ) ;
221245}
222246
223247exports . getCACertificates = getCACertificates ;
0 commit comments