Skip to content

Commit

Permalink
fix: ensure IPv6 addresses can be used as hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Mar 1, 2024
1 parent 8872a8f commit 5c813e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/tender-nails-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"miniflare": patch
---

fix: ensure IPv6 addresses can be used as `host`s

Previously, if Miniflare was configured to start on an IPv6 `host`, it could crash. This change ensures IPv6 addresses are handled correctly. This also fixes `wrangler dev` when using IPv6 addresses such as `::1` with the `--ip` flag.
2 changes: 1 addition & 1 deletion packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ export class Miniflare {
requestedPort = this.#socketPorts?.get(id);
}
// Otherwise, default to a new random port
return `${host}:${requestedPort ?? 0}`;
return `${getURLSafeHost(host)}:${requestedPort ?? 0}`;
}

async #assembleConfig(loopbackPort: number): Promise<Config> {
Expand Down
20 changes: 20 additions & 0 deletions packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,26 @@ const localInterface = (interfaces["en0"] ?? interfaces["eth0"])?.find(
t.is(await res.text(), "body");
}
);
test("Miniflare: can use IPv6 loopback as host", async (t) => {
const mf = new Miniflare({
host: "::1",
modules: true,
script: `export default { fetch(request, env) { return env.SERVICE.fetch(request); } }`,
serviceBindings: {
SERVICE() {
return new Response("body");
},
},
});
t.teardown(() => mf.dispose());

let res = await mf.dispatchFetch("https://example.com");
t.is(await res.text(), "body");

const worker = await mf.getWorker();
res = await worker.fetch("https://example.com");
t.is(await res.text(), "body");
});

test("Miniflare: routes to multiple workers with fallback", async (t) => {
const opts: MiniflareOptions = {
Expand Down

0 comments on commit 5c813e6

Please sign in to comment.