Skip to content

Commit 86c81ff

Browse files
authored
fix: listen on IPv4 loopback only by default on Windows (#4547)
1 parent 71fb0b8 commit 86c81ff

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

.changeset/quick-pens-develop.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
fix: listen on IPv4 loopback only by default on Windows
6+
7+
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.

packages/wrangler/src/__tests__/configuration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe("normalizeAndValidateConfig()", () => {
3030
constellation: [],
3131
hyperdrive: [],
3232
dev: {
33-
ip: "localhost",
33+
ip: process.platform === "win32" ? "127.0.0.1" : "localhost",
3434
local_protocol: "http",
3535
port: undefined, // the default of 8787 is set at runtime
3636
upstream_protocol: "https",

packages/wrangler/src/__tests__/dev.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ describe("wrangler dev", () => {
840840
fs.writeFileSync("index.js", `export default {};`);
841841
await runWrangler("dev");
842842
expect((Dev as jest.Mock).mock.calls[0][0].initialIp).toEqual(
843-
"localhost"
843+
process.platform === "win32" ? "127.0.0.1" : "localhost"
844844
);
845845
expect(std.out).toMatchInlineSnapshot(`""`);
846846
expect(std.warn).toMatchInlineSnapshot(`""`);
@@ -1060,7 +1060,7 @@ describe("wrangler dev", () => {
10601060
fs.writeFileSync("index.js", `export default {};`);
10611061
await runWrangler("dev");
10621062
expect((Dev as jest.Mock).mock.calls[0][0].initialIp).toEqual(
1063-
"localhost"
1063+
process.platform === "win32" ? "127.0.0.1" : "localhost"
10641064
);
10651065
expect(std.out).toMatchInlineSnapshot(`
10661066
"Your worker has access to the following bindings:

packages/wrangler/src/config/validation.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,14 @@ function normalizeAndValidateDev(
390390
rawDev: RawDevConfig
391391
): DevConfig {
392392
const {
393-
ip = "localhost",
393+
// On Windows, when specifying `localhost` as the socket hostname, `workerd`
394+
// will only listen on the IPv4 loopback `127.0.0.1`, not the IPv6 `::1`:
395+
// https://github.com/cloudflare/workerd/issues/1408
396+
// On Node 17+, `fetch()` will only try to fetch the IPv6 address.
397+
// For now, on Windows, we default to listening on IPv4 only and using
398+
// `127.0.0.1` when sending control requests to `workerd` (e.g. with the
399+
// `ProxyController`).
400+
ip = process.platform === "win32" ? "127.0.0.1" : "localhost",
394401
port,
395402
inspector_port,
396403
local_protocol = "http",

packages/wrangler/src/pages/dev.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ export function Options(yargs: CommonYargsArgv) {
9999
},
100100
ip: {
101101
type: "string",
102-
default: "localhost",
102+
// On Windows, when specifying `localhost` as the socket hostname,
103+
// `workerd` will only listen on the IPv4 loopback `127.0.0.1`, not the
104+
// IPv6 `::1`: https://github.com/cloudflare/workerd/issues/1408
105+
// On Node 17+, `fetch()` will only try to fetch the IPv6 address.
106+
// For now, on Windows, we default to listening on IPv4 only and using
107+
// `127.0.0.1` when sending control requests to `workerd` (e.g. with the
108+
// `ProxyController`).
109+
default: process.platform === "win32" ? "127.0.0.1" : "localhost",
103110
description: "The IP address to listen on",
104111
},
105112
port: {

0 commit comments

Comments
 (0)