Skip to content

Commit

Permalink
fix: workaround turbo's eager optimizations
Browse files Browse the repository at this point in the history
closes #690
  • Loading branch information
panva committed Jun 27, 2024
1 parent 03e5e27 commit 723a042
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/lib/decrypt_key_management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ECDH from '../runtime/ecdhes.js'
import { decrypt as pbes2Kw } from '../runtime/pbes2kw.js'
import { decrypt as rsaEs } from '../runtime/rsaes.js'
import { decode as base64url } from '../runtime/base64url.js'
import * as normalize from '../runtime/normalize_key.js'
import normalize from '../runtime/normalize_key.js'

import type { DecryptOptions, JWEHeaderParameters, KeyLike, JWK } from '../types.d'
import { JOSENotSupported, JWEInvalid } from '../util/errors.js'
Expand All @@ -21,10 +21,7 @@ async function decryptKeyManagement(
options?: DecryptOptions,
): Promise<KeyLike | Uint8Array> {
// @ts-ignore
if (normalize.normalizePrivateKey) {
// @ts-ignore
key = await normalize.normalizePrivateKey(key, alg)
}
key = (await normalize.normalizePrivateKey?.(key, alg)) || key

checkKeyType(alg, key, 'decrypt')

Expand Down
7 changes: 2 additions & 5 deletions src/lib/encrypt_key_management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ECDH from '../runtime/ecdhes.js'
import { encrypt as pbes2Kw } from '../runtime/pbes2kw.js'
import { encrypt as rsaEs } from '../runtime/rsaes.js'
import { encode as base64url } from '../runtime/base64url.js'
import * as normalize from '../runtime/normalize_key.js'
import normalize from '../runtime/normalize_key.js'

import type {
KeyLike,
Expand Down Expand Up @@ -33,10 +33,7 @@ async function encryptKeyManagement(
let cek: KeyLike | Uint8Array

// @ts-ignore
if (normalize.normalizePublicKey) {
// @ts-ignore
key = await normalize.normalizePublicKey(key, alg)
}
key = (await normalize.normalizePublicKey?.(key, alg)) || key

checkKeyType(alg, key, 'encrypt')

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/browser/get_sign_verify_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto, { isCryptoKey } from './webcrypto.js'
import { checkSigCryptoKey } from '../../lib/crypto_key.js'
import invalidKeyInput from '../../lib/invalid_key_input.js'
import { types } from './is_key_like.js'
import * as normalize from '../normalize_key.js'
import normalize from '../normalize_key.js'

export default async function getCryptoKey(alg: string, key: unknown, usage: KeyUsage) {
if (usage === 'sign') {
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/browser/normalize_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import importJWK from '../jwk_to_key.js'

const normalizeSecretKey = (k: string) => decode(k)

export const normalizePublicKey = async (key: KeyLike | Uint8Array | unknown, alg: string) => {
const normalizePublicKey = async (key: KeyLike | Uint8Array | unknown, alg: string) => {
// @ts-expect-error
if (key?.[Symbol.toStringTag] === 'KeyObject') {
// @ts-expect-error
Expand All @@ -25,7 +25,7 @@ export const normalizePublicKey = async (key: KeyLike | Uint8Array | unknown, al
return <KeyLike | Uint8Array>key
}

export const normalizePrivateKey = async (key: KeyLike | Uint8Array | unknown, alg: string) => {
const normalizePrivateKey = async (key: KeyLike | Uint8Array | unknown, alg: string) => {
// @ts-expect-error
if (key?.[Symbol.toStringTag] === 'KeyObject') {
// @ts-expect-error
Expand All @@ -39,3 +39,5 @@ export const normalizePrivateKey = async (key: KeyLike | Uint8Array | unknown, a

return <KeyLike | Uint8Array>key
}

export default { normalizePublicKey, normalizePrivateKey }
2 changes: 1 addition & 1 deletion src/runtime/node/normalize_key.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {}
export default {}

0 comments on commit 723a042

Please sign in to comment.