Skip to content

Commit

Permalink
Ignore node: and cloudflare: modules when collecting modules (#569)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
mrbbot committed Nov 1, 2023
1 parent 185b5aa commit 54f801b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/miniflare/src/plugins/core/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ ${dim(modulesConfig)}`;
throw new MiniflareCoreError("ERR_MODULE_DYNAMIC_SPEC", message);
}
const spec = specExpression.value;

// `node:` and `cloudflare:` imports don't need to be included explicitly
if (spec.startsWith("node:") || spec.startsWith("cloudflare:")) {
return;
}

const identifier = path.resolve(path.dirname(referencingPath), spec);
// The runtime requires module identifiers to be relative paths
const name = path.relative("", identifier);
Expand Down
22 changes: 21 additions & 1 deletion packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ test("Miniflare: custom service binding to another Miniflare instance", async (t
});
});

test("Miniflare: uses custom upstream as origin", async (t) => {
test("Miniflare: custom upstream as origin", async (t) => {
const upstream = await useServer(t, (req, res) => {
res.end(`upstream: ${new URL(req.url ?? "", "http://upstream")}`);
});
Expand All @@ -250,3 +250,23 @@ test("Miniflare: uses custom upstream as origin", async (t) => {
const res = await mf.dispatchFetch("https://random:0/path?a=1");
t.is(await res.text(), "upstream: http://upstream/extra/path?a=1");
});

test("Miniflare: `node:` and `cloudflare:` modules", async (t) => {
const mf = new Miniflare({
modules: true,
compatibilityFlags: ["nodejs_compat"],
script: `
import assert from "node:assert";
import { Buffer } from "node:buffer";
import { connect } from "cloudflare:sockets";
export default {
fetch() {
assert.strictEqual(typeof connect, "function");
return new Response(Buffer.from("test").toString("base64"))
}
}
`,
});
const res = await mf.dispatchFetch("http://localhost");
t.is(await res.text(), "dGVzdA==");
});

0 comments on commit 54f801b

Please sign in to comment.