@rangojs/router 0.0.0-experimental.138 → 0.0.0-experimental.139

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.
@@ -0,0 +1,948 @@
1
+ /**
2
+ * Vercel Runtime Cache Store
3
+ *
4
+ * A SegmentCacheStore backed by Vercel's Runtime Cache (`getCache()` from
5
+ * `@vercel/functions`). It is the production store analogue to CFCacheStore, but
6
+ * far smaller: Vercel's Runtime Cache already IS a distributed, tag-aware cache
7
+ * (regional storage, global tag-expire within ~300ms), so none of CFCacheStore's
8
+ * L1/L2 tiering, KV tag-marker comparison, marker memoization, or per-tier
9
+ * timeout budgets are needed here - the platform does that work. What this store
10
+ * adds on top of the raw primitive is the parts Vercel does NOT give us:
11
+ *
12
+ * 1. Stale-while-revalidate. `getCache` has no stale-but-serve: a TTL'd entry
13
+ * simply becomes a miss. We store our own {staleAt, expiresAt} envelope and
14
+ * set the Vercel ttl to (ttl + swr) so the entry survives its SWR window,
15
+ * computing staleness ourselves via the shared cache-policy helpers.
16
+ * 2. A single-keyspace family split. `getCache` is one flat keyspace, whereas
17
+ * the interface has three value families (segments, "use cache" items, full
18
+ * responses). The memory store keeps three separate Maps; here they must be
19
+ * namespaced by a prefix or a set() of a response would clobber a segment
20
+ * cached under the same router key.
21
+ * 3. The platform guardrails: the 2 MB per-item ceiling (oversized writes
22
+ * silently no-op on Vercel, so we skip + report), the per-item tag cap, and
23
+ * cross-deploy non-reconciliation (TTL/tag updates are not reconciled across
24
+ * deployments - bake a build id into `version` or the getCache namespace).
25
+ *
26
+ * Dependency stance: this module imports NOTHING from `@vercel/functions`. The
27
+ * runtime cache handle (and `waitUntil`) are injected through the constructor and
28
+ * typed against the local VercelRuntimeCache shape below, exactly as CFCacheStore
29
+ * takes its `kv`/`ctx` bindings by injection. The consumer passes the real
30
+ * `getCache(...)` result, which satisfies the shape structurally. That keeps the
31
+ * router free of a hard Vercel dependency.
32
+ */
33
+
34
+ import type {
35
+ SegmentCacheStore,
36
+ CachedEntryData,
37
+ CacheDefaults,
38
+ CacheGetResult,
39
+ CacheItemResult,
40
+ CacheItemOptions,
41
+ } from "../types.js";
42
+ import type { RequestContext } from "../../server/request-context.js";
43
+ import { isPerClientSignalHeader } from "../../browser/cookie-name.js";
44
+ import {
45
+ resolveTtl,
46
+ resolveSwrWindow,
47
+ computeExpiration,
48
+ DEFAULT_FUNCTION_TTL,
49
+ } from "../cache-policy.js";
50
+ import { reportCacheError, reportingAsync } from "../cache-error.js";
51
+ import type { CacheErrorCategory } from "../cache-error.js";
52
+
53
+ /**
54
+ * Minimal structural shape of the Vercel Runtime Cache returned by `getCache()`
55
+ * from `@vercel/functions`. Declared locally so @rangojs/router carries no hard
56
+ * dependency on `@vercel/functions`; the real handle satisfies it structurally.
57
+ *
58
+ * @see https://vercel.com/docs/functions/functions-api-reference/vercel-functions-package#getcache
59
+ */
60
+ export interface VercelRuntimeCache {
61
+ /** Returns the stored value, or a nullish value on miss. Never throws on miss. */
62
+ get(key: string): Promise<unknown>;
63
+ /** Stores a value with optional TTL (seconds), tags, and observability name. */
64
+ set(
65
+ key: string,
66
+ value: unknown,
67
+ options?: { ttl?: number; tags?: string[]; name?: string },
68
+ ): Promise<void>;
69
+ /** Removes a value by key. */
70
+ delete(key: string): Promise<void>;
71
+ /** Expires every entry tagged with any of `tag`. Global, ~300ms propagation. */
72
+ expireTag(tag: string | string[]): Promise<void>;
73
+ }
74
+
75
+ /**
76
+ * Vercel Runtime Cache hard per-item ceiling: 2 MB. Writes above this silently
77
+ * no-op on the platform, so the store measures the serialized envelope and skips
78
+ * (fail-open) entries at or above this size.
79
+ */
80
+ export const VERCEL_MAX_ITEM_BYTES: number = 2 * 1024 * 1024;
81
+
82
+ /**
83
+ * Per-item tag ceiling. Vercel's getCache API reference lists 128 tags per item
84
+ * (https://vercel.com/docs/functions/functions-api-reference/vercel-functions-package#getcache).
85
+ * Tags beyond this are dropped with a warning at write time. Does NOT cap
86
+ * invalidateTags() - an invalidation must reach every requested tag.
87
+ */
88
+ export const VERCEL_MAX_TAGS_PER_ITEM: number = 128;
89
+
90
+ /** Max tag length in UTF-8 bytes accepted by Vercel; longer tags are skipped. */
91
+ export const VERCEL_MAX_TAG_BYTES: number = 256;
92
+
93
+ /**
94
+ * URL metacharacters a tag must not contain. The official `@vercel/functions`
95
+ * client interpolates the tag straight into `revalidate?tags=${tag}` with NO
96
+ * URL-encoding (and also sends it in a request header), so a tag carrying one of
97
+ * these is stored intact on write but silently mangled server-side on
98
+ * invalidate: `&` starts a new query param, `?` a query, `#` truncates as a
99
+ * fragment, `%` is read as a (bad) percent-escape, `,` splits the tag list. The
100
+ * entry then never clears while `updateTag()`/`expireTag()` report success -
101
+ * the exact false-success the store's one deliberate throw exists to prevent.
102
+ * Such tags are dropped symmetrically on both the write and invalidate paths.
103
+ */
104
+ const VERCEL_UNSAFE_TAG_CHARS = /[,&#%?]/;
105
+
106
+ /**
107
+ * Herd-dampening window (ms). On a stale read the store pushes the entry's
108
+ * staleAt forward by this much and re-writes it, so other readers in the same
109
+ * region briefly see it as fresh while one revalidates. Best-effort and
110
+ * non-atomic: `getCache` has no compare-and-set, and storage is regional, so a
111
+ * race can still let two readers both trigger revalidation. Matches the intent of
112
+ * CFCacheStore's MAX_REVALIDATION_INTERVAL without its Cache-API atomicity.
113
+ */
114
+ const REVALIDATION_LOCK_MS = 30_000;
115
+
116
+ /** Family prefixes that keep the three value tiers from colliding in the single
117
+ * Vercel keyspace. The router's own semantic prefixes (doc:/partial:/use-cache:)
118
+ * become the suffix; `rg:` namespaces every Rango entry. */
119
+ type CacheFamily = "s" | "i" | "r";
120
+
121
+ /** Stored envelope for a segment-tree entry (get/set). */
122
+ interface VercelSegmentEnvelope {
123
+ /** The cached entry data. */
124
+ d: CachedEntryData;
125
+ /** staleAt (ms since epoch). */
126
+ s: number;
127
+ /** expiresAt (ms since epoch). */
128
+ e: number;
129
+ }
130
+
131
+ /** Stored envelope for a "use cache" function result (getItem/setItem). */
132
+ interface VercelItemEnvelope {
133
+ /** RSC-serialized value. */
134
+ v: string;
135
+ /** RSC-encoded handle blob. */
136
+ h?: string;
137
+ /** staleAt (ms since epoch). */
138
+ s: number;
139
+ /** expiresAt (ms since epoch). */
140
+ e: number;
141
+ /** Tags, surfaced on read so a hit still contributes to the document tag set. */
142
+ t?: string[];
143
+ }
144
+
145
+ /** Stored envelope for a full Response (getResponse/putResponse). */
146
+ interface VercelResponseEnvelope {
147
+ /** base64-encoded body bytes. */
148
+ b: string;
149
+ /** HTTP status. */
150
+ st: number;
151
+ /** Header entries (per-client signal headers already stripped). */
152
+ hd: [string, string][];
153
+ /** staleAt (ms since epoch). */
154
+ s: number;
155
+ /** expiresAt (ms since epoch). */
156
+ e: number;
157
+ /** Tags, preserved so a stale re-stamp keeps them. */
158
+ t?: string[];
159
+ }
160
+
161
+ /** Read-path outcome for the debug sink. */
162
+ export type VercelCacheReadOutcome =
163
+ | "miss"
164
+ | "fresh"
165
+ | "stale-revalidate"
166
+ | "expired"
167
+ | "corrupt"
168
+ | "error";
169
+
170
+ /** Diagnostic event emitted on every read when `debug` is set. */
171
+ export interface VercelCacheReadDebugEvent {
172
+ op: "get" | "getItem" | "getResponse";
173
+ key: string;
174
+ outcome: VercelCacheReadOutcome;
175
+ staleAt?: number;
176
+ expiresAt?: number;
177
+ shouldRevalidate?: boolean;
178
+ /** Wall-clock ms spent in the backing cache.get(). */
179
+ readMs?: number;
180
+ }
181
+
182
+ /** `true` logs each read outcome; a function receives the structured event. */
183
+ export type VercelCacheDebug =
184
+ | boolean
185
+ | ((event: VercelCacheReadDebugEvent) => void);
186
+
187
+ /**
188
+ * Options for VercelCacheStore.
189
+ */
190
+ export interface VercelCacheStoreOptions<TEnv = unknown> {
191
+ /**
192
+ * The Vercel Runtime Cache handle - `getCache()` from `@vercel/functions`.
193
+ * Required. Construct it with a build-hash namespace to bust stale-shaped
194
+ * entries across deployments, since Vercel does not reconcile TTL/tags between
195
+ * deploys:
196
+ *
197
+ * ```ts
198
+ * import { getCache } from "@vercel/functions";
199
+ * new VercelCacheStore({ cache: getCache({ namespace: BUILD_ID }) });
200
+ * ```
201
+ */
202
+ cache: VercelRuntimeCache;
203
+
204
+ /**
205
+ * `waitUntil` from `@vercel/functions`. Used only to run the stale-read
206
+ * re-stamp (herd dampening) off the response path - the router already
207
+ * backgrounds the actual writes. When omitted, the re-stamp runs detached
208
+ * (fire-and-forget) instead.
209
+ */
210
+ waitUntil?: (promise: Promise<unknown>) => void;
211
+
212
+ /**
213
+ * Default ttl/swr for cache() boundaries when not explicitly specified.
214
+ */
215
+ defaults?: CacheDefaults;
216
+
217
+ /**
218
+ * Custom key generator applied to all cache operations (region/locale/user
219
+ * segmentation). Receives the request context and the default key.
220
+ */
221
+ keyGenerator?: (
222
+ ctx: RequestContext<TEnv>,
223
+ defaultKey: string,
224
+ ) => string | Promise<string>;
225
+
226
+ /**
227
+ * Build/version id folded into every stored key as `v/{version}/...`. A second
228
+ * cross-deploy busting layer in addition to (or instead of) the getCache
229
+ * namespace. Changing it invalidates everything this store wrote previously.
230
+ */
231
+ version?: string;
232
+
233
+ /**
234
+ * Max serialized entry size in bytes before a write is skipped. Defaults to
235
+ * VERCEL_MAX_ITEM_BYTES (2 MB - the platform's hard cap).
236
+ */
237
+ maxItemBytes?: number;
238
+
239
+ /**
240
+ * Human-readable label passed as the Vercel `set({ name })` option for
241
+ * observability in the Vercel dashboard.
242
+ */
243
+ name?: string;
244
+
245
+ /**
246
+ * Read diagnostics. `true` logs each read outcome to the console; a function
247
+ * receives the structured VercelCacheReadDebugEvent.
248
+ */
249
+ debug?: VercelCacheDebug;
250
+ }
251
+
252
+ function isRecord(value: unknown): value is Record<string, unknown> {
253
+ return typeof value === "object" && value !== null;
254
+ }
255
+
256
+ /** Encode binary body bytes to base64 in chunks (avoids call-stack blowups). */
257
+ function bufferToBase64(buffer: ArrayBuffer): string {
258
+ const bytes = new Uint8Array(buffer);
259
+ let binary = "";
260
+ const CHUNK = 0x8000;
261
+ for (let i = 0; i < bytes.length; i += CHUNK) {
262
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
263
+ }
264
+ return btoa(binary);
265
+ }
266
+
267
+ /** Decode a base64 body back into bytes. */
268
+ function base64ToBuffer(b64: string): ArrayBuffer {
269
+ const binary = atob(b64);
270
+ const bytes = new Uint8Array(binary.length);
271
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
272
+ return bytes.buffer;
273
+ }
274
+
275
+ /**
276
+ * Vercel Runtime Cache-backed segment cache store.
277
+ *
278
+ * Suitable for production deployments on Vercel Functions (Node runtime). The
279
+ * store is best-effort: every read failure degrades to a miss and every write
280
+ * failure to a no-op (reported, never thrown) - the sole exception is
281
+ * invalidateTags(), which rejects when the injected cache's expireTag() rejects
282
+ * so an awaited updateTag() can surface the failure.
283
+ *
284
+ * Read-your-own-writes caveat: this honesty is only as strong as the cache
285
+ * handle. The official `@vercel/functions` cache (`BuildCache.expireTag`)
286
+ * CATCHES backend failures and resolves, so with the official client a failed
287
+ * expireTag is invisible here and an awaited updateTag() can report success that
288
+ * did not happen. Treat tag invalidation on the official Vercel client as
289
+ * best-effort, not a hard read-your-own-writes guarantee. (A custom cache handle
290
+ * that propagates expireTag rejections does get the strict guarantee, which is
291
+ * what the unit suite exercises.)
292
+ *
293
+ * Key hashing / family isolation. The store namespaces its three value tiers as
294
+ * `rg:{s|i|r}:{key}` so segment, `"use cache"`, and response entries occupy
295
+ * disjoint keyspaces. That separation is only as strong as the cache's
296
+ * `keyHashFunction`: `getCache`'s default is djb2, which folds every key to 32
297
+ * bits (8 hex chars), so at a large live-key count a collision can let one
298
+ * entry's body be served for another key (same-family) or be misread as corrupt
299
+ * (cross-family). For deployments with many cached keys, pass a wider hash so
300
+ * the family split is real isolation, e.g. `getCache({ keyHashFunction })` with
301
+ * a sha256-based function.
302
+ *
303
+ * @example
304
+ * ```ts
305
+ * import { getCache, waitUntil } from "@vercel/functions";
306
+ * import { VercelCacheStore } from "@rangojs/router/cache";
307
+ *
308
+ * export const router = createRouter({
309
+ * cache: () => ({
310
+ * store: new VercelCacheStore({
311
+ * // `process.env` (not `import.meta.env`, which needs a VITE_ prefix and
312
+ * // would be undefined here) so cross-deploy busting via `version` works.
313
+ * cache: getCache({ namespace: process.env.VERCEL_DEPLOYMENT_ID }),
314
+ * waitUntil,
315
+ * defaults: { ttl: 60, swr: 300 },
316
+ * }),
317
+ * }),
318
+ * });
319
+ * ```
320
+ */
321
+ export class VercelCacheStore<
322
+ TEnv = unknown,
323
+ > implements SegmentCacheStore<TEnv> {
324
+ readonly defaults?: CacheDefaults;
325
+ readonly keyGenerator?: (
326
+ ctx: RequestContext<TEnv>,
327
+ defaultKey: string,
328
+ ) => string | Promise<string>;
329
+
330
+ private readonly cache: VercelRuntimeCache;
331
+ private readonly waitUntil?: (promise: Promise<unknown>) => void;
332
+ private readonly version?: string;
333
+ private readonly maxItemBytes: number;
334
+ private readonly name?: string;
335
+ private readonly debug?: VercelCacheDebug;
336
+
337
+ constructor(options: VercelCacheStoreOptions<TEnv>) {
338
+ if (!options || !options.cache) {
339
+ throw new Error(
340
+ "[VercelCacheStore] requires `cache` (the getCache() handle from @vercel/functions)",
341
+ );
342
+ }
343
+ this.cache = options.cache;
344
+ this.waitUntil = options.waitUntil;
345
+ this.defaults = options.defaults;
346
+ this.keyGenerator = options.keyGenerator;
347
+ this.version = options.version;
348
+ this.maxItemBytes = options.maxItemBytes ?? VERCEL_MAX_ITEM_BYTES;
349
+ this.name = options.name;
350
+ this.debug = options.debug;
351
+ }
352
+
353
+ // --- Segment family (get/set/delete) ---
354
+
355
+ async get(key: string): Promise<CacheGetResult | null> {
356
+ const storeKey = this.toStoreKey(key, "s");
357
+ const started = Date.now();
358
+ let raw: unknown;
359
+ try {
360
+ raw = await this.cache.get(storeKey);
361
+ } catch (error) {
362
+ reportCacheError(error, "cache-read", "[VercelCacheStore] get");
363
+ this.emitDebug({ op: "get", key, outcome: "error" });
364
+ return null;
365
+ }
366
+ const readMs = Date.now() - started;
367
+
368
+ if (raw == null) {
369
+ this.emitDebug({ op: "get", key, outcome: "miss", readMs });
370
+ return null;
371
+ }
372
+ const env = this.asSegmentEnvelope(raw);
373
+ if (!env) {
374
+ reportCacheError(
375
+ new Error("malformed segment envelope"),
376
+ "cache-corrupt",
377
+ "[VercelCacheStore] get",
378
+ );
379
+ void this.safeDelete(storeKey);
380
+ this.emitDebug({ op: "get", key, outcome: "corrupt", readMs });
381
+ return null;
382
+ }
383
+
384
+ const now = Date.now();
385
+ if (now > env.e) {
386
+ void this.safeDelete(storeKey);
387
+ this.emitDebug({
388
+ op: "get",
389
+ key,
390
+ outcome: "expired",
391
+ staleAt: env.s,
392
+ expiresAt: env.e,
393
+ readMs,
394
+ });
395
+ return null;
396
+ }
397
+
398
+ const isStale = env.s > 0 && now > env.s;
399
+ if (isStale) {
400
+ this.markRevalidating(
401
+ storeKey,
402
+ env,
403
+ env.d.tags,
404
+ "[VercelCacheStore] get",
405
+ );
406
+ this.emitDebug({
407
+ op: "get",
408
+ key,
409
+ outcome: "stale-revalidate",
410
+ shouldRevalidate: true,
411
+ staleAt: env.s,
412
+ expiresAt: env.e,
413
+ readMs,
414
+ });
415
+ return { data: env.d, shouldRevalidate: true };
416
+ }
417
+
418
+ this.emitDebug({
419
+ op: "get",
420
+ key,
421
+ outcome: "fresh",
422
+ shouldRevalidate: false,
423
+ staleAt: env.s,
424
+ expiresAt: env.e,
425
+ readMs,
426
+ });
427
+ return { data: env.d, shouldRevalidate: false };
428
+ }
429
+
430
+ async set(
431
+ key: string,
432
+ data: CachedEntryData,
433
+ ttl: number,
434
+ swr?: number,
435
+ ): Promise<void> {
436
+ try {
437
+ const swrWindow = resolveSwrWindow(swr, this.defaults);
438
+ const { staleAt, expiresAt } = computeExpiration(ttl, swrWindow);
439
+ // Embed the CLAMPED tags, like putResponse/setItem: the segment family
440
+ // carries its tags inside `d`, and a raw list would resurface dropped
441
+ // tags into recordRequestTags on every hit AND get re-clamped (with a
442
+ // spurious cache-write report) by markRevalidating on every stale read.
443
+ const safeTags = this.clampTagsForWrite(
444
+ data.tags,
445
+ "[VercelCacheStore] set",
446
+ );
447
+ const d: CachedEntryData = data.tags
448
+ ? { ...data, tags: safeTags.length > 0 ? safeTags : undefined }
449
+ : data;
450
+ const env: VercelSegmentEnvelope = { d, s: staleAt, e: expiresAt };
451
+ await this.write(
452
+ this.toStoreKey(key, "s"),
453
+ env,
454
+ ttl + swrWindow,
455
+ safeTags,
456
+ "[VercelCacheStore] set",
457
+ );
458
+ } catch (error) {
459
+ reportCacheError(error, "cache-write", "[VercelCacheStore] set");
460
+ }
461
+ }
462
+
463
+ async delete(key: string): Promise<boolean> {
464
+ try {
465
+ await this.cache.delete(this.toStoreKey(key, "s"));
466
+ // Vercel's delete returns void; we cannot know whether the key existed.
467
+ // The router uses the boolean only for self-heal eviction, so report success.
468
+ return true;
469
+ } catch (error) {
470
+ reportCacheError(error, "cache-delete", "[VercelCacheStore] delete");
471
+ return false;
472
+ }
473
+ }
474
+
475
+ // --- Response family (getResponse/putResponse) ---
476
+
477
+ async getResponse(
478
+ key: string,
479
+ ): Promise<{ response: Response; shouldRevalidate: boolean } | null> {
480
+ const storeKey = this.toStoreKey(key, "r");
481
+ const started = Date.now();
482
+ let raw: unknown;
483
+ try {
484
+ raw = await this.cache.get(storeKey);
485
+ } catch (error) {
486
+ reportCacheError(error, "cache-read", "[VercelCacheStore] getResponse");
487
+ this.emitDebug({ op: "getResponse", key, outcome: "error" });
488
+ return null;
489
+ }
490
+ const readMs = Date.now() - started;
491
+
492
+ if (raw == null) {
493
+ this.emitDebug({ op: "getResponse", key, outcome: "miss", readMs });
494
+ return null;
495
+ }
496
+ const env = this.asResponseEnvelope(raw);
497
+ if (!env) {
498
+ reportCacheError(
499
+ new Error("malformed response envelope"),
500
+ "cache-corrupt",
501
+ "[VercelCacheStore] getResponse",
502
+ );
503
+ void this.safeDelete(storeKey);
504
+ this.emitDebug({ op: "getResponse", key, outcome: "corrupt", readMs });
505
+ return null;
506
+ }
507
+
508
+ const now = Date.now();
509
+ if (now > env.e) {
510
+ void this.safeDelete(storeKey);
511
+ this.emitDebug({
512
+ op: "getResponse",
513
+ key,
514
+ outcome: "expired",
515
+ staleAt: env.s,
516
+ expiresAt: env.e,
517
+ readMs,
518
+ });
519
+ return null;
520
+ }
521
+
522
+ // Reconstruct the Response BEFORE marking revalidating. A corrupt body (e.g.
523
+ // invalid base64 from base64ToBuffer, or bad header entries) would otherwise
524
+ // throw out of getResponse — breaking the fail-open contract — and a stale
525
+ // re-stamp would re-persist the bad value. Treat a reconstruction failure as
526
+ // a corrupt entry: report, evict, and miss.
527
+ let response: Response;
528
+ try {
529
+ response = new Response(base64ToBuffer(env.b), {
530
+ status: env.st,
531
+ headers: new Headers(env.hd),
532
+ });
533
+ } catch (error) {
534
+ reportCacheError(
535
+ error,
536
+ "cache-corrupt",
537
+ "[VercelCacheStore] getResponse",
538
+ );
539
+ void this.safeDelete(storeKey);
540
+ this.emitDebug({ op: "getResponse", key, outcome: "corrupt", readMs });
541
+ return null;
542
+ }
543
+
544
+ const isStale = env.s > 0 && now > env.s;
545
+ if (isStale) {
546
+ this.markRevalidating(
547
+ storeKey,
548
+ env,
549
+ env.t,
550
+ "[VercelCacheStore] getResponse",
551
+ );
552
+ }
553
+ this.emitDebug({
554
+ op: "getResponse",
555
+ key,
556
+ outcome: isStale ? "stale-revalidate" : "fresh",
557
+ shouldRevalidate: isStale,
558
+ staleAt: env.s,
559
+ expiresAt: env.e,
560
+ readMs,
561
+ });
562
+ return { response, shouldRevalidate: isStale };
563
+ }
564
+
565
+ async putResponse(
566
+ key: string,
567
+ response: Response,
568
+ ttl: number,
569
+ swr?: number,
570
+ tags?: string[],
571
+ ): Promise<void> {
572
+ try {
573
+ const body = await response.clone().arrayBuffer();
574
+ const headers: [string, string][] = [];
575
+ response.headers.forEach((value, name) => {
576
+ if (isPerClientSignalHeader(name)) return;
577
+ headers.push([name, value]);
578
+ });
579
+ const swrWindow = resolveSwrWindow(swr, this.defaults);
580
+ const { staleAt, expiresAt } = computeExpiration(ttl, swrWindow);
581
+ // Embed the CLAMPED tags in the envelope, not the raw list: tags dropped
582
+ // on write (unsafe/over-cap) would otherwise resurface on a hit and be
583
+ // merged into an upstream document's tag set, letting it claim a tag this
584
+ // entry can't actually be invalidated by. write() re-clamps (idempotent
585
+ // and silent on an already-clean list).
586
+ const safeTags = this.clampTagsForWrite(
587
+ tags,
588
+ "[VercelCacheStore] putResponse",
589
+ );
590
+ const env: VercelResponseEnvelope = {
591
+ b: bufferToBase64(body),
592
+ st: response.status,
593
+ hd: headers,
594
+ s: staleAt,
595
+ e: expiresAt,
596
+ t: safeTags.length > 0 ? safeTags : undefined,
597
+ };
598
+ await this.write(
599
+ this.toStoreKey(key, "r"),
600
+ env,
601
+ ttl + swrWindow,
602
+ safeTags,
603
+ "[VercelCacheStore] putResponse",
604
+ );
605
+ } catch (error) {
606
+ reportCacheError(error, "cache-write", "[VercelCacheStore] putResponse");
607
+ }
608
+ }
609
+
610
+ // --- Item family ("use cache" - getItem/setItem) ---
611
+
612
+ async getItem(key: string): Promise<CacheItemResult | null> {
613
+ const storeKey = this.toStoreKey(key, "i");
614
+ const started = Date.now();
615
+ let raw: unknown;
616
+ try {
617
+ raw = await this.cache.get(storeKey);
618
+ } catch (error) {
619
+ reportCacheError(error, "cache-read", "[VercelCacheStore] getItem");
620
+ this.emitDebug({ op: "getItem", key, outcome: "error" });
621
+ return null;
622
+ }
623
+ const readMs = Date.now() - started;
624
+
625
+ if (raw == null) {
626
+ this.emitDebug({ op: "getItem", key, outcome: "miss", readMs });
627
+ return null;
628
+ }
629
+ const env = this.asItemEnvelope(raw);
630
+ if (!env) {
631
+ reportCacheError(
632
+ new Error("malformed item envelope"),
633
+ "cache-corrupt",
634
+ "[VercelCacheStore] getItem",
635
+ );
636
+ void this.safeDelete(storeKey);
637
+ this.emitDebug({ op: "getItem", key, outcome: "corrupt", readMs });
638
+ return null;
639
+ }
640
+
641
+ const now = Date.now();
642
+ if (now > env.e) {
643
+ void this.safeDelete(storeKey);
644
+ this.emitDebug({
645
+ op: "getItem",
646
+ key,
647
+ outcome: "expired",
648
+ staleAt: env.s,
649
+ expiresAt: env.e,
650
+ readMs,
651
+ });
652
+ return null;
653
+ }
654
+
655
+ const isStale = env.s > 0 && now > env.s;
656
+ if (isStale) {
657
+ this.markRevalidating(storeKey, env, env.t, "[VercelCacheStore] getItem");
658
+ }
659
+ this.emitDebug({
660
+ op: "getItem",
661
+ key,
662
+ outcome: isStale ? "stale-revalidate" : "fresh",
663
+ shouldRevalidate: isStale,
664
+ staleAt: env.s,
665
+ expiresAt: env.e,
666
+ readMs,
667
+ });
668
+ return {
669
+ value: env.v,
670
+ handles: env.h,
671
+ shouldRevalidate: isStale,
672
+ tags: env.t,
673
+ };
674
+ }
675
+
676
+ async setItem(
677
+ key: string,
678
+ value: string,
679
+ options?: CacheItemOptions,
680
+ ): Promise<void> {
681
+ try {
682
+ const ttl = resolveTtl(options?.ttl, this.defaults, DEFAULT_FUNCTION_TTL);
683
+ const swrWindow = resolveSwrWindow(options?.swr, this.defaults);
684
+ const { staleAt, expiresAt } = computeExpiration(ttl, swrWindow);
685
+ // Store the clamped tags (see putResponse) so dropped tags don't resurface
686
+ // on a hit; write() re-clamps idempotently.
687
+ const safeTags = this.clampTagsForWrite(
688
+ options?.tags,
689
+ "[VercelCacheStore] setItem",
690
+ );
691
+ const env: VercelItemEnvelope = {
692
+ v: value,
693
+ h: options?.handles,
694
+ s: staleAt,
695
+ e: expiresAt,
696
+ t: safeTags.length > 0 ? safeTags : undefined,
697
+ };
698
+ await this.write(
699
+ this.toStoreKey(key, "i"),
700
+ env,
701
+ ttl + swrWindow,
702
+ safeTags,
703
+ "[VercelCacheStore] setItem",
704
+ );
705
+ } catch (error) {
706
+ reportCacheError(error, "cache-write", "[VercelCacheStore] setItem");
707
+ }
708
+ }
709
+
710
+ // --- Tags ---
711
+
712
+ async invalidateTags(tags: string[]): Promise<void> {
713
+ if (!tags || tags.length === 0) return;
714
+ // No per-item cap here: an invalidation must reach every requested tag.
715
+ const safe = this.validateTags(
716
+ tags,
717
+ "[VercelCacheStore] invalidateTags",
718
+ "cache-invalidate",
719
+ );
720
+ if (safe.length === 0) return;
721
+ try {
722
+ await this.cache.expireTag(safe);
723
+ } catch (error) {
724
+ // The one deliberate throw: a failed durable invalidation must reject so an
725
+ // awaited updateTag() surfaces it instead of reporting false success.
726
+ // NOTE: this only fires if the injected cache's expireTag() actually
727
+ // rejects. The official @vercel/functions cache catches backend failures
728
+ // and resolves, so on that client a failure is invisible and this throw
729
+ // never fires (tag invalidation is best-effort there). See the class doc.
730
+ reportCacheError(
731
+ error,
732
+ "cache-invalidate",
733
+ "[VercelCacheStore] invalidateTags",
734
+ );
735
+ throw error instanceof Error ? error : new Error(String(error));
736
+ }
737
+ }
738
+
739
+ // --- Internals ---
740
+
741
+ // The `rg:{family}:` prefix keeps the three value tiers in disjoint
742
+ // keyspaces, but the isolation is only as strong as the cache's
743
+ // keyHashFunction: djb2 (getCache's default) folds this to 32 bits, so at a
744
+ // large live-key count a same-family collision can serve the wrong body and a
745
+ // cross-family one reads as corrupt. See the class doc for passing a wider
746
+ // hash (sha256) when many keys are live.
747
+ private toStoreKey(key: string, family: CacheFamily): string {
748
+ const versionPrefix = this.version ? `v/${this.version}/` : "";
749
+ return `${versionPrefix}rg:${family}:${key}`;
750
+ }
751
+
752
+ private async write(
753
+ storeKey: string,
754
+ value: unknown,
755
+ totalTtlSeconds: number,
756
+ tags: string[] | undefined,
757
+ label: string,
758
+ ): Promise<void> {
759
+ if (!this.withinSizeLimit(value, label)) return;
760
+ const safeTags = this.clampTagsForWrite(tags, label);
761
+ const options: { ttl: number; tags?: string[]; name?: string } = {
762
+ ttl: Math.max(1, Math.ceil(totalTtlSeconds)),
763
+ };
764
+ if (safeTags.length > 0) options.tags = safeTags;
765
+ if (this.name) options.name = this.name;
766
+ await this.cache.set(storeKey, value, options);
767
+ }
768
+
769
+ /**
770
+ * Stale-read herd dampening: push staleAt forward by REVALIDATION_LOCK_MS
771
+ * (clamped to the hard expiry) and re-write the same envelope under the
772
+ * remaining lifetime, so concurrent same-region readers see it as fresh while
773
+ * one revalidates. Best-effort and non-blocking; never throws.
774
+ *
775
+ * Two races are accepted here because `getCache` offers no compare-and-set: a
776
+ * fast background revalidation that lands between this stale read and the
777
+ * re-stamp write can be overwritten by the (staler) locked envelope, and an
778
+ * `expireTag` that deletes the entry in the same window can be resurrected by
779
+ * the re-stamp. Both self-heal within REVALIDATION_LOCK_MS (the re-stamped
780
+ * copy is then re-read as stale and revalidated again, or re-deleted). The
781
+ * window is narrow and the cost is at most one extra revalidation / one brief
782
+ * stale serve; a read-compare would not close it atomically without CAS.
783
+ */
784
+ private markRevalidating<E extends { s: number; e: number }>(
785
+ storeKey: string,
786
+ env: E,
787
+ tags: string[] | undefined,
788
+ label: string,
789
+ ): void {
790
+ const now = Date.now();
791
+ const remainingSeconds = Math.ceil((env.e - now) / 1000);
792
+ if (remainingSeconds <= 0) return;
793
+ const locked: E = {
794
+ ...env,
795
+ s: Math.min(now + REVALIDATION_LOCK_MS, env.e),
796
+ };
797
+ const task = (): Promise<void> =>
798
+ this.write(storeKey, locked, remainingSeconds, tags, label);
799
+ if (this.waitUntil) {
800
+ this.waitUntil(reportingAsync(task, "cache-write", label));
801
+ } else {
802
+ void reportingAsync(task, "cache-write", label);
803
+ }
804
+ }
805
+
806
+ private withinSizeLimit(value: unknown, label: string): boolean {
807
+ try {
808
+ const bytes = new TextEncoder().encode(JSON.stringify(value)).length;
809
+ if (bytes >= this.maxItemBytes) {
810
+ reportCacheError(
811
+ new Error(
812
+ `entry is ${bytes}B, at/above the ${this.maxItemBytes}B cap; not cached`,
813
+ ),
814
+ "cache-write",
815
+ label,
816
+ );
817
+ return false;
818
+ }
819
+ } catch {
820
+ // If the value cannot be measured, allow the write (best-effort).
821
+ }
822
+ return true;
823
+ }
824
+
825
+ /**
826
+ * Drop tags Vercel cannot round-trip: over-length tags, and tags carrying a
827
+ * URL metacharacter the `@vercel/functions` client does not encode (see
828
+ * VERCEL_UNSAFE_TAG_CHARS). Runs on BOTH paths - the write path
829
+ * (clampTagsForWrite) and invalidateTags - so a tag that would silently fail
830
+ * invalidation is never stored in the first place. `category` reports the
831
+ * drop under the caller's real operation (write vs invalidate).
832
+ */
833
+ private validateTags(
834
+ tags: string[],
835
+ label: string,
836
+ category: CacheErrorCategory,
837
+ ): string[] {
838
+ const encoder = new TextEncoder();
839
+ const out: string[] = [];
840
+ for (const tag of tags) {
841
+ const unsafe = VERCEL_UNSAFE_TAG_CHARS.exec(tag);
842
+ if (unsafe) {
843
+ reportCacheError(
844
+ new Error(
845
+ `tag "${tag}" contains an unencodable URL character "${unsafe[0]}"; ` +
846
+ `skipped (it would not round-trip through Vercel tag invalidation)`,
847
+ ),
848
+ category,
849
+ label,
850
+ );
851
+ continue;
852
+ }
853
+ if (encoder.encode(tag).length > VERCEL_MAX_TAG_BYTES) {
854
+ reportCacheError(
855
+ new Error(`tag exceeds ${VERCEL_MAX_TAG_BYTES} bytes; skipped`),
856
+ category,
857
+ label,
858
+ );
859
+ continue;
860
+ }
861
+ out.push(tag);
862
+ }
863
+ return out;
864
+ }
865
+
866
+ /** validateTags + the per-item tag cap. Used on the write path. */
867
+ private clampTagsForWrite(
868
+ tags: string[] | undefined,
869
+ label: string,
870
+ ): string[] {
871
+ if (!tags || tags.length === 0) return [];
872
+ const valid = this.validateTags(tags, label, "cache-write");
873
+ if (valid.length > VERCEL_MAX_TAGS_PER_ITEM) {
874
+ reportCacheError(
875
+ new Error(
876
+ `entry has ${valid.length} tags, over the ${VERCEL_MAX_TAGS_PER_ITEM}-tag cap; ` +
877
+ `keeping the first ${VERCEL_MAX_TAGS_PER_ITEM}`,
878
+ ),
879
+ "cache-write",
880
+ label,
881
+ );
882
+ return valid.slice(0, VERCEL_MAX_TAGS_PER_ITEM);
883
+ }
884
+ return valid;
885
+ }
886
+
887
+ private async safeDelete(storeKey: string): Promise<void> {
888
+ try {
889
+ await this.cache.delete(storeKey);
890
+ } catch (error) {
891
+ reportCacheError(error, "cache-delete", "[VercelCacheStore] evict");
892
+ }
893
+ }
894
+
895
+ private emitDebug(event: VercelCacheReadDebugEvent): void {
896
+ const sink = this.debug;
897
+ if (!sink) return;
898
+ try {
899
+ if (typeof sink === "function") sink(event);
900
+ else console.log("[VercelCacheStore]", event);
901
+ } catch {
902
+ // The debug sink must never break the cache path.
903
+ }
904
+ }
905
+
906
+ private asSegmentEnvelope(raw: unknown): VercelSegmentEnvelope | null {
907
+ if (!isRecord(raw)) return null;
908
+ const { d, s, e } = raw;
909
+ if (
910
+ !isRecord(d) ||
911
+ !Array.isArray((d as Record<string, unknown>).segments)
912
+ ) {
913
+ return null;
914
+ }
915
+ if (typeof s !== "number" || typeof e !== "number") return null;
916
+ return { d: d as unknown as CachedEntryData, s, e };
917
+ }
918
+
919
+ private asItemEnvelope(raw: unknown): VercelItemEnvelope | null {
920
+ if (!isRecord(raw)) return null;
921
+ const { v, h, s, e, t } = raw;
922
+ if (typeof v !== "string") return null;
923
+ if (typeof s !== "number" || typeof e !== "number") return null;
924
+ return {
925
+ v,
926
+ h: typeof h === "string" ? h : undefined,
927
+ s,
928
+ e,
929
+ t: Array.isArray(t) ? (t as string[]) : undefined,
930
+ };
931
+ }
932
+
933
+ private asResponseEnvelope(raw: unknown): VercelResponseEnvelope | null {
934
+ if (!isRecord(raw)) return null;
935
+ const { b, st, hd, s, e, t } = raw;
936
+ if (typeof b !== "string" || typeof st !== "number") return null;
937
+ if (!Array.isArray(hd)) return null;
938
+ if (typeof s !== "number" || typeof e !== "number") return null;
939
+ return {
940
+ b,
941
+ st,
942
+ hd: hd as [string, string][],
943
+ s,
944
+ e,
945
+ t: Array.isArray(t) ? (t as string[]) : undefined,
946
+ };
947
+ }
948
+ }