@sigil-dev/grimoire 0.9.6 → 0.9.10
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/.grimoire/_routes.ts +4 -0
- package/README.md +174 -174
- package/package.json +3 -3
- package/public/__grimoire__/client.js +55 -0
- package/public/__grimoire__/hydrate.js +63 -0
- package/src/client/csrf.ts +47 -0
- package/src/client/enhance.ts +5 -0
- package/src/client/router.ts +622 -619
- package/src/rendering/hydrate.ts +159 -159
- package/src/rendering/index.ts +599 -599
- package/src/routing/scanner.ts +1 -1
- package/src/server/coordinator.ts +376 -364
- package/src/server/index.ts +723 -718
- package/test/client-csrf.test.ts +61 -0
- package/test/client-router.test.ts +366 -366
- package/test/streaming.test.ts +281 -281
|
@@ -1,364 +1,376 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
2
|
-
import { type Subprocess, spawn } from "bun";
|
|
3
|
-
import { log } from "../logger/instance";
|
|
4
|
-
import { matchRoute } from "../routing/router";
|
|
5
|
-
import { type RouteFile, type RouteTree, scanRoutes } from "../routing/scanner";
|
|
6
|
-
import type {
|
|
7
|
-
CoordinatorContext,
|
|
8
|
-
GrimoireConfig,
|
|
9
|
-
GrimoirePlugin,
|
|
10
|
-
PluginContext,
|
|
11
|
-
WorkerDescriptor,
|
|
12
|
-
WorkerMode,
|
|
13
|
-
} from "../types";
|
|
14
|
-
import { buildProject } from "./build";
|
|
15
|
-
import {
|
|
16
|
-
runHook,
|
|
17
|
-
runRouteRequest,
|
|
18
|
-
runSerializeLocals,
|
|
19
|
-
runWorkerSpawn,
|
|
20
|
-
} from "./plugins";
|
|
21
|
-
|
|
22
|
-
const logger = log.scope("loom");
|
|
23
|
-
|
|
24
|
-
async function waitForWorkers(
|
|
25
|
-
workers: LiveWorker[],
|
|
26
|
-
timeoutMs = 10_000,
|
|
27
|
-
): Promise<void> {
|
|
28
|
-
const ready = new Set<number>();
|
|
29
|
-
const start = Date.now();
|
|
30
|
-
while (ready.size < workers.length) {
|
|
31
|
-
if (Date.now() - start > timeoutMs) {
|
|
32
|
-
const pending = workers
|
|
33
|
-
.filter((w) => !ready.has(w.descriptor.globalIndex))
|
|
34
|
-
.map(
|
|
35
|
-
(w) =>
|
|
36
|
-
w.descriptor.name ?? `${w.descriptor.mode}-${w.descriptor.index}`,
|
|
37
|
-
)
|
|
38
|
-
.join(", ");
|
|
39
|
-
throw new Error(`Grimoire: workers timed out: ${pending}`);
|
|
40
|
-
}
|
|
41
|
-
for (const w of workers) {
|
|
42
|
-
if (!ready.has(w.descriptor.globalIndex) && w.descriptor.pid != null) {
|
|
43
|
-
ready.add(w.descriptor.globalIndex);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
await Bun.sleep(50);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function roundRobin(workers: WorkerDescriptor[]): WorkerDescriptor {
|
|
51
|
-
const idx = roundRobinCounter++ % workers.length;
|
|
52
|
-
return workers[idx];
|
|
53
|
-
}
|
|
54
|
-
let roundRobinCounter = 0;
|
|
55
|
-
|
|
56
|
-
function consistentHash(
|
|
57
|
-
req: Request,
|
|
58
|
-
workers: WorkerDescriptor[],
|
|
59
|
-
): WorkerDescriptor {
|
|
60
|
-
const key = req.headers.get("cookie") ?? req.url;
|
|
61
|
-
let hash = 0;
|
|
62
|
-
for (let i = 0; i < key.length; i++)
|
|
63
|
-
hash = (hash * 31 + key.charCodeAt(i)) >>> 0;
|
|
64
|
-
return workers[hash % workers.length];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function defaultRoute(
|
|
68
|
-
req: Request,
|
|
69
|
-
workers: WorkerDescriptor[],
|
|
70
|
-
tree: RouteTree,
|
|
71
|
-
): WorkerDescriptor {
|
|
72
|
-
const isWs = req.headers.get("upgrade")?.toLowerCase() === "websocket";
|
|
73
|
-
if (isWs) {
|
|
74
|
-
const ws = workers.filter((w) => w.mode === "ws" || w.mode === "full");
|
|
75
|
-
if (ws.length) return consistentHash(req, ws);
|
|
76
|
-
}
|
|
77
|
-
const url = new URL(req.url);
|
|
78
|
-
const matched = matchRoute(tree, url);
|
|
79
|
-
if (matched?.route.type === "server") {
|
|
80
|
-
const api = workers.filter((w) => w.mode === "api" || w.mode === "full");
|
|
81
|
-
if (api.length) return roundRobin(api);
|
|
82
|
-
}
|
|
83
|
-
const frontend = workers.filter(
|
|
84
|
-
(w) => w.mode === "frontend" || w.mode === "full",
|
|
85
|
-
);
|
|
86
|
-
if (frontend.length) return roundRobin(frontend);
|
|
87
|
-
return workers[0];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export interface ScaleSpec {
|
|
91
|
-
api?: number;
|
|
92
|
-
frontend?: number;
|
|
93
|
-
ws?: number;
|
|
94
|
-
[mode: string]: number | undefined;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export interface CoordinatorOptions {
|
|
98
|
-
config: GrimoireConfig;
|
|
99
|
-
plugins: GrimoirePlugin[];
|
|
100
|
-
scale: ScaleSpec;
|
|
101
|
-
noBuild: boolean;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
interface LiveWorker {
|
|
105
|
-
descriptor: WorkerDescriptor;
|
|
106
|
-
process: Subprocess;
|
|
107
|
-
port: number;
|
|
108
|
-
env: Record<string, string>;
|
|
109
|
-
intentional: boolean; // ← add
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export async function startCoordinator(options: CoordinatorOptions) {
|
|
113
|
-
const { config, plugins, scale, noBuild } = options;
|
|
114
|
-
const { port = 3000, host = "localhost", routes = "src/routes" } = config;
|
|
115
|
-
let stopping = false;
|
|
116
|
-
|
|
117
|
-
const workerEntry = join(import.meta.dir, "./worker.ts");
|
|
118
|
-
|
|
119
|
-
// ── inner helpers (close over plugins/stopping/workerEntry) ──
|
|
120
|
-
|
|
121
|
-
function spawnWorker(lw: LiveWorker): void {
|
|
122
|
-
const proc = spawn(["bun", "run", workerEntry], {
|
|
123
|
-
env: lw.env,
|
|
124
|
-
ipc(message) {
|
|
125
|
-
if (message?.ready) {
|
|
126
|
-
lw.descriptor.pid = proc.pid;
|
|
127
|
-
runHook(plugins, "onWorkerReady", lw.descriptor);
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
onExit(_, exitCode) {
|
|
131
|
-
if (lw.intentional) {
|
|
132
|
-
lw.intentional = false;
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
runHook(
|
|
136
|
-
plugins,
|
|
137
|
-
"onWorkerDeath",
|
|
138
|
-
lw.descriptor,
|
|
139
|
-
stopping ? "intentional" : "crash",
|
|
140
|
-
);
|
|
141
|
-
if (!stopping) {
|
|
142
|
-
logger.error(
|
|
143
|
-
`worker ${lw.descriptor.name ?? `${lw.descriptor.mode}-${lw.descriptor.index}`} crashed (exit ${exitCode}), respawning...`,
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
lw.process = proc;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async function respawnWorker(lw: LiveWorker): Promise<void> {
|
|
152
|
-
lw.intentional = true;
|
|
153
|
-
lw.process.kill();
|
|
154
|
-
lw.descriptor.pid = undefined;
|
|
155
|
-
spawnWorker(lw);
|
|
156
|
-
await waitForWorkers([lw]);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// 0. plugin resolution
|
|
160
|
-
const pluginCtx: PluginContext = {
|
|
161
|
-
mode: config.dev ? "development" : "production",
|
|
162
|
-
plugins: new Map(plugins.map((p) => [p.name, p])),
|
|
163
|
-
logger: log.scope("plugins"),
|
|
164
|
-
};
|
|
165
|
-
await runHook(plugins, "onPluginsResolved", pluginCtx);
|
|
166
|
-
|
|
167
|
-
// 1. build
|
|
168
|
-
if (!noBuild) {
|
|
169
|
-
const { result } = await buildProject(config, plugins);
|
|
170
|
-
if (!result.success) throw new Error("Grimoire: build failed");
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// 2. ephemeral secret
|
|
174
|
-
const secret = crypto.randomUUID() + crypto.randomUUID();
|
|
175
|
-
|
|
176
|
-
// 3. scan routes
|
|
177
|
-
const tree = await scanRoutes(routes, process.cwd());
|
|
178
|
-
|
|
179
|
-
// 4. route slices
|
|
180
|
-
const slices: Record<string, RouteFile[]> = {
|
|
181
|
-
api: tree.routes.filter((r) => r.type === "server"),
|
|
182
|
-
ws: tree.routes.filter((r) => r.type === "server"),
|
|
183
|
-
frontend: tree.routes.filter(
|
|
184
|
-
(r) => r.type === "page" || r.type === "simple",
|
|
185
|
-
),
|
|
186
|
-
full: [...tree.routes],
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
// 5. spawn workers
|
|
190
|
-
const liveWorkers: LiveWorker[] = [];
|
|
191
|
-
let globalIndex = 0;
|
|
192
|
-
let nextPort = port + 1;
|
|
193
|
-
|
|
194
|
-
for (const [mode, count = 0] of Object.entries(scale)) {
|
|
195
|
-
for (let i = 0; i < count; i++) {
|
|
196
|
-
const workerPort = nextPort++;
|
|
197
|
-
const descriptor: WorkerDescriptor = {
|
|
198
|
-
mode: mode as WorkerMode,
|
|
199
|
-
index: i + 1,
|
|
200
|
-
globalIndex: globalIndex++,
|
|
201
|
-
internalUrl: `http://127.0.0.1:${workerPort}`,
|
|
202
|
-
routes: slices[mode] ?? slices.full,
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const publicEnvVars: Record<string, string> = {};
|
|
206
|
-
for (const [k, v] of Object.entries(process.env)) {
|
|
207
|
-
if (k.startsWith("PUBLIC_") && v !== undefined) publicEnvVars[k] = v;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const workerEnv = await runWorkerSpawn(plugins, descriptor);
|
|
211
|
-
if (workerEnv.name) descriptor.name = workerEnv.name;
|
|
212
|
-
|
|
213
|
-
const env = {
|
|
214
|
-
...process.env,
|
|
215
|
-
...workerEnv.env,
|
|
216
|
-
...publicEnvVars,
|
|
217
|
-
GRIMOIRE_MODE: mode,
|
|
218
|
-
GRIMOIRE_WORKER_INDEX: String(i),
|
|
219
|
-
GRIMOIRE_WORKER_PORT: String(workerPort),
|
|
220
|
-
GRIMOIRE_INTERNAL_SECRET: secret,
|
|
221
|
-
GRIMOIRE_WORKER_NAME: descriptor.name ?? `${mode}-${i}`,
|
|
222
|
-
} as Record<string, string>;
|
|
223
|
-
|
|
224
|
-
const lw: LiveWorker = {
|
|
225
|
-
descriptor,
|
|
226
|
-
process: null as any,
|
|
227
|
-
port: workerPort,
|
|
228
|
-
env,
|
|
229
|
-
intentional: false,
|
|
230
|
-
};
|
|
231
|
-
spawnWorker(lw);
|
|
232
|
-
liveWorkers.push(lw);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// 6. wait for ready
|
|
237
|
-
await waitForWorkers(liveWorkers);
|
|
238
|
-
|
|
239
|
-
// 7. onCoordinatorStart
|
|
240
|
-
const ctx: CoordinatorContext = {
|
|
241
|
-
workers: liveWorkers.map((w) => w.descriptor),
|
|
242
|
-
port,
|
|
243
|
-
secret,
|
|
244
|
-
routes: tree,
|
|
245
|
-
};
|
|
246
|
-
await runHook(plugins, "onCoordinatorStart", ctx);
|
|
247
|
-
|
|
248
|
-
// 8. coordinator server
|
|
249
|
-
const server = Bun.serve({
|
|
250
|
-
port,
|
|
251
|
-
hostname: host,
|
|
252
|
-
fetch: async (req) => {
|
|
253
|
-
const url = new URL(req.url);
|
|
254
|
-
const isWsUpgrade =
|
|
255
|
-
req.headers.get("upgrade")?.toLowerCase() === "websocket";
|
|
256
|
-
|
|
257
|
-
if (isWsUpgrade) {
|
|
258
|
-
const descriptors = liveWorkers.map((w) => w.descriptor);
|
|
259
|
-
const chosen =
|
|
260
|
-
(await runRouteRequest(plugins, req, descriptors, tree)) ??
|
|
261
|
-
defaultRoute(req, descriptors, tree);
|
|
262
|
-
|
|
263
|
-
//@ts-expect-error
|
|
264
|
-
if (
|
|
265
|
-
server.upgrade(req, {
|
|
266
|
-
data: {
|
|
267
|
-
workerUrl: chosen.internalUrl,
|
|
268
|
-
pathname: url.pathname + url.search,
|
|
269
|
-
},
|
|
270
|
-
})
|
|
271
|
-
) {
|
|
272
|
-
return undefined as unknown as Response;
|
|
273
|
-
}
|
|
274
|
-
return new Response("Upgrade Required", { status: 426 });
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const locals: App.Locals = {};
|
|
278
|
-
const descriptors = liveWorkers.map((w) => w.descriptor);
|
|
279
|
-
const chosen =
|
|
280
|
-
(await runRouteRequest(plugins, req, descriptors, tree)) ??
|
|
281
|
-
defaultRoute(req, descriptors, tree);
|
|
282
|
-
const serialized =
|
|
283
|
-
(await runSerializeLocals(plugins, locals)) ??
|
|
284
|
-
defaultSerialize(locals, secret);
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
ws.data
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
.
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { type Subprocess, spawn } from "bun";
|
|
3
|
+
import { log } from "../logger/instance";
|
|
4
|
+
import { matchRoute } from "../routing/router";
|
|
5
|
+
import { type RouteFile, type RouteTree, scanRoutes } from "../routing/scanner";
|
|
6
|
+
import type {
|
|
7
|
+
CoordinatorContext,
|
|
8
|
+
GrimoireConfig,
|
|
9
|
+
GrimoirePlugin,
|
|
10
|
+
PluginContext,
|
|
11
|
+
WorkerDescriptor,
|
|
12
|
+
WorkerMode,
|
|
13
|
+
} from "../types";
|
|
14
|
+
import { buildProject } from "./build";
|
|
15
|
+
import {
|
|
16
|
+
runHook,
|
|
17
|
+
runRouteRequest,
|
|
18
|
+
runSerializeLocals,
|
|
19
|
+
runWorkerSpawn,
|
|
20
|
+
} from "./plugins";
|
|
21
|
+
|
|
22
|
+
const logger = log.scope("loom");
|
|
23
|
+
|
|
24
|
+
async function waitForWorkers(
|
|
25
|
+
workers: LiveWorker[],
|
|
26
|
+
timeoutMs = 10_000,
|
|
27
|
+
): Promise<void> {
|
|
28
|
+
const ready = new Set<number>();
|
|
29
|
+
const start = Date.now();
|
|
30
|
+
while (ready.size < workers.length) {
|
|
31
|
+
if (Date.now() - start > timeoutMs) {
|
|
32
|
+
const pending = workers
|
|
33
|
+
.filter((w) => !ready.has(w.descriptor.globalIndex))
|
|
34
|
+
.map(
|
|
35
|
+
(w) =>
|
|
36
|
+
w.descriptor.name ?? `${w.descriptor.mode}-${w.descriptor.index}`,
|
|
37
|
+
)
|
|
38
|
+
.join(", ");
|
|
39
|
+
throw new Error(`Grimoire: workers timed out: ${pending}`);
|
|
40
|
+
}
|
|
41
|
+
for (const w of workers) {
|
|
42
|
+
if (!ready.has(w.descriptor.globalIndex) && w.descriptor.pid != null) {
|
|
43
|
+
ready.add(w.descriptor.globalIndex);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
await Bun.sleep(50);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function roundRobin(workers: WorkerDescriptor[]): WorkerDescriptor {
|
|
51
|
+
const idx = roundRobinCounter++ % workers.length;
|
|
52
|
+
return workers[idx];
|
|
53
|
+
}
|
|
54
|
+
let roundRobinCounter = 0;
|
|
55
|
+
|
|
56
|
+
function consistentHash(
|
|
57
|
+
req: Request,
|
|
58
|
+
workers: WorkerDescriptor[],
|
|
59
|
+
): WorkerDescriptor {
|
|
60
|
+
const key = req.headers.get("cookie") ?? req.url;
|
|
61
|
+
let hash = 0;
|
|
62
|
+
for (let i = 0; i < key.length; i++)
|
|
63
|
+
hash = (hash * 31 + key.charCodeAt(i)) >>> 0;
|
|
64
|
+
return workers[hash % workers.length];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function defaultRoute(
|
|
68
|
+
req: Request,
|
|
69
|
+
workers: WorkerDescriptor[],
|
|
70
|
+
tree: RouteTree,
|
|
71
|
+
): WorkerDescriptor {
|
|
72
|
+
const isWs = req.headers.get("upgrade")?.toLowerCase() === "websocket";
|
|
73
|
+
if (isWs) {
|
|
74
|
+
const ws = workers.filter((w) => w.mode === "ws" || w.mode === "full");
|
|
75
|
+
if (ws.length) return consistentHash(req, ws);
|
|
76
|
+
}
|
|
77
|
+
const url = new URL(req.url);
|
|
78
|
+
const matched = matchRoute(tree, url);
|
|
79
|
+
if (matched?.route.type === "server") {
|
|
80
|
+
const api = workers.filter((w) => w.mode === "api" || w.mode === "full");
|
|
81
|
+
if (api.length) return roundRobin(api);
|
|
82
|
+
}
|
|
83
|
+
const frontend = workers.filter(
|
|
84
|
+
(w) => w.mode === "frontend" || w.mode === "full",
|
|
85
|
+
);
|
|
86
|
+
if (frontend.length) return roundRobin(frontend);
|
|
87
|
+
return workers[0];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface ScaleSpec {
|
|
91
|
+
api?: number;
|
|
92
|
+
frontend?: number;
|
|
93
|
+
ws?: number;
|
|
94
|
+
[mode: string]: number | undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface CoordinatorOptions {
|
|
98
|
+
config: GrimoireConfig;
|
|
99
|
+
plugins: GrimoirePlugin[];
|
|
100
|
+
scale: ScaleSpec;
|
|
101
|
+
noBuild: boolean;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface LiveWorker {
|
|
105
|
+
descriptor: WorkerDescriptor;
|
|
106
|
+
process: Subprocess;
|
|
107
|
+
port: number;
|
|
108
|
+
env: Record<string, string>;
|
|
109
|
+
intentional: boolean; // ← add
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export async function startCoordinator(options: CoordinatorOptions) {
|
|
113
|
+
const { config, plugins, scale, noBuild } = options;
|
|
114
|
+
const { port = 3000, host = "localhost", routes = "src/routes" } = config;
|
|
115
|
+
let stopping = false;
|
|
116
|
+
|
|
117
|
+
const workerEntry = join(import.meta.dir, "./worker.ts");
|
|
118
|
+
|
|
119
|
+
// ── inner helpers (close over plugins/stopping/workerEntry) ──
|
|
120
|
+
|
|
121
|
+
function spawnWorker(lw: LiveWorker): void {
|
|
122
|
+
const proc = spawn(["bun", "run", workerEntry], {
|
|
123
|
+
env: lw.env,
|
|
124
|
+
ipc(message) {
|
|
125
|
+
if (message?.ready) {
|
|
126
|
+
lw.descriptor.pid = proc.pid;
|
|
127
|
+
runHook(plugins, "onWorkerReady", lw.descriptor);
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
onExit(_, exitCode) {
|
|
131
|
+
if (lw.intentional) {
|
|
132
|
+
lw.intentional = false;
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
runHook(
|
|
136
|
+
plugins,
|
|
137
|
+
"onWorkerDeath",
|
|
138
|
+
lw.descriptor,
|
|
139
|
+
stopping ? "intentional" : "crash",
|
|
140
|
+
);
|
|
141
|
+
if (!stopping) {
|
|
142
|
+
logger.error(
|
|
143
|
+
`worker ${lw.descriptor.name ?? `${lw.descriptor.mode}-${lw.descriptor.index}`} crashed (exit ${exitCode}), respawning...`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
lw.process = proc;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function respawnWorker(lw: LiveWorker): Promise<void> {
|
|
152
|
+
lw.intentional = true;
|
|
153
|
+
lw.process.kill();
|
|
154
|
+
lw.descriptor.pid = undefined;
|
|
155
|
+
spawnWorker(lw);
|
|
156
|
+
await waitForWorkers([lw]);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 0. plugin resolution
|
|
160
|
+
const pluginCtx: PluginContext = {
|
|
161
|
+
mode: config.dev ? "development" : "production",
|
|
162
|
+
plugins: new Map(plugins.map((p) => [p.name, p])),
|
|
163
|
+
logger: log.scope("plugins"),
|
|
164
|
+
};
|
|
165
|
+
await runHook(plugins, "onPluginsResolved", pluginCtx);
|
|
166
|
+
|
|
167
|
+
// 1. build
|
|
168
|
+
if (!noBuild) {
|
|
169
|
+
const { result } = await buildProject(config, plugins);
|
|
170
|
+
if (!result.success) throw new Error("Grimoire: build failed");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 2. ephemeral secret
|
|
174
|
+
const secret = crypto.randomUUID() + crypto.randomUUID();
|
|
175
|
+
|
|
176
|
+
// 3. scan routes
|
|
177
|
+
const tree = await scanRoutes(routes, process.cwd());
|
|
178
|
+
|
|
179
|
+
// 4. route slices
|
|
180
|
+
const slices: Record<string, RouteFile[]> = {
|
|
181
|
+
api: tree.routes.filter((r) => r.type === "server"),
|
|
182
|
+
ws: tree.routes.filter((r) => r.type === "server"),
|
|
183
|
+
frontend: tree.routes.filter(
|
|
184
|
+
(r) => r.type === "page" || r.type === "simple",
|
|
185
|
+
),
|
|
186
|
+
full: [...tree.routes],
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// 5. spawn workers
|
|
190
|
+
const liveWorkers: LiveWorker[] = [];
|
|
191
|
+
let globalIndex = 0;
|
|
192
|
+
let nextPort = port + 1;
|
|
193
|
+
|
|
194
|
+
for (const [mode, count = 0] of Object.entries(scale)) {
|
|
195
|
+
for (let i = 0; i < count; i++) {
|
|
196
|
+
const workerPort = nextPort++;
|
|
197
|
+
const descriptor: WorkerDescriptor = {
|
|
198
|
+
mode: mode as WorkerMode,
|
|
199
|
+
index: i + 1,
|
|
200
|
+
globalIndex: globalIndex++,
|
|
201
|
+
internalUrl: `http://127.0.0.1:${workerPort}`,
|
|
202
|
+
routes: slices[mode] ?? slices.full,
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const publicEnvVars: Record<string, string> = {};
|
|
206
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
207
|
+
if (k.startsWith("PUBLIC_") && v !== undefined) publicEnvVars[k] = v;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const workerEnv = await runWorkerSpawn(plugins, descriptor);
|
|
211
|
+
if (workerEnv.name) descriptor.name = workerEnv.name;
|
|
212
|
+
|
|
213
|
+
const env = {
|
|
214
|
+
...process.env,
|
|
215
|
+
...workerEnv.env,
|
|
216
|
+
...publicEnvVars,
|
|
217
|
+
GRIMOIRE_MODE: mode,
|
|
218
|
+
GRIMOIRE_WORKER_INDEX: String(i),
|
|
219
|
+
GRIMOIRE_WORKER_PORT: String(workerPort),
|
|
220
|
+
GRIMOIRE_INTERNAL_SECRET: secret,
|
|
221
|
+
GRIMOIRE_WORKER_NAME: descriptor.name ?? `${mode}-${i}`,
|
|
222
|
+
} as Record<string, string>;
|
|
223
|
+
|
|
224
|
+
const lw: LiveWorker = {
|
|
225
|
+
descriptor,
|
|
226
|
+
process: null as any,
|
|
227
|
+
port: workerPort,
|
|
228
|
+
env,
|
|
229
|
+
intentional: false,
|
|
230
|
+
};
|
|
231
|
+
spawnWorker(lw);
|
|
232
|
+
liveWorkers.push(lw);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 6. wait for ready
|
|
237
|
+
await waitForWorkers(liveWorkers);
|
|
238
|
+
|
|
239
|
+
// 7. onCoordinatorStart
|
|
240
|
+
const ctx: CoordinatorContext = {
|
|
241
|
+
workers: liveWorkers.map((w) => w.descriptor),
|
|
242
|
+
port,
|
|
243
|
+
secret,
|
|
244
|
+
routes: tree,
|
|
245
|
+
};
|
|
246
|
+
await runHook(plugins, "onCoordinatorStart", ctx);
|
|
247
|
+
|
|
248
|
+
// 8. coordinator server
|
|
249
|
+
const server = Bun.serve({
|
|
250
|
+
port,
|
|
251
|
+
hostname: host,
|
|
252
|
+
fetch: async (req) => {
|
|
253
|
+
const url = new URL(req.url);
|
|
254
|
+
const isWsUpgrade =
|
|
255
|
+
req.headers.get("upgrade")?.toLowerCase() === "websocket";
|
|
256
|
+
|
|
257
|
+
if (isWsUpgrade) {
|
|
258
|
+
const descriptors = liveWorkers.map((w) => w.descriptor);
|
|
259
|
+
const chosen =
|
|
260
|
+
(await runRouteRequest(plugins, req, descriptors, tree)) ??
|
|
261
|
+
defaultRoute(req, descriptors, tree);
|
|
262
|
+
|
|
263
|
+
//@ts-expect-error
|
|
264
|
+
if (
|
|
265
|
+
server.upgrade(req, {
|
|
266
|
+
data: {
|
|
267
|
+
workerUrl: chosen.internalUrl,
|
|
268
|
+
pathname: url.pathname + url.search,
|
|
269
|
+
},
|
|
270
|
+
})
|
|
271
|
+
) {
|
|
272
|
+
return undefined as unknown as Response;
|
|
273
|
+
}
|
|
274
|
+
return new Response("Upgrade Required", { status: 426 });
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const locals: App.Locals = {};
|
|
278
|
+
const descriptors = liveWorkers.map((w) => w.descriptor);
|
|
279
|
+
const chosen =
|
|
280
|
+
(await runRouteRequest(plugins, req, descriptors, tree)) ??
|
|
281
|
+
defaultRoute(req, descriptors, tree);
|
|
282
|
+
const serialized =
|
|
283
|
+
(await runSerializeLocals(plugins, locals)) ??
|
|
284
|
+
defaultSerialize(locals, secret);
|
|
285
|
+
const forwardHeaders: Record<string, string> = {
|
|
286
|
+
...Object.fromEntries(req.headers),
|
|
287
|
+
"X-Grimoire-Locals": serialized,
|
|
288
|
+
"X-Grimoire-Internal": secret,
|
|
289
|
+
};
|
|
290
|
+
// Bun's fetch() to 127.0.0.1 drops the cookie header when it arrives
|
|
291
|
+
// via Object.fromEntries (lowercase "cookie"). Re-set it explicitly
|
|
292
|
+
// under the canonical "Cookie" name so the worker receives it.
|
|
293
|
+
const cookieHeader = req.headers.get("cookie");
|
|
294
|
+
if (cookieHeader) {
|
|
295
|
+
forwardHeaders["Cookie"] = cookieHeader;
|
|
296
|
+
}
|
|
297
|
+
return fetch(chosen.internalUrl + url.pathname + url.search, {
|
|
298
|
+
method: req.method,
|
|
299
|
+
headers: forwardHeaders,
|
|
300
|
+
body: req.body,
|
|
301
|
+
// never follow worker redirects: a 303 from a form action must
|
|
302
|
+
// reach the client verbatim (Set-Cookie + Location), otherwise
|
|
303
|
+
// the coordinator silently swallows the session cookie and
|
|
304
|
+
// returns the redirect target's HTML as a 200.
|
|
305
|
+
redirect: "manual",
|
|
306
|
+
});
|
|
307
|
+
},
|
|
308
|
+
websocket: {
|
|
309
|
+
open(ws) {
|
|
310
|
+
const upstream = new WebSocket(
|
|
311
|
+
//@ts-expect-error we give it data upstream
|
|
312
|
+
ws.data.workerUrl
|
|
313
|
+
.replace("http://", "ws://")
|
|
314
|
+
.replace("https://", "wss://") +
|
|
315
|
+
//@ts-expect-error we give it data upstream
|
|
316
|
+
ws.data.pathname,
|
|
317
|
+
);
|
|
318
|
+
upstream.onmessage = (ev) => ws.send(ev.data);
|
|
319
|
+
upstream.onclose = () => ws.close();
|
|
320
|
+
upstream.onerror = () => ws.close();
|
|
321
|
+
//@ts-expect-error we give it data upstream
|
|
322
|
+
ws.data.upstream = upstream;
|
|
323
|
+
},
|
|
324
|
+
message(ws, data) {
|
|
325
|
+
//@ts-expect-error we give it data upstream
|
|
326
|
+
ws.data.upstream?.send(data);
|
|
327
|
+
},
|
|
328
|
+
close(ws) {
|
|
329
|
+
//@ts-expect-error we give it data upstream
|
|
330
|
+
ws.data.upstream?.close();
|
|
331
|
+
},
|
|
332
|
+
drain(_ws) {},
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
// 9. expose stop + restartAll — CLI owns process lifetime
|
|
337
|
+
const stop = async () => {
|
|
338
|
+
if (stopping) return;
|
|
339
|
+
stopping = true;
|
|
340
|
+
await runHook(plugins, "onStop", "shutdown");
|
|
341
|
+
for (const w of liveWorkers) w.process.kill();
|
|
342
|
+
server.stop(true);
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const restartAll = () =>
|
|
346
|
+
Promise.all(liveWorkers.map((w) => respawnWorker(w)));
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
server,
|
|
350
|
+
stop,
|
|
351
|
+
restartAll,
|
|
352
|
+
workers: liveWorkers.map((w) => ({
|
|
353
|
+
name: w.descriptor.name,
|
|
354
|
+
mode: w.descriptor.mode,
|
|
355
|
+
port: w.port,
|
|
356
|
+
index: w.descriptor.index,
|
|
357
|
+
})),
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function defaultSerialize(locals: App.Locals, secret: string): string {
|
|
362
|
+
const payload = JSON.stringify(locals);
|
|
363
|
+
const sig = new Bun.CryptoHasher("sha256")
|
|
364
|
+
.update(secret + payload)
|
|
365
|
+
.digest("hex");
|
|
366
|
+
return Buffer.from(JSON.stringify({ payload, sig })).toString("base64");
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function _defaultDeserialize(raw: string, secret: string): App.Locals {
|
|
370
|
+
const { payload, sig } = JSON.parse(Buffer.from(raw, "base64").toString());
|
|
371
|
+
const expected = new Bun.CryptoHasher("sha256")
|
|
372
|
+
.update(secret + payload)
|
|
373
|
+
.digest("hex");
|
|
374
|
+
if (sig !== expected) throw new Error("Grimoire: locals signature invalid");
|
|
375
|
+
return JSON.parse(payload);
|
|
376
|
+
}
|