@sigil-dev/grimoire 0.9.21 → 0.9.23

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 (94) hide show
  1. package/README.md +83 -83
  2. package/index.ts +50 -48
  3. package/package.json +22 -10
  4. package/public/__grimoire__/grimoire-client.js +12 -0
  5. package/public/__grimoire__/runtime.js +1241 -0
  6. package/server.ts +22 -20
  7. package/src/adapters/bun.ts +17 -0
  8. package/src/adapters/static.ts +41 -0
  9. package/src/adapters/types.ts +36 -0
  10. package/src/base.ts +91 -0
  11. package/src/client/csrf.ts +47 -47
  12. package/src/client/deferred.ts +179 -179
  13. package/src/client/enhance.ts +160 -160
  14. package/src/client/router.ts +871 -833
  15. package/src/dev/compile-module.ts +211 -211
  16. package/src/dev/loader.ts +141 -141
  17. package/src/dev/resolve.ts +70 -70
  18. package/src/dev/runtime-bundle.ts +51 -51
  19. package/src/env/modules.ts +62 -62
  20. package/src/env/required.ts +42 -0
  21. package/src/env/typegen.ts +61 -61
  22. package/src/headers.ts +105 -105
  23. package/src/logger/transports.ts +117 -117
  24. package/src/rendering/head.ts +49 -49
  25. package/src/rendering/hydrate.ts +161 -161
  26. package/src/rendering/images.ts +43 -43
  27. package/src/rendering/index.ts +755 -749
  28. package/src/rendering/preload.ts +102 -101
  29. package/src/rendering/ssrPlugin.ts +1 -1
  30. package/src/rendering/timing.ts +40 -40
  31. package/src/routing/router.ts +186 -186
  32. package/src/routing/scanner.ts +225 -196
  33. package/src/routing/transform-routes.ts +102 -102
  34. package/src/server/after-response.ts +206 -206
  35. package/src/server/asset-cache.ts +57 -57
  36. package/src/server/build.ts +262 -262
  37. package/src/server/cached.ts +114 -114
  38. package/src/server/client-address.ts +82 -82
  39. package/src/server/client-dir.ts +57 -57
  40. package/src/server/client-entry.ts +44 -44
  41. package/src/server/cookie-utils.ts +80 -80
  42. package/src/server/coordinator.ts +476 -471
  43. package/src/server/hooks.ts +122 -122
  44. package/src/server/index.ts +211 -80
  45. package/src/server/plugins.ts +172 -172
  46. package/src/server/prerender.ts +444 -0
  47. package/src/server/reserved.ts +17 -0
  48. package/src/server/resolver.ts +98 -98
  49. package/src/server/rewrite.ts +28 -0
  50. package/src/server/warm.ts +91 -91
  51. package/src/server/with-headers.ts +27 -27
  52. package/src/server/worker.ts +74 -72
  53. package/src/testing/dom.ts +73 -0
  54. package/src/testing/index.ts +169 -0
  55. package/src/testing/server.ts +162 -0
  56. package/src/typegen/index.ts +450 -450
  57. package/src/types.ts +501 -432
  58. package/test/after-response.test.ts +132 -132
  59. package/test/asset-cache-policy.test.ts +96 -96
  60. package/test/base.test.ts +134 -0
  61. package/test/cached.test.ts +126 -126
  62. package/test/client-address.test.ts +78 -78
  63. package/test/client-csrf.test.ts +61 -61
  64. package/test/client-dir.test.ts +101 -101
  65. package/test/client-router.test.ts +789 -734
  66. package/test/coordinator.test.ts +96 -94
  67. package/test/deferred.test.ts +256 -256
  68. package/test/dev-loader.test.ts +30 -30
  69. package/test/env.test.ts +168 -168
  70. package/test/force-production.test.ts +44 -0
  71. package/test/images.test.ts +49 -49
  72. package/test/locals-header.test.ts +61 -0
  73. package/test/middleware.test.ts +243 -243
  74. package/test/parser-plugins.test.ts +1 -1
  75. package/test/plugin-handle.test.ts +62 -62
  76. package/test/plugin-services-context.test.ts +97 -97
  77. package/test/preload.test.ts +48 -48
  78. package/test/prerender.test.ts +353 -0
  79. package/test/rendering.test.ts +799 -798
  80. package/test/required-env.test.ts +32 -0
  81. package/test/rewrite-html.test.ts +90 -0
  82. package/test/scanning.test.ts +349 -314
  83. package/test/server.test.ts +640 -619
  84. package/test/services.test.ts +90 -90
  85. package/test/streaming.test.ts +381 -378
  86. package/test/testing-helpers.test.ts +166 -0
  87. package/test/typegen.test.ts +808 -799
  88. package/test/warm.test.ts +96 -96
  89. package/test/worker-context.test.ts +59 -59
  90. package/test/workspace-files.test.ts +149 -146
  91. package/test/workspace-versions.test.ts +1 -1
  92. package/.grimoire/_routes.ts +0 -4
  93. package/public/__grimoire__/client.js +0 -55
  94. package/public/__grimoire__/hydrate.js +0 -63
@@ -1,101 +1,102 @@
1
- /**
2
- * `<link rel="modulepreload">` for the chunks a route needs.
3
- *
4
- * The client bundle loads a route's code with a lazy `import()`, which only
5
- * starts once hydrate.js has downloaded and run: HTML, then hydrate.js, then
6
- * the route chunk, then whatever that chunk imports. Naming those chunks in
7
- * the HTML lets the browser fetch them alongside hydrate.js instead of behind
8
- * it.
9
- *
10
- * The chunk graph is read back out of the built hydrate.js (the bundler
11
- * writes it there for its own preloading), so no build step has to cooperate.
12
- */
13
- import { existsSync, readFileSync } from "node:fs";
14
- import { join } from "node:path";
15
- import { clientDir } from "../server/client-dir";
16
- import { hydrateEntry } from "../server/client-entry";
17
-
18
- interface ChunkGraph {
19
- /** route pattern → chunk id */
20
- routes: Map<string, string>;
21
- /** chunk id → [file, ...ids it imports] */
22
- nodes: Map<string, { file: string; deps: string[] }>;
23
- }
24
-
25
- const BASE = "/__grimoire__/";
26
- let graph: ChunkGraph | null | undefined;
27
-
28
- export function parseChunkGraph(source: string): ChunkGraph | null {
29
- const call = /^__chunks\(import\.meta\.url,(\[.*?\]),(\[\[.*?\]\]),(\d+)\)/m.exec(
30
- source,
31
- );
32
- if (!call) return null;
33
- let ids: string[];
34
- let raw: Array<Array<string | number>>;
35
- try {
36
- ids = JSON.parse(call[1]);
37
- raw = JSON.parse(call[2]);
38
- } catch {
39
- return null;
40
- }
41
- const nodes: ChunkGraph["nodes"] = new Map();
42
- ids.forEach((id, i) => {
43
- const [file, ...deps] = raw[i] ?? [];
44
- if (typeof file !== "string") return;
45
- nodes.set(id, { file, deps: deps.map((d) => ids[Number(d)]).filter(Boolean) });
46
- });
47
- const routes = new Map<string, string>();
48
- for (const m of source.matchAll(
49
- /^\s*("(?:[^"\\]|\\.)*"):\s*\{\s*__lazy:\s*\(\)\s*=>\s*\(__preload\("([^"]+)"\)/gm,
50
- )) {
51
- routes.set(JSON.parse(m[1]), m[2]);
52
- }
53
- return { routes, nodes };
54
- }
55
-
56
- let graphOf: string | undefined;
57
-
58
- function load(): ChunkGraph | null {
59
- // a new entry name means a new build, whose chunks differ
60
- const entry = hydrateEntry();
61
- if (graph !== undefined && graphOf === entry) return graph;
62
- graphOf = entry;
63
- const file = join(clientDir(), entry);
64
- graph = existsSync(file) ? parseChunkGraph(readFileSync(file, "utf-8")) : null;
65
- return graph;
66
- }
67
-
68
- /** Files (relative to /__grimoire__/) the route's chunk needs, closure included. */
69
- export function chunksFor(g: ChunkGraph, pattern: string): string[] {
70
- const start = g.routes.get(pattern);
71
- if (!start) return [];
72
- const seen = new Set<string>();
73
- const stack = [start];
74
- const files: string[] = [];
75
- while (stack.length) {
76
- const id = stack.pop() as string;
77
- if (seen.has(id)) continue;
78
- seen.add(id);
79
- const node = g.nodes.get(id);
80
- if (!node) continue;
81
- // hydrate.js itself is already a <script> in the head
82
- if (!/(^|\/)hydrate\.js$/.test(node.file)) files.push(node.file.replace(/^\.\//, ""));
83
- stack.push(...node.deps);
84
- }
85
- return files;
86
- }
87
-
88
- /** The preload tags for a route, or "" in dev / when there is no built graph. */
89
- export function preloadLinks(pattern: string, nonceAttr: string, dev?: boolean): string {
90
- if (dev) return "";
91
- const g = load();
92
- if (!g) return "";
93
- return chunksFor(g, pattern)
94
- .map((f) => `\n <link rel="modulepreload" href="${BASE}${f}" crossorigin${nonceAttr}>`)
95
- .join("");
96
- }
97
-
98
- /** For tests: forget the cached graph. */
99
- export function resetChunkGraph(): void {
100
- graph = undefined;
101
- }
1
+ /**
2
+ * `<link rel="modulepreload">` for the chunks a route needs.
3
+ *
4
+ * The client bundle loads a route's code with a lazy `import()`, which only
5
+ * starts once hydrate.js has downloaded and run: HTML, then hydrate.js, then
6
+ * the route chunk, then whatever that chunk imports. Naming those chunks in
7
+ * the HTML lets the browser fetch them alongside hydrate.js instead of behind
8
+ * it.
9
+ *
10
+ * The chunk graph is read back out of the built hydrate.js (the bundler
11
+ * writes it there for its own preloading), so no build step has to cooperate.
12
+ */
13
+ import { existsSync, readFileSync } from "node:fs";
14
+ import { join } from "node:path";
15
+ import { withBase } from "../base";
16
+ import { clientDir } from "../server/client-dir";
17
+ import { hydrateEntry } from "../server/client-entry";
18
+
19
+ interface ChunkGraph {
20
+ /** route pattern → chunk id */
21
+ routes: Map<string, string>;
22
+ /** chunk id → [file, ...ids it imports] */
23
+ nodes: Map<string, { file: string; deps: string[] }>;
24
+ }
25
+
26
+ const DIR = "/__grimoire__/";
27
+ let graph: ChunkGraph | null | undefined;
28
+
29
+ export function parseChunkGraph(source: string): ChunkGraph | null {
30
+ const call = /^__chunks\(import\.meta\.url,(\[.*?\]),(\[\[.*?\]\]),(\d+)\)/m.exec(
31
+ source,
32
+ );
33
+ if (!call) return null;
34
+ let ids: string[];
35
+ let raw: Array<Array<string | number>>;
36
+ try {
37
+ ids = JSON.parse(call[1]);
38
+ raw = JSON.parse(call[2]);
39
+ } catch {
40
+ return null;
41
+ }
42
+ const nodes: ChunkGraph["nodes"] = new Map();
43
+ ids.forEach((id, i) => {
44
+ const [file, ...deps] = raw[i] ?? [];
45
+ if (typeof file !== "string") return;
46
+ nodes.set(id, { file, deps: deps.map((d) => ids[Number(d)]).filter(Boolean) });
47
+ });
48
+ const routes = new Map<string, string>();
49
+ for (const m of source.matchAll(
50
+ /^\s*("(?:[^"\\]|\\.)*"):\s*\{\s*__lazy:\s*\(\)\s*=>\s*\(__preload\("([^"]+)"\)/gm,
51
+ )) {
52
+ routes.set(JSON.parse(m[1]), m[2]);
53
+ }
54
+ return { routes, nodes };
55
+ }
56
+
57
+ let graphOf: string | undefined;
58
+
59
+ function load(): ChunkGraph | null {
60
+ // a new entry name means a new build, whose chunks differ
61
+ const entry = hydrateEntry();
62
+ if (graph !== undefined && graphOf === entry) return graph;
63
+ graphOf = entry;
64
+ const file = join(clientDir(), entry);
65
+ graph = existsSync(file) ? parseChunkGraph(readFileSync(file, "utf-8")) : null;
66
+ return graph;
67
+ }
68
+
69
+ /** Files (relative to /__grimoire__/) the route's chunk needs, closure included. */
70
+ export function chunksFor(g: ChunkGraph, pattern: string): string[] {
71
+ const start = g.routes.get(pattern);
72
+ if (!start) return [];
73
+ const seen = new Set<string>();
74
+ const stack = [start];
75
+ const files: string[] = [];
76
+ while (stack.length) {
77
+ const id = stack.pop() as string;
78
+ if (seen.has(id)) continue;
79
+ seen.add(id);
80
+ const node = g.nodes.get(id);
81
+ if (!node) continue;
82
+ // hydrate.js itself is already a <script> in the head
83
+ if (!/(^|\/)hydrate\.js$/.test(node.file)) files.push(node.file.replace(/^\.\//, ""));
84
+ stack.push(...node.deps);
85
+ }
86
+ return files;
87
+ }
88
+
89
+ /** The preload tags for a route, or "" in dev / when there is no built graph. */
90
+ export function preloadLinks(pattern: string, nonceAttr: string, dev?: boolean): string {
91
+ if (dev) return "";
92
+ const g = load();
93
+ if (!g) return "";
94
+ return chunksFor(g, pattern)
95
+ .map((f) => `\n <link rel="modulepreload" href="${withBase(DIR)}${f}" crossorigin${nonceAttr}>`)
96
+ .join("");
97
+ }
98
+
99
+ /** For tests: forget the cached graph. */
100
+ export function resetChunkGraph(): void {
101
+ graph = undefined;
102
+ }
@@ -8,7 +8,7 @@ import {
8
8
  import { log } from "../logger/instance";
9
9
  import { rewriteAliases } from "../server/resolver";
10
10
  import type { GrimoirePlugin } from "../types";
11
- import type { ParserPlugin } from "@babel/core";
11
+ import type { ParserPlugin } from "@babel/parser";
12
12
 
13
13
  const logger = log.scope("scope");
14
14
  let registered = false;
@@ -1,40 +1,40 @@
1
- /**
2
- * `Server-Timing` for route renders, so the browser's network panel shows
3
- * where a slow page spent its time (`load` = layout and page server loads,
4
- * `render` = components to HTML) without any extra tooling.
5
- *
6
- * On in dev; in production only when `serverTiming: true`, since durations are
7
- * visible to every visitor.
8
- */
9
- let enabled = false;
10
-
11
- export function setServerTiming(on: boolean): void {
12
- enabled = on;
13
- }
14
-
15
- export interface Timer {
16
- /** Close the phase that has been running since the last mark. */
17
- mark(name: string): void;
18
- /** `{ "Server-Timing": ... }`, or nothing when disabled or no marks. */
19
- headers(): Record<string, string>;
20
- }
21
-
22
- export function createTimer(): Timer {
23
- const marks: Array<[string, number]> = [];
24
- let last = performance.now();
25
- return {
26
- mark(name) {
27
- const now = performance.now();
28
- marks.push([name, now - last]);
29
- last = now;
30
- },
31
- headers() {
32
- if (!enabled || marks.length === 0) return {};
33
- return {
34
- "Server-Timing": marks
35
- .map(([n, d]) => `${n};dur=${d.toFixed(1)}`)
36
- .join(", "),
37
- };
38
- },
39
- };
40
- }
1
+ /**
2
+ * `Server-Timing` for route renders, so the browser's network panel shows
3
+ * where a slow page spent its time (`load` = layout and page server loads,
4
+ * `render` = components to HTML) without any extra tooling.
5
+ *
6
+ * On in dev; in production only when `serverTiming: true`, since durations are
7
+ * visible to every visitor.
8
+ */
9
+ let enabled = false;
10
+
11
+ export function setServerTiming(on: boolean): void {
12
+ enabled = on;
13
+ }
14
+
15
+ export interface Timer {
16
+ /** Close the phase that has been running since the last mark. */
17
+ mark(name: string): void;
18
+ /** `{ "Server-Timing": ... }`, or nothing when disabled or no marks. */
19
+ headers(): Record<string, string>;
20
+ }
21
+
22
+ export function createTimer(): Timer {
23
+ const marks: Array<[string, number]> = [];
24
+ let last = performance.now();
25
+ return {
26
+ mark(name) {
27
+ const now = performance.now();
28
+ marks.push([name, now - last]);
29
+ last = now;
30
+ },
31
+ headers() {
32
+ if (!enabled || marks.length === 0) return {};
33
+ return {
34
+ "Server-Timing": marks
35
+ .map(([n, d]) => `${n};dur=${d.toFixed(1)}`)
36
+ .join(", "),
37
+ };
38
+ },
39
+ };
40
+ }