@sigil-dev/grimoire 0.9.29 → 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
package/src/types.ts
CHANGED
|
@@ -14,7 +14,11 @@ export interface CliCommand {
|
|
|
14
14
|
command(nameAndArgs: string, opts?: { hidden?: boolean }): CliCommand;
|
|
15
15
|
description(text: string): CliCommand;
|
|
16
16
|
alias(alias: string): CliCommand;
|
|
17
|
-
option(
|
|
17
|
+
option(
|
|
18
|
+
flags: string,
|
|
19
|
+
description?: string,
|
|
20
|
+
defaultValue?: unknown,
|
|
21
|
+
): CliCommand;
|
|
18
22
|
argument(name: string, description?: string): CliCommand;
|
|
19
23
|
addHelpText(position: "before" | "after", text: string): CliCommand;
|
|
20
24
|
action(fn: (...args: never[]) => unknown): CliCommand;
|
|
@@ -108,7 +112,9 @@ export interface WorkerContext {
|
|
|
108
112
|
dev: boolean;
|
|
109
113
|
}
|
|
110
114
|
|
|
111
|
-
export function workerContext(
|
|
115
|
+
export function workerContext(
|
|
116
|
+
env: Record<string, string | undefined> = process.env,
|
|
117
|
+
): WorkerContext {
|
|
112
118
|
const parsed = Number(env.GRIMOIRE_WORKER_INDEX);
|
|
113
119
|
const count = Number(env.GRIMOIRE_WORKER_COUNT);
|
|
114
120
|
return {
|
|
@@ -135,9 +141,46 @@ export interface ServicesContext {
|
|
|
135
141
|
services: Record<string, unknown>;
|
|
136
142
|
}
|
|
137
143
|
|
|
138
|
-
|
|
144
|
+
/**
|
|
145
|
+
* A Grimoire plugin. `L` is what it puts on `locals`: declare it and the
|
|
146
|
+
* app's `App.Locals` gets it from sigil.config.ts, with no app.d.ts.
|
|
147
|
+
*
|
|
148
|
+
* ```ts
|
|
149
|
+
* const plugin: GrimoirePlugin<{ user: User | null }> = { name: "auth", handle };
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export interface GrimoirePlugin<L extends object = {}> {
|
|
139
153
|
name: string;
|
|
140
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Type only, never set: what this plugin puts on `locals`. The generated
|
|
157
|
+
* `.grimoire/types/locals.d.ts` merges every registered plugin's into
|
|
158
|
+
* `App.Locals`.
|
|
159
|
+
*/
|
|
160
|
+
readonly $locals?: L;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Plugins this one runs after, by name: its `services()` sees what they
|
|
164
|
+
* published, and its `handle` runs after theirs. A trailing `*` matches
|
|
165
|
+
* by prefix (`"sql:*"`). Names that are not registered are ignored.
|
|
166
|
+
*/
|
|
167
|
+
after?: readonly string[];
|
|
168
|
+
/** Plugins this one runs before, by name; the mirror of `after`. */
|
|
169
|
+
before?: readonly string[];
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Plugins that come with this one: tome brings the plugin-sql database it
|
|
173
|
+
* runs on. They are registered as if listed, before this one. A plugin
|
|
174
|
+
* listed in the config under the same name wins over an included one.
|
|
175
|
+
*/
|
|
176
|
+
readonly includes?: readonly GrimoirePlugin<any>[];
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Directories, project-relative, that hold server code only: importing
|
|
180
|
+
* from one into browser code fails, as `src/lib/server/` does.
|
|
181
|
+
*/
|
|
182
|
+
readonly serverOnly?: readonly string[];
|
|
183
|
+
|
|
141
184
|
/**
|
|
142
185
|
* Environment variables this plugin cannot run without. Checked when a
|
|
143
186
|
* server starts (not by `sigil build`); a missing or empty one fails the
|
|
@@ -169,15 +212,16 @@ export interface GrimoirePlugin {
|
|
|
169
212
|
*
|
|
170
213
|
* `ctx.services` is what the plugins before this one published, so a
|
|
171
214
|
* plugin can build on a resource another plugin owns instead of opening
|
|
172
|
-
* a second one.
|
|
215
|
+
* a second one. Declare `after: ["sql:db"]` and a consumer reads
|
|
173
216
|
* `ctx.services.db` — the same client, not a parallel pool.
|
|
174
217
|
*
|
|
175
|
-
* It is a snapshot taken before this plugin runs, so
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* open their own fallback.
|
|
218
|
+
* It is a snapshot taken before this plugin runs, so declare `after` for
|
|
219
|
+
* what it needs rather than relying on the list order. Fail loudly when
|
|
220
|
+
* the key is absent rather than quietly open your own fallback.
|
|
179
221
|
*/
|
|
180
|
-
services?(
|
|
222
|
+
services?(
|
|
223
|
+
ctx: ServicesContext,
|
|
224
|
+
): Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
181
225
|
|
|
182
226
|
// server lifecycle
|
|
183
227
|
// we dont care about what the impl puts as websocket data therefore unknown.
|
|
@@ -259,10 +303,9 @@ export interface GrimoirePlugin {
|
|
|
259
303
|
* that impossible without blocking module scope. Every call site awaits
|
|
260
304
|
* the result, so a returned Promise is never mistaken for a config.
|
|
261
305
|
*/
|
|
262
|
-
config?: (
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
| Promise<GrimoireConfig | undefined>;
|
|
306
|
+
config?: (
|
|
307
|
+
config: GrimoireConfig,
|
|
308
|
+
) => GrimoireConfig | undefined | Promise<GrimoireConfig | undefined>;
|
|
266
309
|
|
|
267
310
|
/**
|
|
268
311
|
* Register CLI subcommands for this plugin.
|
|
@@ -275,10 +318,7 @@ export interface GrimoirePlugin {
|
|
|
275
318
|
* Plugins without this hook are simply skipped. A plugin that throws here
|
|
276
319
|
* aborts the CLI with that error rather than failing obscurely later.
|
|
277
320
|
*/
|
|
278
|
-
cli?: (
|
|
279
|
-
program: CliCommand,
|
|
280
|
-
context: GrimoireConfig,
|
|
281
|
-
) => void | Promise<void>;
|
|
321
|
+
cli?: (program: CliCommand, context: GrimoireConfig) => void | Promise<void>;
|
|
282
322
|
|
|
283
323
|
// process lifecycle
|
|
284
324
|
|
|
@@ -371,6 +411,11 @@ export interface GrimoireConfig {
|
|
|
371
411
|
plugins?: GrimoirePlugin[];
|
|
372
412
|
routes?: string; // glob, default 'src/routes/**';
|
|
373
413
|
dev?: boolean;
|
|
414
|
+
/**
|
|
415
|
+
* Set by `createTestApp`: the app runs under a test. Plugins with side
|
|
416
|
+
* effects use it to stay in-process (mail lands in `mail.sent`, not SMTP).
|
|
417
|
+
*/
|
|
418
|
+
test?: boolean;
|
|
374
419
|
devEditor?: boolean;
|
|
375
420
|
vitePort?: number;
|
|
376
421
|
alias?: Record<string, string>; // e.g. { "$lib": "src/lib" } — resolved relative to cwd
|
|
@@ -420,10 +465,86 @@ export interface GrimoireConfig {
|
|
|
420
465
|
_warmHeaders?: Record<string, string>;
|
|
421
466
|
}
|
|
422
467
|
|
|
423
|
-
|
|
424
|
-
|
|
468
|
+
/**
|
|
469
|
+
* The project config. Keeps the exact plugins list in its type, which is
|
|
470
|
+
* how `App.Locals` learns what each plugin puts on `locals`.
|
|
471
|
+
*/
|
|
472
|
+
export function defineConfig<
|
|
473
|
+
const P extends readonly GrimoirePlugin<any>[] = [],
|
|
474
|
+
>(
|
|
475
|
+
config: Omit<GrimoireConfig, "plugins"> & { plugins?: P },
|
|
476
|
+
): GrimoireConfig & { readonly $plugins?: P } {
|
|
477
|
+
return config as GrimoireConfig;
|
|
425
478
|
}
|
|
426
479
|
|
|
480
|
+
type UnionToIntersection<U> = (
|
|
481
|
+
U extends unknown
|
|
482
|
+
? (x: U) => void
|
|
483
|
+
: never
|
|
484
|
+
) extends (x: infer I) => void
|
|
485
|
+
? I
|
|
486
|
+
: never;
|
|
487
|
+
|
|
488
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
489
|
+
|
|
490
|
+
/** What one plugin declares; nothing for a plugin typed loosely (`GrimoirePlugin<any>`). */
|
|
491
|
+
type OwnLocals<P> = P extends { readonly $locals?: infer L }
|
|
492
|
+
? IsAny<L> extends true
|
|
493
|
+
? {}
|
|
494
|
+
: NonNullable<L> extends object
|
|
495
|
+
? NonNullable<L>
|
|
496
|
+
: {}
|
|
497
|
+
: {};
|
|
498
|
+
|
|
499
|
+
/** A plugin's locals and those of the plugins it includes. */
|
|
500
|
+
type LocalsOf<P> = OwnLocals<P> &
|
|
501
|
+
(P extends { readonly includes?: readonly (infer I)[] }
|
|
502
|
+
? UnionToIntersection<OwnLocals<I>>
|
|
503
|
+
: {});
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Everything the plugins in a config put on `locals`, merged.
|
|
507
|
+
*
|
|
508
|
+
* ```ts
|
|
509
|
+
* import type config from "./sigil.config";
|
|
510
|
+
* type L = PluginLocals<typeof config>; // { db: ..., user: ..., session: ... }
|
|
511
|
+
* ```
|
|
512
|
+
*/
|
|
513
|
+
export type PluginLocals<C> = C extends { readonly $plugins?: infer P }
|
|
514
|
+
? P extends readonly unknown[]
|
|
515
|
+
? UnionToIntersection<LocalsOf<P[number]>> & {}
|
|
516
|
+
: {}
|
|
517
|
+
: {};
|
|
518
|
+
|
|
519
|
+
/** The database a plugin's tables go in: its literal key, any when it is only `string`. */
|
|
520
|
+
type DatabaseOf<P> = P extends { readonly database?: infer D }
|
|
521
|
+
? [D] extends [undefined]
|
|
522
|
+
? "db"
|
|
523
|
+
: D extends string
|
|
524
|
+
? D
|
|
525
|
+
: "db"
|
|
526
|
+
: "db";
|
|
527
|
+
type OwnTables<P> = P extends { readonly tables?: infer T }
|
|
528
|
+
? T extends Record<string, unknown>
|
|
529
|
+
? { [K in DatabaseOf<P>]: T }
|
|
530
|
+
: {}
|
|
531
|
+
: {};
|
|
532
|
+
type TablesOfPlugin<P> = OwnTables<P> &
|
|
533
|
+
(P extends { readonly includes?: readonly (infer I)[] }
|
|
534
|
+
? UnionToIntersection<OwnTables<I>>
|
|
535
|
+
: {});
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Every table the plugins in a config bring (auth's sessions, the job
|
|
539
|
+
* queue's), by database and name: `{ db: { sessions, sigilJobs } }`.
|
|
540
|
+
* Tome's generated types read it to type relations.
|
|
541
|
+
*/
|
|
542
|
+
export type PluginTables<C> = C extends { readonly $plugins?: infer P }
|
|
543
|
+
? P extends readonly unknown[]
|
|
544
|
+
? UnionToIntersection<TablesOfPlugin<P[number]>> & {}
|
|
545
|
+
: {}
|
|
546
|
+
: {};
|
|
547
|
+
|
|
427
548
|
/** Generic load type for +page.server.ts. For per-route typed params import PageServerLoad from './$types'. */
|
|
428
549
|
export type PageServerLoad<
|
|
429
550
|
P extends Record<string, string> = Record<string, string>,
|
|
@@ -1,132 +1,132 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
3
|
-
_resetAfterResponseForTest,
|
|
4
|
-
afterResponse,
|
|
5
|
-
drainAfterResponse,
|
|
6
|
-
withAfterResponse,
|
|
7
|
-
} from "../src/server/after-response.ts";
|
|
8
|
-
|
|
9
|
-
const req = () => new Request("http://localhost/");
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
_resetAfterResponseForTest();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
describe("afterResponse", () => {
|
|
16
|
-
test("does not run before the body is consumed", async () => {
|
|
17
|
-
let ran = false;
|
|
18
|
-
const res = await withAfterResponse(req(), async () => {
|
|
19
|
-
afterResponse(() => {
|
|
20
|
-
ran = true;
|
|
21
|
-
});
|
|
22
|
-
return new Response("hello");
|
|
23
|
-
});
|
|
24
|
-
expect(ran).toBe(false);
|
|
25
|
-
await res.text();
|
|
26
|
-
await Bun.sleep(10);
|
|
27
|
-
expect(ran).toBe(true);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("a body Bun types itself is still typed when served", async () => {
|
|
31
|
-
const server = Bun.serve({
|
|
32
|
-
port: 0,
|
|
33
|
-
fetch: (r) =>
|
|
34
|
-
withAfterResponse(r, async () => {
|
|
35
|
-
afterResponse(() => {});
|
|
36
|
-
return new Response("not found", { status: 404 });
|
|
37
|
-
}),
|
|
38
|
-
});
|
|
39
|
-
try {
|
|
40
|
-
const res = await fetch(`http://localhost:${server.port}/`);
|
|
41
|
-
expect(res.status).toBe(404);
|
|
42
|
-
expect(res.headers.get("content-type")).toStartWith("text/plain");
|
|
43
|
-
expect(await res.text()).toBe("not found");
|
|
44
|
-
} finally {
|
|
45
|
-
server.stop(true);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("runs for a response with no body", async () => {
|
|
50
|
-
let ran = false;
|
|
51
|
-
await withAfterResponse(req(), async () => {
|
|
52
|
-
afterResponse(() => {
|
|
53
|
-
ran = true;
|
|
54
|
-
});
|
|
55
|
-
return new Response(null, { status: 204 });
|
|
56
|
-
});
|
|
57
|
-
await Bun.sleep(10);
|
|
58
|
-
expect(ran).toBe(true);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test("a throwing task does not corrupt the response", async () => {
|
|
62
|
-
const res = await withAfterResponse(req(), async () => {
|
|
63
|
-
afterResponse(() => {
|
|
64
|
-
throw new Error("boom");
|
|
65
|
-
});
|
|
66
|
-
return new Response("still fine");
|
|
67
|
-
});
|
|
68
|
-
expect(await res.text()).toBe("still fine");
|
|
69
|
-
await Bun.sleep(10);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("tasks are isolated per request under concurrency", async () => {
|
|
73
|
-
const order: string[] = [];
|
|
74
|
-
const make = (tag: string) =>
|
|
75
|
-
withAfterResponse(req(), async () => {
|
|
76
|
-
afterResponse(() => order.push(tag));
|
|
77
|
-
return new Response(tag);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const [a, b] = await Promise.all([make("A"), make("B")]);
|
|
81
|
-
await Promise.all([a.text(), b.text()]);
|
|
82
|
-
await Bun.sleep(20);
|
|
83
|
-
|
|
84
|
-
expect(order.sort()).toEqual(["A", "B"]);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("preserves status, statusText and headers through the rewrap", async () => {
|
|
88
|
-
const res = await withAfterResponse(
|
|
89
|
-
req(),
|
|
90
|
-
async () =>
|
|
91
|
-
new Response("x", {
|
|
92
|
-
status: 404,
|
|
93
|
-
statusText: "Nope",
|
|
94
|
-
headers: { "x-test": "1" },
|
|
95
|
-
}),
|
|
96
|
-
);
|
|
97
|
-
expect(res.status).toBe(404);
|
|
98
|
-
expect(res.headers.get("x-test")).toBe("1");
|
|
99
|
-
expect(await res.text()).toBe("x");
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test("drain waits for a task that is still running", async () => {
|
|
103
|
-
let done = false;
|
|
104
|
-
const res = await withAfterResponse(req(), async () => {
|
|
105
|
-
afterResponse(async () => {
|
|
106
|
-
await Bun.sleep(50);
|
|
107
|
-
done = true;
|
|
108
|
-
});
|
|
109
|
-
return new Response("y");
|
|
110
|
-
});
|
|
111
|
-
await res.text();
|
|
112
|
-
await drainAfterResponse(1000);
|
|
113
|
-
expect(done).toBe(true);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
test("drain returns promptly when nothing is pending", async () => {
|
|
117
|
-
const t = Date.now();
|
|
118
|
-
await drainAfterResponse(500);
|
|
119
|
-
expect(Date.now() - t).toBeLessThan(200);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
test("drain gives up rather than hanging on a stuck task", async () => {
|
|
123
|
-
const res = await withAfterResponse(req(), async () => {
|
|
124
|
-
afterResponse(() => Bun.sleep(60_000));
|
|
125
|
-
return new Response("z");
|
|
126
|
-
});
|
|
127
|
-
await res.text();
|
|
128
|
-
const t = Date.now();
|
|
129
|
-
await drainAfterResponse(200);
|
|
130
|
-
expect(Date.now() - t).toBeLessThan(1500);
|
|
131
|
-
});
|
|
132
|
-
});
|
|
1
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
_resetAfterResponseForTest,
|
|
4
|
+
afterResponse,
|
|
5
|
+
drainAfterResponse,
|
|
6
|
+
withAfterResponse,
|
|
7
|
+
} from "../src/server/after-response.ts";
|
|
8
|
+
|
|
9
|
+
const req = () => new Request("http://localhost/");
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
_resetAfterResponseForTest();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("afterResponse", () => {
|
|
16
|
+
test("does not run before the body is consumed", async () => {
|
|
17
|
+
let ran = false;
|
|
18
|
+
const res = await withAfterResponse(req(), async () => {
|
|
19
|
+
afterResponse(() => {
|
|
20
|
+
ran = true;
|
|
21
|
+
});
|
|
22
|
+
return new Response("hello");
|
|
23
|
+
});
|
|
24
|
+
expect(ran).toBe(false);
|
|
25
|
+
await res.text();
|
|
26
|
+
await Bun.sleep(10);
|
|
27
|
+
expect(ran).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("a body Bun types itself is still typed when served", async () => {
|
|
31
|
+
const server = Bun.serve({
|
|
32
|
+
port: 0,
|
|
33
|
+
fetch: (r) =>
|
|
34
|
+
withAfterResponse(r, async () => {
|
|
35
|
+
afterResponse(() => {});
|
|
36
|
+
return new Response("not found", { status: 404 });
|
|
37
|
+
}),
|
|
38
|
+
});
|
|
39
|
+
try {
|
|
40
|
+
const res = await fetch(`http://localhost:${server.port}/`);
|
|
41
|
+
expect(res.status).toBe(404);
|
|
42
|
+
expect(res.headers.get("content-type")).toStartWith("text/plain");
|
|
43
|
+
expect(await res.text()).toBe("not found");
|
|
44
|
+
} finally {
|
|
45
|
+
server.stop(true);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("runs for a response with no body", async () => {
|
|
50
|
+
let ran = false;
|
|
51
|
+
await withAfterResponse(req(), async () => {
|
|
52
|
+
afterResponse(() => {
|
|
53
|
+
ran = true;
|
|
54
|
+
});
|
|
55
|
+
return new Response(null, { status: 204 });
|
|
56
|
+
});
|
|
57
|
+
await Bun.sleep(10);
|
|
58
|
+
expect(ran).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("a throwing task does not corrupt the response", async () => {
|
|
62
|
+
const res = await withAfterResponse(req(), async () => {
|
|
63
|
+
afterResponse(() => {
|
|
64
|
+
throw new Error("boom");
|
|
65
|
+
});
|
|
66
|
+
return new Response("still fine");
|
|
67
|
+
});
|
|
68
|
+
expect(await res.text()).toBe("still fine");
|
|
69
|
+
await Bun.sleep(10);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("tasks are isolated per request under concurrency", async () => {
|
|
73
|
+
const order: string[] = [];
|
|
74
|
+
const make = (tag: string) =>
|
|
75
|
+
withAfterResponse(req(), async () => {
|
|
76
|
+
afterResponse(() => order.push(tag));
|
|
77
|
+
return new Response(tag);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const [a, b] = await Promise.all([make("A"), make("B")]);
|
|
81
|
+
await Promise.all([a.text(), b.text()]);
|
|
82
|
+
await Bun.sleep(20);
|
|
83
|
+
|
|
84
|
+
expect(order.sort()).toEqual(["A", "B"]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("preserves status, statusText and headers through the rewrap", async () => {
|
|
88
|
+
const res = await withAfterResponse(
|
|
89
|
+
req(),
|
|
90
|
+
async () =>
|
|
91
|
+
new Response("x", {
|
|
92
|
+
status: 404,
|
|
93
|
+
statusText: "Nope",
|
|
94
|
+
headers: { "x-test": "1" },
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
expect(res.status).toBe(404);
|
|
98
|
+
expect(res.headers.get("x-test")).toBe("1");
|
|
99
|
+
expect(await res.text()).toBe("x");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("drain waits for a task that is still running", async () => {
|
|
103
|
+
let done = false;
|
|
104
|
+
const res = await withAfterResponse(req(), async () => {
|
|
105
|
+
afterResponse(async () => {
|
|
106
|
+
await Bun.sleep(50);
|
|
107
|
+
done = true;
|
|
108
|
+
});
|
|
109
|
+
return new Response("y");
|
|
110
|
+
});
|
|
111
|
+
await res.text();
|
|
112
|
+
await drainAfterResponse(1000);
|
|
113
|
+
expect(done).toBe(true);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("drain returns promptly when nothing is pending", async () => {
|
|
117
|
+
const t = Date.now();
|
|
118
|
+
await drainAfterResponse(500);
|
|
119
|
+
expect(Date.now() - t).toBeLessThan(200);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("drain gives up rather than hanging on a stuck task", async () => {
|
|
123
|
+
const res = await withAfterResponse(req(), async () => {
|
|
124
|
+
afterResponse(() => Bun.sleep(60_000));
|
|
125
|
+
return new Response("z");
|
|
126
|
+
});
|
|
127
|
+
await res.text();
|
|
128
|
+
const t = Date.now();
|
|
129
|
+
await drainAfterResponse(200);
|
|
130
|
+
expect(Date.now() - t).toBeLessThan(1500);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
@@ -26,8 +26,12 @@ describe("isContentAddressed", () => {
|
|
|
26
26
|
test("a bundler chunk name counts", () => {
|
|
27
27
|
// Shared chunks are named for their contents, and every page load
|
|
28
28
|
// fetches them. Without this they revalidated each time.
|
|
29
|
-
expect(isContentAddressed("/__grimoire__/chunk-jp4kgm87.js", "")).toBe(
|
|
30
|
-
|
|
29
|
+
expect(isContentAddressed("/__grimoire__/chunk-jp4kgm87.js", "")).toBe(
|
|
30
|
+
true,
|
|
31
|
+
);
|
|
32
|
+
expect(cacheControlFor("/__grimoire__/chunk-1pk15q3t.js", "")).toBe(
|
|
33
|
+
"immutable",
|
|
34
|
+
);
|
|
31
35
|
});
|
|
32
36
|
|
|
33
37
|
test("chunk- must be the whole stem, not a lookalike", () => {
|
package/test/base.test.ts
CHANGED
|
@@ -22,8 +22,12 @@ describe("normalizeBase", () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
test("each mistake is named, with the fix", () => {
|
|
25
|
-
expect(() => normalizeBase("repo")).toThrow(
|
|
26
|
-
|
|
25
|
+
expect(() => normalizeBase("repo")).toThrow(
|
|
26
|
+
/must start with a slash: "\/repo"/,
|
|
27
|
+
);
|
|
28
|
+
expect(() => normalizeBase("/repo/")).toThrow(
|
|
29
|
+
/must not end with a slash: "\/repo"/,
|
|
30
|
+
);
|
|
27
31
|
expect(() => normalizeBase("https://u.github.io/repo")).toThrow(/full URL/);
|
|
28
32
|
expect(() => normalizeBase("//cdn/repo")).toThrow(/full URL/);
|
|
29
33
|
expect(() => normalizeBase("/a b")).toThrow(/plain path/);
|
|
@@ -59,7 +63,10 @@ describe("withBase and stripBase", () => {
|
|
|
59
63
|
describe("extractLinks under a base", () => {
|
|
60
64
|
const page = `<a href="/repo/a">a</a> <a href="/repo">home</a> <a href="/other">no</a> <a href="/repo/f.xml">no</a>`;
|
|
61
65
|
test("strips the base and skips links outside it", () => {
|
|
62
|
-
expect(extractLinks(page, "http://localhost", "/repo").sort()).toEqual([
|
|
66
|
+
expect(extractLinks(page, "http://localhost", "/repo").sort()).toEqual([
|
|
67
|
+
"/",
|
|
68
|
+
"/a",
|
|
69
|
+
]);
|
|
63
70
|
});
|
|
64
71
|
});
|
|
65
72
|
|
|
@@ -90,8 +97,10 @@ describe("a server under a base", () => {
|
|
|
90
97
|
_skipBuild: true,
|
|
91
98
|
_forceProduction: true,
|
|
92
99
|
});
|
|
93
|
-
const get = (
|
|
94
|
-
|
|
100
|
+
const get = (
|
|
101
|
+
app: { fetch(r: Request): Response | Promise<Response> },
|
|
102
|
+
path: string,
|
|
103
|
+
) => Promise.resolve(app.fetch(new Request(`http://localhost${path}`)));
|
|
95
104
|
|
|
96
105
|
test("routes see the route path, not the full one", async () => {
|
|
97
106
|
const app = await make();
|
|
@@ -128,7 +137,12 @@ describe("a server under a base", () => {
|
|
|
128
137
|
|
|
129
138
|
test("a bad base stops startup", async () => {
|
|
130
139
|
await expect(
|
|
131
|
-
createApp({
|
|
140
|
+
createApp({
|
|
141
|
+
routes: dir,
|
|
142
|
+
base: "/repo/",
|
|
143
|
+
_skipBuild: true,
|
|
144
|
+
_forceProduction: true,
|
|
145
|
+
}),
|
|
132
146
|
).rejects.toThrow(/must not end with a slash/);
|
|
133
147
|
});
|
|
134
148
|
});
|