From cd2c42fca02bff463d78398428dcf079a80e2ae6 Mon Sep 17 00:00:00 2001 From: Sunil Pai Date: Mon, 16 May 2022 20:10:38 +0100 Subject: [PATCH] fix: strip leading `*`/`*.` from routes when deducing a host for `dev` (#1018) When given routes, we use the host name from the route to deduce a zone id to pass along with the host to set with dev `session`. Route patterns can include leading `*`/`*.`, which we don't account for when deducing said zone id, resulting in subtle errors for the session. This fix strips those leading characters as appropriate. Fixes https://github.com/cloudflare/wrangler2/issues/1002 --- .changeset/forty-mice-travel.md | 9 +++++++ packages/wrangler/src/__tests__/dev.test.tsx | 26 ++++++++++++++++++++ packages/wrangler/src/index.tsx | 3 +++ 3 files changed, 38 insertions(+) create mode 100644 .changeset/forty-mice-travel.md diff --git a/.changeset/forty-mice-travel.md b/.changeset/forty-mice-travel.md new file mode 100644 index 000000000000..f13d1b957671 --- /dev/null +++ b/.changeset/forty-mice-travel.md @@ -0,0 +1,9 @@ +--- +"wrangler": patch +--- + +fix: strip leading `*`/`*.` from routes when deducing a host for `dev` + +When given routes, we use the host name from the route to deduce a zone id to pass along with the host to set with dev `session`. Route patterns can include leading `*`/`*.`, which we don't account for when deducing said zone id, resulting in subtle errors for the session. This fix strips those leading characters as appropriate. + +Fixes https://github.com/cloudflare/wrangler2/issues/1002 diff --git a/packages/wrangler/src/__tests__/dev.test.tsx b/packages/wrangler/src/__tests__/dev.test.tsx index c4ad66b091f2..3a5a9a4728f8 100644 --- a/packages/wrangler/src/__tests__/dev.test.tsx +++ b/packages/wrangler/src/__tests__/dev.test.tsx @@ -225,6 +225,32 @@ describe("wrangler dev", () => { ); }); + it("should strip leading `*` from given host when deducing a zone id", async () => { + writeWranglerToml({ + main: "index.js", + route: "*some-host.com/some/path/*", + }); + fs.writeFileSync("index.js", `export default {};`); + mockGetZones("some-host.com", [{ id: "some-zone-id" }]); + await runWrangler("dev"); + expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual( + "some-host.com" + ); + }); + + it("should strip leading `*.` from given host when deducing a zone id", async () => { + writeWranglerToml({ + main: "index.js", + route: "*.some-host.com/some/path/*", + }); + fs.writeFileSync("index.js", `export default {};`); + mockGetZones("some-host.com", [{ id: "some-zone-id" }]); + await runWrangler("dev"); + expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual( + "some-host.com" + ); + }); + it("should, when provided, use a configured zone_id", async () => { writeWranglerToml({ main: "index.js", diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 4030fcebb68b..545fb45bd66b 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -934,6 +934,9 @@ export async function main(argv: string[]): Promise { * try to extract a host from it */ function getHost(urlLike: string): string | undefined { + // strip leading * / *. + urlLike = urlLike.replace(/^\*(\.)?/g, ""); + if ( !(urlLike.startsWith("http://") || urlLike.startsWith("https://")) ) {