@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
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import type { RedisClient } from "bun";
|
|
2
|
+
import type {
|
|
3
|
+
PageCacheAdapter,
|
|
4
|
+
PageCacheEntry,
|
|
5
|
+
PageCacheIdentity,
|
|
6
|
+
PageCacheLease,
|
|
7
|
+
} from "../page-cache.ts";
|
|
8
|
+
import {
|
|
9
|
+
ACQUIRE_PAGE_CACHE_LEASE_SCRIPT,
|
|
10
|
+
COMMIT_PAGE_CACHE_ENTRY_SCRIPT,
|
|
11
|
+
INVALIDATE_PAGE_CACHE_PATH_SCRIPT,
|
|
12
|
+
INVALIDATE_PAGE_CACHE_TAGS_SCRIPT,
|
|
13
|
+
READ_PAGE_CACHE_ENTRY_SCRIPT,
|
|
14
|
+
RELEASE_PAGE_CACHE_LEASE_SCRIPT,
|
|
15
|
+
} from "./scripts.ts";
|
|
16
|
+
|
|
17
|
+
const TRAILING_SLASHES = /\/+$/;
|
|
18
|
+
const DEFAULT_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;
|
|
19
|
+
const METADATA_GRACE_MS = 24 * 60 * 60 * 1000;
|
|
20
|
+
|
|
21
|
+
export interface RedisPageCacheOptions {
|
|
22
|
+
client: RedisClient;
|
|
23
|
+
namespace: string;
|
|
24
|
+
retentionMs?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface RedisLeaseDocument {
|
|
28
|
+
expiresAt: number;
|
|
29
|
+
fence: number;
|
|
30
|
+
fields: string[];
|
|
31
|
+
id: string;
|
|
32
|
+
values: number[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface RedisEntryDocument {
|
|
36
|
+
entry: PageCacheEntry;
|
|
37
|
+
fields: string[];
|
|
38
|
+
indexMember: string;
|
|
39
|
+
pathsKey: string;
|
|
40
|
+
tagKeys: string[];
|
|
41
|
+
values: number[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface RedisIndexMember {
|
|
45
|
+
entryKey: string;
|
|
46
|
+
path: string;
|
|
47
|
+
pathsKey: string;
|
|
48
|
+
tagKeys: string[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function assertNamespace(namespace: string): void {
|
|
52
|
+
if (namespace.length === 0) {
|
|
53
|
+
throw new Error("[furin-page-cache-redis] namespace must not be empty.");
|
|
54
|
+
}
|
|
55
|
+
if (!namespace.isWellFormed()) {
|
|
56
|
+
throw new Error("[furin-page-cache-redis] namespace must contain valid Unicode.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function digest(value: string): string {
|
|
61
|
+
return new Bun.CryptoHasher("sha256").update(value).digest("hex");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function identityDigest(identity: PageCacheIdentity): string {
|
|
65
|
+
return digest(JSON.stringify([identity.scope, identity.buildId, identity.mode, identity.key]));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function layoutPaths(path: string): string[] {
|
|
69
|
+
if (path === "/") {
|
|
70
|
+
return ["/"];
|
|
71
|
+
}
|
|
72
|
+
const paths = ["/"];
|
|
73
|
+
let current = "";
|
|
74
|
+
for (const segment of path.split("/").filter(Boolean)) {
|
|
75
|
+
current += `/${segment}`;
|
|
76
|
+
paths.push(current);
|
|
77
|
+
}
|
|
78
|
+
return paths;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function normalizeInvalidationPath(path: string): string {
|
|
82
|
+
return path.length > 1 ? path.replace(TRAILING_SLASHES, "") : path;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function selectorFields(identity: PageCacheIdentity): string[] {
|
|
86
|
+
return [
|
|
87
|
+
`page:${identity.path}`,
|
|
88
|
+
...layoutPaths(identity.path).map((path) => `layout:${path}`),
|
|
89
|
+
...identity.tags.map((tag) => `tag:${tag}`),
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function stringResult(value: unknown, operation: string): string {
|
|
94
|
+
if (typeof value !== "string") {
|
|
95
|
+
throw new Error(`[furin-page-cache-redis] Invalid ${operation} response.`);
|
|
96
|
+
}
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function stringArrayResult(value: unknown, operation: string): string[] {
|
|
101
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string")) {
|
|
102
|
+
throw new Error(`[furin-page-cache-redis] Invalid ${operation} response.`);
|
|
103
|
+
}
|
|
104
|
+
return value as string[];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function parseLease(raw: string): RedisLeaseDocument {
|
|
108
|
+
const value: unknown = JSON.parse(raw);
|
|
109
|
+
if (
|
|
110
|
+
typeof value !== "object" ||
|
|
111
|
+
value === null ||
|
|
112
|
+
!("expiresAt" in value) ||
|
|
113
|
+
typeof value.expiresAt !== "number" ||
|
|
114
|
+
!("fence" in value) ||
|
|
115
|
+
typeof value.fence !== "number" ||
|
|
116
|
+
!("fields" in value) ||
|
|
117
|
+
!Array.isArray(value.fields) ||
|
|
118
|
+
value.fields.some((field) => typeof field !== "string") ||
|
|
119
|
+
!("id" in value) ||
|
|
120
|
+
typeof value.id !== "string" ||
|
|
121
|
+
!("values" in value) ||
|
|
122
|
+
!Array.isArray(value.values) ||
|
|
123
|
+
value.values.some((revision) => typeof revision !== "number")
|
|
124
|
+
) {
|
|
125
|
+
throw new Error("[furin-page-cache-redis] Invalid lease document.");
|
|
126
|
+
}
|
|
127
|
+
return value as RedisLeaseDocument;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function parseEntry(raw: string): PageCacheEntry {
|
|
131
|
+
const value: unknown = JSON.parse(raw);
|
|
132
|
+
if (
|
|
133
|
+
typeof value !== "object" ||
|
|
134
|
+
value === null ||
|
|
135
|
+
!("entry" in value) ||
|
|
136
|
+
typeof value.entry !== "object" ||
|
|
137
|
+
value.entry === null ||
|
|
138
|
+
!("cachedAt" in value.entry) ||
|
|
139
|
+
typeof value.entry.cachedAt !== "number" ||
|
|
140
|
+
!("payload" in value.entry) ||
|
|
141
|
+
typeof value.entry.payload !== "string" ||
|
|
142
|
+
!("revalidate" in value.entry) ||
|
|
143
|
+
(typeof value.entry.revalidate !== "number" && value.entry.revalidate !== null)
|
|
144
|
+
) {
|
|
145
|
+
throw new Error("[furin-page-cache-redis] Invalid cache entry document.");
|
|
146
|
+
}
|
|
147
|
+
return value.entry as PageCacheEntry;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export class RedisPageCache implements PageCacheAdapter {
|
|
151
|
+
private readonly client: RedisClient;
|
|
152
|
+
private readonly metadataRetentionMs: number;
|
|
153
|
+
private readonly prefix: string;
|
|
154
|
+
private readonly retentionMs: number;
|
|
155
|
+
|
|
156
|
+
constructor(options: RedisPageCacheOptions) {
|
|
157
|
+
assertNamespace(options.namespace);
|
|
158
|
+
const retentionMs = options.retentionMs ?? DEFAULT_RETENTION_MS;
|
|
159
|
+
if (
|
|
160
|
+
!Number.isSafeInteger(retentionMs) ||
|
|
161
|
+
retentionMs <= 0 ||
|
|
162
|
+
retentionMs > Number.MAX_SAFE_INTEGER - METADATA_GRACE_MS
|
|
163
|
+
) {
|
|
164
|
+
throw new Error("[furin-page-cache-redis] retentionMs must be a positive safe integer.");
|
|
165
|
+
}
|
|
166
|
+
this.client = options.client;
|
|
167
|
+
this.prefix = `furin:page:{${encodeURIComponent(options.namespace)}}`;
|
|
168
|
+
this.retentionMs = retentionMs;
|
|
169
|
+
this.metadataRetentionMs = retentionMs + METADATA_GRACE_MS;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async acquire({
|
|
173
|
+
identity,
|
|
174
|
+
leaseMs,
|
|
175
|
+
}: {
|
|
176
|
+
identity: PageCacheIdentity;
|
|
177
|
+
leaseMs: number;
|
|
178
|
+
}): Promise<PageCacheLease | null> {
|
|
179
|
+
const id = crypto.randomUUID();
|
|
180
|
+
const keys = [
|
|
181
|
+
this.leaseKey(identity),
|
|
182
|
+
this.fenceKey(identity),
|
|
183
|
+
this.versionsKey(identity.scope),
|
|
184
|
+
this.leasesKey(identity.scope),
|
|
185
|
+
...[...new Set(identity.tags)].map((tag) => this.tagLeasesKey(identity.scope, tag)),
|
|
186
|
+
];
|
|
187
|
+
const raw = await this.client.send("EVAL", [
|
|
188
|
+
ACQUIRE_PAGE_CACHE_LEASE_SCRIPT,
|
|
189
|
+
String(keys.length),
|
|
190
|
+
...keys,
|
|
191
|
+
id,
|
|
192
|
+
String(leaseMs),
|
|
193
|
+
JSON.stringify(selectorFields(identity)),
|
|
194
|
+
String(this.metadataRetentionMs),
|
|
195
|
+
identity.path,
|
|
196
|
+
JSON.stringify(identity.tags),
|
|
197
|
+
]);
|
|
198
|
+
if (raw === null) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
const lease = parseLease(stringResult(raw, "lease acquisition"));
|
|
202
|
+
return {
|
|
203
|
+
expiresAt: lease.expiresAt,
|
|
204
|
+
fence: lease.fence,
|
|
205
|
+
id: lease.id,
|
|
206
|
+
revision: JSON.stringify(lease.values),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async commit({
|
|
211
|
+
entry,
|
|
212
|
+
identity,
|
|
213
|
+
lease,
|
|
214
|
+
}: {
|
|
215
|
+
entry: PageCacheEntry;
|
|
216
|
+
identity: PageCacheIdentity;
|
|
217
|
+
lease: PageCacheLease;
|
|
218
|
+
}): Promise<"stored" | "superseded"> {
|
|
219
|
+
const tagKeys = [...new Set(identity.tags)].map((tag) => this.tagPathsKey(identity.scope, tag));
|
|
220
|
+
const entryKey = this.entryKey(identity);
|
|
221
|
+
const pathsKey = this.pathsKey(identity.scope);
|
|
222
|
+
const indexMember = JSON.stringify({
|
|
223
|
+
entryKey,
|
|
224
|
+
path: identity.path,
|
|
225
|
+
pathsKey,
|
|
226
|
+
tagKeys,
|
|
227
|
+
} satisfies RedisIndexMember);
|
|
228
|
+
const document: RedisEntryDocument = {
|
|
229
|
+
entry,
|
|
230
|
+
fields: selectorFields(identity),
|
|
231
|
+
indexMember,
|
|
232
|
+
pathsKey,
|
|
233
|
+
tagKeys,
|
|
234
|
+
values: JSON.parse(lease.revision) as number[],
|
|
235
|
+
};
|
|
236
|
+
const keys = [
|
|
237
|
+
this.leaseKey(identity),
|
|
238
|
+
entryKey,
|
|
239
|
+
this.versionsKey(identity.scope),
|
|
240
|
+
pathsKey,
|
|
241
|
+
this.leasesKey(identity.scope),
|
|
242
|
+
...tagKeys,
|
|
243
|
+
];
|
|
244
|
+
const result = await this.client.send("EVAL", [
|
|
245
|
+
COMMIT_PAGE_CACHE_ENTRY_SCRIPT,
|
|
246
|
+
String(keys.length),
|
|
247
|
+
...keys,
|
|
248
|
+
lease.id,
|
|
249
|
+
String(lease.fence),
|
|
250
|
+
JSON.stringify(document),
|
|
251
|
+
indexMember,
|
|
252
|
+
String(this.retentionMs),
|
|
253
|
+
String(this.metadataRetentionMs),
|
|
254
|
+
]);
|
|
255
|
+
if (result === "stored" || result === "superseded") {
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
throw new Error("[furin-page-cache-redis] Invalid cache commit response.");
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async invalidate(input: Parameters<PageCacheAdapter["invalidate"]>[0]) {
|
|
262
|
+
if (input.kind === "path") {
|
|
263
|
+
const path = normalizeInvalidationPath(input.path);
|
|
264
|
+
const result = stringArrayResult(
|
|
265
|
+
await this.client.send("EVAL", [
|
|
266
|
+
INVALIDATE_PAGE_CACHE_PATH_SCRIPT,
|
|
267
|
+
"3",
|
|
268
|
+
this.versionsKey(input.scope),
|
|
269
|
+
this.pathsKey(input.scope),
|
|
270
|
+
this.leasesKey(input.scope),
|
|
271
|
+
`${input.type}:${path}`,
|
|
272
|
+
path,
|
|
273
|
+
input.type,
|
|
274
|
+
String(this.metadataRetentionMs),
|
|
275
|
+
]),
|
|
276
|
+
"path invalidation"
|
|
277
|
+
);
|
|
278
|
+
return { invalidated: result.length > 0, paths: result };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const tags = [...new Set(input.tags)];
|
|
282
|
+
const keys = [
|
|
283
|
+
this.versionsKey(input.scope),
|
|
284
|
+
this.pathsKey(input.scope),
|
|
285
|
+
this.leasesKey(input.scope),
|
|
286
|
+
...tags.map((tag) => this.tagPathsKey(input.scope, tag)),
|
|
287
|
+
...tags.map((tag) => this.tagLeasesKey(input.scope, tag)),
|
|
288
|
+
];
|
|
289
|
+
const result = stringArrayResult(
|
|
290
|
+
await this.client.send("EVAL", [
|
|
291
|
+
INVALIDATE_PAGE_CACHE_TAGS_SCRIPT,
|
|
292
|
+
String(keys.length),
|
|
293
|
+
...keys,
|
|
294
|
+
JSON.stringify(tags),
|
|
295
|
+
String(this.metadataRetentionMs),
|
|
296
|
+
]),
|
|
297
|
+
"tag invalidation"
|
|
298
|
+
);
|
|
299
|
+
return { invalidated: result.length > 0, paths: result };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async read(identity: PageCacheIdentity): Promise<PageCacheEntry | null> {
|
|
303
|
+
const raw = await this.client.send("EVAL", [
|
|
304
|
+
READ_PAGE_CACHE_ENTRY_SCRIPT,
|
|
305
|
+
"2",
|
|
306
|
+
this.entryKey(identity),
|
|
307
|
+
this.versionsKey(identity.scope),
|
|
308
|
+
]);
|
|
309
|
+
if (raw === null) {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
return parseEntry(stringResult(raw, "cache read"));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async release({
|
|
316
|
+
identity,
|
|
317
|
+
lease,
|
|
318
|
+
}: {
|
|
319
|
+
identity: PageCacheIdentity;
|
|
320
|
+
lease: PageCacheLease;
|
|
321
|
+
}): Promise<void> {
|
|
322
|
+
await this.client.send("EVAL", [
|
|
323
|
+
RELEASE_PAGE_CACHE_LEASE_SCRIPT,
|
|
324
|
+
"2",
|
|
325
|
+
this.leaseKey(identity),
|
|
326
|
+
this.leasesKey(identity.scope),
|
|
327
|
+
lease.id,
|
|
328
|
+
String(lease.fence),
|
|
329
|
+
]);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private entryKey(identity: PageCacheIdentity): string {
|
|
333
|
+
return `${this.prefix}:entry:${identityDigest(identity)}`;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private fenceKey(identity: PageCacheIdentity): string {
|
|
337
|
+
return `${this.prefix}:fence:${identityDigest(identity)}`;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private leaseKey(identity: PageCacheIdentity): string {
|
|
341
|
+
return `${this.prefix}:lease:${identityDigest(identity)}`;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private pathsKey(scope: string): string {
|
|
345
|
+
return `${this.prefix}:paths:${digest(scope)}`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
private leasesKey(scope: string): string {
|
|
349
|
+
return `${this.prefix}:leases:${digest(scope)}`;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private tagPathsKey(scope: string, tag: string): string {
|
|
353
|
+
return `${this.prefix}:tag-paths:${digest(scope)}:${digest(tag)}`;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private tagLeasesKey(scope: string, tag: string): string {
|
|
357
|
+
return `${this.prefix}:tag-leases:${digest(scope)}:${digest(tag)}`;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
private versionsKey(scope: string): string {
|
|
361
|
+
return `${this.prefix}:versions:${digest(scope)}`;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function redisPageCache(options: RedisPageCacheOptions): PageCacheAdapter {
|
|
366
|
+
return new RedisPageCache(options);
|
|
367
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
export const ACQUIRE_PAGE_CACHE_LEASE_SCRIPT = `
|
|
2
|
+
local raw = redis.call('GET', KEYS[1])
|
|
3
|
+
local now_parts = redis.call('TIME')
|
|
4
|
+
local now = tonumber(now_parts[1]) * 1000 + math.floor(tonumber(now_parts[2]) / 1000)
|
|
5
|
+
if raw then
|
|
6
|
+
local active = cjson.decode(raw)
|
|
7
|
+
if active.expiresAt > now then return nil end
|
|
8
|
+
if active.indexMember then redis.call('ZREM', KEYS[4], active.indexMember) end
|
|
9
|
+
for _, key in ipairs(active.tagLeaseKeys or {}) do
|
|
10
|
+
redis.call('ZREM', key, active.indexMember)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
local fence = redis.call('INCR', KEYS[2])
|
|
14
|
+
redis.call('PEXPIRE', KEYS[2], ARGV[4])
|
|
15
|
+
local fields = cjson.decode(ARGV[3])
|
|
16
|
+
local values = {}
|
|
17
|
+
for index, field in ipairs(fields) do
|
|
18
|
+
values[index] = tonumber(redis.call('HGET', KEYS[3], field) or '0')
|
|
19
|
+
end
|
|
20
|
+
redis.call('PEXPIRE', KEYS[3], ARGV[4])
|
|
21
|
+
local tag_lease_keys = {}
|
|
22
|
+
for index = 5, #KEYS do table.insert(tag_lease_keys, KEYS[index]) end
|
|
23
|
+
local lease = {
|
|
24
|
+
tagLeaseKeys = tag_lease_keys,
|
|
25
|
+
expiresAt = now + tonumber(ARGV[2]),
|
|
26
|
+
fence = fence,
|
|
27
|
+
fields = fields,
|
|
28
|
+
id = ARGV[1],
|
|
29
|
+
indexMember = cjson.encode({id = ARGV[1], path = ARGV[5], tags = cjson.decode(ARGV[6])}),
|
|
30
|
+
values = values
|
|
31
|
+
}
|
|
32
|
+
redis.call('SET', KEYS[1], cjson.encode(lease), 'PX', ARGV[2])
|
|
33
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[4], '-inf', now)
|
|
34
|
+
redis.call('ZADD', KEYS[4], lease.expiresAt, lease.indexMember)
|
|
35
|
+
redis.call('PEXPIRE', KEYS[4], ARGV[4])
|
|
36
|
+
for _, key in ipairs(tag_lease_keys) do
|
|
37
|
+
redis.call('ZREMRANGEBYSCORE', key, '-inf', now)
|
|
38
|
+
redis.call('ZADD', key, lease.expiresAt, lease.indexMember)
|
|
39
|
+
redis.call('PEXPIRE', key, ARGV[4])
|
|
40
|
+
end
|
|
41
|
+
return cjson.encode(lease)
|
|
42
|
+
`;
|
|
43
|
+
|
|
44
|
+
export const COMMIT_PAGE_CACHE_ENTRY_SCRIPT = `
|
|
45
|
+
local raw = redis.call('GET', KEYS[1])
|
|
46
|
+
if not raw then return 'superseded' end
|
|
47
|
+
local lease = cjson.decode(raw)
|
|
48
|
+
local now_parts = redis.call('TIME')
|
|
49
|
+
local now = tonumber(now_parts[1]) * 1000 + math.floor(tonumber(now_parts[2]) / 1000)
|
|
50
|
+
if lease.id ~= ARGV[1] or lease.fence ~= tonumber(ARGV[2]) or lease.expiresAt <= now then
|
|
51
|
+
return 'superseded'
|
|
52
|
+
end
|
|
53
|
+
for index, field in ipairs(lease.fields) do
|
|
54
|
+
local current = tonumber(redis.call('HGET', KEYS[3], field) or '0')
|
|
55
|
+
if current ~= lease.values[index] then
|
|
56
|
+
redis.call('DEL', KEYS[1])
|
|
57
|
+
if lease.indexMember then redis.call('ZREM', KEYS[5], lease.indexMember) end
|
|
58
|
+
for _, key in ipairs(lease.tagLeaseKeys or {}) do redis.call('ZREM', key, lease.indexMember) end
|
|
59
|
+
return 'superseded'
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
local previous_raw = redis.call('GET', KEYS[2])
|
|
63
|
+
if previous_raw then
|
|
64
|
+
local previous = cjson.decode(previous_raw)
|
|
65
|
+
if previous.indexMember then
|
|
66
|
+
redis.call('ZREM', previous.pathsKey, previous.indexMember)
|
|
67
|
+
for _, tag_key in ipairs(previous.tagKeys) do
|
|
68
|
+
redis.call('ZREM', tag_key, previous.indexMember)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
local expires_at = now + tonumber(ARGV[5])
|
|
73
|
+
redis.call('SET', KEYS[2], ARGV[3], 'PX', ARGV[5])
|
|
74
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[4], '-inf', now)
|
|
75
|
+
redis.call('ZADD', KEYS[4], expires_at, ARGV[4])
|
|
76
|
+
redis.call('PEXPIRE', KEYS[4], ARGV[6])
|
|
77
|
+
for index = 6, #KEYS do
|
|
78
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[index], '-inf', now)
|
|
79
|
+
redis.call('ZADD', KEYS[index], expires_at, ARGV[4])
|
|
80
|
+
redis.call('PEXPIRE', KEYS[index], ARGV[6])
|
|
81
|
+
end
|
|
82
|
+
redis.call('PEXPIRE', KEYS[3], ARGV[6])
|
|
83
|
+
redis.call('DEL', KEYS[1])
|
|
84
|
+
if lease.indexMember then redis.call('ZREM', KEYS[5], lease.indexMember) end
|
|
85
|
+
for _, key in ipairs(lease.tagLeaseKeys or {}) do redis.call('ZREM', key, lease.indexMember) end
|
|
86
|
+
return 'stored'
|
|
87
|
+
`;
|
|
88
|
+
|
|
89
|
+
export const READ_PAGE_CACHE_ENTRY_SCRIPT = `
|
|
90
|
+
local raw = redis.call('GET', KEYS[1])
|
|
91
|
+
if not raw then return nil end
|
|
92
|
+
local value = cjson.decode(raw)
|
|
93
|
+
for index, field in ipairs(value.fields) do
|
|
94
|
+
local current = tonumber(redis.call('HGET', KEYS[2], field) or '0')
|
|
95
|
+
if current ~= value.values[index] then
|
|
96
|
+
redis.call('DEL', KEYS[1])
|
|
97
|
+
if value.indexMember then
|
|
98
|
+
redis.call('ZREM', value.pathsKey, value.indexMember)
|
|
99
|
+
for _, tag_key in ipairs(value.tagKeys) do
|
|
100
|
+
redis.call('ZREM', tag_key, value.indexMember)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
return nil
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
return raw
|
|
107
|
+
`;
|
|
108
|
+
|
|
109
|
+
export const RELEASE_PAGE_CACHE_LEASE_SCRIPT = `
|
|
110
|
+
local raw = redis.call('GET', KEYS[1])
|
|
111
|
+
if not raw then return 0 end
|
|
112
|
+
local lease = cjson.decode(raw)
|
|
113
|
+
if lease.id == ARGV[1] and lease.fence == tonumber(ARGV[2]) then
|
|
114
|
+
if lease.indexMember then redis.call('ZREM', KEYS[2], lease.indexMember) end
|
|
115
|
+
for _, key in ipairs(lease.tagLeaseKeys or {}) do redis.call('ZREM', key, lease.indexMember) end
|
|
116
|
+
return redis.call('DEL', KEYS[1])
|
|
117
|
+
end
|
|
118
|
+
return 0
|
|
119
|
+
`;
|
|
120
|
+
|
|
121
|
+
export const INVALIDATE_PAGE_CACHE_PATH_SCRIPT = `
|
|
122
|
+
redis.call('HINCRBY', KEYS[1], ARGV[1], 1)
|
|
123
|
+
redis.call('PEXPIRE', KEYS[1], ARGV[4])
|
|
124
|
+
local now_parts = redis.call('TIME')
|
|
125
|
+
local now = tonumber(now_parts[1]) * 1000 + math.floor(tonumber(now_parts[2]) / 1000)
|
|
126
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', now)
|
|
127
|
+
local members = redis.call('ZRANGE', KEYS[2], 0, -1)
|
|
128
|
+
local seen = {}
|
|
129
|
+
local affected = {}
|
|
130
|
+
local function matches_path(path)
|
|
131
|
+
if path == ARGV[2] then return true end
|
|
132
|
+
return ARGV[3] == 'layout' and (ARGV[2] == '/' or string.sub(path, 1, string.len(ARGV[2]) + 1) == ARGV[2] .. '/')
|
|
133
|
+
end
|
|
134
|
+
for _, member in ipairs(members) do
|
|
135
|
+
local index = cjson.decode(member)
|
|
136
|
+
if matches_path(index.path) then
|
|
137
|
+
redis.call('DEL', index.entryKey)
|
|
138
|
+
redis.call('ZREM', index.pathsKey, member)
|
|
139
|
+
for _, tag_key in ipairs(index.tagKeys) do
|
|
140
|
+
redis.call('ZREM', tag_key, member)
|
|
141
|
+
end
|
|
142
|
+
if not seen[index.path] then
|
|
143
|
+
seen[index.path] = true
|
|
144
|
+
table.insert(affected, index.path)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[3], '-inf', now)
|
|
149
|
+
for _, member in ipairs(redis.call('ZRANGE', KEYS[3], 0, -1)) do
|
|
150
|
+
local lease = cjson.decode(member)
|
|
151
|
+
if matches_path(lease.path) and not seen[lease.path] then
|
|
152
|
+
seen[lease.path] = true
|
|
153
|
+
table.insert(affected, lease.path)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
return affected
|
|
157
|
+
`;
|
|
158
|
+
|
|
159
|
+
export const INVALIDATE_PAGE_CACHE_TAGS_SCRIPT = `
|
|
160
|
+
local tags = cjson.decode(ARGV[1])
|
|
161
|
+
for _, tag in ipairs(tags) do
|
|
162
|
+
redis.call('HINCRBY', KEYS[1], 'tag:' .. tag, 1)
|
|
163
|
+
end
|
|
164
|
+
redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
|
165
|
+
local now_parts = redis.call('TIME')
|
|
166
|
+
local now = tonumber(now_parts[1]) * 1000 + math.floor(tonumber(now_parts[2]) / 1000)
|
|
167
|
+
local seen_members = {}
|
|
168
|
+
local seen_paths = {}
|
|
169
|
+
local affected = {}
|
|
170
|
+
for key_index = 4, 3 + #tags do
|
|
171
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[key_index], '-inf', now)
|
|
172
|
+
local members = redis.call('ZRANGE', KEYS[key_index], 0, -1)
|
|
173
|
+
for _, member in ipairs(members) do
|
|
174
|
+
if not seen_members[member] then
|
|
175
|
+
seen_members[member] = true
|
|
176
|
+
local index = cjson.decode(member)
|
|
177
|
+
redis.call('DEL', index.entryKey)
|
|
178
|
+
redis.call('ZREM', index.pathsKey, member)
|
|
179
|
+
for _, tag_key in ipairs(index.tagKeys) do
|
|
180
|
+
redis.call('ZREM', tag_key, member)
|
|
181
|
+
end
|
|
182
|
+
if not seen_paths[index.path] then
|
|
183
|
+
seen_paths[index.path] = true
|
|
184
|
+
table.insert(affected, index.path)
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
for key_index = 4 + #tags, #KEYS do
|
|
190
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[key_index], '-inf', now)
|
|
191
|
+
for _, member in ipairs(redis.call('ZRANGE', KEYS[key_index], 0, -1)) do
|
|
192
|
+
local lease = cjson.decode(member)
|
|
193
|
+
if not seen_paths[lease.path] then
|
|
194
|
+
seen_paths[lease.path] = true
|
|
195
|
+
table.insert(affected, lease.path)
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
return affected
|
|
200
|
+
`;
|
|
@@ -2,12 +2,12 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
2
2
|
import type { RequestLogger } from "evlog";
|
|
3
3
|
// biome-ignore lint/style/noExportedImports: used locally and re-exported for consumers
|
|
4
4
|
import { createLogger } from "evlog";
|
|
5
|
-
import {
|
|
5
|
+
import { getRequestLogger } from "./evlog.ts";
|
|
6
6
|
|
|
7
7
|
export { createLogger };
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
* Fallback used when
|
|
10
|
+
* Fallback used when getLogger() is called completely outside any context
|
|
11
11
|
* (not in a live request, not in a synthetic render scope).
|
|
12
12
|
*/
|
|
13
13
|
const noopLogger: RequestLogger = {
|
|
@@ -40,9 +40,9 @@ const syntheticRenderStorage = new AsyncLocalStorage<RequestLogger>();
|
|
|
40
40
|
* Import from `@teyik0/furin` instead of `evlog/elysia` so this fallback chain
|
|
41
41
|
* applies in all rendering contexts.
|
|
42
42
|
*/
|
|
43
|
-
export function
|
|
43
|
+
export function getLogger(): RequestLogger {
|
|
44
44
|
try {
|
|
45
|
-
return
|
|
45
|
+
return getRequestLogger();
|
|
46
46
|
} catch {
|
|
47
47
|
return syntheticRenderStorage.getStore() ?? noopLogger;
|
|
48
48
|
}
|
|
@@ -52,7 +52,7 @@ export function useLogger(): RequestLogger {
|
|
|
52
52
|
* Runs `fn` inside a synthetic render scope.
|
|
53
53
|
*
|
|
54
54
|
* Creates a detached `createLogger()` instance for the duration of `fn`.
|
|
55
|
-
* `
|
|
55
|
+
* `getLogger()` calls inside `fn` (including user loaders) return this logger
|
|
56
56
|
* instead of throwing. On completion, the accumulated wide event is emitted to
|
|
57
57
|
* the global drain with the provided initial context (e.g. route pattern).
|
|
58
58
|
*
|
package/src/server/dev/plugin.ts
CHANGED
|
@@ -14,6 +14,15 @@ interface EmbeddedDiagnosticState {
|
|
|
14
14
|
event: Extract<DevDiagnosticEvent, { type: "error" }>;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
export const extensionErrorFilterScript = `window.addEventListener("unhandledrejection", (event) => {
|
|
18
|
+
const stack = event.reason?.stack;
|
|
19
|
+
if (typeof stack !== "string") return;
|
|
20
|
+
const firstFrame = stack.match(/^(?:\\s*at [^\\n]*|[^\\n]*@[^\\n]*)/m)?.[0];
|
|
21
|
+
if (firstFrame && /\\b(?:chrome|moz)-extension:\\/\\//.test(firstFrame)) {
|
|
22
|
+
event.stopImmediatePropagation();
|
|
23
|
+
}
|
|
24
|
+
}, true);`;
|
|
25
|
+
|
|
17
26
|
let overlayClientSource: string | undefined;
|
|
18
27
|
|
|
19
28
|
async function clientSource(): Promise<string> {
|
|
@@ -118,7 +127,7 @@ export function renderDevDiagnosticResponse(
|
|
|
118
127
|
): Response {
|
|
119
128
|
const state = serializeForHtml({ basePath, event });
|
|
120
129
|
return new Response(
|
|
121
|
-
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Furin development error</title>${browserEventsClientScript(basePath)}</head><body><script id="__FURIN_DEV_DIAGNOSTIC__" type="application/json">${state}</script><script type="module" src="${basePath}/_furin/dev/overlay.js"></script></body></html>`,
|
|
130
|
+
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Furin development error</title><script data-furin-extension-error-filter="">${extensionErrorFilterScript}</script>${browserEventsClientScript(basePath)}</head><body><script id="__FURIN_DEV_DIAGNOSTIC__" type="application/json">${state}</script><script type="module" src="${basePath}/_furin/dev/overlay.js"></script></body></html>`,
|
|
122
131
|
{
|
|
123
132
|
headers: {
|
|
124
133
|
"cache-control": "no-store",
|
|
@@ -19,6 +19,10 @@ export function devDiagnosticStore(): undefined {
|
|
|
19
19
|
// Development-only state is absent from production bundles.
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
export function devGraph(): never {
|
|
23
|
+
throw new Error("[furin] Development route state is unavailable in production.");
|
|
24
|
+
}
|
|
25
|
+
|
|
22
26
|
export function injectDevDiagnosticClient(html: string, _basePath: string): string {
|
|
23
27
|
return html;
|
|
24
28
|
}
|