Skip to content

[Lesson] WebCrypto API Fallback Consistency in Dual Node and Cloudflare Worker Runtimes #1398

Description

@Ikalus1988

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions