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

Commit 5db7ae6

Browse files
GregBrimblemrbbot
andauthored
Allow setOptions() to set runtime port (#610)
* Allow `setOptions()` to set runtime port * Automatically pick up existing port when updating * Only update port, remove 'initial' parameter from update function and use helper function to parse port * Update packages/miniflare/src/runtime/index.ts Co-authored-by: MrBBot <[email protected]> * Add test to ensure port is persisted between updates --------- Co-authored-by: MrBBot <[email protected]>
1 parent cfddac3 commit 5db7ae6

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

packages/miniflare/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
Timers,
7070
defaultTimers,
7171
formatResponse,
72+
maybeApply,
7273
} from "./shared";
7374
import { Storage } from "./storage";
7475
import { CoreHeaders } from "./workers";
@@ -468,7 +469,7 @@ export class Miniflare {
468469
this.#removeRuntimeExitHook = exitHook(() => void this.#runtime?.dispose());
469470

470471
// Update config and wait for runtime to start
471-
await this.#assembleAndUpdateConfig(/* initial */ true);
472+
await this.#assembleAndUpdateConfig();
472473
}
473474

474475
async #handleLoopbackCustomService(
@@ -781,12 +782,14 @@ export class Miniflare {
781782
return { services: Array.from(services.values()), sockets };
782783
}
783784

784-
async #assembleAndUpdateConfig(initial = false) {
785+
async #assembleAndUpdateConfig() {
786+
const initial = !this.#runtimeEntryURL;
785787
assert(this.#runtime !== undefined);
786788
const config = await this.#assembleConfig();
787789
const configBuffer = serializeConfig(config);
788790
const maybePort = await this.#runtime.updateConfig(configBuffer, {
789791
signal: this.#disposeController.signal,
792+
entryPort: maybeApply(parseInt, this.#runtimeEntryURL?.port),
790793
});
791794
if (this.#disposeController.signal.aborted) return;
792795
if (maybePort === undefined) {

packages/miniflare/src/runtime/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,15 @@ export interface RuntimeOptions {
7070

7171
export class Runtime {
7272
readonly #command: string;
73-
readonly #args: string[];
7473

7574
#process?: childProcess.ChildProcess;
7675
#processExitPromise?: Promise<void>;
7776

7877
constructor(private opts: RuntimeOptions) {
78+
this.#command = workerdPath;
79+
}
80+
81+
get #args() {
7982
const args: string[] = [
8083
"serve",
8184
// Required to use binary capnp config
@@ -98,18 +101,21 @@ export class Runtime {
98101
args.push("--verbose");
99102
}
100103

101-
this.#command = workerdPath;
102-
this.#args = args;
104+
return args;
103105
}
104106

105107
async updateConfig(
106108
configBuffer: Buffer,
107-
options?: Abortable
109+
options?: Abortable & Partial<Pick<RuntimeOptions, "entryPort">>
108110
): Promise<number | undefined> {
109111
// 1. Stop existing process (if any) and wait for exit
110112
await this.dispose();
111113
// TODO: what happens if runtime crashes?
112114

115+
if (options?.entryPort !== undefined) {
116+
this.opts.entryPort = options.entryPort;
117+
}
118+
113119
// 2. Start new process
114120
const runtimeProcess = childProcess.spawn(this.#command, this.#args, {
115121
stdio: ["pipe", "pipe", "pipe", "pipe"],

packages/miniflare/test/index.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ test("Miniflare: validates options", async (t) => {
5555
);
5656
});
5757

58+
test("Miniflare: keeps port between updates", async (t) => {
59+
const opts: MiniflareOptions = {
60+
port: 0,
61+
script: `addEventListener("fetch", (event) => {
62+
event.respondWith(new Response("a"));
63+
})`,
64+
};
65+
const mf = new Miniflare(opts);
66+
const initialURL = await mf.ready;
67+
68+
await mf.setOptions(opts);
69+
const updatedURL = await mf.ready;
70+
71+
t.not(initialURL.port, "0");
72+
t.is(initialURL.port, updatedURL.port);
73+
});
74+
5875
test("Miniflare: routes to multiple workers with fallback", async (t) => {
5976
const opts: MiniflareOptions = {
6077
workers: [

0 commit comments

Comments
 (0)