Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit fc1c280

Browse files
authoredMay 15, 2023
Ignore node: and cloudflare: modules when collecting modules (#569)
Previously, Miniflare would complain when specifying a `script`/ `scriptPath` that used `node:`/`cloudflare:` imports with `modules: true` set. This change updates Miniflare's module collector to ignore them, as they're automatically included by `workerd`.

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
 

‎packages/tre/src/plugins/core/modules.ts

+6
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ ${dim(modulesConfig)}`;
243243
throw new MiniflareCoreError("ERR_MODULE_DYNAMIC_SPEC", message);
244244
}
245245
const spec = specExpression.value;
246+
247+
// `node:` and `cloudflare:` imports don't need to be included explicitly
248+
if (spec.startsWith("node:") || spec.startsWith("cloudflare:")) {
249+
return;
250+
}
251+
246252
const identifier = path.resolve(path.dirname(referencingPath), spec);
247253
// The runtime requires module identifiers to be relative paths
248254
const name = path.relative("", identifier);

‎packages/tre/test/index.spec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ test("Miniflare: custom service binding to another Miniflare instance", async (t
233233
});
234234
});
235235

236-
test("Miniflare: uses custom upstream as origin", async (t) => {
236+
test("Miniflare: custom upstream as origin", async (t) => {
237237
const upstream = await useServer(t, (req, res) => {
238238
res.end(`upstream: ${new URL(req.url ?? "", "http://upstream")}`);
239239
});
@@ -250,3 +250,23 @@ test("Miniflare: uses custom upstream as origin", async (t) => {
250250
const res = await mf.dispatchFetch("https://random:0/path?a=1");
251251
t.is(await res.text(), "upstream: http://upstream/extra/path?a=1");
252252
});
253+
254+
test("Miniflare: `node:` and `cloudflare:` modules", async (t) => {
255+
const mf = new Miniflare({
256+
modules: true,
257+
compatibilityFlags: ["nodejs_compat"],
258+
script: `
259+
import assert from "node:assert";
260+
import { Buffer } from "node:buffer";
261+
import { connect } from "cloudflare:sockets";
262+
export default {
263+
fetch() {
264+
assert.strictEqual(typeof connect, "function");
265+
return new Response(Buffer.from("test").toString("base64"))
266+
}
267+
}
268+
`,
269+
});
270+
const res = await mf.dispatchFetch("http://localhost");
271+
t.is(await res.text(), "dGVzdA==");
272+
});

0 commit comments

Comments
 (0)
This repository has been archived.