@teyik0/furin 0.4.0-alpha.2 → 0.5.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/dist/build/compile-entry.d.ts +2 -1
- package/dist/build/compile-entry.ts +17 -13
- package/dist/build/elysia-aot.d.ts +9 -0
- package/dist/build/entry-template.d.ts +5 -1
- package/dist/build/entry-template.ts +27 -6
- package/dist/build/index.js +969 -528
- package/dist/build/private-server-sourcemaps.d.ts +2 -0
- package/dist/build/server-app-entry.d.ts +12 -0
- package/dist/build/types.d.ts +2 -0
- package/dist/cache.d.ts +1 -0
- package/dist/cache.js +185 -0
- package/dist/cli/index.js +983 -541
- package/dist/client/document.d.ts +2 -0
- package/dist/client/internal/runtime-types.d.ts +8 -1
- package/dist/client/link.js +232 -11
- package/dist/client/sync.d.ts +1 -0
- package/dist/client.js +11 -0
- package/dist/config.d.ts +28 -22
- package/dist/config.js +25 -2
- package/dist/define-route.d.ts +273 -1009
- package/dist/furin.d.ts +20 -5
- package/dist/furin.js +6431 -5457
- package/dist/plugin/routes.d.ts +6 -0
- package/dist/rsc-client.js +9 -1
- package/dist/rsc.js +19 -7
- package/dist/server/assets/production.d.ts +3 -0
- package/dist/server/auto-invalidate/plugin.d.ts +8 -24
- package/dist/server/auto-invalidate/runtime.d.ts +2 -2
- package/dist/server/browser-events/shutdown.d.ts +3 -0
- package/dist/server/cache/invalidation.d.ts +1 -1
- package/dist/server/cache/page-cache-state.d.ts +5 -0
- package/dist/server/cache/page-cache.d.ts +53 -0
- package/dist/server/cache/redis/index.d.ts +41 -0
- package/dist/server/cache/redis/index.js +451 -0
- package/dist/server/cache/redis/scripts.d.ts +6 -0
- package/dist/server/context-logger.d.ts +2 -2
- package/dist/server/dev/plugin.d.ts +1 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +7 -0
- package/dist/server/evlog.d.ts +14 -0
- package/dist/server/internal.d.ts +6 -0
- package/dist/server/internal.js +2539 -0
- package/dist/server/production-server.d.ts +13 -0
- package/dist/server/production-server.js +2906 -0
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ssg.d.ts +5 -0
- package/dist/server/render/static-params.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +6 -0
- package/dist/server/router/hmr.production.d.ts +5 -0
- package/dist/server/router/plugin.d.ts +5 -1
- package/dist/server/router/schemas.d.ts +1 -0
- package/dist/server/sync/index.js +798 -117
- package/dist/server/sync/plugin.d.ts +31 -158
- package/dist/server/sync/stream.d.ts +38 -49
- package/dist/shared/elysia-contract.d.ts +8 -14
- package/dist/shared/production-build.d.ts +2 -0
- package/package.json +19 -8
- package/src/adapter/README.md +9 -2
- package/src/adapter/bun.ts +146 -41
- package/src/adapter/package.ts +6 -2
- package/src/adapter/runtime-build.ts +11 -4
- package/src/adapter/static.ts +8 -6
- package/src/adapter/vercel.ts +64 -31
- package/src/build/client.ts +18 -1
- package/src/build/compile-entry.ts +17 -13
- package/src/build/elysia-aot.ts +102 -0
- package/src/build/entry-template.ts +27 -6
- package/src/build/hydrate.ts +5 -1
- package/src/build/index.ts +5 -5
- package/src/build/private-server-sourcemaps.ts +20 -0
- package/src/build/production-instrumentation.ts +2 -2
- package/src/build/server-app-entry.ts +61 -0
- package/src/build/shared.ts +3 -1
- package/src/build/ssg-cache.ts +5 -10
- package/src/build/types.ts +2 -0
- package/src/build/virtual-entry.ts +5 -4
- package/src/cache.ts +12 -0
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +6 -2
- package/src/client/document.tsx +19 -0
- package/src/client/internal/runtime-types.ts +10 -1
- package/src/client/sync.ts +1 -0
- package/src/config.ts +33 -4
- package/src/define-route.ts +266 -128
- package/src/furin.ts +168 -154
- package/src/plugin/route-config-autofix.ts +11 -2
- package/src/plugin/routes.ts +85 -29
- package/src/rsc/codec.ts +11 -6
- package/src/rsc/render-error.ts +3 -1
- package/src/server/assets/production.ts +99 -0
- package/src/server/auto-invalidate/plugin.ts +4 -4
- package/src/server/auto-invalidate/runtime.ts +74 -42
- package/src/server/browser-events/plugin.ts +13 -1
- package/src/server/browser-events/shutdown.ts +32 -0
- package/src/server/cache/invalidation.ts +61 -20
- package/src/server/cache/page-cache-state.ts +22 -0
- package/src/server/cache/page-cache.ts +300 -0
- package/src/server/cache/redis/index.ts +367 -0
- package/src/server/cache/redis/scripts.ts +200 -0
- package/src/server/context-logger.ts +5 -5
- package/src/server/dev/plugin.ts +10 -1
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev-page-plugin.ts +54 -10
- package/src/server/evlog.ts +106 -0
- package/src/server/internal.ts +7 -0
- package/src/server/production-server.ts +135 -0
- package/src/server/render/document.tsx +7 -1
- package/src/server/render/isr.ts +480 -132
- package/src/server/render/loaders.ts +18 -12
- package/src/server/render/not-found.ts +4 -4
- package/src/server/render/ppr-document.ts +2 -2
- package/src/server/render/ppr-route.ts +193 -7
- package/src/server/render/ssg.ts +148 -4
- package/src/server/render/ssr.ts +6 -7
- package/src/server/render/static-params.ts +174 -0
- package/src/server/render/template.ts +5 -1
- package/src/server/router/defined-route.ts +2 -1
- package/src/server/router/discovery.ts +3 -1
- package/src/server/router/hmr.production.ts +11 -0
- package/src/server/router/hmr.ts +85 -76
- package/src/server/router/plugin.ts +118 -50
- package/src/server/router/schema-merge.ts +10 -3
- package/src/server/router/schemas.ts +174 -61
- package/src/server/sync/plugin.ts +14 -10
- package/src/server/sync/response.ts +3 -3
- package/src/server/sync/stream.ts +43 -2
- package/src/shared/elysia-contract.ts +23 -25
- package/src/shared/production-build.ts +20 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { physicalPath } from "../../shared/prefix.ts";
|
|
1
2
|
import {
|
|
2
3
|
currentInstrumentationRequest,
|
|
3
4
|
emitCacheInvalidated,
|
|
@@ -16,6 +17,7 @@ import { clearPprRouteCache } from "../render/ppr-route.ts";
|
|
|
16
17
|
import { IS_DEV } from "../runtime-env.ts";
|
|
17
18
|
import { clearDevLoaderCaches } from "./dev-loader";
|
|
18
19
|
import { clearPendingISRRevalidations, isrRouteCache } from "./isr";
|
|
20
|
+
import { getPageCacheAdapter, resetPageCacheAdapter } from "./page-cache-state.ts";
|
|
19
21
|
import {
|
|
20
22
|
type CachePurger,
|
|
21
23
|
setCachePurger as installPurger,
|
|
@@ -67,6 +69,57 @@ export function callCachePurger(paths: string[]): void {
|
|
|
67
69
|
|
|
68
70
|
// ── revalidatePath ───────────────────────────────────────────────────────────
|
|
69
71
|
|
|
72
|
+
function emitPathInvalidation(
|
|
73
|
+
instance: FurinInstance,
|
|
74
|
+
path: string,
|
|
75
|
+
deleted: boolean,
|
|
76
|
+
purgedPaths: readonly string[]
|
|
77
|
+
): void {
|
|
78
|
+
if (!IS_DEV) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
withInstance(instance, () => {
|
|
82
|
+
const request = currentInstrumentationRequest();
|
|
83
|
+
emitCacheInvalidated({
|
|
84
|
+
deleted,
|
|
85
|
+
operationId: request?.operationId ?? null,
|
|
86
|
+
purgedPaths: new Set(purgedPaths).size,
|
|
87
|
+
reason: "path",
|
|
88
|
+
requestId: request?.requestId ?? null,
|
|
89
|
+
target: path,
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function invalidateInstancePath(
|
|
95
|
+
instance: FurinInstance,
|
|
96
|
+
path: string,
|
|
97
|
+
type: RevalidateType
|
|
98
|
+
): Promise<{ deleted: boolean; purgedPaths: string[] }> {
|
|
99
|
+
const result = revalidatePathForInstance(instance, path, type, false);
|
|
100
|
+
const purgedPaths = [physicalPath(instance.prefix, path)];
|
|
101
|
+
for (const purged of result.purgedPaths) {
|
|
102
|
+
purgedPaths.push(physicalPath(instance.prefix, purged));
|
|
103
|
+
}
|
|
104
|
+
const pageCache = getPageCacheAdapter(instance);
|
|
105
|
+
if (pageCache === undefined) {
|
|
106
|
+
emitPathInvalidation(instance, path, result.deleted, purgedPaths);
|
|
107
|
+
return { deleted: result.deleted, purgedPaths };
|
|
108
|
+
}
|
|
109
|
+
const sharedResult = await pageCache.invalidate({
|
|
110
|
+
kind: "path",
|
|
111
|
+
path,
|
|
112
|
+
scope: instance.prefix,
|
|
113
|
+
type,
|
|
114
|
+
});
|
|
115
|
+
for (const purged of sharedResult.paths) {
|
|
116
|
+
purgedPaths.push(physicalPath(instance.prefix, purged));
|
|
117
|
+
}
|
|
118
|
+
const deleted = result.deleted || sharedResult.invalidated;
|
|
119
|
+
emitPathInvalidation(instance, path, deleted, purgedPaths);
|
|
120
|
+
return { deleted, purgedPaths };
|
|
121
|
+
}
|
|
122
|
+
|
|
70
123
|
/**
|
|
71
124
|
* Invalidates `path` across EVERY mounted furin instance — paths are logical
|
|
72
125
|
* (unprefixed), and with shared data two apps can legitimately render the same
|
|
@@ -74,16 +127,15 @@ export function callCachePurger(paths: string[]): void {
|
|
|
74
127
|
* instance's invalidation runs inside its own scope so cache `onDelete` hooks
|
|
75
128
|
* (auto-invalidate unregistration) hit the owning instance's registry.
|
|
76
129
|
*/
|
|
77
|
-
export function revalidatePath(path: string, type: RevalidateType): boolean {
|
|
130
|
+
export async function revalidatePath(path: string, type: RevalidateType): Promise<boolean> {
|
|
78
131
|
let deleted = false;
|
|
79
132
|
const purgedPaths: string[] = [];
|
|
80
|
-
|
|
81
|
-
|
|
133
|
+
const results = await Promise.all(
|
|
134
|
+
allInstances().map((instance) => invalidateInstancePath(instance, path, type))
|
|
135
|
+
);
|
|
136
|
+
for (const result of results) {
|
|
82
137
|
deleted = result.deleted || deleted;
|
|
83
|
-
purgedPaths.push(
|
|
84
|
-
for (const purged of result.purgedPaths) {
|
|
85
|
-
purgedPaths.push(physicalPath(instance.prefix, purged));
|
|
86
|
-
}
|
|
138
|
+
purgedPaths.push(...result.purgedPaths);
|
|
87
139
|
}
|
|
88
140
|
|
|
89
141
|
callCachePurger(dedupePaths(purgedPaths));
|
|
@@ -114,17 +166,7 @@ export function revalidatePathForInstance(
|
|
|
114
166
|
purgedPaths.push(...result.purgedPaths);
|
|
115
167
|
}
|
|
116
168
|
if (IS_DEV && emitPathEvent !== false) {
|
|
117
|
-
|
|
118
|
-
const operationId = request === undefined ? null : request.operationId;
|
|
119
|
-
const requestId = request === undefined ? null : request.requestId;
|
|
120
|
-
emitCacheInvalidated({
|
|
121
|
-
deleted,
|
|
122
|
-
operationId,
|
|
123
|
-
purgedPaths: new Set(purgedPaths).size,
|
|
124
|
-
reason: "path",
|
|
125
|
-
requestId,
|
|
126
|
-
target: path,
|
|
127
|
-
});
|
|
169
|
+
emitPathInvalidation(instance, path, deleted, purgedPaths);
|
|
128
170
|
}
|
|
129
171
|
});
|
|
130
172
|
return { deleted, purgedPaths };
|
|
@@ -147,6 +189,7 @@ export function __resetCacheState(): void {
|
|
|
147
189
|
clearDevLoaderCaches(instance);
|
|
148
190
|
clearPprRouteCache(instance);
|
|
149
191
|
clearPendingISRRevalidations(instance);
|
|
192
|
+
resetPageCacheAdapter(instance);
|
|
150
193
|
});
|
|
151
194
|
instance.buildId = "";
|
|
152
195
|
}
|
|
@@ -157,5 +200,3 @@ export function __resetCacheState(): void {
|
|
|
157
200
|
_globalPendingInvalidations.clear();
|
|
158
201
|
resetCachePurgers();
|
|
159
202
|
}
|
|
160
|
-
|
|
161
|
-
import { physicalPath } from "../../shared/prefix.ts";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type FurinInstance, instanceSlot } from "../instance.ts";
|
|
2
|
+
import type { PageCacheAdapter } from "./page-cache.ts";
|
|
3
|
+
|
|
4
|
+
interface PageCacheAdapterState {
|
|
5
|
+
adapter: PageCacheAdapter | undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const instancePageCacheAdapter = instanceSlot<PageCacheAdapterState>(() => ({
|
|
9
|
+
adapter: undefined,
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
export function getPageCacheAdapter(instance?: FurinInstance): PageCacheAdapter | undefined {
|
|
13
|
+
return instancePageCacheAdapter(instance).adapter;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function setPageCacheAdapter(instance: FurinInstance, adapter: PageCacheAdapter): void {
|
|
17
|
+
instancePageCacheAdapter(instance).adapter = adapter;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function resetPageCacheAdapter(instance: FurinInstance): void {
|
|
21
|
+
instancePageCacheAdapter(instance).adapter = undefined;
|
|
22
|
+
}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
export type PageCacheMode = "isr" | "ppr" | "ssg";
|
|
2
|
+
|
|
3
|
+
const TRAILING_SLASHES = /\/+$/;
|
|
4
|
+
|
|
5
|
+
export interface PageCacheIdentity {
|
|
6
|
+
buildId: string;
|
|
7
|
+
key: string;
|
|
8
|
+
mode: PageCacheMode;
|
|
9
|
+
path: string;
|
|
10
|
+
scope: string;
|
|
11
|
+
tags: readonly string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface PageCacheEntry {
|
|
15
|
+
cachedAt: number;
|
|
16
|
+
payload: string;
|
|
17
|
+
revalidate: number | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PageCacheLease {
|
|
21
|
+
expiresAt: number;
|
|
22
|
+
fence: number;
|
|
23
|
+
id: string;
|
|
24
|
+
revision: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type PageCacheInvalidation =
|
|
28
|
+
| {
|
|
29
|
+
kind: "path";
|
|
30
|
+
path: string;
|
|
31
|
+
scope: string;
|
|
32
|
+
type: "layout" | "page";
|
|
33
|
+
}
|
|
34
|
+
| {
|
|
35
|
+
kind: "tags";
|
|
36
|
+
scope: string;
|
|
37
|
+
tags: readonly string[];
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export interface PageCacheInvalidationResult {
|
|
41
|
+
invalidated: boolean;
|
|
42
|
+
paths: readonly string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface PageCacheAdapter {
|
|
46
|
+
acquire: (input: {
|
|
47
|
+
identity: PageCacheIdentity;
|
|
48
|
+
leaseMs: number;
|
|
49
|
+
}) => Promise<PageCacheLease | null>;
|
|
50
|
+
commit: (input: {
|
|
51
|
+
entry: PageCacheEntry;
|
|
52
|
+
identity: PageCacheIdentity;
|
|
53
|
+
lease: PageCacheLease;
|
|
54
|
+
}) => Promise<"stored" | "superseded">;
|
|
55
|
+
invalidate: (input: PageCacheInvalidation) => Promise<PageCacheInvalidationResult>;
|
|
56
|
+
read: (identity: PageCacheIdentity) => Promise<PageCacheEntry | null>;
|
|
57
|
+
release: (input: { identity: PageCacheIdentity; lease: PageCacheLease }) => Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function waitForPageCacheEntry(
|
|
61
|
+
adapter: PageCacheAdapter,
|
|
62
|
+
identity: PageCacheIdentity,
|
|
63
|
+
timeoutMs: number
|
|
64
|
+
): Promise<PageCacheEntry | null> {
|
|
65
|
+
const deadline = Date.now() + timeoutMs;
|
|
66
|
+
while (Date.now() < deadline) {
|
|
67
|
+
// biome-ignore lint/performance/noAwaitInLoops: bounded polling must observe the preceding read before retrying.
|
|
68
|
+
await Bun.sleep(10);
|
|
69
|
+
const entry = await adapter.read(identity);
|
|
70
|
+
if (entry !== null) {
|
|
71
|
+
return entry;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function identityMatchesPath(
|
|
78
|
+
identity: PageCacheIdentity,
|
|
79
|
+
input: Extract<PageCacheInvalidation, { kind: "path" }>
|
|
80
|
+
): boolean {
|
|
81
|
+
if (identity.scope !== input.scope) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
if (input.type === "page") {
|
|
85
|
+
return identity.path === input.path;
|
|
86
|
+
}
|
|
87
|
+
return (
|
|
88
|
+
input.path === "/" || identity.path === input.path || identity.path.startsWith(`${input.path}/`)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function identityMatchesTags(
|
|
93
|
+
identity: PageCacheIdentity,
|
|
94
|
+
scope: string,
|
|
95
|
+
tags: ReadonlySet<string>
|
|
96
|
+
): boolean {
|
|
97
|
+
return identity.scope === scope && identity.tags.some((tag) => tags.has(tag));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface StoredPageCacheEntry {
|
|
101
|
+
entry: PageCacheEntry;
|
|
102
|
+
identity: PageCacheIdentity;
|
|
103
|
+
revision: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
interface ActivePageCacheLease {
|
|
107
|
+
identity: PageCacheIdentity;
|
|
108
|
+
lease: PageCacheLease;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function invalidateMatchingEntries(
|
|
112
|
+
entries: Map<string, StoredPageCacheEntry>,
|
|
113
|
+
leases: Map<string, ActivePageCacheLease>,
|
|
114
|
+
matches: (identity: PageCacheIdentity) => boolean
|
|
115
|
+
): PageCacheInvalidationResult {
|
|
116
|
+
const affectedPaths = new Set<string>();
|
|
117
|
+
for (const [key, stored] of entries) {
|
|
118
|
+
if (matches(stored.identity)) {
|
|
119
|
+
affectedPaths.add(stored.identity.path);
|
|
120
|
+
entries.delete(key);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
for (const active of leases.values()) {
|
|
124
|
+
if (matches(active.identity)) {
|
|
125
|
+
affectedPaths.add(active.identity.path);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return { invalidated: affectedPaths.size > 0, paths: [...affectedPaths] };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function identityKey(identity: PageCacheIdentity): string {
|
|
132
|
+
return JSON.stringify([identity.scope, identity.buildId, identity.mode, identity.key]);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function layoutPaths(path: string): string[] {
|
|
136
|
+
if (path === "/") {
|
|
137
|
+
return ["/"];
|
|
138
|
+
}
|
|
139
|
+
const segments = path.split("/").filter(Boolean);
|
|
140
|
+
const paths = ["/"];
|
|
141
|
+
let current = "";
|
|
142
|
+
for (const segment of segments) {
|
|
143
|
+
current += `/${segment}`;
|
|
144
|
+
paths.push(current);
|
|
145
|
+
}
|
|
146
|
+
return paths;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function selectorKey(scope: string, selector: "layout" | "page" | "tag", value: string): string {
|
|
150
|
+
return JSON.stringify([scope, selector, value]);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function normalizeInvalidationPath(path: string): string {
|
|
154
|
+
return path.length > 1 ? path.replace(TRAILING_SLASHES, "") : path;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function createMemoryPageCache(): PageCacheAdapter {
|
|
158
|
+
const maxEntries = 1000;
|
|
159
|
+
const entries = new Map<string, StoredPageCacheEntry>();
|
|
160
|
+
const leases = new Map<string, ActivePageCacheLease>();
|
|
161
|
+
const fences = new Map<string, number>();
|
|
162
|
+
const versions = new Map<string, number>();
|
|
163
|
+
|
|
164
|
+
const selectorsFor = (identity: PageCacheIdentity): string[] => [
|
|
165
|
+
selectorKey(identity.scope, "page", identity.path),
|
|
166
|
+
...layoutPaths(identity.path).map((path) => selectorKey(identity.scope, "layout", path)),
|
|
167
|
+
...identity.tags.map((tag) => selectorKey(identity.scope, "tag", tag)),
|
|
168
|
+
];
|
|
169
|
+
|
|
170
|
+
const revisionFor = (identity: PageCacheIdentity): string =>
|
|
171
|
+
JSON.stringify(selectorsFor(identity).map((selector) => versions.get(selector) ?? 0));
|
|
172
|
+
|
|
173
|
+
const increment = (key: string): void => {
|
|
174
|
+
versions.set(key, (versions.get(key) ?? 0) + 1);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const removeExpiredLeases = (now: number): void => {
|
|
178
|
+
for (const [key, active] of leases) {
|
|
179
|
+
if (active.lease.expiresAt <= now) {
|
|
180
|
+
leases.delete(key);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const liveSelectors = (): Set<string> => {
|
|
186
|
+
const selectors = new Set<string>();
|
|
187
|
+
for (const identity of [
|
|
188
|
+
...[...entries.values()].map((stored) => stored.identity),
|
|
189
|
+
...[...leases.values()].map((active) => active.identity),
|
|
190
|
+
]) {
|
|
191
|
+
for (const selector of selectorsFor(identity)) {
|
|
192
|
+
selectors.add(selector);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return selectors;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const cleanupMetadata = (now: number): void => {
|
|
199
|
+
removeExpiredLeases(now);
|
|
200
|
+
const liveKeys = new Set([...entries.keys(), ...leases.keys()]);
|
|
201
|
+
for (const key of fences.keys()) {
|
|
202
|
+
if (!liveKeys.has(key)) {
|
|
203
|
+
fences.delete(key);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const retainedSelectors = liveSelectors();
|
|
207
|
+
for (const selector of versions.keys()) {
|
|
208
|
+
if (!retainedSelectors.has(selector)) {
|
|
209
|
+
versions.delete(selector);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
acquire({ identity, leaseMs }) {
|
|
216
|
+
const key = identityKey(identity);
|
|
217
|
+
const now = Date.now();
|
|
218
|
+
cleanupMetadata(now);
|
|
219
|
+
const active = leases.get(key);
|
|
220
|
+
if (active !== undefined && active.lease.expiresAt > now) {
|
|
221
|
+
return Promise.resolve(null);
|
|
222
|
+
}
|
|
223
|
+
if (leases.size >= maxEntries) {
|
|
224
|
+
return Promise.resolve(null);
|
|
225
|
+
}
|
|
226
|
+
const lease: PageCacheLease = {
|
|
227
|
+
expiresAt: now + leaseMs,
|
|
228
|
+
fence: (fences.get(key) ?? 0) + 1,
|
|
229
|
+
id: crypto.randomUUID(),
|
|
230
|
+
revision: revisionFor(identity),
|
|
231
|
+
};
|
|
232
|
+
fences.set(key, lease.fence);
|
|
233
|
+
leases.set(key, { identity, lease });
|
|
234
|
+
return Promise.resolve(lease);
|
|
235
|
+
},
|
|
236
|
+
commit({ entry, identity, lease }) {
|
|
237
|
+
const key = identityKey(identity);
|
|
238
|
+
cleanupMetadata(Date.now());
|
|
239
|
+
const active = leases.get(key);
|
|
240
|
+
if (active?.lease.id !== lease.id || active.lease.fence !== lease.fence) {
|
|
241
|
+
return Promise.resolve("superseded");
|
|
242
|
+
}
|
|
243
|
+
leases.delete(key);
|
|
244
|
+
if (lease.expiresAt <= Date.now() || lease.revision !== revisionFor(identity)) {
|
|
245
|
+
return Promise.resolve("superseded");
|
|
246
|
+
}
|
|
247
|
+
entries.delete(key);
|
|
248
|
+
entries.set(key, { entry, identity, revision: lease.revision });
|
|
249
|
+
if (entries.size > maxEntries) {
|
|
250
|
+
const oldest = entries.keys().next().value;
|
|
251
|
+
if (oldest !== undefined) {
|
|
252
|
+
entries.delete(oldest);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
cleanupMetadata(Date.now());
|
|
256
|
+
return Promise.resolve("stored");
|
|
257
|
+
},
|
|
258
|
+
invalidate(input) {
|
|
259
|
+
cleanupMetadata(Date.now());
|
|
260
|
+
if (input.kind === "path") {
|
|
261
|
+
const normalizedInput = { ...input, path: normalizeInvalidationPath(input.path) };
|
|
262
|
+
const result = invalidateMatchingEntries(entries, leases, (identity) =>
|
|
263
|
+
identityMatchesPath(identity, normalizedInput)
|
|
264
|
+
);
|
|
265
|
+
increment(selectorKey(input.scope, input.type, normalizedInput.path));
|
|
266
|
+
cleanupMetadata(Date.now());
|
|
267
|
+
return Promise.resolve(result);
|
|
268
|
+
}
|
|
269
|
+
const tags = new Set(input.tags);
|
|
270
|
+
const result = invalidateMatchingEntries(entries, leases, (identity) =>
|
|
271
|
+
identityMatchesTags(identity, input.scope, tags)
|
|
272
|
+
);
|
|
273
|
+
for (const tag of input.tags) {
|
|
274
|
+
increment(selectorKey(input.scope, "tag", tag));
|
|
275
|
+
}
|
|
276
|
+
cleanupMetadata(Date.now());
|
|
277
|
+
return Promise.resolve(result);
|
|
278
|
+
},
|
|
279
|
+
read(identity) {
|
|
280
|
+
cleanupMetadata(Date.now());
|
|
281
|
+
const key = identityKey(identity);
|
|
282
|
+
const stored = entries.get(key);
|
|
283
|
+
if (stored === undefined || stored.revision !== revisionFor(identity)) {
|
|
284
|
+
entries.delete(key);
|
|
285
|
+
return Promise.resolve(null);
|
|
286
|
+
}
|
|
287
|
+
entries.delete(key);
|
|
288
|
+
entries.set(key, stored);
|
|
289
|
+
return Promise.resolve(stored.entry);
|
|
290
|
+
},
|
|
291
|
+
release({ identity, lease }) {
|
|
292
|
+
const key = identityKey(identity);
|
|
293
|
+
if (leases.get(key)?.lease.id === lease.id) {
|
|
294
|
+
leases.delete(key);
|
|
295
|
+
}
|
|
296
|
+
cleanupMetadata(Date.now());
|
|
297
|
+
return Promise.resolve();
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
}
|