`getRandomValues` throwing error `TypeError: Illegal invocation` on Cloudflare Workers
brendonmatos opened this issue · comments
Brendon Matos commented
Environment
unenv@1.7.1
node 18
Reproduction
Unfortunately, runnings wrangler@3 from stackblitz it's still not supported. The original issue was found on Nuxt3 but there the error stops the render of the page, in Nitro this seems like just been ignored.
https://stackblitz.com/edit/github-vk94cj-546phx?file=package.json
Steps:
Describe the bug
Seems like when we are in Cloudflare worker environment, this kind of reference destructuring:
unenv/src/runtime/node/crypto/node.ts
Lines 12 to 22 in 2edb8fd
Makes the invoke "Illegal"
Adding these lines makes work like a charm:
export const webcrypto = new Proxy(
globalThis.crypto as typeof nodeCrypto.webcrypto,
{
get(_, key: keyof typeof globalThis.crypto | "CryptoKey") {
if (key === "CryptoKey") {
return globalThis.CryptoKey;
}
+ if (typeof globalThis.crypto[key] === "function") {
+ // @ts-ignore
+ return globalThis.crypto[key].bind(globalThis.crypto);
+ }
return globalThis.crypto[key];
},
},
);
Additional context
No response
Logs
TypeError: Illegal invocation
at Object.getRandomValues (49seyck67av.js:1821:85)
at 49seyck67av.js:1887:15
at randomBits (49seyck67av.js:1888:5)
at generateKey (49seyck67av.js:1906:18)
at 49seyck67av.js:1938:22
at seal (49seyck67av.js:1940:5)
at 49seyck67av.js:2807:104
at updateSession (49seyck67av.js:2809:6)
at getSession (49seyck67av.js:2801:114)
at useSession (49seyck67av.js:2765:9) {
Pooya Parsa commented
Thanks for the nice reproduction and explanations!