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

Commit 1d535b7

Browse files
authored
Return copy of entry URL from Miniflare#ready (#671)
There are cases where mutating the URL returned from `Miniflare#ready` may be desirable, e.g. changing the protocol to `ws:` and using the `ws` module to establish a WebSocket connection. Previously, doing this would break Miniflare's `dispatchFetch`, as the returned URL was Miniflare's own internal reference. This change makes sure we return a copy.
1 parent 1778afd commit 1d535b7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/miniflare/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,8 @@ export class Miniflare {
10711071
// called by `#init()`, and `#initPromise` doesn't resolve until `#init()`
10721072
// returns.
10731073
assert(this.#runtimeEntryURL !== undefined);
1074-
return this.#runtimeEntryURL;
1074+
// Return a copy so external mutations don't propagate to `#runtimeEntryURL`
1075+
return new URL(this.#runtimeEntryURL.toString());
10751076
}
10761077
get ready(): Promise<URL> {
10771078
return this.#waitForReady();

packages/miniflare/test/index.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ test("Miniflare: validates options", async (t) => {
7777
);
7878
});
7979

80+
test("Miniflare: ready returns copy of entry URL", async (t) => {
81+
const mf = new Miniflare({
82+
port: 0,
83+
modules: true,
84+
script: "",
85+
});
86+
t.teardown(() => mf.dispose());
87+
88+
const url1 = await mf.ready;
89+
url1.protocol = "ws:";
90+
const url2 = await mf.ready;
91+
t.not(url1, url2);
92+
t.is(url2.protocol, "http:");
93+
});
94+
8095
test("Miniflare: keeps port between updates", async (t) => {
8196
const opts: MiniflareOptions = {
8297
port: 0,

0 commit comments

Comments
 (0)