@sigil-dev/grimoire 0.9.1 → 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 +9 -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,73 +1,73 @@
1
- import { isAbsolute, join, resolve } from "node:path";
2
- import type { Plugin } from "vite";
3
- import { renderRoute } from "../rendering";
4
- import { matchRoute } from "../routing/router.ts";
5
- import { scanRoutes } from "../routing/scanner.ts";
6
-
7
- const CLIENT_ENTRY = resolve(import.meta.dir, "./index.ts");
8
-
9
- export function grimoire(options: { routes?: string } = {}): Plugin {
10
- let isBuild = false;
11
-
12
- return {
13
- name: "grimoire",
14
-
15
- configResolved(config) {
16
- isBuild = config.command === "build";
17
- },
18
-
19
- configureServer(vite) {
20
- const routesDir = isAbsolute(options.routes ?? "src/routes")
21
- ? options.routes!
22
- : join(process.cwd(), options.routes ?? "src/routes");
23
-
24
- // client entry
25
- vite.middlewares.use("/__grimoire__/client.js", async (_req, res) => {
26
- const result = await vite.transformRequest(CLIENT_ENTRY);
27
- if (!result) {
28
- res.statusCode = 404;
29
- res.end();
30
- return;
31
- }
32
- res.setHeader("Content-Type", "application/javascript");
33
- res.end(result.code);
34
- });
35
-
36
- // page routes
37
- vite.middlewares.use(async (req, res, next) => {
38
- const url = new URL(req.url!, "http://localhost");
39
- if (url.pathname.startsWith("/__") || url.pathname.includes(".")) {
40
- return next();
41
- }
42
- try {
43
- const tree = await scanRoutes(routesDir, process.cwd());
44
- const matched = matchRoute(tree, url);
45
- if (!matched) return next();
46
-
47
- const response = await renderRoute(
48
- matched,
49
- new Request(`http://localhost${req.url}`),
50
- [],
51
- (path) => vite.ssrLoadModule(path), // Vite transforms SSR files correctly
52
- );
53
-
54
- const html = await response.text();
55
- res.setHeader("Content-Type", "text/html");
56
- res.end(html);
57
- } catch (e) {
58
- vite.ssrFixStacktrace(e as Error);
59
- next(e);
60
- }
61
- });
62
- },
63
-
64
- buildStart() {
65
- if (!isBuild) return;
66
- this.emitFile({
67
- type: "chunk",
68
- id: CLIENT_ENTRY,
69
- fileName: "client.js",
70
- });
71
- },
72
- };
73
- }
1
+ import { isAbsolute, join, resolve } from "node:path";
2
+ import type { Plugin } from "vite";
3
+ import { renderRoute } from "../rendering";
4
+ import { matchRoute } from "../routing/router.ts";
5
+ import { scanRoutes } from "../routing/scanner.ts";
6
+
7
+ const CLIENT_ENTRY = resolve(import.meta.dir, "./index.ts");
8
+
9
+ export function grimoire(options: { routes?: string } = {}): Plugin {
10
+ let isBuild = false;
11
+
12
+ return {
13
+ name: "grimoire",
14
+
15
+ configResolved(config) {
16
+ isBuild = config.command === "build";
17
+ },
18
+
19
+ configureServer(vite) {
20
+ const routesDir = isAbsolute(options.routes ?? "src/routes")
21
+ ? options.routes!
22
+ : join(process.cwd(), options.routes ?? "src/routes");
23
+
24
+ // client entry
25
+ vite.middlewares.use("/__grimoire__/client.js", async (_req, res) => {
26
+ const result = await vite.transformRequest(CLIENT_ENTRY);
27
+ if (!result) {
28
+ res.statusCode = 404;
29
+ res.end();
30
+ return;
31
+ }
32
+ res.setHeader("Content-Type", "application/javascript");
33
+ res.end(result.code);
34
+ });
35
+
36
+ // page routes
37
+ vite.middlewares.use(async (req, res, next) => {
38
+ const url = new URL(req.url!, "http://localhost");
39
+ if (url.pathname.startsWith("/__") || url.pathname.includes(".")) {
40
+ return next();
41
+ }
42
+ try {
43
+ const tree = await scanRoutes(routesDir, process.cwd());
44
+ const matched = matchRoute(tree, url);
45
+ if (!matched) return next();
46
+
47
+ const response = await renderRoute(
48
+ matched,
49
+ new Request(`http://localhost${req.url}`),
50
+ [],
51
+ (path) => vite.ssrLoadModule(path), // Vite transforms SSR files correctly
52
+ );
53
+
54
+ const html = await response.text();
55
+ res.setHeader("Content-Type", "text/html");
56
+ res.end(html);
57
+ } catch (e) {
58
+ vite.ssrFixStacktrace(e as Error);
59
+ next(e);
60
+ }
61
+ });
62
+ },
63
+
64
+ buildStart() {
65
+ if (!isBuild) return;
66
+ this.emitFile({
67
+ type: "chunk",
68
+ id: CLIENT_ENTRY,
69
+ fileName: "client.js",
70
+ });
71
+ },
72
+ };
73
+ }
@@ -1,4 +1,4 @@
1
- export * from "./logger.ts";
2
- export * from "./request-middleware.ts";
3
- export * from "./transports.ts";
4
- export * from "./types.ts";
1
+ export * from "./logger.ts";
2
+ export * from "./request-middleware.ts";
3
+ export * from "./transports.ts";
4
+ export * from "./types.ts";
@@ -1,6 +1,6 @@
1
- import { Logger } from "./logger.ts";
2
- import { ConsoleTransport } from "./transports.ts";
3
-
4
- export const log = new Logger("grimoire", {
5
- transports: [new ConsoleTransport()],
6
- });
1
+ import { Logger } from "./logger.ts";
2
+ import { ConsoleTransport } from "./transports.ts";
3
+
4
+ export const log = new Logger("grimoire", {
5
+ transports: [new ConsoleTransport()],
6
+ });
@@ -1,113 +1,113 @@
1
- // packages/grimoire/src/logger/logger.ts
2
-
3
- import type {
4
- LogEntry,
5
- LoggerOptions,
6
- LogLevel,
7
- LogTransport,
8
- ScopedLogger,
9
- } from "./types.ts";
10
- import { LOG_LEVELS } from "./types.ts";
11
-
12
- export class Logger implements ScopedLogger {
13
- private transports: LogTransport[];
14
- private minLevel: number;
15
- private filter?: (entry: LogEntry) => boolean;
16
- private _scope: string;
17
- private _worker?: string;
18
- private _requestId?: string;
19
-
20
- constructor(scope: string, options: LoggerOptions = {}, worker?: string) {
21
- this._scope = scope;
22
- this._worker = worker;
23
- this.transports = options.transports ?? [];
24
- this.minLevel =
25
- LOG_LEVELS[
26
- options.level ??
27
- (process.env.LOG_LEVEL as LogLevel | undefined) ??
28
- "info"
29
- ] ?? LOG_LEVELS.info;
30
- this.filter = options.filter;
31
- }
32
-
33
- // called by loom coordinator after forking workers
34
- setWorker(name: string) {
35
- this._worker = name;
36
- }
37
-
38
- private write(level: LogLevel, message: string, ...args: unknown[]) {
39
- if (LOG_LEVELS[level] < this.minLevel) return;
40
-
41
- const data =
42
- args.length === 0 ? undefined : args.length === 1 ? args[0] : args;
43
-
44
- const entry: LogEntry = {
45
- level,
46
- scope: this._scope,
47
- message,
48
- data,
49
- timestamp: Date.now(),
50
- worker: this._worker,
51
- requestId: this._requestId,
52
- };
53
-
54
- if (this.filter && !this.filter(entry)) return;
55
- for (const t of this.transports) {
56
- try {
57
- t.write(entry);
58
- } catch {}
59
- }
60
- }
61
-
62
- debug(message: string, ...args: unknown[]) {
63
- this.write("debug", message, ...args);
64
- }
65
- info(message: string, ...args: unknown[]) {
66
- this.write("info", message, ...args);
67
- }
68
- warn(message: string, ...args: unknown[]) {
69
- this.write("warn", message, ...args);
70
- }
71
- error(message: string, ...args: unknown[]) {
72
- this.write("error", message, ...args);
73
- }
74
- fatal(message: string, ...args: unknown[]) {
75
- this.write("fatal", message, ...args);
76
- }
77
-
78
- scope(name: string): ScopedLogger {
79
- const child = new Logger(
80
- `${this._scope}:${name}`,
81
- {
82
- transports: this.transports,
83
- level: Object.keys(LOG_LEVELS).find(
84
- (k) => LOG_LEVELS[k as LogLevel] === this.minLevel,
85
- ) as LogLevel,
86
- filter: this.filter,
87
- },
88
- this._worker,
89
- );
90
- child._requestId = this._requestId;
91
- return child;
92
- }
93
-
94
- withRequest(requestId: string): ScopedLogger {
95
- const child = new Logger(
96
- this._scope,
97
- {
98
- transports: this.transports,
99
- level: Object.keys(LOG_LEVELS).find(
100
- (k) => LOG_LEVELS[k as LogLevel] === this.minLevel,
101
- ) as LogLevel,
102
- filter: this.filter,
103
- },
104
- this._worker,
105
- );
106
- child._requestId = requestId;
107
- return child;
108
- }
109
-
110
- addTransport(t: LogTransport) {
111
- this.transports.push(t);
112
- }
113
- }
1
+ // packages/grimoire/src/logger/logger.ts
2
+
3
+ import type {
4
+ LogEntry,
5
+ LoggerOptions,
6
+ LogLevel,
7
+ LogTransport,
8
+ ScopedLogger,
9
+ } from "./types.ts";
10
+ import { LOG_LEVELS } from "./types.ts";
11
+
12
+ export class Logger implements ScopedLogger {
13
+ private transports: LogTransport[];
14
+ private minLevel: number;
15
+ private filter?: (entry: LogEntry) => boolean;
16
+ private _scope: string;
17
+ private _worker?: string;
18
+ private _requestId?: string;
19
+
20
+ constructor(scope: string, options: LoggerOptions = {}, worker?: string) {
21
+ this._scope = scope;
22
+ this._worker = worker;
23
+ this.transports = options.transports ?? [];
24
+ this.minLevel =
25
+ LOG_LEVELS[
26
+ options.level ??
27
+ (process.env.LOG_LEVEL as LogLevel | undefined) ??
28
+ "info"
29
+ ] ?? LOG_LEVELS.info;
30
+ this.filter = options.filter;
31
+ }
32
+
33
+ // called by loom coordinator after forking workers
34
+ setWorker(name: string) {
35
+ this._worker = name;
36
+ }
37
+
38
+ private write(level: LogLevel, message: string, ...args: unknown[]) {
39
+ if (LOG_LEVELS[level] < this.minLevel) return;
40
+
41
+ const data =
42
+ args.length === 0 ? undefined : args.length === 1 ? args[0] : args;
43
+
44
+ const entry: LogEntry = {
45
+ level,
46
+ scope: this._scope,
47
+ message,
48
+ data,
49
+ timestamp: Date.now(),
50
+ worker: this._worker,
51
+ requestId: this._requestId,
52
+ };
53
+
54
+ if (this.filter && !this.filter(entry)) return;
55
+ for (const t of this.transports) {
56
+ try {
57
+ t.write(entry);
58
+ } catch {}
59
+ }
60
+ }
61
+
62
+ debug(message: string, ...args: unknown[]) {
63
+ this.write("debug", message, ...args);
64
+ }
65
+ info(message: string, ...args: unknown[]) {
66
+ this.write("info", message, ...args);
67
+ }
68
+ warn(message: string, ...args: unknown[]) {
69
+ this.write("warn", message, ...args);
70
+ }
71
+ error(message: string, ...args: unknown[]) {
72
+ this.write("error", message, ...args);
73
+ }
74
+ fatal(message: string, ...args: unknown[]) {
75
+ this.write("fatal", message, ...args);
76
+ }
77
+
78
+ scope(name: string): ScopedLogger {
79
+ const child = new Logger(
80
+ `${this._scope}:${name}`,
81
+ {
82
+ transports: this.transports,
83
+ level: Object.keys(LOG_LEVELS).find(
84
+ (k) => LOG_LEVELS[k as LogLevel] === this.minLevel,
85
+ ) as LogLevel,
86
+ filter: this.filter,
87
+ },
88
+ this._worker,
89
+ );
90
+ child._requestId = this._requestId;
91
+ return child;
92
+ }
93
+
94
+ withRequest(requestId: string): ScopedLogger {
95
+ const child = new Logger(
96
+ this._scope,
97
+ {
98
+ transports: this.transports,
99
+ level: Object.keys(LOG_LEVELS).find(
100
+ (k) => LOG_LEVELS[k as LogLevel] === this.minLevel,
101
+ ) as LogLevel,
102
+ filter: this.filter,
103
+ },
104
+ this._worker,
105
+ );
106
+ child._requestId = requestId;
107
+ return child;
108
+ }
109
+
110
+ addTransport(t: LogTransport) {
111
+ this.transports.push(t);
112
+ }
113
+ }
@@ -1,24 +1,24 @@
1
- // packages/grimoire/src/logger/request-middleware.ts
2
- // Injected automatically by Grimoire before hooks.server.ts runs.
3
-
4
- import crypto from "node:crypto";
5
- import type { Logger } from "./logger.ts";
6
- import type { ScopedLogger } from "./types.ts";
7
-
8
- declare module "@sigil-dev/grimoire" {
9
- interface RequestLocals {
10
- log: ScopedLogger;
11
- }
12
- }
13
-
14
- export function createRequestMiddleware(root: Logger) {
15
- return function requestMiddleware(
16
- _req: Request,
17
- locals: Record<string, unknown>,
18
- ) {
19
- // 8-char hex ID — short enough to read in a terminal
20
- const requestId = crypto.randomUUID().slice(0, 8);
21
- locals.log = root.withRequest(requestId);
22
- (locals.log as any)._requestId = requestId;
23
- };
24
- }
1
+ // packages/grimoire/src/logger/request-middleware.ts
2
+ // Injected automatically by Grimoire before hooks.server.ts runs.
3
+
4
+ import crypto from "node:crypto";
5
+ import type { Logger } from "./logger.ts";
6
+ import type { ScopedLogger } from "./types.ts";
7
+
8
+ declare module "@sigil-dev/grimoire" {
9
+ interface RequestLocals {
10
+ log: ScopedLogger;
11
+ }
12
+ }
13
+
14
+ export function createRequestMiddleware(root: Logger) {
15
+ return function requestMiddleware(
16
+ _req: Request,
17
+ locals: Record<string, unknown>,
18
+ ) {
19
+ // 8-char hex ID — short enough to read in a terminal
20
+ const requestId = crypto.randomUUID().slice(0, 8);
21
+ locals.log = root.withRequest(requestId);
22
+ (locals.log as any)._requestId = requestId;
23
+ };
24
+ }