@sigil-dev/grimoire 0.7.5 → 0.7.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 (50) hide show
  1. package/.grimoire/_routes.dom.js +8 -0
  2. package/.grimoire/_routes.hydrate.js +8 -0
  3. package/.grimoire/tsconfig.generated.json +11 -0
  4. package/.grimoire/types/ambient.d.ts +59 -0
  5. package/.grimoire/types/api/hello/$types.d.ts +50 -0
  6. package/.grimoire/types/api/items/$types.d.ts +50 -0
  7. package/.grimoire/types/echo/$types.d.ts +50 -0
  8. package/.grimoire/types/env-private.d.ts +5 -0
  9. package/.grimoire/types/env-public.d.ts +5 -0
  10. package/.grimoire/types/mixed/$types.d.ts +50 -0
  11. package/.grimoire/types/params/[docId]/$types.d.ts +52 -0
  12. package/.grimoire/types/reject/$types.d.ts +50 -0
  13. package/index.ts +34 -34
  14. package/package.json +8 -4
  15. package/preload.js +2 -0
  16. package/public/__grimoire__/hydrate.js +585 -0
  17. package/public/__grimoire__/index.js +490 -0
  18. package/src/client/head.ts +29 -0
  19. package/src/client/router.ts +224 -76
  20. package/src/env/index.ts +25 -0
  21. package/src/env/plugin.ts +13 -0
  22. package/src/env/private.ts +5 -0
  23. package/src/env/public.ts +7 -0
  24. package/src/env/typegen.ts +51 -0
  25. package/src/integrations/vite.ts +72 -72
  26. package/src/rendering/head.ts +22 -2
  27. package/src/rendering/hydrate.ts +81 -27
  28. package/src/rendering/index.ts +199 -186
  29. package/src/rendering/ssrPlugin.ts +53 -47
  30. package/src/routing/manifest-gen.ts +39 -26
  31. package/src/routing/router.ts +106 -98
  32. package/src/routing/scanner.ts +135 -129
  33. package/src/routing/transform-routes.ts +101 -101
  34. package/src/server/build.ts +147 -90
  35. package/src/server/coordinator.ts +306 -297
  36. package/src/server/hooks.ts +24 -3
  37. package/src/server/index.ts +144 -70
  38. package/src/server/worker.ts +59 -59
  39. package/src/typegen/index.ts +353 -340
  40. package/src/types.ts +269 -260
  41. package/test/context.test.ts +52 -52
  42. package/test/hydration.test.ts +119 -119
  43. package/test/middleware.test.ts +223 -221
  44. package/test/rendering.test.ts +425 -425
  45. package/test/routing.test.ts +83 -45
  46. package/test/scanning.test.ts +181 -169
  47. package/test/server.test.ts +229 -229
  48. package/test/streaming.test.ts +106 -106
  49. package/test/transform-routes.test.ts +84 -84
  50. package/test/typegen.test.ts +19 -1
@@ -1,106 +1,106 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { renderRoute } from "../src/rendering";
3
-
4
- function makeMatched(path: string, filePath: string, params = {}) {
5
- return {
6
- route: { path, params: {}, filePath },
7
- params,
8
- layouts: [],
9
- layoutServers: [],
10
- } as any;
11
- }
12
-
13
- describe("Streaming SSR", () => {
14
- test("returns a ReadableStream response", async () => {
15
- const matched = makeMatched("/", "data:text/javascript,export default (p) => '<div>hello</div>'");
16
- const res = await renderRoute(matched, new Request("http://localhost/"));
17
- expect(res.body).toBeInstanceOf(ReadableStream);
18
- });
19
-
20
- test("stream contains full HTML document", async () => {
21
- const matched = makeMatched("/test", "data:text/javascript,export default (p) => '<p>streaming works</p>'");
22
-
23
- const res = await renderRoute(
24
- matched,
25
- new Request("http://localhost/test"),
26
- );
27
- const html = await res.text();
28
-
29
- expect(html).toContain("<!DOCTYPE html>");
30
- expect(html).toContain('<meta charset="UTF-8" />');
31
- expect(html).toContain("streaming works");
32
- expect(html).toContain("</html>");
33
- expect(html).toContain("__grimoire_state__");
34
- });
35
-
36
- test("stream includes state JSON with route pattern", async () => {
37
- const matched = makeMatched("/spells/:id", "data:text/javascript,export default (p) => '<div>spell</div>'", { id: "fireball" });
38
-
39
- const res = await renderRoute(
40
- matched,
41
- new Request("http://localhost/spells/fireball"),
42
- );
43
- const html = await res.text();
44
-
45
- expect(html).toContain('"pattern":"/spells/:id"');
46
- expect(html).toContain('"id":"fireball"');
47
- });
48
-
49
- test("head script tag comes before head content", async () => {
50
- const matched = makeMatched("/head-test", "data:text/javascript,export default (p) => '<div>head</div>'");
51
-
52
- const res = await renderRoute(
53
- matched,
54
- new Request("http://localhost/head-test"),
55
- );
56
- const html = await res.text();
57
-
58
- const hydrateScript = html.indexOf("hydrate.js");
59
- const headClosing = html.indexOf("</head>");
60
- // hydrate.js should be in <head>
61
- expect(hydrateScript).toBeGreaterThan(-1);
62
- expect(hydrateScript).toBeLessThan(headClosing);
63
- });
64
-
65
- test("stream chunks arrive incrementally", async () => {
66
- const matched = makeMatched("/chunked", "data:text/javascript,export default (p) => '<div>chunked</div>'");
67
-
68
- const res = await renderRoute(
69
- matched,
70
- new Request("http://localhost/chunked"),
71
- );
72
- const reader = res.body!.getReader();
73
- const chunks: string[] = [];
74
-
75
- while (true) {
76
- const { done, value } = await reader.read();
77
- if (done) break;
78
- // Bun ReadableStream returns Uint8Array
79
- chunks.push(
80
- typeof value === "string"
81
- ? value
82
- : new TextDecoder().decode(value.buffer),
83
- );
84
- }
85
-
86
- // Should have multiple chunks (at least DOCTYPE + body)
87
- expect(chunks.length).toBeGreaterThan(1);
88
- // First chunk should start with DOCTYPE
89
- expect(chunks[0]).toContain("<!DOCTYPE html>");
90
- // Last chunk should close the document
91
- expect(chunks[chunks.length - 1]).toContain("</html>");
92
- });
93
-
94
- test("navigation request returns JSON not stream", async () => {
95
- const matched = makeMatched("/nav", "data:text/javascript,export default (p) => '<div>nav</div>'");
96
-
97
- const req = new Request("http://localhost/nav", {
98
- headers: { "x-grimoire-navigate": "1" },
99
- });
100
- const res = await renderRoute(matched, req);
101
- const json = await res.json();
102
-
103
- expect(json.pattern).toBe("/nav");
104
- expect(json.data).toBeDefined();
105
- });
106
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { renderRoute } from "../src/rendering";
3
+
4
+ function makeMatched(path: string, filePath: string, params = {}) {
5
+ return {
6
+ route: { path, params: {}, filePath },
7
+ params,
8
+ layouts: [],
9
+ layoutServers: [],
10
+ } as any;
11
+ }
12
+
13
+ describe("Streaming SSR", () => {
14
+ test("returns a ReadableStream response", async () => {
15
+ const matched = makeMatched("/", "data:text/javascript,export default (p) => '<div>hello</div>'");
16
+ const res = await renderRoute(matched, new Request("http://localhost/"));
17
+ expect(res.body).toBeInstanceOf(ReadableStream);
18
+ });
19
+
20
+ test("stream contains full HTML document", async () => {
21
+ const matched = makeMatched("/test", "data:text/javascript,export default (p) => '<p>streaming works</p>'");
22
+
23
+ const res = await renderRoute(
24
+ matched,
25
+ new Request("http://localhost/test"),
26
+ );
27
+ const html = await res.text();
28
+
29
+ expect(html).toContain("<!DOCTYPE html>");
30
+ expect(html).toContain('<meta charset="UTF-8" />');
31
+ expect(html).toContain("streaming works");
32
+ expect(html).toContain("</html>");
33
+ expect(html).toContain("__grimoire_state__");
34
+ });
35
+
36
+ test("stream includes state JSON with route pattern", async () => {
37
+ const matched = makeMatched("/spells/:id", "data:text/javascript,export default (p) => '<div>spell</div>'", { id: "fireball" });
38
+
39
+ const res = await renderRoute(
40
+ matched,
41
+ new Request("http://localhost/spells/fireball"),
42
+ );
43
+ const html = await res.text();
44
+
45
+ expect(html).toContain('"pattern":"/spells/:id"');
46
+ expect(html).toContain('"id":"fireball"');
47
+ });
48
+
49
+ test("head script tag comes before head content", async () => {
50
+ const matched = makeMatched("/head-test", "data:text/javascript,export default (p) => '<div>head</div>'");
51
+
52
+ const res = await renderRoute(
53
+ matched,
54
+ new Request("http://localhost/head-test"),
55
+ );
56
+ const html = await res.text();
57
+
58
+ const hydrateScript = html.indexOf("hydrate.js");
59
+ const headClosing = html.indexOf("</head>");
60
+ // hydrate.js should be in <head>
61
+ expect(hydrateScript).toBeGreaterThan(-1);
62
+ expect(hydrateScript).toBeLessThan(headClosing);
63
+ });
64
+
65
+ test("stream chunks arrive incrementally", async () => {
66
+ const matched = makeMatched("/chunked", "data:text/javascript,export default (p) => '<div>chunked</div>'");
67
+
68
+ const res = await renderRoute(
69
+ matched,
70
+ new Request("http://localhost/chunked"),
71
+ );
72
+ const reader = res.body!.getReader();
73
+ const chunks: string[] = [];
74
+
75
+ while (true) {
76
+ const { done, value } = await reader.read();
77
+ if (done) break;
78
+ // Bun ReadableStream returns Uint8Array
79
+ chunks.push(
80
+ typeof value === "string"
81
+ ? value
82
+ : new TextDecoder().decode(value.buffer),
83
+ );
84
+ }
85
+
86
+ // Should have multiple chunks (at least DOCTYPE + body)
87
+ expect(chunks.length).toBeGreaterThan(1);
88
+ // First chunk should start with DOCTYPE
89
+ expect(chunks[0]).toContain("<!DOCTYPE html>");
90
+ // Last chunk should close the document
91
+ expect(chunks[chunks.length - 1]).toContain("</html>");
92
+ });
93
+
94
+ test("navigation request returns JSON not stream", async () => {
95
+ const matched = makeMatched("/nav", "data:text/javascript,export default (p) => '<div>nav</div>'");
96
+
97
+ const req = new Request("http://localhost/nav", {
98
+ headers: { "x-grimoire-navigate": "1" },
99
+ });
100
+ const res = await renderRoute(matched, req);
101
+ const json = await res.json();
102
+
103
+ expect(json.pattern).toBe("/nav");
104
+ expect(json.data).toBeDefined();
105
+ });
106
+ });
@@ -1,84 +1,84 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "fs/promises";
3
- import { tmpdir } from "os";
4
- import { join } from "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
- };
84
- }
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from "fs/promises";
3
+ import { tmpdir } from "os";
4
+ import { join } from "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
+ };
84
+ }
@@ -94,6 +94,7 @@ function makeRouteFile(
94
94
  path: "/",
95
95
  clientPath: "/src/routes/+page.tsx",
96
96
  paramNames: [],
97
+ restParamNames: [],
97
98
  ...overrides,
98
99
  };
99
100
  }
@@ -302,6 +303,15 @@ export async function POST() { return Response.json({}); }`,
302
303
  join(tmpDir, "src", "routes", "about.tsx"),
303
304
  "export default () => null;",
304
305
  );
306
+
307
+ // Rest (catch-all) param route
308
+ await mkdir(join(tmpDir, "src", "routes", "files", "[...path]"), {
309
+ recursive: true,
310
+ });
311
+ await writeFile(
312
+ join(tmpDir, "src", "routes", "files", "[...path]", "+server.ts"),
313
+ `export async function GET() { return new Response("ok"); }`,
314
+ );
305
315
  });
306
316
 
307
317
  afterAll(async () => {
@@ -471,7 +481,7 @@ describe("generateTypes — route with +page.server.ts", () => {
471
481
  });
472
482
 
473
483
  describe("generateTypes — +layout.server.ts", () => {
474
- test("LayoutData inferred from load() via ReturnType", async () => {
484
+ test("LayoutData inferred from load() via typeof import", async () => {
475
485
  const content = await readGenerated("src/routes/blog");
476
486
  expect(content).toMatch(/type _LS = typeof import\("/);
477
487
  expect(content).toContain(
@@ -513,6 +523,14 @@ describe("generateTypes — +error.tsx", () => {
513
523
  });
514
524
  });
515
525
 
526
+ describe("generateTypes — rest (catch-all) params", () => {
527
+ test("Params includes rest param name as string", async () => {
528
+ const content = await readGenerated("src/routes/files/[...path]");
529
+ expect(content).toContain("path: string;");
530
+ expect(content).toMatch(/export type Params = \{\s+path: string;\s+\};/);
531
+ });
532
+ });
533
+
516
534
  describe("generateTypes — clears stale output", () => {
517
535
  test("removes files from a deleted route on re-run", async () => {
518
536
  // First run already happened in beforeAll via runGenerate()