@teyik0/furin 0.1.0-alpha.9 → 0.2.0-alpha.2
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/package.json +61 -25
- package/src/adapter/bun.ts +90 -41
- package/src/adapter/static.ts +100 -40
- package/src/build/client.ts +13 -14
- package/src/build/compile-entry.ts +20 -14
- package/src/build/entry-template.ts +90 -8
- package/src/build/hydrate.ts +219 -36
- package/src/build/index.ts +10 -9
- package/src/build/route-types.ts +65 -9
- package/src/build/scan-server.ts +52 -67
- package/src/build/server-routes-entry.ts +7 -11
- package/src/build/shared.ts +2 -1
- package/src/build/ssg-cache.ts +32 -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 +347 -0
- package/src/client/router/boundary-tree.tsx +116 -0
- package/src/client/router/context.ts +40 -0
- package/src/client/router/history.ts +41 -0
- package/src/client/router/index.ts +56 -0
- package/src/client/router/link-utils.ts +177 -0
- package/src/client/router/navigation.ts +61 -0
- package/src/client/router/provider.tsx +759 -0
- package/src/client/router/search/index.ts +99 -0
- package/src/client/router/search-store.ts +86 -0
- package/src/client/router/spa-response.ts +143 -0
- package/src/client/router/types.ts +271 -0
- package/src/client/server-error.ts +47 -0
- package/src/client.ts +80 -3
- package/src/config.ts +11 -3
- package/src/env.d.ts +1 -0
- package/src/furin.ts +114 -29
- 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/{context-logger.ts → server/context-logger.ts} +5 -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 +317 -0
- package/src/server/render/loaders.ts +328 -0
- package/src/server/render/not-found.ts +98 -0
- package/src/{render → server/render}/shell.ts +28 -13
- package/src/server/render/ssg.ts +148 -0
- package/src/server/render/ssr.ts +583 -0
- package/src/server/router/discovery.ts +474 -0
- package/src/server/router/hmr.ts +376 -0
- package/src/server/router/index.ts +28 -0
- package/src/server/router/patterns.ts +219 -0
- package/src/server/router/plugin.ts +384 -0
- package/src/server/router/schemas.ts +278 -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/search-params.ts +182 -0
- package/src/shared/utils/ast-walk.ts +51 -0
- package/src/{utils.ts → shared/utils/index.ts} +32 -1
- package/dist/adapter/bun.d.ts +0 -3
- package/dist/adapter/static.d.ts +0 -3
- package/dist/build/client.d.ts +0 -25
- package/dist/build/compile-entry.d.ts +0 -23
- package/dist/build/entry-template.d.ts +0 -14
- package/dist/build/hydrate.d.ts +0 -24
- package/dist/build/index.d.ts +0 -7
- package/dist/build/index.js +0 -3727
- package/dist/build/route-types.d.ts +0 -21
- package/dist/build/scan-server.d.ts +0 -8
- package/dist/build/server-routes-entry.d.ts +0 -23
- package/dist/build/shared.d.ts +0 -11
- package/dist/build/types.d.ts +0 -76
- package/dist/cli/config.d.ts +0 -9
- package/dist/cli/index.d.ts +0 -2
- package/dist/cli/index.js +0 -3762
- package/dist/client.d.ts +0 -195
- package/dist/client.js +0 -20
- package/dist/config.d.ts +0 -47
- package/dist/config.js +0 -28
- package/dist/context-logger.d.ts +0 -26
- package/dist/dev-page-plugin.d.ts +0 -109
- package/dist/furin.d.ts +0 -34
- package/dist/furin.js +0 -2744
- package/dist/internal.d.ts +0 -19
- package/dist/link.d.ts +0 -245
- package/dist/link.js +0 -1232
- package/dist/plugin/index.d.ts +0 -20
- package/dist/plugin/index.js +0 -1408
- package/dist/plugin/transform-client.d.ts +0 -9
- package/dist/render/assemble.d.ts +0 -15
- 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/render/shell.d.ts +0 -29
- package/dist/render/template.d.ts +0 -6
- package/dist/router.d.ts +0 -45
- package/dist/router.js +0 -1671
- package/dist/runtime-env.d.ts +0 -3
- package/dist/utils.d.ts +0 -6
- package/src/link.tsx +0 -1120
- package/src/render/assemble.ts +0 -66
- package/src/render/cache.ts +0 -241
- package/src/render/element.tsx +0 -28
- package/src/render/index.ts +0 -483
- package/src/render/loaders.ts +0 -117
- package/src/router.ts +0 -439
- /package/src/{render → server/render}/template.ts +0 -0
- /package/src/{runtime-env.ts → server/runtime-env.ts} +0 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { statSync } from "node:fs";
|
|
2
|
+
import { autoInvalidateRegistry } from "../auto-invalidate/registry";
|
|
3
|
+
import { registerCacheInvalidator } from "./registry";
|
|
4
|
+
import { createRouteCache, type RevalidateType } from "./route-cache";
|
|
5
|
+
|
|
6
|
+
export interface DevLoaderCacheEntry {
|
|
7
|
+
dependencies: string[];
|
|
8
|
+
generatedAt: number;
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
loaderData: Record<string, unknown>;
|
|
11
|
+
mode: "isr" | "ssg";
|
|
12
|
+
revalidate: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface InvalidateOutcome {
|
|
16
|
+
cleared: string[];
|
|
17
|
+
isr: number;
|
|
18
|
+
ssg: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const sourceFileToCacheKeys = new Map<string, Set<string>>();
|
|
22
|
+
|
|
23
|
+
interface CacheKindHandle {
|
|
24
|
+
cache: ReturnType<typeof createDevLoaderCache>;
|
|
25
|
+
kind: "isr" | "ssg";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function indexEntryDependencies(cacheKey: string, deps: string[]): void {
|
|
29
|
+
for (const dep of deps) {
|
|
30
|
+
let bucket = sourceFileToCacheKeys.get(dep);
|
|
31
|
+
if (!bucket) {
|
|
32
|
+
bucket = new Set<string>();
|
|
33
|
+
sourceFileToCacheKeys.set(dep, bucket);
|
|
34
|
+
}
|
|
35
|
+
bucket.add(cacheKey);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function unindexEntryDependencies(cacheKey: string, deps: string[]): void {
|
|
40
|
+
for (const dep of deps) {
|
|
41
|
+
const bucket = sourceFileToCacheKeys.get(dep);
|
|
42
|
+
if (!bucket) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
bucket.delete(cacheKey);
|
|
46
|
+
if (bucket.size === 0) {
|
|
47
|
+
sourceFileToCacheKeys.delete(dep);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createDevLoaderCache(name: string) {
|
|
53
|
+
return createRouteCache<DevLoaderCacheEntry>({
|
|
54
|
+
name,
|
|
55
|
+
onDelete: (key, entry) => {
|
|
56
|
+
unindexEntryDependencies(key, entry.dependencies);
|
|
57
|
+
const urlPath = urlPathFromCacheKey(key);
|
|
58
|
+
if (urlPath) {
|
|
59
|
+
autoInvalidateRegistry.unregisterPath(urlPath);
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
onSet: (key, entry, previous) => {
|
|
63
|
+
if (previous) {
|
|
64
|
+
unindexEntryDependencies(key, previous.dependencies);
|
|
65
|
+
}
|
|
66
|
+
indexEntryDependencies(key, entry.dependencies);
|
|
67
|
+
},
|
|
68
|
+
pathFromKey: urlPathFromCacheKey,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const devISRLoaderCache = createDevLoaderCache("render:dev-isr-loader");
|
|
73
|
+
export const devSSGLoaderCache = createDevLoaderCache("render:dev-ssg-loader");
|
|
74
|
+
|
|
75
|
+
const isrHandle: CacheKindHandle = { cache: devISRLoaderCache, kind: "isr" };
|
|
76
|
+
const ssgHandle: CacheKindHandle = { cache: devSSGLoaderCache, kind: "ssg" };
|
|
77
|
+
|
|
78
|
+
function setEntry(handle: CacheKindHandle, key: string, entry: DevLoaderCacheEntry): void {
|
|
79
|
+
handle.cache.set(key, entry);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function getDevISRLoaderCache(key: string): DevLoaderCacheEntry | undefined {
|
|
83
|
+
return devISRLoaderCache.get(key);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function setDevISRLoaderCache(key: string, entry: DevLoaderCacheEntry): void {
|
|
87
|
+
setEntry(isrHandle, key, entry);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function getDevSSGLoaderCache(key: string): DevLoaderCacheEntry | undefined {
|
|
91
|
+
return devSSGLoaderCache.get(key);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function setDevSSGLoaderCache(key: string, entry: DevLoaderCacheEntry): void {
|
|
95
|
+
setEntry(ssgHandle, key, entry);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function urlPathFromCacheKey(key: string): string | null {
|
|
99
|
+
const sep = key.lastIndexOf(":/");
|
|
100
|
+
if (sep === -1) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
return key.slice(sep + 1);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function invalidateDevLoaderCacheByPath(
|
|
107
|
+
path: string,
|
|
108
|
+
type: RevalidateType
|
|
109
|
+
): InvalidateOutcome {
|
|
110
|
+
const cleared: string[] = [];
|
|
111
|
+
let isr = 0;
|
|
112
|
+
let ssg = 0;
|
|
113
|
+
|
|
114
|
+
const matches = (urlPath: string): boolean => {
|
|
115
|
+
if (type === "page") {
|
|
116
|
+
return urlPath === path;
|
|
117
|
+
}
|
|
118
|
+
const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
|
|
119
|
+
return urlPath === path || urlPath.startsWith(prefix);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
for (const handle of [isrHandle, ssgHandle]) {
|
|
123
|
+
for (const key of [...handle.cache.keys()]) {
|
|
124
|
+
const urlPath = urlPathFromCacheKey(key);
|
|
125
|
+
if (urlPath === null || !matches(urlPath)) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const entry = handle.cache.get(key);
|
|
129
|
+
if (!entry) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
handle.cache.delete(key);
|
|
133
|
+
cleared.push(key);
|
|
134
|
+
if (handle.kind === "isr") {
|
|
135
|
+
isr++;
|
|
136
|
+
} else {
|
|
137
|
+
ssg++;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return { cleared, isr, ssg };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOutcome {
|
|
146
|
+
const keys = sourceFileToCacheKeys.get(filePath);
|
|
147
|
+
if (!keys || keys.size === 0) {
|
|
148
|
+
return { cleared: [], isr: 0, ssg: 0 };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const cleared: string[] = [];
|
|
152
|
+
let isr = 0;
|
|
153
|
+
let ssg = 0;
|
|
154
|
+
|
|
155
|
+
const snapshot = [...keys];
|
|
156
|
+
for (const key of snapshot) {
|
|
157
|
+
const isrEntry = devISRLoaderCache.get(key);
|
|
158
|
+
if (isrEntry) {
|
|
159
|
+
devISRLoaderCache.delete(key);
|
|
160
|
+
cleared.push(key);
|
|
161
|
+
isr++;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const ssgEntry = devSSGLoaderCache.get(key);
|
|
165
|
+
if (ssgEntry) {
|
|
166
|
+
devSSGLoaderCache.delete(key);
|
|
167
|
+
cleared.push(key);
|
|
168
|
+
ssg++;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return { cleared, isr, ssg };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function isDevLoaderCacheFresh(entry: DevLoaderCacheEntry): boolean {
|
|
176
|
+
return Date.now() - entry.generatedAt < entry.revalidate * 1000;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function isDevLoaderCacheValid(entry: DevLoaderCacheEntry): boolean {
|
|
180
|
+
if (!isDevLoaderCacheFresh(entry)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
for (const dep of entry.dependencies) {
|
|
184
|
+
let mtimeMs: number;
|
|
185
|
+
try {
|
|
186
|
+
mtimeMs = statSync(dep).mtimeMs;
|
|
187
|
+
} catch {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
if (mtimeMs > entry.generatedAt) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function getAllDevISRLoaderEntries(): [string, DevLoaderCacheEntry][] {
|
|
198
|
+
return [...devISRLoaderCache.entries()];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function getAllDevSSGLoaderEntries(): [string, DevLoaderCacheEntry][] {
|
|
202
|
+
return [...devSSGLoaderCache.entries()];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function __resetDevLoaderCacheState(): void {
|
|
206
|
+
devISRLoaderCache.clear();
|
|
207
|
+
devSSGLoaderCache.clear();
|
|
208
|
+
sourceFileToCacheKeys.clear();
|
|
209
|
+
registerCacheInvalidator(devISRLoaderCache);
|
|
210
|
+
registerCacheInvalidator(devSSGLoaderCache);
|
|
211
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// biome-ignore-all lint/performance/noBarrelFile: intentional barrel for public API
|
|
2
|
+
|
|
3
|
+
export type { DevLoaderCacheEntry, InvalidateOutcome } from "./dev-loader";
|
|
4
|
+
export {
|
|
5
|
+
__resetDevLoaderCacheState,
|
|
6
|
+
getAllDevISRLoaderEntries,
|
|
7
|
+
getAllDevSSGLoaderEntries,
|
|
8
|
+
getDevISRLoaderCache,
|
|
9
|
+
getDevSSGLoaderCache,
|
|
10
|
+
invalidateDevLoaderCacheByPath,
|
|
11
|
+
invalidateDevLoaderCacheBySource,
|
|
12
|
+
isDevLoaderCacheFresh,
|
|
13
|
+
isDevLoaderCacheValid,
|
|
14
|
+
setDevISRLoaderCache,
|
|
15
|
+
setDevSSGLoaderCache,
|
|
16
|
+
urlPathFromCacheKey,
|
|
17
|
+
} from "./dev-loader";
|
|
18
|
+
export {
|
|
19
|
+
__resetCacheState,
|
|
20
|
+
_runWithRequestInvalidationScope,
|
|
21
|
+
callCachePurger,
|
|
22
|
+
consumePendingInvalidations,
|
|
23
|
+
getBuildId,
|
|
24
|
+
revalidatePath,
|
|
25
|
+
setBuildId,
|
|
26
|
+
setCachePurger,
|
|
27
|
+
} from "./invalidation";
|
|
28
|
+
export { deleteISRCache, getISRCache, isrCache, setISRCache } from "./isr";
|
|
29
|
+
export type { ISRCacheEntry, SsgCacheEntry } from "./isr-ssg";
|
|
30
|
+
export type { CacheInvalidator } from "./registry";
|
|
31
|
+
export { registerCacheInvalidator } from "./registry";
|
|
32
|
+
export type { Cache, CacheInvalidationResult, RevalidateType } from "./route-cache";
|
|
33
|
+
export { createRouteCache } from "./route-cache";
|
|
34
|
+
export { getSSGCache, setSSGCache, ssgCache } from "./ssg";
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { createLogger } from "../context-logger";
|
|
3
|
+
import { devISRLoaderCache, devSSGLoaderCache } from "./dev-loader";
|
|
4
|
+
import { isrRouteCache } from "./isr";
|
|
5
|
+
import { clearCacheInvalidators, getCacheInvalidators, registerCacheInvalidator } from "./registry";
|
|
6
|
+
import type { RevalidateType } from "./route-cache";
|
|
7
|
+
import { ssgRouteCache } from "./ssg";
|
|
8
|
+
|
|
9
|
+
// Register core caches at module load time so revalidatePath works out of the box.
|
|
10
|
+
registerCacheInvalidator(isrRouteCache);
|
|
11
|
+
registerCacheInvalidator(ssgRouteCache);
|
|
12
|
+
registerCacheInvalidator(devISRLoaderCache);
|
|
13
|
+
registerCacheInvalidator(devSSGLoaderCache);
|
|
14
|
+
|
|
15
|
+
// ── Build ID ─────────────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
let _buildId = "";
|
|
18
|
+
|
|
19
|
+
export function setBuildId(id: string): void {
|
|
20
|
+
_buildId = id;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getBuildId(): string {
|
|
24
|
+
return _buildId;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ── Pending invalidations (server → client bridge) ───────────────────────────
|
|
28
|
+
|
|
29
|
+
const _requestInvalidationScope = new AsyncLocalStorage<Set<string>>();
|
|
30
|
+
const _globalPendingInvalidations = new Set<string>();
|
|
31
|
+
|
|
32
|
+
function _activeInvalidationSet(): Set<string> {
|
|
33
|
+
return _requestInvalidationScope.getStore() ?? _globalPendingInvalidations;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function _runWithRequestInvalidationScope<T>(fn: () => T): T {
|
|
37
|
+
return _requestInvalidationScope.run(new Set<string>(), fn);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function consumePendingInvalidations(): string[] {
|
|
41
|
+
const set = _activeInvalidationSet();
|
|
42
|
+
if (set.size === 0) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
const paths = [...set];
|
|
46
|
+
set.clear();
|
|
47
|
+
return paths;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ── CDN purger hook ───────────────────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
type CachePurger = (paths: string[]) => Promise<void>;
|
|
53
|
+
let _cachePurger: CachePurger | null = null;
|
|
54
|
+
|
|
55
|
+
export function setCachePurger(fn: CachePurger): void {
|
|
56
|
+
_cachePurger = fn;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function callCachePurger(paths: string[]): void {
|
|
60
|
+
if (!_cachePurger || paths.length === 0) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
_cachePurger(paths).catch((err: unknown) => {
|
|
64
|
+
const logger = createLogger({});
|
|
65
|
+
logger.set({
|
|
66
|
+
furin: {
|
|
67
|
+
action: "cdn_purge_failed",
|
|
68
|
+
paths,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
logger.error(err instanceof Error ? err : new Error(String(err)));
|
|
72
|
+
logger.emit();
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ── revalidatePath ───────────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
export function revalidatePath(path: string, type: RevalidateType): boolean {
|
|
79
|
+
_activeInvalidationSet().add(type === "layout" ? `${path}:layout` : path);
|
|
80
|
+
|
|
81
|
+
let deleted = false;
|
|
82
|
+
const purgedPaths: string[] = [];
|
|
83
|
+
for (const invalidator of getCacheInvalidators()) {
|
|
84
|
+
const result = invalidator.invalidatePath(path, type);
|
|
85
|
+
deleted = result.deleted || deleted;
|
|
86
|
+
purgedPaths.push(...result.purgedPaths);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
callCachePurger(dedupePaths([...purgedPaths, path]));
|
|
90
|
+
return deleted;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function dedupePaths(paths: string[]): string[] {
|
|
94
|
+
return [...new Set(paths)];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @internal — resets all module state between tests */
|
|
98
|
+
export function __resetCacheState(): void {
|
|
99
|
+
isrRouteCache.clear();
|
|
100
|
+
ssgRouteCache.clear();
|
|
101
|
+
devISRLoaderCache.clear();
|
|
102
|
+
devSSGLoaderCache.clear();
|
|
103
|
+
_buildId = "";
|
|
104
|
+
_globalPendingInvalidations.clear();
|
|
105
|
+
_cachePurger = null;
|
|
106
|
+
clearCacheInvalidators();
|
|
107
|
+
registerCacheInvalidator(isrRouteCache);
|
|
108
|
+
registerCacheInvalidator(ssgRouteCache);
|
|
109
|
+
registerCacheInvalidator(devISRLoaderCache);
|
|
110
|
+
registerCacheInvalidator(devSSGLoaderCache);
|
|
111
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { autoInvalidateRegistry } from "../auto-invalidate/registry";
|
|
2
|
+
import { type Cache, createRouteCache } from "./route-cache";
|
|
3
|
+
|
|
4
|
+
export interface ISRCacheEntry {
|
|
5
|
+
generatedAt: number;
|
|
6
|
+
html: string;
|
|
7
|
+
revalidate: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface SsgCacheEntry {
|
|
11
|
+
cachedAt: number;
|
|
12
|
+
html: string;
|
|
13
|
+
/**
|
|
14
|
+
* NDJSON payload identical in shape to what the live `/_furin/data` endpoint
|
|
15
|
+
* emits. Persisted alongside the HTML by the static adapter so SPA navigation
|
|
16
|
+
* on a static-hosted deployment can fetch loader data without going through
|
|
17
|
+
* a runtime endpoint that does not exist on a file server.
|
|
18
|
+
*/
|
|
19
|
+
ndjson: string;
|
|
20
|
+
status: number;
|
|
21
|
+
tags?: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Maximum number of pre-rendered HTML entries (per mode) before LRU eviction. */
|
|
25
|
+
const MAX_HTML_CACHE_SIZE = 1000;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Builds the LRU cache backing one render mode's pre-rendered HTML. ISR and SSG
|
|
29
|
+
* share the same eviction bound and the same `onDelete` hook — which keeps the
|
|
30
|
+
* auto-invalidate path registry in sync — and differ only in entry shape and
|
|
31
|
+
* the diagnostic `name`.
|
|
32
|
+
*/
|
|
33
|
+
export function createHtmlRouteCache<Entry>(mode: "isr" | "ssg"): Cache<Entry> {
|
|
34
|
+
return createRouteCache<Entry>({
|
|
35
|
+
maxSize: MAX_HTML_CACHE_SIZE,
|
|
36
|
+
name: `render:${mode}-html`,
|
|
37
|
+
onDelete: (key) => {
|
|
38
|
+
autoInvalidateRegistry.unregisterPath(key);
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createHtmlRouteCache, type ISRCacheEntry } from "./isr-ssg";
|
|
2
|
+
|
|
3
|
+
export const isrRouteCache = createHtmlRouteCache<ISRCacheEntry>("isr");
|
|
4
|
+
|
|
5
|
+
export const isrCache = isrRouteCache.store;
|
|
6
|
+
|
|
7
|
+
export function getISRCache(key: string): ISRCacheEntry | undefined {
|
|
8
|
+
return isrRouteCache.get(key);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function deleteISRCache(key: string): boolean {
|
|
12
|
+
return isrRouteCache.delete(key);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function setISRCache(key: string, entry: ISRCacheEntry): void {
|
|
16
|
+
isrRouteCache.set(key, entry);
|
|
17
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Cache } from "./route-cache";
|
|
2
|
+
|
|
3
|
+
export type CacheInvalidator = Pick<Cache<unknown>, "invalidatePath" | "name">;
|
|
4
|
+
|
|
5
|
+
const _cacheInvalidators = new Map<string, CacheInvalidator>();
|
|
6
|
+
|
|
7
|
+
export function registerCacheInvalidator(invalidator: CacheInvalidator): () => void {
|
|
8
|
+
const existing = _cacheInvalidators.get(invalidator.name);
|
|
9
|
+
if (existing !== undefined && existing !== invalidator) {
|
|
10
|
+
throw new Error(
|
|
11
|
+
`Cache invalidator "${invalidator.name}" is already registered with a different instance.`
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
if (existing === invalidator) {
|
|
15
|
+
return () => {
|
|
16
|
+
if (_cacheInvalidators.get(invalidator.name) === invalidator) {
|
|
17
|
+
_cacheInvalidators.delete(invalidator.name);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
_cacheInvalidators.set(invalidator.name, invalidator);
|
|
22
|
+
return () => {
|
|
23
|
+
if (_cacheInvalidators.get(invalidator.name) === invalidator) {
|
|
24
|
+
_cacheInvalidators.delete(invalidator.name);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getCacheInvalidators(): IterableIterator<CacheInvalidator> {
|
|
30
|
+
return _cacheInvalidators.values();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function clearCacheInvalidators(): void {
|
|
34
|
+
_cacheInvalidators.clear();
|
|
35
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import type { RequestLogger } from "evlog";
|
|
3
|
+
// biome-ignore lint/style/noExportedImports: used locally and re-exported for consumers
|
|
3
4
|
import { createLogger } from "evlog";
|
|
4
5
|
import { useLogger as _evlogUseLogger } from "evlog/elysia";
|
|
5
6
|
|
|
7
|
+
export { createLogger };
|
|
8
|
+
|
|
6
9
|
/**
|
|
7
10
|
* Fallback used when useLogger() is called completely outside any context
|
|
8
11
|
* (not in a live request, not in a synthetic render scope).
|
|
@@ -11,6 +14,8 @@ const noopLogger: RequestLogger = {
|
|
|
11
14
|
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
12
15
|
set: () => {},
|
|
13
16
|
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
17
|
+
setLevel: () => {},
|
|
18
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
14
19
|
error: () => {},
|
|
15
20
|
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
16
21
|
info: () => {},
|
|
@@ -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
|
+
}
|