@tschk/moonshine-router 0.4.1 → 0.4.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +243 -9
- package/package.json +12 -4
- package/src/data-http.ts +160 -0
- package/src/data-react.ts +41 -0
- package/src/data.ts +138 -0
- package/src/index.ts +4 -2
- package/src/navigation.ts +46 -79
- package/src/pattern.ts +113 -100
- package/src/runtime.ts +124 -0
- package/test/dom.ts +0 -109
- package/test/graph.test.ts +0 -23
- package/test/link.test.tsx +0 -94
- package/test/navigation.test.ts +0 -76
- package/test/pattern.test.ts +0 -114
- package/tsconfig.json +0 -4
package/test/link.test.tsx
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import { createElement } from "react";
|
|
3
|
-
import { renderToStaticMarkup } from "react-dom/server";
|
|
4
|
-
import { isExternal, Link, safeHref } from "../src/navigation";
|
|
5
|
-
import { click, render, setLocation, cleanup } from "./dom";
|
|
6
|
-
|
|
7
|
-
function hrefOf(href: string): string {
|
|
8
|
-
const html = renderToStaticMarkup(createElement(Link, { href }, "go"));
|
|
9
|
-
return /href="([^"]*)"/.exec(html)?.[1] ?? "";
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
describe("safeHref", () => {
|
|
13
|
-
test("neutralizes script-bearing schemes", () => {
|
|
14
|
-
expect(safeHref("javascript:alert(1)")).toBe("#");
|
|
15
|
-
expect(safeHref("JavaScript:alert(1)")).toBe("#");
|
|
16
|
-
expect(safeHref(" javascript:alert(1)")).toBe("#");
|
|
17
|
-
expect(safeHref("java\tscript:alert(1)")).toBe("#");
|
|
18
|
-
expect(safeHref("data:text/html,<script>alert(1)</script>")).toBe("#");
|
|
19
|
-
expect(safeHref("vbscript:msgbox(1)")).toBe("#");
|
|
20
|
-
expect(safeHref("")).toBe("#");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("preserves relative and known-safe absolute targets", () => {
|
|
24
|
-
expect(safeHref("/users/1")).toBe("/users/1");
|
|
25
|
-
expect(safeHref("users/1")).toBe("users/1");
|
|
26
|
-
expect(safeHref("?q=1")).toBe("?q=1");
|
|
27
|
-
expect(safeHref("#anchor")).toBe("#anchor");
|
|
28
|
-
expect(safeHref("//example.com/x")).toBe("//example.com/x");
|
|
29
|
-
expect(safeHref("https://example.com/x")).toBe("https://example.com/x");
|
|
30
|
-
expect(safeHref("mailto:a@b.c")).toBe("mailto:a@b.c");
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
describe("Link", () => {
|
|
35
|
-
test("does not render a script-bearing href", () => {
|
|
36
|
-
expect(hrefOf("javascript:alert(1)")).toBe("#");
|
|
37
|
-
expect(hrefOf("/ok")).toBe("/ok");
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
describe("isExternal", () => {
|
|
42
|
-
test("a scheme, a protocol-relative host, a fragment or a target is the browser's", () => {
|
|
43
|
-
expect(isExternal("https://github.com/me")).toBe(true);
|
|
44
|
-
expect(isExternal("mailto:me@example.com")).toBe(true);
|
|
45
|
-
expect(isExternal("tel:+15551234")).toBe(true);
|
|
46
|
-
expect(isExternal("//cdn.example.com/x")).toBe(true);
|
|
47
|
-
expect(isExternal("#section")).toBe(true);
|
|
48
|
-
expect(isExternal("/about", "_blank")).toBe(true);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("a same-document path is the router's", () => {
|
|
52
|
-
expect(isExternal("/about")).toBe(false);
|
|
53
|
-
expect(isExternal("/about", "_self")).toBe(false);
|
|
54
|
-
expect(isExternal("/a?b=1")).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe("Link and external targets", () => {
|
|
59
|
-
// The regression: safeHref *allows* https/mailto/tel — it only strips schemes
|
|
60
|
-
// that could execute — and Link took that as permission to intercept them.
|
|
61
|
-
// preventDefault ran, navigate rebuilt the URL against the current origin, and
|
|
62
|
-
// `https://github.com/me` became a push to `/me` on this app. The href looked
|
|
63
|
-
// correct the whole time, so it only surfaced as a 404 on click.
|
|
64
|
-
afterEach(cleanup);
|
|
65
|
-
beforeEach(() => setLocation("/start"));
|
|
66
|
-
|
|
67
|
-
async function clicked(href: string, target?: string) {
|
|
68
|
-
const container = await render(createElement(Link, { href, target }, "go"));
|
|
69
|
-
const node = container.querySelector("a");
|
|
70
|
-
if (!node) throw new Error("Link did not render an anchor");
|
|
71
|
-
return click(node);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
test("leaves an external URL to the browser", async () => {
|
|
75
|
-
expect((await clicked("https://github.com/me")).defaultPrevented).toBe(
|
|
76
|
-
false,
|
|
77
|
-
);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("leaves mailto and tel to the browser", async () => {
|
|
81
|
-
expect((await clicked("mailto:me@example.com")).defaultPrevented).toBe(
|
|
82
|
-
false,
|
|
83
|
-
);
|
|
84
|
-
expect((await clicked("tel:+15551234")).defaultPrevented).toBe(false);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("leaves an explicit target to the browser", async () => {
|
|
88
|
-
expect((await clicked("/about", "_blank")).defaultPrevented).toBe(false);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("still intercepts a same-document path", async () => {
|
|
92
|
-
expect((await clicked("/about")).defaultPrevented).toBe(true);
|
|
93
|
-
});
|
|
94
|
-
});
|
package/test/navigation.test.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { splitLocation, safeHref, isExternal } from "../src/navigation";
|
|
3
|
-
|
|
4
|
-
describe("splitLocation", () => {
|
|
5
|
-
test("splits path and query", () => {
|
|
6
|
-
expect(splitLocation("/path?query=1")).toEqual(["/path", "query=1"]);
|
|
7
|
-
});
|
|
8
|
-
test("handles no query string", () => {
|
|
9
|
-
expect(splitLocation("/path")).toEqual(["/path", ""]);
|
|
10
|
-
});
|
|
11
|
-
test("handles empty string", () => {
|
|
12
|
-
expect(splitLocation("")).toEqual(["", ""]);
|
|
13
|
-
});
|
|
14
|
-
test("handles query string only", () => {
|
|
15
|
-
expect(splitLocation("?query=1")).toEqual(["", "query=1"]);
|
|
16
|
-
});
|
|
17
|
-
test("handles multiple question marks by splitting on the first", () => {
|
|
18
|
-
expect(splitLocation("/path?query=1?other=2")).toEqual([
|
|
19
|
-
"/path",
|
|
20
|
-
"query=1?other=2",
|
|
21
|
-
]);
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe("safeHref", () => {
|
|
26
|
-
test("allows safe schemes", () => {
|
|
27
|
-
expect(safeHref("http://example.com")).toBe("http://example.com");
|
|
28
|
-
expect(safeHref("https://example.com")).toBe("https://example.com");
|
|
29
|
-
expect(safeHref("mailto:user@example.com")).toBe("mailto:user@example.com");
|
|
30
|
-
expect(safeHref("tel:+1234567890")).toBe("tel:+1234567890");
|
|
31
|
-
expect(safeHref("ftp://example.com")).toBe("ftp://example.com");
|
|
32
|
-
});
|
|
33
|
-
test("allows relative paths", () => {
|
|
34
|
-
expect(safeHref("/path")).toBe("/path");
|
|
35
|
-
expect(safeHref("?query=1")).toBe("?query=1");
|
|
36
|
-
expect(safeHref("#hash")).toBe("#hash");
|
|
37
|
-
expect(safeHref("path")).toBe("path");
|
|
38
|
-
});
|
|
39
|
-
test("rewrites unsafe schemes to #", () => {
|
|
40
|
-
expect(safeHref("javascript:alert(1)")).toBe("#");
|
|
41
|
-
expect(safeHref("vbscript:msgbox(1)")).toBe("#");
|
|
42
|
-
expect(safeHref("data:text/html,<h1>hello</h1>")).toBe("#");
|
|
43
|
-
});
|
|
44
|
-
test("strips control characters to prevent evasion", () => {
|
|
45
|
-
expect(safeHref("java\x09script:alert(1)")).toBe("#");
|
|
46
|
-
expect(safeHref("java\nscript:alert(1)")).toBe("#");
|
|
47
|
-
});
|
|
48
|
-
test("handles empty string and spaces", () => {
|
|
49
|
-
expect(safeHref("")).toBe("#");
|
|
50
|
-
expect(safeHref(" ")).toBe("#");
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe("isExternal", () => {
|
|
55
|
-
test("identifies scheme-bearing URLs as external", () => {
|
|
56
|
-
expect(isExternal("http://example.com")).toBe(true);
|
|
57
|
-
expect(isExternal("https://example.com")).toBe(true);
|
|
58
|
-
expect(isExternal("mailto:test@test.com")).toBe(true);
|
|
59
|
-
});
|
|
60
|
-
test("identifies protocol-relative URLs as external", () => {
|
|
61
|
-
expect(isExternal("//example.com")).toBe(true);
|
|
62
|
-
});
|
|
63
|
-
test("identifies hash-only URLs as external (browser handles them)", () => {
|
|
64
|
-
expect(isExternal("#section")).toBe(true);
|
|
65
|
-
});
|
|
66
|
-
test("identifies target other than _self as external", () => {
|
|
67
|
-
expect(isExternal("/path", "_blank")).toBe(true);
|
|
68
|
-
expect(isExternal("/path", "_parent")).toBe(true);
|
|
69
|
-
});
|
|
70
|
-
test("identifies same-document relative paths as internal", () => {
|
|
71
|
-
expect(isExternal("/path")).toBe(false);
|
|
72
|
-
expect(isExternal("path")).toBe(false);
|
|
73
|
-
expect(isExternal("/path?query=1")).toBe(false);
|
|
74
|
-
expect(isExternal("/path", "_self")).toBe(false);
|
|
75
|
-
});
|
|
76
|
-
});
|
package/test/pattern.test.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { compilePattern, matchPath } from "../src/pattern";
|
|
3
|
-
|
|
4
|
-
describe("compilePattern", () => {
|
|
5
|
-
test("static matches exact", () => {
|
|
6
|
-
const p = compilePattern("/about");
|
|
7
|
-
expect(p.match("/about")?.params).toEqual({});
|
|
8
|
-
expect(p.match("/other")).toBeNull();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test("dynamic captures a parameter", () => {
|
|
12
|
-
const p = compilePattern("/users/:id");
|
|
13
|
-
const m = p.match("/users/42");
|
|
14
|
-
expect(m?.params).toEqual({ id: "42" });
|
|
15
|
-
expect(m?.score).toEqual([3, 2]);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test("optional segment may be omitted", () => {
|
|
19
|
-
const p = compilePattern("/:tab?");
|
|
20
|
-
expect(p.match("/")?.params).toEqual({});
|
|
21
|
-
expect(p.match("/profile")?.params).toEqual({ tab: "profile" });
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test("rest captures the remainder", () => {
|
|
25
|
-
const p = compilePattern("/blog/*rest");
|
|
26
|
-
const m = p.match("/blog/a/b");
|
|
27
|
-
expect(m?.params).toEqual({ rest: "a/b" });
|
|
28
|
-
expect(m?.score).toEqual([3, 0]);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("decodes parameter values", () => {
|
|
32
|
-
const p = compilePattern("/users/:id");
|
|
33
|
-
expect(p.match("/users/bob%20smith")?.params.id).toBe("bob smith");
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("malformed encoding returns no match", () => {
|
|
37
|
-
const p = compilePattern("/users/:id");
|
|
38
|
-
expect(p.match("/users/bob%ZZ")).toBeNull();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("precedence reflects segment kind", () => {
|
|
42
|
-
const p = compilePattern("/blog/:slug?");
|
|
43
|
-
expect(p.precedence).toEqual([3, 1]);
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
describe("matchPath", () => {
|
|
48
|
-
test("returns a match object", () => {
|
|
49
|
-
const m = matchPath("/users/:id", "/users/7");
|
|
50
|
-
expect(m).not.toBeNull();
|
|
51
|
-
expect(m!.params.id).toBe("7");
|
|
52
|
-
expect(m!.pattern).toBe("/users/:id");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("rejects length mismatch", () => {
|
|
56
|
-
expect(matchPath("/a/b", "/a")).toBeNull();
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
describe("optional segments backtrack", () => {
|
|
61
|
-
// The regression: the matcher consumed a part whenever one was present and
|
|
62
|
-
// never gave it back, so `/:lang?/about` matched `/en/about` and then failed
|
|
63
|
-
// `/about` — an i18n prefix route 404ing on its own default locale. A failed
|
|
64
|
-
// match is indistinguishable from no route, so it surfaced as a bare 404.
|
|
65
|
-
test("matches with the optional segment absent", () => {
|
|
66
|
-
expect(matchPath("/:lang?/about", "/about")?.params).toEqual({});
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("still binds the optional segment when it is present", () => {
|
|
70
|
-
expect(matchPath("/:lang?/about", "/en/about")?.params).toEqual({
|
|
71
|
-
lang: "en",
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test("does not match when the rest of the pattern cannot", () => {
|
|
76
|
-
expect(matchPath("/:lang?/about", "/en/contact")).toBeNull();
|
|
77
|
-
expect(matchPath("/:lang?/about", "/a/b/about")).toBeNull();
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("handles two optionals before a static tail", () => {
|
|
81
|
-
expect(matchPath("/:a?/:b?/end", "/end")?.params).toEqual({});
|
|
82
|
-
expect(matchPath("/:a?/:b?/end", "/x/end")?.params).toEqual({ a: "x" });
|
|
83
|
-
expect(matchPath("/:a?/:b?/end", "/x/y/end")?.params).toEqual({
|
|
84
|
-
a: "x",
|
|
85
|
-
b: "y",
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
describe("rest segments decode per part", () => {
|
|
91
|
-
// Decoding the joined string let an encoded separator smuggle structure past
|
|
92
|
-
// the router: a handler doing `join(dir, params.path)` on the result read
|
|
93
|
-
// outside its directory, and `a%2Fb` was indistinguishable from `a/b`.
|
|
94
|
-
test("rejects an encoded traversal", () => {
|
|
95
|
-
expect(matchPath("/files/*path", "/files/%2e%2e%2fsecret")).toBeNull();
|
|
96
|
-
expect(matchPath("/files/*path", "/files/..%2Fsecret")).toBeNull();
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("rejects an encoded separator", () => {
|
|
100
|
-
expect(matchPath("/files/*path", "/files/a%2Fb")).toBeNull();
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test("still captures an ordinary nested path", () => {
|
|
104
|
-
expect(matchPath("/files/*path", "/files/a/b/c.txt")?.params).toEqual({
|
|
105
|
-
path: "a/b/c.txt",
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test("still decodes ordinary escapes within a part", () => {
|
|
110
|
-
expect(matchPath("/files/*path", "/files/my%20file.txt")?.params).toEqual({
|
|
111
|
-
path: "my file.txt",
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
});
|
package/tsconfig.json
DELETED