Skip to content

Commit f131075

Browse files
committed
tls: simplify getCACertificates() structure per review
1 parent e787797 commit f131075

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

lib/tls.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -202,40 +202,36 @@ function getCACertificatesAsStrings(type = 'default') {
202202
}
203203

204204
function getCACertificates(options = undefined) {
205-
if (typeof options === 'string' || options === undefined) {
206-
return getCACertificatesAsStrings(options);
207-
}
205+
let type = 'default';
206+
let format = 'pem';
208207

209208
if (typeof options === 'object' && options !== null) {
210-
const {
211-
type = 'default',
212-
format = 'pem',
213-
} = options;
214-
215-
validateString(type, 'type');
216-
validateOneOf(format, 'format', ['pem', 'der', 'x509', 'string', 'buffer']);
209+
({ type = 'default', format = 'pem' } = options);
210+
} else if (typeof options === 'string' || options === undefined) {
211+
type = options ?? 'default';
212+
} else {
213+
throw new ERR_INVALID_ARG_TYPE('options', ['string', 'object'], options);
214+
}
217215

218-
const certs = getCACertificatesAsStrings(type);
216+
validateString(type, 'type');
217+
validateOneOf(format, 'format', ['pem', 'der', 'x509', 'string', 'buffer']);
219218

220-
if (format === 'x509') {
221-
return certs.map((cert) => new X509Certificate(cert));
222-
}
219+
const certs = getCACertificatesAsStrings(type);
223220

224-
if (format === 'pem' || format === 'string') {
225-
return certs;
226-
}
227-
228-
const buffers = certs.map((cert) => {
229-
const base64 = cert.replace(/(?:\s|-----BEGIN CERTIFICATE-----|-----END CERTIFICATE-----)+/g, '');
230-
return Buffer.from(base64, 'base64');
231-
});
221+
if (format === 'pem' || format === 'string') {
222+
return certs;
223+
}
232224

233-
if (format === 'der' || format === 'buffer') {
234-
return buffers;
235-
}
225+
if (format === 'x509') {
226+
return certs.map((cert) => new X509Certificate(cert));
236227
}
237228

238-
throw new ERR_INVALID_ARG_TYPE('options', ['string', 'object'], options);
229+
const buffers = certs.map((cert) => {
230+
const base64 = cert.replace(/(?:\s|-----BEGIN CERTIFICATE-----|-----END CERTIFICATE-----)+/g, '');
231+
return Buffer.from(base64, 'base64');
232+
});
233+
234+
return buffers;
239235
}
240236

241237
exports.getCACertificates = getCACertificates;

0 commit comments

Comments
 (0)