Skip to content

Commit

Permalink
feat(node): use native JWK export in node >= 15.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Mar 17, 2021
1 parent f0c2a64 commit 7f3cc44
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/runtime/node/key_to_jwk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import { JOSENotSupported } from '../../util/errors.js'
import getNamedCurve from './get_named_curve.js'
import { isCryptoKey, getKeyObject } from './webcrypto.js'

const [major, minor] = process.version
.substr(1)
.split('.')
.map((str) => parseInt(str, 10))

const jwkExportSupported = major >= 16 || (major === 15 && minor >= 9)

const keyToJWK: JWKConvertFunction = (key: KeyObject | CryptoKey): JWK => {
if (isCryptoKey(key)) {
// eslint-disable-next-line no-param-reassign
Expand All @@ -18,6 +25,11 @@ const keyToJWK: JWKConvertFunction = (key: KeyObject | CryptoKey): JWK => {
throw new TypeError('invalid key argument type')
}

if (jwkExportSupported) {
// @ts-expect-error
return key.export({ format: 'jwk' })
}

switch (key.type) {
case 'secret':
return {
Expand Down

0 comments on commit 7f3cc44

Please sign in to comment.