@sigil-dev/grimoire 0.9.4 → 0.9.5

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 +42 -20
  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 +10 -6
  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 +364 -364
  52. package/src/server/hooks.ts +114 -114
  53. package/src/server/index.ts +711 -677
  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 +295 -298
  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 +242 -242
  76. package/test/transform-routes.test.ts +87 -87
  77. package/test/typegen.test.ts +697 -697
  78. package/tsconfig.json +10 -10
@@ -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,121 +1,121 @@
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
+ 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
+ }