@sigil-dev/grimoire 0.9.11 → 0.9.13
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 +5 -1
- package/package.json +3 -3
- package/server.ts +1 -0
- package/src/client/deferred.ts +179 -0
- package/src/rendering/ssrPlugin.ts +16 -1
- package/src/server/after-response.ts +194 -0
- package/src/server/asset-cache.ts +51 -0
- package/src/server/coordinator.ts +26 -6
- package/src/server/index.ts +593 -468
- package/src/server/plugins.ts +35 -0
- package/src/server/worker.ts +13 -1
- package/src/types.ts +118 -1
- package/test/after-response.test.ts +113 -0
- package/test/asset-cache-policy.test.ts +83 -0
- package/test/coordinator.test.ts +9 -0
- package/test/deferred.test.ts +256 -0
- package/test/parser-plugins.test.ts +92 -0
- package/test/plugin-services-context.test.ts +97 -0
- package/test/services.test.ts +90 -0
- package/test/streaming.test.ts +56 -0
- package/test/worker-context.test.ts +59 -0
- package/test/workspace-files.test.ts +146 -0
- package/test/workspace-versions.test.ts +131 -0
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
package/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// server loads call depends() to register invalidation keys
|
|
2
2
|
export { depends } from "./src/client/deps";
|
|
3
|
+
export type { Deferred } from "./src/client/deferred";
|
|
4
|
+
export { deferred, onDeferred } from "./src/client/deferred";
|
|
3
5
|
export { Head } from "./src/rendering/head";
|
|
4
6
|
// registerSSRPlugin is server-only — import from "@sigil-dev/grimoire/bun"
|
|
5
7
|
export type { RouteFile, RouteTree } from "./src/routing/scanner";
|
|
@@ -19,6 +21,7 @@ export type {
|
|
|
19
21
|
export { createHooks, sequence } from "./src/server/hooks";
|
|
20
22
|
export type {
|
|
21
23
|
BuiltinWorkerMode,
|
|
24
|
+
CliCommand,
|
|
22
25
|
CoordinatorContext,
|
|
23
26
|
GrimoireConfig,
|
|
24
27
|
GrimoirePlugin,
|
|
@@ -33,5 +36,6 @@ export type {
|
|
|
33
36
|
WorkerEnv,
|
|
34
37
|
WorkerMode,
|
|
35
38
|
WsRouteHandler,
|
|
39
|
+
WorkerContext,
|
|
36
40
|
} from "./src/types";
|
|
37
|
-
export { defineConfig } from "./src/types";
|
|
41
|
+
export { defineConfig, workerContext } from "./src/types";
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"module": "index.ts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
7
|
-
"version": "0.9.
|
|
7
|
+
"version": "0.9.13",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./index.ts",
|
|
10
10
|
"./server": "./server.ts",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"test": "bun test"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@sigil-dev/compiler": "0.9.
|
|
27
|
-
"@sigil-dev/runtime": "0.9.
|
|
26
|
+
"@sigil-dev/compiler": "0.9.13",
|
|
27
|
+
"@sigil-dev/runtime": "0.9.13",
|
|
28
28
|
"vite": "^8.0.16"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
package/server.ts
CHANGED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a value that is still in flight.
|
|
3
|
+
*
|
|
4
|
+
* A top-level promise in load data streams in later. The server sends the
|
|
5
|
+
* page immediately, so the component renders with that value `undefined` and
|
|
6
|
+
* the visitor reads the rest of the page while the slow part is still on its
|
|
7
|
+
* way. When it settles, the server sends a small patch script that writes the
|
|
8
|
+
* value into the state and dispatches `grimoire:deferred`.
|
|
9
|
+
*
|
|
10
|
+
* Until now each app wired that event up by hand — the same fifteen lines,
|
|
11
|
+
* copied, with the details easy to get wrong. `deferred()` is those lines.
|
|
12
|
+
*
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const reviews = deferred<Review[]>(props.data.reviews, []);
|
|
15
|
+
*
|
|
16
|
+
* return reviews.done
|
|
17
|
+
* ? <ul>{reviews.value.map((r) => <li key={r.id}>{r.text}</li>)}</ul>
|
|
18
|
+
* : <p>Loading reviews…</p>;
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* The one thing this cannot do is beat hydration. The page's state script
|
|
22
|
+
* holds the values, and hydration waits for the whole response, so a value
|
|
23
|
+
* that arrives after the document does will not re-render the tree on its
|
|
24
|
+
* own. The listener is what closes that gap — it fires for late arrivals and
|
|
25
|
+
* updates the signal, so a component that re-renders (on a click, a
|
|
26
|
+
* navigation, a transition) sees the value. For a value that has to appear
|
|
27
|
+
* the instant it lands without any other interaction, the [Suspense
|
|
28
|
+
* boundary](https://react.dev/reference/react/Suspense) already in your tree
|
|
29
|
+
* is the mechanism — the value goes in as ordinary data and Suspense
|
|
30
|
+
* handles the timing.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The hydration state the server embeds, as this module sees it.
|
|
35
|
+
*
|
|
36
|
+
* Declared locally because the same shape is declared in @sigil-dev/types
|
|
37
|
+
* for applications, and grimoire does not depend on that package — a
|
|
38
|
+
* framework package should not reach for the app's type package to describe
|
|
39
|
+
* its own runtime. The two must agree, and `deferred.test.ts` drives the
|
|
40
|
+
* server's real patch script, so a change to one that the other does not
|
|
41
|
+
* follow fails a test.
|
|
42
|
+
*/
|
|
43
|
+
interface GrimoireStateMirror {
|
|
44
|
+
pattern?: string;
|
|
45
|
+
data?: Record<string, unknown>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The window this module reads. A local alias, not a global augmentation. */
|
|
49
|
+
interface GrimoireWindow {
|
|
50
|
+
__grimoire_state__?: GrimoireStateMirror | HTMLElement;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const state = (): GrimoireStateMirror | HTMLElement | undefined =>
|
|
54
|
+
(globalThis as unknown as { window?: GrimoireWindow }).window
|
|
55
|
+
?.__grimoire_state__;
|
|
56
|
+
|
|
57
|
+
/** The value a component sees while it is still in flight. */
|
|
58
|
+
export interface Deferred<T> {
|
|
59
|
+
/** The value, or `fallback` while it is still loading. */
|
|
60
|
+
readonly value: T;
|
|
61
|
+
/** `true` once the real value has arrived. */
|
|
62
|
+
readonly done: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The current value of a streamed property, or `undefined` if still in flight. */
|
|
66
|
+
function readDeferredValue<T>(key?: string): T | undefined {
|
|
67
|
+
const held = state();
|
|
68
|
+
// The element carries the authoritative copy but has nodeType; the plain
|
|
69
|
+
// mirror does not. Reading the wrong one silently loses the value, so
|
|
70
|
+
// this guard is the difference between working and not.
|
|
71
|
+
if (!held) return undefined;
|
|
72
|
+
const data = held instanceof HTMLElement ? undefined : held.data;
|
|
73
|
+
if (!data) return undefined;
|
|
74
|
+
if (key !== undefined) {
|
|
75
|
+
const one = data[key];
|
|
76
|
+
return one === undefined || one === null ? undefined : (one as T);
|
|
77
|
+
}
|
|
78
|
+
for (const one of Object.values(data)) {
|
|
79
|
+
if (one !== undefined && one !== null) return one as T;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Wrap a possibly-streamed load value.
|
|
86
|
+
*
|
|
87
|
+
* @param incoming The prop from `data`. `undefined` means still loading; any
|
|
88
|
+
* other value is already resolved, which is the normal case on a
|
|
89
|
+
* client-side navigation and on the server.
|
|
90
|
+
* @param fallback What to render while loading.
|
|
91
|
+
* @param key The property name this value streams under. Required when a
|
|
92
|
+
* page has more than one deferred value, because the patch event only says
|
|
93
|
+
* *that* something settled — reading the first non-empty entry in the state
|
|
94
|
+
* would hand one component another's data. With a single deferred value on
|
|
95
|
+
* the page it can be left out, and any settled value is taken.
|
|
96
|
+
*/
|
|
97
|
+
export function deferred<T>(
|
|
98
|
+
incoming: T | undefined,
|
|
99
|
+
fallback: T,
|
|
100
|
+
key?: string,
|
|
101
|
+
): Deferred<T> {
|
|
102
|
+
let value = incoming ?? fallback;
|
|
103
|
+
let done = incoming !== undefined;
|
|
104
|
+
|
|
105
|
+
// Only in a browser: the server has no window, and a value still in
|
|
106
|
+
// flight is genuinely `undefined` here.
|
|
107
|
+
if (typeof window !== "undefined" && !done) {
|
|
108
|
+
// The patch may have landed between this component's first render and
|
|
109
|
+
// the event wiring, which is a real window when a value resolves in
|
|
110
|
+
// ~1ms. Reading on the way in is what closes it.
|
|
111
|
+
const early = readDeferredValue<T>(key);
|
|
112
|
+
if (early !== undefined) {
|
|
113
|
+
value = early;
|
|
114
|
+
done = true;
|
|
115
|
+
} else {
|
|
116
|
+
window.addEventListener("grimoire:deferred", (event) => {
|
|
117
|
+
// With a key, the event names it — so an unrelated value
|
|
118
|
+
// settling does not mark this one done.
|
|
119
|
+
if (key !== undefined) {
|
|
120
|
+
const detail = (event as CustomEvent<{ key?: string }>).detail;
|
|
121
|
+
if (detail?.key !== undefined && detail.key !== key) return;
|
|
122
|
+
}
|
|
123
|
+
const held = readDeferredValue<T>(key);
|
|
124
|
+
if (held === undefined) return;
|
|
125
|
+
value = held;
|
|
126
|
+
done = true;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
get value() {
|
|
133
|
+
return value;
|
|
134
|
+
},
|
|
135
|
+
get done() {
|
|
136
|
+
return done;
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Drive a `$state` from a streamed value.
|
|
143
|
+
*
|
|
144
|
+
* `deferred()` returns a snapshot, which is right for a component that
|
|
145
|
+
* re-renders on its own. This is for a value that is rendered somewhere
|
|
146
|
+
* else, or held in shared state — the pattern where a module-level
|
|
147
|
+
* `$state` is assigned from the event:
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* let omenText = $state("(the ink is still drying…)");
|
|
151
|
+
* onDeferred((value) => { omenText = value; }, "omen");
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* The callback runs once immediately if the value has already landed, so
|
|
155
|
+
* there is no window where it is missed.
|
|
156
|
+
*/
|
|
157
|
+
export function onDeferred<T>(
|
|
158
|
+
apply: (value: T) => void,
|
|
159
|
+
key?: string,
|
|
160
|
+
): void {
|
|
161
|
+
if (typeof window === "undefined") return;
|
|
162
|
+
|
|
163
|
+
// Fires once, immediately, if the value has already landed — the same
|
|
164
|
+
// early-arrival read deferred() does, so there is no window where a
|
|
165
|
+
// value that settled first is missed.
|
|
166
|
+
const current = readDeferredValue<T>(key);
|
|
167
|
+
if (current !== undefined) {
|
|
168
|
+
apply(current);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
window.addEventListener("grimoire:deferred", (event) => {
|
|
172
|
+
if (key !== undefined) {
|
|
173
|
+
const detail = (event as CustomEvent<{ key?: string }>).detail;
|
|
174
|
+
if (detail?.key !== undefined && detail.key !== key) return;
|
|
175
|
+
}
|
|
176
|
+
const held = readDeferredValue<T>(key);
|
|
177
|
+
if (held !== undefined) apply(held);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import { log } from "../logger/instance";
|
|
9
9
|
import { rewriteAliases } from "../server/resolver";
|
|
10
10
|
import type { GrimoirePlugin } from "../types";
|
|
11
|
+
import type { ParserPlugin } from "@babel/core";
|
|
11
12
|
|
|
12
13
|
const logger = log.scope("scope");
|
|
13
14
|
let registered = false;
|
|
@@ -67,7 +68,21 @@ export function registerSSRPlugin(
|
|
|
67
68
|
configFile: false,
|
|
68
69
|
babelrc: false,
|
|
69
70
|
parserOpts: {
|
|
70
|
-
|
|
71
|
+
// The jsx plugin goes on .tsx files only.
|
|
72
|
+
//
|
|
73
|
+
// With it enabled everywhere, Babel parses `<R>(x: R) => x`
|
|
74
|
+
// as the opening of a JSX element and fails with
|
|
75
|
+
// "Unexpected token `>`". So a generic arrow function in a
|
|
76
|
+
// plain .ts file — completely ordinary TypeScript — could
|
|
77
|
+
// not be compiled. Found while writing the Drizzle adapter
|
|
78
|
+
// recipe, where `transaction: <R>(db, fn) => ...` is the
|
|
79
|
+
// form that removes an `as any`.
|
|
80
|
+
//
|
|
81
|
+
// A .tsx file is genuinely ambiguous (`<R>(x) => x` is
|
|
82
|
+
// JSX there), so .tsx keeps both and users write `<R,>`.
|
|
83
|
+
plugins: path.endsWith(".tsx")
|
|
84
|
+
? (["typescript", "jsx"] as ParserPlugin[])
|
|
85
|
+
: (["typescript"] as ParserPlugin[]),
|
|
71
86
|
},
|
|
72
87
|
plugins: [[sigilPlugin, { mode: "ssr", hash }]],
|
|
73
88
|
filename: path,
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { log } from "../logger/instance.ts";
|
|
3
|
+
|
|
4
|
+
const logger = log.scope("server");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Tasks collected for the in-flight request.
|
|
8
|
+
*
|
|
9
|
+
* AsyncLocalStorage is what makes afterResponse() work without threading a
|
|
10
|
+
* context argument through every function that might want to defer work.
|
|
11
|
+
* A plain module-level array would interleave badly under concurrency: two
|
|
12
|
+
* requests running at once could collect each other's tasks, and a task
|
|
13
|
+
* would run at the end of the wrong response.
|
|
14
|
+
*/
|
|
15
|
+
interface RequestScope {
|
|
16
|
+
tasks: Array<() => unknown>;
|
|
17
|
+
/** Resolves when fire() has run every task, so drain can wait for this. */
|
|
18
|
+
flushed: Promise<void>;
|
|
19
|
+
markFlushed: () => void;
|
|
20
|
+
}
|
|
21
|
+
const scope = new AsyncLocalStorage<RequestScope>();
|
|
22
|
+
|
|
23
|
+
let inflight = new Set<Promise<unknown>>();
|
|
24
|
+
/**
|
|
25
|
+
* Flush promises for requests whose body has not finished streaming.
|
|
26
|
+
*
|
|
27
|
+
* Tracked separately from `inflight` because a task is not running yet while
|
|
28
|
+
* its response is still streaming — a shutdown that only waits on running
|
|
29
|
+
* work would return while queued work was still pending.
|
|
30
|
+
*/
|
|
31
|
+
let unflushed = new Set<Promise<void>>();
|
|
32
|
+
let shuttingDown = false;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Run work after the response for the current request has been flushed.
|
|
36
|
+
*
|
|
37
|
+
* Does not block the response, and does not survive a restart — a task that
|
|
38
|
+
* has not finished is lost if the process exits. That makes this for work
|
|
39
|
+
* that is cheap and re-derivable: analytics, cache warming, a last-seen
|
|
40
|
+
* timestamp. Anything a user was promised belongs in a durable queue, and
|
|
41
|
+
* this is the call site to change when a job starts to matter.
|
|
42
|
+
*
|
|
43
|
+
* Errors are logged, never thrown. The response is already sent by the time
|
|
44
|
+
* a task runs, so a failure cannot be reported to anyone.
|
|
45
|
+
*
|
|
46
|
+
* Outside a request (at startup, in `init`) the task runs on the next
|
|
47
|
+
* microtask instead. That keeps the call valid anywhere without pretending
|
|
48
|
+
* there was a response to wait for.
|
|
49
|
+
*/
|
|
50
|
+
export function afterResponse(fn: () => unknown): Promise<unknown> {
|
|
51
|
+
if (shuttingDown) {
|
|
52
|
+
return Promise.reject(
|
|
53
|
+
new Error("afterResponse() called during shutdown"),
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Wrap so a synchronous throw inside fn becomes a rejection instead of
|
|
58
|
+
// propagating out of the caller's await.
|
|
59
|
+
const run = async () => {
|
|
60
|
+
try {
|
|
61
|
+
await fn();
|
|
62
|
+
} catch (e) {
|
|
63
|
+
logger.error("afterResponse task failed:", e);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const current = scope.getStore();
|
|
68
|
+
if (!current) {
|
|
69
|
+
return track(run());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
current.tasks.push(() => track(run()));
|
|
73
|
+
return Promise.resolve();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function deferred(): { flushed: Promise<void>; markFlushed: () => void } {
|
|
77
|
+
let mark!: () => void;
|
|
78
|
+
const flushed = new Promise<void>((resolve) => {
|
|
79
|
+
mark = resolve;
|
|
80
|
+
});
|
|
81
|
+
return { flushed, markFlushed: mark };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function track(p: Promise<unknown>): Promise<unknown> {
|
|
85
|
+
inflight.add(p);
|
|
86
|
+
p.finally(() => inflight.delete(p));
|
|
87
|
+
return p;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Run a handler inside a request scope, then fire the collected tasks once
|
|
92
|
+
* the response body has finished streaming.
|
|
93
|
+
*
|
|
94
|
+
* Wrapping the body is what makes "after the response" true for streaming
|
|
95
|
+
* responses: scheduling on the handler's return would run the task while
|
|
96
|
+
* the client is still receiving. Firing on the stream's end means a slow
|
|
97
|
+
* download does not start the work early.
|
|
98
|
+
*/
|
|
99
|
+
export async function withAfterResponse(
|
|
100
|
+
req: Request,
|
|
101
|
+
handler: (req: Request) => Response | Promise<Response>,
|
|
102
|
+
): Promise<Response> {
|
|
103
|
+
const requestScope: RequestScope = { tasks: [], ...deferred() };
|
|
104
|
+
unflushed.add(requestScope.flushed);
|
|
105
|
+
requestScope.flushed.finally(() => unflushed.delete(requestScope.flushed));
|
|
106
|
+
const response = await scope.run(requestScope, () => handler(req));
|
|
107
|
+
|
|
108
|
+
const fire = () => {
|
|
109
|
+
requestScope.markFlushed();
|
|
110
|
+
if (requestScope.tasks.length === 0) return;
|
|
111
|
+
// Copy first: a task may register another task.
|
|
112
|
+
const tasks = requestScope.tasks.splice(0);
|
|
113
|
+
for (const t of tasks) t();
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
if (!(response instanceof Response) || response.body === null) {
|
|
117
|
+
fire();
|
|
118
|
+
return response as Response;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const { readable, writable } = new TransformStream();
|
|
122
|
+
// Errors on the original body propagate, and the tasks still run: a
|
|
123
|
+
// client disconnecting mid-stream must not silently skip the work.
|
|
124
|
+
Promise.resolve(response.body)
|
|
125
|
+
.then((body) => body?.pipeTo(writable))
|
|
126
|
+
.catch(() => writable.abort())
|
|
127
|
+
.finally(fire);
|
|
128
|
+
|
|
129
|
+
return new Response(readable, {
|
|
130
|
+
status: response.status,
|
|
131
|
+
statusText: response.statusText,
|
|
132
|
+
headers: response.headers,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Wait for in-flight afterResponse tasks so a graceful shutdown does not
|
|
138
|
+
* abandon work already started. Subsequent registrations are rejected.
|
|
139
|
+
*/
|
|
140
|
+
export async function drainAfterResponse(timeoutMs = 5000): Promise<void> {
|
|
141
|
+
shuttingDown = true;
|
|
142
|
+
|
|
143
|
+
const deadline = Date.now() + timeoutMs;
|
|
144
|
+
const timedOut = Symbol("timeout");
|
|
145
|
+
|
|
146
|
+
const waitFor = async <T>(work: Promise<T>): Promise<T | typeof timedOut> => {
|
|
147
|
+
const left = deadline - Date.now();
|
|
148
|
+
if (left <= 0) return timedOut;
|
|
149
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
150
|
+
const result = await Promise.race([
|
|
151
|
+
work,
|
|
152
|
+
new Promise<typeof timedOut>((resolve) => {
|
|
153
|
+
timer = setTimeout(() => resolve(timedOut), left);
|
|
154
|
+
}),
|
|
155
|
+
]);
|
|
156
|
+
if (timer) clearTimeout(timer);
|
|
157
|
+
return result;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// Two waits, not one. A task registered during a request is not in
|
|
161
|
+
// `inflight` until the response body has flushed and it actually
|
|
162
|
+
// starts, so draining only that set would return while a response was
|
|
163
|
+
// still streaming and its work was still queued.
|
|
164
|
+
const flushed = await waitFor(Promise.allSettled([...unflushed]));
|
|
165
|
+
if (flushed === timedOut) {
|
|
166
|
+
logger.warn(
|
|
167
|
+
`responses were still streaming after ${timeoutMs}ms; ` +
|
|
168
|
+
`afterResponse tasks for them will be lost`,
|
|
169
|
+
);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (inflight.size === 0) {
|
|
174
|
+
unflushed.clear();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
logger.info(`waiting for ${inflight.size} afterResponse task(s)...`);
|
|
179
|
+
const result = await waitFor(Promise.allSettled([...inflight]));
|
|
180
|
+
if (result === timedOut) {
|
|
181
|
+
logger.warn(
|
|
182
|
+
`afterResponse tasks did not finish within ${timeoutMs}ms, abandoning them`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
inflight.clear();
|
|
186
|
+
unflushed.clear();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Test-only: reset module state between cases. */
|
|
190
|
+
export function _resetAfterResponseForTest(): void {
|
|
191
|
+
inflight = new Set();
|
|
192
|
+
unflushed = new Set();
|
|
193
|
+
shuttingDown = false;
|
|
194
|
+
}
|
|
@@ -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
|
+
};
|
|
@@ -202,6 +202,16 @@ export async function startCoordinator(options: CoordinatorOptions) {
|
|
|
202
202
|
let globalIndex = 0;
|
|
203
203
|
let nextPort = port + 1;
|
|
204
204
|
|
|
205
|
+
// Total workers across every mode, known before the first spawn. A
|
|
206
|
+
// plugin that creates a per-worker resource needs the whole picture to
|
|
207
|
+
// divide a global budget (a connection pool of N across W workers is
|
|
208
|
+
// N/W each) — and the index alone cannot tell it how many siblings
|
|
209
|
+
// exist. Without this, `max: 20` silently means 20 *per worker*.
|
|
210
|
+
const workerCount = Object.values(scale).reduce<number>(
|
|
211
|
+
(sum, n) => sum + (n ?? 0),
|
|
212
|
+
0,
|
|
213
|
+
);
|
|
214
|
+
|
|
205
215
|
for (const [mode, count = 0] of Object.entries(scale)) {
|
|
206
216
|
for (let i = 0; i < count; i++) {
|
|
207
217
|
const workerPort = nextPort++;
|
|
@@ -227,6 +237,9 @@ export async function startCoordinator(options: CoordinatorOptions) {
|
|
|
227
237
|
...publicEnvVars,
|
|
228
238
|
GRIMOIRE_MODE: mode,
|
|
229
239
|
GRIMOIRE_WORKER_INDEX: String(i),
|
|
240
|
+
// The sibling count, so a per-worker resource can divide a global
|
|
241
|
+
// budget instead of taking the whole of it.
|
|
242
|
+
GRIMOIRE_WORKER_COUNT: String(workerCount),
|
|
230
243
|
GRIMOIRE_WORKER_PORT: String(workerPort),
|
|
231
244
|
GRIMOIRE_INTERNAL_SECRET: secret,
|
|
232
245
|
GRIMOIRE_WORKER_NAME: descriptor.name ?? `${mode}-${i}`,
|
|
@@ -293,13 +306,17 @@ export async function startCoordinator(options: CoordinatorOptions) {
|
|
|
293
306
|
const serialized =
|
|
294
307
|
(await runSerializeLocals(plugins, locals)) ??
|
|
295
308
|
defaultSerialize(locals, secret);
|
|
296
|
-
// the
|
|
297
|
-
// ours under a different case, both
|
|
298
|
-
// comma, and the secret check refused
|
|
309
|
+
// Only the two headers the coordinator writes itself are dropped from
|
|
310
|
+
// the client's: spread next to ours under a different case, both
|
|
311
|
+
// reached the worker joined by a comma, and the secret check refused
|
|
312
|
+
// the request. The rest of x-grimoire-* is the client talking to the
|
|
313
|
+
// worker: x-grimoire-navigate asks for a page's data as JSON and
|
|
314
|
+
// x-grimoire-form marks an enhanced submit. Dropping them made every
|
|
315
|
+
// client-side navigation come back as a full HTML page, so the router
|
|
316
|
+
// reloaded the document on each click.
|
|
299
317
|
const forwardHeaders = new Headers();
|
|
300
318
|
for (const [k, v] of req.headers) {
|
|
301
|
-
if (!k.toLowerCase().
|
|
302
|
-
forwardHeaders.set(k, v);
|
|
319
|
+
if (!INTERNAL_HEADERS.has(k.toLowerCase())) forwardHeaders.set(k, v);
|
|
303
320
|
}
|
|
304
321
|
forwardHeaders.set("X-Grimoire-Locals", serialized);
|
|
305
322
|
forwardHeaders.set("X-Grimoire-Internal", secret);
|
|
@@ -394,6 +411,9 @@ interface WsProxyData {
|
|
|
394
411
|
upstream?: WebSocket;
|
|
395
412
|
}
|
|
396
413
|
|
|
414
|
+
/** Headers the coordinator sets itself; a client's copy is never forwarded. */
|
|
415
|
+
const INTERNAL_HEADERS = new Set(["x-grimoire-locals", "x-grimoire-internal"]);
|
|
416
|
+
|
|
397
417
|
/** Headers for the coordinator's own socket to the worker. */
|
|
398
418
|
function upstreamWsHeaders(
|
|
399
419
|
req: Request,
|
|
@@ -405,7 +425,7 @@ function upstreamWsHeaders(
|
|
|
405
425
|
// the WebSocket client writes its own handshake headers
|
|
406
426
|
if (
|
|
407
427
|
key.startsWith("sec-websocket-") ||
|
|
408
|
-
|
|
428
|
+
INTERNAL_HEADERS.has(key) ||
|
|
409
429
|
key === "upgrade" ||
|
|
410
430
|
key === "connection" ||
|
|
411
431
|
key === "host"
|