Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/native-node-repl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/unenv-preset": minor
---

Add support for native `node:repl` module when the `enable_nodejs_repl_module` and `experimental` compatibility flags are enabled.
39 changes: 39 additions & 0 deletions packages/unenv-preset/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function getCloudflarePreset({
const sqliteOverrides = getSqliteOverrides(compat);
const dgramOverrides = getDgramOverrides(compat);
const streamWrapOverrides = getStreamWrapOverrides(compat);
const replOverrides = getReplOverrides(compat);

// "dynamic" as they depend on the compatibility date and flags
const dynamicNativeModules = [
Expand All @@ -102,6 +103,7 @@ export function getCloudflarePreset({
...sqliteOverrides.nativeModules,
...dgramOverrides.nativeModules,
...streamWrapOverrides.nativeModules,
...replOverrides.nativeModules,
];

// "dynamic" as they depend on the compatibility date and flags
Expand All @@ -122,6 +124,7 @@ export function getCloudflarePreset({
...sqliteOverrides.hybridModules,
...dgramOverrides.hybridModules,
...streamWrapOverrides.hybridModules,
...replOverrides.hybridModules,
];

return {
Expand Down Expand Up @@ -760,3 +763,39 @@ function getStreamWrapOverrides({
hybridModules: [],
};
}

/**
* Returns the overrides for `node:repl` (unenv or workerd)
*
* The native repl implementation:
* - is experimental and has no default enable date
* - can be enabled with the "enable_nodejs_repl_module" flag
* - can be disabled with the "disable_nodejs_repl_module" flag
*/
function getReplOverrides({
compatibilityFlags,
}: {
compatibilityDate: string;
compatibilityFlags: string[];
}): { nativeModules: string[]; hybridModules: string[] } {
const disabledByFlag = compatibilityFlags.includes(
"disable_nodejs_repl_module"
);

const enabledByFlag =
compatibilityFlags.includes("enable_nodejs_repl_module") &&
compatibilityFlags.includes("experimental");

const enabled = enabledByFlag && !disabledByFlag;

// When enabled, use the native `repl` module from workerd
return enabled
? {
nativeModules: ["repl"],
hybridModules: [],
}
: {
nativeModules: [],
hybridModules: [],
};
}
21 changes: 21 additions & 0 deletions packages/wrangler/e2e/unenv-preset/preset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,27 @@ const localTestConfigs: TestConfig[] = [
// },
// },
],
// node:repl (experimental, no default enable date)
[
// TODO: add test for disabled by date (no date defined yet)
// TODO: add test for enabled by date (no date defined yet)
{
name: "repl enabled by flag",
compatibilityDate: "2024-09-23",
compatibilityFlags: ["enable_nodejs_repl_module", "experimental"],
expectRuntimeFlags: {
enable_nodejs_repl_module: true,
},
},
{
name: "repl disabled by flag",
compatibilityDate: "2024-09-23",
compatibilityFlags: ["disable_nodejs_repl_module", "experimental"],
expectRuntimeFlags: {
enable_nodejs_repl_module: false,
},
},
],
].flat() as TestConfig[];

describe.each(localTestConfigs)(
Expand Down
37 changes: 37 additions & 0 deletions packages/wrangler/e2e/unenv-preset/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,43 @@ export const WorkerdTests: Record<string, () => void> = {
// `JSStreamSocket` is the default export of `node:_stream_wrap`
assertTypeOf(streamWrap, "default", "function");
},

async testRepl() {
const repl = await import("node:repl");

// Common exports (both unenv stub and native workerd)
assertTypeOfProperties(repl, {
Copy link
Contributor

@vicb vicb Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we could factor code

for (const target of [repl, repl.default]) {
  assertTypeOfProperties(target, { ... });
}

we could also do that across the file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that in a general refactor PR afterwards?

writer: "function",
start: "function",
Recoverable: "function",
REPLServer: "function",
builtinModules: "object",
_builtinLibs: "object",
REPL_MODE_SLOPPY: "symbol",
REPL_MODE_STRICT: "symbol",
});

assertTypeOfProperties(repl.default, {
writer: "function",
start: "function",
Recoverable: "function",
REPLServer: "function",
builtinModules: "object",
_builtinLibs: "object",
REPL_MODE_SLOPPY: "symbol",
REPL_MODE_STRICT: "symbol",
});

// builtinModules should be an array (not in TypeScript types but exported by both unenv and workerd)
assert.ok(Array.isArray((repl as any).builtinModules));
assert.ok((repl as any).builtinModules.length > 0);

// Both implementations throw when calling start()
assert.throws(
() => repl.start(),
/not implemented|ERR_METHOD_NOT_IMPLEMENTED/
);
},
};

/**
Expand Down
Loading