Skip to content

Commit

Permalink
fix: implement hybrid with alternative method
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 3, 2025
1 parent ee0cdff commit ca451fd
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/presets/cloudflare/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -43,6 +43,7 @@ const cloudflarePages = defineNitroPreset(
esmImport: true,
},
rollupConfig: {
plugins: [hybridNodePlugin],
output: {
entryFileNames: "index.js",
format: "esm",
Expand Down Expand Up @@ -176,6 +177,7 @@ const cloudflareModule = defineNitroPreset(
},
unenv: unenvCfPreset,
rollupConfig: {
plugins: [hybridNodePlugin],
output: {
format: "esm",
exports: "named",
Expand Down Expand Up @@ -211,7 +213,6 @@ const cloudflareDurable = defineNitroPreset(
{
extends: "cloudflare-module",
entry: "./runtime/cloudflare-durable",
unenv: unenvCfPreset,
},
{
name: "cloudflare-durable" as const,
Expand Down
32 changes: 19 additions & 13 deletions src/presets/cloudflare/unenv/preset.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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",
Expand Down Expand Up @@ -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`);
Expand All @@ -60,12 +54,24 @@ export const unenvCfPreset: Preset = {
alias: {
// <id> => node:<id>
...Object.fromEntries(nodeCompatModules.map((m) => [m, `node:${m}`])),
// node:<id> => runtime/<id>.mjs
...Object.fromEntries(hybridNodeCompatModules.map((m) => [m, `node:${m}`])),
// node:<id> => runtime/<id>.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 };
}
},
};
5 changes: 2 additions & 3 deletions src/presets/cloudflare/unenv/runtime/async_hooks.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions src/presets/cloudflare/unenv/runtime/crypto.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -54,9 +56,6 @@ export {
verify,
} from "unenv/runtime/node/crypto/index";

const workerdCrypto =
globalThis["proces" + "s"].getBuiltinModule("node:crypto");

export const {
Certificate,
DiffieHellman,
Expand Down Expand Up @@ -134,7 +133,7 @@ export default {
createVerify,
diffieHellman,
getCipherInfo,
hash,
// hash,
privateDecrypt,
privateEncrypt,
publicDecrypt,
Expand Down
4 changes: 2 additions & 2 deletions src/presets/cloudflare/unenv/runtime/util.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -50,8 +52,6 @@ export {
// styleText,
} from "unenv/runtime/node/util/index";

const workerdUtil = globalThis["proces" + "s"].getBuiltinModule("node:util");

export const {
MIMEParams,
MIMEType,
Expand Down
6 changes: 1 addition & 5 deletions src/presets/cloudflare/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/presets/cloudflare-module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});

Expand Down
6 changes: 5 additions & 1 deletion test/presets/cloudflare-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
Expand Down

0 comments on commit ca451fd

Please sign in to comment.