@teyik0/furin 0.1.0-alpha.9 → 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 +4027 -685
- 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 +4007 -687
- 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 +51 -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 +5291 -662
- 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/{context-logger.d.ts → server/context-logger.d.ts} +2 -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 -7
- package/{src → dist/server}/render/shell.ts +28 -13
- 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 +79 -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 +80 -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/{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 +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 -1232
- 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/router.d.ts +0 -45
- package/dist/router.js +0 -1671
- 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/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,25 @@
|
|
|
1
|
+
import { type AfterHandler, Elysia } from "elysia";
|
|
2
|
+
import {
|
|
3
|
+
appendPendingInvalidationHeader,
|
|
4
|
+
isSuccessfulMutationResponse,
|
|
5
|
+
runInvalidationRules,
|
|
6
|
+
} from "./runtime.ts";
|
|
7
|
+
import type { InvalidationInput } from "./types.ts";
|
|
8
|
+
|
|
9
|
+
type AnyAfterHandleContext = Parameters<AfterHandler>[0];
|
|
10
|
+
|
|
11
|
+
export function furinInvalidate() {
|
|
12
|
+
return new Elysia({ name: "furin-invalidate" }).macro({
|
|
13
|
+
invalidate(rules: InvalidationInput) {
|
|
14
|
+
return {
|
|
15
|
+
afterHandle(ctx: AnyAfterHandleContext) {
|
|
16
|
+
if (!isSuccessfulMutationResponse(ctx)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
runInvalidationRules(rules);
|
|
20
|
+
appendPendingInvalidationHeader(ctx.set);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export class AutoInvalidateRegistry {
|
|
2
|
+
private readonly pathToTags = new Map<string, Set<string>>();
|
|
3
|
+
private readonly tagToPaths = new Map<string, Set<string>>();
|
|
4
|
+
|
|
5
|
+
registerLoaderTags(urlPath: string, tags: readonly string[] | undefined): void {
|
|
6
|
+
if (!tags || tags.length === 0) {
|
|
7
|
+
this.unregisterPath(urlPath);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
this.unregisterPath(urlPath);
|
|
12
|
+
|
|
13
|
+
const uniqueTags = new Set(tags);
|
|
14
|
+
this.pathToTags.set(urlPath, uniqueTags);
|
|
15
|
+
for (const tag of uniqueTags) {
|
|
16
|
+
let paths = this.tagToPaths.get(tag);
|
|
17
|
+
if (!paths) {
|
|
18
|
+
paths = new Set<string>();
|
|
19
|
+
this.tagToPaths.set(tag, paths);
|
|
20
|
+
}
|
|
21
|
+
paths.add(urlPath);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
pathsForTags(tags: readonly string[]): string[] {
|
|
26
|
+
const paths = new Set<string>();
|
|
27
|
+
for (const tag of tags) {
|
|
28
|
+
for (const path of this.tagToPaths.get(tag) ?? []) {
|
|
29
|
+
paths.add(path);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return [...paths];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
unregisterPath(urlPath: string): void {
|
|
36
|
+
const tags = this.pathToTags.get(urlPath);
|
|
37
|
+
if (!tags) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (const tag of tags) {
|
|
42
|
+
const paths = this.tagToPaths.get(tag);
|
|
43
|
+
if (!paths) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
paths.delete(urlPath);
|
|
47
|
+
if (paths.size === 0) {
|
|
48
|
+
this.tagToPaths.delete(tag);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
this.pathToTags.delete(urlPath);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
reset(): void {
|
|
55
|
+
this.pathToTags.clear();
|
|
56
|
+
this.tagToPaths.clear();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const autoInvalidateRegistry = new AutoInvalidateRegistry();
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { Context } from "elysia";
|
|
2
|
+
import { consumePendingInvalidations, revalidatePath } from "../cache/invalidation.ts";
|
|
3
|
+
import { autoInvalidateRegistry } from "./registry.ts";
|
|
4
|
+
import type { InvalidationInput, InvalidationRule } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
function toRules(input: InvalidationInput): readonly InvalidationRule[] {
|
|
7
|
+
return Array.isArray(input) ? input : [input as InvalidationRule];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function statusFromResponseValue(responseValue: unknown): number | undefined {
|
|
11
|
+
if (responseValue instanceof Response) {
|
|
12
|
+
return responseValue.status;
|
|
13
|
+
}
|
|
14
|
+
if (responseValue && typeof responseValue === "object" && "status" in responseValue) {
|
|
15
|
+
const status = (responseValue as { status?: unknown }).status;
|
|
16
|
+
if (typeof status === "number") {
|
|
17
|
+
return status;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (responseValue && typeof responseValue === "object" && "code" in responseValue) {
|
|
21
|
+
const code = (responseValue as { code?: unknown }).code;
|
|
22
|
+
if (typeof code === "number") {
|
|
23
|
+
return code;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isSuccessfulMutationResponse(
|
|
30
|
+
ctx: Pick<Context, "set"> & {
|
|
31
|
+
responseValue?: unknown;
|
|
32
|
+
response?: unknown;
|
|
33
|
+
}
|
|
34
|
+
): boolean {
|
|
35
|
+
const responseStatus =
|
|
36
|
+
statusFromResponseValue(ctx.responseValue) ?? statusFromResponseValue(ctx.response);
|
|
37
|
+
if (responseStatus !== undefined) {
|
|
38
|
+
return responseStatus >= 200 && responseStatus < 400;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const setStatus = ctx.set.status;
|
|
42
|
+
if (typeof setStatus === "number") {
|
|
43
|
+
return setStatus >= 200 && setStatus < 400;
|
|
44
|
+
}
|
|
45
|
+
if (typeof setStatus === "string") {
|
|
46
|
+
// Elysia allows string status codes (e.g. "Not Found"). We cannot map
|
|
47
|
+
// every possible string to its numeric code without the StatusMap, so we
|
|
48
|
+
// treat known client-error (4xx) and server-error (5xx) texts as failures
|
|
49
|
+
// and everything else as success (1xx, 2xx, 3xx, and any unknown string).
|
|
50
|
+
const errorStatusTexts = new Set([
|
|
51
|
+
"Bad Request",
|
|
52
|
+
"Unauthorized",
|
|
53
|
+
"Payment Required",
|
|
54
|
+
"Forbidden",
|
|
55
|
+
"Not Found",
|
|
56
|
+
"Method Not Allowed",
|
|
57
|
+
"Not Acceptable",
|
|
58
|
+
"Proxy Authentication Required",
|
|
59
|
+
"Request Timeout",
|
|
60
|
+
"Conflict",
|
|
61
|
+
"Gone",
|
|
62
|
+
"Length Required",
|
|
63
|
+
"Precondition Failed",
|
|
64
|
+
"Payload Too Large",
|
|
65
|
+
"URI Too Long",
|
|
66
|
+
"Unsupported Media Type",
|
|
67
|
+
"Range Not Satisfiable",
|
|
68
|
+
"Expectation Failed",
|
|
69
|
+
"I'm a teapot",
|
|
70
|
+
"Misdirected Request",
|
|
71
|
+
"Unprocessable Content",
|
|
72
|
+
"Locked",
|
|
73
|
+
"Failed Dependency",
|
|
74
|
+
"Too Early",
|
|
75
|
+
"Upgrade Required",
|
|
76
|
+
"Precondition Required",
|
|
77
|
+
"Too Many Requests",
|
|
78
|
+
"Enhance Your Calm",
|
|
79
|
+
"Request Header Fields Too Large",
|
|
80
|
+
"Unavailable For Legal Reasons",
|
|
81
|
+
"Internal Server Error",
|
|
82
|
+
"Not Implemented",
|
|
83
|
+
"Bad Gateway",
|
|
84
|
+
"Service Unavailable",
|
|
85
|
+
"Gateway Timeout",
|
|
86
|
+
"HTTP Version Not Supported",
|
|
87
|
+
"Variant Also Negotiates",
|
|
88
|
+
"Insufficient Storage",
|
|
89
|
+
"Loop Detected",
|
|
90
|
+
"Not Extended",
|
|
91
|
+
"Network Authentication Required",
|
|
92
|
+
]);
|
|
93
|
+
return !errorStatusTexts.has(setStatus);
|
|
94
|
+
}
|
|
95
|
+
// No explicit status was set — default to success (200).
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function appendPendingInvalidationHeader(set: Context["set"]): void {
|
|
100
|
+
const pending = consumePendingInvalidations();
|
|
101
|
+
if (pending.length === 0) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const headerName = "x-furin-revalidate";
|
|
106
|
+
const existing = set.headers[headerName];
|
|
107
|
+
set.headers[headerName] =
|
|
108
|
+
typeof existing === "string" && existing.length > 0
|
|
109
|
+
? `${existing},${pending.join(",")}`
|
|
110
|
+
: pending.join(",");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function revalidateTag(tags: string | readonly string[]): boolean {
|
|
114
|
+
const tagList = typeof tags === "string" ? [tags] : [...tags];
|
|
115
|
+
let deleted = false;
|
|
116
|
+
for (const path of autoInvalidateRegistry.pathsForTags(tagList)) {
|
|
117
|
+
deleted = revalidatePath(path, "page") || deleted;
|
|
118
|
+
}
|
|
119
|
+
return deleted;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function runInvalidationRules(input: InvalidationInput): boolean {
|
|
123
|
+
let deleted = false;
|
|
124
|
+
for (const rule of toRules(input)) {
|
|
125
|
+
if ("path" in rule && rule.path) {
|
|
126
|
+
deleted = revalidatePath(rule.path, rule.type) || deleted;
|
|
127
|
+
}
|
|
128
|
+
if (rule.tags && rule.tags.length > 0) {
|
|
129
|
+
deleted = revalidateTag(rule.tags) || deleted;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return deleted;
|
|
133
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CacheTag } from "../../furin.ts";
|
|
2
|
+
import type { RevalidateType } from "../cache/route-cache.ts";
|
|
3
|
+
|
|
4
|
+
export type InvalidationRule =
|
|
5
|
+
| {
|
|
6
|
+
path: string;
|
|
7
|
+
tags?: readonly CacheTag[];
|
|
8
|
+
type: RevalidateType;
|
|
9
|
+
}
|
|
10
|
+
| {
|
|
11
|
+
path?: never;
|
|
12
|
+
tags: readonly CacheTag[];
|
|
13
|
+
type?: never;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type InvalidationInput = InvalidationRule | readonly InvalidationRule[];
|
|
@@ -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
|
+
}
|