@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,56 +1,56 @@
1
- export interface InteractiveOptions {
2
- onQuit: () => void | Promise<void>;
3
- onRestart: () => void | Promise<void>;
4
- onClear: () => void;
5
- }
6
-
7
- /**
8
- * Returns a promise that resolves only when explicitly settled.
9
- * Used to keep the dev/start process alive until the user quits.
10
- */
11
- export function createWaiter() {
12
- let resolve!: () => void;
13
- const promise = new Promise<void>((r) => {
14
- resolve = r;
15
- });
16
- return { promise, resolve };
17
- }
18
-
19
- /**
20
- * Listen for interactive keypresses in dev mode.
21
- * Mirrors Vite's UX: q=quit, r=restart, c=clear.
22
- * Falls back gracefully if stdin is not a TTY (CI, piped input).
23
- */
24
- export function listenForKeys(opts: InteractiveOptions) {
25
- if (!process.stdin.isTTY) return;
26
-
27
- process.stdin.setRawMode(true);
28
- process.stdin.resume();
29
- process.stdin.setEncoding("utf8");
30
-
31
- process.stdin.on("data", async (key: string) => {
32
- // ctrl+c
33
- if (key === "\x03") {
34
- await opts.onQuit();
35
- return;
36
- }
37
-
38
- switch (key.toLowerCase()) {
39
- case "q":
40
- await opts.onQuit();
41
- break;
42
- case "r":
43
- await opts.onRestart();
44
- break;
45
- case "c":
46
- opts.onClear();
47
- break;
48
- }
49
- });
50
- }
51
-
52
- export function stopListening() {
53
- if (!process.stdin.isTTY) return;
54
- process.stdin.setRawMode(false);
55
- process.stdin.pause();
56
- }
1
+ export interface InteractiveOptions {
2
+ onQuit: () => void | Promise<void>;
3
+ onRestart: () => void | Promise<void>;
4
+ onClear: () => void;
5
+ }
6
+
7
+ /**
8
+ * Returns a promise that resolves only when explicitly settled.
9
+ * Used to keep the dev/start process alive until the user quits.
10
+ */
11
+ export function createWaiter() {
12
+ let resolve!: () => void;
13
+ const promise = new Promise<void>((r) => {
14
+ resolve = r;
15
+ });
16
+ return { promise, resolve };
17
+ }
18
+
19
+ /**
20
+ * Listen for interactive keypresses in dev mode.
21
+ * Mirrors Vite's UX: q=quit, r=restart, c=clear.
22
+ * Falls back gracefully if stdin is not a TTY (CI, piped input).
23
+ */
24
+ export function listenForKeys(opts: InteractiveOptions) {
25
+ if (!process.stdin.isTTY) return;
26
+
27
+ process.stdin.setRawMode(true);
28
+ process.stdin.resume();
29
+ process.stdin.setEncoding("utf8");
30
+
31
+ process.stdin.on("data", async (key: string) => {
32
+ // ctrl+c
33
+ if (key === "\x03") {
34
+ await opts.onQuit();
35
+ return;
36
+ }
37
+
38
+ switch (key.toLowerCase()) {
39
+ case "q":
40
+ await opts.onQuit();
41
+ break;
42
+ case "r":
43
+ await opts.onRestart();
44
+ break;
45
+ case "c":
46
+ opts.onClear();
47
+ break;
48
+ }
49
+ });
50
+ }
51
+
52
+ export function stopListening() {
53
+ if (!process.stdin.isTTY) return;
54
+ process.stdin.setRawMode(false);
55
+ process.stdin.pause();
56
+ }
@@ -1,120 +1,121 @@
1
- import type { RouteTree } from "../routing/scanner";
2
- import type {
3
- BuildResult,
4
- CoordinatorContext,
5
- GrimoirePlugin,
6
- LoadContext, PluginContext,
7
- Route,
8
- Server,
9
- WorkerDescriptor,
10
- WorkerEnv,
11
- } from "../types";
12
-
13
- // Fire-and-forget hooks routed through runHook.
14
- // onRequest and onRouteRender are intentionally excluded: they have distinct
15
- // calling conventions (middleware chain / inline transform loop) handled in index.ts.
16
- // Keep HookArgs in sync with GrimoirePlugin in types.ts.
17
- type HookArgs = {
18
- onPluginsResolved: [ctx: PluginContext];
19
- onStart: [server: Server];
20
- onStop: [reason: "shutdown" | "restart"];
21
- onBuildStart: [];
22
- onBuildEnd: [result: BuildResult];
23
- onRouteLoad: [route: Route, context: LoadContext];
24
- onCoordinatorStart: [ctx: CoordinatorContext];
25
- onWorkerReady: [worker: WorkerDescriptor];
26
- onWorkerDeath: [worker: WorkerDescriptor, reason: "crash" | "intentional"];
27
- };
28
- type FireAndForgetHook = keyof HookArgs;
29
-
30
- export async function runHook<K extends FireAndForgetHook>(
31
- plugins: GrimoirePlugin[],
32
- hook: K,
33
- ...args: HookArgs[K]
34
- ): Promise<void> {
35
- for (const plugin of plugins) {
36
- const fn = plugin[hook] as ((...a: any[]) => any) | undefined;
37
- await fn?.(...args);
38
- }
39
- }
40
-
41
- export async function runRequestHooks(
42
- plugins: GrimoirePlugin[],
43
- req: Request,
44
- final: () => Promise<Response>,
45
- ): Promise<Response> {
46
- const chain = plugins
47
- .filter((p) => p.onRequest)
48
- .reduceRight(
49
- (next, plugin) => async () => plugin.onRequest!(req, next),
50
- final,
51
- );
52
- return chain();
53
- }
54
-
55
- /**
56
- * Calls onWorkerSpawn on ALL plugins and merges results.
57
- * env is combined (later plugins override earlier on conflict).
58
- * name is last-write-wins.
59
- */
60
- export async function runWorkerSpawn(
61
- plugins: GrimoirePlugin[],
62
- worker: WorkerDescriptor,
63
- ): Promise<WorkerEnv> {
64
- const merged: WorkerEnv = { env: {} };
65
- for (const plugin of plugins) {
66
- const result = await plugin.onWorkerSpawn?.(worker);
67
- //@ts-expect-error WTF TS? its result? meaning its not gonna do anything if null.
68
- if (result?.env) Object.assign(merged.env!, result.env);
69
- //@ts-expect-error WTF TS? its result? meaning its not gonna do anything if null.
70
- if (result?.name) merged.name = result.name;
71
- }
72
- return merged;
73
- }
74
-
75
- /**
76
- * First plugin to return a non-null string wins.
77
- * Returns null if no plugin handles it — coordinator uses default.
78
- */
79
- export async function runSerializeLocals(
80
- plugins: GrimoirePlugin[],
81
- locals: App.Locals,
82
- ): Promise<string | null> {
83
- for (const plugin of plugins) {
84
- const result = await plugin.serializeLocals?.(locals);
85
- if (result != null) return result;
86
- }
87
- return null;
88
- }
89
-
90
- /**
91
- * First plugin to return non-null wins.
92
- * Returns null if no plugin handles it — worker uses default.
93
- */
94
- export async function runDeserializeLocals(
95
- plugins: GrimoirePlugin[],
96
- raw: string,
97
- ): Promise<App.Locals | null> {
98
- for (const plugin of plugins) {
99
- const result = await plugin.deserializeLocals?.(raw);
100
- if (result != null) return result;
101
- }
102
- return null;
103
- }
104
-
105
- /**
106
- * First plugin to return a WorkerDescriptor wins.
107
- * Returns null if no plugin handles it — coordinator uses default routing.
108
- */
109
- export async function runRouteRequest(
110
- plugins: GrimoirePlugin[],
111
- req: Request,
112
- workers: WorkerDescriptor[],
113
- routes: RouteTree,
114
- ): Promise<WorkerDescriptor | null> {
115
- for (const plugin of plugins) {
116
- const result = await plugin.routeRequest?.(req, workers, routes);
117
- if (result != null) return result;
118
- }
119
- return null;
120
- }
1
+ import type { Server } from "bun"
2
+ import type { RouteTree } from "../routing/scanner";
3
+ import type {
4
+ BuildResult,
5
+ CoordinatorContext,
6
+ GrimoirePlugin,
7
+ LoadContext,
8
+ PluginContext,
9
+ Route,
10
+ WorkerDescriptor,
11
+ WorkerEnv,
12
+ } from "../types";
13
+
14
+ // Fire-and-forget hooks routed through runHook.
15
+ // onRequest and onRouteRender are intentionally excluded: they have distinct
16
+ // calling conventions (middleware chain / inline transform loop) handled in index.ts.
17
+ // Keep HookArgs in sync with GrimoirePlugin in types.ts.
18
+ type HookArgs = {
19
+ onPluginsResolved: [ctx: PluginContext];
20
+ onStart: [server: Server<unknown>];
21
+ onStop: [reason: "shutdown" | "restart"];
22
+ onBuildStart: [];
23
+ onBuildEnd: [result: BuildResult];
24
+ onRouteLoad: [route: Route, context: LoadContext];
25
+ onCoordinatorStart: [ctx: CoordinatorContext];
26
+ onWorkerReady: [worker: WorkerDescriptor];
27
+ onWorkerDeath: [worker: WorkerDescriptor, reason: "crash" | "intentional"];
28
+ };
29
+ type FireAndForgetHook = keyof HookArgs;
30
+
31
+ export async function runHook<K extends FireAndForgetHook>(
32
+ plugins: GrimoirePlugin[],
33
+ hook: K,
34
+ ...args: HookArgs[K]
35
+ ): Promise<void> {
36
+ for (const plugin of plugins) {
37
+ const fn = plugin[hook] as ((...a: any[]) => any) | undefined;
38
+ await fn?.(...args);
39
+ }
40
+ }
41
+
42
+ export async function runRequestHooks(
43
+ plugins: GrimoirePlugin[],
44
+ req: Request,
45
+ final: () => Promise<Response>,
46
+ ): Promise<Response> {
47
+ const chain = plugins
48
+ .filter((p) => p.onRequest)
49
+ .reduceRight(
50
+ (next, plugin) => async () => plugin.onRequest!(req, next),
51
+ final,
52
+ );
53
+ return chain();
54
+ }
55
+
56
+ /**
57
+ * Calls onWorkerSpawn on ALL plugins and merges results.
58
+ * env is combined (later plugins override earlier on conflict).
59
+ * name is last-write-wins.
60
+ */
61
+ export async function runWorkerSpawn(
62
+ plugins: GrimoirePlugin[],
63
+ worker: WorkerDescriptor,
64
+ ): Promise<WorkerEnv> {
65
+ const merged: WorkerEnv = { env: {} };
66
+ for (const plugin of plugins) {
67
+ const result = await plugin.onWorkerSpawn?.(worker);
68
+ //@ts-expect-error WTF TS? its result? meaning its not gonna do anything if null.
69
+ if (result?.env) Object.assign(merged.env!, result.env);
70
+ //@ts-expect-error WTF TS? its result? meaning its not gonna do anything if null.
71
+ if (result?.name) merged.name = result.name;
72
+ }
73
+ return merged;
74
+ }
75
+
76
+ /**
77
+ * First plugin to return a non-null string wins.
78
+ * Returns null if no plugin handles it — coordinator uses default.
79
+ */
80
+ export async function runSerializeLocals(
81
+ plugins: GrimoirePlugin[],
82
+ locals: App.Locals,
83
+ ): Promise<string | null> {
84
+ for (const plugin of plugins) {
85
+ const result = await plugin.serializeLocals?.(locals);
86
+ if (result != null) return result;
87
+ }
88
+ return null;
89
+ }
90
+
91
+ /**
92
+ * First plugin to return non-null wins.
93
+ * Returns null if no plugin handles it — worker uses default.
94
+ */
95
+ export async function runDeserializeLocals(
96
+ plugins: GrimoirePlugin[],
97
+ raw: string,
98
+ ): Promise<App.Locals | null> {
99
+ for (const plugin of plugins) {
100
+ const result = await plugin.deserializeLocals?.(raw);
101
+ if (result != null) return result;
102
+ }
103
+ return null;
104
+ }
105
+
106
+ /**
107
+ * First plugin to return a WorkerDescriptor wins.
108
+ * Returns null if no plugin handles it — coordinator uses default routing.
109
+ */
110
+ export async function runRouteRequest(
111
+ plugins: GrimoirePlugin[],
112
+ req: Request,
113
+ workers: WorkerDescriptor[],
114
+ routes: RouteTree,
115
+ ): Promise<WorkerDescriptor | null> {
116
+ for (const plugin of plugins) {
117
+ const result = await plugin.routeRequest?.(req, workers, routes);
118
+ if (result != null) return result;
119
+ }
120
+ return null;
121
+ }
@@ -1,58 +1,58 @@
1
- import { join } from "node:path";
2
- import type { GrimoireConfig, GrimoirePlugin, WorkerMode } from "../types";
3
- import { createServer } from "./index";
4
-
5
- export interface WorkerOptions {
6
- config: GrimoireConfig;
7
- plugins: GrimoirePlugin[];
8
- secret: string;
9
- mode: WorkerMode;
10
- }
11
-
12
- export async function startWorker(options: WorkerOptions) {
13
- const { secret, plugins } = options;
14
-
15
- const workerAuthPlugin: GrimoirePlugin = {
16
- name: "__grimoire-worker-auth",
17
- async onRequest(req, next) {
18
- if (req.headers.get("X-Grimoire-Internal") !== secret) {
19
- return new Response("Forbidden", { status: 403 });
20
- }
21
- return next();
22
- },
23
- };
24
-
25
- return createServer({
26
- ...options.config,
27
- plugins: [workerAuthPlugin, ...plugins],
28
- _skipBuild: true,
29
- });
30
- }
31
-
32
- // bootstrap when spawned by coordinator
33
- if (process.env.GRIMOIRE_INTERNAL_SECRET) {
34
- const secret = process.env.GRIMOIRE_INTERNAL_SECRET;
35
- const port = Number(process.env.GRIMOIRE_WORKER_PORT ?? 3001);
36
- const cwd = process.cwd();
37
-
38
- let config: GrimoireConfig = {};
39
- try {
40
- const mod = await import(join(cwd, "sigil.config.ts"));
41
- config = mod.default ?? {};
42
- } catch {}
43
-
44
- let finalConfig: GrimoireConfig = { ...config, port, host: "127.0.0.1" };
45
- const plugins = finalConfig.plugins ?? [];
46
- for (const plugin of plugins) {
47
- finalConfig = plugin.config?.(finalConfig) ?? finalConfig;
48
- }
49
-
50
- await startWorker({
51
- config: finalConfig,
52
- plugins,
53
- secret,
54
- mode: (process.env.GRIMOIRE_MODE ?? "full") as WorkerMode,
55
- });
56
-
57
- process.send?.({ ready: true });
58
- }
1
+ import { join } from "node:path";
2
+ import type { GrimoireConfig, GrimoirePlugin, WorkerMode } from "../types";
3
+ import { createServer } from "./index";
4
+
5
+ export interface WorkerOptions {
6
+ config: GrimoireConfig;
7
+ plugins: GrimoirePlugin[];
8
+ secret: string;
9
+ mode: WorkerMode;
10
+ }
11
+
12
+ export async function startWorker(options: WorkerOptions) {
13
+ const { secret, plugins } = options;
14
+
15
+ const workerAuthPlugin: GrimoirePlugin = {
16
+ name: "__grimoire-worker-auth",
17
+ async onRequest(req, next) {
18
+ if (req.headers.get("X-Grimoire-Internal") !== secret) {
19
+ return new Response("Forbidden", { status: 403 });
20
+ }
21
+ return next();
22
+ },
23
+ };
24
+
25
+ return createServer({
26
+ ...options.config,
27
+ plugins: [workerAuthPlugin, ...plugins],
28
+ _skipBuild: true,
29
+ });
30
+ }
31
+
32
+ // bootstrap when spawned by coordinator
33
+ if (process.env.GRIMOIRE_INTERNAL_SECRET) {
34
+ const secret = process.env.GRIMOIRE_INTERNAL_SECRET;
35
+ const port = Number(process.env.GRIMOIRE_WORKER_PORT ?? 3001);
36
+ const cwd = process.cwd();
37
+
38
+ let config: GrimoireConfig = {};
39
+ try {
40
+ const mod = await import(join(cwd, "sigil.config.ts"));
41
+ config = mod.default ?? {};
42
+ } catch {}
43
+
44
+ let finalConfig: GrimoireConfig = { ...config, port, host: "127.0.0.1" };
45
+ const plugins = finalConfig.plugins ?? [];
46
+ for (const plugin of plugins) {
47
+ finalConfig = plugin.config?.(finalConfig) ?? finalConfig;
48
+ }
49
+
50
+ await startWorker({
51
+ config: finalConfig,
52
+ plugins,
53
+ secret,
54
+ mode: (process.env.GRIMOIRE_MODE ?? "full") as WorkerMode,
55
+ });
56
+
57
+ process.send?.({ ready: true });
58
+ }