@sigil-dev/grimoire 0.9.2 → 0.9.3

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 (88) hide show
  1. package/index.ts +35 -35
  2. package/package.json +8 -8
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +13 -7
  6. package/src/client/enhance.ts +98 -98
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +2 -2
  9. package/src/client/router.ts +445 -371
  10. package/src/client/scope.ts +22 -22
  11. package/src/dev/compile-module.ts +163 -163
  12. package/src/dev/effect-registry.ts +23 -23
  13. package/src/dev/graph.ts +114 -114
  14. package/src/dev/hmr-client.ts +185 -185
  15. package/src/dev/loader.ts +47 -47
  16. package/src/dev/paths.ts +14 -14
  17. package/src/dev/runtime-bundle.ts +52 -52
  18. package/src/dev/watcher.ts +47 -46
  19. package/src/env/dynamic-private.ts +19 -19
  20. package/src/env/dynamic-public.ts +22 -22
  21. package/src/env/index.ts +25 -25
  22. package/src/env/plugin.ts +19 -19
  23. package/src/env/private.ts +5 -5
  24. package/src/env/public.ts +7 -7
  25. package/src/env/typegen.ts +61 -61
  26. package/src/headers.ts +114 -114
  27. package/src/integrations/vite.ts +73 -73
  28. package/src/logger/index.ts +4 -4
  29. package/src/logger/instance.ts +6 -6
  30. package/src/logger/logger.ts +113 -113
  31. package/src/logger/request-middleware.ts +24 -24
  32. package/src/logger/transports.ts +117 -117
  33. package/src/logger/types.ts +39 -39
  34. package/src/rendering/head.ts +47 -47
  35. package/src/rendering/hydrate.ts +98 -79
  36. package/src/rendering/index.ts +455 -372
  37. package/src/rendering/ssrPlugin.ts +69 -69
  38. package/src/routing/manifest-gen.ts +12 -11
  39. package/src/routing/router.ts +109 -109
  40. package/src/routing/scanner.ts +141 -141
  41. package/src/routing/transform-routes.ts +102 -102
  42. package/src/sentinels/error.ts +52 -52
  43. package/src/sentinels/fail.ts +41 -41
  44. package/src/sentinels/redirect.ts +35 -35
  45. package/src/server/build.ts +243 -219
  46. package/src/server/context.ts +10 -10
  47. package/src/server/cookie-utils.ts +66 -66
  48. package/src/server/coordinator.ts +364 -356
  49. package/src/server/hooks.ts +114 -114
  50. package/src/server/index.ts +655 -658
  51. package/src/server/interactive.ts +56 -56
  52. package/src/server/plugins.ts +121 -120
  53. package/src/server/worker.ts +58 -58
  54. package/src/typegen/index.ts +356 -356
  55. package/src/types.ts +4 -4
  56. package/test/client-router.test.ts +37 -31
  57. package/test/context.test.ts +52 -52
  58. package/test/env.test.ts +70 -70
  59. package/test/exports.test.ts +37 -37
  60. package/test/fail.test.ts +46 -46
  61. package/test/headers.test.ts +100 -100
  62. package/test/hydration.test.ts +119 -119
  63. package/test/middleware.test.ts +223 -223
  64. package/test/preload.ts +5 -5
  65. package/test/redirect-error.test.ts +112 -112
  66. package/test/rendering.test.ts +615 -579
  67. package/test/routing.test.ts +81 -81
  68. package/test/scanning.test.ts +200 -200
  69. package/test/scope.test.ts +195 -195
  70. package/test/server.test.ts +249 -249
  71. package/test/streaming.test.ts +125 -125
  72. package/test/transform-routes.test.ts +84 -84
  73. package/test/typegen.test.ts +682 -682
  74. package/tsconfig.json +10 -10
  75. package/.grimoire/_routes.dom.js +0 -8
  76. package/.grimoire/_routes.hydrate.js +0 -8
  77. package/.grimoire/tsconfig.generated.json +0 -11
  78. package/.grimoire/types/ambient.d.ts +0 -59
  79. package/.grimoire/types/api/hello/$types.d.ts +0 -50
  80. package/.grimoire/types/api/items/$types.d.ts +0 -50
  81. package/.grimoire/types/echo/$types.d.ts +0 -50
  82. package/.grimoire/types/env-private.d.ts +0 -5
  83. package/.grimoire/types/env-public.d.ts +0 -5
  84. package/.grimoire/types/mixed/$types.d.ts +0 -50
  85. package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
  86. package/.grimoire/types/reject/$types.d.ts +0 -50
  87. package/public/__grimoire__/hydrate.js +0 -585
  88. package/public/__grimoire__/index.js +0 -490
@@ -1,195 +1,195 @@
1
- /**
2
- * Tests for the effect-scope mechanism used by the router to prevent effect
3
- * accumulation across client-side navigations.
4
- *
5
- * Two invariants must hold forever:
6
- * 1. Effects created during a page mount stop running when the page is disposed.
7
- * 2. __nodes is reset to [] before each SPA navigation so the new page gets
8
- * a fresh empty pool instead of recycling stale DOM nodes.
9
- *
10
- * await tick() must be called after all signal edits, given Bun's runner is
11
- * synchronous and doesnt wait for our microtasks. In the browser the microtasks
12
- * flush between event loop turns
13
- */
14
- import { describe, expect, test } from "bun:test";
15
- import {
16
- createEffect,
17
- createSignal,
18
- withEffectScope as runtimeScope,
19
- tick,
20
- } from "@sigil-dev/runtime";
21
- import { withEffectScope } from "../src/client/scope.ts";
22
-
23
- describe("grimoire withEffectScope", () => {
24
- test("effects inside scope stop after dispose", async () => {
25
- const count = createSignal(0);
26
- let runs = 0;
27
- const dispose = withEffectScope(() => {
28
- createEffect(() => {
29
- count();
30
- runs++;
31
- });
32
- });
33
-
34
- count.set(1);
35
- await tick(); // let microtask queue flush
36
- expect(runs).toBe(2);
37
-
38
- dispose();
39
- count.set(2);
40
- await tick();
41
- expect(runs).toBe(2);
42
- });
43
-
44
- test("simulates two page navigations — only latest page's effects run", async () => {
45
- const count = createSignal(0);
46
- let pageARuns = 0;
47
- let pageBRuns = 0;
48
-
49
- // Mount page A
50
- let disposeCurrentPage = withEffectScope(() => {
51
- createEffect(() => {
52
- count();
53
- pageARuns++;
54
- });
55
- });
56
- count.set(1);
57
- await tick();
58
- expect(pageARuns).toBe(2);
59
- expect(pageBRuns).toBe(0);
60
-
61
- // Navigate to page B — dispose A first
62
- disposeCurrentPage();
63
- disposeCurrentPage = withEffectScope(() => {
64
- createEffect(() => {
65
- count();
66
- pageBRuns++;
67
- });
68
- });
69
-
70
- count.set(2);
71
- await tick();
72
- expect(pageARuns).toBe(2); // A's effect is gone
73
- expect(pageBRuns).toBe(2); // B initial + update
74
-
75
- // Navigate away from B — dispose B
76
- disposeCurrentPage();
77
- count.set(3);
78
- await tick();
79
- expect(pageARuns).toBe(2);
80
- expect(pageBRuns).toBe(2); // B's effect is gone too
81
- });
82
-
83
- test("three navigations: each page's effects are independent", async () => {
84
- const signal = createSignal("a");
85
- const log: string[] = [];
86
-
87
- const mountPage = (label: string) =>
88
- withEffectScope(() => {
89
- createEffect(() => {
90
- log.push(`${label}:${signal()}`);
91
- });
92
- });
93
-
94
- const disposeA = mountPage("A");
95
- const disposeB = mountPage("B");
96
-
97
- signal.set("b");
98
- await tick();
99
- expect(log).toEqual(["A:a", "B:a", "A:b", "B:b"]);
100
-
101
- disposeA();
102
- signal.set("c");
103
- await tick();
104
- expect(log).toEqual(["A:a", "B:a", "A:b", "B:b", "B:c"]); // only B ran
105
-
106
- disposeB();
107
- signal.set("d");
108
- await tick();
109
- expect(log).toEqual(["A:a", "B:a", "A:b", "B:b", "B:c"]); // neither ran
110
- });
111
-
112
- test("grimoire scope uses same globalThis channel as runtime scope", async () => {
113
- // Both must route through the same global so that createEffect
114
- // in page components (compiled by sigil, imports @sigil-dev/runtime)
115
- // is captured by grimoire's router scope.
116
- const count = createSignal(0);
117
- let runs = 0;
118
-
119
- // grimoire's local withEffectScope wraps a createEffect from @sigil-dev/runtime
120
- const dispose = withEffectScope(() => {
121
- createEffect(() => {
122
- count();
123
- runs++;
124
- });
125
- });
126
-
127
- count.set(1);
128
- await tick();
129
- expect(runs).toBe(2);
130
-
131
- dispose();
132
- count.set(2);
133
- await tick();
134
- expect(runs).toBe(2); // runtime's createEffect was captured by grimoire's scope ✓
135
- });
136
-
137
- test("runtime's withEffectScope and grimoire's are interchangeable", async () => {
138
- // The runtime exports its own withEffectScope for user code.
139
- // Both implementations must work the same way via the globalThis channel.
140
- const count = createSignal(0);
141
- let runs = 0;
142
-
143
- const dispose = runtimeScope(() => {
144
- createEffect(() => {
145
- count();
146
- runs++;
147
- });
148
- });
149
-
150
- count.set(1);
151
- await tick();
152
- expect(runs).toBe(2);
153
- dispose();
154
- count.set(2);
155
- await tick();
156
- expect(runs).toBe(2);
157
- });
158
- });
159
-
160
- describe("__nodes pool reset on SPA navigation", () => {
161
- test("navigate sets __nodes to [] before calling Page()", () => {
162
- // Simulate exactly what router.ts's navigate() does:
163
- // dispose old effects → set __nodes = [] → call Page() → replaceChildren(node)
164
- // We only care that Page() sees an empty pool.
165
- let nodesAtCallTime: unknown = "not set";
166
-
167
- const mockPage = () => {
168
- nodesAtCallTime = (globalThis as any).__nodes;
169
- return document.createElement("div"); // satisfy replaceChildren
170
- };
171
- // Mimic the three lines in navigate() that matter
172
- (globalThis as any).__nodes = ["stale", "dom", "nodes"]; // leftover from previous page
173
- (globalThis as any).__nodes = []; // the reset
174
- mockPage();
175
-
176
- expect(nodesAtCallTime).toEqual([]);
177
- });
178
-
179
- test("Page() called during SSR hydration sees real DOM nodes, not empty pool", () => {
180
- // Contrast: hydrate.ts sets __nodes to Array.from(slot.childNodes) before calling Page()
181
- const fakeSlotChild = document.createElement("div");
182
- let nodesAtCallTime: unknown = "not set";
183
-
184
- const mockPage = () => {
185
- nodesAtCallTime = (globalThis as any).__nodes;
186
- };
187
-
188
- (globalThis as any).__nodes = [fakeSlotChild]; // hydrate.ts sets real nodes
189
- mockPage();
190
-
191
- expect(Array.isArray(nodesAtCallTime)).toBe(true);
192
- expect((nodesAtCallTime as unknown[]).length).toBe(1);
193
- expect((nodesAtCallTime as unknown[])[0]).toBe(fakeSlotChild);
194
- });
195
- });
1
+ /**
2
+ * Tests for the effect-scope mechanism used by the router to prevent effect
3
+ * accumulation across client-side navigations.
4
+ *
5
+ * Two invariants must hold forever:
6
+ * 1. Effects created during a page mount stop running when the page is disposed.
7
+ * 2. __nodes is reset to [] before each SPA navigation so the new page gets
8
+ * a fresh empty pool instead of recycling stale DOM nodes.
9
+ *
10
+ * await tick() must be called after all signal edits, given Bun's runner is
11
+ * synchronous and doesnt wait for our microtasks. In the browser the microtasks
12
+ * flush between event loop turns
13
+ */
14
+ import { describe, expect, test } from "bun:test";
15
+ import {
16
+ createEffect,
17
+ createSignal,
18
+ withEffectScope as runtimeScope,
19
+ tick,
20
+ } from "@sigil-dev/runtime";
21
+ import { withEffectScope } from "../src/client/scope.ts";
22
+
23
+ describe("grimoire withEffectScope", () => {
24
+ test("effects inside scope stop after dispose", async () => {
25
+ const count = createSignal(0);
26
+ let runs = 0;
27
+ const dispose = withEffectScope(() => {
28
+ createEffect(() => {
29
+ count();
30
+ runs++;
31
+ });
32
+ });
33
+
34
+ count.set(1);
35
+ await tick(); // let microtask queue flush
36
+ expect(runs).toBe(2);
37
+
38
+ dispose();
39
+ count.set(2);
40
+ await tick();
41
+ expect(runs).toBe(2);
42
+ });
43
+
44
+ test("simulates two page navigations — only latest page's effects run", async () => {
45
+ const count = createSignal(0);
46
+ let pageARuns = 0;
47
+ let pageBRuns = 0;
48
+
49
+ // Mount page A
50
+ let disposeCurrentPage = withEffectScope(() => {
51
+ createEffect(() => {
52
+ count();
53
+ pageARuns++;
54
+ });
55
+ });
56
+ count.set(1);
57
+ await tick();
58
+ expect(pageARuns).toBe(2);
59
+ expect(pageBRuns).toBe(0);
60
+
61
+ // Navigate to page B — dispose A first
62
+ disposeCurrentPage();
63
+ disposeCurrentPage = withEffectScope(() => {
64
+ createEffect(() => {
65
+ count();
66
+ pageBRuns++;
67
+ });
68
+ });
69
+
70
+ count.set(2);
71
+ await tick();
72
+ expect(pageARuns).toBe(2); // A's effect is gone
73
+ expect(pageBRuns).toBe(2); // B initial + update
74
+
75
+ // Navigate away from B — dispose B
76
+ disposeCurrentPage();
77
+ count.set(3);
78
+ await tick();
79
+ expect(pageARuns).toBe(2);
80
+ expect(pageBRuns).toBe(2); // B's effect is gone too
81
+ });
82
+
83
+ test("three navigations: each page's effects are independent", async () => {
84
+ const signal = createSignal("a");
85
+ const log: string[] = [];
86
+
87
+ const mountPage = (label: string) =>
88
+ withEffectScope(() => {
89
+ createEffect(() => {
90
+ log.push(`${label}:${signal()}`);
91
+ });
92
+ });
93
+
94
+ const disposeA = mountPage("A");
95
+ const disposeB = mountPage("B");
96
+
97
+ signal.set("b");
98
+ await tick();
99
+ expect(log).toEqual(["A:a", "B:a", "A:b", "B:b"]);
100
+
101
+ disposeA();
102
+ signal.set("c");
103
+ await tick();
104
+ expect(log).toEqual(["A:a", "B:a", "A:b", "B:b", "B:c"]); // only B ran
105
+
106
+ disposeB();
107
+ signal.set("d");
108
+ await tick();
109
+ expect(log).toEqual(["A:a", "B:a", "A:b", "B:b", "B:c"]); // neither ran
110
+ });
111
+
112
+ test("grimoire scope uses same globalThis channel as runtime scope", async () => {
113
+ // Both must route through the same global so that createEffect
114
+ // in page components (compiled by sigil, imports @sigil-dev/runtime)
115
+ // is captured by grimoire's router scope.
116
+ const count = createSignal(0);
117
+ let runs = 0;
118
+
119
+ // grimoire's local withEffectScope wraps a createEffect from @sigil-dev/runtime
120
+ const dispose = withEffectScope(() => {
121
+ createEffect(() => {
122
+ count();
123
+ runs++;
124
+ });
125
+ });
126
+
127
+ count.set(1);
128
+ await tick();
129
+ expect(runs).toBe(2);
130
+
131
+ dispose();
132
+ count.set(2);
133
+ await tick();
134
+ expect(runs).toBe(2); // runtime's createEffect was captured by grimoire's scope ✓
135
+ });
136
+
137
+ test("runtime's withEffectScope and grimoire's are interchangeable", async () => {
138
+ // The runtime exports its own withEffectScope for user code.
139
+ // Both implementations must work the same way via the globalThis channel.
140
+ const count = createSignal(0);
141
+ let runs = 0;
142
+
143
+ const dispose = runtimeScope(() => {
144
+ createEffect(() => {
145
+ count();
146
+ runs++;
147
+ });
148
+ });
149
+
150
+ count.set(1);
151
+ await tick();
152
+ expect(runs).toBe(2);
153
+ dispose();
154
+ count.set(2);
155
+ await tick();
156
+ expect(runs).toBe(2);
157
+ });
158
+ });
159
+
160
+ describe("__nodes pool reset on SPA navigation", () => {
161
+ test("navigate sets __nodes to [] before calling Page()", () => {
162
+ // Simulate exactly what router.ts's navigate() does:
163
+ // dispose old effects → set __nodes = [] → call Page() → replaceChildren(node)
164
+ // We only care that Page() sees an empty pool.
165
+ let nodesAtCallTime: unknown = "not set";
166
+
167
+ const mockPage = () => {
168
+ nodesAtCallTime = (globalThis as any).__nodes;
169
+ return document.createElement("div"); // satisfy replaceChildren
170
+ };
171
+ // Mimic the three lines in navigate() that matter
172
+ (globalThis as any).__nodes = ["stale", "dom", "nodes"]; // leftover from previous page
173
+ (globalThis as any).__nodes = []; // the reset
174
+ mockPage();
175
+
176
+ expect(nodesAtCallTime).toEqual([]);
177
+ });
178
+
179
+ test("Page() called during SSR hydration sees real DOM nodes, not empty pool", () => {
180
+ // Contrast: hydrate.ts sets __nodes to Array.from(slot.childNodes) before calling Page()
181
+ const fakeSlotChild = document.createElement("div");
182
+ let nodesAtCallTime: unknown = "not set";
183
+
184
+ const mockPage = () => {
185
+ nodesAtCallTime = (globalThis as any).__nodes;
186
+ };
187
+
188
+ (globalThis as any).__nodes = [fakeSlotChild]; // hydrate.ts sets real nodes
189
+ mockPage();
190
+
191
+ expect(Array.isArray(nodesAtCallTime)).toBe(true);
192
+ expect((nodesAtCallTime as unknown[]).length).toBe(1);
193
+ expect((nodesAtCallTime as unknown[])[0]).toBe(fakeSlotChild);
194
+ });
195
+ });