@teyik0/furin 0.1.0-alpha.8 → 0.2.0-alpha.1
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/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/adapter/bun.d.ts +2 -2
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/client.d.ts +2 -2
- package/dist/build/compile-entry.d.ts +2 -16
- package/dist/build/compile-entry.ts +109 -0
- package/dist/build/entry-template.d.ts +24 -0
- package/dist/build/entry-template.ts +156 -0
- package/dist/build/hydrate.d.ts +3 -3
- package/dist/build/index.d.ts +1 -0
- package/dist/build/index.js +4734 -548
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/server-routes-entry.d.ts +2 -12
- package/dist/build/server-routes-entry.ts +36 -0
- package/dist/build/shared.d.ts +1 -1
- package/dist/build/ssg-cache.d.ts +4 -0
- package/dist/build/types.d.ts +5 -1
- package/dist/cli/index.js +4713 -549
- package/dist/client/boundaries.d.ts +117 -0
- package/dist/client/default-screens.d.ts +17 -0
- package/dist/client/link.d.ts +22 -0
- package/dist/client/link.js +3450 -0
- package/dist/client/router/boundary-tree.d.ts +26 -0
- package/dist/client/router/context.d.ts +8 -0
- package/dist/client/router/history.d.ts +9 -0
- package/dist/client/router/index.d.ts +7 -0
- package/dist/client/router/link-utils.d.ts +51 -0
- package/dist/client/router/provider.d.ts +4 -0
- package/dist/client/router/spa-response.d.ts +58 -0
- package/dist/{link.d.ts → client/router/types.d.ts} +114 -80
- package/dist/client/server-error.d.ts +12 -0
- package/dist/client.d.ts +54 -4
- package/dist/client.js +77 -1
- package/dist/config.d.ts +12 -2
- package/dist/config.js +1 -0
- package/dist/env.d.ts +50 -0
- package/dist/furin.d.ts +37 -2
- package/dist/furin.js +5288 -601
- package/dist/plugin/index.js +140 -48
- package/dist/plugin/transform-client.d.ts +2 -1
- package/dist/server/auto-invalidate/index.d.ts +4 -0
- package/dist/server/auto-invalidate/plugin.d.ts +38 -0
- package/dist/server/auto-invalidate/registry.d.ts +9 -0
- package/dist/server/auto-invalidate/runtime.d.ts +9 -0
- package/dist/server/auto-invalidate/types.d.ts +12 -0
- package/dist/server/cache/dev-loader.d.ts +28 -0
- package/dist/server/cache/index.d.ts +10 -0
- package/dist/server/cache/invalidation.d.ts +12 -0
- package/dist/server/cache/isr-ssg.d.ts +26 -0
- package/dist/server/cache/isr.d.ts +6 -0
- package/dist/server/cache/registry.d.ts +5 -0
- package/dist/server/cache/route-cache.d.ts +26 -0
- package/dist/server/cache/ssg.d.ts +5 -0
- package/dist/server/context-logger.d.ts +28 -0
- package/dist/server/dev-inspector.d.ts +89 -0
- package/dist/{internal.d.ts → server/internal.d.ts} +16 -0
- package/dist/server/internal.js +17 -0
- package/dist/server/lang-detect.d.ts +5 -0
- package/dist/server/render/assemble.d.ts +47 -0
- package/dist/server/render/element.d.ts +24 -0
- package/dist/server/render/index.d.ts +7 -0
- package/dist/server/render/index.ts +22 -0
- package/dist/server/render/isr.d.ts +3 -0
- package/dist/server/render/loaders.d.ts +64 -0
- package/dist/server/render/not-found.d.ts +6 -0
- package/dist/{render → server/render}/shell.d.ts +17 -4
- package/{src → dist/server}/render/shell.ts +31 -9
- package/dist/server/render/ssg.d.ts +8 -0
- package/dist/server/render/ssr.d.ts +107 -0
- package/dist/server/router/discovery.d.ts +16 -0
- package/dist/server/router/hmr.d.ts +64 -0
- package/dist/server/router/index.d.ts +6 -0
- package/dist/server/router/index.js +28 -0
- package/dist/server/router/index.ts +29 -0
- package/dist/server/router/patterns.d.ts +40 -0
- package/dist/server/router/plugin.d.ts +20 -0
- package/dist/server/router/schemas.d.ts +51 -0
- package/dist/server/router/types.d.ts +60 -0
- package/dist/server/runtime-env.js +10 -0
- package/dist/shared/await.d.ts +67 -0
- package/dist/shared/deferred-ndjson.d.ts +32 -0
- package/dist/shared/digest.d.ts +16 -0
- package/dist/shared/error.d.ts +37 -0
- package/dist/shared/not-found.d.ts +19 -0
- package/dist/shared/utils/ast-walk.d.ts +18 -0
- package/dist/shared/utils/index.d.ts +13 -0
- package/package.json +56 -25
- package/src/adapter/bun.ts +90 -41
- package/src/adapter/static.ts +83 -36
- package/src/build/client.ts +8 -12
- package/src/build/compile-entry.ts +20 -14
- package/src/build/entry-template.ts +90 -8
- package/src/build/hydrate.ts +213 -36
- package/src/build/index.ts +10 -9
- package/src/build/route-types.ts +30 -4
- package/src/build/scan-server.ts +52 -67
- package/src/build/server-routes-entry.ts +7 -11
- package/src/build/shared.ts +1 -1
- package/src/build/ssg-cache.ts +31 -0
- package/src/build/types.ts +5 -1
- package/src/cli/index.ts +1 -0
- package/src/client/boundaries.tsx +259 -0
- package/src/client/default-screens.tsx +304 -0
- package/src/client/link.tsx +338 -0
- package/src/client/router/boundary-tree.tsx +116 -0
- package/src/client/router/context.ts +38 -0
- package/src/client/router/history.ts +41 -0
- package/src/client/router/index.ts +47 -0
- package/src/client/router/link-utils.ts +180 -0
- package/src/client/router/provider.tsx +723 -0
- package/src/client/router/spa-response.ts +143 -0
- package/src/client/router/types.ts +263 -0
- package/src/client/server-error.ts +47 -0
- package/src/client.ts +83 -3
- package/src/config.ts +11 -3
- package/src/env.d.ts +1 -0
- package/src/furin.ts +104 -26
- package/src/plugin/index.ts +7 -8
- package/src/plugin/transform-client.ts +146 -120
- package/src/server/auto-invalidate/index.ts +10 -0
- package/src/server/auto-invalidate/plugin.ts +25 -0
- package/src/server/auto-invalidate/registry.ts +60 -0
- package/src/server/auto-invalidate/runtime.ts +133 -0
- package/src/server/auto-invalidate/types.ts +16 -0
- package/src/server/cache/dev-loader.ts +211 -0
- package/src/server/cache/index.ts +34 -0
- package/src/server/cache/invalidation.ts +111 -0
- package/src/server/cache/isr-ssg.ts +41 -0
- package/src/server/cache/isr.ts +17 -0
- package/src/server/cache/registry.ts +35 -0
- package/src/server/cache/route-cache.ts +124 -0
- package/src/server/cache/ssg.ts +15 -0
- package/src/server/context-logger.ts +76 -0
- package/src/server/dev-inspector.ts +60 -0
- package/src/{dev-page-plugin.ts → server/dev-page-plugin.ts} +15 -0
- package/src/{internal.ts → server/internal.ts} +17 -0
- package/src/server/lang-detect.ts +46 -0
- package/src/server/render/assemble.ts +133 -0
- package/src/server/render/element.tsx +113 -0
- package/src/server/render/index.ts +22 -0
- package/src/server/render/isr.ts +295 -0
- package/src/server/render/loaders.ts +328 -0
- package/src/server/render/not-found.ts +96 -0
- package/src/server/render/shell.ts +202 -0
- package/src/server/render/ssg.ts +137 -0
- package/src/server/render/ssr.ts +545 -0
- package/src/server/router/discovery.ts +474 -0
- package/src/server/router/hmr.ts +372 -0
- package/src/server/router/index.ts +29 -0
- package/src/server/router/patterns.ts +214 -0
- package/src/server/router/plugin.ts +401 -0
- package/src/server/router/schemas.ts +163 -0
- package/src/server/router/types.ts +59 -0
- package/src/shared/await.tsx +192 -0
- package/src/shared/deferred-ndjson.ts +244 -0
- package/src/shared/digest.ts +71 -0
- package/src/shared/error.ts +49 -0
- package/src/shared/not-found.ts +40 -0
- package/src/shared/utils/ast-walk.ts +51 -0
- package/src/{utils.ts → shared/utils/index.ts} +32 -1
- package/dist/link.js +0 -1173
- package/dist/render/assemble.d.ts +0 -13
- package/dist/render/cache.d.ts +0 -95
- package/dist/render/element.d.ts +0 -4
- package/dist/render/index.d.ts +0 -27
- package/dist/render/loaders.d.ts +0 -12
- package/dist/router.d.ts +0 -34
- package/dist/router.js +0 -830
- package/dist/utils.d.ts +0 -6
- package/src/link.tsx +0 -1035
- package/src/render/assemble.ts +0 -64
- package/src/render/cache.ts +0 -241
- package/src/render/element.tsx +0 -28
- package/src/render/index.ts +0 -479
- package/src/render/loaders.ts +0 -113
- package/src/router.ts +0 -400
- /package/dist/{dev-page-plugin.d.ts → server/dev-page-plugin.d.ts} +0 -0
- /package/dist/{render → server/render}/template.d.ts +0 -0
- /package/dist/{runtime-env.d.ts → server/runtime-env.d.ts} +0 -0
- /package/src/{render → server/render}/template.ts +0 -0
- /package/src/{runtime-env.ts → server/runtime-env.ts} +0 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
export type RevalidateType = "page" | "layout";
|
|
2
|
+
|
|
3
|
+
export interface CacheInvalidationResult {
|
|
4
|
+
deleted: boolean;
|
|
5
|
+
purgedPaths: string[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface Cache<Entry> {
|
|
9
|
+
clear(): void;
|
|
10
|
+
delete(key: string): boolean;
|
|
11
|
+
entries(): IterableIterator<[string, Entry]>;
|
|
12
|
+
get(key: string): Entry | undefined;
|
|
13
|
+
has(key: string): boolean;
|
|
14
|
+
invalidatePath(path: string, type: RevalidateType): CacheInvalidationResult;
|
|
15
|
+
keys(): IterableIterator<string>;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
set(key: string, entry: Entry): void;
|
|
18
|
+
get size(): number;
|
|
19
|
+
readonly store: Map<string, Entry>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RouteCacheOptions<Entry> {
|
|
23
|
+
maxSize?: number;
|
|
24
|
+
name: string;
|
|
25
|
+
onDelete?: (key: string, entry: Entry) => void;
|
|
26
|
+
onSet?: (key: string, entry: Entry, previous: Entry | undefined) => void;
|
|
27
|
+
pathFromKey?: (key: string) => string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function defaultPathFromKey(key: string): string {
|
|
31
|
+
return key;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function matchesPath(urlPath: string, path: string, type: RevalidateType): boolean {
|
|
35
|
+
if (type === "page") {
|
|
36
|
+
return urlPath === path;
|
|
37
|
+
}
|
|
38
|
+
const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
|
|
39
|
+
return urlPath === path || urlPath.startsWith(prefix);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createRouteCache<Entry>(options: RouteCacheOptions<Entry>): Cache<Entry> {
|
|
43
|
+
const store = new Map<string, Entry>();
|
|
44
|
+
const pathFromKey = options.pathFromKey ?? defaultPathFromKey;
|
|
45
|
+
|
|
46
|
+
const evictOldest = (): void => {
|
|
47
|
+
if (options.maxSize === undefined || store.size <= options.maxSize) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const oldest = store.keys().next().value;
|
|
51
|
+
if (oldest !== undefined) {
|
|
52
|
+
deleteEntry(oldest);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const deleteEntry = (key: string): boolean => {
|
|
57
|
+
const entry = store.get(key);
|
|
58
|
+
if (entry === undefined) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
store.delete(key);
|
|
62
|
+
options.onDelete?.(key, entry);
|
|
63
|
+
return true;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
name: options.name,
|
|
68
|
+
store,
|
|
69
|
+
get size() {
|
|
70
|
+
return store.size;
|
|
71
|
+
},
|
|
72
|
+
get(key) {
|
|
73
|
+
const entry = store.get(key);
|
|
74
|
+
if (entry !== undefined && options.maxSize !== undefined) {
|
|
75
|
+
store.delete(key);
|
|
76
|
+
store.set(key, entry);
|
|
77
|
+
}
|
|
78
|
+
return entry;
|
|
79
|
+
},
|
|
80
|
+
set(key, entry) {
|
|
81
|
+
const previous = store.get(key);
|
|
82
|
+
if (previous !== undefined) {
|
|
83
|
+
store.delete(key);
|
|
84
|
+
}
|
|
85
|
+
store.set(key, entry);
|
|
86
|
+
options.onSet?.(key, entry, previous);
|
|
87
|
+
evictOldest();
|
|
88
|
+
},
|
|
89
|
+
delete(key) {
|
|
90
|
+
return deleteEntry(key);
|
|
91
|
+
},
|
|
92
|
+
clear() {
|
|
93
|
+
for (const key of [...store.keys()]) {
|
|
94
|
+
deleteEntry(key);
|
|
95
|
+
}
|
|
96
|
+
store.clear();
|
|
97
|
+
},
|
|
98
|
+
has(key) {
|
|
99
|
+
return store.has(key);
|
|
100
|
+
},
|
|
101
|
+
entries() {
|
|
102
|
+
return store.entries();
|
|
103
|
+
},
|
|
104
|
+
keys() {
|
|
105
|
+
return store.keys();
|
|
106
|
+
},
|
|
107
|
+
invalidatePath(path, type) {
|
|
108
|
+
let deleted = false;
|
|
109
|
+
const purgedPaths: string[] = [];
|
|
110
|
+
|
|
111
|
+
for (const key of [...store.keys()]) {
|
|
112
|
+
const urlPath = pathFromKey(key);
|
|
113
|
+
if (urlPath === null || !matchesPath(urlPath, path, type)) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
deleteEntry(key);
|
|
117
|
+
deleted = true;
|
|
118
|
+
purgedPaths.push(urlPath);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return { deleted, purgedPaths: [...new Set(purgedPaths)] };
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { autoInvalidateRegistry } from "../auto-invalidate/registry";
|
|
2
|
+
import { createHtmlRouteCache, type SsgCacheEntry } from "./isr-ssg";
|
|
3
|
+
|
|
4
|
+
export const ssgRouteCache = createHtmlRouteCache<SsgCacheEntry>("ssg");
|
|
5
|
+
|
|
6
|
+
export const ssgCache = ssgRouteCache.store;
|
|
7
|
+
|
|
8
|
+
export function getSSGCache(key: string): SsgCacheEntry | undefined {
|
|
9
|
+
return ssgRouteCache.get(key);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function setSSGCache(key: string, entry: SsgCacheEntry): void {
|
|
13
|
+
ssgRouteCache.set(key, entry);
|
|
14
|
+
autoInvalidateRegistry.registerLoaderTags(key, entry.tags);
|
|
15
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import type { RequestLogger } from "evlog";
|
|
3
|
+
// biome-ignore lint/style/noExportedImports: used locally and re-exported for consumers
|
|
4
|
+
import { createLogger } from "evlog";
|
|
5
|
+
import { useLogger as _evlogUseLogger } from "evlog/elysia";
|
|
6
|
+
|
|
7
|
+
export { createLogger };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Fallback used when useLogger() is called completely outside any context
|
|
11
|
+
* (not in a live request, not in a synthetic render scope).
|
|
12
|
+
*/
|
|
13
|
+
const noopLogger: RequestLogger = {
|
|
14
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
15
|
+
set: () => {},
|
|
16
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
17
|
+
setLevel: () => {},
|
|
18
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
19
|
+
error: () => {},
|
|
20
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
21
|
+
info: () => {},
|
|
22
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
23
|
+
warn: () => {},
|
|
24
|
+
emit: () => null,
|
|
25
|
+
getContext: () => ({}),
|
|
26
|
+
fork: (_label, fn) => fn() as undefined,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const syntheticRenderStorage = new AsyncLocalStorage<RequestLogger>();
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns the current request-scoped logger.
|
|
33
|
+
*
|
|
34
|
+
* Priority:
|
|
35
|
+
* 1. Live Elysia request handled by the evlog() plugin → full request-scoped wide event
|
|
36
|
+
* 2. Synthetic render scope (ISR revalidation, SSG pre-render) → detached createLogger()
|
|
37
|
+
* that still drains to the configured adapter (Datadog, Axiom, etc.)
|
|
38
|
+
* 3. Completely outside any context → no-op
|
|
39
|
+
*
|
|
40
|
+
* Import from `@teyik0/furin` instead of `evlog/elysia` so this fallback chain
|
|
41
|
+
* applies in all rendering contexts.
|
|
42
|
+
*/
|
|
43
|
+
export function useLogger(): RequestLogger {
|
|
44
|
+
try {
|
|
45
|
+
return _evlogUseLogger();
|
|
46
|
+
} catch {
|
|
47
|
+
return syntheticRenderStorage.getStore() ?? noopLogger;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Runs `fn` inside a synthetic render scope.
|
|
53
|
+
*
|
|
54
|
+
* Creates a detached `createLogger()` instance for the duration of `fn`.
|
|
55
|
+
* `useLogger()` calls inside `fn` (including user loaders) return this logger
|
|
56
|
+
* instead of throwing. On completion, the accumulated wide event is emitted to
|
|
57
|
+
* the global drain with the provided initial context (e.g. route pattern).
|
|
58
|
+
*
|
|
59
|
+
* Used by renderForPath() which drives both ISR background revalidation and
|
|
60
|
+
* SSG pre-renders — neither has a live Elysia request context.
|
|
61
|
+
*/
|
|
62
|
+
export async function runInSyntheticRenderScope<T>(
|
|
63
|
+
fn: () => Promise<T> | T,
|
|
64
|
+
initialContext: Record<string, unknown>
|
|
65
|
+
): Promise<T> {
|
|
66
|
+
const logger = createLogger(initialContext);
|
|
67
|
+
try {
|
|
68
|
+
const result = await syntheticRenderStorage.run(logger, () => Promise.resolve(fn()));
|
|
69
|
+
logger.emit();
|
|
70
|
+
return result;
|
|
71
|
+
} catch (err) {
|
|
72
|
+
logger.error(err instanceof Error ? err : new Error(String(err)));
|
|
73
|
+
logger.emit();
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Elysia } from "elysia";
|
|
2
|
+
import {
|
|
3
|
+
type DevLoaderCacheEntry,
|
|
4
|
+
getAllDevISRLoaderEntries,
|
|
5
|
+
getAllDevSSGLoaderEntries,
|
|
6
|
+
isDevLoaderCacheFresh,
|
|
7
|
+
} from "./cache/dev-loader.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Dev "Live ISR" inspector.
|
|
11
|
+
*
|
|
12
|
+
* Mounts read-only JSON endpoints that surface the active entries of the
|
|
13
|
+
* dev-mode loader caches. Designed to back a future browser DevTools panel:
|
|
14
|
+
* the JSON shape is stable and carries enough information for a UI to render
|
|
15
|
+
* a freshness-aware entry list with dependencies and a data preview.
|
|
16
|
+
*
|
|
17
|
+
* The plugin always registers the routes — caller is responsible for gating
|
|
18
|
+
* on `IS_DEV` to keep them out of production builds.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Public response shape — keep stable for downstream UI consumers. */
|
|
22
|
+
export interface InspectorEntry {
|
|
23
|
+
/** Per-entry preview of the cached loader output. */
|
|
24
|
+
dataPreview: Record<string, unknown>;
|
|
25
|
+
/** Source files whose change drops this entry. */
|
|
26
|
+
dependencies: string[];
|
|
27
|
+
/** ms timestamp at which the entry was written. */
|
|
28
|
+
generatedAt: number;
|
|
29
|
+
/** True iff the entry is still within its `revalidate` window. */
|
|
30
|
+
isFresh: boolean;
|
|
31
|
+
/** Resolved cache key (the same path used by `revalidatePath`). */
|
|
32
|
+
key: string;
|
|
33
|
+
/** Cache origin — surfaced verbatim so a UI can group by mode. */
|
|
34
|
+
mode: "isr" | "ssg";
|
|
35
|
+
/** Revalidate window in seconds. SSG entries report `-1` for "indefinite". */
|
|
36
|
+
revalidate: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function projectEntry(key: string, entry: DevLoaderCacheEntry): InspectorEntry {
|
|
40
|
+
const revalidate = Number.isFinite(entry.revalidate) ? entry.revalidate : -1;
|
|
41
|
+
return {
|
|
42
|
+
dataPreview: entry.loaderData,
|
|
43
|
+
dependencies: entry.dependencies,
|
|
44
|
+
generatedAt: entry.generatedAt,
|
|
45
|
+
isFresh: isDevLoaderCacheFresh(entry),
|
|
46
|
+
key,
|
|
47
|
+
mode: entry.mode,
|
|
48
|
+
revalidate,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createDevInspectorPlugin() {
|
|
53
|
+
return new Elysia({ name: "furin-dev-inspector" })
|
|
54
|
+
.get("/__furin/_inspect/isr", () =>
|
|
55
|
+
getAllDevISRLoaderEntries().map(([key, entry]) => projectEntry(key, entry))
|
|
56
|
+
)
|
|
57
|
+
.get("/__furin/_inspect/ssg", () =>
|
|
58
|
+
getAllDevSSGLoaderEntries().map(([key, entry]) => projectEntry(key, entry))
|
|
59
|
+
);
|
|
60
|
+
}
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
|
|
54
54
|
import { dirname, resolve } from "node:path";
|
|
55
55
|
import { fileURLToPath } from "node:url";
|
|
56
|
+
import { invalidateDevLoaderCacheBySource } from "./cache/dev-loader.ts";
|
|
56
57
|
|
|
57
58
|
// Matches ?furin-server with an optional &t=<ms> cache-buster.
|
|
58
59
|
const FURIN_SERVER_FILTER = /\?furin-server(?:&t=\d+)?$/;
|
|
@@ -63,6 +64,8 @@ const T_PARAM_RE = /&t=(\d+)/;
|
|
|
63
64
|
const STRIP_FURIN_SERVER_RE = /\?furin-server.*$/;
|
|
64
65
|
const STRIP_T_PARAM_RE = /\?t=\d+$/;
|
|
65
66
|
|
|
67
|
+
let _pluginRegistered = false;
|
|
68
|
+
|
|
66
69
|
type SourceLoader = "js" | "jsx" | "ts" | "tsx";
|
|
67
70
|
|
|
68
71
|
// ── Singleton package resolution ───────────────────────────────────────────────
|
|
@@ -116,6 +119,7 @@ function escapeRegExp(s: string): string {
|
|
|
116
119
|
export function rewriteSingletonImports(source: string): string {
|
|
117
120
|
let result = source;
|
|
118
121
|
for (const [pkg, absPath] of SINGLETON_PATHS) {
|
|
122
|
+
// react-doctor-disable-next-line react-doctor/js-hoist-regexp
|
|
119
123
|
const re = new RegExp(`((?:from|import(?:\\s+type)?)\\s+)["']${escapeRegExp(pkg)}["']`, "g");
|
|
120
124
|
result = result.replace(re, (_, g1: string) => `${g1}"${absPath}"`);
|
|
121
125
|
}
|
|
@@ -277,6 +281,7 @@ export function rewriteBareImports(source: string, transpiled: string, dir: stri
|
|
|
277
281
|
// that both import and re-export from the same package. The regex only
|
|
278
282
|
// matches when the specifier is preceded by `from` or `import` so string
|
|
279
283
|
// literals in non-import positions are left untouched.
|
|
284
|
+
// react-doctor-disable-next-line react-doctor/js-hoist-regexp
|
|
280
285
|
const re = new RegExp(`((?:from|import(?:\\s+type)?)\\s+)["']${escapeRegExp(spec)}["']`, "g");
|
|
281
286
|
result = result.replace(re, (_, g1: string) => `${g1}"${resolved}"`);
|
|
282
287
|
}
|
|
@@ -331,6 +336,10 @@ function transformDevSource(
|
|
|
331
336
|
}
|
|
332
337
|
|
|
333
338
|
export function registerDevPagePlugin(): void {
|
|
339
|
+
if (_pluginRegistered) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
_pluginRegistered = true;
|
|
334
343
|
Bun.plugin({
|
|
335
344
|
name: "furin-dev-page-loader",
|
|
336
345
|
setup(build) {
|
|
@@ -364,6 +373,12 @@ export function registerDevPagePlugin(): void {
|
|
|
364
373
|
throw new Error(`[furin] Unsupported source loader for ${args.path}`);
|
|
365
374
|
}
|
|
366
375
|
|
|
376
|
+
// Every re-evaluation of a workspace source in dev is either the
|
|
377
|
+
// initial load (cache empty → no-op) or a post-edit HMR re-read.
|
|
378
|
+
// Both are correct moments to drop dependent dev loader cache
|
|
379
|
+
// entries — they will be re-populated on the next request.
|
|
380
|
+
invalidateDevLoaderCacheBySource(args.path);
|
|
381
|
+
|
|
367
382
|
if (shouldSkipWorkspaceTransform(args.path)) {
|
|
368
383
|
return {
|
|
369
384
|
contents: raw,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { SsgCacheEntry } from "./cache/index.ts";
|
|
2
|
+
|
|
1
3
|
// ── Compile-time context for compiled binaries ──────────────────────────────
|
|
2
4
|
// The generated compile entry calls `__setCompileContext()` before importing
|
|
3
5
|
// server.ts. At runtime, `router.ts` and `furin.ts` use `getCompileContext()`
|
|
@@ -18,8 +20,23 @@ export interface CompileContext {
|
|
|
18
20
|
buildId?: string;
|
|
19
21
|
embedded?: EmbeddedAppData;
|
|
20
22
|
modules: Record<string, unknown>;
|
|
23
|
+
/** Root-level conventions discovered at compile time. */
|
|
24
|
+
rootConventions?: { errorPath?: string; notFoundPath?: string };
|
|
21
25
|
rootPath: string;
|
|
26
|
+
/** Per-route metadata including pre-computed segment boundaries. */
|
|
27
|
+
routeMetadata?: Record<
|
|
28
|
+
string,
|
|
29
|
+
{
|
|
30
|
+
segmentBoundaries: Array<{
|
|
31
|
+
depth: number;
|
|
32
|
+
path: string;
|
|
33
|
+
errorPath?: string;
|
|
34
|
+
notFoundPath?: string;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
>;
|
|
22
38
|
routes: CompileContextRoute[];
|
|
39
|
+
ssgCache?: Record<string, SsgCacheEntry>;
|
|
23
40
|
}
|
|
24
41
|
|
|
25
42
|
let _compileCtx: CompileContext | null = null;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { SourceLang } from "yuku-parser";
|
|
2
|
+
|
|
3
|
+
export function detectLangFromPath(filePath: string): SourceLang {
|
|
4
|
+
if (filePath.endsWith(".d.ts")) {
|
|
5
|
+
return "dts";
|
|
6
|
+
}
|
|
7
|
+
const ext = filePath.split(".").pop()?.toLowerCase();
|
|
8
|
+
switch (ext) {
|
|
9
|
+
case "ts":
|
|
10
|
+
return "ts";
|
|
11
|
+
case "tsx":
|
|
12
|
+
return "tsx";
|
|
13
|
+
case "jsx":
|
|
14
|
+
return "jsx";
|
|
15
|
+
default:
|
|
16
|
+
return "js";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface MaybeWrappedNode {
|
|
21
|
+
expression?: unknown;
|
|
22
|
+
type: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const TS_WRAPPER_TYPES = new Set([
|
|
26
|
+
"TSAsExpression",
|
|
27
|
+
"TSSatisfiesExpression",
|
|
28
|
+
"TSTypeAssertion",
|
|
29
|
+
"TSNonNullExpression",
|
|
30
|
+
"TSInstantiationExpression",
|
|
31
|
+
"ParenthesizedExpression",
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
export function unwrapTSExpression<T extends { type: string }>(node: T): T {
|
|
35
|
+
let current: MaybeWrappedNode = node;
|
|
36
|
+
while (
|
|
37
|
+
current &&
|
|
38
|
+
typeof current === "object" &&
|
|
39
|
+
TS_WRAPPER_TYPES.has(current.type) &&
|
|
40
|
+
current.expression &&
|
|
41
|
+
typeof current.expression === "object"
|
|
42
|
+
) {
|
|
43
|
+
current = current.expression as MaybeWrappedNode;
|
|
44
|
+
}
|
|
45
|
+
return current as T;
|
|
46
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { toCrossJSON } from "seroval";
|
|
2
|
+
import type { buildHeadInjection } from "./shell";
|
|
3
|
+
import { safeJson } from "./shell";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Single source of truth for the global registry name used by the SSR
|
|
7
|
+
* streaming script, the client hydration entry, and any test helper. Keeping
|
|
8
|
+
* it centralised here avoids `<script>`/JS source drift if the name is ever
|
|
9
|
+
* renamed.
|
|
10
|
+
*/
|
|
11
|
+
export const FURIN_DEFERRED_GLOBAL = "__FURIN_DEFERRED__";
|
|
12
|
+
|
|
13
|
+
// ── Deferred streaming helpers ─────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generates the `window.__FURIN_DEFERRED__` registry script that is injected
|
|
17
|
+
* BEFORE the React stream starts. It sets up the resolve/reject/getPromise API
|
|
18
|
+
* for late chunks.
|
|
19
|
+
*
|
|
20
|
+
* Client-side `<Await>` components call `getPromise(key)` to obtain a Promise
|
|
21
|
+
* they can pass to React 19's `use()`. The late resolution scripts call
|
|
22
|
+
* `resolve(key, chunk)` / `reject(key, chunk)` to settle those Promises.
|
|
23
|
+
*
|
|
24
|
+
* Synchronous loader data is NOT carried here — it lives exclusively in the
|
|
25
|
+
* `__FURIN_DATA__` JSON script that the hydrate entry reads.
|
|
26
|
+
*
|
|
27
|
+
* CSP note: this is an inline `<script>` — nonce support is a v2 concern.
|
|
28
|
+
*/
|
|
29
|
+
export function buildDeferredScript(deferredKeys: string[]): string {
|
|
30
|
+
const keysJson = safeJson(deferredKeys);
|
|
31
|
+
return `<script id="${FURIN_DEFERRED_GLOBAL}">
|
|
32
|
+
window.${FURIN_DEFERRED_GLOBAL} = {
|
|
33
|
+
_deferredKeys: ${keysJson},
|
|
34
|
+
_chunks: {},
|
|
35
|
+
_resolvers: {},
|
|
36
|
+
resolve(key, chunk) {
|
|
37
|
+
this._chunks[key] = { a: 0, v: chunk };
|
|
38
|
+
},
|
|
39
|
+
reject(key, chunk) {
|
|
40
|
+
this._chunks[key] = { a: 1, v: chunk };
|
|
41
|
+
},
|
|
42
|
+
getPromise(key) {
|
|
43
|
+
if (!this._resolvers[key]) {
|
|
44
|
+
var res, rej;
|
|
45
|
+
var p = new Promise(function(resolve, reject) { res = resolve; rej = reject; });
|
|
46
|
+
this._resolvers[key] = { resolve: res, reject: rej, promise: p };
|
|
47
|
+
}
|
|
48
|
+
return this._resolvers[key].promise;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
</script>`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Generates a late `<script>` that settles one deferred Promise on the client.
|
|
56
|
+
* The `chunk` must be a seroval CrossJSON value produced by `toCrossJSON(value)`.
|
|
57
|
+
*
|
|
58
|
+
* @param key - The Promise key (matches what `getPromise(key)` was called with)
|
|
59
|
+
* @param chunk - The already-serialised CrossJSON value
|
|
60
|
+
* @param action - "resolve" or "reject"
|
|
61
|
+
*/
|
|
62
|
+
export function buildDeferredResolution(
|
|
63
|
+
key: string,
|
|
64
|
+
chunk: ReturnType<typeof toCrossJSON>,
|
|
65
|
+
action: "resolve" | "reject"
|
|
66
|
+
): string {
|
|
67
|
+
const chunkJson = safeJson(chunk);
|
|
68
|
+
return `<script>window.${FURIN_DEFERRED_GLOBAL}.${action}(${safeJson(key)},${chunkJson})</script>`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Minimal context passed to background / synthetic render helpers — only `request` is needed. */
|
|
72
|
+
export interface LoaderContext {
|
|
73
|
+
request: Request;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function resolvePath(pattern: string, params: Record<string, string>): string {
|
|
77
|
+
let path = pattern;
|
|
78
|
+
for (const [key, val] of Object.entries(params ?? {})) {
|
|
79
|
+
path = path.replace(key === "*" ? "*" : `:${key}`, val);
|
|
80
|
+
}
|
|
81
|
+
return path;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function streamToString(stream: ReadableStream): Promise<string> {
|
|
85
|
+
const reader = stream.getReader();
|
|
86
|
+
const decoder = new TextDecoder();
|
|
87
|
+
let html = "";
|
|
88
|
+
|
|
89
|
+
for (;;) {
|
|
90
|
+
const { done, value } = await reader.read();
|
|
91
|
+
if (done) {
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
html += decoder.decode(value, { stream: true });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
html += decoder.decode();
|
|
98
|
+
return html;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface SplitTemplate {
|
|
102
|
+
bodyPost: string;
|
|
103
|
+
bodyPre: string;
|
|
104
|
+
headPre: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function splitTemplate(template: string): SplitTemplate {
|
|
108
|
+
const [headPre, afterHead = ""] = template.split("<!--ssr-head-->");
|
|
109
|
+
const [bodyPre, bodyPost = ""] = afterHead.split("<!--ssr-outlet-->");
|
|
110
|
+
return { headPre, bodyPre, bodyPost } as SplitTemplate;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function assembleHTML(
|
|
114
|
+
template: string,
|
|
115
|
+
headData: ReturnType<typeof buildHeadInjection>,
|
|
116
|
+
reactHtml: string,
|
|
117
|
+
data: Record<string, unknown> | undefined
|
|
118
|
+
): string {
|
|
119
|
+
const { headPre, bodyPre, bodyPost } = splitTemplate(template);
|
|
120
|
+
|
|
121
|
+
const dataScript = data
|
|
122
|
+
? `<script id="__FURIN_DATA__" type="application/json">${safeJson(data)}</script>`
|
|
123
|
+
: "";
|
|
124
|
+
|
|
125
|
+
let injectedBodyPost = bodyPost;
|
|
126
|
+
if (dataScript) {
|
|
127
|
+
injectedBodyPost = bodyPost.includes("</body>")
|
|
128
|
+
? bodyPost.replace("</body>", `${dataScript}</body>`)
|
|
129
|
+
: bodyPost + dataScript;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return headPre + headData + bodyPre + reactHtml + injectedBodyPost;
|
|
133
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { wrapSegmentBoundaries } from "../../client/boundaries.tsx";
|
|
3
|
+
import { DefaultErrorFallback, DefaultNotFoundFallback } from "../../client/default-screens.tsx";
|
|
4
|
+
import type { RuntimeRoute } from "../../client.ts";
|
|
5
|
+
import type { ErrorComponent } from "../../shared/error.ts";
|
|
6
|
+
import type { FurinNotFoundError, NotFoundComponent } from "../../shared/not-found.ts";
|
|
7
|
+
import type { ResolvedRoute, SegmentBoundary } from "../router/index.ts";
|
|
8
|
+
import { IS_DEV } from "../runtime-env.ts";
|
|
9
|
+
|
|
10
|
+
export function buildElement(
|
|
11
|
+
route: ResolvedRoute,
|
|
12
|
+
data: Record<string, unknown>,
|
|
13
|
+
rootLayout: RuntimeRoute
|
|
14
|
+
): ReactNode {
|
|
15
|
+
const Component = route.page.component;
|
|
16
|
+
let element: ReactNode = <Component {...data} />;
|
|
17
|
+
|
|
18
|
+
// Index segmentBoundaries by depth for O(1) lookup during the wrap loop.
|
|
19
|
+
// Directory depth `d` maps 1:1 to routeChain[d] in Furin's model (routeChain
|
|
20
|
+
// is ordered shallow→deep, with index 0 being the root).
|
|
21
|
+
const byDepth = new Map<number, SegmentBoundary>();
|
|
22
|
+
// Defensive fallback: some legacy callers / tests construct a ResolvedRoute
|
|
23
|
+
// without the segmentBoundaries field.
|
|
24
|
+
for (const segment of route.segmentBoundaries ?? []) {
|
|
25
|
+
byDepth.set(segment.depth, segment);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Build inside-out. At each level we first wrap the accumulated subtree
|
|
29
|
+
// with the boundary declared at this depth (so the boundary sits INSIDE
|
|
30
|
+
// the layout at the same depth), THEN wrap with the layout itself.
|
|
31
|
+
for (let i = route.routeChain.length - 1; i >= 1; i--) {
|
|
32
|
+
element = wrapSegmentBoundaries(element, byDepth.get(i), undefined);
|
|
33
|
+
const routeEntry = route.routeChain[i];
|
|
34
|
+
if (routeEntry?.layout) {
|
|
35
|
+
const Layout = routeEntry.layout;
|
|
36
|
+
element = <Layout {...data}>{element}</Layout>;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Depth 0 = pagesDir itself = the root layout directory. Boundary wraps
|
|
41
|
+
// everything below the root layout; root layout wraps the boundary.
|
|
42
|
+
element = wrapSegmentBoundaries(element, byDepth.get(0), undefined);
|
|
43
|
+
|
|
44
|
+
if (rootLayout.layout) {
|
|
45
|
+
const RootLayoutComponent = rootLayout.layout;
|
|
46
|
+
element = <RootLayoutComponent {...data}>{element}</RootLayoutComponent>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return element;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function buildNotFoundElement(
|
|
53
|
+
component: NotFoundComponent | undefined,
|
|
54
|
+
error: FurinNotFoundError
|
|
55
|
+
): ReactNode {
|
|
56
|
+
const NotFound = component ?? DefaultNotFoundFallback;
|
|
57
|
+
return <NotFound error={{ message: error.message, data: error.data }} />;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function errorMessageOf(err: unknown): string {
|
|
61
|
+
if (err instanceof Error) {
|
|
62
|
+
return err.message;
|
|
63
|
+
}
|
|
64
|
+
if (typeof err === "string") {
|
|
65
|
+
return err;
|
|
66
|
+
}
|
|
67
|
+
return "";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const SERVER_RESET_NOOP = () => {
|
|
71
|
+
/* reset is a client-only action; the response is already committed here */
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const GENERIC_ERROR_MESSAGE = "An unexpected error occurred.";
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Builds the error element rendered when a loader (or the SSR shell) fails.
|
|
78
|
+
*
|
|
79
|
+
* @param component - User-declared `error.tsx` component, or `undefined` to
|
|
80
|
+
* fall back to the built-in `DefaultErrorScreen` with a generic message.
|
|
81
|
+
* @param error - The original thrown value. Kept for `errorMessageOf` lookup
|
|
82
|
+
* when no explicit `messageOverride` is provided (e.g. shell-render errors).
|
|
83
|
+
* @param digest - 10-hex-char digest correlating with server logs.
|
|
84
|
+
* @param messageOverride - Pre-extracted public message. Set by the loader
|
|
85
|
+
* pipeline when the thrown value is a `Response` (whose body has already
|
|
86
|
+
* been consumed in `runLoaders`); pass `undefined` to derive the message
|
|
87
|
+
* from `error` via `errorMessageOf`.
|
|
88
|
+
* @param status - HTTP status to surface in `ErrorProps.error.status`. The
|
|
89
|
+
* loader pipeline passes the thrown `Response.status` (default 500); the
|
|
90
|
+
* shell-error recovery path always passes 500.
|
|
91
|
+
*/
|
|
92
|
+
export function buildErrorElement(
|
|
93
|
+
component: ErrorComponent | undefined,
|
|
94
|
+
error: unknown,
|
|
95
|
+
digest: string,
|
|
96
|
+
messageOverride: string | undefined,
|
|
97
|
+
status: number
|
|
98
|
+
): ReactNode {
|
|
99
|
+
const ErrorView = component ?? DefaultErrorFallback;
|
|
100
|
+
let message: string;
|
|
101
|
+
if (component) {
|
|
102
|
+
message = messageOverride ?? errorMessageOf(error);
|
|
103
|
+
} else if (IS_DEV) {
|
|
104
|
+
// No user error.tsx: in dev, surface the real error message so the
|
|
105
|
+
// developer can see what actually broke instead of a generic placeholder.
|
|
106
|
+
// Production stays generic to avoid leaking internals — the digest still
|
|
107
|
+
// correlates the rendered page with the full server-side log entry.
|
|
108
|
+
message = (messageOverride ?? errorMessageOf(error)) || GENERIC_ERROR_MESSAGE;
|
|
109
|
+
} else {
|
|
110
|
+
message = GENERIC_ERROR_MESSAGE;
|
|
111
|
+
}
|
|
112
|
+
return <ErrorView error={{ message, digest, status }} reset={SERVER_RESET_NOOP} />;
|
|
113
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// biome-ignore-all lint/performance/noBarrelFile: intentional barrel for public API
|
|
2
|
+
|
|
3
|
+
export { type LoaderContext, streamToString } from "./assemble.ts";
|
|
4
|
+
export { buildElement, buildErrorElement } from "./element.tsx";
|
|
5
|
+
export { handleISR } from "./isr.ts";
|
|
6
|
+
export { type LoaderResult, runLoaders, serializeDeferredRejection } from "./loaders.ts";
|
|
7
|
+
export { renderRootNotFound } from "./not-found.ts";
|
|
8
|
+
|
|
9
|
+
export { prerenderSSG, warmSSGCache } from "./ssg.ts";
|
|
10
|
+
export {
|
|
11
|
+
assertDeferredModeAllowed,
|
|
12
|
+
type PreparedRender,
|
|
13
|
+
prepareRender,
|
|
14
|
+
type RenderResult,
|
|
15
|
+
renderForPath,
|
|
16
|
+
renderSSR,
|
|
17
|
+
renderToHTML,
|
|
18
|
+
renderToStream,
|
|
19
|
+
serializeLoaderDataNdjson,
|
|
20
|
+
splitBeforeBodyClose,
|
|
21
|
+
withSSRRouterContext,
|
|
22
|
+
} from "./ssr.ts";
|