From 33c2ee31db8c851e4ab18ae4d4f14de5360933fe Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 3 Feb 2025 20:42:03 +0100 Subject: [PATCH 1/4] feat(cloudflare): enable native node.js compatibility --- src/presets/cloudflare/preset.ts | 30 +-- src/presets/cloudflare/unenv/preset.ts | 71 +++++++ .../cloudflare/unenv/runtime/async_hooks.mjs | 30 +++ .../cloudflare/unenv/runtime/crypto.mjs | 190 ++++++++++++++++++ src/presets/cloudflare/unenv/runtime/util.mjs | 128 ++++++++++++ src/presets/cloudflare/utils.ts | 7 + test/fixture/wrangler.toml | 2 +- test/presets/cloudflare-module.test.ts | 2 +- test/presets/cloudflare-pages.test.ts | 2 +- 9 files changed, 433 insertions(+), 29 deletions(-) create mode 100644 src/presets/cloudflare/unenv/preset.ts create mode 100644 src/presets/cloudflare/unenv/runtime/async_hooks.mjs create mode 100644 src/presets/cloudflare/unenv/runtime/crypto.mjs create mode 100644 src/presets/cloudflare/unenv/runtime/util.mjs diff --git a/src/presets/cloudflare/preset.ts b/src/presets/cloudflare/preset.ts index cab73b5fc0..64af4baedc 100644 --- a/src/presets/cloudflare/preset.ts +++ b/src/presets/cloudflare/preset.ts @@ -8,20 +8,10 @@ import { writeCFPagesHeaders, writeCFPagesRedirects, } from "./utils"; +import { unenvCfPreset } from "./unenv/preset"; export type { CloudflareOptions as PresetOptions } from "./types"; -const cloudflareExternals = [ - // https://developers.cloudflare.com/email-routing/email-workers/reply-email-workers/ - "cloudflare:email", - // https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/ - "cloudflare:sockets", - // https://developers.cloudflare.com/durable-objects/get-started/walkthrough/ - "cloudflare:workers", - // https://developers.cloudflare.com/workflows/build/workers-api/ - "cloudflare:workflows", -] as const; - // TODO: Remove when wrangler -C support landed // https://github.com/cloudflare/workers-sdk/pull/7994 const isWindows = process.platform === "win32"; @@ -42,9 +32,7 @@ const cloudflarePages = defineNitroPreset( publicDir: "{{ output.dir }}/{{ baseURL }}", serverDir: "{{ output.dir }}/_worker.js", }, - unenv: { - external: [...cloudflareExternals], - }, + unenv: unenvCfPreset, alias: { // Hotfix: Cloudflare appends /index.html if mime is not found and things like ico are not in standard lite.js! // https://github.com/nitrojs/nitro/pull/933 @@ -116,9 +104,6 @@ const cloudflare = defineNitroPreset( wasm: { lazy: true, }, - unenv: { - external: [...cloudflareExternals], - }, hooks: { async compiled(nitro: Nitro) { await writeFile( @@ -156,9 +141,6 @@ const cloudflareModuleLegacy = defineNitroPreset( inlineDynamicImports: false, }, }, - unenv: { - external: [...cloudflareExternals], - }, wasm: { lazy: false, esmImport: true, @@ -192,9 +174,7 @@ const cloudflareModule = defineNitroPreset( preview: commandWithDir("npx wrangler dev"), deploy: commandWithDir("npx wrangler deploy"), }, - unenv: { - external: [...cloudflareExternals], - }, + unenv: unenvCfPreset, rollupConfig: { output: { format: "esm", @@ -231,9 +211,7 @@ const cloudflareDurable = defineNitroPreset( { extends: "cloudflare-module", entry: "./runtime/cloudflare-durable", - unenv: { - external: [...cloudflareExternals], - }, + unenv: unenvCfPreset, }, { name: "cloudflare-durable" as const, diff --git a/src/presets/cloudflare/unenv/preset.ts b/src/presets/cloudflare/unenv/preset.ts new file mode 100644 index 0000000000..bf4f43023a --- /dev/null +++ b/src/presets/cloudflare/unenv/preset.ts @@ -0,0 +1,71 @@ +// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/preset.ts + +import type { Preset } from "unenv"; +import { fileURLToPath } from "mlly"; +import { join } from "pathe"; + +export const cloudflareExternals = [ + "cloudflare:email", + "cloudflare:sockets", + "cloudflare:workers", + "cloudflare:workflows", +] as const; + +// Built-in APIs provided by workerd with nodejs compatibility. +export const nodeCompatModules = [ + "_stream_duplex", + "_stream_passthrough", + "_stream_readable", + "_stream_transform", + "_stream_writable", + "assert", + "assert/strict", + "buffer", + "diagnostics_channel", + "dns", + "dns/promises", + "events", + "net", + "path", + "path/posix", + "path/win32", + "querystring", + "stream", + "stream/consumers", + "stream/promises", + "stream/web", + "string_decoder", + "timers", + "timers/promises", + "url", + "util/types", + "zlib", +]; + +// Modules implemented via a mix of workerd APIs and polyfills. +export const hybridNodeCompatModules = [ + "async_hooks", + "crypto", + "perf_hooks", + "util", + "sys", + "node:sys", +]; + +const presetRuntimeDir = fileURLToPath(new URL("runtime/", import.meta.url)); +const resolvePresetRuntime = (m: string) => join(presetRuntimeDir, `${m}.mjs`); + +export const unenvCfPreset: Preset = { + external: nodeCompatModules.map((m) => `node:${m}`), + alias: { + // => node: + ...Object.fromEntries(nodeCompatModules.map((m) => [m, `node:${m}`])), + // node: => runtime/.mjs + ...Object.fromEntries( + hybridNodeCompatModules.map((m) => [ + `node:${m}`, + resolvePresetRuntime(m === "sys" ? "util" : m), + ]) + ), + }, +}; diff --git a/src/presets/cloudflare/unenv/runtime/async_hooks.mjs b/src/presets/cloudflare/unenv/runtime/async_hooks.mjs new file mode 100644 index 0000000000..17ea4522d2 --- /dev/null +++ b/src/presets/cloudflare/unenv/runtime/async_hooks.mjs @@ -0,0 +1,30 @@ +// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/async_hooks/index.ts + +import { + // asyncWrapProviders, + createHook, + executionAsyncId, + executionAsyncResource, + triggerAsyncId, +} from "unenv/runtime/node/async_hooks/index"; + +export { + // asyncWrapProviders, + createHook, + executionAsyncId, + executionAsyncResource, + triggerAsyncId, +} from "unenv/runtime/node/async_hooks/index"; + +const workerdAsyncHooks = globalThis["proces" + "s"].getBuiltinModule("node:async_hooks"); + +export const { AsyncLocalStorage, AsyncResource } = workerdAsyncHooks; + +export default { + createHook, + executionAsyncId, + executionAsyncResource, + triggerAsyncId, + AsyncLocalStorage, + AsyncResource, +} diff --git a/src/presets/cloudflare/unenv/runtime/crypto.mjs b/src/presets/cloudflare/unenv/runtime/crypto.mjs new file mode 100644 index 0000000000..ed5e538637 --- /dev/null +++ b/src/presets/cloudflare/unenv/runtime/crypto.mjs @@ -0,0 +1,190 @@ +// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/crypto/index.ts + +import { + Cipher, + Cipheriv, + constants, + createCipher, + createCipheriv, + createDecipher, + createDecipheriv, + createECDH, + createSign, + createVerify, + Decipher, + Decipheriv, + diffieHellman, + ECDH, + getCipherInfo, + // hash, + privateDecrypt, + privateEncrypt, + pseudoRandomBytes, + publicDecrypt, + publicEncrypt, + Sign, + sign, + webcrypto as unenvCryptoWebcrypto, + Verify, + verify, +} from "unenv/runtime/node/crypto/index"; + +export { + Cipher, + Cipheriv, + Decipher, + Decipheriv, + ECDH, + Sign, + Verify, + constants, + createCipheriv, + createDecipheriv, + createECDH, + createSign, + createVerify, + diffieHellman, + getCipherInfo, + // hash, + privateDecrypt, + privateEncrypt, + publicDecrypt, + publicEncrypt, + sign, + verify, +} from "unenv/runtime/node/crypto/index"; + +const workerdCrypto = globalThis["proces" + "s"].getBuiltinModule("node:crypto"); + +export const { + Certificate, + DiffieHellman, + DiffieHellmanGroup, + Hash, + Hmac, + KeyObject, + X509Certificate, + checkPrime, + checkPrimeSync, + createDiffieHellman, + createDiffieHellmanGroup, + createHash, + createHmac, + createPrivateKey, + createPublicKey, + createSecretKey, + generateKey, + generateKeyPair, + generateKeyPairSync, + generateKeySync, + generatePrime, + generatePrimeSync, + getCiphers, + getCurves, + getDiffieHellman, + getFips, + getHashes, + hkdf, + hkdfSync, + pbkdf2, + pbkdf2Sync, + randomBytes, + randomFill, + randomFillSync, + randomInt, + randomUUID, + scrypt, + scryptSync, + secureHeapUsed, + setEngine, + setFips, + subtle, + timingSafeEqual, +} = workerdCrypto; + +export const getRandomValues = workerdCrypto.getRandomValues.bind( + workerdCrypto.webcrypto +); + +export const webcrypto = { + CryptoKey: unenvCryptoWebcrypto.CryptoKey, + getRandomValues, + randomUUID, + subtle, +} + +const fips = workerdCrypto.fips; + +export default { + Certificate, + Cipher, + Cipheriv, + Decipher, + Decipheriv, + ECDH, + Sign, + Verify, + X509Certificate, + constants, + createCipheriv, + createDecipheriv, + createECDH, + createSign, + createVerify, + diffieHellman, + getCipherInfo, + hash, + privateDecrypt, + privateEncrypt, + publicDecrypt, + publicEncrypt, + scrypt, + scryptSync, + sign, + verify, + createCipher, + createDecipher, + pseudoRandomBytes, + DiffieHellman, + DiffieHellmanGroup, + Hash, + Hmac, + KeyObject, + checkPrime, + checkPrimeSync, + createDiffieHellman, + createDiffieHellmanGroup, + createHash, + createHmac, + createPrivateKey, + createPublicKey, + createSecretKey, + generateKey, + generateKeyPair, + generateKeyPairSync, + generateKeySync, + generatePrime, + generatePrimeSync, + getCiphers, + getCurves, + getDiffieHellman, + getFips, + getHashes, + getRandomValues, + hkdf, + hkdfSync, + pbkdf2, + pbkdf2Sync, + randomBytes, + randomFill, + randomFillSync, + randomInt, + randomUUID, + secureHeapUsed, + setEngine, + setFips, + subtle, + timingSafeEqual, + fips, + webcrypto, +} diff --git a/src/presets/cloudflare/unenv/runtime/util.mjs b/src/presets/cloudflare/unenv/runtime/util.mjs new file mode 100644 index 0000000000..2f42ecd7da --- /dev/null +++ b/src/presets/cloudflare/unenv/runtime/util.mjs @@ -0,0 +1,128 @@ +// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/util/index.ts + +import { + _errnoException, + _exceptionWithHostPort, + getSystemErrorMap, + getSystemErrorName, + isArray, + isBoolean, + isBuffer, + isDate, + isDeepStrictEqual, + isError, + isFunction, + isNull, + isNullOrUndefined, + isNumber, + isObject, + isPrimitive, + isRegExp, + isString, + isSymbol, + isUndefined, + // parseEnv, + // styleText, +} from "unenv/runtime/node/util/index"; + +export { + _errnoException, + _exceptionWithHostPort, + getSystemErrorMap, + getSystemErrorName, + isArray, + isBoolean, + isBuffer, + isDate, + isDeepStrictEqual, + isError, + isFunction, + isNull, + isNullOrUndefined, + isNumber, + isObject, + isPrimitive, + isRegExp, + isString, + isSymbol, + isUndefined, + // parseEnv, + // styleText, +} from "unenv/runtime/node/util/index"; + +const workerdUtil = globalThis["proces" + "s"].getBuiltinModule("node:util"); + +export const { + MIMEParams, + MIMEType, + TextDecoder, + TextEncoder, + _extend, + aborted, + callbackify, + debug, + debuglog, + deprecate, + format, + formatWithOptions, + getCallSite, + inherits, + inspect, + log, + parseArgs, + promisify, + stripVTControlCharacters, + toUSVString, + transferableAbortController, + transferableAbortSignal, +} = workerdUtil; + +export const types = workerdUtil.types; + +export default { + _errnoException, + _exceptionWithHostPort, + getSystemErrorMap, + getSystemErrorName, + isArray, + isBoolean, + isBuffer, + isDate, + isDeepStrictEqual, + isError, + isFunction, + isNull, + isNullOrUndefined, + isNumber, + isObject, + isPrimitive, + isRegExp, + isString, + isSymbol, + isUndefined, + // parseEnv, + // styleText, + MIMEParams, + MIMEType, + TextDecoder, + TextEncoder, + _extend, + aborted, + callbackify, + debug, + debuglog, + deprecate, + format, + formatWithOptions, + getCallSite, + inherits, + inspect, + log, + parseArgs, + promisify, + stripVTControlCharacters, + toUSVString, + transferableAbortController, + transferableAbortSignal, + types, +} diff --git a/src/presets/cloudflare/utils.ts b/src/presets/cloudflare/utils.ts index 4622f65f31..936c504acd 100644 --- a/src/presets/cloudflare/utils.ts +++ b/src/presets/cloudflare/utils.ts @@ -190,6 +190,13 @@ export async function writeWranglerConfig(nitro: Nitro, isPages: boolean) { nitro.options.compatibilityDate.cloudflare || nitro.options.compatibilityDate.default; + // Node.js compatibility + defaults.compatibility_flags = [ + // "nodejs_compat", + "nodejs_compat_v2", + // "no_nodejs_compat_v2", + ]; + if (isPages) { // Pages defaults.pages_build_output_dir = relative( diff --git a/test/fixture/wrangler.toml b/test/fixture/wrangler.toml index 84d615edf3..af80923610 100644 --- a/test/fixture/wrangler.toml +++ b/test/fixture/wrangler.toml @@ -1,3 +1,3 @@ name = "nitro-test" compatibility_date = "2024-09-19" -assets = { directory = "./.output/public/", binding = "ASSETS"} +# assets = { directory = "./.output/public/", binding = "ASSETS"} diff --git a/test/presets/cloudflare-module.test.ts b/test/presets/cloudflare-module.test.ts index 927cf441f6..328a9cbd44 100644 --- a/test/presets/cloudflare-module.test.ts +++ b/test/presets/cloudflare-module.test.ts @@ -22,7 +22,7 @@ describe("nitro:preset:cloudflare-module", async () => { not_found_handling: "none" /* default */, }, }, - compatibilityFlags: ["streams_enable_constructors"], + compatibilityFlags: ["streams_enable_constructors", "nodejs_compat"], bindings: { ...ctx.env }, }); diff --git a/test/presets/cloudflare-pages.test.ts b/test/presets/cloudflare-pages.test.ts index 4b5a3d5af6..2388617cbe 100644 --- a/test/presets/cloudflare-pages.test.ts +++ b/test/presets/cloudflare-pages.test.ts @@ -15,7 +15,7 @@ describe.skipIf(isWindows)("nitro:preset:cloudflare-pages", async () => { modules: true, scriptPath: resolve(ctx.outDir, "_worker.js", "index.js"), modulesRules: [{ type: "CompiledWasm", include: ["**/*.wasm"] }], - compatibilityFlags: ["streams_enable_constructors"], + compatibilityFlags: ["streams_enable_constructors", "nodejs_compat"], sitePath: "", bindings: { ...ctx.env }, }); From ee0cdff8e4a0beba517946caab5360ea2ee2709f Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 3 Feb 2025 19:51:49 +0000 Subject: [PATCH 2/4] chore: apply automated updates --- src/presets/cloudflare/unenv/runtime/async_hooks.mjs | 5 +++-- src/presets/cloudflare/unenv/runtime/crypto.mjs | 7 ++++--- src/presets/cloudflare/unenv/runtime/util.mjs | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/presets/cloudflare/unenv/runtime/async_hooks.mjs b/src/presets/cloudflare/unenv/runtime/async_hooks.mjs index 17ea4522d2..65a20715e7 100644 --- a/src/presets/cloudflare/unenv/runtime/async_hooks.mjs +++ b/src/presets/cloudflare/unenv/runtime/async_hooks.mjs @@ -16,7 +16,8 @@ export { triggerAsyncId, } from "unenv/runtime/node/async_hooks/index"; -const workerdAsyncHooks = globalThis["proces" + "s"].getBuiltinModule("node:async_hooks"); +const workerdAsyncHooks = + globalThis["proces" + "s"].getBuiltinModule("node:async_hooks"); export const { AsyncLocalStorage, AsyncResource } = workerdAsyncHooks; @@ -27,4 +28,4 @@ export default { triggerAsyncId, AsyncLocalStorage, AsyncResource, -} +}; diff --git a/src/presets/cloudflare/unenv/runtime/crypto.mjs b/src/presets/cloudflare/unenv/runtime/crypto.mjs index ed5e538637..5087c1d0bd 100644 --- a/src/presets/cloudflare/unenv/runtime/crypto.mjs +++ b/src/presets/cloudflare/unenv/runtime/crypto.mjs @@ -54,7 +54,8 @@ export { verify, } from "unenv/runtime/node/crypto/index"; -const workerdCrypto = globalThis["proces" + "s"].getBuiltinModule("node:crypto"); +const workerdCrypto = + globalThis["proces" + "s"].getBuiltinModule("node:crypto"); export const { Certificate, @@ -111,7 +112,7 @@ export const webcrypto = { getRandomValues, randomUUID, subtle, -} +}; const fips = workerdCrypto.fips; @@ -187,4 +188,4 @@ export default { timingSafeEqual, fips, webcrypto, -} +}; diff --git a/src/presets/cloudflare/unenv/runtime/util.mjs b/src/presets/cloudflare/unenv/runtime/util.mjs index 2f42ecd7da..e19953d3a9 100644 --- a/src/presets/cloudflare/unenv/runtime/util.mjs +++ b/src/presets/cloudflare/unenv/runtime/util.mjs @@ -125,4 +125,4 @@ export default { transferableAbortController, transferableAbortSignal, types, -} +}; From ca451fd0af4ffaf0c5fca19c12a141d6f0b10d07 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 3 Feb 2025 21:46:31 +0100 Subject: [PATCH 3/4] fix: implement hybrid with alternative method --- src/presets/cloudflare/preset.ts | 5 +-- src/presets/cloudflare/unenv/preset.ts | 32 +++++++++++-------- .../cloudflare/unenv/runtime/async_hooks.mjs | 5 ++- .../cloudflare/unenv/runtime/crypto.mjs | 7 ++-- src/presets/cloudflare/unenv/runtime/util.mjs | 4 +-- src/presets/cloudflare/utils.ts | 6 +--- test/presets/cloudflare-module.test.ts | 6 +++- test/presets/cloudflare-pages.test.ts | 6 +++- 8 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/presets/cloudflare/preset.ts b/src/presets/cloudflare/preset.ts index 64af4baedc..207f4b4de0 100644 --- a/src/presets/cloudflare/preset.ts +++ b/src/presets/cloudflare/preset.ts @@ -8,7 +8,7 @@ import { writeCFPagesHeaders, writeCFPagesRedirects, } from "./utils"; -import { unenvCfPreset } from "./unenv/preset"; +import { hybridNodePlugin, unenvCfPreset } from "./unenv/preset"; export type { CloudflareOptions as PresetOptions } from "./types"; @@ -43,6 +43,7 @@ const cloudflarePages = defineNitroPreset( esmImport: true, }, rollupConfig: { + plugins: [hybridNodePlugin], output: { entryFileNames: "index.js", format: "esm", @@ -176,6 +177,7 @@ const cloudflareModule = defineNitroPreset( }, unenv: unenvCfPreset, rollupConfig: { + plugins: [hybridNodePlugin], output: { format: "esm", exports: "named", @@ -211,7 +213,6 @@ const cloudflareDurable = defineNitroPreset( { extends: "cloudflare-module", entry: "./runtime/cloudflare-durable", - unenv: unenvCfPreset, }, { name: "cloudflare-durable" as const, diff --git a/src/presets/cloudflare/unenv/preset.ts b/src/presets/cloudflare/unenv/preset.ts index bf4f43023a..47e93c748e 100644 --- a/src/presets/cloudflare/unenv/preset.ts +++ b/src/presets/cloudflare/unenv/preset.ts @@ -1,6 +1,6 @@ -// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/preset.ts - import type { Preset } from "unenv"; +import type { Plugin } from "rollup"; + import { fileURLToPath } from "mlly"; import { join } from "pathe"; @@ -11,7 +11,8 @@ export const cloudflareExternals = [ "cloudflare:workflows", ] as const; -// Built-in APIs provided by workerd with nodejs compatibility. +// Built-in APIs provided by workerd with nodejs compatibility +// https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/preset.ts export const nodeCompatModules = [ "_stream_duplex", "_stream_passthrough", @@ -42,15 +43,8 @@ export const nodeCompatModules = [ "zlib", ]; -// Modules implemented via a mix of workerd APIs and polyfills. -export const hybridNodeCompatModules = [ - "async_hooks", - "crypto", - "perf_hooks", - "util", - "sys", - "node:sys", -]; +// Modules implemented via a mix of workerd APIs and polyfills +export const hybridNodeCompatModules = ["async_hooks", "crypto", "util"]; const presetRuntimeDir = fileURLToPath(new URL("runtime/", import.meta.url)); const resolvePresetRuntime = (m: string) => join(presetRuntimeDir, `${m}.mjs`); @@ -60,12 +54,24 @@ export const unenvCfPreset: Preset = { alias: { // => node: ...Object.fromEntries(nodeCompatModules.map((m) => [m, `node:${m}`])), - // node: => runtime/.mjs + ...Object.fromEntries(hybridNodeCompatModules.map((m) => [m, `node:${m}`])), + // node: => runtime/.mjs (hybrid) ...Object.fromEntries( hybridNodeCompatModules.map((m) => [ `node:${m}`, resolvePresetRuntime(m === "sys" ? "util" : m), ]) ), + sys: resolvePresetRuntime("util"), + "node:sys": resolvePresetRuntime("util"), + }, +}; + +export const hybridNodePlugin: Plugin = { + name: "nitro:cloudflare:hybrid-node-compat", + resolveId(id) { + if (id.startsWith("#workerd/node:")) { + return { id: id.slice("#workerd/".length), external: true }; + } }, }; diff --git a/src/presets/cloudflare/unenv/runtime/async_hooks.mjs b/src/presets/cloudflare/unenv/runtime/async_hooks.mjs index 65a20715e7..726b32bf55 100644 --- a/src/presets/cloudflare/unenv/runtime/async_hooks.mjs +++ b/src/presets/cloudflare/unenv/runtime/async_hooks.mjs @@ -1,5 +1,7 @@ // https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/async_hooks/index.ts +import workerdAsyncHooks from "#workerd/node:async_hooks" + import { // asyncWrapProviders, createHook, @@ -16,9 +18,6 @@ export { triggerAsyncId, } from "unenv/runtime/node/async_hooks/index"; -const workerdAsyncHooks = - globalThis["proces" + "s"].getBuiltinModule("node:async_hooks"); - export const { AsyncLocalStorage, AsyncResource } = workerdAsyncHooks; export default { diff --git a/src/presets/cloudflare/unenv/runtime/crypto.mjs b/src/presets/cloudflare/unenv/runtime/crypto.mjs index 5087c1d0bd..8ecd9b5c37 100644 --- a/src/presets/cloudflare/unenv/runtime/crypto.mjs +++ b/src/presets/cloudflare/unenv/runtime/crypto.mjs @@ -1,5 +1,7 @@ // https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/crypto/index.ts +import workerdCrypto from "#workerd/node:crypto" + import { Cipher, Cipheriv, @@ -54,9 +56,6 @@ export { verify, } from "unenv/runtime/node/crypto/index"; -const workerdCrypto = - globalThis["proces" + "s"].getBuiltinModule("node:crypto"); - export const { Certificate, DiffieHellman, @@ -134,7 +133,7 @@ export default { createVerify, diffieHellman, getCipherInfo, - hash, + // hash, privateDecrypt, privateEncrypt, publicDecrypt, diff --git a/src/presets/cloudflare/unenv/runtime/util.mjs b/src/presets/cloudflare/unenv/runtime/util.mjs index e19953d3a9..612fc4d8a2 100644 --- a/src/presets/cloudflare/unenv/runtime/util.mjs +++ b/src/presets/cloudflare/unenv/runtime/util.mjs @@ -1,5 +1,7 @@ // https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/util/index.ts +import workerdUtil from "#workerd/node:util"; + import { _errnoException, _exceptionWithHostPort, @@ -50,8 +52,6 @@ export { // styleText, } from "unenv/runtime/node/util/index"; -const workerdUtil = globalThis["proces" + "s"].getBuiltinModule("node:util"); - export const { MIMEParams, MIMEType, diff --git a/src/presets/cloudflare/utils.ts b/src/presets/cloudflare/utils.ts index 936c504acd..f88afcb5bb 100644 --- a/src/presets/cloudflare/utils.ts +++ b/src/presets/cloudflare/utils.ts @@ -191,11 +191,7 @@ export async function writeWranglerConfig(nitro: Nitro, isPages: boolean) { nitro.options.compatibilityDate.default; // Node.js compatibility - defaults.compatibility_flags = [ - // "nodejs_compat", - "nodejs_compat_v2", - // "no_nodejs_compat_v2", - ]; + defaults.compatibility_flags = ["nodejs_compat", "no_nodejs_compat_v2"]; if (isPages) { // Pages diff --git a/test/presets/cloudflare-module.test.ts b/test/presets/cloudflare-module.test.ts index 328a9cbd44..49e1d38e14 100644 --- a/test/presets/cloudflare-module.test.ts +++ b/test/presets/cloudflare-module.test.ts @@ -22,7 +22,11 @@ describe("nitro:preset:cloudflare-module", async () => { not_found_handling: "none" /* default */, }, }, - compatibilityFlags: ["streams_enable_constructors", "nodejs_compat"], + compatibilityFlags: [ + "streams_enable_constructors", + "nodejs_compat", + "no_nodejs_compat_v2", + ], bindings: { ...ctx.env }, }); diff --git a/test/presets/cloudflare-pages.test.ts b/test/presets/cloudflare-pages.test.ts index 2388617cbe..3a5a70965a 100644 --- a/test/presets/cloudflare-pages.test.ts +++ b/test/presets/cloudflare-pages.test.ts @@ -15,7 +15,11 @@ describe.skipIf(isWindows)("nitro:preset:cloudflare-pages", async () => { modules: true, scriptPath: resolve(ctx.outDir, "_worker.js", "index.js"), modulesRules: [{ type: "CompiledWasm", include: ["**/*.wasm"] }], - compatibilityFlags: ["streams_enable_constructors", "nodejs_compat"], + compatibilityFlags: [ + "streams_enable_constructors", + "nodejs_compat", + "no_nodejs_compat_v2", + ], sitePath: "", bindings: { ...ctx.env }, }); From 7a677933ee0f4f8742beddd501d5ff96c455752c Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 3 Feb 2025 20:47:17 +0000 Subject: [PATCH 4/4] chore: apply automated updates --- src/presets/cloudflare/unenv/runtime/async_hooks.mjs | 2 +- src/presets/cloudflare/unenv/runtime/crypto.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/presets/cloudflare/unenv/runtime/async_hooks.mjs b/src/presets/cloudflare/unenv/runtime/async_hooks.mjs index 726b32bf55..6660ec7aaf 100644 --- a/src/presets/cloudflare/unenv/runtime/async_hooks.mjs +++ b/src/presets/cloudflare/unenv/runtime/async_hooks.mjs @@ -1,6 +1,6 @@ // https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/async_hooks/index.ts -import workerdAsyncHooks from "#workerd/node:async_hooks" +import workerdAsyncHooks from "#workerd/node:async_hooks"; import { // asyncWrapProviders, diff --git a/src/presets/cloudflare/unenv/runtime/crypto.mjs b/src/presets/cloudflare/unenv/runtime/crypto.mjs index 8ecd9b5c37..ed3bcc66ab 100644 --- a/src/presets/cloudflare/unenv/runtime/crypto.mjs +++ b/src/presets/cloudflare/unenv/runtime/crypto.mjs @@ -1,6 +1,6 @@ // https://github.com/cloudflare/workers-sdk/blob/main/packages/unenv-preset/src/runtime/node/crypto/index.ts -import workerdCrypto from "#workerd/node:crypto" +import workerdCrypto from "#workerd/node:crypto"; import { Cipher,