Skip to content

Commit

Permalink
fix: listen on IPv4 loopback only by default on Windows (#4547)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot authored Dec 5, 2023
1 parent 71fb0b8 commit 86c81ff
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .changeset/quick-pens-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": minor
---

fix: listen on IPv4 loopback only by default on Windows

Due to a [known issue](https://github.com/cloudflare/workerd/issues/1408), `workerd` will only listen on the IPv4 loopback address `127.0.0.1` when it's asked to listen on `localhost`. On Node.js > 17, `localhost` will resolve to the IPv6 loopback address, meaning requests to `workerd` would fail. This change switches to using the IPv4 loopback address throughout Wrangler on Windows, while [workerd#1408](https://github.com/cloudflare/workerd/issues/1408) gets fixed.
2 changes: 1 addition & 1 deletion packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("normalizeAndValidateConfig()", () => {
constellation: [],
hyperdrive: [],
dev: {
ip: "localhost",
ip: process.platform === "win32" ? "127.0.0.1" : "localhost",
local_protocol: "http",
port: undefined, // the default of 8787 is set at runtime
upstream_protocol: "https",
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ describe("wrangler dev", () => {
fs.writeFileSync("index.js", `export default {};`);
await runWrangler("dev");
expect((Dev as jest.Mock).mock.calls[0][0].initialIp).toEqual(
"localhost"
process.platform === "win32" ? "127.0.0.1" : "localhost"
);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
Expand Down Expand Up @@ -1060,7 +1060,7 @@ describe("wrangler dev", () => {
fs.writeFileSync("index.js", `export default {};`);
await runWrangler("dev");
expect((Dev as jest.Mock).mock.calls[0][0].initialIp).toEqual(
"localhost"
process.platform === "win32" ? "127.0.0.1" : "localhost"
);
expect(std.out).toMatchInlineSnapshot(`
"Your worker has access to the following bindings:
Expand Down
9 changes: 8 additions & 1 deletion packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,14 @@ function normalizeAndValidateDev(
rawDev: RawDevConfig
): DevConfig {
const {
ip = "localhost",
// On Windows, when specifying `localhost` as the socket hostname, `workerd`
// will only listen on the IPv4 loopback `127.0.0.1`, not the IPv6 `::1`:
// https://github.com/cloudflare/workerd/issues/1408
// On Node 17+, `fetch()` will only try to fetch the IPv6 address.
// For now, on Windows, we default to listening on IPv4 only and using
// `127.0.0.1` when sending control requests to `workerd` (e.g. with the
// `ProxyController`).
ip = process.platform === "win32" ? "127.0.0.1" : "localhost",
port,
inspector_port,
local_protocol = "http",
Expand Down
9 changes: 8 additions & 1 deletion packages/wrangler/src/pages/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ export function Options(yargs: CommonYargsArgv) {
},
ip: {
type: "string",
default: "localhost",
// On Windows, when specifying `localhost` as the socket hostname,
// `workerd` will only listen on the IPv4 loopback `127.0.0.1`, not the
// IPv6 `::1`: https://github.com/cloudflare/workerd/issues/1408
// On Node 17+, `fetch()` will only try to fetch the IPv6 address.
// For now, on Windows, we default to listening on IPv4 only and using
// `127.0.0.1` when sending control requests to `workerd` (e.g. with the
// `ProxyController`).
default: process.platform === "win32" ? "127.0.0.1" : "localhost",
description: "The IP address to listen on",
},
port: {
Expand Down

0 comments on commit 86c81ff

Please sign in to comment.