diff --git a/src/runtime/node/crypto/node.ts b/src/runtime/node/crypto/node.ts index f9fa7e71..523f2186 100644 --- a/src/runtime/node/crypto/node.ts +++ b/src/runtime/node/crypto/node.ts @@ -9,6 +9,18 @@ const MAX_RANDOM_VALUE_BYTES: number = 65_536; // ---- implemented Utils ---- +export const webcrypto = new Proxy( + globalThis.crypto as typeof nodeCrypto.webcrypto, + { + get(_, key: keyof typeof globalThis.crypto | "CryptoKey") { + if (key === "CryptoKey") { + return globalThis.CryptoKey; + } + return globalThis.crypto[key]; + }, + }, +); + export const randomBytes: typeof nodeCrypto.randomBytes = ( size: number, cb?: (err: Error | null, buf: Buffer) => void, diff --git a/src/runtime/node/crypto/web.ts b/src/runtime/node/crypto/web.ts index 3d4715d7..b8900435 100644 --- a/src/runtime/node/crypto/web.ts +++ b/src/runtime/node/crypto/web.ts @@ -1,21 +1,12 @@ // https://nodejs.org/api/crypto.html // https://github.com/unjs/uncrypto -import type nodeCrypto from "node:crypto"; -export const CryptoKey = - globalThis.CryptoKey as unknown as typeof nodeCrypto.webcrypto.CryptoKey; - -export const webcrypto: Crypto & typeof nodeCrypto.webcrypto = { - CryptoKey, - ...globalThis.crypto, -}; - -export const subtle: SubtleCrypto = webcrypto.subtle; +export const subtle: SubtleCrypto = globalThis.crypto?.subtle; export const randomUUID: Crypto["randomUUID"] = () => { - return webcrypto.randomUUID(); + return globalThis.crypto?.randomUUID(); }; export const getRandomValues: Crypto["getRandomValues"] = (array: any) => { - return webcrypto.getRandomValues(array); + return globalThis.crypto?.getRandomValues(array); };