Skip to content

Commit 9a27f52

Browse files
penalosaCarmenPopoviciu
authored andcommitted
Remove NodeJSCompatModule
1 parent 8e3688f commit 9a27f52

File tree

12 files changed

+38
-49
lines changed

12 files changed

+38
-49
lines changed

.changeset/violet-ravens-draw.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"miniflare": patch
3+
"@cloudflare/vitest-pool-workers": patch
4+
"wrangler": patch
5+
---
6+
7+
Remove `NodeJSCompatModule`. This was never fully supported, and never worked for deploying Workers from Wrangler.

packages/miniflare/src/plugins/core/modules.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const SUGGEST_NODE =
2121
"that uses Node.js built-ins, you'll either need to:" +
2222
"\n- Bundle your Worker, configuring your bundler to polyfill Node.js built-ins" +
2323
"\n- Configure your bundler to load Workers-compatible builds by changing the main fields/conditions" +
24-
"\n- Enable the `nodejs_compat` compatibility flag and use the `NodeJsCompatModule` module type" +
24+
"\n- Enable the `nodejs_compat` compatibility flag" +
2525
"\n- Find an alternative package that doesn't require Node.js built-ins";
2626

2727
const builtinModulesWithPrefix = builtinModules.concat(
@@ -43,7 +43,6 @@ export function maybeGetStringScriptPathIndex(
4343
export const ModuleRuleTypeSchema = z.enum([
4444
"ESModule",
4545
"CommonJS",
46-
"NodeJsCompatModule",
4746
"Text",
4847
"Data",
4948
"CompiledWasm",
@@ -52,7 +51,7 @@ export const ModuleRuleTypeSchema = z.enum([
5251
]);
5352
export type ModuleRuleType = z.infer<typeof ModuleRuleTypeSchema>;
5453

55-
type JavaScriptModuleRuleType = "ESModule" | "CommonJS" | "NodeJsCompatModule";
54+
type JavaScriptModuleRuleType = "ESModule" | "CommonJS";
5655

5756
export const ModuleRuleSchema = z.object({
5857
type: ModuleRuleTypeSchema,
@@ -291,15 +290,14 @@ ${dim(modulesConfig)}`;
291290
}
292291
const spec = specExpression.value;
293292

294-
const isNodeJsCompatModule = referencingType === "NodeJsCompatModule";
295293
if (
296294
// `cloudflare:` and `workerd:` imports don't need to be included explicitly
297295
spec.startsWith("cloudflare:") ||
298296
spec.startsWith("workerd:") ||
299297
// Node.js compat v1 requires imports to be prefixed with `node:`
300298
(this.#nodejsCompatMode === "v1" && spec.startsWith("node:")) ||
301299
// Node.js compat modules and v2 can also handle non-prefixed imports
302-
((this.#nodejsCompatMode === "v2" || isNodeJsCompatModule) &&
300+
(this.#nodejsCompatMode === "v2" &&
303301
builtinModulesWithPrefix.includes(spec)) ||
304302
// Async Local Storage mode (node_als) only deals with `node:async_hooks` imports
305303
(this.#nodejsCompatMode === "als" && spec === "node:async_hooks") ||
@@ -345,7 +343,6 @@ ${dim(modulesConfig)}`;
345343
switch (rule.type) {
346344
case "ESModule":
347345
case "CommonJS":
348-
case "NodeJsCompatModule":
349346
const code = data.toString("utf8");
350347
this.#visitJavaScriptModule(code, identifier, rule.type);
351348
break;
@@ -383,8 +380,6 @@ function createJavaScriptModule(
383380
return { name, esModule: code };
384381
} else if (type === "CommonJS") {
385382
return { name, commonJsModule: code };
386-
} else if (type === "NodeJsCompatModule") {
387-
return { name, nodeJsCompatModule: code };
388383
}
389384
// noinspection UnnecessaryLocalVariableJS
390385
const exhaustive: never = type;
@@ -409,7 +404,6 @@ export function convertModuleDefinition(
409404
switch (def.type) {
410405
case "ESModule":
411406
case "CommonJS":
412-
case "NodeJsCompatModule":
413407
return createJavaScriptModule(
414408
contentsToString(contents),
415409
name,
@@ -441,8 +435,6 @@ function convertWorkerModule(mod: Worker_Module): ModuleDefinition {
441435

442436
if ("esModule" in m) return { path, type: "ESModule" };
443437
else if ("commonJsModule" in m) return { path, type: "CommonJS" };
444-
else if ("nodeJsCompatModule" in m)
445-
return { path, type: "NodeJsCompatModule" };
446438
else if ("text" in m) return { path, type: "Text" };
447439
else if ("data" in m) return { path, type: "Data" };
448440
else if ("wasm" in m) return { path, type: "CompiledWasm" };

packages/miniflare/src/runtime/config/workerd.ts

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export type Worker_Module = {
7878
| { data?: Uint8Array }
7979
| { wasm?: Uint8Array }
8080
| { json?: string }
81-
| { nodeJsCompatModule?: string }
8281
| { pythonModule?: string }
8382
| { pythonRequirement?: string }
8483
);

packages/miniflare/test/fixtures/modules/index.node.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const assert = require("assert");
33
// Test `require()` of Node built-in module with prefix
44
const assert2 = require("node:assert");
55

6-
// `Buffer` should be global in `NodeJsCompatModule`s
6+
// `Buffer` should be global in `CommonJS`s with node_compat_v2 turned on
77
assert.strictEqual(typeof Buffer, "function");
88
assert2(true);
99

packages/miniflare/test/plugins/core/modules.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test("Miniflare: accepts manually defined modules", async (t) => {
2020
// Check with just `path`
2121
const mf = new Miniflare({
2222
compatibilityDate: "2023-08-01",
23-
compatibilityFlags: ["nodejs_compat"],
23+
compatibilityFlags: ["nodejs_compat_v2"],
2424
// TODO(soon): remove `modulesRoot` once https://github.com/cloudflare/workerd/issues/1101 fixed
2525
// and add separate test for that
2626
modulesRoot: ROOT,
@@ -29,7 +29,7 @@ test("Miniflare: accepts manually defined modules", async (t) => {
2929
{ type: "ESModule", path: path.join(ROOT, "blobs.mjs") },
3030
{ type: "ESModule", path: path.join(ROOT, "blobs-indirect.mjs") },
3131
{ type: "CommonJS", path: path.join(ROOT, "index.cjs") },
32-
{ type: "NodeJsCompatModule", path: path.join(ROOT, "index.node.cjs") },
32+
{ type: "CommonJS", path: path.join(ROOT, "index.node.cjs") },
3333
// Testing modules in subdirectories
3434
{ type: "Text", path: path.join(ROOT, "blobs", "text.txt") },
3535
{ type: "Data", path: path.join(ROOT, "blobs", "data.bin") },
@@ -51,7 +51,7 @@ test("Miniflare: accepts manually defined modules", async (t) => {
5151
"AGFzbQEAAAABBwFgAn9/AX8DAgEABwcBA2FkZAAACgkBBwAgACABawsACgRuYW1lAgMBAAA=";
5252
await mf.setOptions({
5353
compatibilityDate: "2023-08-01",
54-
compatibilityFlags: ["nodejs_compat"],
54+
compatibilityFlags: ["nodejs_compat_v2"],
5555
modules: [
5656
{ type: "ESModule", path: path.join(ROOT, "index.mjs") },
5757
{
@@ -79,7 +79,7 @@ test("Miniflare: accepts manually defined modules", async (t) => {
7979
`,
8080
},
8181
{
82-
type: "NodeJsCompatModule",
82+
type: "CommonJS",
8383
path: path.join(ROOT, "index.node.cjs"),
8484
contents: `module.exports = "node:";`,
8585
},
@@ -112,14 +112,14 @@ test("Miniflare: automatically collects modules", async (t) => {
112112
modules: true,
113113
modulesRoot: ROOT,
114114
modulesRules: [
115-
// Implicitly testing default module rules for `ESModule` and `CommonJS`
116-
{ type: "NodeJsCompatModule", include: ["**/*.node.cjs"] },
115+
// Implicitly testing default module rules for `ESModule`
116+
{ type: "CommonJS", include: ["**/*.node.cjs", "**/*.cjs"] },
117117
{ type: "Text", include: ["**/*.txt"] },
118118
{ type: "Data", include: ["**/*.bin"] },
119119
{ type: "CompiledWasm", include: ["**/*.wasm"] },
120120
],
121121
compatibilityDate: "2023-08-01",
122-
compatibilityFlags: ["nodejs_compat"],
122+
compatibilityFlags: ["nodejs_compat_v2"],
123123
scriptPath: path.join(ROOT, "index.mjs"),
124124
});
125125
t.teardown(() => mf.dispose());
@@ -207,13 +207,13 @@ test("Miniflare: cannot automatically collect modules from dynamic import expres
207207
modulesRoot: ROOT,
208208
modulesRules: [
209209
// Implicitly testing default module rules for `ESModule` and `CommonJS`
210-
{ type: "NodeJsCompatModule", include: ["**/*.node.cjs"] },
210+
{ type: "CommonJS", include: ["**/*.node.cjs", "**/*.cjs"] },
211211
{ type: "Text", include: ["**/*.txt"] },
212212
{ type: "Data", include: ["**/*.bin"] },
213213
{ type: "CompiledWasm", include: ["**/*.wasm"] },
214214
],
215215
compatibilityDate: "2023-08-01",
216-
compatibilityFlags: ["nodejs_compat"],
216+
compatibilityFlags: ["nodejs_compat_v2"],
217217
scriptPath,
218218
});
219219

@@ -233,7 +233,7 @@ You must manually define your modules when constructing Miniflare:
233233
modules: [
234234
{ type: "ESModule", path: "index-dynamic.mjs" },
235235
{ type: "CommonJS", path: "index.cjs" },
236-
{ type: "NodeJsCompatModule", path: "index.node.cjs" },
236+
{ type: "CommonJS", path: "index.node.cjs" },
237237
{ type: "ESModule", path: "blobs-indirect.mjs" },
238238
{ type: "ESModule", path: "blobs.mjs" },
239239
{ type: "Text", path: "blobs/text.txt" },

packages/vitest-pool-workers/src/pool/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,8 @@ function buildProjectWorkerOptions(
372372
runnerWorker.compatibilityFlags
373373
);
374374

375-
if (mode !== "v1" && mode !== "v2") {
376-
runnerWorker.compatibilityFlags.push(
377-
"nodejs_compat",
378-
"no_nodejs_compat_v2"
379-
);
375+
if (mode !== "v2") {
376+
runnerWorker.compatibilityFlags.push("nodejs_compat_v2");
380377
}
381378

382379
// Required for `workerd:unsafe` module. We don't require this flag to be set

packages/vitest-pool-workers/src/pool/module-fallback.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,6 @@ function maybeGetForceTypeModuleContents(
409409
return { esModule: contents.toString() };
410410
case "CommonJS":
411411
return { commonJsModule: contents.toString() };
412-
case "NodeJsCompatModule":
413-
return { nodeJsCompatModule: contents.toString() };
414412
case "Text":
415413
return { text: contents.toString() };
416414
case "Data":
@@ -510,8 +508,8 @@ async function load(
510508
// Respond with CommonJS module
511509

512510
// If we're `import`ing a CommonJS module, or we're `require`ing a `node:*`
513-
// module from a NodeJsCompatModule, return an ES module shim. Note
514-
// NodeJsCompatModules can `require` ES modules, using the default export.
511+
// module from a CommonJS, return an ES module shim. Note
512+
// CommonJS can `require` ES modules, using the default export.
515513
const insertCjsEsmShim = method === "import" || specifier.startsWith("node:");
516514
if (insertCjsEsmShim && !disableCjsEsmShim) {
517515
const fileName = posixPath.basename(filePath);
@@ -526,9 +524,9 @@ async function load(
526524
}
527525

528526
// Otherwise, if we're `require`ing a non-`node:*` module, just return a
529-
// NodeJsCompatModule
527+
// CommonJS
530528
debuglog(logBase, "cjs:", filePath);
531-
return buildModuleResponse(target, { nodeJsCompatModule: contents });
529+
return buildModuleResponse(target, { commonJsModule: contents });
532530
}
533531

534532
export async function handleModuleFallbackRequest(

packages/wrangler/src/__tests__/api/startDevWorker/LocalRuntimeController.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe("LocalRuntimeController", () => {
149149
const config = {
150150
name: "worker",
151151
entrypoint: "NOT_REAL",
152-
compatibilityFlags: ["nodejs_compat"],
152+
compatibilityFlags: ["nodejs_compat_v2"],
153153
compatibilityDate: "2023-10-01",
154154
};
155155
const bundle: Bundle = {
@@ -171,7 +171,7 @@ describe("LocalRuntimeController", () => {
171171
`,
172172
},
173173
{
174-
type: "nodejs-compat-module",
174+
type: "commonjs",
175175
name: "base64.cjs",
176176
filePath: "/virtual/node/base64.cjs",
177177
content: `module.exports = {
@@ -223,7 +223,7 @@ describe("LocalRuntimeController", () => {
223223
});
224224
} else if (pathname === "/throw-commonjs") {
225225
try { add.throw(); } catch (e) { return new Response(e.stack); }
226-
} else if (pathname === "/throw-nodejs-compat-module") {
226+
} else if (pathname === "/throw-other-commonjs") {
227227
try { base64.throw(); } catch (e) { return new Response(e.stack); }
228228
} else {
229229
return new Response(null, { status: 404 });
@@ -270,12 +270,12 @@ describe("LocalRuntimeController", () => {
270270
at Object.fetch (file:///D:/virtual/esm/index.mjs:15:19)"
271271
`);
272272

273-
// Check stack traces from NodeJsCompatModule modules include file path
274-
res = await fetch(new URL("/throw-nodejs-compat-module", url));
273+
// Check stack traces from CommonJS modules include file path
274+
res = await fetch(new URL("/throw-other-commonjs", url));
275275
expect(res.status).toBe(200);
276276
expect(normalizeDrive(await res.text())).toMatchInlineSnapshot(`
277277
"Error: Oops!
278-
at Object.throw (file:///D:/virtual/esm/base64.cjs:9:14)
278+
at Object.throw (file:///D:/virtual/node/base64.cjs:9:14)
279279
at Object.fetch (file:///D:/virtual/esm/index.mjs:17:22)"
280280
`);
281281
} else {
@@ -285,12 +285,12 @@ describe("LocalRuntimeController", () => {
285285
at Object.fetch (file:///virtual/esm/index.mjs:15:19)"
286286
`);
287287

288-
// Check stack traces from NodeJsCompatModule modules include file path
289-
res = await fetch(new URL("/throw-nodejs-compat-module", url));
288+
// Check stack traces from CommonJS modules include file path
289+
res = await fetch(new URL("/throw-other-commonjs", url));
290290
expect(res.status).toBe(200);
291291
expect(await res.text()).toMatchInlineSnapshot(`
292292
"Error: Oops!
293-
at Object.throw (file:///virtual/esm/base64.cjs:9:14)
293+
at Object.throw (file:///virtual/node/base64.cjs:9:14)
294294
at Object.fetch (file:///virtual/esm/index.mjs:17:22)"
295295
`);
296296
}

packages/wrangler/src/config/environment.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,7 @@ export type ConfigModuleRuleType =
870870
| "Text"
871871
| "Data"
872872
| "PythonModule"
873-
| "PythonRequirement"
874-
| "NodeJsCompatModule";
873+
| "PythonRequirement";
875874

876875
export type TailConsumer = {
877876
/** The name of the service tail events will be forwarded to. */

packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const moduleTypeMimeType: {
2727
text: "text/plain",
2828
python: "text/x-python",
2929
"python-requirement": "text/x-python-requirement",
30-
"nodejs-compat-module": undefined,
3130
};
3231

3332
function toMimeType(type: CfModuleType): string {

packages/wrangler/src/deployment-bundle/module-collection.ts

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export const RuleTypeToModuleType: Record<ConfigModuleRuleType, CfModuleType> =
3636
Text: "text",
3737
PythonModule: "python",
3838
PythonRequirement: "python-requirement",
39-
NodeJsCompatModule: "nodejs-compat-module",
4039
};
4140

4241
export const ModuleTypeToRuleType = flipObject(RuleTypeToModuleType);

packages/wrangler/src/deployment-bundle/worker.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export type CfModuleType =
2222
| "text"
2323
| "buffer"
2424
| "python"
25-
| "python-requirement"
26-
| "nodejs-compat-module";
25+
| "python-requirement";
2726

2827
/**
2928
* An imported module.

0 commit comments

Comments
 (0)