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

Commit c40fe41

Browse files
authored
Actually respect port and host option (#424)
* Actually respect `port` and `host` option Previously, `port` was being passed through `get-port` before being used. This meant that if `Miniflare` was disposed and then immediately created again (as opposed to using `setOptions`), it couldn't use the same `port`, as `get-port` deliberately doesn't allocate recently allocated ports. Notably, Wrangler does exactly this: re-create `Miniflare` instances. Ideally, we should be using `setOptions` there, but this at least means the port stays the same between reloads. This PR also passes the `host` option to `workerd`, now that we're not putting an HTTP proxy in front of `workerd` in Miniflare. * Remove `!` from ready message URL Ctrl/CMD-cliking on this URL in VSCode included the `!` in the URL, opening the wrong URL in browser.
1 parent 4230563 commit c40fe41

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

packages/tre/src/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,11 @@ export class Miniflare {
218218
const loopbackPort = address.port;
219219

220220
// Start runtime
221-
const entryPort = await getPort({ port: this.#sharedOpts.core.port });
222-
223-
// TODO: respect entry `host` option
224-
this.#runtime = new this.#runtimeConstructor(entryPort, loopbackPort);
221+
const host = this.#sharedOpts.core.host ?? "127.0.0.1";
222+
const port = this.#sharedOpts.core.port ?? (await getPort({ port: 8787 }));
223+
this.#runtime = new this.#runtimeConstructor(host, port, loopbackPort);
225224
this.#removeRuntimeExitHook = exitHook(() => void this.#runtime?.dispose());
226-
227-
this.#runtimeEntryURL = new URL(`http://127.0.0.1:${entryPort}`);
225+
this.#runtimeEntryURL = new URL(`http://127.0.0.1:${port}`);
228226

229227
const config = await this.#assembleConfig();
230228
assert(config !== undefined);
@@ -233,7 +231,7 @@ export class Miniflare {
233231

234232
// Wait for runtime to start
235233
if (await this.#waitForRuntime()) {
236-
console.log(bold(green(`Ready on ${this.#runtimeEntryURL}! 🎉`)));
234+
console.log(bold(green(`Ready on ${this.#runtimeEntryURL} 🎉`)));
237235
}
238236
}
239237

@@ -493,7 +491,7 @@ export class Miniflare {
493491

494492
if (await this.#waitForRuntime()) {
495493
console.log(
496-
bold(green(`Updated and ready on ${this.#runtimeEntryURL}! 🎉`))
494+
bold(green(`Updated and ready on ${this.#runtimeEntryURL} 🎉`))
497495
);
498496
}
499497
updatePromise.resolve();

packages/tre/src/runtime/index.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import crypto from "crypto";
33
import fs from "fs";
44
import os from "os";
55
import path from "path";
6+
import { red } from "kleur/colors";
67
import workerdPath, {
78
compatibilityDate as supportedCompatibilityDate,
89
} from "workerd";
@@ -11,6 +12,7 @@ import { Awaitable, MiniflareCoreError } from "../shared";
1112

1213
export abstract class Runtime {
1314
constructor(
15+
protected readonly entryHost: string,
1416
protected readonly entryPort: number,
1517
protected readonly loopbackPort: number
1618
) {}
@@ -21,12 +23,11 @@ export abstract class Runtime {
2123
}
2224

2325
export interface RuntimeConstructor {
24-
new (entryPort: number, loopbackPort: number): Runtime;
26+
new (entryHost: string, entryPort: number, loopbackPort: number): Runtime;
2527

2628
isSupported(): boolean;
2729
supportSuggestion: string;
2830
description: string;
29-
distribution: string;
3031
}
3132

3233
const COMMON_RUNTIME_ARGS = [
@@ -64,7 +65,7 @@ function pipeOutput(runtime: childProcess.ChildProcessWithoutNullStreams) {
6465
console.log(trimTrailingNewline(data));
6566
});
6667
runtime.stderr.on("data", (data) => {
67-
console.error(trimTrailingNewline(data));
68+
console.error(red(trimTrailingNewline(data)));
6869
});
6970
// runtime.stdout.pipe(process.stdout);
7071
// runtime.stderr.pipe(process.stderr);
@@ -76,16 +77,15 @@ class NativeRuntime extends Runtime {
7677
}
7778
static supportSuggestion = "Run using a Linux or macOS based system";
7879
static description = "natively ⚡️";
79-
static distribution = `${process.platform}-${process.arch}`;
8080

8181
readonly #command: string;
8282
readonly #args: string[];
8383

8484
#process?: childProcess.ChildProcess;
8585
#processExitPromise?: Promise<void>;
8686

87-
constructor(entryPort: number, loopbackPort: number) {
88-
super(entryPort, loopbackPort);
87+
constructor(entryHost: string, entryPort: number, loopbackPort: number) {
88+
super(entryHost, entryPort, loopbackPort);
8989
const [command, ...args] = this.getCommand();
9090
this.#command = command;
9191
this.#args = args;
@@ -95,7 +95,7 @@ class NativeRuntime extends Runtime {
9595
return [
9696
workerdPath,
9797
...COMMON_RUNTIME_ARGS,
98-
`--socket-addr=${SOCKET_ENTRY}=127.0.0.1:${this.entryPort}`,
98+
`--socket-addr=${SOCKET_ENTRY}=${this.entryHost}:${this.entryPort}`,
9999
`--external-addr=${SERVICE_LOOPBACK}=127.0.0.1:${this.loopbackPort}`,
100100
// TODO: consider adding support for unix sockets?
101101
// `--socket-fd=${SOCKET_ENTRY}=${this.entryPort}`,
@@ -141,7 +141,6 @@ class WSLRuntime extends NativeRuntime {
141141
"Install the Windows Subsystem for Linux (https://aka.ms/wsl), " +
142142
"then run as you are at the moment";
143143
static description = "using WSL ✨";
144-
static distribution = `linux-${process.arch}`;
145144

146145
getCommand(): string[] {
147146
const command = super.getCommand();
@@ -160,7 +159,6 @@ class DockerRuntime extends Runtime {
160159
"Install Docker Desktop (https://www.docker.com/products/docker-desktop/), " +
161160
"then run as you are at the moment";
162161
static description = "using Docker 🐳";
163-
static distribution = `linux-${process.arch}`;
164162

165163
#configPath = path.join(
166164
os.tmpdir(),
@@ -193,7 +191,7 @@ class DockerRuntime extends Runtime {
193191
`--volume=${RESTART_PATH}:/restart.sh`,
194192
`--volume=${workerdPath}:/runtime`,
195193
`--volume=${this.#configPath}:/miniflare-config.bin`,
196-
`--publish=127.0.0.1:${this.entryPort}:8787`,
194+
`--publish=${this.entryHost}:${this.entryPort}:8787`,
197195
"debian:bullseye-slim",
198196
"/restart.sh",
199197
"/runtime",

0 commit comments

Comments
 (0)