@sigil-dev/grimoire 0.9.4 → 0.9.6

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 (78) hide show
  1. package/index.ts +37 -37
  2. package/package.json +3 -3
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +36 -36
  6. package/src/client/enhance.ts +102 -102
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +5 -5
  9. package/src/client/router.ts +128 -38
  10. package/src/client/scope.ts +22 -22
  11. package/src/dev/compile-module.ts +215 -163
  12. package/src/dev/effect-registry.ts +23 -23
  13. package/src/dev/graph.ts +109 -114
  14. package/src/dev/hmr-client.ts +202 -185
  15. package/src/dev/hmr-server.ts +261 -187
  16. package/src/dev/loader.ts +120 -47
  17. package/src/dev/paths.ts +14 -14
  18. package/src/dev/resolve.ts +70 -0
  19. package/src/dev/runtime-bundle.ts +52 -52
  20. package/src/dev/scan.ts +78 -0
  21. package/src/dev/watcher.ts +69 -47
  22. package/src/env/dynamic-private.ts +19 -19
  23. package/src/env/dynamic-public.ts +22 -22
  24. package/src/env/index.ts +25 -25
  25. package/src/env/plugin.ts +19 -19
  26. package/src/env/private.ts +5 -5
  27. package/src/env/public.ts +7 -7
  28. package/src/env/typegen.ts +61 -61
  29. package/src/headers.ts +114 -114
  30. package/src/integrations/vite.ts +73 -73
  31. package/src/logger/index.ts +4 -4
  32. package/src/logger/instance.ts +6 -6
  33. package/src/logger/logger.ts +113 -113
  34. package/src/logger/request-middleware.ts +24 -24
  35. package/src/logger/transports.ts +117 -117
  36. package/src/logger/types.ts +39 -39
  37. package/src/rendering/head.ts +52 -47
  38. package/src/rendering/hydrate.ts +16 -3
  39. package/src/rendering/index.ts +43 -17
  40. package/src/rendering/ssrPlugin.ts +78 -69
  41. package/src/routing/manifest-gen.ts +55 -55
  42. package/src/routing/router.ts +135 -135
  43. package/src/routing/scanner.ts +196 -187
  44. package/src/routing/transform-routes.ts +102 -102
  45. package/src/sentinels/error.ts +52 -52
  46. package/src/sentinels/fail.ts +41 -41
  47. package/src/sentinels/redirect.ts +35 -35
  48. package/src/server/build.ts +250 -255
  49. package/src/server/context.ts +10 -10
  50. package/src/server/cookie-utils.ts +66 -66
  51. package/src/server/coordinator.ts +1 -1
  52. package/src/server/hooks.ts +114 -114
  53. package/src/server/index.ts +110 -69
  54. package/src/server/interactive.ts +56 -56
  55. package/src/server/plugins.ts +121 -121
  56. package/src/server/worker.ts +58 -58
  57. package/src/typegen/index.ts +389 -389
  58. package/src/types.ts +286 -286
  59. package/test/client-router.test.ts +76 -8
  60. package/test/context.test.ts +52 -52
  61. package/test/env.test.ts +90 -90
  62. package/test/exports.test.ts +37 -37
  63. package/test/fail.test.ts +46 -46
  64. package/test/headers.test.ts +100 -100
  65. package/test/hmr.test.ts +426 -0
  66. package/test/hydration.test.ts +119 -119
  67. package/test/middleware.test.ts +223 -223
  68. package/test/preload.ts +5 -5
  69. package/test/redirect-error.test.ts +112 -112
  70. package/test/rendering.test.ts +719 -728
  71. package/test/routing.test.ts +197 -194
  72. package/test/scanning.test.ts +314 -254
  73. package/test/scope.test.ts +195 -195
  74. package/test/server.test.ts +360 -354
  75. package/test/streaming.test.ts +40 -1
  76. package/test/transform-routes.test.ts +87 -87
  77. package/test/typegen.test.ts +697 -697
  78. package/tsconfig.json +10 -10
@@ -128,6 +128,45 @@ describe("Streaming SSR", () => {
128
128
  expect(json.data).toBeDefined();
129
129
  });
130
130
 
131
+ // The JSON payload and the HTML document share one URL and are picked by a
132
+ // request header. Without Vary any cache may serve the JSON to a plain
133
+ // document request, which rendered the raw state object as the page.
134
+ test("navigation JSON declares Vary on the navigate header", async () => {
135
+ const matched = makeMatched(
136
+ "/nav-vary",
137
+ "data:text/javascript,export default (p) => '<div>nav</div>'",
138
+ );
139
+
140
+ const res = await renderRoute(
141
+ matched,
142
+ new Request("http://localhost/nav-vary", {
143
+ headers: { "x-grimoire-navigate": "1" },
144
+ }),
145
+ );
146
+
147
+ expect(res.headers.get("content-type")).toContain("application/json");
148
+ expect(res.headers.get("vary")?.toLowerCase()).toContain(
149
+ "x-grimoire-navigate",
150
+ );
151
+ });
152
+
153
+ test("HTML document declares Vary on the navigate header", async () => {
154
+ const matched = makeMatched(
155
+ "/doc-vary",
156
+ "data:text/javascript,export default (p) => '<div>doc</div>'",
157
+ );
158
+
159
+ const res = await renderRoute(
160
+ matched,
161
+ new Request("http://localhost/doc-vary"),
162
+ );
163
+
164
+ expect(res.headers.get("content-type")).toContain("text/html");
165
+ expect(res.headers.get("vary")?.toLowerCase()).toContain(
166
+ "x-grimoire-navigate",
167
+ );
168
+ });
169
+
131
170
  test("state JSON escapes </script> in data (XSS)", async () => {
132
171
  const evil = "</script><script>alert(1)</script>";
133
172
  const src = [
@@ -188,7 +227,7 @@ describe("streamed promises (deferred data)", () => {
188
227
  await writeFile(join(dir, "+page.server.ts"), serverSrc);
189
228
  await writeFile(
190
229
  join(dir, "+page.tsx"),
191
- 'export default function P({ data }) { return <div>{data.fast}{String(data.slow)}{String(data.boom)}</div>; }',
230
+ "export default function P({ data }) { return <div>{data.fast}{String(data.slow)}{String(data.boom)}</div>; }",
192
231
  );
193
232
  const tree = await scanRoutes(dir);
194
233
  const matched = matchRoute(tree, new URL("http://localhost/"));
@@ -1,87 +1,87 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
- import type { RouteFile } from "../src/routing/scanner";
6
- import { transformRoutes } from "../src/routing/transform-routes";
7
-
8
- describe("transformRoutes", () => {
9
- test("pre-transforms TypeScript TSX routes to unique JavaScript files", async () => {
10
- const root = await mkdtemp(join(tmpdir(), "grimoire-transform-"));
11
- try {
12
- const routesDir = join(root, "src", "routes");
13
- const spellDir = join(routesDir, "spells", "[id]");
14
- const otherDir = join(routesDir, "other");
15
- const outDir = join(root, ".grimoire", "compiled");
16
- await mkdir(spellDir, { recursive: true });
17
- await mkdir(otherDir, { recursive: true });
18
- await mkdir(outDir, { recursive: true });
19
-
20
- const spellRoute = join(spellDir, "+page.tsx");
21
- const otherRoute = join(otherDir, "+page.tsx");
22
- const spellData = join(spellDir, "data.ts");
23
- await writeFile(
24
- spellData,
25
- `
26
- export const SPELLS = { fire: "Fire" } as const;
27
- `,
28
- );
29
- await writeFile(
30
- spellRoute,
31
- `
32
- import { SPELLS } from "./data";
33
- type PageProps = { params: { id: keyof typeof SPELLS } };
34
-
35
- export default function Page({ params }: PageProps) {
36
- const n: number = 1;
37
- return <h1>{SPELLS[params.id] ?? n}</h1>;
38
- }
39
- `,
40
- );
41
- await writeFile(
42
- otherRoute,
43
- `
44
- export default function Other({ count }: { count: number }) {
45
- return <p>{count}</p>;
46
- }
47
- `,
48
- );
49
-
50
- const routes: RouteFile[] = [
51
- route(spellRoute, "/spells/:id"),
52
- route(otherRoute, "/other"),
53
- ];
54
- const files = await transformRoutes(routes, outDir, "dom");
55
-
56
- const spellOut = files.get(spellRoute);
57
- const otherOut = files.get(otherRoute);
58
- expect(spellOut).not.toEqual(otherOut);
59
- expect(spellOut).toContain(".dom.js");
60
- expect(otherOut).toContain(".dom.js");
61
-
62
- const code = await readFile(spellOut!, "utf8");
63
- expect(code).toContain("export default function Page");
64
- expect(code).toContain(
65
- spellData.replace(/\.ts$/, "").replace(/\\/g, "\\\\"),
66
- );
67
- expect(code).not.toContain(": number");
68
- expect(code).not.toContain("keyof typeof");
69
- expect(code).not.toContain("as const");
70
- } finally {
71
- await rm(root, { recursive: true, force: true });
72
- }
73
- });
74
- });
75
-
76
- function route(filePath: string, path: string): RouteFile {
77
- return {
78
- path,
79
- filePath,
80
- clientPath: filePath,
81
- type: "page",
82
- paramNames: [],
83
- optionalParamNames: [],
84
- restParamNames: [],
85
- paramMatchers: {},
86
- };
87
- }
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import type { RouteFile } from "../src/routing/scanner";
6
+ import { transformRoutes } from "../src/routing/transform-routes";
7
+
8
+ describe("transformRoutes", () => {
9
+ test("pre-transforms TypeScript TSX routes to unique JavaScript files", async () => {
10
+ const root = await mkdtemp(join(tmpdir(), "grimoire-transform-"));
11
+ try {
12
+ const routesDir = join(root, "src", "routes");
13
+ const spellDir = join(routesDir, "spells", "[id]");
14
+ const otherDir = join(routesDir, "other");
15
+ const outDir = join(root, ".grimoire", "compiled");
16
+ await mkdir(spellDir, { recursive: true });
17
+ await mkdir(otherDir, { recursive: true });
18
+ await mkdir(outDir, { recursive: true });
19
+
20
+ const spellRoute = join(spellDir, "+page.tsx");
21
+ const otherRoute = join(otherDir, "+page.tsx");
22
+ const spellData = join(spellDir, "data.ts");
23
+ await writeFile(
24
+ spellData,
25
+ `
26
+ export const SPELLS = { fire: "Fire" } as const;
27
+ `,
28
+ );
29
+ await writeFile(
30
+ spellRoute,
31
+ `
32
+ import { SPELLS } from "./data";
33
+ type PageProps = { params: { id: keyof typeof SPELLS } };
34
+
35
+ export default function Page({ params }: PageProps) {
36
+ const n: number = 1;
37
+ return <h1>{SPELLS[params.id] ?? n}</h1>;
38
+ }
39
+ `,
40
+ );
41
+ await writeFile(
42
+ otherRoute,
43
+ `
44
+ export default function Other({ count }: { count: number }) {
45
+ return <p>{count}</p>;
46
+ }
47
+ `,
48
+ );
49
+
50
+ const routes: RouteFile[] = [
51
+ route(spellRoute, "/spells/:id"),
52
+ route(otherRoute, "/other"),
53
+ ];
54
+ const files = await transformRoutes(routes, outDir, "dom");
55
+
56
+ const spellOut = files.get(spellRoute);
57
+ const otherOut = files.get(otherRoute);
58
+ expect(spellOut).not.toEqual(otherOut);
59
+ expect(spellOut).toContain(".dom.js");
60
+ expect(otherOut).toContain(".dom.js");
61
+
62
+ const code = await readFile(spellOut!, "utf8");
63
+ expect(code).toContain("export default function Page");
64
+ expect(code).toContain(
65
+ spellData.replace(/\.ts$/, "").replace(/\\/g, "\\\\"),
66
+ );
67
+ expect(code).not.toContain(": number");
68
+ expect(code).not.toContain("keyof typeof");
69
+ expect(code).not.toContain("as const");
70
+ } finally {
71
+ await rm(root, { recursive: true, force: true });
72
+ }
73
+ });
74
+ });
75
+
76
+ function route(filePath: string, path: string): RouteFile {
77
+ return {
78
+ path,
79
+ filePath,
80
+ clientPath: filePath,
81
+ type: "page",
82
+ paramNames: [],
83
+ optionalParamNames: [],
84
+ restParamNames: [],
85
+ paramMatchers: {},
86
+ };
87
+ }