@sigil-dev/grimoire 0.9.21 → 0.9.23

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 (94) hide show
  1. package/README.md +83 -83
  2. package/index.ts +50 -48
  3. package/package.json +22 -10
  4. package/public/__grimoire__/grimoire-client.js +12 -0
  5. package/public/__grimoire__/runtime.js +1241 -0
  6. package/server.ts +22 -20
  7. package/src/adapters/bun.ts +17 -0
  8. package/src/adapters/static.ts +41 -0
  9. package/src/adapters/types.ts +36 -0
  10. package/src/base.ts +91 -0
  11. package/src/client/csrf.ts +47 -47
  12. package/src/client/deferred.ts +179 -179
  13. package/src/client/enhance.ts +160 -160
  14. package/src/client/router.ts +871 -833
  15. package/src/dev/compile-module.ts +211 -211
  16. package/src/dev/loader.ts +141 -141
  17. package/src/dev/resolve.ts +70 -70
  18. package/src/dev/runtime-bundle.ts +51 -51
  19. package/src/env/modules.ts +62 -62
  20. package/src/env/required.ts +42 -0
  21. package/src/env/typegen.ts +61 -61
  22. package/src/headers.ts +105 -105
  23. package/src/logger/transports.ts +117 -117
  24. package/src/rendering/head.ts +49 -49
  25. package/src/rendering/hydrate.ts +161 -161
  26. package/src/rendering/images.ts +43 -43
  27. package/src/rendering/index.ts +755 -749
  28. package/src/rendering/preload.ts +102 -101
  29. package/src/rendering/ssrPlugin.ts +1 -1
  30. package/src/rendering/timing.ts +40 -40
  31. package/src/routing/router.ts +186 -186
  32. package/src/routing/scanner.ts +225 -196
  33. package/src/routing/transform-routes.ts +102 -102
  34. package/src/server/after-response.ts +206 -206
  35. package/src/server/asset-cache.ts +57 -57
  36. package/src/server/build.ts +262 -262
  37. package/src/server/cached.ts +114 -114
  38. package/src/server/client-address.ts +82 -82
  39. package/src/server/client-dir.ts +57 -57
  40. package/src/server/client-entry.ts +44 -44
  41. package/src/server/cookie-utils.ts +80 -80
  42. package/src/server/coordinator.ts +476 -471
  43. package/src/server/hooks.ts +122 -122
  44. package/src/server/index.ts +211 -80
  45. package/src/server/plugins.ts +172 -172
  46. package/src/server/prerender.ts +444 -0
  47. package/src/server/reserved.ts +17 -0
  48. package/src/server/resolver.ts +98 -98
  49. package/src/server/rewrite.ts +28 -0
  50. package/src/server/warm.ts +91 -91
  51. package/src/server/with-headers.ts +27 -27
  52. package/src/server/worker.ts +74 -72
  53. package/src/testing/dom.ts +73 -0
  54. package/src/testing/index.ts +169 -0
  55. package/src/testing/server.ts +162 -0
  56. package/src/typegen/index.ts +450 -450
  57. package/src/types.ts +501 -432
  58. package/test/after-response.test.ts +132 -132
  59. package/test/asset-cache-policy.test.ts +96 -96
  60. package/test/base.test.ts +134 -0
  61. package/test/cached.test.ts +126 -126
  62. package/test/client-address.test.ts +78 -78
  63. package/test/client-csrf.test.ts +61 -61
  64. package/test/client-dir.test.ts +101 -101
  65. package/test/client-router.test.ts +789 -734
  66. package/test/coordinator.test.ts +96 -94
  67. package/test/deferred.test.ts +256 -256
  68. package/test/dev-loader.test.ts +30 -30
  69. package/test/env.test.ts +168 -168
  70. package/test/force-production.test.ts +44 -0
  71. package/test/images.test.ts +49 -49
  72. package/test/locals-header.test.ts +61 -0
  73. package/test/middleware.test.ts +243 -243
  74. package/test/parser-plugins.test.ts +1 -1
  75. package/test/plugin-handle.test.ts +62 -62
  76. package/test/plugin-services-context.test.ts +97 -97
  77. package/test/preload.test.ts +48 -48
  78. package/test/prerender.test.ts +353 -0
  79. package/test/rendering.test.ts +799 -798
  80. package/test/required-env.test.ts +32 -0
  81. package/test/rewrite-html.test.ts +90 -0
  82. package/test/scanning.test.ts +349 -314
  83. package/test/server.test.ts +640 -619
  84. package/test/services.test.ts +90 -90
  85. package/test/streaming.test.ts +381 -378
  86. package/test/testing-helpers.test.ts +166 -0
  87. package/test/typegen.test.ts +808 -799
  88. package/test/warm.test.ts +96 -96
  89. package/test/worker-context.test.ts +59 -59
  90. package/test/workspace-files.test.ts +149 -146
  91. package/test/workspace-versions.test.ts +1 -1
  92. package/.grimoire/_routes.ts +0 -4
  93. package/public/__grimoire__/client.js +0 -55
  94. package/public/__grimoire__/hydrate.js +0 -63
@@ -1,186 +1,186 @@
1
- import type { RouteFile, RouteTree } from "./scanner";
2
-
3
- export interface MatchedRoute {
4
- route: RouteFile;
5
- params: Record<string, string>;
6
- layouts: RouteFile[]; // was: layout?: RouteFile
7
- layoutServers: RouteFile[]; // was: layoutServer?: RouteFile
8
- pageServer?: RouteFile;
9
- /** No page has this address: the layouts that cover it render the closest +error.tsx with a 404. */
10
- notFound?: boolean;
11
- }
12
-
13
- export function matchRoute(
14
- tree: RouteTree,
15
- url: URL,
16
- matchers?: Record<string, (value: string) => boolean>,
17
- ): MatchedRoute | null {
18
- const pathname = url.pathname;
19
-
20
- // check server routes first
21
- for (const server of tree.servers) {
22
- const params = matchPattern(server.path, pathname);
23
- if (params !== null)
24
- return {
25
- route: server,
26
- params,
27
- layouts: [],
28
- layoutServers: [],
29
- };
30
- }
31
-
32
- // then pages
33
- for (const route of tree.routes) {
34
- if (route.type === "pageServer") continue;
35
- const params = matchPattern(route.path, pathname);
36
- if (params === null) continue;
37
-
38
- // [param=matcher] validation — a false matcher rejects the route
39
- let matched = true;
40
- for (const [param, name] of Object.entries(route.paramMatchers ?? {})) {
41
- const fn = matchers?.[name];
42
- if (!fn || !fn(params[param] ?? "")) {
43
- matched = false;
44
- break;
45
- }
46
- }
47
- if (!matched) continue;
48
- // find applicable layouts by matching route pattern prefix, not URL pathname.
49
- // pathname.startsWith(l.path) would fail for parameterized layouts like /w/:workspace.
50
- const layoutMatches = (l: RouteFile) =>
51
- l.path === "/" ||
52
- route.path === l.path ||
53
- route.path.startsWith(`${l.path}/`);
54
-
55
- const layouts = tree.layouts
56
- .filter((l) => l.type === "layout" && layoutMatches(l))
57
- .sort((a, b) => a.path.length - b.path.length); // root first, innermost last
58
-
59
- const layoutServers = tree.layouts
60
- .filter((l) => l.type === "layoutServer" && layoutMatches(l))
61
- .sort((a, b) => a.path.length - b.path.length); // root first, innermost last
62
-
63
- const pageServer = tree.routes.find(
64
- (r) => r.type === "pageServer" && r.path === route.path,
65
- );
66
-
67
- return { route, params, layouts, layoutServers, pageServer };
68
- }
69
-
70
- return null;
71
- }
72
-
73
- /** Whether a folder's pattern (a layout's or an error page's path) covers pathname. */
74
- function covers(
75
- folder: string,
76
- pathname: string,
77
- ): Record<string, string> | null {
78
- if (folder === "/" || folder === "") return {};
79
- const params = matchPattern(`${folder.replace(/\/$/, "")}/*__rest`, pathname);
80
- if (params) delete params.__rest;
81
- return params;
82
- }
83
-
84
- /** How specific a folder pattern is: deeper first, then fixed segments over params. */
85
- function depth(folder: string): number {
86
- const parts = folder.split("/").filter(Boolean);
87
- return (
88
- parts.length * 2 -
89
- (parts.some((p) => p.startsWith(":") || p.startsWith("*")) ? 1 : 0)
90
- );
91
- }
92
-
93
- /**
94
- * The +error.tsx closest to pathname: the deepest folder whose pattern covers
95
- * it, so routes/events/[id]/+error.tsx catches /events/42/anything.
96
- */
97
- export function findClosestError(
98
- errors: RouteFile[],
99
- pathname: string,
100
- ): RouteFile | null {
101
- let best: RouteFile | null = null;
102
- let bestDepth = -1;
103
- for (const e of errors) {
104
- const d = depth(e.path);
105
- if (d > bestDepth && covers(e.path, pathname)) {
106
- best = e;
107
- bestDepth = d;
108
- }
109
- }
110
- return best;
111
- }
112
-
113
- /**
114
- * A match for an address no page has: the layouts (and their server loads)
115
- * of every folder that covers it, so the 404 renders the closest +error.tsx
116
- * inside the site's own layout, exactly like a page whose load throws
117
- * error(404). Without this an unknown address got the error page bare.
118
- */
119
- export function matchNotFound(tree: RouteTree, url: URL): MatchedRoute {
120
- const pathname = url.pathname;
121
- const params: Record<string, string> = {};
122
- const within = (l: RouteFile) => {
123
- const p = covers(l.path, pathname);
124
- if (p) Object.assign(params, p);
125
- return p !== null;
126
- };
127
- const byDepth = (a: RouteFile, b: RouteFile) => a.path.length - b.path.length;
128
- return {
129
- route: { path: pathname, filePath: "", type: "page" } as RouteFile,
130
- params,
131
- layouts: tree.layouts
132
- .filter((l) => l.type === "layout" && within(l))
133
- .sort(byDepth),
134
- layoutServers: tree.layouts
135
- .filter((l) => l.type === "layoutServer" && within(l))
136
- .sort(byDepth),
137
- notFound: true,
138
- };
139
- }
140
-
141
- function matchPattern(
142
- pattern: string,
143
- pathname: string,
144
- ): Record<string, string> | null {
145
- const patternParts = pattern.split("/").filter(Boolean);
146
- const pathParts = pathname.split("/").filter(Boolean);
147
-
148
- const params: Record<string, string> = {};
149
-
150
- // Depth-first walk over (patternIndex, pathIndex). Optional segments try
151
- // consuming one path segment first, then zero — so /[[lang]]/about matches
152
- // both /en/about and /about.
153
- function walk(pi: number, pj: number): boolean {
154
- if (pi === patternParts.length) return pj === pathParts.length;
155
- const patternPart = patternParts[pi]!;
156
-
157
- if (patternPart.startsWith("*")) {
158
- // Rest param — captures zero or more remaining segments joined by "/".
159
- // Rest must be the last segment in the pattern.
160
- params[patternPart.slice(1)] = pathParts.slice(pj).join("/");
161
- return true;
162
- }
163
-
164
- const optional = /^:(.+)\?$/.exec(patternPart);
165
- if (optional) {
166
- const name = optional[1]!;
167
- if (pj < pathParts.length) {
168
- params[name] = pathParts[pj]!;
169
- if (walk(pi + 1, pj + 1)) return true;
170
- delete params[name];
171
- }
172
- return walk(pi + 1, pj);
173
- }
174
-
175
- if (pj >= pathParts.length) return false;
176
-
177
- if (patternPart.startsWith(":")) {
178
- params[patternPart.slice(1)] = pathParts[pj]!;
179
- return walk(pi + 1, pj + 1);
180
- }
181
- if (patternPart !== pathParts[pj]) return false;
182
- return walk(pi + 1, pj + 1);
183
- }
184
-
185
- return walk(0, 0) ? params : null;
186
- }
1
+ import type { RouteFile, RouteTree } from "./scanner";
2
+
3
+ export interface MatchedRoute {
4
+ route: RouteFile;
5
+ params: Record<string, string>;
6
+ layouts: RouteFile[]; // was: layout?: RouteFile
7
+ layoutServers: RouteFile[]; // was: layoutServer?: RouteFile
8
+ pageServer?: RouteFile;
9
+ /** No page has this address: the layouts that cover it render the closest +error.tsx with a 404. */
10
+ notFound?: boolean;
11
+ }
12
+
13
+ export function matchRoute(
14
+ tree: RouteTree,
15
+ url: URL,
16
+ matchers?: Record<string, (value: string) => boolean>,
17
+ ): MatchedRoute | null {
18
+ const pathname = url.pathname;
19
+
20
+ // check server routes first
21
+ for (const server of tree.servers) {
22
+ const params = matchPattern(server.path, pathname);
23
+ if (params !== null)
24
+ return {
25
+ route: server,
26
+ params,
27
+ layouts: [],
28
+ layoutServers: [],
29
+ };
30
+ }
31
+
32
+ // then pages
33
+ for (const route of tree.routes) {
34
+ if (route.type === "pageServer") continue;
35
+ const params = matchPattern(route.path, pathname);
36
+ if (params === null) continue;
37
+
38
+ // [param=matcher] validation — a false matcher rejects the route
39
+ let matched = true;
40
+ for (const [param, name] of Object.entries(route.paramMatchers ?? {})) {
41
+ const fn = matchers?.[name];
42
+ if (!fn || !fn(params[param] ?? "")) {
43
+ matched = false;
44
+ break;
45
+ }
46
+ }
47
+ if (!matched) continue;
48
+ // find applicable layouts by matching route pattern prefix, not URL pathname.
49
+ // pathname.startsWith(l.path) would fail for parameterized layouts like /w/:workspace.
50
+ const layoutMatches = (l: RouteFile) =>
51
+ l.path === "/" ||
52
+ route.path === l.path ||
53
+ route.path.startsWith(`${l.path}/`);
54
+
55
+ const layouts = tree.layouts
56
+ .filter((l) => l.type === "layout" && layoutMatches(l))
57
+ .sort((a, b) => a.path.length - b.path.length); // root first, innermost last
58
+
59
+ const layoutServers = tree.layouts
60
+ .filter((l) => l.type === "layoutServer" && layoutMatches(l))
61
+ .sort((a, b) => a.path.length - b.path.length); // root first, innermost last
62
+
63
+ const pageServer = tree.routes.find(
64
+ (r) => r.type === "pageServer" && r.path === route.path,
65
+ );
66
+
67
+ return { route, params, layouts, layoutServers, pageServer };
68
+ }
69
+
70
+ return null;
71
+ }
72
+
73
+ /** Whether a folder's pattern (a layout's or an error page's path) covers pathname. */
74
+ function covers(
75
+ folder: string,
76
+ pathname: string,
77
+ ): Record<string, string> | null {
78
+ if (folder === "/" || folder === "") return {};
79
+ const params = matchPattern(`${folder.replace(/\/$/, "")}/*__rest`, pathname);
80
+ if (params) delete params.__rest;
81
+ return params;
82
+ }
83
+
84
+ /** How specific a folder pattern is: deeper first, then fixed segments over params. */
85
+ function depth(folder: string): number {
86
+ const parts = folder.split("/").filter(Boolean);
87
+ return (
88
+ parts.length * 2 -
89
+ (parts.some((p) => p.startsWith(":") || p.startsWith("*")) ? 1 : 0)
90
+ );
91
+ }
92
+
93
+ /**
94
+ * The +error.tsx closest to pathname: the deepest folder whose pattern covers
95
+ * it, so routes/events/[id]/+error.tsx catches /events/42/anything.
96
+ */
97
+ export function findClosestError(
98
+ errors: RouteFile[],
99
+ pathname: string,
100
+ ): RouteFile | null {
101
+ let best: RouteFile | null = null;
102
+ let bestDepth = -1;
103
+ for (const e of errors) {
104
+ const d = depth(e.path);
105
+ if (d > bestDepth && covers(e.path, pathname)) {
106
+ best = e;
107
+ bestDepth = d;
108
+ }
109
+ }
110
+ return best;
111
+ }
112
+
113
+ /**
114
+ * A match for an address no page has: the layouts (and their server loads)
115
+ * of every folder that covers it, so the 404 renders the closest +error.tsx
116
+ * inside the site's own layout, exactly like a page whose load throws
117
+ * error(404). Without this an unknown address got the error page bare.
118
+ */
119
+ export function matchNotFound(tree: RouteTree, url: URL): MatchedRoute {
120
+ const pathname = url.pathname;
121
+ const params: Record<string, string> = {};
122
+ const within = (l: RouteFile) => {
123
+ const p = covers(l.path, pathname);
124
+ if (p) Object.assign(params, p);
125
+ return p !== null;
126
+ };
127
+ const byDepth = (a: RouteFile, b: RouteFile) => a.path.length - b.path.length;
128
+ return {
129
+ route: { path: pathname, filePath: "", type: "page" } as RouteFile,
130
+ params,
131
+ layouts: tree.layouts
132
+ .filter((l) => l.type === "layout" && within(l))
133
+ .sort(byDepth),
134
+ layoutServers: tree.layouts
135
+ .filter((l) => l.type === "layoutServer" && within(l))
136
+ .sort(byDepth),
137
+ notFound: true,
138
+ };
139
+ }
140
+
141
+ function matchPattern(
142
+ pattern: string,
143
+ pathname: string,
144
+ ): Record<string, string> | null {
145
+ const patternParts = pattern.split("/").filter(Boolean);
146
+ const pathParts = pathname.split("/").filter(Boolean);
147
+
148
+ const params: Record<string, string> = {};
149
+
150
+ // Depth-first walk over (patternIndex, pathIndex). Optional segments try
151
+ // consuming one path segment first, then zero — so /[[lang]]/about matches
152
+ // both /en/about and /about.
153
+ function walk(pi: number, pj: number): boolean {
154
+ if (pi === patternParts.length) return pj === pathParts.length;
155
+ const patternPart = patternParts[pi]!;
156
+
157
+ if (patternPart.startsWith("*")) {
158
+ // Rest param — captures zero or more remaining segments joined by "/".
159
+ // Rest must be the last segment in the pattern.
160
+ params[patternPart.slice(1)] = pathParts.slice(pj).join("/");
161
+ return true;
162
+ }
163
+
164
+ const optional = /^:(.+)\?$/.exec(patternPart);
165
+ if (optional) {
166
+ const name = optional[1]!;
167
+ if (pj < pathParts.length) {
168
+ params[name] = pathParts[pj]!;
169
+ if (walk(pi + 1, pj + 1)) return true;
170
+ delete params[name];
171
+ }
172
+ return walk(pi + 1, pj);
173
+ }
174
+
175
+ if (pj >= pathParts.length) return false;
176
+
177
+ if (patternPart.startsWith(":")) {
178
+ params[patternPart.slice(1)] = pathParts[pj]!;
179
+ return walk(pi + 1, pj + 1);
180
+ }
181
+ if (patternPart !== pathParts[pj]) return false;
182
+ return walk(pi + 1, pj + 1);
183
+ }
184
+
185
+ return walk(0, 0) ? params : null;
186
+ }