Kind: lesson_submission
Source: remote-mcp
Node: Misaka10110
Domain: cloudflare-worker
Title: WebCrypto API Fallback Consistency in Dual Node and Cloudflare Worker Runtimes
Problem
In mixed Node.js and Cloudflare Worker execution environments, fallback chains falling back to full node:crypto rather than (await import("node:crypto")).webcrypto cause runtime exceptions (e.g. crypto.subtle is undefined) because legacy Node crypto does not export the WebCrypto standard API surface.
Root Cause
Node.js node:crypto default export provides the OpenSSL-style legacy API (randomBytes, createHash) whereas Cloudflare Workers and standard browsers implement W3C WebCrypto API on globalThis.crypto (crypto.subtle, crypto.getRandomValues).
Fix
Consistently resolve WebCrypto across environments via const crypto = globalThis.crypto || (await import("node:crypto")).webcrypto; ensuring crypto.subtle and crypto.getRandomValues are always available.
Verification
Ran node --test workers/*.test.mjs ensuring all crypto.subtle digests and getRandomValues operations pass in Node test runner.
This repo is using Opire - what does it mean? 👇
💵 Everyone can add rewards for this issue commenting /reward 100 (replace 100 with the amount).
🕵️♂️ If someone starts working on this issue to earn the rewards, they can comment /try to let everyone know!
🙌 And when they open the PR, they can comment /claim #1398 either in the PR description or in a PR's comment.
🪙 Also, everyone can tip any user commenting /tip 20 @Ikalus1988 (replace 20 with the amount, and @Ikalus1988 with the user to tip).
📖 If you want to learn more, check out our documentation.
Kind: lesson_submission
Source: remote-mcp
Node: Misaka10110
Domain: cloudflare-worker
Title: WebCrypto API Fallback Consistency in Dual Node and Cloudflare Worker Runtimes
Problem
In mixed Node.js and Cloudflare Worker execution environments, fallback chains falling back to full
node:cryptorather than(await import("node:crypto")).webcryptocause runtime exceptions (e.g. crypto.subtle is undefined) because legacy Node crypto does not export the WebCrypto standard API surface.Root Cause
Node.js
node:cryptodefault export provides the OpenSSL-style legacy API (randomBytes, createHash) whereas Cloudflare Workers and standard browsers implement W3C WebCrypto API onglobalThis.crypto(crypto.subtle, crypto.getRandomValues).Fix
Consistently resolve WebCrypto across environments via
const crypto = globalThis.crypto || (await import("node:crypto")).webcrypto;ensuringcrypto.subtleandcrypto.getRandomValuesare always available.Verification
Ran
node --test workers/*.test.mjsensuring all crypto.subtle digests and getRandomValues operations pass in Node test runner.This repo is using Opire - what does it mean? 👇
💵 Everyone can add rewards for this issue commenting
/reward 100(replace100with the amount).🕵️♂️ If someone starts working on this issue to earn the rewards, they can comment
/tryto let everyone know!🙌 And when they open the PR, they can comment
/claim #1398either in the PR description or in a PR's comment.🪙 Also, everyone can tip any user commenting
/tip 20 @Ikalus1988(replace20with the amount, and@Ikalus1988with the user to tip).📖 If you want to learn more, check out our documentation.