@sigil-dev/grimoire 0.9.28 → 0.9.30
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/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
2
|
-
import { transformSync } from "@babel/core";
|
|
3
|
-
import sigilPlugin from "@sigil-dev/compiler/babel";
|
|
4
|
-
import {
|
|
5
|
-
clientStyleSnippet,
|
|
6
|
-
computeHash,
|
|
7
|
-
extractStyles,
|
|
8
|
-
} from "@sigil-dev/compiler/css";
|
|
9
|
-
import type { GrimoirePlugin } from "../types";
|
|
10
|
-
import type { RouteFile } from "./scanner";
|
|
11
|
-
|
|
12
|
-
function rewriteRelativeImports(filePath: string) {
|
|
13
|
-
const basedir = dirname(filePath);
|
|
14
|
-
const rewrite = (source?: { value: string }) => {
|
|
15
|
-
if (!source?.value.startsWith(".")) return;
|
|
16
|
-
source.value = resolve(basedir, source.value);
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
name: "sigil-rewrite-relative-route-imports",
|
|
21
|
-
visitor: {
|
|
22
|
-
ImportDeclaration(path: any) {
|
|
23
|
-
rewrite(path.node.source);
|
|
24
|
-
},
|
|
25
|
-
ExportNamedDeclaration(path: any) {
|
|
26
|
-
rewrite(path.node.source);
|
|
27
|
-
},
|
|
28
|
-
ExportAllDeclaration(path: any) {
|
|
29
|
-
rewrite(path.node.source);
|
|
30
|
-
},
|
|
31
|
-
CallExpression(path: any) {
|
|
32
|
-
if (path.node.callee.type !== "Import") return;
|
|
33
|
-
rewrite(path.node.arguments[0]);
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Pre-transforms route .tsx/.jsx files to plain JS using the Babel sigil
|
|
41
|
-
* pipeline, writing the output to `outDir`. Returns a Map from the original
|
|
42
|
-
* filePath to the compiled .js path.
|
|
43
|
-
*
|
|
44
|
-
* Workaround for Bun 1.3.13: Bun.build's native parser runs before onLoad
|
|
45
|
-
* hooks fire, crashing on TypeScript syntax. Pre-transforming to JS means
|
|
46
|
-
* Bun.build only ever sees plain JavaScript.
|
|
47
|
-
*/
|
|
48
|
-
export async function transformRoutes(
|
|
49
|
-
routes: RouteFile[],
|
|
50
|
-
outDir: string,
|
|
51
|
-
mode: "hydrate" | "dom",
|
|
52
|
-
plugins: GrimoirePlugin[] = [],
|
|
53
|
-
): Promise<Map<string, string>> {
|
|
54
|
-
const map = new Map<string, string>();
|
|
55
|
-
// loader: "ts" — Babel already consumed JSX, Bun only strips remaining types
|
|
56
|
-
const transpiler = new Bun.Transpiler({ loader: "ts", target: "browser" });
|
|
57
|
-
|
|
58
|
-
await Promise.all(
|
|
59
|
-
routes.map(async (route, index) => {
|
|
60
|
-
// scope classes only where there is scoped CSS to match. The CSS
|
|
61
|
-
// used to be dropped here: a route's own <style> never reached
|
|
62
|
-
// the browser build
|
|
63
|
-
const styles = extractStyles(
|
|
64
|
-
await Bun.file(route.filePath).text(),
|
|
65
|
-
computeHash(route.filePath),
|
|
66
|
-
route.filePath,
|
|
67
|
-
);
|
|
68
|
-
const code = styles.code;
|
|
69
|
-
const hash = styles.css ? computeHash(route.filePath) : undefined;
|
|
70
|
-
|
|
71
|
-
const res = transformSync(code, {
|
|
72
|
-
configFile: false,
|
|
73
|
-
babelrc: false,
|
|
74
|
-
parserOpts: {
|
|
75
|
-
plugins: ["typescript", "jsx"], // no isTSX
|
|
76
|
-
},
|
|
77
|
-
plugins: [
|
|
78
|
-
[sigilPlugin, { hash, mode }],
|
|
79
|
-
//@ts-expect-error it fills what it needs
|
|
80
|
-
rewriteRelativeImports(route.filePath),
|
|
81
|
-
],
|
|
82
|
-
filename: route.filePath,
|
|
83
|
-
});
|
|
84
|
-
let out = transpiler.transformSync(res?.code ?? "");
|
|
85
|
-
if (styles.css && hash) out = clientStyleSnippet(hash, styles.css) + out;
|
|
86
|
-
for (const plugin of plugins) {
|
|
87
|
-
if (plugin.transform)
|
|
88
|
-
out = (await plugin.transform(out, route.filePath)) ?? out;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const rel = relative(process.cwd(), route.filePath)
|
|
92
|
-
.replace(/\.tsx?$/, "")
|
|
93
|
-
.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
94
|
-
const nameBase = rel || basename(route.filePath).replace(/\.[jt]sx$/, "");
|
|
95
|
-
const outPath = join(outDir, `${index}-${nameBase}.${mode}.js`);
|
|
96
|
-
|
|
97
|
-
await Bun.write(outPath, out);
|
|
98
|
-
map.set(route.filePath, outPath);
|
|
99
|
-
}),
|
|
100
|
-
);
|
|
101
|
-
return map;
|
|
102
|
-
}
|
|
1
|
+
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
2
|
+
import { transformSync } from "@babel/core";
|
|
3
|
+
import sigilPlugin from "@sigil-dev/compiler/babel";
|
|
4
|
+
import {
|
|
5
|
+
clientStyleSnippet,
|
|
6
|
+
computeHash,
|
|
7
|
+
extractStyles,
|
|
8
|
+
} from "@sigil-dev/compiler/css";
|
|
9
|
+
import type { GrimoirePlugin } from "../types";
|
|
10
|
+
import type { RouteFile } from "./scanner";
|
|
11
|
+
|
|
12
|
+
function rewriteRelativeImports(filePath: string) {
|
|
13
|
+
const basedir = dirname(filePath);
|
|
14
|
+
const rewrite = (source?: { value: string }) => {
|
|
15
|
+
if (!source?.value.startsWith(".")) return;
|
|
16
|
+
source.value = resolve(basedir, source.value);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
name: "sigil-rewrite-relative-route-imports",
|
|
21
|
+
visitor: {
|
|
22
|
+
ImportDeclaration(path: any) {
|
|
23
|
+
rewrite(path.node.source);
|
|
24
|
+
},
|
|
25
|
+
ExportNamedDeclaration(path: any) {
|
|
26
|
+
rewrite(path.node.source);
|
|
27
|
+
},
|
|
28
|
+
ExportAllDeclaration(path: any) {
|
|
29
|
+
rewrite(path.node.source);
|
|
30
|
+
},
|
|
31
|
+
CallExpression(path: any) {
|
|
32
|
+
if (path.node.callee.type !== "Import") return;
|
|
33
|
+
rewrite(path.node.arguments[0]);
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Pre-transforms route .tsx/.jsx files to plain JS using the Babel sigil
|
|
41
|
+
* pipeline, writing the output to `outDir`. Returns a Map from the original
|
|
42
|
+
* filePath to the compiled .js path.
|
|
43
|
+
*
|
|
44
|
+
* Workaround for Bun 1.3.13: Bun.build's native parser runs before onLoad
|
|
45
|
+
* hooks fire, crashing on TypeScript syntax. Pre-transforming to JS means
|
|
46
|
+
* Bun.build only ever sees plain JavaScript.
|
|
47
|
+
*/
|
|
48
|
+
export async function transformRoutes(
|
|
49
|
+
routes: RouteFile[],
|
|
50
|
+
outDir: string,
|
|
51
|
+
mode: "hydrate" | "dom",
|
|
52
|
+
plugins: GrimoirePlugin[] = [],
|
|
53
|
+
): Promise<Map<string, string>> {
|
|
54
|
+
const map = new Map<string, string>();
|
|
55
|
+
// loader: "ts" — Babel already consumed JSX, Bun only strips remaining types
|
|
56
|
+
const transpiler = new Bun.Transpiler({ loader: "ts", target: "browser" });
|
|
57
|
+
|
|
58
|
+
await Promise.all(
|
|
59
|
+
routes.map(async (route, index) => {
|
|
60
|
+
// scope classes only where there is scoped CSS to match. The CSS
|
|
61
|
+
// used to be dropped here: a route's own <style> never reached
|
|
62
|
+
// the browser build
|
|
63
|
+
const styles = extractStyles(
|
|
64
|
+
await Bun.file(route.filePath).text(),
|
|
65
|
+
computeHash(route.filePath),
|
|
66
|
+
route.filePath,
|
|
67
|
+
);
|
|
68
|
+
const code = styles.code;
|
|
69
|
+
const hash = styles.css ? computeHash(route.filePath) : undefined;
|
|
70
|
+
|
|
71
|
+
const res = transformSync(code, {
|
|
72
|
+
configFile: false,
|
|
73
|
+
babelrc: false,
|
|
74
|
+
parserOpts: {
|
|
75
|
+
plugins: ["typescript", "jsx"], // no isTSX
|
|
76
|
+
},
|
|
77
|
+
plugins: [
|
|
78
|
+
[sigilPlugin, { hash, mode }],
|
|
79
|
+
//@ts-expect-error it fills what it needs
|
|
80
|
+
rewriteRelativeImports(route.filePath),
|
|
81
|
+
],
|
|
82
|
+
filename: route.filePath,
|
|
83
|
+
});
|
|
84
|
+
let out = transpiler.transformSync(res?.code ?? "");
|
|
85
|
+
if (styles.css && hash) out = clientStyleSnippet(hash, styles.css) + out;
|
|
86
|
+
for (const plugin of plugins) {
|
|
87
|
+
if (plugin.transform)
|
|
88
|
+
out = (await plugin.transform(out, route.filePath)) ?? out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const rel = relative(process.cwd(), route.filePath)
|
|
92
|
+
.replace(/\.tsx?$/, "")
|
|
93
|
+
.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
94
|
+
const nameBase = rel || basename(route.filePath).replace(/\.[jt]sx$/, "");
|
|
95
|
+
const outPath = join(outDir, `${index}-${nameBase}.${mode}.js`);
|
|
96
|
+
|
|
97
|
+
await Bun.write(outPath, out);
|
|
98
|
+
map.set(route.filePath, outPath);
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
return map;
|
|
102
|
+
}
|
|
@@ -1,206 +1,206 @@
|
|
|
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(new Error("afterResponse() called during shutdown"));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Wrap so a synchronous throw inside fn becomes a rejection instead of
|
|
56
|
-
// propagating out of the caller's await.
|
|
57
|
-
const run = async () => {
|
|
58
|
-
try {
|
|
59
|
-
await fn();
|
|
60
|
-
} catch (e) {
|
|
61
|
-
logger.error("afterResponse task failed:", e);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const current = scope.getStore();
|
|
66
|
-
if (!current) {
|
|
67
|
-
return track(run());
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
current.tasks.push(() => track(run()));
|
|
71
|
-
return Promise.resolve();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function deferred(): { flushed: Promise<void>; markFlushed: () => void } {
|
|
75
|
-
let mark!: () => void;
|
|
76
|
-
const flushed = new Promise<void>((resolve) => {
|
|
77
|
-
mark = resolve;
|
|
78
|
-
});
|
|
79
|
-
return { flushed, markFlushed: mark };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function track(p: Promise<unknown>): Promise<unknown> {
|
|
83
|
-
inflight.add(p);
|
|
84
|
-
p.finally(() => inflight.delete(p));
|
|
85
|
-
return p;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Run a handler inside a request scope, then fire the collected tasks once
|
|
90
|
-
* the response body has finished streaming.
|
|
91
|
-
*
|
|
92
|
-
* Wrapping the body is what makes "after the response" true for streaming
|
|
93
|
-
* responses: scheduling on the handler's return would run the task while
|
|
94
|
-
* the client is still receiving. Firing on the stream's end means a slow
|
|
95
|
-
* download does not start the work early.
|
|
96
|
-
*/
|
|
97
|
-
export async function withAfterResponse(
|
|
98
|
-
req: Request,
|
|
99
|
-
handler: (req: Request) => Response | Promise<Response>,
|
|
100
|
-
): Promise<Response> {
|
|
101
|
-
const requestScope: RequestScope = { tasks: [], ...deferred() };
|
|
102
|
-
unflushed.add(requestScope.flushed);
|
|
103
|
-
requestScope.flushed.finally(() => unflushed.delete(requestScope.flushed));
|
|
104
|
-
const response = await scope.run(requestScope, () => handler(req));
|
|
105
|
-
|
|
106
|
-
const fire = () => {
|
|
107
|
-
requestScope.markFlushed();
|
|
108
|
-
if (requestScope.tasks.length === 0) return;
|
|
109
|
-
// Copy first: a task may register another task.
|
|
110
|
-
const tasks = requestScope.tasks.splice(0);
|
|
111
|
-
for (const t of tasks) t();
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
if (!(response instanceof Response)) {
|
|
115
|
-
fire();
|
|
116
|
-
return response as Response;
|
|
117
|
-
}
|
|
118
|
-
// A body Bun types itself (a string, a file, a blob) has no Content-Type
|
|
119
|
-
// in response.headers: Bun adds it when sending. Merely reading
|
|
120
|
-
// response.body turns it into a stream that Bun then sends untyped,
|
|
121
|
-
// which browsers treat as a download, so check the headers first and
|
|
122
|
-
// leave such a response alone. Its body is complete already: run the
|
|
123
|
-
// tasks on the next turn, once it has gone out.
|
|
124
|
-
if (!response.headers.has("content-type")) {
|
|
125
|
-
setTimeout(fire, 0);
|
|
126
|
-
return response;
|
|
127
|
-
}
|
|
128
|
-
if (response.body === null) {
|
|
129
|
-
fire();
|
|
130
|
-
return response;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const { readable, writable } = new TransformStream();
|
|
134
|
-
// Errors on the original body propagate, and the tasks still run: a
|
|
135
|
-
// client disconnecting mid-stream must not silently skip the work.
|
|
136
|
-
Promise.resolve(response.body)
|
|
137
|
-
.then((body) => body?.pipeTo(writable))
|
|
138
|
-
.catch(() => writable.abort())
|
|
139
|
-
.finally(fire);
|
|
140
|
-
|
|
141
|
-
return new Response(readable, {
|
|
142
|
-
status: response.status,
|
|
143
|
-
statusText: response.statusText,
|
|
144
|
-
headers: response.headers,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Wait for in-flight afterResponse tasks so a graceful shutdown does not
|
|
150
|
-
* abandon work already started. Subsequent registrations are rejected.
|
|
151
|
-
*/
|
|
152
|
-
export async function drainAfterResponse(timeoutMs = 5000): Promise<void> {
|
|
153
|
-
shuttingDown = true;
|
|
154
|
-
|
|
155
|
-
const deadline = Date.now() + timeoutMs;
|
|
156
|
-
const timedOut = Symbol("timeout");
|
|
157
|
-
|
|
158
|
-
const waitFor = async <T>(work: Promise<T>): Promise<T | typeof timedOut> => {
|
|
159
|
-
const left = deadline - Date.now();
|
|
160
|
-
if (left <= 0) return timedOut;
|
|
161
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
162
|
-
const result = await Promise.race([
|
|
163
|
-
work,
|
|
164
|
-
new Promise<typeof timedOut>((resolve) => {
|
|
165
|
-
timer = setTimeout(() => resolve(timedOut), left);
|
|
166
|
-
}),
|
|
167
|
-
]);
|
|
168
|
-
if (timer) clearTimeout(timer);
|
|
169
|
-
return result;
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
// Two waits, not one. A task registered during a request is not in
|
|
173
|
-
// `inflight` until the response body has flushed and it actually
|
|
174
|
-
// starts, so draining only that set would return while a response was
|
|
175
|
-
// still streaming and its work was still queued.
|
|
176
|
-
const flushed = await waitFor(Promise.allSettled([...unflushed]));
|
|
177
|
-
if (flushed === timedOut) {
|
|
178
|
-
logger.warn(
|
|
179
|
-
`responses were still streaming after ${timeoutMs}ms; ` +
|
|
180
|
-
`afterResponse tasks for them will be lost`,
|
|
181
|
-
);
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (inflight.size === 0) {
|
|
186
|
-
unflushed.clear();
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
logger.info(`waiting for ${inflight.size} afterResponse task(s)...`);
|
|
191
|
-
const result = await waitFor(Promise.allSettled([...inflight]));
|
|
192
|
-
if (result === timedOut) {
|
|
193
|
-
logger.warn(
|
|
194
|
-
`afterResponse tasks did not finish within ${timeoutMs}ms, abandoning them`,
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
inflight.clear();
|
|
198
|
-
unflushed.clear();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/** Test-only: reset module state between cases. */
|
|
202
|
-
export function _resetAfterResponseForTest(): void {
|
|
203
|
-
inflight = new Set();
|
|
204
|
-
unflushed = new Set();
|
|
205
|
-
shuttingDown = false;
|
|
206
|
-
}
|
|
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(new Error("afterResponse() called during shutdown"));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Wrap so a synchronous throw inside fn becomes a rejection instead of
|
|
56
|
+
// propagating out of the caller's await.
|
|
57
|
+
const run = async () => {
|
|
58
|
+
try {
|
|
59
|
+
await fn();
|
|
60
|
+
} catch (e) {
|
|
61
|
+
logger.error("afterResponse task failed:", e);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const current = scope.getStore();
|
|
66
|
+
if (!current) {
|
|
67
|
+
return track(run());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
current.tasks.push(() => track(run()));
|
|
71
|
+
return Promise.resolve();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function deferred(): { flushed: Promise<void>; markFlushed: () => void } {
|
|
75
|
+
let mark!: () => void;
|
|
76
|
+
const flushed = new Promise<void>((resolve) => {
|
|
77
|
+
mark = resolve;
|
|
78
|
+
});
|
|
79
|
+
return { flushed, markFlushed: mark };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function track(p: Promise<unknown>): Promise<unknown> {
|
|
83
|
+
inflight.add(p);
|
|
84
|
+
p.finally(() => inflight.delete(p));
|
|
85
|
+
return p;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Run a handler inside a request scope, then fire the collected tasks once
|
|
90
|
+
* the response body has finished streaming.
|
|
91
|
+
*
|
|
92
|
+
* Wrapping the body is what makes "after the response" true for streaming
|
|
93
|
+
* responses: scheduling on the handler's return would run the task while
|
|
94
|
+
* the client is still receiving. Firing on the stream's end means a slow
|
|
95
|
+
* download does not start the work early.
|
|
96
|
+
*/
|
|
97
|
+
export async function withAfterResponse(
|
|
98
|
+
req: Request,
|
|
99
|
+
handler: (req: Request) => Response | Promise<Response>,
|
|
100
|
+
): Promise<Response> {
|
|
101
|
+
const requestScope: RequestScope = { tasks: [], ...deferred() };
|
|
102
|
+
unflushed.add(requestScope.flushed);
|
|
103
|
+
requestScope.flushed.finally(() => unflushed.delete(requestScope.flushed));
|
|
104
|
+
const response = await scope.run(requestScope, () => handler(req));
|
|
105
|
+
|
|
106
|
+
const fire = () => {
|
|
107
|
+
requestScope.markFlushed();
|
|
108
|
+
if (requestScope.tasks.length === 0) return;
|
|
109
|
+
// Copy first: a task may register another task.
|
|
110
|
+
const tasks = requestScope.tasks.splice(0);
|
|
111
|
+
for (const t of tasks) t();
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
if (!(response instanceof Response)) {
|
|
115
|
+
fire();
|
|
116
|
+
return response as Response;
|
|
117
|
+
}
|
|
118
|
+
// A body Bun types itself (a string, a file, a blob) has no Content-Type
|
|
119
|
+
// in response.headers: Bun adds it when sending. Merely reading
|
|
120
|
+
// response.body turns it into a stream that Bun then sends untyped,
|
|
121
|
+
// which browsers treat as a download, so check the headers first and
|
|
122
|
+
// leave such a response alone. Its body is complete already: run the
|
|
123
|
+
// tasks on the next turn, once it has gone out.
|
|
124
|
+
if (!response.headers.has("content-type")) {
|
|
125
|
+
setTimeout(fire, 0);
|
|
126
|
+
return response;
|
|
127
|
+
}
|
|
128
|
+
if (response.body === null) {
|
|
129
|
+
fire();
|
|
130
|
+
return response;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const { readable, writable } = new TransformStream();
|
|
134
|
+
// Errors on the original body propagate, and the tasks still run: a
|
|
135
|
+
// client disconnecting mid-stream must not silently skip the work.
|
|
136
|
+
Promise.resolve(response.body)
|
|
137
|
+
.then((body) => body?.pipeTo(writable))
|
|
138
|
+
.catch(() => writable.abort())
|
|
139
|
+
.finally(fire);
|
|
140
|
+
|
|
141
|
+
return new Response(readable, {
|
|
142
|
+
status: response.status,
|
|
143
|
+
statusText: response.statusText,
|
|
144
|
+
headers: response.headers,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Wait for in-flight afterResponse tasks so a graceful shutdown does not
|
|
150
|
+
* abandon work already started. Subsequent registrations are rejected.
|
|
151
|
+
*/
|
|
152
|
+
export async function drainAfterResponse(timeoutMs = 5000): Promise<void> {
|
|
153
|
+
shuttingDown = true;
|
|
154
|
+
|
|
155
|
+
const deadline = Date.now() + timeoutMs;
|
|
156
|
+
const timedOut = Symbol("timeout");
|
|
157
|
+
|
|
158
|
+
const waitFor = async <T>(work: Promise<T>): Promise<T | typeof timedOut> => {
|
|
159
|
+
const left = deadline - Date.now();
|
|
160
|
+
if (left <= 0) return timedOut;
|
|
161
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
162
|
+
const result = await Promise.race([
|
|
163
|
+
work,
|
|
164
|
+
new Promise<typeof timedOut>((resolve) => {
|
|
165
|
+
timer = setTimeout(() => resolve(timedOut), left);
|
|
166
|
+
}),
|
|
167
|
+
]);
|
|
168
|
+
if (timer) clearTimeout(timer);
|
|
169
|
+
return result;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// Two waits, not one. A task registered during a request is not in
|
|
173
|
+
// `inflight` until the response body has flushed and it actually
|
|
174
|
+
// starts, so draining only that set would return while a response was
|
|
175
|
+
// still streaming and its work was still queued.
|
|
176
|
+
const flushed = await waitFor(Promise.allSettled([...unflushed]));
|
|
177
|
+
if (flushed === timedOut) {
|
|
178
|
+
logger.warn(
|
|
179
|
+
`responses were still streaming after ${timeoutMs}ms; ` +
|
|
180
|
+
`afterResponse tasks for them will be lost`,
|
|
181
|
+
);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (inflight.size === 0) {
|
|
186
|
+
unflushed.clear();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
logger.info(`waiting for ${inflight.size} afterResponse task(s)...`);
|
|
191
|
+
const result = await waitFor(Promise.allSettled([...inflight]));
|
|
192
|
+
if (result === timedOut) {
|
|
193
|
+
logger.warn(
|
|
194
|
+
`afterResponse tasks did not finish within ${timeoutMs}ms, abandoning them`,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
inflight.clear();
|
|
198
|
+
unflushed.clear();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Test-only: reset module state between cases. */
|
|
202
|
+
export function _resetAfterResponseForTest(): void {
|
|
203
|
+
inflight = new Set();
|
|
204
|
+
unflushed = new Set();
|
|
205
|
+
shuttingDown = false;
|
|
206
|
+
}
|