@tschk/moonshine-router 0.3.7 → 0.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tschk/moonshine-router",
3
- "version": "0.3.7",
3
+ "version": "0.4.0",
4
4
  "description": "Renderer-neutral route graph and matcher with optional React navigation",
5
5
  "license": "ISC",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -19,6 +19,7 @@ export {
19
19
  useNavigate,
20
20
  useRouter,
21
21
  MoonshineRouter,
22
+ isExternal,
22
23
  Link,
23
24
  safeHref,
24
25
  } from "./navigation.js";
package/src/navigation.ts CHANGED
@@ -6,7 +6,6 @@ import {
6
6
  useEffect,
7
7
  useMemo,
8
8
  useRef,
9
- useState,
10
9
  useSyncExternalStore,
11
10
  type ReactNode,
12
11
  } from "react";
@@ -180,9 +179,6 @@ export function MoonshineRouter(props: MoonshineRouterProps): ReactNode {
180
179
  // Routes are matched on the pathname alone; the query rides along in the
181
180
  // signal only so that changing it re-renders.
182
181
  const pathname = splitLocation(props.path ?? browserPath)[0];
183
- const [ready, setReady] = useState(
184
- typeof window === "undefined" || props.path !== undefined,
185
- );
186
182
 
187
183
  // Compiling every pattern is a sizeable share of this component's render, and
188
184
  // the result depends only on the route list.
@@ -203,7 +199,6 @@ export function MoonshineRouter(props: MoonshineRouterProps): ReactNode {
203
199
  activeRouter = runtime;
204
200
  if (props.path === undefined) {
205
201
  runtime.syncFromBrowser();
206
- setReady(true);
207
202
  const onPop = () => runtime.syncFromBrowser();
208
203
  window.addEventListener("popstate", onPop);
209
204
  return () => {
@@ -224,10 +219,6 @@ export function MoonshineRouter(props: MoonshineRouterProps): ReactNode {
224
219
  runtime,
225
220
  };
226
221
 
227
- if (!ready && props.path === undefined && typeof window !== "undefined") {
228
- // Avoid hydration flicker on first client paint before popstate sync
229
- }
230
-
231
222
  return createElement(
232
223
  RouterContext.Provider,
233
224
  { value },
@@ -265,21 +256,54 @@ export function safeHref(href: string): string {
265
256
  }
266
257
 
267
258
  /** Anchor that uses client-side navigation. */
259
+ /**
260
+ * Whether the browser should keep its own handling of `href`.
261
+ *
262
+ * A client router may only intercept a same-document navigation. Anything
263
+ * carrying a scheme (`https:`, `mailto:`, `tel:`), a protocol-relative `//host`,
264
+ * a bare fragment, or an explicit non-`_self` target belongs to the browser: it
265
+ * opens a mail client, a new tab, or another origin, none of which this router
266
+ * can do.
267
+ *
268
+ * `safeHref` deliberately allows those schemes through — it only strips ones
269
+ * that could execute. Allowing a scheme is not the same as claiming to handle
270
+ * it, and conflating the two is how `<Link href="https://github.com/me">` came
271
+ * to push `/me` onto the app's own origin: preventDefault ran, `navigate`
272
+ * rebuilt the URL against `window.location.origin`, and the user landed on a
273
+ * 404 instead of GitHub.
274
+ *
275
+ * Exported so the host adapters share one definition rather than each carrying
276
+ * a copy — three of them already did, byte for byte, and the copy this module
277
+ * lacked is exactly the one that was wrong.
278
+ */
279
+ export function isExternal(href: string, target?: string): boolean {
280
+ if (target && target !== "_self") return true;
281
+ return (
282
+ /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(href) ||
283
+ href.startsWith("//") ||
284
+ href.startsWith("#")
285
+ );
286
+ }
287
+
268
288
  export function Link(props: {
269
289
  href: string;
270
290
  children?: ReactNode;
271
291
  className?: string;
272
292
  replace?: boolean;
293
+ target?: string;
273
294
  }): ReactNode {
274
295
  const nav = useNavigate();
275
296
  const href = safeHref(props.href);
297
+ const external = isExternal(href, props.target);
276
298
  return createElement(
277
299
  "a",
278
300
  {
279
301
  href,
280
302
  className: props.className,
303
+ target: props.target,
281
304
  onClick: (event: MouseEvent) => {
282
305
  if (
306
+ external ||
283
307
  event.defaultPrevented ||
284
308
  event.button !== 0 ||
285
309
  event.metaKey ||
package/src/pattern.ts CHANGED
@@ -75,64 +75,113 @@ export function compilePattern(pattern: string): CompiledPattern {
75
75
  normalized = "/";
76
76
  }
77
77
 
78
- const match = (pathname: string): MatchResult | null => {
79
- const pathParts = splitPath(pathname);
80
- const params: RouteParams = {};
81
- const score: number[] = [];
82
- let i = 0;
83
-
84
- for (const segment of segments) {
85
- if (segment.kind === "rest") {
86
- const remaining = pathParts.slice(i).join("/");
87
- try {
88
- params[segment.name] = decodeURIComponent(remaining);
89
- } catch {
90
- return null;
91
- }
92
- score.push(SEGMENT_PRECEDENCE.rest);
93
- i = pathParts.length;
94
- continue;
78
+ let pathParts: string[] = [];
79
+
80
+ /**
81
+ * Decodes the tail a `*rest` segment captures.
82
+ *
83
+ * Per part, not on the joined string. Decoding after the join lets an encoded
84
+ * separator smuggle structure past the router: `/files/%2e%2e%2fsecret` used
85
+ * to arrive as `path: "../secret"`, and a handler doing the obvious
86
+ * `join(dir, params.path)` then read outside its directory. `a%2Fb` and `a/b`
87
+ * were likewise indistinguishable. Same rules the static server already
88
+ * applies in `resolveStaticPath`, so the two agree about what a path segment
89
+ * may contain.
90
+ */
91
+ const decodeRest = (parts: readonly string[]): string | null => {
92
+ const decoded: string[] = [];
93
+ for (const part of parts) {
94
+ const d = decodePart(part);
95
+ if (
96
+ d === null ||
97
+ d === "." ||
98
+ d === ".." ||
99
+ d.includes("\0") ||
100
+ d.includes("/")
101
+ ) {
102
+ return null;
95
103
  }
104
+ decoded.push(d);
105
+ }
106
+ return decoded.join("/");
107
+ };
96
108
 
97
- if (segment.kind === "static") {
98
- const part = pathParts[i];
99
- if (part === undefined) return null;
100
- const decoded = decodePart(part);
101
- if (decoded === null) return null;
102
- if (decoded !== segment.value) return null;
103
- score.push(SEGMENT_PRECEDENCE.static);
104
- i++;
105
- continue;
106
- }
109
+ /**
110
+ * Matches `segments` from `si` against `pathParts` from `pi`.
111
+ *
112
+ * Recursive because an optional segment has to be able to give its part back.
113
+ * The old linear pass consumed a part whenever one was present, so
114
+ * `/:lang?/about` matched `/en/about` and then failed `/about` — the i18n
115
+ * prefix route 404ing on its own default locale, silently, because a failed
116
+ * match is indistinguishable from no route. Consuming is tried first so the
117
+ * greedy reading still wins where both work.
118
+ */
119
+ const matchFrom = (si: number, pi: number): MatchResult | null => {
120
+ if (si === segments.length) {
121
+ return pi === pathParts.length ? { params: {}, score: [] } : null;
122
+ }
107
123
 
108
- if (segment.kind === "dynamic") {
109
- const part = pathParts[i];
110
- if (part === undefined) return null;
111
- const decoded = decodePart(part);
112
- if (decoded === null) return null;
113
- params[segment.name] = decoded;
114
- score.push(SEGMENT_PRECEDENCE.dynamic);
115
- i++;
116
- continue;
117
- }
124
+ const segment = segments[si];
125
+ const part = pathParts[pi];
126
+
127
+ const withHead = (
128
+ rest: MatchResult,
129
+ points: number,
130
+ binding?: [string, string],
131
+ ): MatchResult => ({
132
+ params: binding
133
+ ? { [binding[0]]: binding[1], ...rest.params }
134
+ : rest.params,
135
+ score: [points, ...rest.score],
136
+ });
137
+
138
+ if (segment.kind === "rest") {
139
+ const value = decodeRest(pathParts.slice(pi));
140
+ if (value === null) return null;
141
+ const rest = matchFrom(si + 1, pathParts.length);
142
+ return (
143
+ rest && withHead(rest, SEGMENT_PRECEDENCE.rest, [segment.name, value])
144
+ );
145
+ }
118
146
 
119
- if (segment.kind === "optional") {
120
- const part = pathParts[i];
121
- if (part === undefined) {
122
- score.push(0);
123
- continue;
124
- }
125
- const decoded = decodePart(part);
126
- if (decoded === null) return null;
127
- params[segment.name] = decoded;
128
- score.push(SEGMENT_PRECEDENCE.optional);
129
- i++;
130
- continue;
147
+ if (segment.kind === "static") {
148
+ if (part === undefined) return null;
149
+ const decoded = decodePart(part);
150
+ if (decoded === null || decoded !== segment.value) return null;
151
+ const rest = matchFrom(si + 1, pi + 1);
152
+ return rest && withHead(rest, SEGMENT_PRECEDENCE.static);
153
+ }
154
+
155
+ if (segment.kind === "dynamic") {
156
+ if (part === undefined) return null;
157
+ const decoded = decodePart(part);
158
+ if (decoded === null) return null;
159
+ const rest = matchFrom(si + 1, pi + 1);
160
+ return (
161
+ rest &&
162
+ withHead(rest, SEGMENT_PRECEDENCE.dynamic, [segment.name, decoded])
163
+ );
164
+ }
165
+
166
+ // optional: consume first, then fall back to skipping.
167
+ if (part !== undefined) {
168
+ const decoded = decodePart(part);
169
+ if (decoded !== null) {
170
+ const rest = matchFrom(si + 1, pi + 1);
171
+ if (rest)
172
+ return withHead(rest, SEGMENT_PRECEDENCE.optional, [
173
+ segment.name,
174
+ decoded,
175
+ ]);
131
176
  }
132
177
  }
178
+ const skipped = matchFrom(si + 1, pi);
179
+ return skipped && withHead(skipped, 0);
180
+ };
133
181
 
134
- if (i < pathParts.length) return null;
135
- return { params, score };
182
+ const match = (pathname: string): MatchResult | null => {
183
+ pathParts = splitPath(pathname);
184
+ return matchFrom(0, 0);
136
185
  };
137
186
 
138
187
  return { pattern, segments, precedence, normalized, match };
package/test/dom.ts ADDED
@@ -0,0 +1,109 @@
1
+ import { GlobalRegistrator } from "@happy-dom/global-registrator";
2
+ import { act } from "react";
3
+ import { createRoot } from "react-dom/client";
4
+ import type { ReactNode } from "react";
5
+
6
+ /**
7
+ * `bun test` shares one process across files, so registering happy-dom leaks
8
+ * its browser implementations into every other suite: its `Request` strips
9
+ * forbidden headers like `Cookie`, and its streams are not the ones SSR
10
+ * renderers write to. Keep the DOM, put Bun's platform globals back.
11
+ */
12
+ const keepNative = [
13
+ "Request",
14
+ "Response",
15
+ "Headers",
16
+ "fetch",
17
+ "ReadableStream",
18
+ "WritableStream",
19
+ "TransformStream",
20
+ "TextEncoder",
21
+ "TextDecoder",
22
+ "AbortController",
23
+ "AbortSignal",
24
+ "Blob",
25
+ "FormData",
26
+ "URL",
27
+ "URLSearchParams",
28
+ "setTimeout",
29
+ "clearTimeout",
30
+ "setInterval",
31
+ "clearInterval",
32
+ "queueMicrotask",
33
+ ] as const;
34
+
35
+ if (!("happyDOM" in globalThis)) {
36
+ const scope = globalThis as unknown as Record<string, unknown>;
37
+ const native = Object.fromEntries(keepNative.map((key) => [key, scope[key]]));
38
+ GlobalRegistrator.register({ url: "https://example.com/start" });
39
+ Object.assign(scope, native);
40
+ }
41
+
42
+ (
43
+ globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
44
+ ).IS_REACT_ACT_ENVIRONMENT = true;
45
+
46
+ const mounted: { unmount(): void }[] = [];
47
+
48
+ /** Mounts `element` into a fresh detached container and flushes effects. */
49
+ export async function render(element: ReactNode): Promise<HTMLElement> {
50
+ const container = document.createElement("div");
51
+ document.body.append(container);
52
+ const root = createRoot(container);
53
+ mounted.push(root);
54
+ await act(async () => {
55
+ root.render(element);
56
+ });
57
+ return container;
58
+ }
59
+
60
+ /**
61
+ * Unmounts every root from the current test. Without this, trees left
62
+ * subscribed to the router signal re-render on the next test's navigation.
63
+ */
64
+ export async function cleanup(): Promise<void> {
65
+ await act(async () => {
66
+ for (const root of mounted.splice(0)) root.unmount();
67
+ });
68
+ document.body.replaceChildren();
69
+ }
70
+
71
+ /**
72
+ * Dispatches a real bubbling click so React's root listener runs, and reports
73
+ * whether the handler claimed the event.
74
+ *
75
+ * A document-level guard records `defaultPrevented` and then cancels the event
76
+ * regardless, so happy-dom does not follow un-intercepted anchors and leave the
77
+ * document on a URL later tests cannot navigate from.
78
+ */
79
+ export async function click(
80
+ node: Element,
81
+ init: MouseEventInit = {},
82
+ ): Promise<{ defaultPrevented: boolean }> {
83
+ let defaultPrevented = false;
84
+ const guard = (event: Event): void => {
85
+ defaultPrevented = event.defaultPrevented;
86
+ event.preventDefault();
87
+ };
88
+ document.addEventListener("click", guard);
89
+ try {
90
+ await act(async () => {
91
+ node.dispatchEvent(
92
+ new MouseEvent("click", {
93
+ bubbles: true,
94
+ cancelable: true,
95
+ button: 0,
96
+ ...init,
97
+ }),
98
+ );
99
+ });
100
+ } finally {
101
+ document.removeEventListener("click", guard);
102
+ }
103
+ return { defaultPrevented };
104
+ }
105
+
106
+ /** Resets the document location between navigation assertions. */
107
+ export function setLocation(url: string): void {
108
+ window.history.replaceState({}, "", new URL(url, "https://example.com").href);
109
+ }
@@ -1,7 +1,8 @@
1
- import { describe, expect, test } from "bun:test";
1
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2
2
  import { createElement } from "react";
3
3
  import { renderToStaticMarkup } from "react-dom/server";
4
- import { Link, safeHref } from "../src/navigation";
4
+ import { isExternal, Link, safeHref } from "../src/navigation";
5
+ import { click, render, setLocation, cleanup } from "./dom";
5
6
 
6
7
  function hrefOf(href: string): string {
7
8
  const html = renderToStaticMarkup(createElement(Link, { href }, "go"));
@@ -36,3 +37,58 @@ describe("Link", () => {
36
37
  expect(hrefOf("/ok")).toBe("/ok");
37
38
  });
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
+ });
@@ -0,0 +1,76 @@
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
+ });
@@ -56,3 +56,59 @@ describe("matchPath", () => {
56
56
  expect(matchPath("/a/b", "/a")).toBeNull();
57
57
  });
58
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
+ });