Skip to content

Commit 0aa959a

Browse files
Use native node:vm module when available (#10621)
1 parent af54c63 commit 0aa959a

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

‎.changeset/upset-ducks-sin.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/unenv-preset": patch
3+
---
4+
5+
Use native node:vm module when available
6+
7+
It is enabled starting on 2025-10-01 or when the `enable_nodejs_vm_module` compatibility flag is set.

‎packages/unenv-preset/src/preset.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export function getCloudflarePreset({
7878
const domainOverrides = getDomainOverrides(compat);
7979
const wasiOverrides = getWasiOverrides(compat);
8080
const consoleOverrides = getConsoleOverrides(compat);
81+
const vmOverrides = getVmOverrides(compat);
8182

8283
// "dynamic" as they depend on the compatibility date and flags
8384
const dynamicNativeModules = [
@@ -92,6 +93,7 @@ export function getCloudflarePreset({
9293
...domainOverrides.nativeModules,
9394
...wasiOverrides.nativeModules,
9495
...consoleOverrides.nativeModules,
96+
...vmOverrides.nativeModules,
9597
];
9698

9799
// "dynamic" as they depend on the compatibility date and flags
@@ -107,6 +109,7 @@ export function getCloudflarePreset({
107109
...domainOverrides.hybridModules,
108110
...wasiOverrides.hybridModules,
109111
...consoleOverrides.hybridModules,
112+
...vmOverrides.hybridModules,
110113
];
111114

112115
return {
@@ -557,3 +560,39 @@ function getConsoleOverrides({
557560
inject: { console: "@cloudflare/unenv-preset/node/console" },
558561
};
559562
}
563+
564+
/**
565+
* Returns the overrides for `node:vm` (unenv or workerd)
566+
*
567+
* The native vm implementation:
568+
* - is enabled starting from 2025-10-01
569+
* - can be enabled with the "enable_nodejs_vm_module" flag
570+
* - can be disabled with the "disable_nodejs_vm_module" flag
571+
*/
572+
function getVmOverrides({
573+
compatibilityDate,
574+
compatibilityFlags,
575+
}: {
576+
compatibilityDate: string;
577+
compatibilityFlags: string[];
578+
}): { nativeModules: string[]; hybridModules: string[] } {
579+
const disabledByFlag = compatibilityFlags.includes(
580+
"disable_nodejs_vm_module"
581+
);
582+
583+
const enabledByFlag = compatibilityFlags.includes("enable_nodejs_vm_module");
584+
const enabledByDate = compatibilityDate >= "2025-10-01";
585+
586+
const enabled = (enabledByFlag || enabledByDate) && !disabledByFlag;
587+
588+
// The native `vm` module implements all the node APIs so we can use it directly
589+
return enabled
590+
? {
591+
nativeModules: ["vm"],
592+
hybridModules: [],
593+
}
594+
: {
595+
nativeModules: [],
596+
hybridModules: [],
597+
};
598+
}

‎packages/wrangler/e2e/unenv-preset/preset.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,39 @@ const localTestConfigs: TestConfig[] = [
377377
},
378378
},
379379
],
380+
// node:vm
381+
[
382+
{
383+
name: "vm disabled by date",
384+
compatibilityDate: "2024-09-23",
385+
expectRuntimeFlags: {
386+
enable_nodejs_vm_module: false,
387+
},
388+
},
389+
{
390+
name: "vm enabled by date",
391+
compatibilityDate: "2025-10-01",
392+
expectRuntimeFlags: {
393+
enable_nodejs_vm_module: true,
394+
},
395+
},
396+
{
397+
name: "vm enabled by flag",
398+
compatibilityDate: "2024-09-23",
399+
compatibilityFlags: ["enable_nodejs_vm_module"],
400+
expectRuntimeFlags: {
401+
enable_nodejs_vm_module: true,
402+
},
403+
},
404+
{
405+
name: "vm disabled by flag",
406+
compatibilityDate: "2025-10-01",
407+
compatibilityFlags: ["disable_nodejs_vm_module"],
408+
expectRuntimeFlags: {
409+
enable_nodejs_vm_module: false,
410+
},
411+
},
412+
],
380413
].flat() as TestConfig[];
381414

382415
describe.each(localTestConfigs)(

‎packages/wrangler/e2e/unenv-preset/worker/index.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,36 @@ export const WorkerdTests: Record<string, () => void> = {
698698
/not implemented/
699699
);
700700
},
701+
702+
async testVm() {
703+
const vm = await import("node:vm");
704+
705+
assertTypeOfProperties(vm, {
706+
Script: "function",
707+
constants: "object",
708+
compileFunction: "function",
709+
createContext: "function",
710+
createScript: "function",
711+
isContext: "function",
712+
measureMemory: "function",
713+
runInContext: "function",
714+
runInThisContext: "function",
715+
runInNewContext: "function",
716+
});
717+
718+
assertTypeOfProperties(vm.default, {
719+
Script: "function",
720+
compileFunction: "function",
721+
constants: "object",
722+
createContext: "function",
723+
isContext: "function",
724+
measureMemory: "function",
725+
runInContext: "function",
726+
runInNewContext: "function",
727+
runInThisContext: "function",
728+
createScript: "function",
729+
});
730+
},
701731
};
702732

703733
/**

0 commit comments

Comments
 (0)