@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.
- package/index.ts +35 -35
- package/package.json +9 -8
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +13 -7
- package/src/client/enhance.ts +98 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +2 -2
- package/src/client/router.ts +445 -371
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +163 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +114 -114
- package/src/dev/hmr-client.ts +185 -185
- package/src/dev/loader.ts +47 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/watcher.ts +47 -46
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +47 -47
- package/src/rendering/hydrate.ts +98 -79
- package/src/rendering/index.ts +455 -372
- package/src/rendering/ssrPlugin.ts +69 -69
- package/src/routing/manifest-gen.ts +12 -11
- package/src/routing/router.ts +109 -109
- package/src/routing/scanner.ts +141 -141
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +243 -219
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -356
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +655 -658
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -120
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +356 -356
- package/src/types.ts +4 -4
- package/test/client-router.test.ts +37 -31
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +70 -70
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +615 -579
- package/test/routing.test.ts +81 -81
- package/test/scanning.test.ts +200 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +249 -249
- package/test/streaming.test.ts +125 -125
- package/test/transform-routes.test.ts +84 -84
- package/test/typegen.test.ts +682 -682
- package/tsconfig.json +10 -10
- package/.grimoire/_routes.dom.js +0 -8
- package/.grimoire/_routes.hydrate.js +0 -8
- package/.grimoire/tsconfig.generated.json +0 -11
- package/.grimoire/types/ambient.d.ts +0 -59
- package/.grimoire/types/api/hello/$types.d.ts +0 -50
- package/.grimoire/types/api/items/$types.d.ts +0 -50
- package/.grimoire/types/echo/$types.d.ts +0 -50
- package/.grimoire/types/env-private.d.ts +0 -5
- package/.grimoire/types/env-public.d.ts +0 -5
- package/.grimoire/types/mixed/$types.d.ts +0 -50
- package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
- package/.grimoire/types/reject/$types.d.ts +0 -50
- package/public/__grimoire__/hydrate.js +0 -585
- package/public/__grimoire__/index.js +0 -490
package/src/logger/transports.ts
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
// packages/grimoire/src/logger/transports.ts
|
|
2
|
-
|
|
3
|
-
import { appendFileSync } from "node:fs";
|
|
4
|
-
import type { LogEntry, LogTransport } from "./types.ts";
|
|
5
|
-
|
|
6
|
-
// ── colours (only applied when stdout is a TTY) ──────────────────────────────
|
|
7
|
-
|
|
8
|
-
const isTTY = process.stdout.isTTY;
|
|
9
|
-
const c = isTTY
|
|
10
|
-
? {
|
|
11
|
-
reset: "\x1b[0m",
|
|
12
|
-
dim: "\x1b[2m",
|
|
13
|
-
bold: "\x1b[1m",
|
|
14
|
-
debug: "\x1b[90m", // grey
|
|
15
|
-
info: "\x1b[36m", // cyan
|
|
16
|
-
warn: "\x1b[33m", // yellow
|
|
17
|
-
error: "\x1b[31m", // red
|
|
18
|
-
fatal: "\x1b[35m", // magenta
|
|
19
|
-
scope: "\x1b[34m", // blue
|
|
20
|
-
req: "\x1b[32m", // green
|
|
21
|
-
}
|
|
22
|
-
: (Object.fromEntries(
|
|
23
|
-
[
|
|
24
|
-
"reset",
|
|
25
|
-
"dim",
|
|
26
|
-
"bold",
|
|
27
|
-
"debug",
|
|
28
|
-
"info",
|
|
29
|
-
"warn",
|
|
30
|
-
"error",
|
|
31
|
-
"fatal",
|
|
32
|
-
"scope",
|
|
33
|
-
"req",
|
|
34
|
-
].map((k) => [k, ""]),
|
|
35
|
-
) as Record<string, string>);
|
|
36
|
-
|
|
37
|
-
const LEVEL_LABEL: Record<string, string> = {
|
|
38
|
-
debug: "DBG",
|
|
39
|
-
info: "INF",
|
|
40
|
-
warn: "WRN",
|
|
41
|
-
error: "ERR",
|
|
42
|
-
fatal: "FTL",
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
function ts(timestamp: number): string {
|
|
46
|
-
return new Date(timestamp).toISOString().slice(11, 23); // HH:mm:ss.ms
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function formatData(data: unknown): string {
|
|
50
|
-
if (data === undefined) return "";
|
|
51
|
-
if (data instanceof Error) return `\n ${data.stack ?? data.message}`;
|
|
52
|
-
if (typeof data !== "object" || data === null) return ` ${String(data)}`;
|
|
53
|
-
|
|
54
|
-
// flat key=value for simple objects, JSON block only for nested/complex
|
|
55
|
-
const entries = Object.entries(data as Record<string, unknown>);
|
|
56
|
-
const isFlat = entries.every(([, v]) => typeof v !== "object" || v === null);
|
|
57
|
-
if (isFlat) {
|
|
58
|
-
return ` ${entries.map(([k, v]) => `${colors.dim(`${k}=`)}${v}`).join(" ")}`;
|
|
59
|
-
}
|
|
60
|
-
// nested — indent but don't pretty-print the whole thing
|
|
61
|
-
return `\n ${JSON.stringify(data).replace(/,/g, ", ")}`;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// ── ConsoleTransport ─────────────────────────────────────────────────────────
|
|
65
|
-
|
|
66
|
-
export class ConsoleTransport implements LogTransport {
|
|
67
|
-
write(entry: LogEntry) {
|
|
68
|
-
const levelColor = c[entry.level] ?? c.info;
|
|
69
|
-
const label = LEVEL_LABEL[entry.level] ?? entry.level.toUpperCase();
|
|
70
|
-
|
|
71
|
-
const worker = entry.worker ? `${c.dim}[${entry.worker}]${c.reset} ` : "";
|
|
72
|
-
const requestId = entry.requestId
|
|
73
|
-
? `${c.req}#${entry.requestId.slice(0, 8)}${c.reset} `
|
|
74
|
-
: "";
|
|
75
|
-
const scope = `${c.scope}${entry.scope}${c.reset}`;
|
|
76
|
-
const time = `${c.dim}${ts(entry.timestamp)}${c.reset}`;
|
|
77
|
-
const lvl = `${levelColor}${c.bold}${label}${c.reset}`;
|
|
78
|
-
const msg = entry.message;
|
|
79
|
-
const data = formatData(entry.data);
|
|
80
|
-
|
|
81
|
-
const line = `${time} ${lvl} ${worker}${requestId}${scope} ${msg}${data}`;
|
|
82
|
-
|
|
83
|
-
if (entry.level === "error" || entry.level === "fatal") {
|
|
84
|
-
process.stderr.write(`${line}\n`);
|
|
85
|
-
} else {
|
|
86
|
-
process.stdout.write(`${line}\n`);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// ── FileTransport ────────────────────────────────────────────────────────────
|
|
92
|
-
// Writes newline-delimited JSON for ingestion by log collectors.
|
|
93
|
-
|
|
94
|
-
export class FileTransport implements LogTransport {
|
|
95
|
-
constructor(private path: string) {}
|
|
96
|
-
|
|
97
|
-
write(entry: LogEntry) {
|
|
98
|
-
try {
|
|
99
|
-
appendFileSync(this.path, `${JSON.stringify(entry)}\n`);
|
|
100
|
-
} catch {
|
|
101
|
-
// if the file write fails we can't really do anything useful
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// ── NullTransport ────────────────────────────────────────────────────────────
|
|
107
|
-
// For tests — captures entries instead of printing them.
|
|
108
|
-
|
|
109
|
-
export class NullTransport implements LogTransport {
|
|
110
|
-
readonly entries: LogEntry[] = [];
|
|
111
|
-
write(entry: LogEntry) {
|
|
112
|
-
this.entries.push(entry);
|
|
113
|
-
}
|
|
114
|
-
clear() {
|
|
115
|
-
this.entries.length = 0;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
1
|
+
// packages/grimoire/src/logger/transports.ts
|
|
2
|
+
|
|
3
|
+
import { appendFileSync } from "node:fs";
|
|
4
|
+
import type { LogEntry, LogTransport } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
// ── colours (only applied when stdout is a TTY) ──────────────────────────────
|
|
7
|
+
|
|
8
|
+
const isTTY = process.stdout.isTTY;
|
|
9
|
+
const c = isTTY
|
|
10
|
+
? {
|
|
11
|
+
reset: "\x1b[0m",
|
|
12
|
+
dim: "\x1b[2m",
|
|
13
|
+
bold: "\x1b[1m",
|
|
14
|
+
debug: "\x1b[90m", // grey
|
|
15
|
+
info: "\x1b[36m", // cyan
|
|
16
|
+
warn: "\x1b[33m", // yellow
|
|
17
|
+
error: "\x1b[31m", // red
|
|
18
|
+
fatal: "\x1b[35m", // magenta
|
|
19
|
+
scope: "\x1b[34m", // blue
|
|
20
|
+
req: "\x1b[32m", // green
|
|
21
|
+
}
|
|
22
|
+
: (Object.fromEntries(
|
|
23
|
+
[
|
|
24
|
+
"reset",
|
|
25
|
+
"dim",
|
|
26
|
+
"bold",
|
|
27
|
+
"debug",
|
|
28
|
+
"info",
|
|
29
|
+
"warn",
|
|
30
|
+
"error",
|
|
31
|
+
"fatal",
|
|
32
|
+
"scope",
|
|
33
|
+
"req",
|
|
34
|
+
].map((k) => [k, ""]),
|
|
35
|
+
) as Record<string, string>);
|
|
36
|
+
|
|
37
|
+
const LEVEL_LABEL: Record<string, string> = {
|
|
38
|
+
debug: "DBG",
|
|
39
|
+
info: "INF",
|
|
40
|
+
warn: "WRN",
|
|
41
|
+
error: "ERR",
|
|
42
|
+
fatal: "FTL",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function ts(timestamp: number): string {
|
|
46
|
+
return new Date(timestamp).toISOString().slice(11, 23); // HH:mm:ss.ms
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function formatData(data: unknown): string {
|
|
50
|
+
if (data === undefined) return "";
|
|
51
|
+
if (data instanceof Error) return `\n ${data.stack ?? data.message}`;
|
|
52
|
+
if (typeof data !== "object" || data === null) return ` ${String(data)}`;
|
|
53
|
+
|
|
54
|
+
// flat key=value for simple objects, JSON block only for nested/complex
|
|
55
|
+
const entries = Object.entries(data as Record<string, unknown>);
|
|
56
|
+
const isFlat = entries.every(([, v]) => typeof v !== "object" || v === null);
|
|
57
|
+
if (isFlat) {
|
|
58
|
+
return ` ${entries.map(([k, v]) => `${colors.dim(`${k}=`)}${v}`).join(" ")}`;
|
|
59
|
+
}
|
|
60
|
+
// nested — indent but don't pretty-print the whole thing
|
|
61
|
+
return `\n ${JSON.stringify(data).replace(/,/g, ", ")}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ── ConsoleTransport ─────────────────────────────────────────────────────────
|
|
65
|
+
|
|
66
|
+
export class ConsoleTransport implements LogTransport {
|
|
67
|
+
write(entry: LogEntry) {
|
|
68
|
+
const levelColor = c[entry.level] ?? c.info;
|
|
69
|
+
const label = LEVEL_LABEL[entry.level] ?? entry.level.toUpperCase();
|
|
70
|
+
|
|
71
|
+
const worker = entry.worker ? `${c.dim}[${entry.worker}]${c.reset} ` : "";
|
|
72
|
+
const requestId = entry.requestId
|
|
73
|
+
? `${c.req}#${entry.requestId.slice(0, 8)}${c.reset} `
|
|
74
|
+
: "";
|
|
75
|
+
const scope = `${c.scope}${entry.scope}${c.reset}`;
|
|
76
|
+
const time = `${c.dim}${ts(entry.timestamp)}${c.reset}`;
|
|
77
|
+
const lvl = `${levelColor}${c.bold}${label}${c.reset}`;
|
|
78
|
+
const msg = entry.message;
|
|
79
|
+
const data = formatData(entry.data);
|
|
80
|
+
|
|
81
|
+
const line = `${time} ${lvl} ${worker}${requestId}${scope} ${msg}${data}`;
|
|
82
|
+
|
|
83
|
+
if (entry.level === "error" || entry.level === "fatal") {
|
|
84
|
+
process.stderr.write(`${line}\n`);
|
|
85
|
+
} else {
|
|
86
|
+
process.stdout.write(`${line}\n`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ── FileTransport ────────────────────────────────────────────────────────────
|
|
92
|
+
// Writes newline-delimited JSON for ingestion by log collectors.
|
|
93
|
+
|
|
94
|
+
export class FileTransport implements LogTransport {
|
|
95
|
+
constructor(private path: string) {}
|
|
96
|
+
|
|
97
|
+
write(entry: LogEntry) {
|
|
98
|
+
try {
|
|
99
|
+
appendFileSync(this.path, `${JSON.stringify(entry)}\n`);
|
|
100
|
+
} catch {
|
|
101
|
+
// if the file write fails we can't really do anything useful
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ── NullTransport ────────────────────────────────────────────────────────────
|
|
107
|
+
// For tests — captures entries instead of printing them.
|
|
108
|
+
|
|
109
|
+
export class NullTransport implements LogTransport {
|
|
110
|
+
readonly entries: LogEntry[] = [];
|
|
111
|
+
write(entry: LogEntry) {
|
|
112
|
+
this.entries.push(entry);
|
|
113
|
+
}
|
|
114
|
+
clear() {
|
|
115
|
+
this.entries.length = 0;
|
|
116
|
+
}
|
|
117
|
+
}
|
package/src/logger/types.ts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
export type LogLevel = "debug" | "info" | "warn" | "error" | "fatal";
|
|
2
|
-
|
|
3
|
-
export const LOG_LEVELS: Record<LogLevel, number> = {
|
|
4
|
-
debug: 0,
|
|
5
|
-
info: 1,
|
|
6
|
-
warn: 2,
|
|
7
|
-
error: 3,
|
|
8
|
-
fatal: 4,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export interface LogEntry {
|
|
12
|
-
level: LogLevel;
|
|
13
|
-
scope: string;
|
|
14
|
-
message: string;
|
|
15
|
-
data?: unknown;
|
|
16
|
-
timestamp: number;
|
|
17
|
-
worker?: string; // injected by loom, e.g. "violet"
|
|
18
|
-
requestId?: string; // injected per-request by middleware
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface LogTransport {
|
|
22
|
-
write(entry: LogEntry): void | Promise<void>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface ScopedLogger {
|
|
26
|
-
debug(message: string, ...args: unknown[]): void;
|
|
27
|
-
info(message: string, ...args: unknown[]): void;
|
|
28
|
-
warn(message: string, ...args: unknown[]): void;
|
|
29
|
-
error(message: string, ...args: unknown[]): void;
|
|
30
|
-
fatal(message: string, ...args: unknown[]): void;
|
|
31
|
-
scope(name: string): ScopedLogger;
|
|
32
|
-
withRequest(requestId: string): ScopedLogger;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface LoggerOptions {
|
|
36
|
-
transports?: LogTransport[];
|
|
37
|
-
level?: LogLevel;
|
|
38
|
-
filter?: (entry: LogEntry) => boolean;
|
|
39
|
-
}
|
|
1
|
+
export type LogLevel = "debug" | "info" | "warn" | "error" | "fatal";
|
|
2
|
+
|
|
3
|
+
export const LOG_LEVELS: Record<LogLevel, number> = {
|
|
4
|
+
debug: 0,
|
|
5
|
+
info: 1,
|
|
6
|
+
warn: 2,
|
|
7
|
+
error: 3,
|
|
8
|
+
fatal: 4,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export interface LogEntry {
|
|
12
|
+
level: LogLevel;
|
|
13
|
+
scope: string;
|
|
14
|
+
message: string;
|
|
15
|
+
data?: unknown;
|
|
16
|
+
timestamp: number;
|
|
17
|
+
worker?: string; // injected by loom, e.g. "violet"
|
|
18
|
+
requestId?: string; // injected per-request by middleware
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface LogTransport {
|
|
22
|
+
write(entry: LogEntry): void | Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ScopedLogger {
|
|
26
|
+
debug(message: string, ...args: unknown[]): void;
|
|
27
|
+
info(message: string, ...args: unknown[]): void;
|
|
28
|
+
warn(message: string, ...args: unknown[]): void;
|
|
29
|
+
error(message: string, ...args: unknown[]): void;
|
|
30
|
+
fatal(message: string, ...args: unknown[]): void;
|
|
31
|
+
scope(name: string): ScopedLogger;
|
|
32
|
+
withRequest(requestId: string): ScopedLogger;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface LoggerOptions {
|
|
36
|
+
transports?: LogTransport[];
|
|
37
|
+
level?: LogLevel;
|
|
38
|
+
filter?: (entry: LogEntry) => boolean;
|
|
39
|
+
}
|
package/src/rendering/head.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
2
|
-
|
|
3
|
-
const HeadKey = createContext<string[]>();
|
|
4
|
-
const NonceKey = createContext<string>();
|
|
5
|
-
|
|
6
|
-
export function initHead(nonce?: string): void {
|
|
7
|
-
setContext(HeadKey, []);
|
|
8
|
-
if (nonce) setContext(NonceKey, nonce);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function setHeadNonce(nonce: string): void {
|
|
12
|
-
setContext(NonceKey, nonce);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function getHeadNonce(): string {
|
|
16
|
-
return getContext(NonceKey) ?? "";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function collectHead(): string {
|
|
20
|
-
return (getContext(HeadKey) ?? []).join("\n ");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function Head({
|
|
24
|
-
children,
|
|
25
|
-
}: {
|
|
26
|
-
children: string | Node;
|
|
27
|
-
}): string | Comment {
|
|
28
|
-
if (typeof document === "undefined") {
|
|
29
|
-
const buf = getContext(HeadKey);
|
|
30
|
-
if (buf) {
|
|
31
|
-
const nonce = getContext(NonceKey);
|
|
32
|
-
if (nonce) {
|
|
33
|
-
const withNonce = String(children)
|
|
34
|
-
.replace(/<script /g, `<script nonce="${nonce}" `)
|
|
35
|
-
.replace(/<style /g, `<style nonce="${nonce}" `);
|
|
36
|
-
buf.push(withNonce);
|
|
37
|
-
} else {
|
|
38
|
-
buf.push(children as string);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return "";
|
|
42
|
-
}
|
|
43
|
-
if (children instanceof Node) {
|
|
44
|
-
document.head.appendChild(children);
|
|
45
|
-
}
|
|
46
|
-
return document.createComment("head");
|
|
47
|
-
}
|
|
1
|
+
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
2
|
+
|
|
3
|
+
const HeadKey = createContext<string[]>();
|
|
4
|
+
const NonceKey = createContext<string>();
|
|
5
|
+
|
|
6
|
+
export function initHead(nonce?: string): void {
|
|
7
|
+
setContext(HeadKey, []);
|
|
8
|
+
if (nonce) setContext(NonceKey, nonce);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function setHeadNonce(nonce: string): void {
|
|
12
|
+
setContext(NonceKey, nonce);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getHeadNonce(): string {
|
|
16
|
+
return getContext(NonceKey) ?? "";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function collectHead(): string {
|
|
20
|
+
return (getContext(HeadKey) ?? []).join("\n ");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function Head({
|
|
24
|
+
children,
|
|
25
|
+
}: {
|
|
26
|
+
children: string | Node;
|
|
27
|
+
}): string | Comment {
|
|
28
|
+
if (typeof document === "undefined") {
|
|
29
|
+
const buf = getContext(HeadKey);
|
|
30
|
+
if (buf) {
|
|
31
|
+
const nonce = getContext(NonceKey);
|
|
32
|
+
if (nonce) {
|
|
33
|
+
const withNonce = String(children)
|
|
34
|
+
.replace(/<script /g, `<script nonce="${nonce}" `)
|
|
35
|
+
.replace(/<style /g, `<style nonce="${nonce}" `);
|
|
36
|
+
buf.push(withNonce);
|
|
37
|
+
} else {
|
|
38
|
+
buf.push(children as string);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
if (children instanceof Node) {
|
|
44
|
+
document.head.appendChild(children);
|
|
45
|
+
}
|
|
46
|
+
return document.createComment("head");
|
|
47
|
+
}
|
package/src/rendering/hydrate.ts
CHANGED
|
@@ -24,96 +24,115 @@ import { Head } from "./head";
|
|
|
24
24
|
(globalThis as any).__grimoire_onNavigate__ = onNavigate;
|
|
25
25
|
(globalThis as any).__grimoire_afterNavigate__ = afterNavigate;
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
async function resolveInitialPage(pattern: string): Promise<any> {
|
|
28
|
+
const entry = routes[pattern];
|
|
29
|
+
if (!entry) return null;
|
|
30
|
+
if (typeof entry === "function") return entry;
|
|
31
|
+
return (await entry.__lazy()).default;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function hydrate(): Promise<(() => void) | undefined> {
|
|
35
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
36
|
+
if (!stateEl) return undefined;
|
|
29
37
|
|
|
30
|
-
if (stateEl) {
|
|
31
38
|
const state = JSON.parse(stateEl.textContent!);
|
|
32
|
-
const Page =
|
|
33
|
-
if (Page)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const matchedLayouts = layouts
|
|
37
|
-
.filter(
|
|
38
|
-
(l: any) =>
|
|
39
|
-
l.path === "/" ||
|
|
40
|
-
state.pattern === l.path ||
|
|
41
|
-
state.pattern.startsWith(`${l.path}/`),
|
|
42
|
-
)
|
|
43
|
-
.sort((a: any, b: any) => a.path.length - b.path.length);
|
|
39
|
+
const Page = await resolveInitialPage(state.pattern);
|
|
40
|
+
if (!Page) return undefined;
|
|
41
|
+
const slot = document.getElementById("grimoire-root");
|
|
42
|
+
if (!slot) return undefined;
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
44
|
+
const matchedLayouts = layouts
|
|
45
|
+
.filter(
|
|
46
|
+
(l: any) =>
|
|
47
|
+
l.path === "/" ||
|
|
48
|
+
state.pattern === l.path ||
|
|
49
|
+
state.pattern.startsWith(`${l.path}/`),
|
|
50
|
+
)
|
|
51
|
+
.sort((a: any, b: any) => a.path.length - b.path.length);
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
// The page's SSR container is findable by id, so the page can hydrate
|
|
54
|
+
// in place inside it while a single root layout claims from the
|
|
55
|
+
// top-level pool. With nested layouts (>1) the inner layout's DOM
|
|
56
|
+
// position can't be located in the flat pool — fall back to clearing
|
|
57
|
+
// the SSR content and re-rendering in DOM mode (empty pool).
|
|
58
|
+
const pageHost = document.getElementById("grimoire-page");
|
|
59
|
+
const hasLayouts = matchedLayouts.length > 0;
|
|
60
|
+
const inPlace = !hasLayouts || (matchedLayouts.length === 1 && !!pageHost);
|
|
61
|
+
if (!inPlace) {
|
|
62
|
+
slot.replaceChildren();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const ssrClones = inPlace
|
|
66
|
+
? Array.from(slot.childNodes).map((n) => n.cloneNode(true))
|
|
67
|
+
: [];
|
|
68
|
+
pushHydrationNodes(inPlace ? (Array.from(slot.childNodes) as ChildNode[]) : []);
|
|
69
|
+
let initialDispose: (() => void) | undefined;
|
|
70
|
+
try {
|
|
71
|
+
initialDispose = withEffectScope(() => {
|
|
59
72
|
try {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
73
|
+
let renderFn = () => {
|
|
74
|
+
const pageDiv =
|
|
75
|
+
hasLayouts && inPlace && pageHost
|
|
76
|
+
? pageHost
|
|
77
|
+
: claim(getHydrationNodes(), "div");
|
|
78
|
+
pageDiv.id = "grimoire-page";
|
|
65
79
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
if (pageDiv.childNodes.length > 0) {
|
|
81
|
+
pushHydrationNodes(Array.from(pageDiv.childNodes) as ChildNode[]);
|
|
82
|
+
let pageNode: unknown;
|
|
83
|
+
try {
|
|
84
|
+
pageNode = Page({
|
|
85
|
+
data: state.data,
|
|
86
|
+
params: state.params,
|
|
87
|
+
});
|
|
88
|
+
} finally {
|
|
89
|
+
// always rebalance the global hydration stack — a throwing
|
|
90
|
+
// page component must not corrupt claims for the whole session
|
|
91
|
+
popHydrationNodes();
|
|
92
|
+
}
|
|
93
|
+
insert(pageDiv, pageNode);
|
|
94
|
+
} else {
|
|
95
|
+
const pageNode = Page({
|
|
96
|
+
data: state.data,
|
|
97
|
+
params: state.params,
|
|
98
|
+
});
|
|
99
|
+
insert(pageDiv, pageNode);
|
|
100
|
+
}
|
|
83
101
|
|
|
84
|
-
|
|
85
|
-
|
|
102
|
+
return pageDiv;
|
|
103
|
+
};
|
|
86
104
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
105
|
+
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
106
|
+
const LayoutComponent = matchedLayouts[i].component;
|
|
107
|
+
const innerRender = renderFn;
|
|
108
|
+
const layoutData = state.layoutData?.[i];
|
|
109
|
+
renderFn = () => {
|
|
110
|
+
// Pre-render inner content so children is a Node (insert() doesn't call functions)
|
|
111
|
+
const childNode = innerRender();
|
|
112
|
+
return LayoutComponent({
|
|
113
|
+
data: layoutData,
|
|
114
|
+
params: state.params,
|
|
115
|
+
children: childNode,
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
}
|
|
101
119
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
} catch (e) {
|
|
109
|
-
console.warn("[grimoire] hydration error:", e);
|
|
120
|
+
const rootNode = renderFn();
|
|
121
|
+
insert(slot, rootNode);
|
|
122
|
+
} finally {
|
|
123
|
+
popHydrationNodes();
|
|
110
124
|
}
|
|
125
|
+
});
|
|
126
|
+
} catch (e) {
|
|
127
|
+
console.warn("[grimoire] hydration error:", e);
|
|
128
|
+
}
|
|
111
129
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
}
|
|
130
|
+
if (!slot.hasChildNodes() && ssrClones.length > 0) {
|
|
131
|
+
slot.replaceChildren(...ssrClones);
|
|
116
132
|
}
|
|
133
|
+
return initialDispose;
|
|
117
134
|
}
|
|
118
135
|
|
|
119
|
-
|
|
136
|
+
hydrate().then((initialDispose) => {
|
|
137
|
+
initRouter(routes, layouts, initialDispose);
|
|
138
|
+
});
|