Skip to content

Commit 92392a0

Browse files
committed
refactor: update getSPKI to use crypto.createPublicKey when available
fixes #752
1 parent f3ba4c7 commit 92392a0

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/lib/asn1.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,18 @@ function spkiFromX509(buf: Uint8Array) {
250250
return encodeBase64(tbsCertificate[tbsCertificate[0].raw[0] === 0xa0 ? 6 : 5].raw)
251251
}
252252

253-
let X509Certificate: any
253+
let createPublicKey: any
254254
function getSPKI(x509: string): string {
255-
if (
255+
try {
256256
// @ts-ignore
257-
(X509Certificate ||= globalThis.process?.getBuiltinModule?.('node:crypto')?.X509Certificate)
258-
) {
257+
createPublicKey ??= globalThis.process?.getBuiltinModule?.('node:crypto')?.createPublicKey
258+
} catch {
259+
createPublicKey = 0
260+
}
261+
262+
if (createPublicKey) {
259263
try {
260-
return new X509Certificate(x509).publicKey.export({ format: 'pem', type: 'spki' })
264+
return new createPublicKey(x509).export({ format: 'pem', type: 'spki' })
261265
} catch {}
262266
}
263267
const pem = x509.replace(/(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g, '')

0 commit comments

Comments
 (0)