Skip to content

Commit

Permalink
fix: strip leading */*. from routes when deducing a host for dev
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
threepointone committed May 16, 2022
1 parent 02a2cae commit e7beb4f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/forty-mice-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"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.
26 changes: 26 additions & 0 deletions packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,9 @@ export async function main(argv: string[]): Promise<void> {
* 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://"))
) {
Expand Down

0 comments on commit e7beb4f

Please sign in to comment.