@cloudflare/workers-response-store 1.1.2 → 1.1.4

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/README.md CHANGED
@@ -179,14 +179,14 @@ Workers Cache stays off for the Worker as a whole and for `default`; only `Respo
179
179
 
180
180
  The object returned by `createWorkersResponseStore()` and `createWorkersResponseStoreClient()` implements the same API:
181
181
 
182
- | Method | Behavior |
183
- | ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
184
- | `fetch(request)` | Reads a canonical `GET` cache key. Returns a stored response or a `404` Response Store miss. Unconditional stale reads return immediately and regenerate in the background; Workers Cache conditional revalidations and hard-expired reads wait for regeneration, unless the entry was stored with `expiryBehavior: "miss"`. |
185
- | `put(request, response, options?)` | Stores a response under a canonical `GET` cache key. `options.revalidator` supplies `{ id, args }` for future regeneration. Set `purgeExisting: true` when replacing an entry that may already be in Workers Cache. Set `expiryBehavior: "miss"` to get a miss instead of a regeneration once the entry expires. |
186
- | `refresh({ tags, pathPrefixes })` | Regenerates matching entries, then invalidates their prior edge responses: Workers Cache marks them stale and refetches the committed revisions in the background. At least one selector is required. |
187
- | `invalidate({ tags, pathPrefixes, expire })` | Marks matching entries stale without regenerating them. Reads keep serving them through their stale-while-revalidate window, capped at `expire` seconds after the call when set, and regenerate them like any stale entry. Like Next.js, an `expire` deadline also caps entries with those tags published before it. Workers Cache refetches the edge responses of entries with a revalidator in the background and purges the others. At least one selector is required. |
188
- | `purge({ tags, pathPrefixes, purgeEverything })` | Removes matching metadata, records tag invalidations, deletes response bodies, and purges corresponding edge responses. At least one selector or `purgeEverything: true` is required. |
189
- | `getTagExpiration(tags)` | Returns the latest invalidation timestamp for a set of framework-managed soft tags. Most integrations do not need this low-level method. |
182
+ | Method | Behavior |
183
+ | ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
184
+ | `fetch(request)` | Reads a canonical `GET` cache key. Returns a stored response or a `404` Response Store miss. Unconditional stale reads return immediately and regenerate in the background; Workers Cache conditional revalidations and hard-expired reads wait for regeneration, unless the entry was stored with `expiryBehavior: "miss"`. A stale entry without a revalidator is returned as stored. |
185
+ | `put(request, response, options?)` | Stores a response under a canonical `GET` cache key. `options.revalidator` supplies `{ id, args }` for future regeneration. Set `purgeExisting: true` when replacing an entry that may already be in Workers Cache; the purge is skipped for a first write, or for a replaced entry without a revalidator once it is stale (unless an `invalidate()` edge purge for it is still queued). Set `expiryBehavior: "miss"` to get a miss instead of a regeneration once the entry expires. |
186
+ | `refresh({ tags, pathPrefixes })` | Regenerates matching entries, then invalidates their prior edge responses: Workers Cache marks them stale and refetches the committed revisions in the background. At least one selector is required. |
187
+ | `invalidate({ tags, pathPrefixes, expire })` | Marks matching entries stale without regenerating them. Reads keep serving them through their stale-while-revalidate window, capped at `expire` seconds after the call when set, and regenerate them like any stale entry. Like Next.js, an `expire` deadline also caps entries with those tags published before it. Workers Cache refetches the edge responses of entries with a revalidator in the background and purges the others. At least one selector is required. |
188
+ | `purge({ tags, pathPrefixes, purgeEverything })` | Removes matching metadata, records tag invalidations, deletes response bodies, and purges corresponding edge responses. At least one selector or `purgeEverything: true` is required. |
189
+ | `getTagExpiration(tags)` | Returns the latest invalidation timestamp for a set of framework-managed soft tags. Most integrations do not need this low-level method. |
190
190
 
191
191
  Mutation methods return:
192
192
 
@@ -211,7 +211,7 @@ type ResponseStoreMutationResult = {
211
211
 
212
212
  Freshness is derived from `Cloudflare-CDN-Cache-Control`, then `CDN-Cache-Control`, then `Cache-Control`. The store preserves an incoming `Age` value and advances it while the response is stored.
213
213
 
214
- Stored responses preserve application-provided validators and include a revision-specific `ETag` when none is provided. A generated `Last-Modified` would conflate revisions written within the same second. Workers Cache sends validators back as `If-None-Match` or `If-Modified-Since` when revalidating. If the backing response is stale, that invocation claims regeneration and returns the committed response directly to Workers Cache, avoiding a second layer of SWR. If another invocation already holds the claim, the callback fails so Workers Cache can retain its stale response and retry. Fresh backing responses are reused without regeneration. Conditional headers identify revalidation, including foreground expiry; they do not identify background execution specifically.
214
+ Stored responses preserve application-provided validators and include a revision-specific `ETag` when none is provided. A generated `Last-Modified` would conflate revisions written within the same second. Workers Cache sends validators back as `If-None-Match` or `If-Modified-Since` when revalidating. If the backing response is stale and has a revalidator, that invocation claims regeneration and returns the committed response directly to Workers Cache, avoiding a second layer of SWR. If another invocation already holds the claim, the callback fails so Workers Cache can retain its stale response and retry. Fresh backing responses are reused without regeneration. Conditional headers identify revalidation, including foreground expiry; they do not identify background execution specifically.
215
215
 
216
216
  Set `Cache-Tag` on the response passed to `put()` to associate comma-separated purge tags. `refresh()`, `invalidate()`, and `purge()` also accept pathname prefixes. Tags are matched case-insensitively for invalidation.
217
217
 
@@ -289,6 +289,7 @@ application Worker
289
289
  - R2 tombstones are durably queued in SQLite and drained in bounded batches. A failed R2 operation leaves its tombstone queued so a later purge can retry it instead of losing the anti-resurrection fence.
290
290
  - Invalidation rewrites each matching R2 object as a stale copy under a higher revision through the same queue, so a write reserved before the invalidation cannot publish over it. If R2 does not hold the entry's committed revision yet, the key is tombstoned instead.
291
291
  - An unconditional read of stale R2 content returns immediately while `ctx.waitUntil()` runs one claimed regeneration. A conditional Workers Cache revalidation instead waits for regeneration and returns the committed replacement in that same invocation.
292
+ - An entry stored without a revalidator is served stale through its stale-while-revalidate window until its writer replaces it; nothing regenerates it. Its edge copy gets no stale-while-revalidate or stale-if-error window and expires when the entry goes stale, so a `put()` replacing it from then on needs no edge purge, unless an `invalidate()` made it stale and its edge purge is still queued. The skip assumes the edge holds only that entry's own copy: if an earlier, longer-lived copy outlived a refused purge (a shorter freshness than the entry it replaced, or an entry with a revalidator before it), Workers Cache can serve that copy until it expires, or until Workers Cache revalidates it. Until that replacement, every stale read reaches the binding and R2 instead of Workers Cache.
292
293
  - Hard-expired responses are never served. Reads wait for regeneration and therefore require a stored revalidator descriptor, unless the entry was stored with `expiryBehavior: "miss"`: such a read returns a Response Store miss and starts no regeneration, so the caller regenerates the value itself. `refresh()` still regenerates the entry through its revalidator.
293
294
  - Abandoned write reservations are retained for one hour before alarm-driven cleanup fences them from later publication. Response bodies are written only after Durable Object publication, so this cleanup does not perform R2 operations.
294
295
  - RPC-transferred response bodies are buffered before R2 writes because transferred streams do not retain the fixed-length marker required by R2's single-part put API. Account for Worker memory limits when choosing maximum response sizes.
package/dist/binding.d.ts CHANGED
@@ -10,6 +10,24 @@ export type SerializableValue = null | boolean | number | string | SerializableV
10
10
  export type ResponseStorePutOptions = {
11
11
  /** @internal Collapse overlapping framework writes for the same cache key. */
12
12
  coalesce?: boolean;
13
+ /**
14
+ * @internal The revision a stale read returned (`X-Workers-Response-Store-Revision`)
15
+ * that this write refreshes. While another refresh of that revision is published and
16
+ * fresh, and the key hasn't been invalidated or purged since, the write is dropped instead
17
+ * of replacing (and purging) it, and returns
18
+ * `{ backingStoreUpdated: false, edgePurgeAccepted: false }`. A value that isn't a
19
+ * positive integer is ignored.
20
+ */
21
+ replacesRevision?: number;
22
+ /**
23
+ * @internal With `replacesRevision`: tags the caller checks itself when reading (soft
24
+ * tags), and the `getTagExpiration()` of those tags that the stale read saw. Once they
25
+ * have expired since, the write isn't dropped. A malformed value leaves an ordinary write.
26
+ */
27
+ replacesTagExpiration?: {
28
+ tags: string[];
29
+ expiration: number;
30
+ };
13
31
  revalidator?: RevalidatorDescriptor;
14
32
  purgeExisting?: boolean;
15
33
  /**
@@ -67,12 +85,16 @@ type EntryMetadata = {
67
85
  };
68
86
  export type CandidateMetadata = EntryMetadata & {
69
87
  fenceTags: string[];
88
+ /** When the response's generation started (`GENERATED_AT_HEADER`). */
89
+ generatedAt?: number;
70
90
  };
71
91
  export type StoredEntry = EntryMetadata & {
72
92
  keyHash: string;
73
93
  cacheKey: string;
74
94
  activeRevision: number;
75
95
  latestRevision: number;
96
+ /** Set on entries read from R2, whose metadata records only whether a revalidator exists. */
97
+ hasRevalidator?: boolean;
76
98
  };
77
99
  export type RefreshCandidate = Pick<StoredEntry, "keyHash" | "cacheKey" | "activeRevision" | "latestRevision"> & ({
78
100
  hasRevalidator: boolean;
@@ -185,4 +207,5 @@ export declare class ResponseStoreBinding extends WorkerEntrypoint<WorkersRespon
185
207
  private updatePendingEdges;
186
208
  purge(options: ResponseStorePurgeOptions): Promise<ResponseStoreMutationResult>;
187
209
  }
188
- //#endregion
210
+ //#endregion
211
+ export {};
package/dist/binding.js CHANGED
@@ -75,6 +75,20 @@ const NULL_BODY_STATUSES = /* @__PURE__ */ new Set([
75
75
  304
76
76
  ]);
77
77
  const AGE_BASIS_HEADER = "X-Workers-Response-Store-Age-Basis";
78
+ /**
79
+ * @internal When a response's generation started (ms since epoch), on a put or a
80
+ * revalidator's response. Like Next.js "use cache", the entry is published stale if its
81
+ * tags were invalidated after that, and expired if they were purged, or an `expire` deadline
82
+ * passed, after it. Its freshness and stale-while-revalidate windows also count from then.
83
+ * A value that isn't a positive finite number is ignored, a fractional one is rounded down,
84
+ * and one in the future, e.g. from a skewed clock, counts as when the Store received it. It
85
+ * isn't stored.
86
+ */
87
+ const GENERATED_AT_HEADER = "X-Workers-Response-Store-Generated-At";
88
+ function generatedAtFromResponse(response) {
89
+ const value = Math.floor(Number(response.headers.get(GENERATED_AT_HEADER) ?? void 0));
90
+ return Number.isSafeInteger(value) && value > 0 ? Math.min(value, Date.now()) : void 0;
91
+ }
78
92
  const STORAGE_LAYOUT_VERSION = "r2-v2";
79
93
  const pendingPuts = /* @__PURE__ */ new Map();
80
94
  const pendingR2Reads = /* @__PURE__ */ new Map();
@@ -167,6 +181,9 @@ async function readBodyPrefix(body, prefixLength) {
167
181
  })
168
182
  };
169
183
  }
184
+ function hasRevalidator(entry) {
185
+ return entry.hasRevalidator ?? entry.revalidator !== null;
186
+ }
170
187
  function purgeTagForEntry(entry) {
171
188
  return `runtime-cache-${entry.keyHash}`;
172
189
  }
@@ -322,6 +339,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
322
339
  freshUntil,
323
340
  swrUntil,
324
341
  revalidator: null,
342
+ hasRevalidator: metadata.noRevalidator !== "1",
325
343
  expiryBehavior: metadata.expiryBehavior === "miss" ? "miss" : "regenerate",
326
344
  cacheTags: [],
327
345
  activeRevision: latestRevision,
@@ -394,7 +412,8 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
394
412
  freshUntil: String(entry.freshUntil),
395
413
  swrUntil: String(entry.swrUntil),
396
414
  latestRevision: String(entry.activeRevision),
397
- ...entry.expiryBehavior === "miss" ? { expiryBehavior: "miss" } : {}
415
+ ...entry.expiryBehavior === "miss" ? { expiryBehavior: "miss" } : {},
416
+ ...hasRevalidator(entry) ? {} : { noRevalidator: "1" }
398
417
  };
399
418
  if (customMetadataSize(customMetadata) > R2_CUSTOM_METADATA_SAFE_BYTES) {
400
419
  const responseMetadata = new TextEncoder().encode(JSON.stringify({
@@ -410,7 +429,8 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
410
429
  freshUntil: String(entry.freshUntil),
411
430
  swrUntil: String(entry.swrUntil),
412
431
  latestRevision: String(entry.activeRevision),
413
- ...entry.expiryBehavior === "miss" ? { expiryBehavior: "miss" } : {}
432
+ ...entry.expiryBehavior === "miss" ? { expiryBehavior: "miss" } : {},
433
+ ...hasRevalidator(entry) ? {} : { noRevalidator: "1" }
414
434
  };
415
435
  }
416
436
  return await this.writeR2Revision(this.r2ObjectKey(entry.keyHash), entry.activeRevision, storedBody, customMetadata, expectedEtag);
@@ -448,7 +468,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
448
468
  if (!headers.has("ETag")) headers.set("ETag", `W/"${encodeURIComponent(entry.objectKey)}:${entry.activeRevision}"`);
449
469
  headers.set(AGE_BASIS_HEADER, `${createdAt}:${initialAge}`);
450
470
  headers.set("Age", String(representationAge(createdAt, initialAge, now)));
451
- headers.set("Cloudflare-CDN-Cache-Control", edgeCacheControl(entry.freshUntil, entry.swrUntil, now));
471
+ headers.set("Cloudflare-CDN-Cache-Control", edgeCacheControl(entry.freshUntil, entry.swrUntil, now, hasRevalidator(entry)));
452
472
  headers.set("Cache-Tag", cacheTagHeader(entry));
453
473
  headers.set("X-Workers-Response-Store", now < entry.freshUntil ? "BLOB-FRESH" : "BLOB-STALE");
454
474
  headers.set("X-Workers-Response-Store-Revision", String(entry.activeRevision));
@@ -493,19 +513,20 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
493
513
  if (!body) await storedBody.cancel();
494
514
  return this.createStoredResponse(storedEntry, body, status, createdAt, initialAge, now);
495
515
  }
496
- async storeResponse(metadata, request, response, revalidator, expiryBehavior, reservation, cacheTags = cacheTagsFromResponse(response), expectedR2Etag) {
516
+ async storeResponse(metadata, request, response, revalidator, expiryBehavior, reservation, cacheTags = cacheTagsFromResponse(response), expectedR2Etag, replaces, generatedAt = generatedAtFromResponse(response)) {
497
517
  const cacheKey = reservation ?? await this.deriveCacheKey(request);
498
518
  const write = reservation ?? await this.reserveWrite(metadata, cacheKey, cacheTags);
499
519
  const { keyHash, objectKey: reservationObjectKey, revision } = write;
500
520
  let publication;
521
+ let publishedAt;
501
522
  let body;
502
523
  let policy;
503
524
  try {
504
525
  const now = Date.now();
505
- policy = deriveCachePolicy(response.headers, now);
526
+ policy = deriveCachePolicy(response.headers, now, generatedAt);
506
527
  const responseHeaders = [...response.headers].filter(([name]) => {
507
528
  const lower = name.toLowerCase();
508
- return lower !== "age" && lower !== "cache-tag" && lower !== "cf-cache-status" && lower !== "content-length";
529
+ return lower !== "age" && lower !== "cache-tag" && lower !== "cf-cache-status" && lower !== "content-length" && lower !== GENERATED_AT_HEADER.toLowerCase();
509
530
  });
510
531
  const candidate = {
511
532
  fenceTags: [.../* @__PURE__ */ new Set([...write.fenceTags, ...cacheTags])],
@@ -516,10 +537,12 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
516
537
  swrUntil: policy.swrUntil,
517
538
  revalidator: revalidator ?? null,
518
539
  expiryBehavior,
519
- cacheTags
540
+ cacheTags,
541
+ ...generatedAt !== void 0 ? { generatedAt } : {}
520
542
  };
521
543
  body = response.body ? await response.arrayBuffer() : /* @__PURE__ */ new ArrayBuffer(0);
522
- publication = await metadata.publish(keyHash, revision, candidate, write.claimId, reservationObjectKey);
544
+ publication = await metadata.publish(keyHash, revision, candidate, write.claimId, reservationObjectKey, replaces);
545
+ publishedAt = Date.now();
523
546
  } catch (error) {
524
547
  await this.releaseFailedWrite(metadata, write);
525
548
  throw error;
@@ -557,6 +580,11 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
557
580
  edgePurgeRequired: publication.edgePurgeRequired,
558
581
  published: true,
559
582
  entry,
583
+ ...replaces ? { replacedRevision: replaces.revision } : {},
584
+ ...generatedAt !== void 0 ? { generatedAt } : {},
585
+ publishedAt,
586
+ ...publication.publishedStale ? { publishedStale: true } : {},
587
+ policyFreshUntil: policy.freshUntil,
560
588
  ...stored && entry ? { response: this.createStoredResponse(entry, NULL_BODY_STATUSES.has(response.status) ? null : body, response.status, policy.createdAt, policy.initialAge) } : {}
561
589
  };
562
590
  }
@@ -617,11 +645,11 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
617
645
  headers: MISS_HEADERS
618
646
  });
619
647
  const now = Date.now();
620
- const revalidateStale = now >= entry.freshUntil && (request.headers.has("If-None-Match") || request.headers.has("If-Modified-Since"));
648
+ const revalidateStale = hasRevalidator(entry) && now >= entry.freshUntil && (request.headers.has("If-None-Match") || request.headers.has("If-Modified-Since"));
621
649
  if (now < entry.swrUntil && !revalidateStale) {
622
650
  const stored = await this.readStoredResponse(entry, now, r2Read?.object);
623
651
  if (stored) {
624
- if (now < entry.freshUntil) return stored;
652
+ if (now < entry.freshUntil || !hasRevalidator(entry)) return stored;
625
653
  this.ctx.waitUntil(Promise.resolve().then(async () => {
626
654
  const metadata = this.getMetadata(keyHash);
627
655
  await this.revalidateEntryInBackground(metadata, entry, expectedR2Etag);
@@ -665,11 +693,22 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
665
693
  return this.getTagMetadata(tags).getTagExpiration(tags);
666
694
  }
667
695
  async put(request, response, options = {}) {
696
+ const generatedAt = generatedAtFromResponse(response);
668
697
  const cacheKey = await this.deriveCacheKey(request);
669
698
  const { keyHash } = cacheKey;
670
699
  const metadata = this.getMetadata(keyHash);
671
700
  const cacheTags = cacheTagsFromResponse(response);
672
701
  const pendingPutKey = `${this.getVersionId()}:${this.shardCount}:${keyHash}:${Boolean(options.purgeExisting)}`;
702
+ const tagExpiration = options.replacesTagExpiration;
703
+ const validTagExpiration = tagExpiration === void 0 || Array.isArray(tagExpiration?.tags) && tagExpiration.tags.every((tag) => typeof tag === "string") && Number.isFinite(tagExpiration.expiration);
704
+ const replacesRevision = Number.isSafeInteger(options.replacesRevision) && options.replacesRevision > 0 && validTagExpiration ? options.replacesRevision : void 0;
705
+ const replaces = replacesRevision === void 0 ? void 0 : {
706
+ revision: replacesRevision,
707
+ ...tagExpiration ? {
708
+ tags: tagExpiration.tags,
709
+ tagExpiration: tagExpiration.expiration
710
+ } : {}
711
+ };
673
712
  let reservation;
674
713
  if (options.coalesce) for (;;) {
675
714
  const pending = pendingPuts.get(pendingPutKey);
@@ -698,6 +737,8 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
698
737
  continue;
699
738
  }
700
739
  if (result?.published && result.entry) {
740
+ if (replaces !== void 0 && (result.entry.activeRevision <= replaces.revision || result.replacedRevision === replaces.revision)) break;
741
+ if ((result.publishedStale || (result.policyFreshUntil ?? Number.POSITIVE_INFINITY) <= Date.now()) && (generatedAt !== void 0 || result.generatedAt !== void 0) && (generatedAt ?? Number.POSITIVE_INFINITY) > (result.generatedAt ?? result.publishedAt ?? Number.POSITIVE_INFINITY)) break;
701
742
  const objectKey = reservation.objectKey;
702
743
  await metadata.finishPendingObjects([objectKey]).catch((error) => this.logCleanupFailure(objectKey, error));
703
744
  response.body?.cancel().catch(() => {});
@@ -710,7 +751,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
710
751
  }
711
752
  const write = (async () => {
712
753
  reservation ??= await this.reserveWrite(metadata, cacheKey, cacheTags);
713
- return this.storeResponse(metadata, request, response, options.revalidator, options.expiryBehavior === "miss" ? "miss" : "regenerate", reservation, cacheTags);
754
+ return this.storeResponse(metadata, request, response, options.revalidator, options.expiryBehavior === "miss" ? "miss" : "regenerate", reservation, cacheTags, void 0, replaces, generatedAt);
714
755
  })();
715
756
  let cleared = () => {};
716
757
  if (options.coalesce) {
@@ -5,7 +5,11 @@ export type CachePolicy = {
5
5
  freshUntil: number;
6
6
  swrUntil: number;
7
7
  };
8
- export declare function deriveCachePolicy(headers: Headers, now?: number): CachePolicy;
8
+ /**
9
+ * `generatedAt` dates the response from when its generation started: it is at least that old,
10
+ * and its stale-while-revalidate window still ends that long after its max-age.
11
+ */
12
+ export declare function deriveCachePolicy(headers: Headers, now?: number, generatedAt?: number): CachePolicy;
9
13
  export declare function representationAge(createdAt: number, initialAge: number, now?: number): number;
10
- export declare function edgeCacheControl(freshUntil: number, swrUntil: number, now?: number): string;
14
+ export declare function edgeCacheControl(freshUntil: number, swrUntil: number, now?: number, hasRevalidator?: boolean): string;
11
15
  //#endregion
@@ -6,7 +6,11 @@ function parseSeconds(value) {
6
6
  const parsed = Number(match[1] ?? match[2]);
7
7
  return Number.isSafeInteger(parsed) ? parsed : void 0;
8
8
  }
9
- function deriveCachePolicy(headers, now = Date.now()) {
9
+ /**
10
+ * `generatedAt` dates the response from when its generation started: it is at least that old,
11
+ * and its stale-while-revalidate window still ends that long after its max-age.
12
+ */
13
+ function deriveCachePolicy(headers, now = Date.now(), generatedAt) {
10
14
  const cacheControl = headers.get("Cloudflare-CDN-Cache-Control") ?? headers.get("CDN-Cache-Control") ?? headers.get("Cache-Control");
11
15
  const directives = /* @__PURE__ */ new Map();
12
16
  for (const part of cacheControl?.split(",") ?? []) {
@@ -17,19 +21,23 @@ function deriveCachePolicy(headers, now = Date.now()) {
17
21
  const cacheStorageForbidden = directives.has("no-store") || directives.has("private");
18
22
  const staleServingForbidden = directives.has("s-maxage") || directives.has("must-revalidate") || directives.has("proxy-revalidate");
19
23
  const maxAge = cacheStorageForbidden || directives.has("no-cache") ? 0 : parseSeconds(directives.get("s-maxage")) ?? parseSeconds(directives.get("max-age")) ?? 0;
20
- const initialAge = parseSeconds(headers.get("Age") ?? void 0) ?? 0;
21
- const freshUntil = now + Math.max(0, maxAge - initialAge) * 1e3;
24
+ const ageMs = Math.max((parseSeconds(headers.get("Age") ?? void 0) ?? 0) * 1e3, generatedAt !== void 0 && generatedAt <= now ? now - generatedAt : 0);
25
+ const initialAge = Math.floor(ageMs / 1e3);
26
+ const freshUntil = now + Math.max(0, maxAge * 1e3 - ageMs);
27
+ const staleWhileRevalidate = cacheStorageForbidden || staleServingForbidden ? 0 : parseSeconds(directives.get("stale-while-revalidate")) ?? 0;
28
+ const staleMsUsed = generatedAt === void 0 ? 0 : Math.max(0, ageMs - maxAge * 1e3);
22
29
  return {
23
30
  createdAt: now,
24
31
  initialAge,
25
32
  freshUntil,
26
- swrUntil: freshUntil + (cacheStorageForbidden || staleServingForbidden ? 0 : parseSeconds(directives.get("stale-while-revalidate")) ?? 0) * 1e3
33
+ swrUntil: freshUntil + Math.max(0, staleWhileRevalidate * 1e3 - staleMsUsed)
27
34
  };
28
35
  }
29
36
  function representationAge(createdAt, initialAge, now = Date.now()) {
30
37
  return initialAge + Math.max(0, Math.floor((now - createdAt) / 1e3));
31
38
  }
32
- function edgeCacheControl(freshUntil, swrUntil, now = Date.now()) {
39
+ function edgeCacheControl(freshUntil, swrUntil, now = Date.now(), hasRevalidator = true) {
40
+ if (!hasRevalidator) return `max-age=${Math.max(0, Math.floor((freshUntil - now) / 1e3))}, stale-while-revalidate=0, stale-if-error=0`;
33
41
  if (now < freshUntil) return `max-age=${Math.max(0, Math.ceil((freshUntil - now) / 1e3))}, stale-while-revalidate=${Math.max(0, Math.ceil((swrUntil - freshUntil) / 1e3))}`;
34
42
  return `max-age=0, stale-while-revalidate=${Math.max(0, Math.ceil((swrUntil - now) / 1e3))}`;
35
43
  }
@@ -11,6 +11,11 @@ type PublicationResult = {
11
11
  edgePurgeRequired: boolean;
12
12
  entry: StoredEntry | null;
13
13
  published: boolean;
14
+ /**
15
+ * Published stale, or expired: generated before an invalidation, or with no fresh window
16
+ * left by this transaction's clock.
17
+ */
18
+ publishedStale?: true;
14
19
  };
15
20
  type WriteReservation = {
16
21
  objectKey: string;
@@ -26,6 +31,12 @@ export type PurgeReservation = {
26
31
  pendingTombstones: number;
27
32
  tombstoneSequence: number;
28
33
  };
34
+ /** The revision a refresh read stale, and the expiration of the soft tags that read saw. */
35
+ export type RefreshedRevision = {
36
+ revision: number;
37
+ tags?: string[];
38
+ tagExpiration?: number;
39
+ };
29
40
  type TombstoneDrainResult = {
30
41
  cursor?: string;
31
42
  failures: string[];
@@ -43,7 +54,7 @@ export type CacheMetadataStub = DurableObjectStub & {
43
54
  finishPendingObjects(objectKeys: string[]): Promise<void>;
44
55
  listExpiredPendingObjects(cutoff: number, limit: number): Promise<string[]>;
45
56
  sweepExpiredPendingObjects(cutoff?: number): Promise<number>;
46
- publish(keyHash: string, revision: number, metadata: CandidateMetadata, claimId?: string, reservationObjectKey?: string): Promise<PublicationResult>;
57
+ publish(keyHash: string, revision: number, metadata: CandidateMetadata, claimId?: string, reservationObjectKey?: string, replaces?: RefreshedRevision): Promise<PublicationResult>;
47
58
  invalidatePublishedRevision(keyHash: string, revision: number): Promise<TombstoneDrainResult>;
48
59
  getEntry(keyHash: string): Promise<StoredEntry | null>;
49
60
  getTagExpiration(tags: string[]): Promise<number>;
@@ -84,12 +95,14 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
84
95
  reserveWrite(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number): Promise<WriteReservation>;
85
96
  replaceFailedWrite(keyHash: string, cacheKey: string, objectKeyPrefix: string, failedObjectKey: string, fenceTags: string[], createdAt: number): Promise<WriteReservation | null>;
86
97
  reserveRegeneration(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number, expectedActiveRevision?: number, expectedLatestRevision?: number): Promise<RegenerationReservation | null>;
87
- publish(keyHash: string, revision: number, metadata: CandidateMetadata, claimId?: string, reservationObjectKey?: string): Promise<PublicationResult>;
98
+ publish(keyHash: string, revision: number, metadata: CandidateMetadata, claimId?: string, reservationObjectKey?: string, replaces?: RefreshedRevision): Promise<PublicationResult>;
88
99
  invalidatePublishedRevision(keyHash: string, revision: number): Promise<TombstoneDrainResult>;
89
100
  getEntry(keyHash: string): StoredEntry | null;
90
101
  private getTagInvalidationMaximum;
91
102
  /** The earliest `expire` deadline still ahead of `now` among `tags`, or Infinity. */
92
103
  private getTagExpiryDeadline;
104
+ /** Whether an `expire` deadline among `tags` passed at or after `since`, by `now`. */
105
+ private hasTagExpiredSince;
93
106
  getTagExpiration(tags: string[]): number;
94
107
  findRefreshCandidates(options: ResponseStoreRefreshOptions): StoredEntry[];
95
108
  findRefreshCandidates(options: ResponseStoreRefreshOptions, projection: "reservation"): RefreshCandidate[];
@@ -98,6 +111,8 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
98
111
  * `invalidatedAt` also expires the tags for soft-tag reads; pass 0 to leave
99
112
  * their expiration unchanged. Like Next.js, `expiresAt` replaces the tags'
100
113
  * deadline for entries published before it; null keeps the current one.
114
+ * `staleAt` marks the tags stale for entries generated before it; pass 0 to
115
+ * leave it unchanged.
101
116
  */
102
117
  private recordInvalidations;
103
118
  purgeMatching(options: ResponseStorePurgeOptions, invalidatedAt?: number): Promise<PurgeReservation>;
@@ -116,4 +131,5 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
116
131
  markTombstonesEdgePurgedThrough(tombstoneSequence: number): void;
117
132
  inspect(): StoredEntry[];
118
133
  }
119
- //#endregion
134
+ //#endregion
135
+ export {};
@@ -78,7 +78,9 @@ var CacheMetadata = class extends DurableObject {
78
78
  revalidator_args TEXT,
79
79
  expiry_behavior TEXT,
80
80
  cache_tags TEXT,
81
- tombstoned INTEGER NOT NULL DEFAULT 0
81
+ tombstoned INTEGER NOT NULL DEFAULT 0,
82
+ replaced_revision INTEGER,
83
+ invalidated_revision INTEGER
82
84
  );
83
85
  CREATE INDEX IF NOT EXISTS entries_cache_key ON entries(cache_key);
84
86
  CREATE INDEX IF NOT EXISTS entries_active_object_key
@@ -101,7 +103,8 @@ var CacheMetadata = class extends DurableObject {
101
103
  tag TEXT PRIMARY KEY,
102
104
  invalidated_at INTEGER NOT NULL,
103
105
  invalidation_sequence INTEGER NOT NULL DEFAULT 0,
104
- expires_at INTEGER
106
+ expires_at INTEGER,
107
+ stale_at INTEGER NOT NULL DEFAULT 0
105
108
  ) WITHOUT ROWID;
106
109
  CREATE TABLE IF NOT EXISTS metadata_schema_migrations (
107
110
  version INTEGER PRIMARY KEY
@@ -138,8 +141,8 @@ var CacheMetadata = class extends DurableObject {
138
141
  ) WITHOUT ROWID;
139
142
  `);
140
143
  ctx.storage.transactionSync(() => {
141
- const migrations = new Set(ctx.storage.sql.exec("SELECT version FROM metadata_schema_migrations WHERE version IN (2, 3, 4, 5, 6, 7, 8)").toArray().map(({ version }) => version));
142
- if (migrations.size === 7) return;
144
+ const migrations = new Set(ctx.storage.sql.exec("SELECT version FROM metadata_schema_migrations WHERE version IN (2, 3, 4, 5, 6, 7, 8, 9, 10)").toArray().map(({ version }) => version));
145
+ if (migrations.size === 9) return;
143
146
  const schemas = ctx.storage.sql.exec(`SELECT name, sql FROM sqlite_schema
144
147
  WHERE type = 'table'
145
148
  AND name IN ('entries', 'tag_invalidations', 'metadata_state', 'pending_objects', 'pending_r2_tombstones')`).toArray();
@@ -183,6 +186,18 @@ var CacheMetadata = class extends DurableObject {
183
186
  `);
184
187
  ctx.storage.sql.exec("INSERT INTO metadata_schema_migrations (version) VALUES (8)");
185
188
  }
189
+ if (!migrations.has(9)) {
190
+ if (!(schemas.find(({ name }) => name === "entries")?.sql)?.includes("replaced_revision")) ctx.storage.sql.exec(`
191
+ ALTER TABLE entries ADD COLUMN replaced_revision INTEGER;
192
+ ALTER TABLE entries ADD COLUMN invalidated_revision INTEGER;
193
+ `);
194
+ ctx.storage.sql.exec("UPDATE entries SET invalidated_revision = active_revision");
195
+ ctx.storage.sql.exec("INSERT INTO metadata_schema_migrations (version) VALUES (9)");
196
+ }
197
+ if (!migrations.has(10)) {
198
+ if (!(schemas.find(({ name }) => name === "tag_invalidations")?.sql)?.includes("stale_at")) ctx.storage.sql.exec("ALTER TABLE tag_invalidations ADD COLUMN stale_at INTEGER NOT NULL DEFAULT 0");
199
+ ctx.storage.sql.exec("INSERT INTO metadata_schema_migrations (version) VALUES (10)");
200
+ }
186
201
  });
187
202
  ctx.storage.sql.exec(`
188
203
  CREATE INDEX IF NOT EXISTS pending_r2_tombstones_r2_pending
@@ -438,7 +453,7 @@ var CacheMetadata = class extends DurableObject {
438
453
  if (result?.reservation) await this.ensureCleanupAlarm(createdAt);
439
454
  return result;
440
455
  }
441
- async publish(keyHash, revision, metadata, claimId, reservationObjectKey = metadata.objectKey) {
456
+ async publish(keyHash, revision, metadata, claimId, reservationObjectKey = metadata.objectKey, replaces) {
442
457
  return this.ctx.storage.transactionSync(() => {
443
458
  const current = this.ctx.storage.sql.exec(`SELECT entries.*,
444
459
  revalidation_claims.active_revision AS claim_active_revision,
@@ -452,7 +467,7 @@ var CacheMetadata = class extends DurableObject {
452
467
  LEFT JOIN pending_objects ON pending_objects.object_key = ?
453
468
  LEFT JOIN revalidation_claims ON revalidation_claims.key_hash = entries.key_hash
454
469
  WHERE entries.key_hash = ?`, reservationObjectKey, keyHash).toArray()[0];
455
- if (!current || current.pending_invalidation_sequence === null || current.pending_invalidation_sequence === void 0 || current.pending_publishable !== 1 || revision > current.latest_revision || current.active_revision !== null && revision <= current.active_revision || claimId !== void 0 && (current.claim_id !== claimId || current.claim_revision !== revision || current.claim_active_revision !== current.active_revision) || metadata.fenceTags.length > 0 && current.current_invalidation_sequence > current.pending_invalidation_sequence && this.getTagInvalidationMaximum(metadata.fenceTags, "invalidation_sequence") > current.pending_invalidation_sequence) {
470
+ if (!current || current.pending_invalidation_sequence === null || current.pending_invalidation_sequence === void 0 || current.pending_publishable !== 1 || revision > current.latest_revision || current.active_revision !== null && revision <= current.active_revision || replaces !== void 0 && current.replaced_revision === replaces.revision && (current.invalidated_revision === null || current.invalidated_revision <= replaces.revision) && current.fresh_until !== null && Date.now() < current.fresh_until && (replaces.tags === void 0 || this.getTagExpiration(replaces.tags) <= replaces.tagExpiration) || claimId !== void 0 && (current.claim_id !== claimId || current.claim_revision !== revision || current.claim_active_revision !== current.active_revision) || metadata.fenceTags.length > 0 && current.current_invalidation_sequence > current.pending_invalidation_sequence && this.getTagInvalidationMaximum(metadata.fenceTags, "invalidation_sequence") > current.pending_invalidation_sequence) {
456
471
  if (claimId) this.ctx.storage.sql.exec("DELETE FROM revalidation_claims WHERE key_hash = ? AND claim_id = ?", keyHash, claimId);
457
472
  this.ctx.storage.sql.exec("DELETE FROM pending_objects WHERE object_key = ?", reservationObjectKey);
458
473
  return {
@@ -461,16 +476,29 @@ var CacheMetadata = class extends DurableObject {
461
476
  published: false
462
477
  };
463
478
  }
464
- const deadline = this.getTagExpiryDeadline(metadata.cacheTags, Date.now());
465
- const freshUntil = Math.min(metadata.freshUntil, deadline);
466
- const swrUntil = Math.min(metadata.swrUntil, deadline);
479
+ const now = Date.now();
480
+ const deadline = this.getTagExpiryDeadline(metadata.cacheTags, now);
481
+ let freshUntil = Math.min(metadata.freshUntil, deadline);
482
+ let swrUntil = Math.min(metadata.swrUntil, deadline);
483
+ let startedBeforeInvalidation = false;
484
+ if (metadata.generatedAt !== void 0) {
485
+ if (this.getTagInvalidationMaximum(metadata.cacheTags, "invalidated_at") >= metadata.generatedAt || this.hasTagExpiredSince(metadata.cacheTags, metadata.generatedAt, now)) {
486
+ freshUntil = Math.min(freshUntil, now);
487
+ swrUntil = Math.min(swrUntil, now);
488
+ startedBeforeInvalidation = true;
489
+ } else if (this.getTagInvalidationMaximum(metadata.cacheTags, "stale_at") >= metadata.generatedAt) {
490
+ freshUntil = Math.min(freshUntil, now);
491
+ startedBeforeInvalidation = true;
492
+ }
493
+ }
467
494
  const published = this.ctx.storage.sql.exec(`UPDATE entries SET
468
495
  active_revision = ?, object_key = ?, status_text = ?, response_headers = ?,
469
496
  fresh_until = ?, swr_until = ?,
470
- revalidator_id = ?, revalidator_args = ?, expiry_behavior = ?, cache_tags = ?, tombstoned = 0
497
+ revalidator_id = ?, revalidator_args = ?, expiry_behavior = ?, cache_tags = ?, tombstoned = 0,
498
+ replaced_revision = ?
471
499
  WHERE key_hash = ? AND latest_revision >= ?
472
500
  AND (active_revision IS NULL OR active_revision < ?)
473
- RETURNING key_hash`, revision, metadata.objectKey, metadata.statusText, JSON.stringify(metadata.responseHeaders), freshUntil, swrUntil, metadata.revalidator?.id ?? null, metadata.revalidator ? JSON.stringify(metadata.revalidator.args) : null, metadata.expiryBehavior === "miss" ? "miss" : null, JSON.stringify(metadata.cacheTags), keyHash, revision, revision).toArray().length === 1;
501
+ RETURNING key_hash`, revision, metadata.objectKey, metadata.statusText, JSON.stringify(metadata.responseHeaders), freshUntil, swrUntil, metadata.revalidator?.id ?? null, metadata.revalidator ? JSON.stringify(metadata.revalidator.args) : null, metadata.expiryBehavior === "miss" ? "miss" : null, JSON.stringify(metadata.cacheTags), replaces !== void 0 && current.active_revision !== null && replaces.revision <= current.active_revision ? replaces.revision : null, keyHash, revision, revision).toArray().length === 1;
474
502
  if (published) this.replaceEntryTags(keyHash, metadata.cacheTags);
475
503
  this.ctx.storage.sql.exec("DELETE FROM pending_objects WHERE object_key = ?", reservationObjectKey);
476
504
  if (claimId) this.ctx.storage.sql.exec("DELETE FROM revalidation_claims WHERE key_hash = ? AND claim_id = ?", keyHash, claimId);
@@ -488,10 +516,12 @@ var CacheMetadata = class extends DurableObject {
488
516
  expiryBehavior: metadata.expiryBehavior,
489
517
  cacheTags: metadata.cacheTags
490
518
  };
519
+ const replacedEdgeCopyExpired = current.revalidator_id === null && !current.tombstoned && current.fresh_until !== null && now >= current.fresh_until && this.ctx.storage.sql.exec("SELECT 1 FROM pending_r2_tombstones WHERE key_hash = ? AND edge_purge_complete = 0", keyHash).toArray().length === 0;
491
520
  return {
492
- edgePurgeRequired: published && current.active_revision !== null,
521
+ edgePurgeRequired: published && current.active_revision !== null && !replacedEdgeCopyExpired,
493
522
  entry: published ? entry : null,
494
- published
523
+ published,
524
+ ...published && (startedBeforeInvalidation || freshUntil <= now) ? { publishedStale: true } : {}
495
525
  };
496
526
  });
497
527
  }
@@ -508,8 +538,8 @@ var CacheMetadata = class extends DurableObject {
508
538
  latest_revision = ?, active_revision = ?, object_key = NULL,
509
539
  status_text = NULL, response_headers = NULL, fresh_until = NULL,
510
540
  swr_until = NULL, revalidator_id = NULL, revalidator_args = NULL,
511
- expiry_behavior = NULL, cache_tags = NULL, tombstoned = 1
512
- WHERE key_hash = ? AND active_revision = ?`, tombstoneRevision, tombstoneRevision, keyHash, revision);
541
+ expiry_behavior = NULL, cache_tags = NULL, tombstoned = 1, invalidated_revision = ?
542
+ WHERE key_hash = ? AND active_revision = ?`, tombstoneRevision, tombstoneRevision, tombstoneRevision, keyHash, revision);
513
543
  this.ctx.storage.sql.exec("DELETE FROM revalidation_claims WHERE key_hash = ?", keyHash);
514
544
  this.ctx.storage.sql.exec("DELETE FROM entry_tags WHERE key_hash = ?", keyHash);
515
545
  return true;
@@ -546,6 +576,13 @@ var CacheMetadata = class extends DurableObject {
546
576
  }
547
577
  return deadline;
548
578
  }
579
+ /** Whether an `expire` deadline among `tags` passed at or after `since`, by `now`. */
580
+ hasTagExpiredSince(tags, since, now) {
581
+ for (const batch of batches(normalizeTags(tags), 98)) if (this.ctx.storage.sql.exec(`SELECT 1 FROM tag_invalidations
582
+ WHERE expires_at >= ? AND expires_at <= ? AND tag IN (${batch.map(() => "?").join(", ")})
583
+ LIMIT 1`, since, now, ...batch).toArray().length) return true;
584
+ return false;
585
+ }
549
586
  getTagExpiration(tags) {
550
587
  return this.getTagInvalidationMaximum(tags, "invalidated_at");
551
588
  }
@@ -564,25 +601,29 @@ var CacheMetadata = class extends DurableObject {
564
601
  * `invalidatedAt` also expires the tags for soft-tag reads; pass 0 to leave
565
602
  * their expiration unchanged. Like Next.js, `expiresAt` replaces the tags'
566
603
  * deadline for entries published before it; null keeps the current one.
604
+ * `staleAt` marks the tags stale for entries generated before it; pass 0 to
605
+ * leave it unchanged.
567
606
  */
568
- recordInvalidations(keyHashes, tags, invalidatedAt, expiresAt) {
607
+ recordInvalidations(keyHashes, tags, invalidatedAt, expiresAt, staleAt) {
569
608
  this.ctx.storage.sql.exec(`UPDATE metadata_state SET tag_invalidation_sequence = tag_invalidation_sequence + 1
570
609
  WHERE singleton = 1`);
571
610
  for (const batch of batches(keyHashes, MAX_SQL_PARAMETERS)) this.ctx.storage.sql.exec(`INSERT INTO key_invalidations (key_hash, invalidation_sequence) VALUES ${batch.map(() => "(?, (SELECT tag_invalidation_sequence FROM metadata_state WHERE singleton = 1))").join(", ")}
572
611
  ON CONFLICT(key_hash) DO UPDATE SET invalidation_sequence =
573
612
  MAX(key_invalidations.invalidation_sequence, excluded.invalidation_sequence)`, ...batch);
574
- for (const batch of batches(tags, Math.floor(MAX_SQL_PARAMETERS / 3))) this.ctx.storage.sql.exec(`INSERT INTO tag_invalidations
575
- (tag, invalidated_at, expires_at, invalidation_sequence) VALUES ${batch.map(() => "(?, ?, ?, (SELECT tag_invalidation_sequence FROM metadata_state WHERE singleton = 1))").join(", ")}
613
+ for (const batch of batches(tags, Math.floor(MAX_SQL_PARAMETERS / 4))) this.ctx.storage.sql.exec(`INSERT INTO tag_invalidations
614
+ (tag, invalidated_at, expires_at, stale_at, invalidation_sequence) VALUES ${batch.map(() => "(?, ?, ?, ?, (SELECT tag_invalidation_sequence FROM metadata_state WHERE singleton = 1))").join(", ")}
576
615
  ON CONFLICT(tag) DO UPDATE SET invalidated_at =
577
616
  MAX(tag_invalidations.invalidated_at, excluded.invalidated_at),
578
617
  expires_at = COALESCE(excluded.expires_at, tag_invalidations.expires_at),
618
+ stale_at = MAX(tag_invalidations.stale_at, excluded.stale_at),
579
619
  invalidation_sequence = MAX(
580
620
  tag_invalidations.invalidation_sequence,
581
621
  excluded.invalidation_sequence
582
622
  )`, ...batch.flatMap((tag) => [
583
623
  tag,
584
624
  invalidatedAt,
585
- expiresAt
625
+ expiresAt,
626
+ staleAt
586
627
  ]));
587
628
  }
588
629
  async purgeMatching(options, invalidatedAt = Date.now()) {
@@ -590,7 +631,7 @@ var CacheMetadata = class extends DurableObject {
590
631
  const matches = this.findMatchingEntryRows(options, "purge");
591
632
  const tombstoneSequence = this.tombstoneSequence(matches.some((row) => row.object_key !== null));
592
633
  const tags = normalizeTags(options.tags ?? []);
593
- this.recordInvalidations(matches.map((row) => row.key_hash), tags, invalidatedAt, invalidatedAt);
634
+ this.recordInvalidations(matches.map((row) => row.key_hash), tags, invalidatedAt, invalidatedAt, 0);
594
635
  for (const batch of batches(matches, 99)) {
595
636
  const active = batch.filter((row) => row.object_key !== null);
596
637
  for (const entryBatch of batches(active, MAX_SQL_PARAMETERS / 5)) this.ctx.storage.sql.exec(`INSERT OR REPLACE INTO pending_r2_tombstones
@@ -615,7 +656,8 @@ var CacheMetadata = class extends DurableObject {
615
656
  revalidator_args = NULL,
616
657
  expiry_behavior = NULL,
617
658
  cache_tags = NULL,
618
- tombstoned = 1
659
+ tombstoned = 1,
660
+ invalidated_revision = latest_revision + 1
619
661
  WHERE key_hash IN (${placeholders})`, ...keyHashes);
620
662
  this.ctx.storage.sql.exec(`DELETE FROM revalidation_claims WHERE key_hash IN (${placeholders})`, ...keyHashes);
621
663
  this.ctx.storage.sql.exec(`DELETE FROM entry_tags WHERE key_hash IN (${placeholders})`, ...keyHashes);
@@ -642,7 +684,7 @@ var CacheMetadata = class extends DurableObject {
642
684
  const reserved = rows.filter((row) => !row.refreshable).map((row) => row.key_hash);
643
685
  const tags = normalizeTags(options.tags ?? []);
644
686
  const tombstoneSequence = this.tombstoneSequence(matches.length > 0);
645
- this.recordInvalidations(rows.map((row) => row.key_hash), tags, 0, expiresAt ?? null);
687
+ this.recordInvalidations(rows.map((row) => row.key_hash), tags, 0, expiresAt ?? null, invalidatedAt);
646
688
  for (const batch of batches(reserved, MAX_SQL_PARAMETERS)) this.ctx.storage.sql.exec(`UPDATE entries SET
647
689
  latest_revision = latest_revision + 1,
648
690
  active_revision = latest_revision + 1
@@ -684,7 +726,8 @@ var CacheMetadata = class extends DurableObject {
684
726
  latest_revision = latest_revision + 1,
685
727
  active_revision = latest_revision + 1,
686
728
  fresh_until = MIN(fresh_until, ?),
687
- swr_until = MIN(swr_until, COALESCE(?, swr_until))
729
+ swr_until = MIN(swr_until, COALESCE(?, swr_until)),
730
+ invalidated_revision = latest_revision + 1
688
731
  WHERE key_hash IN (${placeholders})`, invalidatedAt, expiresAt ?? null, ...keyHashes);
689
732
  this.ctx.storage.sql.exec(`DELETE FROM revalidation_claims WHERE key_hash IN (${placeholders})`, ...keyHashes);
690
733
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/workers-response-store",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "An R2-backed programmatic response store for Cloudflare Workers.",
5
5
  "homepage": "https://vinext.dev/",
6
6
  "license": "MIT",
@@ -28,10 +28,8 @@
28
28
  "@types/node": "^25.9.2",
29
29
  "miniflare": "5.20260730.0-alpha",
30
30
  "typescript": "^7.0.0",
31
- "vite": "npm:@voidzero-dev/vite-plus-core@1.1.0",
32
- "vite-plus": "1.1.0",
33
- "vitest": "5.0.3",
34
- "wrangler": "^4.118.0"
31
+ "vite-plus": "1.3.0",
32
+ "wrangler": "^4.153.0"
35
33
  },
36
34
  "engines": {
37
35
  "node": ">=22"
@@ -45,7 +43,7 @@
45
43
  "deploy": "npm run build && wrangler deploy",
46
44
  "deploy:service": "npm run build && wrangler deploy --config example/service-binding/wrangler.cache.jsonc && wrangler deploy --config example/service-binding/wrangler.user.jsonc",
47
45
  "lint": "vp lint .",
48
- "test": "npm run check && npm run build:examples && vitest run tests/e2e.test.ts tests/service-binding-e2e.test.ts",
49
- "test:live": "vitest run tests/live-e2e.test.ts tests/service-binding-live-e2e.test.ts"
46
+ "test": "npm run check && npm run build:examples && vp test run tests/e2e.test.ts tests/service-binding-e2e.test.ts",
47
+ "test:live": "vp test run tests/live-e2e.test.ts tests/service-binding-live-e2e.test.ts"
50
48
  }
51
49
  }