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

Commit 79440d7

Browse files
authored
Use localhost instead of 127.0.0.1 for loopback hostname (#615)
Miniflare starts its loopback server on the same host as the `workerd` server, so it's accessible on all the same addresses as the runtime. This allows the loopback server to be the target for the live reload web socket. This means if the `host: "localhost"` option is set, we start a Node `http` server with `localhost` as the hostname. Node will perform a DNS lookup to work out which IP address and interface to listen on, but will only listen on the first entry. In Node 17+, this will be the IPv6 interface. Unfortunately, we previously hardcoded the loopback address as the IPv4 loopback, meaning `workerd` was unable to connect. This change switches to using `localhost`, which `workerd` will resolve to either IPv4 or v6. Closes cloudflare/workers-sdk#3515
1 parent 715a86f commit 79440d7

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

packages/miniflare/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -733,16 +733,16 @@ export class Miniflare {
733733
};
734734

735735
#startLoopbackServer(
736-
port: string | number,
737-
hostname?: string
736+
port: number,
737+
hostname: string
738738
): Promise<StoppableServer> {
739739
return new Promise((resolve) => {
740740
const server = stoppable(
741741
http.createServer(this.#handleLoopback),
742742
/* grace */ 0
743743
);
744744
server.on("upgrade", this.#handleLoopbackUpgrade);
745-
server.listen(port as any, hostname, () => resolve(server));
745+
server.listen(port, hostname, () => resolve(server));
746746
});
747747
}
748748

packages/miniflare/src/runtime/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class Runtime {
8787
// (e.g. "streams_enable_constructors"), see https://github.com/cloudflare/workerd/pull/21
8888
"--experimental",
8989
`--socket-addr=${SOCKET_ENTRY}=${this.opts.entryHost}:${this.opts.entryPort}`,
90-
`--external-addr=${SERVICE_LOOPBACK}=127.0.0.1:${this.opts.loopbackPort}`,
90+
`--external-addr=${SERVICE_LOOPBACK}=localhost:${this.opts.loopbackPort}`,
9191
// Configure extra pipe for receiving control messages (e.g. when ready)
9292
"--control-fd=3",
9393
// Read config from stdin

0 commit comments

Comments
 (0)