@sigil-dev/grimoire 0.9.29 → 0.9.31

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 (105) hide show
  1. package/bin/sigil.ts +4 -0
  2. package/index.ts +16 -10
  3. package/package.json +14 -6
  4. package/server.ts +2 -2
  5. package/src/adapters/bun.ts +17 -17
  6. package/src/adapters/static.ts +41 -41
  7. package/src/adapters/types.ts +36 -36
  8. package/src/base.ts +4 -1
  9. package/src/client/csrf.ts +47 -47
  10. package/src/client/deferred.ts +1 -4
  11. package/src/client/enhance.ts +160 -160
  12. package/src/client/router.ts +927 -926
  13. package/src/config-file.ts +84 -0
  14. package/src/dev/compile-module.ts +199 -193
  15. package/src/dev/hmr-client.ts +229 -229
  16. package/src/dev/hmr-server.ts +274 -266
  17. package/src/dev/loader.ts +144 -144
  18. package/src/dev/runtime-bundle.ts +5 -3
  19. package/src/duration.ts +46 -0
  20. package/src/env/modules.ts +62 -62
  21. package/src/env/required.ts +42 -42
  22. package/src/env/typegen.ts +61 -61
  23. package/src/headers.ts +105 -105
  24. package/src/logger/transports.ts +117 -117
  25. package/src/plugin-order.ts +88 -0
  26. package/src/rendering/head.ts +49 -49
  27. package/src/rendering/hydrate.ts +162 -162
  28. package/src/rendering/images.ts +43 -43
  29. package/src/rendering/preload.ts +22 -8
  30. package/src/rendering/ssrPlugin.ts +107 -107
  31. package/src/rendering/timing.ts +40 -40
  32. package/src/routing/route-ids.ts +65 -0
  33. package/src/routing/route.ts +65 -0
  34. package/src/routing/router.ts +84 -32
  35. package/src/routing/scanner.ts +18 -5
  36. package/src/routing/transform-routes.ts +102 -102
  37. package/src/server/after-response.ts +206 -206
  38. package/src/server/asset-cache.ts +57 -57
  39. package/src/server/build.ts +304 -295
  40. package/src/server/cached.ts +114 -114
  41. package/src/server/client-address.ts +82 -82
  42. package/src/server/client-dir.ts +63 -63
  43. package/src/server/client-entry.ts +44 -44
  44. package/src/server/context.ts +18 -10
  45. package/src/server/cookie-utils.ts +80 -80
  46. package/src/server/coordinator.ts +479 -476
  47. package/src/server/hooks.ts +122 -122
  48. package/src/server/index.ts +1181 -1171
  49. package/src/server/plugins.ts +172 -172
  50. package/src/server/prerender.ts +473 -473
  51. package/src/server/reserved.ts +17 -17
  52. package/src/server/resolver.ts +149 -123
  53. package/src/server/rewrite.ts +28 -28
  54. package/src/server/warm.ts +9 -3
  55. package/src/server/with-headers.ts +27 -27
  56. package/src/server/worker.ts +2 -6
  57. package/src/server-only.ts +78 -0
  58. package/src/testing/dom.ts +73 -73
  59. package/src/testing/index.ts +169 -169
  60. package/src/testing/server.ts +1 -0
  61. package/src/typegen/index.ts +97 -2
  62. package/src/types.ts +642 -508
  63. package/test/after-response.test.ts +132 -132
  64. package/test/asset-cache-policy.test.ts +6 -2
  65. package/test/base.test.ts +20 -6
  66. package/test/cached.test.ts +126 -126
  67. package/test/client-address.test.ts +78 -78
  68. package/test/client-csrf.test.ts +61 -61
  69. package/test/client-dir.test.ts +101 -101
  70. package/test/client-router.test.ts +37 -5
  71. package/test/config-file.test.ts +84 -0
  72. package/test/coordinator.test.ts +9 -2
  73. package/test/deferred.test.ts +4 -2
  74. package/test/dev-loader.test.ts +30 -30
  75. package/test/env.test.ts +171 -171
  76. package/test/force-production.test.ts +7 -2
  77. package/test/images.test.ts +20 -8
  78. package/test/locals-header.test.ts +61 -61
  79. package/test/middleware.test.ts +243 -243
  80. package/test/parser-plugins.test.ts +98 -92
  81. package/test/plugin-handle.test.ts +62 -62
  82. package/test/plugin-order.test.ts +98 -0
  83. package/test/plugin-routes.test.ts +47 -42
  84. package/test/plugin-services-context.test.ts +7 -2
  85. package/test/preload.test.ts +4 -1
  86. package/test/prerender.test.ts +3 -1
  87. package/test/rendering.test.ts +3 -2
  88. package/test/required-env.test.ts +32 -32
  89. package/test/resolver-windows.test.ts +22 -23
  90. package/test/rewrite-html.test.ts +90 -90
  91. package/test/route-group-layouts.test.ts +38 -36
  92. package/test/route.test.ts +103 -0
  93. package/test/routing.test.ts +225 -197
  94. package/test/scanning.test.ts +53 -33
  95. package/test/server-context.test.ts +22 -0
  96. package/test/server-only.test.ts +110 -0
  97. package/test/server.test.ts +659 -640
  98. package/test/services.test.ts +3 -3
  99. package/test/streaming.test.ts +11 -6
  100. package/test/testing-helpers.test.ts +166 -166
  101. package/test/typegen.test.ts +209 -112
  102. package/test/warm.test.ts +96 -96
  103. package/test/worker-context.test.ts +59 -59
  104. package/test/workspace-files.test.ts +26 -10
  105. package/test/workspace-versions.test.ts +146 -131
@@ -35,7 +35,9 @@ const setWindow = (w: FakeWindow | undefined) => {
35
35
  (globalThis as unknown as { window?: FakeWindow }).window = w;
36
36
  };
37
37
 
38
- const fakeWindow = (state: unknown = { pattern: "/", data: {} }): FakeWindow => {
38
+ const fakeWindow = (
39
+ state: unknown = { pattern: "/", data: {} },
40
+ ): FakeWindow => {
39
41
  const w: FakeWindow = {
40
42
  __grimoire_state__: state,
41
43
  addEventListener: (type, fn) => {
@@ -86,7 +88,7 @@ function reset() {
86
88
  /** Run with no `window` at all, then put the real one back. */
87
89
  async function withoutWindow<T>(fn: () => T): Promise<T> {
88
90
  const g = globalThis as unknown as { window?: unknown };
89
- const had = Object.prototype.hasOwnProperty.call(g, "window");
91
+ const had = Object.hasOwn(g, "window");
90
92
  const saved = g.window;
91
93
  // `delete` rather than `undefined`: a global that exists with the value
92
94
  // undefined is still `typeof window === "object"`, so the helper would
@@ -1,30 +1,30 @@
1
- import { afterAll, expect, test } from "bun:test";
2
- import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
- import { DevGraph } from "../src/dev/graph";
6
- import { makeDevLoader } from "../src/dev/loader";
7
-
8
- const root = mkdtempSync(join(tmpdir(), "grimoire-dev-loader-"));
9
- afterAll(() => rmSync(root, { recursive: true, force: true }));
10
-
11
- test("a component with a scoped <style> block renders server-side", async () => {
12
- const file = join(root, "Card.tsx");
13
- // the tag is split so this test file's own preload doesn't strip it
14
- const tag = (name: string) => `<${name}>`;
15
- writeFileSync(
16
- file,
17
- `export const Card = ({ title }) => <div class="card">{title}</div>;
18
-
19
- ${tag("style")}
20
- .card { padding: 1rem; }
21
- .card .title { text-decoration: line-through; color: #888; }
22
- ${tag("/style")}
23
- `,
24
- );
25
- const load = makeDevLoader(new DevGraph(), root);
26
- const mod = await load(file);
27
- const html = String(mod.Card({ title: "hi" }));
28
- expect(html).toContain(">hi<");
29
- expect(html).toMatch(/class="card s-?[a-z0-9]+"|class="card [a-z0-9-]+"/);
30
- });
1
+ import { afterAll, expect, test } from "bun:test";
2
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { DevGraph } from "../src/dev/graph";
6
+ import { makeDevLoader } from "../src/dev/loader";
7
+
8
+ const root = mkdtempSync(join(tmpdir(), "grimoire-dev-loader-"));
9
+ afterAll(() => rmSync(root, { recursive: true, force: true }));
10
+
11
+ test("a component with a scoped <style> block renders server-side", async () => {
12
+ const file = join(root, "Card.tsx");
13
+ // the tag is split so this test file's own preload doesn't strip it
14
+ const tag = (name: string) => `<${name}>`;
15
+ writeFileSync(
16
+ file,
17
+ `export const Card = ({ title }) => <div class="card">{title}</div>;
18
+
19
+ ${tag("style")}
20
+ .card { padding: 1rem; }
21
+ .card .title { text-decoration: line-through; color: #888; }
22
+ ${tag("/style")}
23
+ `,
24
+ );
25
+ const load = makeDevLoader(new DevGraph(), root);
26
+ const mod = await load(file);
27
+ const html = String(mod.Card({ title: "hi" }));
28
+ expect(html).toContain(">hi<");
29
+ expect(html).toMatch(/class="card s-?[a-z0-9]+"|class="card [a-z0-9-]+"/);
30
+ });
package/test/env.test.ts CHANGED
@@ -1,171 +1,171 @@
1
- // packages/grimoire/src/env/env.test.ts
2
- import { afterEach, beforeEach, describe, expect, test } from "bun:test";
3
- import { mkdir, rm, writeFile } from "node:fs/promises";
4
- import { tmpdir } from "node:os";
5
- import { join } from "node:path";
6
- import { envModuleSource } from "../src/env/modules.ts";
7
- import { resolverPlugin, rewriteAliases } from "../src/server/resolver.ts";
8
-
9
- describe("$env/dynamic/public", () => {
10
- let orig: Record<string, string | undefined> = {};
11
-
12
- beforeEach(() => {
13
- orig = {
14
- PUBLIC_APP_NAME: process.env.PUBLIC_APP_NAME,
15
- PUBLIC_API_URL: process.env.PUBLIC_API_URL,
16
- SECRET_KEY: process.env.SECRET_KEY,
17
- };
18
- });
19
-
20
- afterEach(() => {
21
- for (const [k, v] of Object.entries(orig)) {
22
- if (v === undefined) delete process.env[k];
23
- else process.env[k] = v;
24
- }
25
- });
26
-
27
- test("reads PUBLIC_ vars at call time", async () => {
28
- process.env.PUBLIC_APP_NAME = "grimoire";
29
- const { env } = await import("../src/env/dynamic-public.ts");
30
- expect(env.PUBLIC_APP_NAME).toBe("grimoire");
31
- });
32
-
33
- test("reflects changes after import", async () => {
34
- const { env } = await import("../src/env/dynamic-public.ts");
35
- process.env.PUBLIC_APP_NAME = "changed";
36
- expect(env.PUBLIC_APP_NAME).toBe("changed");
37
- });
38
-
39
- test("does not expose non-PUBLIC_ vars", async () => {
40
- process.env.SECRET_KEY = "supersecret";
41
- const { env } = await import("../src/env/dynamic-public.ts");
42
- expect(env.SECRET_KEY).toBeUndefined();
43
- });
44
-
45
- test("returns empty string for missing PUBLIC_ key", async () => {
46
- delete process.env.PUBLIC_API_URL;
47
- const { env } = await import("../src/env/dynamic-public.ts");
48
- expect(env.PUBLIC_API_URL).toBe("");
49
- });
50
-
51
- test("ownKeys only includes PUBLIC_ vars", async () => {
52
- process.env.PUBLIC_APP_NAME = "grimoire";
53
- process.env.SECRET_KEY = "secret";
54
- const { env } = await import("../src/env/dynamic-public.ts");
55
- const keys = Object.keys(env);
56
- expect(keys.every((k) => k.startsWith("PUBLIC_"))).toBe(true);
57
- });
58
- });
59
-
60
- describe("$env/dynamic/private", () => {
61
- let orig: string | undefined;
62
-
63
- beforeEach(() => {
64
- orig = process.env.DATABASE_URL;
65
- });
66
-
67
- afterEach(() => {
68
- if (orig === undefined) delete process.env.DATABASE_URL;
69
- else process.env.DATABASE_URL = orig;
70
- });
71
-
72
- test("reads any env var at call time", async () => {
73
- process.env.DATABASE_URL = "postgres://localhost/test";
74
- const { env } = await import("../src/env/dynamic-private.ts");
75
- expect(env.DATABASE_URL).toBe("postgres://localhost/test");
76
- });
77
-
78
- test("reflects changes after import", async () => {
79
- const { env } = await import("../src/env/dynamic-private.ts");
80
- process.env.DATABASE_URL = "postgres://localhost/changed";
81
- expect(env.DATABASE_URL).toBe("postgres://localhost/changed");
82
- });
83
-
84
- test("returns empty string for missing key", async () => {
85
- delete process.env.DATABASE_URL;
86
- const { env } = await import("../src/env/dynamic-private.ts");
87
- expect(env.DATABASE_URL).toBe("");
88
- });
89
-
90
- test("exposes PUBLIC_ vars too", async () => {
91
- process.env.PUBLIC_APP_NAME = "grimoire";
92
- const { env } = await import("../src/env/dynamic-private.ts");
93
- expect(env.PUBLIC_APP_NAME).toBe("grimoire");
94
- });
95
- });
96
-
97
- describe("$env modules in builds", () => {
98
- test("static modules export each variable by name, and env", () => {
99
- process.env.PUBLIC_SITE = "docs";
100
- const src: string = envModuleSource("$env/static/public", "browser");
101
- expect(src).toContain('export const PUBLIC_SITE = env["PUBLIC_SITE"];');
102
- expect(src).not.toContain("SECRET");
103
- delete process.env.PUBLIC_SITE;
104
- });
105
-
106
- test("private modules can't be built for the browser", () => {
107
- for (const kind of ["static", "dynamic"]) {
108
- expect(() => envModuleSource(`$env/${kind}/private`, "browser")).toThrow(
109
- /server secrets/,
110
- );
111
- }
112
- });
113
-
114
- test("a browser build resolves $env and $lib, and inlines no secrets", async () => {
115
- const dir = join(tmpdir(), `grimoire-env-build-${Date.now()}`);
116
- await mkdir(join(dir, "src/lib"), { recursive: true });
117
- await writeFile(
118
- join(dir, "src/lib/greet.ts"),
119
- 'export const greet = (s: string) => "hi " + s;',
120
- );
121
- await writeFile(
122
- join(dir, "entry.ts"),
123
- [
124
- 'import { PUBLIC_WHO } from "$env/static/public";',
125
- 'import { env } from "$env/dynamic/public";',
126
- 'import { greet } from "$lib/greet";',
127
- "console.log(greet(PUBLIC_WHO), env.PUBLIC_WHO);",
128
- ].join("\n"),
129
- );
130
- process.env.PUBLIC_WHO = "world";
131
- process.env.SECRET_WHO = "hunter2";
132
- const cwd = process.cwd();
133
- process.chdir(dir);
134
- try {
135
- const result = await Bun.build({
136
- entrypoints: [join(dir, "entry.ts")],
137
- plugins: [resolverPlugin({}, "browser")],
138
- throw: false,
139
- });
140
- expect(result.success).toBe(true);
141
- const code = await result.outputs[0]!.text();
142
- expect(code).toContain('"world"');
143
- expect(code).toContain('"hi "');
144
- expect(code).not.toContain("hunter2");
145
- } finally {
146
- process.chdir(cwd);
147
- delete process.env.PUBLIC_WHO;
148
- delete process.env.SECRET_WHO;
149
- await rm(dir, { recursive: true, force: true });
150
- }
151
- });
152
-
153
- test("server modules get aliased imports rewritten to files", async () => {
154
- const dir = join(tmpdir(), `grimoire-alias-${Date.now()}`);
155
- await mkdir(join(dir, "lib"), { recursive: true });
156
- await writeFile(join(dir, "lib/db.ts"), "export const db = 1;");
157
- try {
158
- const out = rewriteAliases(
159
- 'import { db } from "$lib/db";\nimport x from "other";',
160
- { $lib: join(dir, "lib") },
161
- );
162
- // forward slashes even on Windows: the path lands in a string literal
163
- expect(out).toContain(
164
- `from "${join(dir, "lib/db.ts").replaceAll("\\", "/")}"`,
165
- );
166
- expect(out).toContain('from "other"');
167
- } finally {
168
- await rm(dir, { recursive: true, force: true });
169
- }
170
- });
171
- });
1
+ // packages/grimoire/src/env/env.test.ts
2
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
3
+ import { mkdir, rm, writeFile } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { envModuleSource } from "../src/env/modules.ts";
7
+ import { resolverPlugin, rewriteAliases } from "../src/server/resolver.ts";
8
+
9
+ describe("$env/dynamic/public", () => {
10
+ let orig: Record<string, string | undefined> = {};
11
+
12
+ beforeEach(() => {
13
+ orig = {
14
+ PUBLIC_APP_NAME: process.env.PUBLIC_APP_NAME,
15
+ PUBLIC_API_URL: process.env.PUBLIC_API_URL,
16
+ SECRET_KEY: process.env.SECRET_KEY,
17
+ };
18
+ });
19
+
20
+ afterEach(() => {
21
+ for (const [k, v] of Object.entries(orig)) {
22
+ if (v === undefined) delete process.env[k];
23
+ else process.env[k] = v;
24
+ }
25
+ });
26
+
27
+ test("reads PUBLIC_ vars at call time", async () => {
28
+ process.env.PUBLIC_APP_NAME = "grimoire";
29
+ const { env } = await import("../src/env/dynamic-public.ts");
30
+ expect(env.PUBLIC_APP_NAME).toBe("grimoire");
31
+ });
32
+
33
+ test("reflects changes after import", async () => {
34
+ const { env } = await import("../src/env/dynamic-public.ts");
35
+ process.env.PUBLIC_APP_NAME = "changed";
36
+ expect(env.PUBLIC_APP_NAME).toBe("changed");
37
+ });
38
+
39
+ test("does not expose non-PUBLIC_ vars", async () => {
40
+ process.env.SECRET_KEY = "supersecret";
41
+ const { env } = await import("../src/env/dynamic-public.ts");
42
+ expect(env.SECRET_KEY).toBeUndefined();
43
+ });
44
+
45
+ test("returns empty string for missing PUBLIC_ key", async () => {
46
+ delete process.env.PUBLIC_API_URL;
47
+ const { env } = await import("../src/env/dynamic-public.ts");
48
+ expect(env.PUBLIC_API_URL).toBe("");
49
+ });
50
+
51
+ test("ownKeys only includes PUBLIC_ vars", async () => {
52
+ process.env.PUBLIC_APP_NAME = "grimoire";
53
+ process.env.SECRET_KEY = "secret";
54
+ const { env } = await import("../src/env/dynamic-public.ts");
55
+ const keys = Object.keys(env);
56
+ expect(keys.every((k) => k.startsWith("PUBLIC_"))).toBe(true);
57
+ });
58
+ });
59
+
60
+ describe("$env/dynamic/private", () => {
61
+ let orig: string | undefined;
62
+
63
+ beforeEach(() => {
64
+ orig = process.env.DATABASE_URL;
65
+ });
66
+
67
+ afterEach(() => {
68
+ if (orig === undefined) delete process.env.DATABASE_URL;
69
+ else process.env.DATABASE_URL = orig;
70
+ });
71
+
72
+ test("reads any env var at call time", async () => {
73
+ process.env.DATABASE_URL = "postgres://localhost/test";
74
+ const { env } = await import("../src/env/dynamic-private.ts");
75
+ expect(env.DATABASE_URL).toBe("postgres://localhost/test");
76
+ });
77
+
78
+ test("reflects changes after import", async () => {
79
+ const { env } = await import("../src/env/dynamic-private.ts");
80
+ process.env.DATABASE_URL = "postgres://localhost/changed";
81
+ expect(env.DATABASE_URL).toBe("postgres://localhost/changed");
82
+ });
83
+
84
+ test("returns empty string for missing key", async () => {
85
+ delete process.env.DATABASE_URL;
86
+ const { env } = await import("../src/env/dynamic-private.ts");
87
+ expect(env.DATABASE_URL).toBe("");
88
+ });
89
+
90
+ test("exposes PUBLIC_ vars too", async () => {
91
+ process.env.PUBLIC_APP_NAME = "grimoire";
92
+ const { env } = await import("../src/env/dynamic-private.ts");
93
+ expect(env.PUBLIC_APP_NAME).toBe("grimoire");
94
+ });
95
+ });
96
+
97
+ describe("$env modules in builds", () => {
98
+ test("static modules export each variable by name, and env", () => {
99
+ process.env.PUBLIC_SITE = "docs";
100
+ const src: string = envModuleSource("$env/static/public", "browser");
101
+ expect(src).toContain('export const PUBLIC_SITE = env["PUBLIC_SITE"];');
102
+ expect(src).not.toContain("SECRET");
103
+ delete process.env.PUBLIC_SITE;
104
+ });
105
+
106
+ test("private modules can't be built for the browser", () => {
107
+ for (const kind of ["static", "dynamic"]) {
108
+ expect(() => envModuleSource(`$env/${kind}/private`, "browser")).toThrow(
109
+ /server secrets/,
110
+ );
111
+ }
112
+ });
113
+
114
+ test("a browser build resolves $env and $lib, and inlines no secrets", async () => {
115
+ const dir = join(tmpdir(), `grimoire-env-build-${Date.now()}`);
116
+ await mkdir(join(dir, "src/lib"), { recursive: true });
117
+ await writeFile(
118
+ join(dir, "src/lib/greet.ts"),
119
+ 'export const greet = (s: string) => "hi " + s;',
120
+ );
121
+ await writeFile(
122
+ join(dir, "entry.ts"),
123
+ [
124
+ 'import { PUBLIC_WHO } from "$env/static/public";',
125
+ 'import { env } from "$env/dynamic/public";',
126
+ 'import { greet } from "$lib/greet";',
127
+ "console.log(greet(PUBLIC_WHO), env.PUBLIC_WHO);",
128
+ ].join("\n"),
129
+ );
130
+ process.env.PUBLIC_WHO = "world";
131
+ process.env.SECRET_WHO = "hunter2";
132
+ const cwd = process.cwd();
133
+ process.chdir(dir);
134
+ try {
135
+ const result = await Bun.build({
136
+ entrypoints: [join(dir, "entry.ts")],
137
+ plugins: [resolverPlugin({}, "browser")],
138
+ throw: false,
139
+ });
140
+ expect(result.success).toBe(true);
141
+ const code = await result.outputs[0]!.text();
142
+ expect(code).toContain('"world"');
143
+ expect(code).toContain('"hi "');
144
+ expect(code).not.toContain("hunter2");
145
+ } finally {
146
+ process.chdir(cwd);
147
+ delete process.env.PUBLIC_WHO;
148
+ delete process.env.SECRET_WHO;
149
+ await rm(dir, { recursive: true, force: true });
150
+ }
151
+ });
152
+
153
+ test("server modules get aliased imports rewritten to files", async () => {
154
+ const dir = join(tmpdir(), `grimoire-alias-${Date.now()}`);
155
+ await mkdir(join(dir, "lib"), { recursive: true });
156
+ await writeFile(join(dir, "lib/db.ts"), "export const db = 1;");
157
+ try {
158
+ const out = rewriteAliases(
159
+ 'import { db } from "$lib/db";\nimport x from "other";',
160
+ { $lib: join(dir, "lib") },
161
+ );
162
+ // forward slashes even on Windows: the path lands in a string literal
163
+ expect(out).toContain(
164
+ `from "${join(dir, "lib/db.ts").replaceAll("\\", "/")}"`,
165
+ );
166
+ expect(out).toContain('from "other"');
167
+ } finally {
168
+ await rm(dir, { recursive: true, force: true });
169
+ }
170
+ });
171
+ });
@@ -18,7 +18,10 @@ let dir: string;
18
18
  beforeAll(async () => {
19
19
  dir = join(tmpdir(), `grimoire-force-prod-${Date.now()}`);
20
20
  await mkdir(join(dir, "ping"), { recursive: true });
21
- await writeFile(join(dir, "ping", "+server.ts"), "export const GET = () => new Response('pong');");
21
+ await writeFile(
22
+ join(dir, "ping", "+server.ts"),
23
+ "export const GET = () => new Response('pong');",
24
+ );
22
25
  });
23
26
 
24
27
  afterAll(() => rm(dir, { recursive: true, force: true }));
@@ -38,7 +41,9 @@ describe("_forceProduction", () => {
38
41
  });
39
42
  expect(app.config.dev).toBe(false);
40
43
  // the probe is the HMR client script: harmless, and only served in dev
41
- const res = await app.fetch(new Request("http://localhost/__grimoire__/hmr-client.js"));
44
+ const res = await app.fetch(
45
+ new Request("http://localhost/__grimoire__/hmr-client.js"),
46
+ );
42
47
  expect(res.status).toBe(404);
43
48
  });
44
49
  });
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from "bun:test";
2
- import { optimizeImages, EAGER_IMAGE_COUNT } from "../src/rendering/images.ts";
2
+ import { EAGER_IMAGE_COUNT, optimizeImages } from "../src/rendering/images.ts";
3
3
 
4
4
  const imgs = (n: number, attrs = "") =>
5
5
  Array.from({ length: n }, (_, i) => `<img src="/${i}.png"${attrs}>`).join("");
@@ -11,30 +11,42 @@ describe("optimizeImages", () => {
11
11
  for (const t of tags.slice(0, EAGER_IMAGE_COUNT))
12
12
  expect(t).not.toContain("loading");
13
13
  for (const t of tags.slice(EAGER_IMAGE_COUNT))
14
- expect(t).toBe(`<img src="/${tags.indexOf(t)}.png" loading="lazy" decoding="async">`);
14
+ expect(t).toBe(
15
+ `<img src="/${tags.indexOf(t)}.png" loading="lazy" decoding="async">`,
16
+ );
15
17
  });
16
18
 
17
19
  test("respects the author's loading and decoding", () => {
18
20
  const out = optimizeImages(
19
- imgs(EAGER_IMAGE_COUNT) + `<img src="/x.png" loading="eager" decoding="sync" />`,
21
+ imgs(EAGER_IMAGE_COUNT) +
22
+ `<img src="/x.png" loading="eager" decoding="sync" />`,
20
23
  );
21
- expect(out.endsWith(`<img src="/x.png" loading="eager" decoding="sync" />`)).toBe(true);
24
+ expect(
25
+ out.endsWith(`<img src="/x.png" loading="eager" decoding="sync" />`),
26
+ ).toBe(true);
22
27
  });
23
28
 
24
29
  test("handles self-closing tags and never touches script strings", () => {
25
30
  const script = `<script>var s='<img src="q.png">'</script>`;
26
- const out = optimizeImages(imgs(EAGER_IMAGE_COUNT) + script + `<img src="/y.png" />`);
31
+ const out = optimizeImages(
32
+ imgs(EAGER_IMAGE_COUNT) + script + `<img src="/y.png" />`,
33
+ );
27
34
  expect(out).toContain(script);
28
- expect(out).toContain(`<img src="/y.png" loading="lazy" decoding="async" />`);
35
+ expect(out).toContain(
36
+ `<img src="/y.png" loading="lazy" decoding="async" />`,
37
+ );
29
38
  });
30
39
 
31
40
  test("a priority image is fetched first and does not use up the eager budget", () => {
32
41
  const hero = `<img src="/hero.png" data-sigil-priority>`;
33
42
  const out = optimizeImages(hero + imgs(EAGER_IMAGE_COUNT + 1));
34
43
  const tags = out.match(/<img[^>]*>/g)!;
35
- expect(tags[0]).toBe(`<img src="/hero.png" data-sigil-priority fetchpriority="high">`);
44
+ expect(tags[0]).toBe(
45
+ `<img src="/hero.png" data-sigil-priority fetchpriority="high">`,
46
+ );
36
47
  // the four after it are still eager, the fifth is deferred
37
- for (const t of tags.slice(1, 1 + EAGER_IMAGE_COUNT)) expect(t).not.toContain("loading");
48
+ for (const t of tags.slice(1, 1 + EAGER_IMAGE_COUNT))
49
+ expect(t).not.toContain("loading");
38
50
  expect(tags.at(-1)).toContain('loading="lazy"');
39
51
  });
40
52
 
@@ -1,61 +1,61 @@
1
- import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
- import { mkdir, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
- import { createApp } from "../src/server";
6
- import type { GrimoirePlugin } from "../src/types";
7
-
8
- /**
9
- * `X-Grimoire-Locals` is how the coordinator tells a worker who the user is.
10
- * A server that answers the public directly has no coordinator, so the header
11
- * is just something a client typed: honouring it would let anyone choose their
12
- * own `locals`, including a session.
13
- */
14
-
15
- let dir: string;
16
-
17
- beforeAll(async () => {
18
- dir = join(tmpdir(), `grimoire-locals-${Date.now()}`);
19
- await mkdir(join(dir, "who"), { recursive: true });
20
- await writeFile(
21
- join(dir, "who", "+server.ts"),
22
- "export function GET({ locals }) { return Response.json({ user: locals.user ?? null }); }",
23
- );
24
- });
25
-
26
- afterAll(() => rm(dir, { recursive: true, force: true }));
27
-
28
- // What a signing plugin would do once it has verified the coordinator's mark.
29
- const trusting: GrimoirePlugin = {
30
- name: "trusting",
31
- deserializeLocals: (raw) => ({ user: raw }) as unknown as App.Locals,
32
- };
33
-
34
- async function whoAmI(
35
- behindCoordinator: boolean | undefined,
36
- ): Promise<unknown> {
37
- const app = await createApp({
38
- routes: dir,
39
- _skipBuild: true,
40
- plugins: [trusting],
41
- _behindCoordinator: behindCoordinator,
42
- });
43
- const res = await app.fetch(
44
- new Request("http://localhost/who", {
45
- headers: { "X-Grimoire-Locals": "admin" },
46
- }),
47
- );
48
- // no close(): it starts a process-wide shutdown, and nothing here holds a resource
49
- return ((await res.json()) as { user: unknown }).user;
50
- }
51
-
52
- describe("X-Grimoire-Locals", () => {
53
- test("is ignored by a server with no coordinator in front of it", async () => {
54
- expect(await whoAmI(undefined)).toBeNull();
55
- expect(await whoAmI(false)).toBeNull();
56
- });
57
-
58
- test("is honoured by a worker behind the coordinator", async () => {
59
- expect(await whoAmI(true)).toBe("admin");
60
- });
61
- });
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { mkdir, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { createApp } from "../src/server";
6
+ import type { GrimoirePlugin } from "../src/types";
7
+
8
+ /**
9
+ * `X-Grimoire-Locals` is how the coordinator tells a worker who the user is.
10
+ * A server that answers the public directly has no coordinator, so the header
11
+ * is just something a client typed: honouring it would let anyone choose their
12
+ * own `locals`, including a session.
13
+ */
14
+
15
+ let dir: string;
16
+
17
+ beforeAll(async () => {
18
+ dir = join(tmpdir(), `grimoire-locals-${Date.now()}`);
19
+ await mkdir(join(dir, "who"), { recursive: true });
20
+ await writeFile(
21
+ join(dir, "who", "+server.ts"),
22
+ "export function GET({ locals }) { return Response.json({ user: locals.user ?? null }); }",
23
+ );
24
+ });
25
+
26
+ afterAll(() => rm(dir, { recursive: true, force: true }));
27
+
28
+ // What a signing plugin would do once it has verified the coordinator's mark.
29
+ const trusting: GrimoirePlugin = {
30
+ name: "trusting",
31
+ deserializeLocals: (raw) => ({ user: raw }) as unknown as App.Locals,
32
+ };
33
+
34
+ async function whoAmI(
35
+ behindCoordinator: boolean | undefined,
36
+ ): Promise<unknown> {
37
+ const app = await createApp({
38
+ routes: dir,
39
+ _skipBuild: true,
40
+ plugins: [trusting],
41
+ _behindCoordinator: behindCoordinator,
42
+ });
43
+ const res = await app.fetch(
44
+ new Request("http://localhost/who", {
45
+ headers: { "X-Grimoire-Locals": "admin" },
46
+ }),
47
+ );
48
+ // no close(): it starts a process-wide shutdown, and nothing here holds a resource
49
+ return ((await res.json()) as { user: unknown }).user;
50
+ }
51
+
52
+ describe("X-Grimoire-Locals", () => {
53
+ test("is ignored by a server with no coordinator in front of it", async () => {
54
+ expect(await whoAmI(undefined)).toBeNull();
55
+ expect(await whoAmI(false)).toBeNull();
56
+ });
57
+
58
+ test("is honoured by a worker behind the coordinator", async () => {
59
+ expect(await whoAmI(true)).toBe("admin");
60
+ });
61
+ });