@sigil-dev/grimoire 0.9.20 → 0.9.22

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.
Files changed (94) hide show
  1. package/README.md +83 -83
  2. package/index.ts +50 -48
  3. package/package.json +16 -5
  4. package/public/__grimoire__/grimoire-client.js +12 -0
  5. package/public/__grimoire__/runtime.js +1241 -0
  6. package/server.ts +22 -20
  7. package/src/adapters/bun.ts +17 -0
  8. package/src/adapters/static.ts +41 -0
  9. package/src/adapters/types.ts +36 -0
  10. package/src/base.ts +91 -0
  11. package/src/client/csrf.ts +47 -47
  12. package/src/client/deferred.ts +179 -179
  13. package/src/client/enhance.ts +160 -160
  14. package/src/client/router.ts +870 -833
  15. package/src/dev/compile-module.ts +211 -211
  16. package/src/dev/loader.ts +141 -141
  17. package/src/dev/resolve.ts +70 -70
  18. package/src/dev/runtime-bundle.ts +51 -51
  19. package/src/env/modules.ts +62 -62
  20. package/src/env/required.ts +42 -0
  21. package/src/env/typegen.ts +61 -61
  22. package/src/headers.ts +105 -114
  23. package/src/logger/transports.ts +117 -117
  24. package/src/rendering/head.ts +49 -49
  25. package/src/rendering/hydrate.ts +161 -161
  26. package/src/rendering/images.ts +43 -43
  27. package/src/rendering/index.ts +755 -745
  28. package/src/rendering/preload.ts +101 -95
  29. package/src/rendering/ssrPlugin.ts +108 -108
  30. package/src/rendering/timing.ts +40 -40
  31. package/src/routing/router.ts +186 -186
  32. package/src/routing/scanner.ts +225 -196
  33. package/src/routing/transform-routes.ts +102 -102
  34. package/src/server/after-response.ts +206 -194
  35. package/src/server/asset-cache.ts +57 -55
  36. package/src/server/build.ts +262 -249
  37. package/src/server/cached.ts +114 -114
  38. package/src/server/client-address.ts +82 -82
  39. package/src/server/client-dir.ts +57 -57
  40. package/src/server/client-entry.ts +44 -0
  41. package/src/server/cookie-utils.ts +80 -80
  42. package/src/server/coordinator.ts +476 -471
  43. package/src/server/hooks.ts +122 -122
  44. package/src/server/index.ts +1138 -1022
  45. package/src/server/plugins.ts +172 -172
  46. package/src/server/prerender.ts +444 -0
  47. package/src/server/reserved.ts +17 -0
  48. package/src/server/resolver.ts +98 -98
  49. package/src/server/rewrite.ts +28 -0
  50. package/src/server/warm.ts +91 -91
  51. package/src/server/with-headers.ts +27 -0
  52. package/src/server/worker.ts +74 -72
  53. package/src/testing/dom.ts +73 -0
  54. package/src/testing/index.ts +169 -0
  55. package/src/testing/server.ts +162 -0
  56. package/src/typegen/index.ts +450 -450
  57. package/src/types.ts +501 -432
  58. package/test/after-response.test.ts +132 -113
  59. package/test/asset-cache-policy.test.ts +96 -96
  60. package/test/base.test.ts +134 -0
  61. package/test/cached.test.ts +126 -126
  62. package/test/client-address.test.ts +78 -78
  63. package/test/client-csrf.test.ts +61 -61
  64. package/test/client-dir.test.ts +101 -69
  65. package/test/client-router.test.ts +789 -734
  66. package/test/coordinator.test.ts +96 -94
  67. package/test/deferred.test.ts +256 -256
  68. package/test/dev-loader.test.ts +30 -30
  69. package/test/env.test.ts +168 -168
  70. package/test/force-production.test.ts +44 -0
  71. package/test/images.test.ts +49 -49
  72. package/test/locals-header.test.ts +61 -0
  73. package/test/middleware.test.ts +243 -243
  74. package/test/parser-plugins.test.ts +92 -92
  75. package/test/plugin-handle.test.ts +62 -62
  76. package/test/plugin-services-context.test.ts +97 -97
  77. package/test/preload.test.ts +48 -48
  78. package/test/prerender.test.ts +353 -0
  79. package/test/rendering.test.ts +798 -798
  80. package/test/required-env.test.ts +32 -0
  81. package/test/rewrite-html.test.ts +90 -0
  82. package/test/scanning.test.ts +349 -314
  83. package/test/server.test.ts +640 -619
  84. package/test/services.test.ts +90 -90
  85. package/test/streaming.test.ts +378 -378
  86. package/test/testing-helpers.test.ts +166 -0
  87. package/test/typegen.test.ts +805 -799
  88. package/test/warm.test.ts +96 -96
  89. package/test/worker-context.test.ts +59 -59
  90. package/test/workspace-files.test.ts +149 -146
  91. package/test/workspace-versions.test.ts +131 -131
  92. package/.grimoire/_routes.ts +0 -4
  93. package/public/__grimoire__/client.js +0 -55
  94. package/public/__grimoire__/hydrate.js +0 -63
@@ -1,62 +1,62 @@
1
- import { describe, expect, test } from "bun:test";
2
- import type { Handle, RequestEvent } from "../src/server/hooks.ts";
3
- import { composeHandle } from "../src/server/plugins.ts";
4
- import type { GrimoirePlugin } from "../src/types.ts";
5
-
6
- const event = () => ({ locals: {} }) as unknown as RequestEvent;
7
- const ok = () => new Response("page");
8
- const named = (name: string, handle?: Handle): GrimoirePlugin => ({
9
- name,
10
- handle,
11
- });
12
-
13
- describe("composeHandle", () => {
14
- test("is undefined when nobody has a handle", () => {
15
- expect(composeHandle([named("a")], undefined)).toBeUndefined();
16
- });
17
-
18
- test("plugins run in order, then the app's handle, which sees their locals", async () => {
19
- const seen: string[] = [];
20
- const a = named("a", ({ event, resolve }) => {
21
- (event.locals as unknown as Record<string, unknown>).user = "ada";
22
- seen.push("a");
23
- return resolve(event);
24
- });
25
- const b = named("b", ({ event, resolve }) => {
26
- seen.push("b");
27
- return resolve(event);
28
- });
29
- const app: Handle = ({ event, resolve }) => {
30
- seen.push(
31
- `app:${(event.locals as unknown as Record<string, unknown>).user}`,
32
- );
33
- return resolve(event);
34
- };
35
- const res = await composeHandle([a, b], app)!({
36
- event: event(),
37
- resolve: ok,
38
- });
39
- expect(await res.text()).toBe("page");
40
- expect(seen).toEqual(["a", "b", "app:ada"]);
41
- });
42
-
43
- test("a plugin can answer without calling resolve", async () => {
44
- let reached = false;
45
- const stop = named("stop", () => new Response("no", { status: 401 }));
46
- const res = await composeHandle([stop], () => {
47
- reached = true;
48
- return ok();
49
- })!({ event: event(), resolve: ok });
50
- expect(res.status).toBe(401);
51
- expect(reached).toBe(false);
52
- });
53
-
54
- test("a lone plugin handle is used as is", async () => {
55
- const only = named("only", ({ resolve, event }) => resolve(event));
56
- const res = await composeHandle([only], undefined)!({
57
- event: event(),
58
- resolve: ok,
59
- });
60
- expect(await res.text()).toBe("page");
61
- });
62
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import type { Handle, RequestEvent } from "../src/server/hooks.ts";
3
+ import { composeHandle } from "../src/server/plugins.ts";
4
+ import type { GrimoirePlugin } from "../src/types.ts";
5
+
6
+ const event = () => ({ locals: {} }) as unknown as RequestEvent;
7
+ const ok = () => new Response("page");
8
+ const named = (name: string, handle?: Handle): GrimoirePlugin => ({
9
+ name,
10
+ handle,
11
+ });
12
+
13
+ describe("composeHandle", () => {
14
+ test("is undefined when nobody has a handle", () => {
15
+ expect(composeHandle([named("a")], undefined)).toBeUndefined();
16
+ });
17
+
18
+ test("plugins run in order, then the app's handle, which sees their locals", async () => {
19
+ const seen: string[] = [];
20
+ const a = named("a", ({ event, resolve }) => {
21
+ (event.locals as unknown as Record<string, unknown>).user = "ada";
22
+ seen.push("a");
23
+ return resolve(event);
24
+ });
25
+ const b = named("b", ({ event, resolve }) => {
26
+ seen.push("b");
27
+ return resolve(event);
28
+ });
29
+ const app: Handle = ({ event, resolve }) => {
30
+ seen.push(
31
+ `app:${(event.locals as unknown as Record<string, unknown>).user}`,
32
+ );
33
+ return resolve(event);
34
+ };
35
+ const res = await composeHandle([a, b], app)!({
36
+ event: event(),
37
+ resolve: ok,
38
+ });
39
+ expect(await res.text()).toBe("page");
40
+ expect(seen).toEqual(["a", "b", "app:ada"]);
41
+ });
42
+
43
+ test("a plugin can answer without calling resolve", async () => {
44
+ let reached = false;
45
+ const stop = named("stop", () => new Response("no", { status: 401 }));
46
+ const res = await composeHandle([stop], () => {
47
+ reached = true;
48
+ return ok();
49
+ })!({ event: event(), resolve: ok });
50
+ expect(res.status).toBe(401);
51
+ expect(reached).toBe(false);
52
+ });
53
+
54
+ test("a lone plugin handle is used as is", async () => {
55
+ const only = named("only", ({ resolve, event }) => resolve(event));
56
+ const res = await composeHandle([only], undefined)!({
57
+ event: event(),
58
+ resolve: ok,
59
+ });
60
+ expect(await res.text()).toBe("page");
61
+ });
62
+ });
@@ -1,97 +1,97 @@
1
- import { describe, expect, mock, test } from "bun:test";
2
- import { collectServices } from "../src/server/plugins";
3
-
4
- /**
5
- * Can one plugin use a database another plugin opened?
6
- *
7
- * The SQL plugin owns the connection and publishes it as a service. A second
8
- * plugin — jobs, a rate limiter, anything that needs to persist something —
9
- * wants that same connection rather than opening its own pool and splitting
10
- * the budget again.
11
- *
12
- * As things stand `services()` is called with no arguments, so a plugin can
13
- * only ever see what it produced itself. sekai-viewer works around this with
14
- * an `inject` plugin that re-publishes the client, which means the ordering
15
- * of that plugin in the array is load-bearing and nothing says so.
16
- */
17
-
18
- type AnyPlugin = Parameters<typeof collectServices>[0][number];
19
-
20
- function sqlPlugin(key = "db") {
21
- return {
22
- name: "sql",
23
- config: () => ({}),
24
- services: () => ({ [key]: { tag: "client" } }),
25
- } as unknown as AnyPlugin;
26
- }
27
-
28
- describe("plugin services", () => {
29
- test("a later plugin can read what an earlier one published", async () => {
30
- const consumer = {
31
- name: "consumer",
32
- // The whole point: use the client instead of opening a second pool.
33
- services: async (ctx: { services: Record<string, unknown> }) => ({
34
- seen: Boolean(ctx.services.db),
35
- }),
36
- } as unknown as AnyPlugin;
37
-
38
- const collected = await collectServices([sqlPlugin(), consumer]);
39
-
40
- expect(collected.seen).toBe(true);
41
- });
42
-
43
- test("what an earlier plugin published is handed to the consumer", async () => {
44
- const received: Record<string, unknown> = {};
45
- const consumer = {
46
- name: "consumer",
47
- services: (ctx: { services: Record<string, unknown> }) => {
48
- Object.assign(received, ctx.services);
49
- return {};
50
- },
51
- } as unknown as AnyPlugin;
52
-
53
- await collectServices([sqlPlugin("db"), consumer]);
54
-
55
- expect(received.db).toEqual({ tag: "client" });
56
- });
57
-
58
- test("the client a consumer reads is the same object the sql plugin owns", async () => {
59
- const client = { tag: "client" };
60
- const owner = { name: "sql", services: () => ({ db: client }) } as unknown as AnyPlugin;
61
- const consumer = {
62
- name: "consumer",
63
- services: (ctx: { services: Record<string, unknown> }) => ({ used: ctx.services.db }),
64
- } as unknown as AnyPlugin;
65
-
66
- const collected = await collectServices([owner, consumer]);
67
-
68
- // Identity, not equality: opening a second pool to compare is the
69
- // thing this is meant to prevent.
70
- expect(collected.used).toBe(client);
71
- });
72
-
73
- test("a consumer placed before the sql plugin sees nothing, rather than throwing", async () => {
74
- // Order-independence is the point. A plugin must not have to be
75
- // registered after the plugin it depends on to be correct.
76
- const consumer = {
77
- name: "consumer",
78
- services: (ctx: { services: Record<string, unknown> }) => ({
79
- seen: Boolean(ctx.services.db),
80
- }),
81
- } as unknown as AnyPlugin;
82
-
83
- const collected = await collectServices([consumer, sqlPlugin()]);
84
-
85
- expect(collected.seen).toBe(false);
86
- expect(collected.db).toEqual({ tag: "client" });
87
- });
88
-
89
- test("a plugin that ignores the context still works", async () => {
90
- const solo = {
91
- name: "solo",
92
- services: () => ({ a: 1 }),
93
- } as unknown as AnyPlugin;
94
-
95
- expect(await collectServices([solo])).toEqual({ a: 1 });
96
- });
97
- });
1
+ import { describe, expect, mock, test } from "bun:test";
2
+ import { collectServices } from "../src/server/plugins";
3
+
4
+ /**
5
+ * Can one plugin use a database another plugin opened?
6
+ *
7
+ * The SQL plugin owns the connection and publishes it as a service. A second
8
+ * plugin — jobs, a rate limiter, anything that needs to persist something —
9
+ * wants that same connection rather than opening its own pool and splitting
10
+ * the budget again.
11
+ *
12
+ * As things stand `services()` is called with no arguments, so a plugin can
13
+ * only ever see what it produced itself. sekai-viewer works around this with
14
+ * an `inject` plugin that re-publishes the client, which means the ordering
15
+ * of that plugin in the array is load-bearing and nothing says so.
16
+ */
17
+
18
+ type AnyPlugin = Parameters<typeof collectServices>[0][number];
19
+
20
+ function sqlPlugin(key = "db") {
21
+ return {
22
+ name: "sql",
23
+ config: () => ({}),
24
+ services: () => ({ [key]: { tag: "client" } }),
25
+ } as unknown as AnyPlugin;
26
+ }
27
+
28
+ describe("plugin services", () => {
29
+ test("a later plugin can read what an earlier one published", async () => {
30
+ const consumer = {
31
+ name: "consumer",
32
+ // The whole point: use the client instead of opening a second pool.
33
+ services: async (ctx: { services: Record<string, unknown> }) => ({
34
+ seen: Boolean(ctx.services.db),
35
+ }),
36
+ } as unknown as AnyPlugin;
37
+
38
+ const collected = await collectServices([sqlPlugin(), consumer]);
39
+
40
+ expect(collected.seen).toBe(true);
41
+ });
42
+
43
+ test("what an earlier plugin published is handed to the consumer", async () => {
44
+ const received: Record<string, unknown> = {};
45
+ const consumer = {
46
+ name: "consumer",
47
+ services: (ctx: { services: Record<string, unknown> }) => {
48
+ Object.assign(received, ctx.services);
49
+ return {};
50
+ },
51
+ } as unknown as AnyPlugin;
52
+
53
+ await collectServices([sqlPlugin("db"), consumer]);
54
+
55
+ expect(received.db).toEqual({ tag: "client" });
56
+ });
57
+
58
+ test("the client a consumer reads is the same object the sql plugin owns", async () => {
59
+ const client = { tag: "client" };
60
+ const owner = { name: "sql", services: () => ({ db: client }) } as unknown as AnyPlugin;
61
+ const consumer = {
62
+ name: "consumer",
63
+ services: (ctx: { services: Record<string, unknown> }) => ({ used: ctx.services.db }),
64
+ } as unknown as AnyPlugin;
65
+
66
+ const collected = await collectServices([owner, consumer]);
67
+
68
+ // Identity, not equality: opening a second pool to compare is the
69
+ // thing this is meant to prevent.
70
+ expect(collected.used).toBe(client);
71
+ });
72
+
73
+ test("a consumer placed before the sql plugin sees nothing, rather than throwing", async () => {
74
+ // Order-independence is the point. A plugin must not have to be
75
+ // registered after the plugin it depends on to be correct.
76
+ const consumer = {
77
+ name: "consumer",
78
+ services: (ctx: { services: Record<string, unknown> }) => ({
79
+ seen: Boolean(ctx.services.db),
80
+ }),
81
+ } as unknown as AnyPlugin;
82
+
83
+ const collected = await collectServices([consumer, sqlPlugin()]);
84
+
85
+ expect(collected.seen).toBe(false);
86
+ expect(collected.db).toEqual({ tag: "client" });
87
+ });
88
+
89
+ test("a plugin that ignores the context still works", async () => {
90
+ const solo = {
91
+ name: "solo",
92
+ services: () => ({ a: 1 }),
93
+ } as unknown as AnyPlugin;
94
+
95
+ expect(await collectServices([solo])).toEqual({ a: 1 });
96
+ });
97
+ });
@@ -1,48 +1,48 @@
1
- import { afterEach, describe, expect, test } from "bun:test";
2
- import {
3
- chunksFor,
4
- parseChunkGraph,
5
- preloadLinks,
6
- resetChunkGraph,
7
- } from "../src/rendering/preload.ts";
8
-
9
- // the shape Bun writes into hydrate.js for a split build
10
- const SOURCE = `var x = 1;
11
- __chunks(import.meta.url,["hy","home","cards","shared","leaf"],[["./hydrate.js",3],["./chunk-home.js",3],["./chunk-cards.js",3,4],["./chunk-shared.js"],["./chunk-leaf.js",3]],0);
12
- var routes = {
13
- "/": { __lazy: () => (__preload("home"), import("./chunk-home.js")) },
14
- "/cards/:id": { __lazy: () => (__preload("cards"), import("./chunk-cards.js")) },
15
- };`;
16
-
17
- afterEach(() => resetChunkGraph());
18
-
19
- describe("chunk graph", () => {
20
- test("routes map to chunks and closures include shared imports", () => {
21
- const g = parseChunkGraph(SOURCE)!;
22
- expect(g.routes.get("/")).toBe("home");
23
- expect(chunksFor(g, "/").sort()).toEqual(["chunk-home.js", "chunk-shared.js"]);
24
- expect(chunksFor(g, "/cards/:id").sort()).toEqual([
25
- "chunk-cards.js",
26
- "chunk-leaf.js",
27
- "chunk-shared.js",
28
- ]);
29
- });
30
-
31
- test("hydrate.js itself is never listed, unknown routes give nothing", () => {
32
- const g = parseChunkGraph(SOURCE)!;
33
- expect(chunksFor(g, "/cards/:id")).not.toContain("hydrate.js");
34
- expect(chunksFor(g, "/missing")).toEqual([]);
35
- });
36
-
37
- test("output without a chunk graph parses to null", () => {
38
- expect(parseChunkGraph("console.log(1)")).toBeNull();
39
- });
40
- });
41
-
42
- describe("preloadLinks", () => {
43
- test("is empty in dev and when no build exists", () => {
44
- expect(preloadLinks("/", "", true)).toBe("");
45
- // cwd here has no .grimoire/client/hydrate.js
46
- expect(preloadLinks("/", "")).toBe("");
47
- });
48
- });
1
+ import { afterEach, describe, expect, test } from "bun:test";
2
+ import {
3
+ chunksFor,
4
+ parseChunkGraph,
5
+ preloadLinks,
6
+ resetChunkGraph,
7
+ } from "../src/rendering/preload.ts";
8
+
9
+ // the shape Bun writes into hydrate.js for a split build
10
+ const SOURCE = `var x = 1;
11
+ __chunks(import.meta.url,["hy","home","cards","shared","leaf"],[["./hydrate.js",3],["./chunk-home.js",3],["./chunk-cards.js",3,4],["./chunk-shared.js"],["./chunk-leaf.js",3]],0);
12
+ var routes = {
13
+ "/": { __lazy: () => (__preload("home"), import("./chunk-home.js")) },
14
+ "/cards/:id": { __lazy: () => (__preload("cards"), import("./chunk-cards.js")) },
15
+ };`;
16
+
17
+ afterEach(() => resetChunkGraph());
18
+
19
+ describe("chunk graph", () => {
20
+ test("routes map to chunks and closures include shared imports", () => {
21
+ const g = parseChunkGraph(SOURCE)!;
22
+ expect(g.routes.get("/")).toBe("home");
23
+ expect(chunksFor(g, "/").sort()).toEqual(["chunk-home.js", "chunk-shared.js"]);
24
+ expect(chunksFor(g, "/cards/:id").sort()).toEqual([
25
+ "chunk-cards.js",
26
+ "chunk-leaf.js",
27
+ "chunk-shared.js",
28
+ ]);
29
+ });
30
+
31
+ test("hydrate.js itself is never listed, unknown routes give nothing", () => {
32
+ const g = parseChunkGraph(SOURCE)!;
33
+ expect(chunksFor(g, "/cards/:id")).not.toContain("hydrate.js");
34
+ expect(chunksFor(g, "/missing")).toEqual([]);
35
+ });
36
+
37
+ test("output without a chunk graph parses to null", () => {
38
+ expect(parseChunkGraph("console.log(1)")).toBeNull();
39
+ });
40
+ });
41
+
42
+ describe("preloadLinks", () => {
43
+ test("is empty in dev and when no build exists", () => {
44
+ expect(preloadLinks("/", "", true)).toBe("");
45
+ // cwd here has no .grimoire/client/hydrate.js
46
+ expect(preloadLinks("/", "")).toBe("");
47
+ });
48
+ });