@sigil-dev/grimoire 0.9.20 → 0.9.22
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 +83 -83
- package/index.ts +50 -48
- package/package.json +16 -5
- package/public/__grimoire__/grimoire-client.js +12 -0
- package/public/__grimoire__/runtime.js +1241 -0
- package/server.ts +22 -20
- package/src/adapters/bun.ts +17 -0
- package/src/adapters/static.ts +41 -0
- package/src/adapters/types.ts +36 -0
- package/src/base.ts +91 -0
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +179 -179
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +870 -833
- package/src/dev/compile-module.ts +211 -211
- package/src/dev/loader.ts +141 -141
- package/src/dev/resolve.ts +70 -70
- package/src/dev/runtime-bundle.ts +51 -51
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -0
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -114
- package/src/logger/transports.ts +117 -117
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +161 -161
- package/src/rendering/images.ts +43 -43
- package/src/rendering/index.ts +755 -745
- package/src/rendering/preload.ts +101 -95
- package/src/rendering/ssrPlugin.ts +108 -108
- package/src/rendering/timing.ts +40 -40
- package/src/routing/router.ts +186 -186
- package/src/routing/scanner.ts +225 -196
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -194
- package/src/server/asset-cache.ts +57 -55
- package/src/server/build.ts +262 -249
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +57 -57
- package/src/server/client-entry.ts +44 -0
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -471
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +1138 -1022
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +444 -0
- package/src/server/reserved.ts +17 -0
- package/src/server/resolver.ts +98 -98
- package/src/server/rewrite.ts +28 -0
- package/src/server/warm.ts +91 -91
- package/src/server/with-headers.ts +27 -0
- package/src/server/worker.ts +74 -72
- package/src/testing/dom.ts +73 -0
- package/src/testing/index.ts +169 -0
- package/src/testing/server.ts +162 -0
- package/src/typegen/index.ts +450 -450
- package/src/types.ts +501 -432
- package/test/after-response.test.ts +132 -113
- package/test/asset-cache-policy.test.ts +96 -96
- package/test/base.test.ts +134 -0
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -69
- package/test/client-router.test.ts +789 -734
- package/test/coordinator.test.ts +96 -94
- package/test/deferred.test.ts +256 -256
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +168 -168
- package/test/force-production.test.ts +44 -0
- package/test/images.test.ts +49 -49
- package/test/locals-header.test.ts +61 -0
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +92 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-services-context.test.ts +97 -97
- package/test/preload.test.ts +48 -48
- package/test/prerender.test.ts +353 -0
- package/test/rendering.test.ts +798 -798
- package/test/required-env.test.ts +32 -0
- package/test/rewrite-html.test.ts +90 -0
- package/test/scanning.test.ts +349 -314
- package/test/server.test.ts +640 -619
- package/test/services.test.ts +90 -90
- package/test/streaming.test.ts +378 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +805 -799
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +149 -146
- package/test/workspace-versions.test.ts +131 -131
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
package/src/routing/router.ts
CHANGED
|
@@ -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
|
+
}
|