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.

Fixes #1002
  • Loading branch information
threepointone committed May 16, 2022
1 parent a6e2057 commit 91548aa
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 0 deletions.
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
3 changes: 3 additions & 0 deletions .github/workflows/create-pullrequest-prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
if: steps.node-modules-cache.outputs.cache-hit != 'true' || runner.os == 'Windows'
run: npm ci

- name: Restore workspaces
run: npm install

- name: Modify package.json version
run: node .github/version-script.js

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/experimental-wasm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
- name: Install NPM Dependencies
run: npm ci

- name: Restore workspaces
run: npm install

- name: Modify package.json version
run: node .github/version-script.js

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/prereleases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ jobs:
if: steps.node-modules-cache.outputs.cache-hit != 'true' || runner.os == 'Windows'
run: npm ci

- name: Restore workspaces
run: npm install

- name: Build wrangler
run: npm run build
working-directory: packages/wrangler
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pullrequests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
if: steps.node-modules-cache.outputs.cache-hit != 'true' || runner.os == 'Windows'
run: npm ci

- name: Restore workspaces
run: npm install

- name: Check for errors
run: npm run check

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
if: steps.node-modules-cache.outputs.cache-hit != 'true' || runner.os == 'Windows'
run: npm ci

- name: Restore workspaces
run: npm install

- name: Check for errors
run: npm run check

Expand Down
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 91548aa

Please sign in to comment.