Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strip leading */*. from routes when deducing a host for dev #1018

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/forty-mice-travel.md
Original file line number Diff line number Diff line change
@@ -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
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