Skip to content

Commit

Permalink
Switch default to localhost instead of 127.0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jspspike committed Aug 9, 2023
1 parent cf080a8 commit fd6654c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,16 @@ export class Miniflare {
this.#workerOpts = workerOpts;
this.#log = this.#sharedOpts.core.log ?? new NoOpLog();
this.#timers = this.#sharedOpts.core.timers ?? defaultTimers;
this.#host = this.#sharedOpts.core.host ?? "127.0.0.1";
this.#host = this.#sharedOpts.core.host ?? "localhost";
this.#accessibleHost =
this.#host === "*" || this.#host === "0.0.0.0" ? "127.0.0.1" : this.#host;
this.#host === "*" || this.#host === "0.0.0.0" || this.#host === "::"
? "localhost"
: this.#host;

if (net.isIPv6(this.#accessibleHost)) {
this.#accessibleHost = `[${this.#accessibleHost}]`;
}

this.#initPlugins();

this.#liveReloadServer = new WebSocketServer({ noServer: true });
Expand Down Expand Up @@ -527,7 +534,7 @@ export class Miniflare {
// Start runtime
const port = this.#sharedOpts.core.port ?? 0;
const opts: RuntimeOptions = {
entryHost: this.#host,
entryHost: net.isIPv6(this.#host) ? `[${this.#host}]` : this.#host,
entryPort: port,
loopbackPort: this.#loopbackPort,
inspectorPort: this.#sharedOpts.core.inspectorPort,
Expand Down Expand Up @@ -624,7 +631,7 @@ export class Miniflare {
// Extract original URL passed to `fetch`
const url = new URL(
headers.get(CoreHeaders.ORIGINAL_URL) ?? req.url ?? "",
"http://127.0.0.1"
"http://localhost"
);
headers.delete(CoreHeaders.ORIGINAL_URL);

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 @@ -95,7 +95,7 @@ export class Runtime {
];
if (this.opts.inspectorPort !== undefined) {
// Required to enable the V8 inspector
args.push(`--inspector-addr=127.0.0.1:${this.opts.inspectorPort}`);
args.push(`--inspector-addr=localhost:${this.opts.inspectorPort}`);
}
if (this.opts.verbose) {
args.push("--verbose");
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 @@ -498,3 +498,23 @@ test("Miniflare: Accepts https requests", async (t) => {

t.assert(log.logs[0][1].startsWith("Ready on https://"));
});

test("Miniflare: listens on ::1 for localhost", async (t) => {
const log = new TestLog(t);

const mf = new Miniflare({
log,
modules: true,
script: `export default {
fetch() {
return new Response("Hello world");
}
}`,
});

let res = await mf.dispatchFetch("https://127.0.0.1/");
t.true(res.ok);

res = await mf.dispatchFetch("https://[::1]/");
t.true(res.ok);
});

0 comments on commit fd6654c

Please sign in to comment.