@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
@@ -1,78 +1,78 @@
1
- import { afterEach, describe, expect, test } from "bun:test";
2
- import {
3
- CLIENT_ADDRESS_HEADER,
4
- clientAddress,
5
- resolveClientAddress,
6
- } from "../src/server/client-address.ts";
7
-
8
- const req = (headers: Record<string, string> = {}) =>
9
- new Request("http://localhost/", { headers });
10
- const server = (address: string | null) =>
11
- ({
12
- requestIP: () => (address ? { address, family: "IPv4", port: 1 } : null),
13
- }) as never;
14
-
15
- describe("the client address", () => {
16
- const secret = process.env.GRIMOIRE_INTERNAL_SECRET;
17
- afterEach(() => {
18
- if (secret === undefined) delete process.env.GRIMOIRE_INTERNAL_SECRET;
19
- else process.env.GRIMOIRE_INTERNAL_SECRET = secret;
20
- });
21
-
22
- test("a direct connection is the visitor, whatever it claims", () => {
23
- expect(
24
- resolveClientAddress(req({ "x-forwarded-for": "1.1.1.1" }), "8.8.8.8"),
25
- ).toBe("8.8.8.8");
26
- });
27
-
28
- test("behind a local or private proxy, the visitor is the last forwarded entry", () => {
29
- for (const proxy of [
30
- "127.0.0.1",
31
- "::1",
32
- "10.0.0.2",
33
- "172.18.0.5",
34
- "192.168.1.9",
35
- "::ffff:127.0.0.1",
36
- ]) {
37
- expect(
38
- resolveClientAddress(
39
- req({ "x-forwarded-for": "6.6.6.6, 5.5.5.5" }),
40
- proxy,
41
- ),
42
- ).toBe("5.5.5.5");
43
- }
44
- });
45
-
46
- test("172.32.x is public, not Docker's range", () => {
47
- expect(
48
- resolveClientAddress(req({ "x-forwarded-for": "5.5.5.5" }), "172.32.0.1"),
49
- ).toBe("172.32.0.1");
50
- });
51
-
52
- test("a local connection with no forwarding header is itself", () => {
53
- expect(resolveClientAddress(req(), "127.0.0.1")).toBe("127.0.0.1");
54
- expect(
55
- resolveClientAddress(req({ "x-real-ip": "4.4.4.4" }), "127.0.0.1"),
56
- ).toBe("4.4.4.4");
57
- });
58
-
59
- test("outside a worker the coordinator's header is not believed", () => {
60
- delete process.env.GRIMOIRE_INTERNAL_SECRET;
61
- expect(
62
- clientAddress(
63
- req({ [CLIENT_ADDRESS_HEADER]: "1.1.1.1" }),
64
- server("8.8.8.8"),
65
- ),
66
- ).toBe("8.8.8.8");
67
- });
68
-
69
- test("inside a worker it is the address the coordinator worked out", () => {
70
- process.env.GRIMOIRE_INTERNAL_SECRET = "s";
71
- expect(
72
- clientAddress(
73
- req({ [CLIENT_ADDRESS_HEADER]: "5.5.5.5" }),
74
- server("127.0.0.1"),
75
- ),
76
- ).toBe("5.5.5.5");
77
- });
78
- });
1
+ import { afterEach, describe, expect, test } from "bun:test";
2
+ import {
3
+ CLIENT_ADDRESS_HEADER,
4
+ clientAddress,
5
+ resolveClientAddress,
6
+ } from "../src/server/client-address.ts";
7
+
8
+ const req = (headers: Record<string, string> = {}) =>
9
+ new Request("http://localhost/", { headers });
10
+ const server = (address: string | null) =>
11
+ ({
12
+ requestIP: () => (address ? { address, family: "IPv4", port: 1 } : null),
13
+ }) as never;
14
+
15
+ describe("the client address", () => {
16
+ const secret = process.env.GRIMOIRE_INTERNAL_SECRET;
17
+ afterEach(() => {
18
+ if (secret === undefined) delete process.env.GRIMOIRE_INTERNAL_SECRET;
19
+ else process.env.GRIMOIRE_INTERNAL_SECRET = secret;
20
+ });
21
+
22
+ test("a direct connection is the visitor, whatever it claims", () => {
23
+ expect(
24
+ resolveClientAddress(req({ "x-forwarded-for": "1.1.1.1" }), "8.8.8.8"),
25
+ ).toBe("8.8.8.8");
26
+ });
27
+
28
+ test("behind a local or private proxy, the visitor is the last forwarded entry", () => {
29
+ for (const proxy of [
30
+ "127.0.0.1",
31
+ "::1",
32
+ "10.0.0.2",
33
+ "172.18.0.5",
34
+ "192.168.1.9",
35
+ "::ffff:127.0.0.1",
36
+ ]) {
37
+ expect(
38
+ resolveClientAddress(
39
+ req({ "x-forwarded-for": "6.6.6.6, 5.5.5.5" }),
40
+ proxy,
41
+ ),
42
+ ).toBe("5.5.5.5");
43
+ }
44
+ });
45
+
46
+ test("172.32.x is public, not Docker's range", () => {
47
+ expect(
48
+ resolveClientAddress(req({ "x-forwarded-for": "5.5.5.5" }), "172.32.0.1"),
49
+ ).toBe("172.32.0.1");
50
+ });
51
+
52
+ test("a local connection with no forwarding header is itself", () => {
53
+ expect(resolveClientAddress(req(), "127.0.0.1")).toBe("127.0.0.1");
54
+ expect(
55
+ resolveClientAddress(req({ "x-real-ip": "4.4.4.4" }), "127.0.0.1"),
56
+ ).toBe("4.4.4.4");
57
+ });
58
+
59
+ test("outside a worker the coordinator's header is not believed", () => {
60
+ delete process.env.GRIMOIRE_INTERNAL_SECRET;
61
+ expect(
62
+ clientAddress(
63
+ req({ [CLIENT_ADDRESS_HEADER]: "1.1.1.1" }),
64
+ server("8.8.8.8"),
65
+ ),
66
+ ).toBe("8.8.8.8");
67
+ });
68
+
69
+ test("inside a worker it is the address the coordinator worked out", () => {
70
+ process.env.GRIMOIRE_INTERNAL_SECRET = "s";
71
+ expect(
72
+ clientAddress(
73
+ req({ [CLIENT_ADDRESS_HEADER]: "5.5.5.5" }),
74
+ server("127.0.0.1"),
75
+ ),
76
+ ).toBe("5.5.5.5");
77
+ });
78
+ });
@@ -1,61 +1,61 @@
1
- import { beforeEach, describe, expect, test } from "bun:test";
2
- import { ensureCsrf, onSubmitEnsureCsrf } from "../src/client/csrf.ts";
3
-
4
- // The root test/preload.ts installs happy-dom globals; this package's
5
- // compiler options have no DOM lib, so reach them untyped.
6
- const { window, document, Event } = globalThis as any;
7
-
8
- window.location.href = "http://localhost/";
9
-
10
- const token = (form: any) =>
11
- form.querySelector('input[name="_csrf"]')?.value ?? null;
12
-
13
- beforeEach(() => {
14
- document.cookie = "_csrf=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
15
- document.body.innerHTML = "";
16
- });
17
-
18
- describe("client CSRF", () => {
19
- test("a client-rendered form gets the cookie's token", () => {
20
- document.cookie = "_csrf=abc123; path=/";
21
- const form = document.createElement("form") as any;
22
- form.setAttribute("method", "post");
23
- ensureCsrf(form);
24
- expect(token(form)).toBe("abc123");
25
- expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
26
- expect(form.firstElementChild.hasAttribute("data-grimoire-csrf")).toBe(
27
- true,
28
- );
29
- });
30
-
31
- test("a stale SSR token is replaced, not duplicated", () => {
32
- document.cookie = "_csrf=fresh; path=/";
33
- document.body.innerHTML = `<form method="post"><input type="hidden" name="_csrf" value="stale" data-grimoire-csrf></form>`;
34
- const form = document.querySelector("form") as any;
35
- ensureCsrf(form);
36
- expect(token(form)).toBe("fresh");
37
- expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
38
- });
39
-
40
- test("no cookie, no change", () => {
41
- const form = document.createElement("form") as any;
42
- ensureCsrf(form);
43
- expect(token(form)).toBeNull();
44
- });
45
-
46
- test("the submit listener only touches POST forms", () => {
47
- document.cookie = "_csrf=tok; path=/";
48
- document.body.innerHTML = `<form method="post" id="p"></form><form id="g"></form>`;
49
- document.addEventListener("submit", onSubmitEnsureCsrf as any, true);
50
- for (const id of ["p", "g"]) {
51
- document
52
- .getElementById(id)!
53
- .dispatchEvent(
54
- new Event("submit", { bubbles: true, cancelable: true }),
55
- );
56
- }
57
- document.removeEventListener("submit", onSubmitEnsureCsrf as any, true);
58
- expect(token(document.getElementById("p"))).toBe("tok");
59
- expect(token(document.getElementById("g"))).toBeNull();
60
- });
61
- });
1
+ import { beforeEach, describe, expect, test } from "bun:test";
2
+ import { ensureCsrf, onSubmitEnsureCsrf } from "../src/client/csrf.ts";
3
+
4
+ // The root test/preload.ts installs happy-dom globals; this package's
5
+ // compiler options have no DOM lib, so reach them untyped.
6
+ const { window, document, Event } = globalThis as any;
7
+
8
+ window.location.href = "http://localhost/";
9
+
10
+ const token = (form: any) =>
11
+ form.querySelector('input[name="_csrf"]')?.value ?? null;
12
+
13
+ beforeEach(() => {
14
+ document.cookie = "_csrf=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
15
+ document.body.innerHTML = "";
16
+ });
17
+
18
+ describe("client CSRF", () => {
19
+ test("a client-rendered form gets the cookie's token", () => {
20
+ document.cookie = "_csrf=abc123; path=/";
21
+ const form = document.createElement("form") as any;
22
+ form.setAttribute("method", "post");
23
+ ensureCsrf(form);
24
+ expect(token(form)).toBe("abc123");
25
+ expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
26
+ expect(form.firstElementChild.hasAttribute("data-grimoire-csrf")).toBe(
27
+ true,
28
+ );
29
+ });
30
+
31
+ test("a stale SSR token is replaced, not duplicated", () => {
32
+ document.cookie = "_csrf=fresh; path=/";
33
+ document.body.innerHTML = `<form method="post"><input type="hidden" name="_csrf" value="stale" data-grimoire-csrf></form>`;
34
+ const form = document.querySelector("form") as any;
35
+ ensureCsrf(form);
36
+ expect(token(form)).toBe("fresh");
37
+ expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
38
+ });
39
+
40
+ test("no cookie, no change", () => {
41
+ const form = document.createElement("form") as any;
42
+ ensureCsrf(form);
43
+ expect(token(form)).toBeNull();
44
+ });
45
+
46
+ test("the submit listener only touches POST forms", () => {
47
+ document.cookie = "_csrf=tok; path=/";
48
+ document.body.innerHTML = `<form method="post" id="p"></form><form id="g"></form>`;
49
+ document.addEventListener("submit", onSubmitEnsureCsrf as any, true);
50
+ for (const id of ["p", "g"]) {
51
+ document
52
+ .getElementById(id)!
53
+ .dispatchEvent(
54
+ new Event("submit", { bubbles: true, cancelable: true }),
55
+ );
56
+ }
57
+ document.removeEventListener("submit", onSubmitEnsureCsrf as any, true);
58
+ expect(token(document.getElementById("p"))).toBe("tok");
59
+ expect(token(document.getElementById("g"))).toBeNull();
60
+ });
61
+ });
@@ -1,101 +1,101 @@
1
- import { afterEach, describe, expect, test } from "bun:test";
2
- import {
3
- existsSync,
4
- mkdirSync,
5
- mkdtempSync,
6
- readdirSync,
7
- rmSync,
8
- } from "node:fs";
9
- import { tmpdir } from "node:os";
10
- import { join } from "node:path";
11
- import {
12
- clientDir,
13
- pruneClientDir,
14
- resetClientDir,
15
- } from "../src/server/client-dir.ts";
16
-
17
- let root: string;
18
- const touch = async (path: string) => {
19
- mkdirSync(join(path, ".."), { recursive: true });
20
- await Bun.write(path, "x");
21
- };
22
-
23
- afterEach(() => rmSync(root, { recursive: true, force: true }));
24
-
25
- describe("the client bundle directory", () => {
26
- test("lives in .grimoire, not public", () => {
27
- root = mkdtempSync(join(tmpdir(), "client-dir-"));
28
- expect(clientDir(root)).toBe(join(root, ".grimoire", "client"));
29
- });
30
-
31
- test("a build starts from nothing, and the old public/__grimoire__ goes", async () => {
32
- root = mkdtempSync(join(tmpdir(), "client-dir-"));
33
- await touch(join(clientDir(root), "chunk-aaaaaaaa.js"));
34
- await touch(join(root, "public", "__grimoire__", "hydrate.js"));
35
- await touch(join(root, "public", "style.css"));
36
-
37
- await resetClientDir(root);
38
-
39
- expect(existsSync(clientDir(root))).toBe(false);
40
- expect(existsSync(join(root, "public", "__grimoire__"))).toBe(false);
41
- // the app's own files are untouched
42
- expect(existsSync(join(root, "public", "style.css"))).toBe(true);
43
- });
44
-
45
- test("a dev rebuild drops the chunks it replaced and keeps the dev runtime", async () => {
46
- root = mkdtempSync(join(tmpdir(), "client-dir-"));
47
- const dir = clientDir(root);
48
- for (const f of [
49
- "hydrate.js",
50
- "chunk-old00000.js",
51
- "chunk-new00000.js",
52
- "runtime.js",
53
- "grimoire-client.js",
54
- ])
55
- await touch(join(dir, f));
56
-
57
- await pruneClientDir(
58
- [join(dir, "hydrate.js"), join(dir, "chunk-new00000.js")],
59
- root,
60
- );
61
-
62
- expect(readdirSync(dir).sort()).toEqual([
63
- "chunk-new00000.js",
64
- "grimoire-client.js",
65
- "hydrate.js",
66
- "runtime.js",
67
- ]);
68
- });
69
- });
70
-
71
- describe("the client entry", () => {
72
- test("is recorded by name and read back, falling back to hydrate.js", async () => {
73
- const { hydrateEntry, recordClientEntry } = await import(
74
- "../src/server/client-entry.ts"
75
- );
76
- root = mkdtempSync(join(tmpdir(), "client-entry-"));
77
- expect(hydrateEntry(root)).toBe("hydrate.js");
78
- mkdirSync(join(root, ".grimoire"), { recursive: true });
79
- recordClientEntry(
80
- [
81
- { path: join(clientDir(root), "chunk-aaaaaaaa.js"), kind: "chunk" },
82
- {
83
- path: join(clientDir(root), "hydrate-k3x9q2ab.js"),
84
- kind: "entry-point",
85
- },
86
- ],
87
- root,
88
- );
89
- expect(hydrateEntry(root)).toBe("hydrate-k3x9q2ab.js");
90
- });
91
-
92
- test("a hashed entry is cached for good; a fixed name is revalidated", async () => {
93
- const { cacheControlFor } = await import("../src/server/asset-cache.ts");
94
- expect(cacheControlFor("/__grimoire__/hydrate-k3x9q2ab.js", "")).toBe(
95
- "immutable",
96
- );
97
- expect(cacheControlFor("/__grimoire__/hydrate.js", "")).toBe("revalidate");
98
- // a public file shaped like one is not assumed to be hashed
99
- expect(cacheControlFor("/jquery-plugins.js", "")).toBe("revalidate");
100
- });
101
- });
1
+ import { afterEach, describe, expect, test } from "bun:test";
2
+ import {
3
+ existsSync,
4
+ mkdirSync,
5
+ mkdtempSync,
6
+ readdirSync,
7
+ rmSync,
8
+ } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import {
12
+ clientDir,
13
+ pruneClientDir,
14
+ resetClientDir,
15
+ } from "../src/server/client-dir.ts";
16
+
17
+ let root: string;
18
+ const touch = async (path: string) => {
19
+ mkdirSync(join(path, ".."), { recursive: true });
20
+ await Bun.write(path, "x");
21
+ };
22
+
23
+ afterEach(() => rmSync(root, { recursive: true, force: true }));
24
+
25
+ describe("the client bundle directory", () => {
26
+ test("lives in .grimoire, not public", () => {
27
+ root = mkdtempSync(join(tmpdir(), "client-dir-"));
28
+ expect(clientDir(root)).toBe(join(root, ".grimoire", "client"));
29
+ });
30
+
31
+ test("a build starts from nothing, and the old public/__grimoire__ goes", async () => {
32
+ root = mkdtempSync(join(tmpdir(), "client-dir-"));
33
+ await touch(join(clientDir(root), "chunk-aaaaaaaa.js"));
34
+ await touch(join(root, "public", "__grimoire__", "hydrate.js"));
35
+ await touch(join(root, "public", "style.css"));
36
+
37
+ await resetClientDir(root);
38
+
39
+ expect(existsSync(clientDir(root))).toBe(false);
40
+ expect(existsSync(join(root, "public", "__grimoire__"))).toBe(false);
41
+ // the app's own files are untouched
42
+ expect(existsSync(join(root, "public", "style.css"))).toBe(true);
43
+ });
44
+
45
+ test("a dev rebuild drops the chunks it replaced and keeps the dev runtime", async () => {
46
+ root = mkdtempSync(join(tmpdir(), "client-dir-"));
47
+ const dir = clientDir(root);
48
+ for (const f of [
49
+ "hydrate.js",
50
+ "chunk-old00000.js",
51
+ "chunk-new00000.js",
52
+ "runtime.js",
53
+ "grimoire-client.js",
54
+ ])
55
+ await touch(join(dir, f));
56
+
57
+ await pruneClientDir(
58
+ [join(dir, "hydrate.js"), join(dir, "chunk-new00000.js")],
59
+ root,
60
+ );
61
+
62
+ expect(readdirSync(dir).sort()).toEqual([
63
+ "chunk-new00000.js",
64
+ "grimoire-client.js",
65
+ "hydrate.js",
66
+ "runtime.js",
67
+ ]);
68
+ });
69
+ });
70
+
71
+ describe("the client entry", () => {
72
+ test("is recorded by name and read back, falling back to hydrate.js", async () => {
73
+ const { hydrateEntry, recordClientEntry } = await import(
74
+ "../src/server/client-entry.ts"
75
+ );
76
+ root = mkdtempSync(join(tmpdir(), "client-entry-"));
77
+ expect(hydrateEntry(root)).toBe("hydrate.js");
78
+ mkdirSync(join(root, ".grimoire"), { recursive: true });
79
+ recordClientEntry(
80
+ [
81
+ { path: join(clientDir(root), "chunk-aaaaaaaa.js"), kind: "chunk" },
82
+ {
83
+ path: join(clientDir(root), "hydrate-k3x9q2ab.js"),
84
+ kind: "entry-point",
85
+ },
86
+ ],
87
+ root,
88
+ );
89
+ expect(hydrateEntry(root)).toBe("hydrate-k3x9q2ab.js");
90
+ });
91
+
92
+ test("a hashed entry is cached for good; a fixed name is revalidated", async () => {
93
+ const { cacheControlFor } = await import("../src/server/asset-cache.ts");
94
+ expect(cacheControlFor("/__grimoire__/hydrate-k3x9q2ab.js", "")).toBe(
95
+ "immutable",
96
+ );
97
+ expect(cacheControlFor("/__grimoire__/hydrate.js", "")).toBe("revalidate");
98
+ // a public file shaped like one is not assumed to be hashed
99
+ expect(cacheControlFor("/jquery-plugins.js", "")).toBe("revalidate");
100
+ });
101
+ });
@@ -299,9 +299,14 @@ describe("shared-element view transitions", () => {
299
299
  const docEnv = document as unknown as {
300
300
  startViewTransition?: (cb: () => void) => unknown;
301
301
  };
302
- const seen: { oldNamed: string; otherNamed: string; newNamed: string }[] = [];
302
+ const seen: { oldNamed: string; otherNamed: string; newNamed: string }[] =
303
+ [];
303
304
  docEnv.startViewTransition = (cb: () => void) => {
304
- const before = { oldNamed: nameOf(oldHero), otherNamed: nameOf(other), newNamed: "" };
305
+ const before = {
306
+ oldNamed: nameOf(oldHero),
307
+ otherNamed: nameOf(other),
308
+ newNamed: "",
309
+ };
305
310
  cb();
306
311
  before.newNamed = nameOf(newHero!);
307
312
  seen.push(before);
@@ -313,7 +318,9 @@ describe("shared-element view transitions", () => {
313
318
  await router.navigate("/shared");
314
319
  // the old element is named when the browser snapshots, the new one
315
320
  // once the swap ran; an element with no partner is left alone
316
- expect(seen).toEqual([{ oldNamed: "hero-7", otherNamed: "", newNamed: "hero-7" }]);
321
+ expect(seen).toEqual([
322
+ { oldNamed: "hero-7", otherNamed: "", newNamed: "hero-7" },
323
+ ]);
317
324
  await Promise.resolve();
318
325
  await Promise.resolve();
319
326
  expect(nameOf(newHero!)).toBe(""); // cleaned up when it finished
@@ -338,7 +345,9 @@ describe("shared-element view transitions", () => {
338
345
  };
339
346
  let named = 0;
340
347
  docEnv.startViewTransition = (cb: () => void) => {
341
- named = document.querySelectorAll('[style*="view-transition-name"]').length;
348
+ named = document.querySelectorAll(
349
+ '[style*="view-transition-name"]',
350
+ ).length;
342
351
  cb();
343
352
  return undefined;
344
353
  };
@@ -457,7 +466,9 @@ describe("prefetch reuse", () => {
457
466
  a.setAttribute("href", "/hov");
458
467
  document.body.appendChild(a);
459
468
  a.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
460
- a.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
469
+ a.dispatchEvent(
470
+ new MouseEvent("click", { bubbles: true, cancelable: true }),
471
+ );
461
472
  await Bun.sleep(200); // past the 80ms hover delay
462
473
  expect(root.childNodes.length).toBeGreaterThan(0); // the click navigated
463
474
  expect(shim.calls.length).toBe(1); // its own request, not a second
@@ -766,6 +777,27 @@ describe("invalidate", () => {
766
777
  });
767
778
  });
768
779
 
780
+ describe("invalidate keeps the query string", () => {
781
+ test("a filtered page reloads with its filters", async () => {
782
+ const root = document.createElement("div");
783
+ root.id = "grimoire-root";
784
+ document.body.appendChild(root);
785
+ router.initRouter({ "/search": () => document.createElement("main") }, []);
786
+ const shim = captureFetch();
787
+ try {
788
+ await router.navigate("/search?q=dune&page=2");
789
+ shim.calls.length = 0;
790
+ await router.invalidateAll();
791
+ expect(shim.calls.length).toBe(1);
792
+ expect(shim.calls[0]!.input).toContain("q=dune");
793
+ expect(shim.calls[0]!.input).toContain("page=2");
794
+ } finally {
795
+ shim.restore();
796
+ root.remove();
797
+ }
798
+ });
799
+ });
800
+
769
801
  describe("navigate — under a base", () => {
770
802
  test("fetches and records the full path, but routes and loads see the route path", async () => {
771
803
  const { setBase } = await import("../src/base.ts");
@@ -0,0 +1,84 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { envReadsIn, importConfig, unsetConfigEnv } from "../src/config-file";
6
+
7
+ /**
8
+ * sigil.config.ts is loaded by `sigil build`, the coordinator and the CLI,
9
+ * not only where requests are served. A variable it reads while loading is
10
+ * read in all of those, and `adapter(Bun.env.DATABASE_URL!)` broke the build.
11
+ */
12
+ describe("environment reads in sigil.config.ts", () => {
13
+ test("finds every spelling", () => {
14
+ const src = `
15
+ adapter(Bun.env.DATABASE_URL!);
16
+ const a = process.env.SECRET_A;
17
+ const b = import.meta.env["SECRET_B"];
18
+ const c = Bun.env['SECRET_C'];
19
+ `;
20
+ expect(envReadsIn(src).sort()).toEqual(
21
+ ["DATABASE_URL", "SECRET_A", "SECRET_B", "SECRET_C"].sort(),
22
+ );
23
+ });
24
+
25
+ test("a read with a fallback is not a problem", () => {
26
+ expect(
27
+ envReadsIn(
28
+ "port: Number(Bun.env.PORT ?? 3000), host: process.env.HOST || 'x'",
29
+ ),
30
+ ).toEqual([]);
31
+ });
32
+
33
+ test("only unset or empty ones are reported", () => {
34
+ expect(unsetConfigEnv(["A", "B", "C"], { A: "1", B: "" })).toEqual([
35
+ "B",
36
+ "C",
37
+ ]);
38
+ });
39
+
40
+ function project(source: string): string {
41
+ const dir = mkdtempSync(join(tmpdir(), "sigil-config-"));
42
+ writeFileSync(join(dir, "sigil.config.ts"), source);
43
+ return dir;
44
+ }
45
+
46
+ test("warns, naming the variable, when it is unset at load", async () => {
47
+ const name = `SIGIL_CFG_TEST_${Date.now()}`;
48
+ const dir = project(
49
+ `export default { port: Number(Bun.env.${name}) || undefined, x: Bun.env.${name}! };`,
50
+ );
51
+ const warnings: string[] = [];
52
+ const config = await importConfig(dir, { warn: (m) => warnings.push(m) });
53
+ expect(config).toHaveProperty("x");
54
+ expect(warnings).toHaveLength(1);
55
+ expect(warnings[0]).toContain(name);
56
+ expect(warnings[0]).toContain("adapter()");
57
+ });
58
+
59
+ test("says nothing when everything it reads is set", async () => {
60
+ const name = `SIGIL_CFG_SET_${Date.now()}`;
61
+ process.env[name] = "yes";
62
+ try {
63
+ const dir = project(`export default { x: Bun.env.${name}! };`);
64
+ const warnings: string[] = [];
65
+ await importConfig(dir, { warn: (m) => warnings.push(m) });
66
+ expect(warnings).toEqual([]);
67
+ } finally {
68
+ delete process.env[name];
69
+ }
70
+ });
71
+
72
+ test("a config that throws is rethrown with the unset variables named", async () => {
73
+ const name = `SIGIL_CFG_THROW_${Date.now()}`;
74
+ const dir = project(
75
+ `const url = Bun.env.${name}; if (!url) throw new Error("no url"); export default {};`,
76
+ );
77
+ await expect(importConfig(dir, { warn: () => {} })).rejects.toThrow(name);
78
+ });
79
+
80
+ test("no config is an empty one", async () => {
81
+ const dir = mkdtempSync(join(tmpdir(), "sigil-config-"));
82
+ expect(await importConfig(dir)).toEqual({});
83
+ });
84
+ });
@@ -42,7 +42,12 @@ describe("coordinator", () => {
42
42
  process.chdir(cwd);
43
43
  // a worker that has just been stopped can still hold the directory as its
44
44
  // cwd for a moment, which Windows reports as EBUSY
45
- await rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
45
+ await rm(root, {
46
+ recursive: true,
47
+ force: true,
48
+ maxRetries: 10,
49
+ retryDelay: 100,
50
+ });
46
51
  });
47
52
 
48
53
  test("forwards requests with their cookies", async () => {
@@ -92,5 +97,7 @@ describe("coordinator", () => {
92
97
  }
93
98
  expect(status).toBe(200);
94
99
  expect(worker.pid).not.toBe(before);
95
- });
100
+ // the loop above waits up to 6s, past bun's 5s default, so a slow
101
+ // machine failed the test instead of the restart
102
+ }, 15_000);
96
103
  });