-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generate valid URL route paths for pages on Windows
Previously route paths were manipulated by file-system path utilities. On Windows this resulted in URLs that had backslashes, which are invalid for such URLs. Fixes #51 Closes #235 Closes #330 Closes #327
- Loading branch information
1 parent
3575892
commit e151223
Showing
6 changed files
with
95 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: generate valid URL route paths for pages on Windows | ||
|
||
Previously route paths were manipulated by file-system path utilities. | ||
On Windows this resulted in URLs that had backslashes, which are invalid for such URLs. | ||
|
||
Fixes #51 | ||
Closes #235 | ||
Closes #330 | ||
Closes #327 |
34 changes: 21 additions & 13 deletions
34
packages/wrangler/pages/functions/filepath-routing.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,47 @@ | ||
import { toUrlPath } from "../../src/paths"; | ||
import { compareRoutes } from "./filepath-routing"; | ||
|
||
describe("compareRoutes()", () => { | ||
const url = toUrlPath; | ||
test("routes / last", () => { | ||
expect(compareRoutes("/", "/foo")).toBeGreaterThanOrEqual(1); | ||
expect(compareRoutes("/", "/:foo")).toBeGreaterThanOrEqual(1); | ||
expect(compareRoutes("/", "/:foo*")).toBeGreaterThanOrEqual(1); | ||
expect(compareRoutes([url("/")], [url("/foo")])).toBeGreaterThanOrEqual(1); | ||
expect(compareRoutes([url("/")], [url("/:foo")])).toBeGreaterThanOrEqual(1); | ||
expect(compareRoutes([url("/")], [url("/:foo*")])).toBeGreaterThanOrEqual( | ||
1 | ||
); | ||
}); | ||
|
||
test("routes with fewer segments come after those with more segments", () => { | ||
expect(compareRoutes("/foo", "/foo/bar")).toBeGreaterThanOrEqual(1); | ||
expect(compareRoutes("/foo", "/foo/bar/cat")).toBeGreaterThanOrEqual(1); | ||
expect( | ||
compareRoutes([url("/foo")], [url("/foo/bar")]) | ||
).toBeGreaterThanOrEqual(1); | ||
expect( | ||
compareRoutes([url("/foo")], [url("/foo/bar/cat")]) | ||
).toBeGreaterThanOrEqual(1); | ||
}); | ||
|
||
test("routes with wildcard segments come after those without", () => { | ||
expect(compareRoutes("/:foo*", "/foo")).toBe(1); | ||
expect(compareRoutes("/:foo*", "/:foo")).toBe(1); | ||
expect(compareRoutes([url("/:foo*")], [url("/foo")])).toBe(1); | ||
expect(compareRoutes([url("/:foo*")], [url("/:foo")])).toBe(1); | ||
}); | ||
|
||
test("routes with dynamic segments come after those without", () => { | ||
expect(compareRoutes("/:foo", "/foo")).toBe(1); | ||
expect(compareRoutes([url("/:foo")], [url("/foo")])).toBe(1); | ||
}); | ||
|
||
test("routes with dynamic segments occuring earlier come after those with dynamic segments in later positions", () => { | ||
expect(compareRoutes("/foo/:id/bar", "/foo/bar/:id")).toBe(1); | ||
test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => { | ||
expect(compareRoutes([url("/foo/:id/bar")], [url("/foo/bar/:id")])).toBe(1); | ||
}); | ||
|
||
test("routes with no HTTP method come after those specifying a method", () => { | ||
expect(compareRoutes("/foo", "GET /foo")).toBe(1); | ||
expect(compareRoutes([url("/foo")], [url("/foo"), "GET"])).toBe(1); | ||
}); | ||
|
||
test("two equal routes are sorted according to their original position in the list", () => { | ||
expect(compareRoutes("GET /foo", "GET /foo")).toBe(0); | ||
expect(compareRoutes([url("/foo"), "GET"], [url("/foo"), "GET"])).toBe(0); | ||
}); | ||
|
||
test("it returns -1 if the first argument should appear first in the list", () => { | ||
expect(compareRoutes("GET /foo", "/foo")).toBe(-1); | ||
expect(compareRoutes([url("/foo"), "GET"], [url("/foo")])).toBe(-1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { assert } from "console"; | ||
|
||
type DiscriminatedPath<Discriminator extends string> = string & { | ||
_discriminator: Discriminator; | ||
}; | ||
|
||
/** | ||
* A branded string that expects to be URL compatible. | ||
* | ||
* Require this type when you want callers to ensure that they have converted file-path strings into URL-safe paths. | ||
*/ | ||
export type UrlPath = DiscriminatedPath<"UrlPath">; | ||
|
||
/** | ||
* Convert a file-path string to a URL-path string. | ||
* | ||
* Use this helper to convert a `string` to a `UrlPath` when it is not clear whether the string needs normalizing. | ||
* Replaces all back-slashes with forward-slashes, and throws an error if the path contains a drive letter (e.g. `C:`). | ||
*/ | ||
export function toUrlPath(path: string): UrlPath { | ||
assert( | ||
!/^[a-z]:/i.test(path), | ||
"Tried to convert a Windows file path with a drive to a URL path." | ||
); | ||
return path.replace(/\\/g, "/") as UrlPath; | ||
} |