Skip to content

Commit

Permalink
Return copy of entry URL from Miniflare#ready (#671)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrbbot committed Nov 1, 2023
1 parent 0f8e7b9 commit 2f10057
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,8 @@ export class Miniflare {
// called by `#init()`, and `#initPromise` doesn't resolve until `#init()`
// returns.
assert(this.#runtimeEntryURL !== undefined);
return this.#runtimeEntryURL;
// Return a copy so external mutations don't propagate to `#runtimeEntryURL`
return new URL(this.#runtimeEntryURL.toString());
}
get ready(): Promise<URL> {
return this.#waitForReady();
Expand Down
15 changes: 15 additions & 0 deletions packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ test("Miniflare: validates options", async (t) => {
);
});

test("Miniflare: ready returns copy of entry URL", async (t) => {
const mf = new Miniflare({
port: 0,
modules: true,
script: "",
});
t.teardown(() => mf.dispose());

const url1 = await mf.ready;
url1.protocol = "ws:";
const url2 = await mf.ready;
t.not(url1, url2);
t.is(url2.protocol, "http:");
});

test("Miniflare: keeps port between updates", async (t) => {
const opts: MiniflareOptions = {
port: 0,
Expand Down

0 comments on commit 2f10057

Please sign in to comment.