Skip to content

Commit

Permalink
perf: cache public DPoP CryptoKey's JWK representation for re-use
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Nov 21, 2022
1 parent 3eb165a commit 2858d06
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,10 +1233,15 @@ async function dpopProofJwt(
headers.set('dpop', proof)
}

const jwkCache = Symbol()
/** Exports an asymmetric crypto key as bare JWK */
async function publicJwk(key: CryptoKey) {
async function publicJwk(key: CryptoKey & { [jwkCache]?: JWK }) {
if (key[jwkCache]) {
return key[jwkCache]
}
const { kty, e, n, x, y, crv } = await crypto.subtle.exportKey('jwk', key)
return { kty, crv, e, n, x, y }
const jwk = (key[jwkCache] = { kty, e, n, x, y, crv })
return jwk
}

/**
Expand Down

0 comments on commit 2858d06

Please sign in to comment.