@sigil-dev/grimoire 0.9.4 → 0.9.6

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.
Files changed (78) hide show
  1. package/index.ts +37 -37
  2. package/package.json +3 -3
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +36 -36
  6. package/src/client/enhance.ts +102 -102
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +5 -5
  9. package/src/client/router.ts +128 -38
  10. package/src/client/scope.ts +22 -22
  11. package/src/dev/compile-module.ts +215 -163
  12. package/src/dev/effect-registry.ts +23 -23
  13. package/src/dev/graph.ts +109 -114
  14. package/src/dev/hmr-client.ts +202 -185
  15. package/src/dev/hmr-server.ts +261 -187
  16. package/src/dev/loader.ts +120 -47
  17. package/src/dev/paths.ts +14 -14
  18. package/src/dev/resolve.ts +70 -0
  19. package/src/dev/runtime-bundle.ts +52 -52
  20. package/src/dev/scan.ts +78 -0
  21. package/src/dev/watcher.ts +69 -47
  22. package/src/env/dynamic-private.ts +19 -19
  23. package/src/env/dynamic-public.ts +22 -22
  24. package/src/env/index.ts +25 -25
  25. package/src/env/plugin.ts +19 -19
  26. package/src/env/private.ts +5 -5
  27. package/src/env/public.ts +7 -7
  28. package/src/env/typegen.ts +61 -61
  29. package/src/headers.ts +114 -114
  30. package/src/integrations/vite.ts +73 -73
  31. package/src/logger/index.ts +4 -4
  32. package/src/logger/instance.ts +6 -6
  33. package/src/logger/logger.ts +113 -113
  34. package/src/logger/request-middleware.ts +24 -24
  35. package/src/logger/transports.ts +117 -117
  36. package/src/logger/types.ts +39 -39
  37. package/src/rendering/head.ts +52 -47
  38. package/src/rendering/hydrate.ts +16 -3
  39. package/src/rendering/index.ts +43 -17
  40. package/src/rendering/ssrPlugin.ts +78 -69
  41. package/src/routing/manifest-gen.ts +55 -55
  42. package/src/routing/router.ts +135 -135
  43. package/src/routing/scanner.ts +196 -187
  44. package/src/routing/transform-routes.ts +102 -102
  45. package/src/sentinels/error.ts +52 -52
  46. package/src/sentinels/fail.ts +41 -41
  47. package/src/sentinels/redirect.ts +35 -35
  48. package/src/server/build.ts +250 -255
  49. package/src/server/context.ts +10 -10
  50. package/src/server/cookie-utils.ts +66 -66
  51. package/src/server/coordinator.ts +1 -1
  52. package/src/server/hooks.ts +114 -114
  53. package/src/server/index.ts +110 -69
  54. package/src/server/interactive.ts +56 -56
  55. package/src/server/plugins.ts +121 -121
  56. package/src/server/worker.ts +58 -58
  57. package/src/typegen/index.ts +389 -389
  58. package/src/types.ts +286 -286
  59. package/test/client-router.test.ts +76 -8
  60. package/test/context.test.ts +52 -52
  61. package/test/env.test.ts +90 -90
  62. package/test/exports.test.ts +37 -37
  63. package/test/fail.test.ts +46 -46
  64. package/test/headers.test.ts +100 -100
  65. package/test/hmr.test.ts +426 -0
  66. package/test/hydration.test.ts +119 -119
  67. package/test/middleware.test.ts +223 -223
  68. package/test/preload.ts +5 -5
  69. package/test/redirect-error.test.ts +112 -112
  70. package/test/rendering.test.ts +719 -728
  71. package/test/routing.test.ts +197 -194
  72. package/test/scanning.test.ts +314 -254
  73. package/test/scope.test.ts +195 -195
  74. package/test/server.test.ts +360 -354
  75. package/test/streaming.test.ts +40 -1
  76. package/test/transform-routes.test.ts +87 -87
  77. package/test/typegen.test.ts +697 -697
  78. package/tsconfig.json +10 -10
@@ -1,194 +1,197 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { matchRoute } from "../src/routing/router";
3
- import { generateManifest } from "../src/routing/manifest-gen";
4
- import type { RouteFile, RouteTree } from "../src/routing/scanner";
5
-
6
- const makeRoute = (path: string, type: RouteFile["type"]): RouteFile => ({
7
- path,
8
- filePath: `/app/routes${path}`,
9
- clientPath: `/src/routes${path}`,
10
- type,
11
- paramNames: path
12
- .split("/")
13
- .filter((p) => p.startsWith(":") && !p.endsWith("?"))
14
- .map((p) => p.slice(1)),
15
- optionalParamNames: path
16
- .split("/")
17
- .filter((p) => p.startsWith(":") && p.endsWith("?"))
18
- .map((p) => p.slice(1, -1)),
19
- restParamNames: path
20
- .split("/")
21
- .filter((p) => p.startsWith("*"))
22
- .map((p) => p.slice(1)),
23
- paramMatchers: {},
24
- });
25
-
26
- const noMatcherFiles = { paramMatcherFiles: [] as never[] };
27
-
28
- const tree: RouteTree = {
29
- routes: [
30
- makeRoute("/", "page"),
31
- makeRoute("/about", "simple"),
32
- makeRoute("/blog", "page"),
33
- makeRoute("/blog/:slug", "page"),
34
- makeRoute("/blog/:slug", "pageServer"),
35
- ],
36
- layouts: [makeRoute("/", "layout"), makeRoute("/blog", "layout")],
37
- servers: [makeRoute("/api/todos", "server")],
38
- errors: [],
39
- ...noMatcherFiles,
40
- };
41
-
42
- describe("matchRoute", () => {
43
- test("matches static route", () => {
44
- const match = matchRoute(tree, new URL("http://localhost/about"));
45
- expect(match?.route.path).toBe("/about");
46
- expect(match?.params).toEqual({});
47
- });
48
-
49
- test("matches dynamic route and extracts params", () => {
50
- const match = matchRoute(tree, new URL("http://localhost/blog/hello"));
51
- expect(match?.route.path).toBe("/blog/:slug");
52
- expect(match?.params).toEqual({ slug: "hello" });
53
- });
54
-
55
- test("returns null for unknown route", () => {
56
- const match = matchRoute(tree, new URL("http://localhost/unknown"));
57
- expect(match).toBeNull();
58
- });
59
- });
60
-
61
- describe("matchRoute — rest params (catch-all)", () => {
62
- const restTree: RouteTree = {
63
- routes: [],
64
- layouts: [],
65
- servers: [makeRoute("/docs/*path", "server")],
66
- errors: [],
67
- ...noMatcherFiles,
68
- };
69
-
70
- test("matches single remaining segment", () => {
71
- const match = matchRoute(restTree, new URL("http://localhost/docs/abc"));
72
- expect(match?.route.path).toBe("/docs/*path");
73
- expect(match?.params).toEqual({ path: "abc" });
74
- });
75
-
76
- test("matches multiple remaining segments joined by /", () => {
77
- const match = matchRoute(restTree, new URL("http://localhost/docs/a/b/c"));
78
- expect(match?.route.path).toBe("/docs/*path");
79
- expect(match?.params).toEqual({ path: "a/b/c" });
80
- });
81
-
82
- test("matches zero remaining segments as empty string", () => {
83
- const match = matchRoute(restTree, new URL("http://localhost/docs/"));
84
- expect(match?.route.path).toBe("/docs/*path");
85
- expect(match?.params).toEqual({ path: "" });
86
- });
87
-
88
- test("does not match when prefix segments don't line up", () => {
89
- const match = matchRoute(restTree, new URL("http://localhost/other/a"));
90
- expect(match).toBeNull();
91
- });
92
- });
93
-
94
- describe("matchRoute — optional params", () => {
95
- // ordered as the scanner would: static (0) < optional (1) < required (2)
96
- const optTree: RouteTree = {
97
- routes: [
98
- makeRoute("/about", "simple"),
99
- makeRoute("/:lang?", "page"),
100
- makeRoute("/:slug", "page"),
101
- ],
102
- layouts: [],
103
- servers: [],
104
- errors: [],
105
- ...noMatcherFiles,
106
- };
107
-
108
- test("optional segment consumes one path segment", () => {
109
- const match = matchRoute(optTree, new URL("http://localhost/en"));
110
- expect(match?.route.path).toBe("/:lang?");
111
- expect(match?.params).toEqual({ lang: "en" });
112
- });
113
-
114
- test("optional segment matches zero segments", () => {
115
- const match = matchRoute(optTree, new URL("http://localhost/"));
116
- expect(match?.route.path).toBe("/:lang?");
117
- expect(match?.params).toEqual({});
118
- });
119
-
120
- test("static beats optional", () => {
121
- const match = matchRoute(optTree, new URL("http://localhost/about"));
122
- expect(match?.route.path).toBe("/about");
123
- });
124
-
125
- test("optional beats required param", () => {
126
- const match = matchRoute(optTree, new URL("http://localhost/en"));
127
- expect(match?.route.path).toBe("/:lang?");
128
- expect(match?.route.path).not.toBe("/:slug");
129
- });
130
-
131
- test("mid-pattern optional backtracks: /about matches /[[lang]]/about", () => {
132
- const nested: RouteTree = {
133
- routes: [makeRoute("/:lang?/about", "page")],
134
- layouts: [],
135
- servers: [],
136
- errors: [],
137
- ...noMatcherFiles,
138
- };
139
- const zero = matchRoute(nested, new URL("http://localhost/about"));
140
- expect(zero?.params).toEqual({});
141
- const one = matchRoute(nested, new URL("http://localhost/en/about"));
142
- expect(one?.params).toEqual({ lang: "en" });
143
- });
144
- });
145
-
146
- describe("matchRoute — custom matchers", () => {
147
- const intMatch = (v: string) => /^\d+$/.test(v);
148
- const matcherTree: RouteTree = {
149
- routes: [makeRoute("/post/:id", "page")],
150
- layouts: [],
151
- servers: [],
152
- errors: [],
153
- ...noMatcherFiles,
154
- };
155
- matcherTree.routes[0]!.paramMatchers = { id: "int" };
156
-
157
- test("matcher passes → route matches", () => {
158
- const match = matchRoute(matcherTree, new URL("http://localhost/post/42"), {
159
- int: intMatch,
160
- });
161
- expect(match?.params).toEqual({ id: "42" });
162
- });
163
-
164
- test("matcher fails → route is rejected (404)", () => {
165
- const match = matchRoute(matcherTree, new URL("http://localhost/post/abc"), {
166
- int: intMatch,
167
- });
168
- expect(match).toBeNull();
169
- });
170
-
171
- test("missing matcher module rejects the route", () => {
172
- const match = matchRoute(matcherTree, new URL("http://localhost/post/42"));
173
- expect(match).toBeNull();
174
- });
175
- });
176
-
177
- describe("generateManifest — param matchers", () => {
178
- test("emits eager matcher imports and record", () => {
179
- const manifest = generateManifest(
180
- [makeRoute("/", "page")],
181
- undefined,
182
- [{ name: "int", filePath: "/app/params/int.ts" }],
183
- );
184
- expect(manifest).toContain('import * as __param_int from "/app/params/int.ts"');
185
- expect(manifest).toContain('"int": __param_int.match');
186
- expect(manifest).toContain("export const matchers = {");
187
- });
188
-
189
- test("emits matchers record even when none exist", () => {
190
- const manifest = generateManifest([makeRoute("/", "page")]);
191
- expect(manifest).toMatch(/export const matchers = \{\s*\};/);
192
- });
193
- });
194
-
1
+ import { describe, expect, test } from "bun:test";
2
+ import { generateManifest } from "../src/routing/manifest-gen";
3
+ import { matchRoute } from "../src/routing/router";
4
+ import type { RouteFile, RouteTree } from "../src/routing/scanner";
5
+
6
+ const makeRoute = (path: string, type: RouteFile["type"]): RouteFile => ({
7
+ path,
8
+ filePath: `/app/routes${path}`,
9
+ clientPath: `/src/routes${path}`,
10
+ type,
11
+ paramNames: path
12
+ .split("/")
13
+ .filter((p) => p.startsWith(":") && !p.endsWith("?"))
14
+ .map((p) => p.slice(1)),
15
+ optionalParamNames: path
16
+ .split("/")
17
+ .filter((p) => p.startsWith(":") && p.endsWith("?"))
18
+ .map((p) => p.slice(1, -1)),
19
+ restParamNames: path
20
+ .split("/")
21
+ .filter((p) => p.startsWith("*"))
22
+ .map((p) => p.slice(1)),
23
+ paramMatchers: {},
24
+ });
25
+
26
+ const noMatcherFiles = { paramMatcherFiles: [] as never[] };
27
+
28
+ const tree: RouteTree = {
29
+ routes: [
30
+ makeRoute("/", "page"),
31
+ makeRoute("/about", "simple"),
32
+ makeRoute("/blog", "page"),
33
+ makeRoute("/blog/:slug", "page"),
34
+ makeRoute("/blog/:slug", "pageServer"),
35
+ ],
36
+ layouts: [makeRoute("/", "layout"), makeRoute("/blog", "layout")],
37
+ servers: [makeRoute("/api/todos", "server")],
38
+ errors: [],
39
+ ...noMatcherFiles,
40
+ };
41
+
42
+ describe("matchRoute", () => {
43
+ test("matches static route", () => {
44
+ const match = matchRoute(tree, new URL("http://localhost/about"));
45
+ expect(match?.route.path).toBe("/about");
46
+ expect(match?.params).toEqual({});
47
+ });
48
+
49
+ test("matches dynamic route and extracts params", () => {
50
+ const match = matchRoute(tree, new URL("http://localhost/blog/hello"));
51
+ expect(match?.route.path).toBe("/blog/:slug");
52
+ expect(match?.params).toEqual({ slug: "hello" });
53
+ });
54
+
55
+ test("returns null for unknown route", () => {
56
+ const match = matchRoute(tree, new URL("http://localhost/unknown"));
57
+ expect(match).toBeNull();
58
+ });
59
+ });
60
+
61
+ describe("matchRoute — rest params (catch-all)", () => {
62
+ const restTree: RouteTree = {
63
+ routes: [],
64
+ layouts: [],
65
+ servers: [makeRoute("/docs/*path", "server")],
66
+ errors: [],
67
+ ...noMatcherFiles,
68
+ };
69
+
70
+ test("matches single remaining segment", () => {
71
+ const match = matchRoute(restTree, new URL("http://localhost/docs/abc"));
72
+ expect(match?.route.path).toBe("/docs/*path");
73
+ expect(match?.params).toEqual({ path: "abc" });
74
+ });
75
+
76
+ test("matches multiple remaining segments joined by /", () => {
77
+ const match = matchRoute(restTree, new URL("http://localhost/docs/a/b/c"));
78
+ expect(match?.route.path).toBe("/docs/*path");
79
+ expect(match?.params).toEqual({ path: "a/b/c" });
80
+ });
81
+
82
+ test("matches zero remaining segments as empty string", () => {
83
+ const match = matchRoute(restTree, new URL("http://localhost/docs/"));
84
+ expect(match?.route.path).toBe("/docs/*path");
85
+ expect(match?.params).toEqual({ path: "" });
86
+ });
87
+
88
+ test("does not match when prefix segments don't line up", () => {
89
+ const match = matchRoute(restTree, new URL("http://localhost/other/a"));
90
+ expect(match).toBeNull();
91
+ });
92
+ });
93
+
94
+ describe("matchRoute — optional params", () => {
95
+ // ordered as the scanner would: static (0) < optional (1) < required (2)
96
+ const optTree: RouteTree = {
97
+ routes: [
98
+ makeRoute("/about", "simple"),
99
+ makeRoute("/:lang?", "page"),
100
+ makeRoute("/:slug", "page"),
101
+ ],
102
+ layouts: [],
103
+ servers: [],
104
+ errors: [],
105
+ ...noMatcherFiles,
106
+ };
107
+
108
+ test("optional segment consumes one path segment", () => {
109
+ const match = matchRoute(optTree, new URL("http://localhost/en"));
110
+ expect(match?.route.path).toBe("/:lang?");
111
+ expect(match?.params).toEqual({ lang: "en" });
112
+ });
113
+
114
+ test("optional segment matches zero segments", () => {
115
+ const match = matchRoute(optTree, new URL("http://localhost/"));
116
+ expect(match?.route.path).toBe("/:lang?");
117
+ expect(match?.params).toEqual({});
118
+ });
119
+
120
+ test("static beats optional", () => {
121
+ const match = matchRoute(optTree, new URL("http://localhost/about"));
122
+ expect(match?.route.path).toBe("/about");
123
+ });
124
+
125
+ test("optional beats required param", () => {
126
+ const match = matchRoute(optTree, new URL("http://localhost/en"));
127
+ expect(match?.route.path).toBe("/:lang?");
128
+ expect(match?.route.path).not.toBe("/:slug");
129
+ });
130
+
131
+ test("mid-pattern optional backtracks: /about matches /[[lang]]/about", () => {
132
+ const nested: RouteTree = {
133
+ routes: [makeRoute("/:lang?/about", "page")],
134
+ layouts: [],
135
+ servers: [],
136
+ errors: [],
137
+ ...noMatcherFiles,
138
+ };
139
+ const zero = matchRoute(nested, new URL("http://localhost/about"));
140
+ expect(zero?.params).toEqual({});
141
+ const one = matchRoute(nested, new URL("http://localhost/en/about"));
142
+ expect(one?.params).toEqual({ lang: "en" });
143
+ });
144
+ });
145
+
146
+ describe("matchRoute — custom matchers", () => {
147
+ const intMatch = (v: string) => /^\d+$/.test(v);
148
+ const matcherTree: RouteTree = {
149
+ routes: [makeRoute("/post/:id", "page")],
150
+ layouts: [],
151
+ servers: [],
152
+ errors: [],
153
+ ...noMatcherFiles,
154
+ };
155
+ matcherTree.routes[0]!.paramMatchers = { id: "int" };
156
+
157
+ test("matcher passes → route matches", () => {
158
+ const match = matchRoute(matcherTree, new URL("http://localhost/post/42"), {
159
+ int: intMatch,
160
+ });
161
+ expect(match?.params).toEqual({ id: "42" });
162
+ });
163
+
164
+ test("matcher fails → route is rejected (404)", () => {
165
+ const match = matchRoute(
166
+ matcherTree,
167
+ new URL("http://localhost/post/abc"),
168
+ {
169
+ int: intMatch,
170
+ },
171
+ );
172
+ expect(match).toBeNull();
173
+ });
174
+
175
+ test("missing matcher module rejects the route", () => {
176
+ const match = matchRoute(matcherTree, new URL("http://localhost/post/42"));
177
+ expect(match).toBeNull();
178
+ });
179
+ });
180
+
181
+ describe("generateManifest — param matchers", () => {
182
+ test("emits eager matcher imports and record", () => {
183
+ const manifest = generateManifest([makeRoute("/", "page")], undefined, [
184
+ { name: "int", filePath: "/app/params/int.ts" },
185
+ ]);
186
+ expect(manifest).toContain(
187
+ 'import * as __param_int from "/app/params/int.ts"',
188
+ );
189
+ expect(manifest).toContain('"int": __param_int.match');
190
+ expect(manifest).toContain("export const matchers = {");
191
+ });
192
+
193
+ test("emits matchers record even when none exist", () => {
194
+ const manifest = generateManifest([makeRoute("/", "page")]);
195
+ expect(manifest).toMatch(/export const matchers = \{\s*\};/);
196
+ });
197
+ });