Skip to content

Commit 4a158e9

Browse files
Use the native node:wasi module when available (#11025)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
1 parent 0cf696d commit 4a158e9

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

.changeset/yummy-geckos-bet.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@cloudflare/unenv-preset": patch
3+
"wrangler": patch
4+
---
5+
6+
Use the native `node:wasi` module when available
7+
8+
It is enabled when the `enable_nodejs_wasi_module` compatibility flag is set.

packages/unenv-preset/src/preset.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function getCloudflarePreset({
7676
const clusterOverrides = getClusterOverrides(compat);
7777
const traceEventsOverrides = getTraceEventsOverrides(compat);
7878
const domainOverrides = getDomainOverrides(compat);
79+
const wasiOverrides = getWasiOverrides(compat);
7980

8081
// "dynamic" as they depend on the compatibility date and flags
8182
const dynamicNativeModules = [
@@ -88,6 +89,7 @@ export function getCloudflarePreset({
8889
...clusterOverrides.nativeModules,
8990
...traceEventsOverrides.nativeModules,
9091
...domainOverrides.nativeModules,
92+
...wasiOverrides.nativeModules,
9193
];
9294

9395
// "dynamic" as they depend on the compatibility date and flags
@@ -101,6 +103,7 @@ export function getCloudflarePreset({
101103
...clusterOverrides.hybridModules,
102104
...traceEventsOverrides.hybridModules,
103105
...domainOverrides.hybridModules,
106+
...wasiOverrides.hybridModules,
104107
];
105108

106109
return {
@@ -469,3 +472,41 @@ function getDomainOverrides({
469472
hybridModules: [],
470473
};
471474
}
475+
476+
/**
477+
* Returns the overrides for `node:wasi` (unenv or workerd)
478+
*
479+
* The native wasi implementation:
480+
* - is experimental
481+
* - can be enabled with the "enable_nodejs_wasi_module" flag
482+
* - can be disabled with the "disable_nodejs_wasi_module" flag
483+
*/
484+
function getWasiOverrides({
485+
// eslint-disable-next-line unused-imports/no-unused-vars
486+
compatibilityDate,
487+
compatibilityFlags,
488+
}: {
489+
compatibilityDate: string;
490+
compatibilityFlags: string[];
491+
}): { nativeModules: string[]; hybridModules: string[] } {
492+
const disabledByFlag = compatibilityFlags.includes(
493+
"disable_nodejs_wasi_module"
494+
);
495+
496+
// TODO: add `enabledByDate` when a date is defined in workerd
497+
const enabledByFlag =
498+
compatibilityFlags.includes("enable_nodejs_wasi_module") &&
499+
compatibilityFlags.includes("experimental");
500+
501+
const enabled = enabledByFlag && !disabledByFlag;
502+
503+
return enabled
504+
? {
505+
nativeModules: ["wasi"],
506+
hybridModules: [],
507+
}
508+
: {
509+
nativeModules: [],
510+
hybridModules: [],
511+
};
512+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,28 @@ const localTestConfigs: TestConfig[] = [
325325
},
326326
},
327327
],
328+
// wasi
329+
[
330+
// TODO: add test for disabled by date (no date defined yet)
331+
// TODO: add test for enabled by date (no date defined yet)
332+
{
333+
name: "wasi enabled by flag",
334+
compatibilityDate: "2024-09-23",
335+
compatibilityFlags: ["enable_nodejs_wasi_module", "experimental"],
336+
expectRuntimeFlags: {
337+
enable_nodejs_wasi_module: true,
338+
},
339+
},
340+
// TODO: update the date past the default enable date (when defined)
341+
{
342+
name: "wasi disabled by flag",
343+
compatibilityDate: "2024-09-23",
344+
compatibilityFlags: ["disable_nodejs_wasi_module", "experimental"],
345+
expectRuntimeFlags: {
346+
enable_nodejs_wasi_module: false,
347+
},
348+
},
349+
],
328350
].flat() as TestConfig[];
329351

330352
describe.each(localTestConfigs)(

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,18 @@ export const WorkerdTests: Record<string, () => void> = {
607607
assertTypeOf(domain, "exit", "function");
608608
assertTypeOf(domain, "remove", "function");
609609
},
610+
611+
async testWasi() {
612+
const wasi = await import("node:wasi");
613+
614+
assert.strictEqual(typeof wasi.WASI, "function");
615+
616+
assert.throws(() => new wasi.WASI(), /not implemented/);
617+
assert.throws(
618+
() => new wasi.WASI({ version: "preview1" }),
619+
/not implemented/
620+
);
621+
},
610622
};
611623

612624
function assertTypeOf(target: unknown, property: string, expectType: string) {

0 commit comments

Comments
 (0)