@sigil-dev/grimoire 0.7.4 → 0.7.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 (50) hide show
  1. package/.grimoire/_routes.dom.js +8 -0
  2. package/.grimoire/_routes.hydrate.js +8 -0
  3. package/.grimoire/tsconfig.generated.json +11 -0
  4. package/.grimoire/types/ambient.d.ts +59 -0
  5. package/.grimoire/types/api/hello/$types.d.ts +50 -0
  6. package/.grimoire/types/api/items/$types.d.ts +50 -0
  7. package/.grimoire/types/echo/$types.d.ts +50 -0
  8. package/.grimoire/types/env-private.d.ts +5 -0
  9. package/.grimoire/types/env-public.d.ts +5 -0
  10. package/.grimoire/types/mixed/$types.d.ts +50 -0
  11. package/.grimoire/types/params/[docId]/$types.d.ts +52 -0
  12. package/.grimoire/types/reject/$types.d.ts +50 -0
  13. package/index.ts +34 -34
  14. package/package.json +8 -4
  15. package/preload.js +2 -0
  16. package/public/__grimoire__/hydrate.js +585 -0
  17. package/public/__grimoire__/index.js +490 -0
  18. package/src/client/head.ts +29 -0
  19. package/src/client/router.ts +224 -76
  20. package/src/env/index.ts +25 -0
  21. package/src/env/plugin.ts +13 -0
  22. package/src/env/private.ts +5 -0
  23. package/src/env/public.ts +7 -0
  24. package/src/env/typegen.ts +51 -0
  25. package/src/integrations/vite.ts +72 -72
  26. package/src/rendering/head.ts +22 -2
  27. package/src/rendering/hydrate.ts +81 -26
  28. package/src/rendering/index.ts +199 -186
  29. package/src/rendering/ssrPlugin.ts +53 -42
  30. package/src/routing/manifest-gen.ts +39 -26
  31. package/src/routing/router.ts +106 -98
  32. package/src/routing/scanner.ts +135 -129
  33. package/src/routing/transform-routes.ts +101 -96
  34. package/src/server/build.ts +147 -90
  35. package/src/server/coordinator.ts +306 -297
  36. package/src/server/hooks.ts +24 -3
  37. package/src/server/index.ts +148 -71
  38. package/src/server/worker.ts +59 -59
  39. package/src/typegen/index.ts +353 -340
  40. package/src/types.ts +269 -260
  41. package/test/context.test.ts +52 -52
  42. package/test/hydration.test.ts +119 -119
  43. package/test/middleware.test.ts +223 -221
  44. package/test/rendering.test.ts +425 -425
  45. package/test/routing.test.ts +83 -45
  46. package/test/scanning.test.ts +181 -169
  47. package/test/server.test.ts +229 -229
  48. package/test/streaming.test.ts +106 -106
  49. package/test/transform-routes.test.ts +84 -84
  50. package/test/typegen.test.ts +19 -1
@@ -1,297 +1,306 @@
1
- import { type Subprocess, spawn } from "bun";
2
- import { join } from "path";
3
- import { matchRoute } from "../routing/router";
4
- import { type RouteFile, type RouteTree, scanRoutes } from "../routing/scanner";
5
- import type {
6
- CoordinatorContext,
7
- GrimoireConfig,
8
- GrimoirePlugin,
9
- Route,
10
- WorkerDescriptor,
11
- WorkerMode,
12
- } from "../types";
13
- import { buildProject } from "./build";
14
- import {
15
- runHook,
16
- runRouteRequest,
17
- runSerializeLocals,
18
- runWorkerSpawn,
19
- } from "./plugins";
20
-
21
- async function waitForWorkers(
22
- workers: LiveWorker[],
23
- timeoutMs = 10_000,
24
- ): Promise<void> {
25
- const ready = new Set<number>();
26
- const start = Date.now();
27
-
28
- while (ready.size < workers.length) {
29
- if (Date.now() - start > timeoutMs) {
30
- const pending = workers
31
- .filter((w) => !ready.has(w.descriptor.globalIndex))
32
- .map(
33
- (w) =>
34
- w.descriptor.name ?? `${w.descriptor.mode}-${w.descriptor.index}`,
35
- )
36
- .join(", ");
37
- throw new Error(
38
- `Grimoire: workers timed out waiting to become ready: ${pending}`,
39
- );
40
- }
41
- for (const w of workers) {
42
- if (ready.has(w.descriptor.globalIndex)) continue;
43
- if (w.descriptor.pid != null) {
44
- ready.add(w.descriptor.globalIndex);
45
- }
46
- }
47
- await Bun.sleep(50);
48
- }
49
- }
50
-
51
- function roundRobin(workers: WorkerDescriptor[]): WorkerDescriptor {
52
- const idx = roundRobinCounter++ % workers.length;
53
- return workers[idx];
54
- }
55
- let roundRobinCounter = 0;
56
-
57
- function consistentHash(
58
- req: Request,
59
- workers: WorkerDescriptor[],
60
- ): WorkerDescriptor {
61
- const key = req.headers.get("cookie") ?? req.url;
62
- let hash = 0;
63
- for (let i = 0; i < key.length; i++) {
64
- hash = (hash * 31 + key.charCodeAt(i)) >>> 0;
65
- }
66
- return workers[hash % workers.length];
67
- }
68
-
69
- function defaultRoute(
70
- req: Request,
71
- workers: WorkerDescriptor[],
72
- tree: RouteTree,
73
- ): WorkerDescriptor {
74
- const isWs = req.headers.get("upgrade")?.toLowerCase() === "websocket";
75
-
76
- if (isWs) {
77
- const wsWorkers = workers.filter(
78
- (w) => w.mode === "ws" || w.mode === "full",
79
- );
80
- if (wsWorkers.length) return consistentHash(req, wsWorkers);
81
- }
82
-
83
- const url = new URL(req.url);
84
- const matched = matchRoute(tree, url);
85
-
86
- if (matched?.route.type === "server") {
87
- const apiWorkers = workers.filter(
88
- (w) => w.mode === "api" || w.mode === "full",
89
- );
90
- if (apiWorkers.length) return roundRobin(apiWorkers);
91
- }
92
-
93
- const frontendWorkers = workers.filter(
94
- (w) => w.mode === "frontend" || w.mode === "full",
95
- );
96
- if (frontendWorkers.length) return roundRobin(frontendWorkers);
97
-
98
- // fallback — should not happen if scale spec is valid
99
- return workers[0];
100
- }
101
-
102
- export interface ScaleSpec {
103
- api?: number;
104
- frontend?: number;
105
- ws?: number;
106
- [mode: string]: number | undefined;
107
- }
108
-
109
- export interface CoordinatorOptions {
110
- config: GrimoireConfig;
111
- plugins: GrimoirePlugin[];
112
- scale: ScaleSpec;
113
- noBuild: boolean;
114
- }
115
-
116
- interface LiveWorker {
117
- descriptor: WorkerDescriptor;
118
- process: Subprocess;
119
- port: number;
120
- }
121
-
122
- export async function startCoordinator(options: CoordinatorOptions) {
123
- const { config, plugins, scale, noBuild } = options;
124
- const { port = 3000, host = "localhost", routes = "src/routes" } = config;
125
- let stopping = false;
126
-
127
- // 1. build if needed
128
- if (!noBuild) {
129
- const { result } = await buildProject(config, plugins);
130
- if (!result.success) throw new Error("Grimoire: build failed");
131
- }
132
-
133
- // 2. generate ephemeral secret
134
- const secret = crypto.randomUUID() + crypto.randomUUID(); // 72 hex chars
135
-
136
- // 3. scan routes once — coordinator owns the route tree
137
- const routesDir = /* resolve */ routes;
138
- const tree = await scanRoutes(routesDir, process.cwd());
139
-
140
- // 4. derive route slices per mode
141
- const slices: Record<string, RouteFile[]> = {
142
- api: tree.routes.filter((r) => r.type === "server"),
143
- ws: tree.routes.filter((r) => r.type === "server"),
144
- frontend: tree.routes.filter(
145
- (r) => r.type === "page" || r.type === "simple",
146
- ),
147
- full: [...tree.routes],
148
- };
149
-
150
- // 5. spawn workers
151
- const liveWorkers: LiveWorker[] = [];
152
- let globalIndex = 0;
153
- let nextPort = port + 1;
154
-
155
- for (const [mode, count = 0] of Object.entries(scale)) {
156
- for (let i = 0; i < count; i++) {
157
- const workerPort = nextPort++;
158
- const descriptor: WorkerDescriptor = {
159
- mode: mode as WorkerMode,
160
- index: i + 1,
161
- globalIndex: globalIndex++,
162
- internalUrl: `http://127.0.0.1:${workerPort}`,
163
- routes: slices[mode] ?? slices.full,
164
- };
165
-
166
- // let plugins inject env and name
167
- const workerEnv = await runWorkerSpawn(plugins, descriptor);
168
- const workerEntry = join(import.meta.dir, "./worker.ts");
169
- if (workerEnv.name) descriptor.name = workerEnv.name;
170
-
171
- const proc = spawn(["bun", "run", workerEntry], {
172
- env: {
173
- ...process.env,
174
- ...workerEnv.env,
175
- GRIMOIRE_MODE: mode,
176
- GRIMOIRE_WORKER_INDEX: String(i),
177
- GRIMOIRE_WORKER_PORT: String(workerPort),
178
- GRIMOIRE_INTERNAL_SECRET: secret,
179
- GRIMOIRE_WORKER_NAME: descriptor.name ?? `${mode}-${i}`,
180
- },
181
- ipc(message) {
182
- // worker sends { ready: true } when Bun.serve is up
183
- if (message?.ready) {
184
- descriptor.pid = proc.pid;
185
- runHook(plugins, "onWorkerReady", descriptor);
186
- }
187
- },
188
- onExit(proc, exitCode) {
189
- const intentional = stopping;
190
- runHook(
191
- plugins,
192
- "onWorkerDeath",
193
- descriptor,
194
- intentional ? "intentional" : "crash",
195
- );
196
- if (!intentional) {
197
- console.error(
198
- `[grimoire] worker ${descriptor.name ?? descriptor.mode + "-" + descriptor.index} crashed (exit ${exitCode}), respawning...`,
199
- );
200
- // respawn: same descriptor, same port
201
- // (simplified — plugin-scale can override this behavior)
202
- }
203
- },
204
- });
205
-
206
- liveWorkers.push({ descriptor, process: proc, port: workerPort });
207
- }
208
- }
209
-
210
- // 6. wait for all workers ready (with timeout)
211
- // workers send IPC { ready: true } — coordinator waits up to 10s
212
- await waitForWorkers(liveWorkers);
213
-
214
- // 7. fire onCoordinatorStart
215
- const ctx: CoordinatorContext = {
216
- workers: liveWorkers.map((w) => w.descriptor),
217
- port,
218
- secret,
219
- routes: tree,
220
- };
221
- await runHook(plugins, "onCoordinatorStart", ctx);
222
-
223
- // 8. start coordinator Bun.serve
224
-
225
- const server = Bun.serve({
226
- port,
227
- hostname: host,
228
- fetch: async (req) => {
229
- // run hooks.server.ts to populate locals
230
- const locals: App.Locals = {};
231
- // ... same handle() chain as createServer but resolve() just
232
- // forwards instead of rendering
233
-
234
- // pick worker
235
- const descriptors = liveWorkers.map((w) => w.descriptor);
236
- const chosen =
237
- (await runRouteRequest(plugins, req, descriptors, tree)) ??
238
- defaultRoute(req, descriptors, tree);
239
-
240
- // serialize locals
241
- const serialized =
242
- (await runSerializeLocals(plugins, locals)) ??
243
- defaultSerialize(locals, secret);
244
-
245
- // forward
246
- return fetch(chosen.internalUrl + new URL(req.url).pathname, {
247
- method: req.method,
248
- headers: {
249
- //@ts-expect-error shut Up
250
- ...Object.fromEntries(req.headers),
251
- "X-Grimoire-Locals": serialized,
252
- "X-Grimoire-Internal": secret,
253
- },
254
- body: req.body,
255
- });
256
- },
257
- });
258
-
259
- // 9. shutdown
260
- const handleShutdown = async () => {
261
- if (stopping) return;
262
- stopping = true;
263
- await runHook(plugins, "onStop", "shutdown");
264
- for (const w of liveWorkers) w.process.kill();
265
- server.stop();
266
- process.exit(0);
267
- };
268
- process.on("SIGINT", handleShutdown);
269
- process.on("SIGTERM", handleShutdown);
270
-
271
- return {
272
- server,
273
- workers: liveWorkers.map((w) => ({
274
- name: w.descriptor.name,
275
- mode: w.descriptor.mode,
276
- port: w.port,
277
- index: w.descriptor.index,
278
- })),
279
- };
280
- }
281
-
282
- function defaultSerialize(locals: App.Locals, secret: string): string {
283
- const payload = JSON.stringify(locals);
284
- const sig = new Bun.CryptoHasher("sha256")
285
- .update(secret + payload)
286
- .digest("hex");
287
- return Buffer.from(JSON.stringify({ payload, sig })).toString("base64");
288
- }
289
-
290
- function defaultDeserialize(raw: string, secret: string): App.Locals {
291
- const { payload, sig } = JSON.parse(Buffer.from(raw, "base64").toString());
292
- const expected = new Bun.CryptoHasher("sha256")
293
- .update(secret + payload)
294
- .digest("hex");
295
- if (sig !== expected) throw new Error("Grimoire: locals signature invalid");
296
- return JSON.parse(payload);
297
- }
1
+ import { type Subprocess, spawn } from "bun";
2
+ import { join } from "path";
3
+ import { matchRoute } from "../routing/router";
4
+ import { type RouteFile, type RouteTree, scanRoutes } from "../routing/scanner";
5
+ import type {
6
+ CoordinatorContext,
7
+ GrimoireConfig,
8
+ GrimoirePlugin,
9
+ Route,
10
+ WorkerDescriptor,
11
+ WorkerMode,
12
+ } from "../types";
13
+ import { buildProject } from "./build";
14
+ import {
15
+ runHook,
16
+ runRouteRequest,
17
+ runSerializeLocals,
18
+ runWorkerSpawn,
19
+ } from "./plugins";
20
+
21
+ async function waitForWorkers(
22
+ workers: LiveWorker[],
23
+ timeoutMs = 10_000,
24
+ ): Promise<void> {
25
+ const ready = new Set<number>();
26
+ const start = Date.now();
27
+
28
+ while (ready.size < workers.length) {
29
+ if (Date.now() - start > timeoutMs) {
30
+ const pending = workers
31
+ .filter((w) => !ready.has(w.descriptor.globalIndex))
32
+ .map(
33
+ (w) =>
34
+ w.descriptor.name ?? `${w.descriptor.mode}-${w.descriptor.index}`,
35
+ )
36
+ .join(", ");
37
+ throw new Error(
38
+ `Grimoire: workers timed out waiting to become ready: ${pending}`,
39
+ );
40
+ }
41
+ for (const w of workers) {
42
+ if (ready.has(w.descriptor.globalIndex)) continue;
43
+ if (w.descriptor.pid != null) {
44
+ ready.add(w.descriptor.globalIndex);
45
+ }
46
+ }
47
+ await Bun.sleep(50);
48
+ }
49
+ }
50
+
51
+ function roundRobin(workers: WorkerDescriptor[]): WorkerDescriptor {
52
+ const idx = roundRobinCounter++ % workers.length;
53
+ return workers[idx];
54
+ }
55
+ let roundRobinCounter = 0;
56
+
57
+ function consistentHash(
58
+ req: Request,
59
+ workers: WorkerDescriptor[],
60
+ ): WorkerDescriptor {
61
+ const key = req.headers.get("cookie") ?? req.url;
62
+ let hash = 0;
63
+ for (let i = 0; i < key.length; i++) {
64
+ hash = (hash * 31 + key.charCodeAt(i)) >>> 0;
65
+ }
66
+ return workers[hash % workers.length];
67
+ }
68
+
69
+ function defaultRoute(
70
+ req: Request,
71
+ workers: WorkerDescriptor[],
72
+ tree: RouteTree,
73
+ ): WorkerDescriptor {
74
+ const isWs = req.headers.get("upgrade")?.toLowerCase() === "websocket";
75
+
76
+ if (isWs) {
77
+ const wsWorkers = workers.filter(
78
+ (w) => w.mode === "ws" || w.mode === "full",
79
+ );
80
+ if (wsWorkers.length) return consistentHash(req, wsWorkers);
81
+ }
82
+
83
+ const url = new URL(req.url);
84
+ const matched = matchRoute(tree, url);
85
+
86
+ if (matched?.route.type === "server") {
87
+ const apiWorkers = workers.filter(
88
+ (w) => w.mode === "api" || w.mode === "full",
89
+ );
90
+ if (apiWorkers.length) return roundRobin(apiWorkers);
91
+ }
92
+
93
+ const frontendWorkers = workers.filter(
94
+ (w) => w.mode === "frontend" || w.mode === "full",
95
+ );
96
+ if (frontendWorkers.length) return roundRobin(frontendWorkers);
97
+
98
+ // fallback — should not happen if scale spec is valid
99
+ return workers[0];
100
+ }
101
+
102
+ export interface ScaleSpec {
103
+ api?: number;
104
+ frontend?: number;
105
+ ws?: number;
106
+ [mode: string]: number | undefined;
107
+ }
108
+
109
+ export interface CoordinatorOptions {
110
+ config: GrimoireConfig;
111
+ plugins: GrimoirePlugin[];
112
+ scale: ScaleSpec;
113
+ noBuild: boolean;
114
+ }
115
+
116
+ interface LiveWorker {
117
+ descriptor: WorkerDescriptor;
118
+ process: Subprocess;
119
+ port: number;
120
+ }
121
+
122
+ export async function startCoordinator(options: CoordinatorOptions) {
123
+ const { config, plugins, scale, noBuild } = options;
124
+ const { port = 3000, host = "localhost", routes = "src/routes" } = config;
125
+ let stopping = false;
126
+
127
+ // 1. build if needed
128
+ if (!noBuild) {
129
+ const { result } = await buildProject(config, plugins);
130
+ if (!result.success) throw new Error("Grimoire: build failed");
131
+ }
132
+
133
+ // 2. generate ephemeral secret
134
+ const secret = crypto.randomUUID() + crypto.randomUUID(); // 72 hex chars
135
+
136
+ // 3. scan routes once — coordinator owns the route tree
137
+ const routesDir = /* resolve */ routes;
138
+ const tree = await scanRoutes(routesDir, process.cwd());
139
+
140
+ // 4. derive route slices per mode
141
+ const slices: Record<string, RouteFile[]> = {
142
+ api: tree.routes.filter((r) => r.type === "server"),
143
+ ws: tree.routes.filter((r) => r.type === "server"),
144
+ frontend: tree.routes.filter(
145
+ (r) => r.type === "page" || r.type === "simple",
146
+ ),
147
+ full: [...tree.routes],
148
+ };
149
+
150
+ // 5. spawn workers
151
+ const liveWorkers: LiveWorker[] = [];
152
+ let globalIndex = 0;
153
+ let nextPort = port + 1;
154
+
155
+ for (const [mode, count = 0] of Object.entries(scale)) {
156
+ for (let i = 0; i < count; i++) {
157
+ const workerPort = nextPort++;
158
+ const descriptor: WorkerDescriptor = {
159
+ mode: mode as WorkerMode,
160
+ index: i + 1,
161
+ globalIndex: globalIndex++,
162
+ internalUrl: `http://127.0.0.1:${workerPort}`,
163
+ routes: slices[mode] ?? slices.full,
164
+ };
165
+
166
+ // let plugins inject env and name
167
+ const workerEnv = await runWorkerSpawn(plugins, descriptor);
168
+ const workerEntry = join(import.meta.dir, "./worker.ts");
169
+ if (workerEnv.name) descriptor.name = workerEnv.name;
170
+
171
+ // Inject env vars prefixed with PUBLIC_ so they're available to client bundles
172
+ const publicEnvVars: Record<string, string> = {};
173
+ for (const [key, value] of Object.entries(process.env)) {
174
+ if (key.startsWith("PUBLIC_") && value !== undefined) {
175
+ publicEnvVars[key] = value;
176
+ }
177
+ }
178
+
179
+ const proc = spawn(["bun", "run", workerEntry], {
180
+ env: {
181
+ ...process.env,
182
+ ...workerEnv.env,
183
+ ...publicEnvVars,
184
+ GRIMOIRE_MODE: mode,
185
+ GRIMOIRE_WORKER_INDEX: String(i),
186
+ GRIMOIRE_WORKER_PORT: String(workerPort),
187
+ GRIMOIRE_INTERNAL_SECRET: secret,
188
+ GRIMOIRE_WORKER_NAME: descriptor.name ?? `${mode}-${i}`,
189
+ },
190
+ ipc(message) {
191
+ // worker sends { ready: true } when Bun.serve is up
192
+ if (message?.ready) {
193
+ descriptor.pid = proc.pid;
194
+ runHook(plugins, "onWorkerReady", descriptor);
195
+ }
196
+ },
197
+ onExit(proc, exitCode) {
198
+ const intentional = stopping;
199
+ runHook(
200
+ plugins,
201
+ "onWorkerDeath",
202
+ descriptor,
203
+ intentional ? "intentional" : "crash",
204
+ );
205
+ if (!intentional) {
206
+ console.error(
207
+ `[grimoire] worker ${descriptor.name ?? descriptor.mode + "-" + descriptor.index} crashed (exit ${exitCode}), respawning...`,
208
+ );
209
+ // respawn: same descriptor, same port
210
+ // (simplified plugin-scale can override this behavior)
211
+ }
212
+ },
213
+ });
214
+
215
+ liveWorkers.push({ descriptor, process: proc, port: workerPort });
216
+ }
217
+ }
218
+
219
+ // 6. wait for all workers ready (with timeout)
220
+ // workers send IPC { ready: true } — coordinator waits up to 10s
221
+ await waitForWorkers(liveWorkers);
222
+
223
+ // 7. fire onCoordinatorStart
224
+ const ctx: CoordinatorContext = {
225
+ workers: liveWorkers.map((w) => w.descriptor),
226
+ port,
227
+ secret,
228
+ routes: tree,
229
+ };
230
+ await runHook(plugins, "onCoordinatorStart", ctx);
231
+
232
+ // 8. start coordinator Bun.serve
233
+
234
+ const server = Bun.serve({
235
+ port,
236
+ hostname: host,
237
+ fetch: async (req) => {
238
+ // run hooks.server.ts to populate locals
239
+ const locals: App.Locals = {};
240
+ // ... same handle() chain as createServer but resolve() just
241
+ // forwards instead of rendering
242
+
243
+ // pick worker
244
+ const descriptors = liveWorkers.map((w) => w.descriptor);
245
+ const chosen =
246
+ (await runRouteRequest(plugins, req, descriptors, tree)) ??
247
+ defaultRoute(req, descriptors, tree);
248
+
249
+ // serialize locals
250
+ const serialized =
251
+ (await runSerializeLocals(plugins, locals)) ??
252
+ defaultSerialize(locals, secret);
253
+
254
+ // forward
255
+ return fetch(chosen.internalUrl + new URL(req.url).pathname, {
256
+ method: req.method,
257
+ headers: {
258
+ //@ts-expect-error shut Up
259
+ ...Object.fromEntries(req.headers),
260
+ "X-Grimoire-Locals": serialized,
261
+ "X-Grimoire-Internal": secret,
262
+ },
263
+ body: req.body,
264
+ });
265
+ },
266
+ });
267
+
268
+ // 9. shutdown
269
+ const handleShutdown = async () => {
270
+ if (stopping) return;
271
+ stopping = true;
272
+ await runHook(plugins, "onStop", "shutdown");
273
+ for (const w of liveWorkers) w.process.kill();
274
+ server.stop();
275
+ process.exit(0);
276
+ };
277
+ process.on("SIGINT", handleShutdown);
278
+ process.on("SIGTERM", handleShutdown);
279
+
280
+ return {
281
+ server,
282
+ workers: liveWorkers.map((w) => ({
283
+ name: w.descriptor.name,
284
+ mode: w.descriptor.mode,
285
+ port: w.port,
286
+ index: w.descriptor.index,
287
+ })),
288
+ };
289
+ }
290
+
291
+ function defaultSerialize(locals: App.Locals, secret: string): string {
292
+ const payload = JSON.stringify(locals);
293
+ const sig = new Bun.CryptoHasher("sha256")
294
+ .update(secret + payload)
295
+ .digest("hex");
296
+ return Buffer.from(JSON.stringify({ payload, sig })).toString("base64");
297
+ }
298
+
299
+ function defaultDeserialize(raw: string, secret: string): App.Locals {
300
+ const { payload, sig } = JSON.parse(Buffer.from(raw, "base64").toString());
301
+ const expected = new Bun.CryptoHasher("sha256")
302
+ .update(secret + payload)
303
+ .digest("hex");
304
+ if (sig !== expected) throw new Error("Grimoire: locals signature invalid");
305
+ return JSON.parse(payload);
306
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * SvelteKit-style server hooks.
3
3
  *
4
- * Create a hooks.index.ts in your project root:
4
+ * Create a hooks.server.ts in your project root:
5
5
  *
6
6
  * import type { Handle } from "@sigil-dev/grimoire/hooks";
7
7
  *
@@ -12,6 +12,10 @@
12
12
  * return response;
13
13
  * };
14
14
  *
15
+ * export const handleError: HandleError = ({ error, status, message }) => {
16
+ * console.error(`[${status}] ${message}`, error);
17
+ * };
18
+ *
15
19
  * // optional: runs once at server start
16
20
  * export const init = () => {
17
21
  * console.log("Server started");
@@ -32,8 +36,10 @@ export interface RequestEvent {
32
36
  request: Request;
33
37
  url: URL;
34
38
  params: Record<string, string>;
35
- locals: Record<string, any>;
39
+ locals: App.Locals;
36
40
  cookies: Cookies;
41
+ route: { id: string };
42
+ fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
37
43
  setHeaders: (headers: Record<string, string>) => void;
38
44
  }
39
45
 
@@ -60,6 +66,21 @@ export type Handle = (input: {
60
66
  resolve: ResolveFunction;
61
67
  }) => MaybePromise<Response>;
62
68
 
69
+ export interface HandleErrorInput {
70
+ error: unknown;
71
+ event: RequestEvent;
72
+ status: number;
73
+ message: string;
74
+ }
75
+ export type HandleError = (input: HandleErrorInput) => void | Promise<void>;
76
+
77
+ export interface HandleFetchInput {
78
+ request: Request;
79
+ fetch: typeof globalThis.fetch;
80
+ event: RequestEvent;
81
+ }
82
+ export type HandleFetch = (input: HandleFetchInput) => MaybePromise<Response>;
83
+
63
84
  export type InitFunction = () => void | Promise<void>;
64
85
 
65
86
  /**
@@ -73,7 +94,7 @@ export function sequence(...handlers: Handle[]): Handle {
73
94
  const next: ResolveFunction = async (evt) => {
74
95
  if (i < handlers.length) {
75
96
  const handler = handlers[i++];
76
- return handler({ event: evt, resolve: next });
97
+ return handler!({ event: evt, resolve: next });
77
98
  }
78
99
  return resolve(evt);
79
100
  };