@sigil-dev/grimoire 0.9.10 → 0.9.12
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/index.ts +1 -0
- package/package.json +3 -3
- package/server.ts +1 -0
- package/src/client/csrf.ts +1 -1
- package/src/client/enhance.ts +63 -29
- package/src/client/router.ts +35 -11
- package/src/dev/compile-module.ts +12 -16
- package/src/dev/loader.ts +25 -4
- package/src/dev/resolve.ts +1 -1
- package/src/env/modules.ts +62 -0
- package/src/env/typegen.ts +2 -2
- package/src/rendering/head.ts +2 -5
- package/src/rendering/hydrate.ts +2 -0
- package/src/rendering/index.ts +144 -25
- package/src/rendering/ssrPlugin.ts +28 -13
- package/src/routing/transform-routes.ts +16 -16
- package/src/server/after-response.ts +194 -0
- package/src/server/asset-cache.ts +51 -0
- package/src/server/build.ts +17 -25
- package/src/server/cookie-utils.ts +19 -5
- package/src/server/coordinator.ts +113 -54
- package/src/server/hooks.ts +3 -1
- package/src/server/index.ts +598 -394
- package/src/server/resolver.ts +98 -0
- package/src/server/worker.ts +19 -6
- package/src/typegen/index.ts +17 -4
- package/src/types.ts +44 -1
- package/test/after-response.test.ts +113 -0
- package/test/asset-cache-policy.test.ts +83 -0
- package/test/client-router.test.ts +188 -0
- package/test/coordinator.test.ts +85 -0
- package/test/dev-loader.test.ts +30 -0
- package/test/env.test.ts +78 -0
- package/test/middleware.test.ts +20 -0
- package/test/rendering.test.ts +46 -0
- package/test/server.test.ts +204 -9
- package/test/streaming.test.ts +42 -1
- package/test/typegen.test.ts +31 -1
- package/src/client/head.ts +0 -30
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cache policy for files served out of public/.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from the server so the decision can be tested without booting
|
|
5
|
+
* one, and so there is exactly one copy of it. A policy test that keeps its
|
|
6
|
+
* own duplicate of the logic passes forever regardless of what the server
|
|
7
|
+
* does, which is worse than having no test.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A URL that changes whenever its content changes: a build-stamped hash, or
|
|
12
|
+
* a version query the app bumps per deploy.
|
|
13
|
+
*
|
|
14
|
+
* `?v=` deliberately counts. A file that a caller knows to re-request under
|
|
15
|
+
* a new query is content-addressed by that query, and that is the same
|
|
16
|
+
* guarantee a hash gives — the URL is the key. Requiring a literal hash in
|
|
17
|
+
* the path would send every project that uses the query form back to
|
|
18
|
+
* revalidation for no reason.
|
|
19
|
+
*/
|
|
20
|
+
export function isContentAddressed(pathname: string, search: string): boolean {
|
|
21
|
+
// /style.a1b2c3d4.css — a hash inserted before the extension.
|
|
22
|
+
if (/\.[0-9a-f]{8,}\.[a-z0-9]+$/i.test(pathname)) return true;
|
|
23
|
+
// A non-empty version query, e.g. /style.css?v=3. Deliberately does not
|
|
24
|
+
// treat `?` with an empty value as addressed: /style.css? is the same
|
|
25
|
+
// URL and would cache forever without ever changing.
|
|
26
|
+
return /[?&]v=[^&#]+/.test(search);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The `Cache-Control` for a public asset, by its URL. */
|
|
30
|
+
export function cacheControlFor(
|
|
31
|
+
pathname: string,
|
|
32
|
+
search: string,
|
|
33
|
+
): "no-cache" | "immutable" | "revalidate" {
|
|
34
|
+
// The document is what references the assets. Caching it hard means
|
|
35
|
+
// serving stale references to assets that are long gone.
|
|
36
|
+
if (pathname === "/" || pathname.endsWith(".html")) return "no-cache";
|
|
37
|
+
if (isContentAddressed(pathname, search)) return "immutable";
|
|
38
|
+
// Cached, but revalidated every time. Costs a conditional request and
|
|
39
|
+
// guarantees a deploy is visible on the next load — the right trade for a
|
|
40
|
+
// file whose URL is not content-addressed.
|
|
41
|
+
return "revalidate";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const CACHE_CONTROL: Record<
|
|
45
|
+
ReturnType<typeof cacheControlFor>,
|
|
46
|
+
string
|
|
47
|
+
> = {
|
|
48
|
+
"no-cache": "no-cache",
|
|
49
|
+
immutable: "public, max-age=31536000, immutable",
|
|
50
|
+
revalidate: "public, max-age=0, must-revalidate",
|
|
51
|
+
};
|
package/src/server/build.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { mkdir } from "node:fs/promises";
|
|
2
2
|
import { isAbsolute, join } from "node:path";
|
|
3
|
-
import { cwd } from "node:process";
|
|
4
3
|
import { sigil } from "@sigil-dev/compiler/bun";
|
|
5
4
|
import { generateManifest } from "../routing/manifest-gen";
|
|
6
5
|
import type { RouteTree } from "../routing/scanner";
|
|
@@ -9,18 +8,7 @@ import { transformRoutes } from "../routing/transform-routes";
|
|
|
9
8
|
import { generateTypes } from "../typegen";
|
|
10
9
|
import type { BuildResult, GrimoireConfig, GrimoirePlugin } from "../types";
|
|
11
10
|
import { runHook } from "./plugins";
|
|
12
|
-
|
|
13
|
-
function makeAliases(config: GrimoireConfig): Record<string, string> {
|
|
14
|
-
return {
|
|
15
|
-
...Object.fromEntries(
|
|
16
|
-
Object.entries(config.alias ?? {}).map(([k, v]) => [k, join(cwd(), v)]),
|
|
17
|
-
),
|
|
18
|
-
"$env/static/public": join(import.meta.dir, "../env/public.ts"),
|
|
19
|
-
"$env/static/private": join(import.meta.dir, "../env/private.ts"),
|
|
20
|
-
"$env/dynamic/public": join(import.meta.dir, "../env/dynamic-public.ts"),
|
|
21
|
-
"$env/dynamic/private": join(import.meta.dir, "../env/dynamic-private.ts"),
|
|
22
|
-
};
|
|
23
|
-
}
|
|
11
|
+
import { projectAliases, resolverPlugin } from "./resolver";
|
|
24
12
|
|
|
25
13
|
const makeRoutesPlugin = (manifestPath: string) => ({
|
|
26
14
|
name: "grimoire-routes",
|
|
@@ -73,31 +61,32 @@ export async function rebuildClient(
|
|
|
73
61
|
]);
|
|
74
62
|
|
|
75
63
|
const publicDir = join(process.cwd(), "public/__grimoire__");
|
|
76
|
-
const resolvedAlias = makeAliases(config);
|
|
77
64
|
|
|
78
65
|
const [hydrateResult, domResult] = await Promise.all([
|
|
79
66
|
Bun.build({
|
|
80
67
|
entrypoints: [join(import.meta.dir, "../rendering/hydrate.ts")],
|
|
81
68
|
outdir: publicDir,
|
|
69
|
+
// report failures in result.logs instead of throwing past them
|
|
70
|
+
throw: false,
|
|
82
71
|
// lazy route entries in the manifest use dynamic import()
|
|
83
72
|
splitting: true,
|
|
84
73
|
plugins: [
|
|
85
74
|
sigil({ mode: "hydrate", dev: isDev }),
|
|
86
75
|
makeRoutesPlugin(hydrateManifest),
|
|
76
|
+
resolverPlugin(config, "browser"),
|
|
87
77
|
],
|
|
88
|
-
// @ts-expect-error
|
|
89
|
-
alias: resolvedAlias,
|
|
90
78
|
}),
|
|
91
79
|
Bun.build({
|
|
92
80
|
entrypoints: [join(import.meta.dir, "../client/index.ts")],
|
|
93
81
|
outdir: publicDir,
|
|
82
|
+
// report failures in result.logs instead of throwing past them
|
|
83
|
+
throw: false,
|
|
94
84
|
splitting: true,
|
|
95
85
|
plugins: [
|
|
96
86
|
sigil({ mode: "dom", dev: isDev }),
|
|
97
87
|
makeRoutesPlugin(domManifest),
|
|
88
|
+
resolverPlugin(config, "browser"),
|
|
98
89
|
],
|
|
99
|
-
// @ts-expect-error
|
|
100
|
-
alias: resolvedAlias,
|
|
101
90
|
}),
|
|
102
91
|
]);
|
|
103
92
|
|
|
@@ -134,6 +123,7 @@ export async function buildProject(
|
|
|
134
123
|
projectRoot: process.cwd(),
|
|
135
124
|
routesDir,
|
|
136
125
|
outDir: join(outDir, "types"),
|
|
126
|
+
aliases: projectAliases(config),
|
|
137
127
|
});
|
|
138
128
|
|
|
139
129
|
const { generateEnvTypes } = await import("../env/typegen");
|
|
@@ -167,7 +157,6 @@ export async function buildProject(
|
|
|
167
157
|
),
|
|
168
158
|
]);
|
|
169
159
|
|
|
170
|
-
const resolvedAlias = makeAliases(config);
|
|
171
160
|
const bundleStrategy = config.bundleStrategy ?? "split";
|
|
172
161
|
const publicDir = join(process.cwd(), "public/__grimoire__");
|
|
173
162
|
|
|
@@ -178,6 +167,8 @@ export async function buildProject(
|
|
|
178
167
|
join(import.meta.dir, "../client/index.ts"),
|
|
179
168
|
],
|
|
180
169
|
outdir: publicDir,
|
|
170
|
+
// report failures in result.logs instead of throwing past them
|
|
171
|
+
throw: false,
|
|
181
172
|
// lazy route entries in the manifest use dynamic import()
|
|
182
173
|
splitting: true,
|
|
183
174
|
plugins: [
|
|
@@ -185,9 +176,8 @@ export async function buildProject(
|
|
|
185
176
|
sigil({ mode: "dom", dev: isDev }),
|
|
186
177
|
makeRoutesPlugin(hydrateManifest),
|
|
187
178
|
makeRoutesPlugin(domManifest),
|
|
179
|
+
resolverPlugin(config, "browser"),
|
|
188
180
|
],
|
|
189
|
-
// @ts-expect-error
|
|
190
|
-
alias: resolvedAlias,
|
|
191
181
|
});
|
|
192
182
|
|
|
193
183
|
if (!singleResult.success) {
|
|
@@ -208,25 +198,27 @@ export async function buildProject(
|
|
|
208
198
|
Bun.build({
|
|
209
199
|
entrypoints: [join(import.meta.dir, "../rendering/hydrate.ts")],
|
|
210
200
|
outdir: publicDir,
|
|
201
|
+
// report failures in result.logs instead of throwing past them
|
|
202
|
+
throw: false,
|
|
211
203
|
// lazy route entries in the manifest use dynamic import()
|
|
212
204
|
splitting: true,
|
|
213
205
|
plugins: [
|
|
214
206
|
sigil({ mode: "hydrate", dev: isDev }),
|
|
215
207
|
makeRoutesPlugin(hydrateManifest),
|
|
208
|
+
resolverPlugin(config, "browser"),
|
|
216
209
|
],
|
|
217
|
-
// @ts-expect-error
|
|
218
|
-
alias: resolvedAlias,
|
|
219
210
|
}),
|
|
220
211
|
Bun.build({
|
|
221
212
|
entrypoints: [join(import.meta.dir, "../client/index.ts")],
|
|
222
213
|
outdir: publicDir,
|
|
214
|
+
// report failures in result.logs instead of throwing past them
|
|
215
|
+
throw: false,
|
|
223
216
|
splitting: true,
|
|
224
217
|
plugins: [
|
|
225
218
|
sigil({ mode: "dom", dev: isDev }),
|
|
226
219
|
makeRoutesPlugin(domManifest),
|
|
220
|
+
resolverPlugin(config, "browser"),
|
|
227
221
|
],
|
|
228
|
-
// @ts-expect-error
|
|
229
|
-
alias: resolvedAlias,
|
|
230
222
|
}),
|
|
231
223
|
]);
|
|
232
224
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { CookieOptions } from "./hooks";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Parse cookies from a Cookie header string.
|
|
3
5
|
*/
|
|
@@ -6,7 +8,14 @@ export function parseCookies(cookieHeader: string): Map<string, string> {
|
|
|
6
8
|
if (!cookieHeader) return map;
|
|
7
9
|
for (const pair of cookieHeader.split(";")) {
|
|
8
10
|
const [name, ...rest] = pair.split("=");
|
|
9
|
-
if (name)
|
|
11
|
+
if (!name) continue;
|
|
12
|
+
// set() encodes values, so get() must decode them to round-trip
|
|
13
|
+
const raw = rest.join("=").trim();
|
|
14
|
+
let value = raw;
|
|
15
|
+
try {
|
|
16
|
+
value = decodeURIComponent(raw);
|
|
17
|
+
} catch {}
|
|
18
|
+
map.set(name.trim(), value);
|
|
10
19
|
}
|
|
11
20
|
return map;
|
|
12
21
|
}
|
|
@@ -51,13 +60,18 @@ export function createCookies(
|
|
|
51
60
|
get(name: string) {
|
|
52
61
|
return store.get(name);
|
|
53
62
|
},
|
|
54
|
-
|
|
63
|
+
// Path defaults to "/": without one the browser scopes the cookie to
|
|
64
|
+
// the request's directory, so a session set by /account/login would
|
|
65
|
+
// never reach /, and a delete from another page would miss it
|
|
66
|
+
set(name: string, value: string, options?: CookieOptions) {
|
|
55
67
|
store.set(name, value);
|
|
56
|
-
pending.push(serializeCookie(name, value, options));
|
|
68
|
+
pending.push(serializeCookie(name, value, { path: "/", ...options }));
|
|
57
69
|
},
|
|
58
|
-
delete(name: string) {
|
|
70
|
+
delete(name: string, options?: Pick<CookieOptions, "path" | "domain">) {
|
|
59
71
|
store.delete(name);
|
|
60
|
-
pending.push(
|
|
72
|
+
pending.push(
|
|
73
|
+
serializeCookie(name, "", { path: "/", ...options, maxAge: 0 }),
|
|
74
|
+
);
|
|
61
75
|
},
|
|
62
76
|
toHeaders() {
|
|
63
77
|
return pending;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
|
-
import { type Subprocess, spawn } from "bun";
|
|
2
|
+
import { type ServerWebSocket, type Subprocess, spawn } from "bun";
|
|
3
3
|
import { log } from "../logger/instance";
|
|
4
4
|
import { matchRoute } from "../routing/router";
|
|
5
5
|
import { type RouteFile, type RouteTree, scanRoutes } from "../routing/scanner";
|
|
@@ -106,7 +106,10 @@ interface LiveWorker {
|
|
|
106
106
|
process: Subprocess;
|
|
107
107
|
port: number;
|
|
108
108
|
env: Record<string, string>;
|
|
109
|
-
intentional: boolean;
|
|
109
|
+
intentional: boolean;
|
|
110
|
+
/** When the current process was spawned, and the next restart delay. */
|
|
111
|
+
startedAt: number;
|
|
112
|
+
backoff: number;
|
|
110
113
|
}
|
|
111
114
|
|
|
112
115
|
export async function startCoordinator(options: CoordinatorOptions) {
|
|
@@ -132,20 +135,28 @@ export async function startCoordinator(options: CoordinatorOptions) {
|
|
|
132
135
|
lw.intentional = false;
|
|
133
136
|
return;
|
|
134
137
|
}
|
|
138
|
+
lw.descriptor.pid = undefined;
|
|
135
139
|
runHook(
|
|
136
140
|
plugins,
|
|
137
141
|
"onWorkerDeath",
|
|
138
142
|
lw.descriptor,
|
|
139
143
|
stopping ? "intentional" : "crash",
|
|
140
144
|
);
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
145
|
+
if (stopping) return;
|
|
146
|
+
// a worker that stayed up a while starts over at one second; one
|
|
147
|
+
// that keeps dying on startup waits longer each time, up to 30s
|
|
148
|
+
const upFor = Date.now() - lw.startedAt;
|
|
149
|
+
lw.backoff = upFor > 30_000 ? 1_000 : Math.min(lw.backoff * 2, 30_000);
|
|
150
|
+
logger.error(
|
|
151
|
+
`worker ${lw.descriptor.name ?? `${lw.descriptor.mode}-${lw.descriptor.index}`} crashed (exit ${exitCode}), respawning in ${lw.backoff / 1000}s`,
|
|
152
|
+
);
|
|
153
|
+
setTimeout(() => {
|
|
154
|
+
if (!stopping) spawnWorker(lw);
|
|
155
|
+
}, lw.backoff);
|
|
146
156
|
},
|
|
147
157
|
});
|
|
148
158
|
lw.process = proc;
|
|
159
|
+
lw.startedAt = Date.now();
|
|
149
160
|
}
|
|
150
161
|
|
|
151
162
|
async function respawnWorker(lw: LiveWorker): Promise<void> {
|
|
@@ -227,6 +238,8 @@ export async function startCoordinator(options: CoordinatorOptions) {
|
|
|
227
238
|
port: workerPort,
|
|
228
239
|
env,
|
|
229
240
|
intentional: false,
|
|
241
|
+
startedAt: 0,
|
|
242
|
+
backoff: 500,
|
|
230
243
|
};
|
|
231
244
|
spawnWorker(lw);
|
|
232
245
|
liveWorkers.push(lw);
|
|
@@ -260,15 +273,13 @@ export async function startCoordinator(options: CoordinatorOptions) {
|
|
|
260
273
|
(await runRouteRequest(plugins, req, descriptors, tree)) ??
|
|
261
274
|
defaultRoute(req, descriptors, tree);
|
|
262
275
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
})
|
|
271
|
-
) {
|
|
276
|
+
const data: WsProxyData = {
|
|
277
|
+
workerUrl: chosen.internalUrl,
|
|
278
|
+
pathname: url.pathname + url.search,
|
|
279
|
+
headers: upstreamWsHeaders(req, secret),
|
|
280
|
+
pending: [],
|
|
281
|
+
};
|
|
282
|
+
if (server.upgrade(req, { data })) {
|
|
272
283
|
return undefined as unknown as Response;
|
|
273
284
|
}
|
|
274
285
|
return new Response("Upgrade Required", { status: 426 });
|
|
@@ -282,54 +293,67 @@ export async function startCoordinator(options: CoordinatorOptions) {
|
|
|
282
293
|
const serialized =
|
|
283
294
|
(await runSerializeLocals(plugins, locals)) ??
|
|
284
295
|
defaultSerialize(locals, secret);
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
+
// the client's own x-grimoire-* headers are dropped: spread next to
|
|
297
|
+
// ours under a different case, both reached the worker joined by a
|
|
298
|
+
// comma, and the secret check refused the request
|
|
299
|
+
const forwardHeaders = new Headers();
|
|
300
|
+
for (const [k, v] of req.headers) {
|
|
301
|
+
if (!k.toLowerCase().startsWith("x-grimoire-"))
|
|
302
|
+
forwardHeaders.set(k, v);
|
|
303
|
+
}
|
|
304
|
+
forwardHeaders.set("X-Grimoire-Locals", serialized);
|
|
305
|
+
forwardHeaders.set("X-Grimoire-Internal", secret);
|
|
306
|
+
try {
|
|
307
|
+
return await fetch(chosen.internalUrl + url.pathname + url.search, {
|
|
308
|
+
method: req.method,
|
|
309
|
+
headers: forwardHeaders,
|
|
310
|
+
body: req.body,
|
|
311
|
+
// never follow worker redirects: a 303 from a form action must
|
|
312
|
+
// reach the client verbatim (Set-Cookie + Location), otherwise
|
|
313
|
+
// the coordinator silently swallows the session cookie and
|
|
314
|
+
// returns the redirect target's HTML as a 200.
|
|
315
|
+
redirect: "manual",
|
|
316
|
+
});
|
|
317
|
+
} catch {
|
|
318
|
+
// the worker is down (it's being restarted)
|
|
319
|
+
return new Response("Bad Gateway", {
|
|
320
|
+
status: 502,
|
|
321
|
+
headers: { "Retry-After": "1" },
|
|
322
|
+
});
|
|
296
323
|
}
|
|
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
324
|
},
|
|
308
325
|
websocket: {
|
|
309
|
-
|
|
326
|
+
// Relay to the worker. Messages that arrive before the worker's
|
|
327
|
+
// socket is open are queued: sending on a connecting socket throws,
|
|
328
|
+
// and uncaught, that took the whole coordinator down.
|
|
329
|
+
open(ws: ServerWebSocket<WsProxyData>) {
|
|
330
|
+
const d = ws.data;
|
|
310
331
|
const upstream = new WebSocket(
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
//@ts-expect-error we give it data upstream
|
|
316
|
-
ws.data.pathname,
|
|
332
|
+
d.workerUrl.replace(/^http/, "ws") + d.pathname,
|
|
333
|
+
// Bun extension: the worker checks the internal secret on the
|
|
334
|
+
// upgrade, and hooks need the visitor's cookies
|
|
335
|
+
{ headers: d.headers } as unknown as string[],
|
|
317
336
|
);
|
|
337
|
+
upstream.binaryType = "arraybuffer";
|
|
338
|
+
upstream.onopen = () => {
|
|
339
|
+
for (const m of d.pending) upstream.send(m);
|
|
340
|
+
d.pending = [];
|
|
341
|
+
};
|
|
318
342
|
upstream.onmessage = (ev) => ws.send(ev.data);
|
|
319
|
-
upstream.onclose = () =>
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
343
|
+
upstream.onclose = (ev) =>
|
|
344
|
+
ws.close(ev.code === 1005 ? 1000 : ev.code, ev.reason);
|
|
345
|
+
upstream.onerror = () => ws.close(1011);
|
|
346
|
+
d.upstream = upstream;
|
|
323
347
|
},
|
|
324
|
-
message(ws
|
|
325
|
-
|
|
326
|
-
|
|
348
|
+
message(ws: ServerWebSocket<WsProxyData>, data) {
|
|
349
|
+
const upstream = ws.data.upstream;
|
|
350
|
+
if (upstream?.readyState === WebSocket.OPEN) upstream.send(data);
|
|
351
|
+
else if (upstream?.readyState === WebSocket.CONNECTING)
|
|
352
|
+
ws.data.pending.push(data);
|
|
327
353
|
},
|
|
328
|
-
close(ws) {
|
|
329
|
-
//@ts-expect-error we give it data upstream
|
|
354
|
+
close(ws: ServerWebSocket<WsProxyData>) {
|
|
330
355
|
ws.data.upstream?.close();
|
|
331
356
|
},
|
|
332
|
-
drain(_ws) {},
|
|
333
357
|
},
|
|
334
358
|
});
|
|
335
359
|
|
|
@@ -354,10 +378,45 @@ export async function startCoordinator(options: CoordinatorOptions) {
|
|
|
354
378
|
mode: w.descriptor.mode,
|
|
355
379
|
port: w.port,
|
|
356
380
|
index: w.descriptor.index,
|
|
381
|
+
/** The worker's current process id (changes when it's respawned). */
|
|
382
|
+
get pid() {
|
|
383
|
+
return w.process.pid;
|
|
384
|
+
},
|
|
357
385
|
})),
|
|
358
386
|
};
|
|
359
387
|
}
|
|
360
388
|
|
|
389
|
+
interface WsProxyData {
|
|
390
|
+
workerUrl: string;
|
|
391
|
+
pathname: string;
|
|
392
|
+
headers: Record<string, string>;
|
|
393
|
+
pending: (string | Buffer)[];
|
|
394
|
+
upstream?: WebSocket;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Headers for the coordinator's own socket to the worker. */
|
|
398
|
+
function upstreamWsHeaders(
|
|
399
|
+
req: Request,
|
|
400
|
+
secret: string,
|
|
401
|
+
): Record<string, string> {
|
|
402
|
+
const out: Record<string, string> = {};
|
|
403
|
+
for (const [k, v] of req.headers) {
|
|
404
|
+
const key = k.toLowerCase();
|
|
405
|
+
// the WebSocket client writes its own handshake headers
|
|
406
|
+
if (
|
|
407
|
+
key.startsWith("sec-websocket-") ||
|
|
408
|
+
key.startsWith("x-grimoire-") ||
|
|
409
|
+
key === "upgrade" ||
|
|
410
|
+
key === "connection" ||
|
|
411
|
+
key === "host"
|
|
412
|
+
)
|
|
413
|
+
continue;
|
|
414
|
+
out[k] = v;
|
|
415
|
+
}
|
|
416
|
+
out["X-Grimoire-Internal"] = secret;
|
|
417
|
+
return out;
|
|
418
|
+
}
|
|
419
|
+
|
|
361
420
|
function defaultSerialize(locals: App.Locals, secret: string): string {
|
|
362
421
|
const payload = JSON.stringify(locals);
|
|
363
422
|
const sig = new Bun.CryptoHasher("sha256")
|
package/src/server/hooks.ts
CHANGED
|
@@ -45,8 +45,10 @@ export interface RequestEvent {
|
|
|
45
45
|
|
|
46
46
|
export interface Cookies {
|
|
47
47
|
get(name: string): string | undefined;
|
|
48
|
+
/** Path defaults to "/". */
|
|
48
49
|
set(name: string, value: string, options?: CookieOptions): void;
|
|
49
|
-
|
|
50
|
+
/** Pass the path and domain the cookie was set with; path defaults to "/". */
|
|
51
|
+
delete(name: string, options?: Pick<CookieOptions, "path" | "domain">): void;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
export interface CookieOptions {
|