Skip to content

Commit

Permalink
Use localhost instead of 127.0.0.1 for loopback hostname (#615)
Browse files Browse the repository at this point in the history
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 #3515
  • Loading branch information
mrbbot committed Nov 1, 2023
1 parent 6511918 commit 218e34f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,16 @@ export class Miniflare {
};

#startLoopbackServer(
port: string | number,
hostname?: string
port: number,
hostname: string
): Promise<StoppableServer> {
return new Promise((resolve) => {
const server = stoppable(
http.createServer(this.#handleLoopback),
/* grace */ 0
);
server.on("upgrade", this.#handleLoopbackUpgrade);
server.listen(port as any, hostname, () => resolve(server));
server.listen(port, hostname, () => resolve(server));
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/miniflare/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Runtime {
// (e.g. "streams_enable_constructors"), see https://github.com/cloudflare/workerd/pull/21
"--experimental",
`--socket-addr=${SOCKET_ENTRY}=${this.opts.entryHost}:${this.opts.entryPort}`,
`--external-addr=${SERVICE_LOOPBACK}=127.0.0.1:${this.opts.loopbackPort}`,
`--external-addr=${SERVICE_LOOPBACK}=localhost:${this.opts.loopbackPort}`,
// Configure extra pipe for receiving control messages (e.g. when ready)
"--control-fd=3",
// Read config from stdin
Expand Down

0 comments on commit 218e34f

Please sign in to comment.