@sigil-dev/grimoire 0.9.2 → 0.9.4

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 (88) hide show
  1. package/index.ts +37 -35
  2. package/package.json +9 -12
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +31 -8
  6. package/src/client/enhance.ts +102 -98
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +5 -5
  9. package/src/client/router.ts +529 -371
  10. package/src/client/scope.ts +22 -22
  11. package/src/dev/compile-module.ts +163 -163
  12. package/src/dev/effect-registry.ts +23 -23
  13. package/src/dev/graph.ts +114 -114
  14. package/src/dev/hmr-client.ts +185 -185
  15. package/src/dev/loader.ts +47 -47
  16. package/src/dev/paths.ts +14 -14
  17. package/src/dev/runtime-bundle.ts +52 -52
  18. package/src/dev/watcher.ts +47 -46
  19. package/src/env/dynamic-private.ts +19 -19
  20. package/src/env/dynamic-public.ts +22 -22
  21. package/src/env/index.ts +25 -25
  22. package/src/env/plugin.ts +19 -19
  23. package/src/env/private.ts +5 -5
  24. package/src/env/public.ts +7 -7
  25. package/src/env/typegen.ts +61 -61
  26. package/src/headers.ts +114 -114
  27. package/src/integrations/vite.ts +73 -73
  28. package/src/logger/index.ts +4 -4
  29. package/src/logger/instance.ts +6 -6
  30. package/src/logger/logger.ts +113 -113
  31. package/src/logger/request-middleware.ts +24 -24
  32. package/src/logger/transports.ts +117 -117
  33. package/src/logger/types.ts +39 -39
  34. package/src/rendering/head.ts +47 -47
  35. package/src/rendering/hydrate.ts +146 -119
  36. package/src/rendering/index.ts +573 -372
  37. package/src/rendering/ssrPlugin.ts +69 -69
  38. package/src/routing/manifest-gen.ts +55 -42
  39. package/src/routing/router.ts +135 -109
  40. package/src/routing/scanner.ts +187 -141
  41. package/src/routing/transform-routes.ts +102 -102
  42. package/src/sentinels/error.ts +52 -52
  43. package/src/sentinels/fail.ts +41 -41
  44. package/src/sentinels/redirect.ts +35 -35
  45. package/src/server/build.ts +255 -219
  46. package/src/server/context.ts +10 -10
  47. package/src/server/cookie-utils.ts +66 -66
  48. package/src/server/coordinator.ts +364 -356
  49. package/src/server/hooks.ts +114 -114
  50. package/src/server/index.ts +677 -658
  51. package/src/server/interactive.ts +56 -56
  52. package/src/server/plugins.ts +121 -120
  53. package/src/server/worker.ts +58 -58
  54. package/src/typegen/index.ts +389 -356
  55. package/src/types.ts +8 -4
  56. package/test/client-router.test.ts +295 -35
  57. package/test/context.test.ts +52 -52
  58. package/test/env.test.ts +70 -70
  59. package/test/exports.test.ts +37 -37
  60. package/test/fail.test.ts +46 -46
  61. package/test/headers.test.ts +100 -100
  62. package/test/hydration.test.ts +119 -119
  63. package/test/middleware.test.ts +223 -223
  64. package/test/preload.ts +5 -5
  65. package/test/redirect-error.test.ts +112 -112
  66. package/test/rendering.test.ts +728 -579
  67. package/test/routing.test.ts +194 -81
  68. package/test/scanning.test.ts +254 -200
  69. package/test/scope.test.ts +195 -195
  70. package/test/server.test.ts +354 -249
  71. package/test/streaming.test.ts +242 -125
  72. package/test/transform-routes.test.ts +87 -84
  73. package/test/typegen.test.ts +697 -682
  74. package/tsconfig.json +10 -10
  75. package/.grimoire/_routes.dom.js +0 -8
  76. package/.grimoire/_routes.hydrate.js +0 -8
  77. package/.grimoire/tsconfig.generated.json +0 -11
  78. package/.grimoire/types/ambient.d.ts +0 -59
  79. package/.grimoire/types/api/hello/$types.d.ts +0 -50
  80. package/.grimoire/types/api/items/$types.d.ts +0 -50
  81. package/.grimoire/types/echo/$types.d.ts +0 -50
  82. package/.grimoire/types/env-private.d.ts +0 -5
  83. package/.grimoire/types/env-public.d.ts +0 -5
  84. package/.grimoire/types/mixed/$types.d.ts +0 -50
  85. package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
  86. package/.grimoire/types/reject/$types.d.ts +0 -50
  87. package/public/__grimoire__/hydrate.js +0 -585
  88. package/public/__grimoire__/index.js +0 -490
@@ -1,81 +1,194 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { matchRoute } from "../src/routing/router";
3
- import type { RouteFile, RouteTree } from "../src/routing/scanner";
4
-
5
- const makeRoute = (path: string, type: RouteFile["type"]): RouteFile => ({
6
- path,
7
- filePath: `/app/routes${path}`,
8
- type,
9
- paramNames: path
10
- .split("/")
11
- .filter((p) => p.startsWith(":"))
12
- .map((p) => p.slice(1)),
13
- restParamNames: path
14
- .split("/")
15
- .filter((p) => p.startsWith("*"))
16
- .map((p) => p.slice(1)),
17
- });
18
-
19
- const tree: RouteTree = {
20
- routes: [
21
- makeRoute("/", "page"),
22
- makeRoute("/about", "simple"),
23
- makeRoute("/blog", "page"),
24
- makeRoute("/blog/:slug", "page"),
25
- makeRoute("/blog/:slug", "pageServer"),
26
- ],
27
- layouts: [makeRoute("/", "layout"), makeRoute("/blog", "layout")],
28
- servers: [makeRoute("/api/todos", "server")],
29
- errors: [],
30
- };
31
-
32
- describe("matchRoute", () => {
33
- test("matches static route", () => {
34
- const match = matchRoute(tree, new URL("http://localhost/about"));
35
- expect(match?.route.path).toBe("/about");
36
- expect(match?.params).toEqual({});
37
- });
38
-
39
- test("matches dynamic route and extracts params", () => {
40
- const match = matchRoute(tree, new URL("http://localhost/blog/hello"));
41
- expect(match?.route.path).toBe("/blog/:slug");
42
- expect(match?.params).toEqual({ slug: "hello" });
43
- });
44
-
45
- test("returns null for unknown route", () => {
46
- const match = matchRoute(tree, new URL("http://localhost/unknown"));
47
- expect(match).toBeNull();
48
- });
49
- });
50
-
51
- describe("matchRoute — rest params (catch-all)", () => {
52
- const restTree: RouteTree = {
53
- routes: [],
54
- layouts: [],
55
- servers: [makeRoute("/docs/*path", "server")],
56
- errors: [],
57
- };
58
-
59
- test("matches single remaining segment", () => {
60
- const match = matchRoute(restTree, new URL("http://localhost/docs/abc"));
61
- expect(match?.route.path).toBe("/docs/*path");
62
- expect(match?.params).toEqual({ path: "abc" });
63
- });
64
-
65
- test("matches multiple remaining segments joined by /", () => {
66
- const match = matchRoute(restTree, new URL("http://localhost/docs/a/b/c"));
67
- expect(match?.route.path).toBe("/docs/*path");
68
- expect(match?.params).toEqual({ path: "a/b/c" });
69
- });
70
-
71
- test("matches zero remaining segments as empty string", () => {
72
- const match = matchRoute(restTree, new URL("http://localhost/docs/"));
73
- expect(match?.route.path).toBe("/docs/*path");
74
- expect(match?.params).toEqual({ path: "" });
75
- });
76
-
77
- test("does not match when prefix segments don't line up", () => {
78
- const match = matchRoute(restTree, new URL("http://localhost/other/a"));
79
- expect(match).toBeNull();
80
- });
81
- });
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
+