@cloudflare/workers-response-store 0.1.0-beta.1 → 1.0.0

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
@@ -123,8 +123,11 @@ export default {
123
123
  return render(request, env, ctx);
124
124
  }
125
125
 
126
- // Cache identity is pathname + query string. Use a canonical GET request
127
- // with only information that is safe to share between visitors.
126
+ // Cache identity is the key's pathname + query string. Use a canonical GET
127
+ // request with only information that is safe to share between visitors.
128
+ // This minimal sample keeps the full query, so every query variant gets its
129
+ // own entry. Leave out query parameters the response doesn't depend on, so
130
+ // requests that differ only in them share one entry.
128
131
  const cacheRequest = new Request(request.url);
129
132
  const cached = await responseStore.fetch(cacheRequest);
130
133
  if (!isResponseStoreMiss(cached)) {
@@ -192,9 +195,10 @@ type ResponseStoreMutationResult = {
192
195
  ### Cache keys
193
196
 
194
197
  - Keys must be `GET` requests.
195
- - Identity is the URL pathname plus query string; scheme and host are ignored.
198
+ - Identity is the key URL's pathname plus query string; scheme and host are ignored. The key doesn't have to be the visitor's URL.
196
199
  - Every key stores its response and read metadata together in one version-scoped R2 object by default, so fresh hits and misses do not query the metadata Durable Object. No special key format is required.
197
200
  - Build the key from trusted route and vary data. Do not include arbitrary visitor headers or other unbounded input unless it intentionally creates a distinct shared response.
201
+ - Leave out query parameters the response doesn't depend on, so requests that differ only in them share one entry.
198
202
 
199
203
  ### Freshness and tags
200
204
 
@@ -245,6 +249,19 @@ Each cache key and all its revisions, claims, and pending objects route to one d
245
249
 
246
250
  Changing the shard count selects a new metadata and R2 layout. Treat it as a cache-cold deployment change, not an in-place scaling control.
247
251
 
252
+ ### Metadata location
253
+
254
+ Both deployment modes accept a Durable Object location hint:
255
+
256
+ ```ts
257
+ const responseStore = createWorkersResponseStore<Env>({
258
+ locationHint: "weur",
259
+ regenerate,
260
+ });
261
+ ```
262
+
263
+ Use the same `locationHint` option with `createWorkersResponseStoreClient()` in service-binding mode. The hint is best-effort and only affects the first creation of each metadata Durable Object. Existing objects never move when this option changes, so roll out a changed hint as a cache-cold deployment and choose it alongside the R2 bucket's location.
264
+
248
265
  ## Storage and operational behavior
249
266
 
250
267
  ```text
package/dist/binding.d.ts CHANGED
@@ -57,6 +57,13 @@ export type StoredEntry = EntryMetadata & {
57
57
  activeRevision: number;
58
58
  latestRevision: number;
59
59
  };
60
+ export type RefreshCandidate = Pick<StoredEntry, "keyHash" | "cacheKey" | "activeRevision" | "latestRevision"> & ({
61
+ hasRevalidator: boolean;
62
+ revalidator?: never;
63
+ } | {
64
+ hasRevalidator?: never;
65
+ revalidator: RevalidatorDescriptor | null;
66
+ });
60
67
  export type PurgedEntry = {
61
68
  keyHash: string;
62
69
  cacheKey: string;
@@ -70,17 +77,21 @@ export type WorkersResponseStoreEnv = {
70
77
  CACHE_BODIES: R2Bucket;
71
78
  CACHE_METADATA: DurableObjectNamespace<undefined>;
72
79
  CF_VERSION_METADATA?: WorkerVersionMetadata;
80
+ WORKERS_RESPONSE_STORE_E2E_EDGE_PURGE_MODE?: "disabled";
73
81
  };
74
82
  export type WorkersResponseStoreProps = {
75
83
  versionId?: string;
76
- locationHint?: DurableObjectLocationHint;
84
+ locationHint?: ResponseStoreLocationHint;
77
85
  revalidator?: RevalidationService;
78
86
  shards?: number;
79
87
  };
88
+ export type ResponseStoreLocationHint = DurableObjectLocationHint;
89
+ export declare function validateResponseStoreLocationHint(locationHint: unknown): ResponseStoreLocationHint | undefined;
80
90
  export type ResponseStoreServiceProps = Pick<WorkersResponseStoreProps, "locationHint">;
81
91
  export type ResponseStoreServiceInvocation = {
82
92
  versionId: string;
83
93
  revalidator: RevalidationService;
94
+ locationHint?: ResponseStoreLocationHint;
84
95
  shards?: number;
85
96
  };
86
97
  export type ResponseStoreExecutionContext = Pick<ExecutionContext, "exports">;
package/dist/binding.js CHANGED
@@ -7,6 +7,23 @@ var R2PublicationError = class extends Error {
7
7
  super("R2 response publication failed", { cause });
8
8
  }
9
9
  };
10
+ const RESPONSE_STORE_LOCATION_HINTS = {
11
+ afr: true,
12
+ apac: true,
13
+ "apac-ne": true,
14
+ "apac-se": true,
15
+ eeur: true,
16
+ enam: true,
17
+ me: true,
18
+ oc: true,
19
+ sam: true,
20
+ weur: true,
21
+ wnam: true
22
+ };
23
+ function validateResponseStoreLocationHint(locationHint) {
24
+ if (locationHint !== void 0 && (typeof locationHint !== "string" || !Object.hasOwn(RESPONSE_STORE_LOCATION_HINTS, locationHint))) throw new TypeError("Workers Response Store locationHint is not supported by Cloudflare");
25
+ return locationHint;
26
+ }
10
27
  function getWorkersResponseStore(ctx, props = {}) {
11
28
  const binding = Reflect.get(ctx.exports, "ResponseStoreBinding");
12
29
  if (typeof binding !== "function") throw new Error("The ResponseStoreBinding entrypoint is not exported");
@@ -43,6 +60,7 @@ const MISS_HEADERS = {
43
60
  const BACKGROUND_REVALIDATION_LEASE_MS = 3e4;
44
61
  const CACHE_PURGE_BATCH_SIZE = 100;
45
62
  const PURGE_TOMBSTONE_BATCH_SIZE = 400;
63
+ const REFRESH_CONCURRENCY = 6;
46
64
  const ISOLATE_MISS_CACHE_CAPACITY = 1024;
47
65
  const ISOLATE_MISS_CACHE_TTL_MS = 1e3;
48
66
  const MAX_R2_CAS_ATTEMPTS = 3;
@@ -65,6 +83,28 @@ function validateResponseStoreShards(shards) {
65
83
  function* batches(values, size) {
66
84
  for (let offset = 0; offset < values.length; offset += size) yield values.slice(offset, offset + size);
67
85
  }
86
+ async function settleWithConcurrency(values, concurrency, operation) {
87
+ const results = Array.from({ length: values.length });
88
+ let nextIndex = 0;
89
+ async function worker() {
90
+ while (nextIndex < values.length) {
91
+ const index = nextIndex++;
92
+ try {
93
+ results[index] = {
94
+ status: "fulfilled",
95
+ value: await operation(values[index])
96
+ };
97
+ } catch (reason) {
98
+ results[index] = {
99
+ status: "rejected",
100
+ reason
101
+ };
102
+ }
103
+ }
104
+ }
105
+ await Promise.all(Array.from({ length: Math.min(concurrency, values.length) }, () => worker()));
106
+ return results;
107
+ }
68
108
  function metadataInteger(value) {
69
109
  if (!value || !/^\d+$/.test(value)) return;
70
110
  const parsed = Number(value);
@@ -195,6 +235,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
195
235
  };
196
236
  }
197
237
  async purgeEdgeCache(options) {
238
+ if (this.env.WORKERS_RESPONSE_STORE_E2E_EDGE_PURGE_MODE === "disabled") return false;
198
239
  if (!this.ctx.cache) {
199
240
  console.error(JSON.stringify({
200
241
  message: "Workers Response Store cache purge is unavailable",
@@ -222,12 +263,11 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
222
263
  async purgeR2TombstoneEdges(entries) {
223
264
  return await this.purgeEdgeCacheByTags(entries.map(purgeTagForEntry));
224
265
  }
225
- async purgePendingEdgeEntries(metadata, purgeByTag) {
226
- let accepted = true;
266
+ async purgePendingEdgeEntries(metadata, tombstoneSequence) {
227
267
  for (;;) {
228
- const entries = await metadata.listPendingEdgePurges(PURGE_TOMBSTONE_BATCH_SIZE);
229
- if (!entries.length) return accepted;
230
- if (purgeByTag && !await this.purgeEdgeCacheByTags(entries.map(purgeTagForEntry))) accepted = false;
268
+ const entries = await metadata.listPendingEdgePurges(PURGE_TOMBSTONE_BATCH_SIZE, tombstoneSequence);
269
+ if (!entries.length) return true;
270
+ if (!await this.purgeEdgeCacheByTags(entries.map(purgeTagForEntry))) return false;
231
271
  await metadata.markTombstonesEdgePurged(entries);
232
272
  }
233
273
  }
@@ -458,6 +498,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
458
498
  throw error;
459
499
  }
460
500
  if (!publication.published) return {
501
+ edgePurgeRequired: false,
461
502
  published: false,
462
503
  entry: this.readableEntry(publication.entry)
463
504
  };
@@ -487,6 +528,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
487
528
  }
488
529
  const entry = this.readableEntry(publication.entry);
489
530
  return {
531
+ edgePurgeRequired: publication.edgePurgeRequired,
490
532
  published: true,
491
533
  entry,
492
534
  ...stored && entry ? { response: this.createStoredResponse(entry, NULL_BODY_STATUSES.has(response.status) ? null : body, response.status, policy.createdAt, policy.initialAge) } : {}
@@ -620,7 +662,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
620
662
  response.body?.cancel().catch(() => {});
621
663
  return {
622
664
  backingStoreUpdated: true,
623
- edgePurgeAccepted: options.purgeExisting ? await this.purgeEdgeCacheByTags([purgeTagForEntry(result.entry)]) : true
665
+ edgePurgeAccepted: options.purgeExisting && result.edgePurgeRequired ? await this.purgeEdgeCacheByTags([purgeTagForEntry(result.entry)]) : true
624
666
  };
625
667
  }
626
668
  if (pendingPuts.get(pendingPutKey) === pending) pendingPuts.delete(pendingPutKey);
@@ -638,7 +680,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
638
680
  };
639
681
  return {
640
682
  backingStoreUpdated: true,
641
- edgePurgeAccepted: options.purgeExisting ? await this.purgeEdgeCacheByTags([purgeTagForEntry(result.entry)]) : true
683
+ edgePurgeAccepted: options.purgeExisting && result.edgePurgeRequired ? await this.purgeEdgeCacheByTags([purgeTagForEntry(result.entry)]) : true
642
684
  };
643
685
  } finally {
644
686
  if (options.coalesce && pendingPuts.get(pendingPutKey) === write) pendingPuts.delete(pendingPutKey);
@@ -647,12 +689,12 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
647
689
  async refresh(options) {
648
690
  if (!options.tags?.length && !options.pathPrefixes?.length) throw new TypeError("refresh() requires tags or pathPrefixes");
649
691
  const reserved = await Promise.allSettled(this.getMetadataShards().map(async (metadata) => ({
650
- candidates: await metadata.reserveRefresh(options, this.objectKeyRoot(), Date.now()),
692
+ candidates: await metadata.findRefreshCandidates(options, "reservation"),
651
693
  metadata
652
694
  })));
653
695
  const failures = reserved.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
654
- const candidates = reserved.flatMap((result) => result.status === "fulfilled" ? [result.value] : []).flatMap(({ candidates, metadata }) => candidates.map((candidate) => ({
655
- ...candidate,
696
+ const candidates = reserved.flatMap((result) => result.status === "fulfilled" ? [result.value] : []).flatMap(({ candidates, metadata }) => candidates.map((entry) => ({
697
+ entry,
656
698
  metadata
657
699
  })));
658
700
  if (candidates.length === 0) {
@@ -662,15 +704,18 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
662
704
  edgePurgeAccepted: false
663
705
  };
664
706
  }
665
- const settled = await Promise.allSettled(candidates.map(async ({ entry, metadata, reservation }) => {
666
- const result = await this.regenerateEntry(metadata, entry, "manual", reservation ? {
667
- cacheKey: entry.cacheKey,
668
- fenceTags: entry.cacheTags,
669
- keyHash: entry.keyHash,
670
- ...reservation
671
- } : void 0);
707
+ const settled = await settleWithConcurrency(candidates, REFRESH_CONCURRENCY, async ({ entry, metadata }) => {
708
+ if (!("hasRevalidator" in entry ? entry.hasRevalidator : entry.revalidator !== null)) throw new Error("Cache entry has no configured revalidator");
709
+ const regeneration = await metadata.reserveRegeneration(entry.keyHash, entry.cacheKey, this.objectKeyPrefix(entry.keyHash), Date.now(), entry.activeRevision, entry.latestRevision);
710
+ if (!regeneration?.reservation) return null;
711
+ const result = await this.regenerateEntry(metadata, regeneration.entry, "manual", {
712
+ cacheKey: regeneration.entry.cacheKey,
713
+ fenceTags: regeneration.entry.cacheTags,
714
+ keyHash: regeneration.entry.keyHash,
715
+ ...regeneration.reservation
716
+ });
672
717
  return result.published ? result.entry : null;
673
- }));
718
+ });
674
719
  const refreshed = [];
675
720
  for (const result of settled) if (result.status === "rejected") failures.push(result.reason);
676
721
  else if (result.value) refreshed.push(result.value);
@@ -694,7 +739,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
694
739
  const batchCount = Math.ceil(reservation.pendingTombstones / PURGE_TOMBSTONE_BATCH_SIZE);
695
740
  let cursor;
696
741
  for (let batch = 0; batch < batchCount; batch++) try {
697
- const drained = await metadata.drainPendingTombstones(PURGE_TOMBSTONE_BATCH_SIZE, void 0, cursor);
742
+ const drained = await metadata.drainPendingTombstones(PURGE_TOMBSTONE_BATCH_SIZE, void 0, cursor, reservation.tombstoneSequence);
698
743
  cursor = drained.cursor;
699
744
  failures.push(...drained.failures.map((failure) => new Error(failure)));
700
745
  } catch (error) {
@@ -704,15 +749,17 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
704
749
  let edgePurgeAccepted = true;
705
750
  if (options.purgeEverything && failures.length === 0) try {
706
751
  edgePurgeAccepted = await this.purgeEdgeCache({ purgeEverything: true });
707
- const acknowledged = await Promise.allSettled(reservations.map(({ metadata }) => this.purgePendingEdgeEntries(metadata, true)));
708
- for (const result of acknowledged) if (result.status === "rejected") failures.push(result.reason);
752
+ if (edgePurgeAccepted) {
753
+ const acknowledged = await Promise.allSettled(reservations.map(({ metadata, reservation }) => metadata.markTombstonesEdgePurgedThrough(reservation.tombstoneSequence)));
754
+ for (const result of acknowledged) if (result.status === "rejected") failures.push(result.reason);
755
+ }
709
756
  } catch (error) {
710
757
  edgePurgeAccepted = false;
711
758
  failures.push(error);
712
759
  }
713
760
  else {
714
761
  if (options.purgeEverything) edgePurgeAccepted = false;
715
- const acknowledged = await Promise.allSettled(reservations.map(({ metadata }) => this.purgePendingEdgeEntries(metadata, true)));
762
+ const acknowledged = await Promise.allSettled(reservations.filter(({ reservation }) => reservation.pendingTombstones > 0).map(({ metadata, reservation }) => this.purgePendingEdgeEntries(metadata, reservation.tombstoneSequence)));
716
763
  for (const result of acknowledged) if (result.status === "rejected") failures.push(result.reason);
717
764
  else if (!result.value) edgePurgeAccepted = false;
718
765
  }
@@ -724,4 +771,4 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
724
771
  }
725
772
  };
726
773
  //#endregion
727
- export { ResponseStoreBinding, ResponseStoreService, getWorkersResponseStore, validateResponseStoreShards };
774
+ export { ResponseStoreBinding, ResponseStoreService, getWorkersResponseStore, validateResponseStoreLocationHint, validateResponseStoreShards };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ResponseStoreBinding, ResponseStoreMutationResult, ResponseStorePurgeOptions, ResponseStorePutOptions, ResponseStoreRefreshOptions, RevalidationInput, RevalidationReason, RevalidationService, SerializableValue, WorkersResponseStore, WorkersResponseStoreEnv } from "./binding.js";
1
+ import { ResponseStoreBinding, ResponseStoreLocationHint, ResponseStoreMutationResult, ResponseStorePurgeOptions, ResponseStorePutOptions, ResponseStoreRefreshOptions, RevalidationInput, RevalidationReason, RevalidationService, SerializableValue, WorkersResponseStore, WorkersResponseStoreEnv } from "./binding.js";
2
2
  import { CacheMetadata } from "./metadata-do.js";
3
3
  import { WorkerEntrypoint } from "cloudflare:workers";
4
4
  //#region src/index.d.ts
@@ -22,6 +22,12 @@ type WorkersResponseStoreClientDefinition<Env> = WorkersResponseStore & {
22
22
  };
23
23
  export type ResponseStoreClientEntrypoint<Env = WorkersResponseStoreClientEnv> = new (ctx: ExecutionContext, env: Env) => WorkerEntrypoint<Env> & WorkersResponseStore;
24
24
  export type WorkersResponseStoreOptions<Env> = {
25
+ /**
26
+ * Best-effort location for metadata Durable Objects on first creation.
27
+ * Changing this does not move existing objects and should be treated as a
28
+ * cache-cold deployment change.
29
+ */
30
+ locationHint?: ResponseStoreLocationHint;
25
31
  /** Split version-scoped metadata across this many Durable Objects. */
26
32
  shards?: number;
27
33
  regenerate(input: RevalidationInput, context: ResponseStoreRevalidationContext<Env>): Response | Promise<Response>;
@@ -33,4 +39,4 @@ export type WorkersResponseStoreClientEnv = {
33
39
  };
34
40
  export declare function createWorkersResponseStoreClient<Env extends WorkersResponseStoreClientEnv = WorkersResponseStoreClientEnv>(options: WorkersResponseStoreOptions<Env>): WorkersResponseStoreClientDefinition<Env>;
35
41
  //#endregion
36
- export type { ResponseStoreMutationResult, ResponseStorePurgeOptions, ResponseStorePutOptions, ResponseStoreRefreshOptions, RevalidationInput, RevalidationReason, SerializableValue, WorkersResponseStore, WorkersResponseStoreEnv };
42
+ export type { ResponseStoreLocationHint, ResponseStoreMutationResult, ResponseStorePurgeOptions, ResponseStorePutOptions, ResponseStoreRefreshOptions, RevalidationInput, RevalidationReason, SerializableValue, WorkersResponseStore, WorkersResponseStoreEnv };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ResponseStoreBinding, getWorkersResponseStore, validateResponseStoreShards } from "./binding.js";
1
+ import { ResponseStoreBinding, getWorkersResponseStore, validateResponseStoreLocationHint, validateResponseStoreShards } from "./binding.js";
2
2
  import { CacheMetadata } from "./metadata-do.js";
3
3
  import { WorkerEntrypoint, exports } from "cloudflare:workers";
4
4
  //#region src/index.ts
@@ -22,9 +22,13 @@ function createStoreFacade(getStore) {
22
22
  };
23
23
  }
24
24
  function createWorkersResponseStore(options) {
25
+ const locationHint = validateResponseStoreLocationHint(options.locationHint);
25
26
  const shards = validateResponseStoreShards(options.shards);
26
27
  const ResponseStoreRevalidator = createRevalidatorEntrypoint(options);
27
- const getStore = () => getWorkersResponseStore({ exports }, { shards });
28
+ const getStore = () => getWorkersResponseStore({ exports }, {
29
+ locationHint,
30
+ shards
31
+ });
28
32
  return {
29
33
  entrypoints: {
30
34
  CacheMetadata,
@@ -35,6 +39,7 @@ function createWorkersResponseStore(options) {
35
39
  };
36
40
  }
37
41
  function createWorkersResponseStoreClient(options) {
42
+ const locationHint = validateResponseStoreLocationHint(options.locationHint);
38
43
  const shards = validateResponseStoreShards(options.shards);
39
44
  const ResponseStoreRevalidator = createRevalidatorEntrypoint(options);
40
45
  class ResponseStoreClient extends WorkerEntrypoint {
@@ -49,6 +54,7 @@ function createWorkersResponseStoreClient(options) {
49
54
  return {
50
55
  versionId,
51
56
  revalidator: factory({ props: {} }),
57
+ ...locationHint === void 0 ? {} : { locationHint },
52
58
  ...shards === void 0 ? {} : { shards }
53
59
  };
54
60
  }
@@ -1,4 +1,4 @@
1
- import { CandidateMetadata, PurgedEntry, ResponseStorePurgeOptions, ResponseStoreRefreshOptions, StoredEntry } from "./binding.js";
1
+ import { CandidateMetadata, PurgedEntry, RefreshCandidate, ResponseStorePurgeOptions, ResponseStoreRefreshOptions, StoredEntry } from "./binding.js";
2
2
  import { DurableObject } from "cloudflare:workers";
3
3
  //#region src/metadata-do.d.ts
4
4
  type RevalidationClaim = {
@@ -8,6 +8,7 @@ type RevalidationClaim = {
8
8
  revision: number;
9
9
  };
10
10
  type PublicationResult = {
11
+ edgePurgeRequired: boolean;
11
12
  entry: StoredEntry | null;
12
13
  published: boolean;
13
14
  };
@@ -16,10 +17,6 @@ type WriteReservation = {
16
17
  r2ObjectAbsent: boolean;
17
18
  revision: number;
18
19
  };
19
- type RefreshCandidate = {
20
- entry: StoredEntry;
21
- reservation?: Pick<WriteReservation, "objectKey" | "revision">;
22
- };
23
20
  type RegenerationReservation = {
24
21
  entry: StoredEntry;
25
22
  reservation?: WriteReservation;
@@ -27,6 +24,7 @@ type RegenerationReservation = {
27
24
  type PurgeReservation = {
28
25
  backingStoreUpdated: boolean;
29
26
  pendingTombstones: number;
27
+ tombstoneSequence: number;
30
28
  };
31
29
  type TombstoneDrainResult = {
32
30
  cursor?: string;
@@ -37,7 +35,7 @@ type TombstoneDrainResult = {
37
35
  export type CacheMetadataStub = DurableObjectStub & {
38
36
  reserveWrite(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number): Promise<WriteReservation>;
39
37
  replaceFailedWrite(keyHash: string, cacheKey: string, objectKeyPrefix: string, failedObjectKey: string, fenceTags: string[], createdAt: number): Promise<WriteReservation | null>;
40
- reserveRegeneration(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number): Promise<RegenerationReservation | null>;
38
+ reserveRegeneration(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number, expectedActiveRevision?: number, expectedLatestRevision?: number): Promise<RegenerationReservation | null>;
41
39
  claimRevalidation(keyHash: string, activeRevision: number, cacheKey: string, objectKeyPrefix: string, now: number, leaseMs: number): Promise<RevalidationClaim | null>;
42
40
  trackPendingObject(objectKey: string, createdAt: number): Promise<void>;
43
41
  trackPendingObjects(objectKeys: string[], createdAt: number): Promise<void>;
@@ -49,12 +47,14 @@ export type CacheMetadataStub = DurableObjectStub & {
49
47
  invalidatePublishedRevision(keyHash: string, revision: number): Promise<TombstoneDrainResult>;
50
48
  getEntry(keyHash: string): Promise<StoredEntry | null>;
51
49
  getTagExpiration(tags: string[]): Promise<number>;
52
- reserveRefresh(options: ResponseStoreRefreshOptions, objectKeyRoot: string, createdAt: number): Promise<RefreshCandidate[]>;
50
+ findRefreshCandidates(options: ResponseStoreRefreshOptions): Promise<StoredEntry[]>;
51
+ findRefreshCandidates(options: ResponseStoreRefreshOptions, projection: "reservation"): Promise<RefreshCandidate[]>;
53
52
  purgeMatching(options: ResponseStorePurgeOptions, invalidatedAt?: number): Promise<PurgeReservation>;
54
- drainPendingTombstones(limit: number, keyHash?: string, afterKeyHash?: string): Promise<TombstoneDrainResult>;
53
+ drainPendingTombstones(limit: number, keyHash?: string, afterKeyHash?: string, tombstoneSequence?: number): Promise<TombstoneDrainResult>;
55
54
  retryPendingTombstones(): Promise<boolean>;
56
- listPendingEdgePurges(limit: number): Promise<PurgedEntry[]>;
55
+ listPendingEdgePurges(limit: number, tombstoneSequence?: number): Promise<PurgedEntry[]>;
57
56
  markTombstonesEdgePurged(entries: PurgedEntry[]): Promise<void>;
57
+ markTombstonesEdgePurgedThrough(tombstoneSequence: number): Promise<void>;
58
58
  inspect(): Promise<StoredEntry[]>;
59
59
  };
60
60
  type CacheMetadataEnv = {
@@ -66,8 +66,10 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
66
66
  private ensureCleanupAlarm;
67
67
  private scheduleCleanupAlarm;
68
68
  private logCleanupFailure;
69
+ private tombstoneSequence;
69
70
  private getR2TombstoneEdgePurger;
70
71
  private findMatchingEntryRows;
72
+ private replaceEntryTags;
71
73
  trackPendingObjects(objectKeys: string[], createdAt: number): Promise<void>;
72
74
  trackPendingObject(objectKey: string, createdAt: number): Promise<void>;
73
75
  releaseWrite(keyHash: string, objectKey: string, claimId?: string): void;
@@ -80,19 +82,21 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
80
82
  private reserveRevision;
81
83
  reserveWrite(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number): Promise<WriteReservation>;
82
84
  replaceFailedWrite(keyHash: string, cacheKey: string, objectKeyPrefix: string, failedObjectKey: string, fenceTags: string[], createdAt: number): Promise<WriteReservation | null>;
83
- reserveRegeneration(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number): Promise<RegenerationReservation | null>;
85
+ reserveRegeneration(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number, expectedActiveRevision?: number, expectedLatestRevision?: number): Promise<RegenerationReservation | null>;
84
86
  publish(keyHash: string, revision: number, metadata: CandidateMetadata, claimId?: string, reservationObjectKey?: string): Promise<PublicationResult>;
85
87
  invalidatePublishedRevision(keyHash: string, revision: number): Promise<TombstoneDrainResult>;
86
88
  getEntry(keyHash: string): StoredEntry | null;
87
89
  private getTagInvalidationMaximum;
88
90
  getTagExpiration(tags: string[]): number;
89
- reserveRefresh(options: ResponseStoreRefreshOptions, objectKeyRoot: string, createdAt: number): Promise<RefreshCandidate[]>;
91
+ findRefreshCandidates(options: ResponseStoreRefreshOptions): StoredEntry[];
92
+ findRefreshCandidates(options: ResponseStoreRefreshOptions, projection: "reservation"): RefreshCandidate[];
90
93
  purgeMatching(options: ResponseStorePurgeOptions, invalidatedAt?: number): Promise<PurgeReservation>;
91
94
  private writeR2Tombstone;
92
95
  private finishCompletedTombstones;
93
- drainPendingTombstones(limit: number, keyHash?: string, afterKeyHash?: string): Promise<TombstoneDrainResult>;
94
- listPendingEdgePurges(limit: number): PurgedEntry[];
96
+ drainPendingTombstones(limit: number, keyHash?: string, afterKeyHash?: string, tombstoneSequence?: number): Promise<TombstoneDrainResult>;
97
+ listPendingEdgePurges(limit: number, tombstoneSequence?: number): PurgedEntry[];
95
98
  markTombstonesEdgePurged(entries: PurgedEntry[]): void;
99
+ markTombstonesEdgePurgedThrough(tombstoneSequence: number): void;
96
100
  inspect(): StoredEntry[];
97
101
  }
98
102
  //#endregion
@@ -1,3 +1,4 @@
1
+ import { mapSettledWithR2Concurrency } from "./r2-concurrency.js";
1
2
  import { DurableObject } from "cloudflare:workers";
2
3
  //#region src/metadata-do.ts
3
4
  const MAX_SQL_PARAMETERS = 100;
@@ -68,6 +69,12 @@ var CacheMetadata = class extends DurableObject {
68
69
  CREATE INDEX IF NOT EXISTS entries_cache_key ON entries(cache_key);
69
70
  CREATE INDEX IF NOT EXISTS entries_active_object_key
70
71
  ON entries(object_key) WHERE tombstoned = 0;
72
+ CREATE TABLE IF NOT EXISTS entry_tags (
73
+ tag TEXT NOT NULL,
74
+ key_hash TEXT NOT NULL,
75
+ PRIMARY KEY (tag, key_hash)
76
+ ) WITHOUT ROWID;
77
+ CREATE INDEX IF NOT EXISTS entry_tags_key_hash ON entry_tags(key_hash);
71
78
  CREATE TABLE IF NOT EXISTS revalidation_claims (
72
79
  key_hash TEXT PRIMARY KEY,
73
80
  active_revision INTEGER NOT NULL,
@@ -86,7 +93,8 @@ var CacheMetadata = class extends DurableObject {
86
93
  );
87
94
  CREATE TABLE IF NOT EXISTS metadata_state (
88
95
  singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
89
- tag_invalidation_sequence INTEGER NOT NULL
96
+ tag_invalidation_sequence INTEGER NOT NULL,
97
+ tombstone_sequence INTEGER NOT NULL DEFAULT 0
90
98
  );
91
99
  INSERT OR IGNORE INTO metadata_state (singleton, tag_invalidation_sequence) VALUES (1, 0);
92
100
  CREATE TABLE IF NOT EXISTS key_invalidations (
@@ -106,15 +114,16 @@ var CacheMetadata = class extends DurableObject {
106
114
  object_key TEXT NOT NULL,
107
115
  revision INTEGER NOT NULL,
108
116
  r2_complete INTEGER NOT NULL DEFAULT 0,
109
- edge_purge_complete INTEGER NOT NULL DEFAULT 0
117
+ edge_purge_complete INTEGER NOT NULL DEFAULT 0,
118
+ tombstone_sequence INTEGER NOT NULL DEFAULT 0
110
119
  ) WITHOUT ROWID;
111
120
  `);
112
121
  ctx.storage.transactionSync(() => {
113
- const migrations = new Set(ctx.storage.sql.exec("SELECT version FROM metadata_schema_migrations WHERE version IN (2, 3, 4)").toArray().map(({ version }) => version));
114
- if (migrations.size === 3) return;
122
+ const migrations = new Set(ctx.storage.sql.exec("SELECT version FROM metadata_schema_migrations WHERE version IN (2, 3, 4, 5, 6)").toArray().map(({ version }) => version));
123
+ if (migrations.size === 5) return;
115
124
  const schemas = ctx.storage.sql.exec(`SELECT name, sql FROM sqlite_schema
116
125
  WHERE type = 'table'
117
- AND name IN ('tag_invalidations', 'pending_objects', 'pending_r2_tombstones')`).toArray();
126
+ AND name IN ('tag_invalidations', 'metadata_state', 'pending_objects', 'pending_r2_tombstones')`).toArray();
118
127
  if (!migrations.has(2) && !schemas.find(({ name }) => name === "tag_invalidations")?.sql.includes("invalidation_sequence")) ctx.storage.sql.exec("ALTER TABLE tag_invalidations ADD COLUMN invalidation_sequence INTEGER NOT NULL DEFAULT 0");
119
128
  if (!migrations.has(2) && !schemas.find(({ name }) => name === "pending_objects")?.sql.includes("invalidation_sequence")) ctx.storage.sql.exec("ALTER TABLE pending_objects ADD COLUMN invalidation_sequence INTEGER NOT NULL DEFAULT 0");
120
129
  if (!migrations.has(2)) ctx.storage.sql.exec("INSERT INTO metadata_schema_migrations (version) VALUES (2)");
@@ -128,7 +137,33 @@ var CacheMetadata = class extends DurableObject {
128
137
  if (!tombstones?.includes("edge_purge_complete")) ctx.storage.sql.exec("ALTER TABLE pending_r2_tombstones ADD COLUMN edge_purge_complete INTEGER NOT NULL DEFAULT 0");
129
138
  ctx.storage.sql.exec("INSERT INTO metadata_schema_migrations (version) VALUES (4)");
130
139
  }
140
+ if (!migrations.has(5)) {
141
+ const metadataState = schemas.find(({ name }) => name === "metadata_state")?.sql;
142
+ const tombstones = schemas.find(({ name }) => name === "pending_r2_tombstones")?.sql;
143
+ if (!metadataState?.includes("tombstone_sequence")) ctx.storage.sql.exec("ALTER TABLE metadata_state ADD COLUMN tombstone_sequence INTEGER NOT NULL DEFAULT 0");
144
+ if (!tombstones?.includes("tombstone_sequence")) ctx.storage.sql.exec("ALTER TABLE pending_r2_tombstones ADD COLUMN tombstone_sequence INTEGER NOT NULL DEFAULT 0");
145
+ ctx.storage.sql.exec("INSERT INTO metadata_schema_migrations (version) VALUES (5)");
146
+ }
147
+ if (!migrations.has(6)) {
148
+ ctx.storage.sql.exec("DELETE FROM entry_tags");
149
+ const entries = ctx.storage.sql.exec("SELECT key_hash, cache_tags FROM entries WHERE cache_tags IS NOT NULL");
150
+ for (const entry of entries) this.replaceEntryTags(entry.key_hash, JSON.parse(entry.cache_tags));
151
+ ctx.storage.sql.exec("INSERT INTO metadata_schema_migrations (version) VALUES (6)");
152
+ }
131
153
  });
154
+ ctx.storage.sql.exec(`
155
+ CREATE INDEX IF NOT EXISTS pending_r2_tombstones_r2_pending
156
+ ON pending_r2_tombstones(key_hash) WHERE r2_complete = 0;
157
+ CREATE INDEX IF NOT EXISTS pending_r2_tombstones_edge_pending
158
+ ON pending_r2_tombstones(key_hash)
159
+ WHERE r2_complete = 1 AND edge_purge_complete = 0;
160
+ CREATE INDEX IF NOT EXISTS pending_r2_tombstones_r2_sequence
161
+ ON pending_r2_tombstones(tombstone_sequence, key_hash)
162
+ WHERE r2_complete = 0;
163
+ CREATE INDEX IF NOT EXISTS pending_r2_tombstones_edge_sequence
164
+ ON pending_r2_tombstones(tombstone_sequence, key_hash)
165
+ WHERE r2_complete = 1 AND edge_purge_complete = 0;
166
+ `);
132
167
  });
133
168
  }
134
169
  async ensureCleanupAlarm(createdAt) {
@@ -146,17 +181,48 @@ var CacheMetadata = class extends DurableObject {
146
181
  error: error instanceof Error ? error.message : String(error)
147
182
  }));
148
183
  }
184
+ tombstoneSequence(increment = false) {
185
+ const statement = increment ? `UPDATE metadata_state SET tombstone_sequence = tombstone_sequence + 1
186
+ WHERE singleton = 1 RETURNING tombstone_sequence` : "SELECT tombstone_sequence FROM metadata_state WHERE singleton = 1";
187
+ return this.ctx.storage.sql.exec(statement).one().tombstone_sequence;
188
+ }
149
189
  getR2TombstoneEdgePurger() {
150
190
  const factory = Reflect.get(this.ctx.exports, "ResponseStoreBinding");
151
191
  if (typeof factory !== "function") throw new Error("The ResponseStoreBinding entrypoint is not exported");
152
192
  return factory({ props: {} });
153
193
  }
154
- findMatchingEntryRows(options, includePending = false) {
155
- const rows = this.ctx.storage.sql.exec(includePending ? "SELECT * FROM entries" : "SELECT * FROM entries WHERE tombstoned = 0 AND active_revision IS NOT NULL").toArray();
156
- if (options.purgeEverything) return rows;
157
- const tags = new Set(normalizeTags(options.tags ?? []));
158
- const prefixes = options.pathPrefixes ?? [];
159
- return rows.filter((row) => prefixes.some((prefix) => row.cache_key.startsWith(prefix)) || normalizeTags(JSON.parse(row.cache_tags ?? "[]")).some((tag) => tags.has(tag)));
194
+ findMatchingEntryRows(options, projection = "entry") {
195
+ const selectors = [];
196
+ const parameters = [];
197
+ if (!options.purgeEverything) {
198
+ const tags = normalizeTags(options.tags ?? []);
199
+ if (tags.length) {
200
+ selectors.push(`entries.key_hash IN (
201
+ SELECT key_hash FROM entry_tags
202
+ WHERE tag IN (SELECT value FROM json_each(?))
203
+ )`);
204
+ parameters.push(JSON.stringify(tags));
205
+ }
206
+ if (options.pathPrefixes?.length) {
207
+ selectors.push(`entries.key_hash IN (
208
+ SELECT path_entry.key_hash FROM json_each(?) AS path_prefix
209
+ JOIN entries AS path_entry
210
+ ON path_entry.cache_key >= path_prefix.value
211
+ AND path_entry.cache_key < path_prefix.value || char(127)
212
+ )`);
213
+ parameters.push(JSON.stringify(options.pathPrefixes));
214
+ }
215
+ }
216
+ if (!options.purgeEverything && !selectors.length) return [];
217
+ const conditions = [];
218
+ if (projection !== "purge") conditions.push("tombstoned = 0 AND active_revision IS NOT NULL");
219
+ if (projection === "refresh") conditions.push("object_key IS NOT NULL AND response_headers IS NOT NULL AND fresh_until IS NOT NULL AND swr_until IS NOT NULL");
220
+ if (!options.purgeEverything) conditions.push(`(${selectors.join(" OR ")})`);
221
+ return this.ctx.storage.sql.exec(`SELECT ${projection === "purge" ? "key_hash, cache_key, latest_revision, object_key" : projection === "refresh" ? "key_hash, cache_key, active_revision, latest_revision, revalidator_id IS NOT NULL AS has_revalidator" : "*"} FROM entries ${conditions.length ? `WHERE ${conditions.join(" AND ")}` : ""}`, ...parameters).toArray();
222
+ }
223
+ replaceEntryTags(keyHash, tags) {
224
+ this.ctx.storage.sql.exec("DELETE FROM entry_tags WHERE key_hash = ?", keyHash);
225
+ for (const batch of batches(normalizeTags(tags), MAX_SQL_PARAMETERS / 2)) this.ctx.storage.sql.exec(`INSERT INTO entry_tags (tag, key_hash) VALUES ${batch.map(() => "(?, ?)").join(", ")}`, ...batch.flatMap((tag) => [tag, keyHash]));
160
226
  }
161
227
  async trackPendingObjects(objectKeys, createdAt) {
162
228
  if (!objectKeys.length) return;
@@ -237,7 +303,7 @@ var CacheMetadata = class extends DurableObject {
237
303
  if (!await this.getR2TombstoneEdgePurger().purgeR2TombstoneEdges(edgeEntries)) throw new Error("Workers Response Store cache purge is unavailable");
238
304
  this.markTombstonesEdgePurged(edgeEntries);
239
305
  }
240
- return this.ctx.storage.sql.exec("SELECT COUNT(*) AS count FROM pending_r2_tombstones").one().count > 0;
306
+ return this.ctx.storage.sql.exec("SELECT EXISTS(SELECT 1 FROM pending_r2_tombstones) AS pending").one().pending === 1;
241
307
  }
242
308
  async claimRevalidation(keyHash, activeRevision, cacheKey, objectKeyPrefix, now, leaseMs) {
243
309
  const claim = this.ctx.storage.transactionSync(() => {
@@ -315,11 +381,12 @@ var CacheMetadata = class extends DurableObject {
315
381
  if (reservation) await this.ensureCleanupAlarm(createdAt);
316
382
  return reservation;
317
383
  }
318
- async reserveRegeneration(keyHash, cacheKey, objectKeyPrefix, createdAt) {
384
+ async reserveRegeneration(keyHash, cacheKey, objectKeyPrefix, createdAt, expectedActiveRevision, expectedLatestRevision) {
319
385
  const result = this.ctx.storage.transactionSync(() => {
320
386
  const current = this.ctx.storage.sql.exec("SELECT * FROM entries WHERE key_hash = ? AND cache_key = ?", keyHash, cacheKey).toArray()[0];
321
387
  const entry = current ? storedEntryFromRow(current) : null;
322
388
  if (!entry) return null;
389
+ if (expectedActiveRevision !== void 0 && entry.activeRevision !== expectedActiveRevision || expectedLatestRevision !== void 0 && entry.latestRevision !== expectedLatestRevision) return null;
323
390
  if (!entry.revalidator) return { entry };
324
391
  const revision = this.reserveRevision(keyHash, cacheKey, current);
325
392
  const objectKey = `${objectKeyPrefix}/${revision}`;
@@ -356,6 +423,7 @@ var CacheMetadata = class extends DurableObject {
356
423
  if (claimId) this.ctx.storage.sql.exec("DELETE FROM revalidation_claims WHERE key_hash = ? AND claim_id = ?", keyHash, claimId);
357
424
  this.ctx.storage.sql.exec("DELETE FROM pending_objects WHERE object_key = ?", reservationObjectKey);
358
425
  return {
426
+ edgePurgeRequired: false,
359
427
  entry: current ? storedEntryFromRow(current) : null,
360
428
  published: false
361
429
  };
@@ -367,6 +435,7 @@ var CacheMetadata = class extends DurableObject {
367
435
  WHERE key_hash = ? AND latest_revision >= ?
368
436
  AND (active_revision IS NULL OR active_revision < ?)
369
437
  RETURNING key_hash`, revision, metadata.objectKey, metadata.statusText, JSON.stringify(metadata.responseHeaders), metadata.freshUntil, metadata.swrUntil, metadata.revalidator?.id ?? null, metadata.revalidator ? JSON.stringify(metadata.revalidator.args) : null, JSON.stringify(metadata.cacheTags), keyHash, revision, revision).toArray().length === 1;
438
+ if (published) this.replaceEntryTags(keyHash, metadata.cacheTags);
370
439
  this.ctx.storage.sql.exec("DELETE FROM pending_objects WHERE object_key = ?", reservationObjectKey);
371
440
  if (claimId) this.ctx.storage.sql.exec("DELETE FROM revalidation_claims WHERE key_hash = ? AND claim_id = ?", keyHash, claimId);
372
441
  const entry = {
@@ -383,6 +452,7 @@ var CacheMetadata = class extends DurableObject {
383
452
  cacheTags: metadata.cacheTags
384
453
  };
385
454
  return {
455
+ edgePurgeRequired: published && current.active_revision !== null,
386
456
  entry: published ? entry : null,
387
457
  published
388
458
  };
@@ -393,8 +463,10 @@ var CacheMetadata = class extends DurableObject {
393
463
  const current = this.ctx.storage.sql.exec("SELECT * FROM entries WHERE key_hash = ?", keyHash).toArray()[0];
394
464
  if (!current || current.active_revision !== revision || current.object_key === null) return false;
395
465
  const tombstoneRevision = current.latest_revision + 1;
466
+ const tombstoneSequence = this.tombstoneSequence(true);
396
467
  this.ctx.storage.sql.exec(`INSERT OR REPLACE INTO pending_r2_tombstones
397
- (key_hash, cache_key, object_key, revision) VALUES (?, ?, ?, ?)`, keyHash, current.cache_key, current.object_key, tombstoneRevision);
468
+ (key_hash, cache_key, object_key, revision, tombstone_sequence)
469
+ VALUES (?, ?, ?, ?, ?)`, keyHash, current.cache_key, current.object_key, tombstoneRevision, tombstoneSequence);
398
470
  this.ctx.storage.sql.exec(`UPDATE entries SET
399
471
  latest_revision = ?, active_revision = ?, object_key = NULL,
400
472
  status_text = NULL, response_headers = NULL, fresh_until = NULL,
@@ -402,6 +474,7 @@ var CacheMetadata = class extends DurableObject {
402
474
  cache_tags = NULL, tombstoned = 1
403
475
  WHERE key_hash = ? AND active_revision = ?`, tombstoneRevision, tombstoneRevision, keyHash, revision);
404
476
  this.ctx.storage.sql.exec("DELETE FROM revalidation_claims WHERE key_hash = ?", keyHash);
477
+ this.ctx.storage.sql.exec("DELETE FROM entry_tags WHERE key_hash = ?", keyHash);
405
478
  return true;
406
479
  })) return {
407
480
  failures: [],
@@ -429,43 +502,20 @@ var CacheMetadata = class extends DurableObject {
429
502
  getTagExpiration(tags) {
430
503
  return this.getTagInvalidationMaximum(tags, "invalidated_at");
431
504
  }
432
- async reserveRefresh(options, objectKeyRoot, createdAt) {
433
- const candidates = this.ctx.storage.transactionSync(() => {
434
- const matches = this.findMatchingEntryRows(options);
435
- const reservations = matches.flatMap((row) => {
436
- return storedEntryFromRow(row)?.revalidator ? [{
437
- keyHash: row.key_hash,
438
- objectKey: `${objectKeyRoot}/${row.key_hash}/${row.latest_revision + 1}`,
439
- revision: row.latest_revision + 1
440
- }] : [];
441
- });
442
- for (const batch of batches(reservations, MAX_SQL_PARAMETERS)) {
443
- const keyHashes = batch.map(({ keyHash }) => keyHash);
444
- this.ctx.storage.sql.exec(`UPDATE entries SET latest_revision = latest_revision + 1
445
- WHERE key_hash IN (${keyHashes.map(() => "?").join(", ")})`, ...keyHashes);
446
- }
447
- for (const batch of batches(reservations, MAX_SQL_PARAMETERS / 2)) this.ctx.storage.sql.exec(`INSERT OR REPLACE INTO pending_objects
448
- (object_key, created_at, invalidation_sequence) VALUES ${batch.map(() => "(?, ?, (SELECT tag_invalidation_sequence FROM metadata_state WHERE singleton = 1))").join(", ")}`, ...batch.flatMap(({ objectKey }) => [objectKey, createdAt]));
449
- const byKey = new Map(reservations.map((reservation) => [reservation.keyHash, reservation]));
450
- return matches.flatMap((row) => {
451
- const entry = storedEntryFromRow(row);
452
- if (!entry) return [];
453
- const reservation = byKey.get(row.key_hash);
454
- return [{
455
- entry,
456
- ...reservation ? { reservation: {
457
- objectKey: reservation.objectKey,
458
- revision: reservation.revision
459
- } } : {}
460
- }];
461
- });
462
- });
463
- if (candidates.some(({ reservation }) => reservation)) await this.ensureCleanupAlarm(createdAt);
464
- return candidates;
505
+ findRefreshCandidates(options, projection) {
506
+ if (projection === void 0) return storedEntriesFromRows(this.findMatchingEntryRows(options));
507
+ return this.findMatchingEntryRows(options, "refresh").map((row) => ({
508
+ keyHash: row.key_hash,
509
+ cacheKey: row.cache_key,
510
+ activeRevision: row.active_revision,
511
+ latestRevision: row.latest_revision,
512
+ hasRevalidator: row.has_revalidator === 1
513
+ }));
465
514
  }
466
515
  async purgeMatching(options, invalidatedAt = Date.now()) {
467
516
  const reservation = this.ctx.storage.transactionSync(() => {
468
- const matches = this.findMatchingEntryRows(options, true);
517
+ const matches = this.findMatchingEntryRows(options, "purge");
518
+ const tombstoneSequence = this.tombstoneSequence(matches.some((row) => row.object_key !== null));
469
519
  const tags = normalizeTags(options.tags ?? []);
470
520
  this.ctx.storage.sql.exec(`UPDATE metadata_state SET tag_invalidation_sequence = tag_invalidation_sequence + 1
471
521
  WHERE singleton = 1`);
@@ -482,12 +532,13 @@ var CacheMetadata = class extends DurableObject {
482
532
  )`, ...batch.flatMap((tag) => [tag, invalidatedAt]));
483
533
  for (const batch of batches(matches, 99)) {
484
534
  const active = batch.filter((row) => row.object_key !== null);
485
- for (const entryBatch of batches(active, MAX_SQL_PARAMETERS / 4)) this.ctx.storage.sql.exec(`INSERT OR REPLACE INTO pending_r2_tombstones
486
- (key_hash, cache_key, object_key, revision) VALUES ${entryBatch.map(() => "(?, ?, ?, ?)").join(", ")}`, ...entryBatch.flatMap((row) => [
535
+ for (const entryBatch of batches(active, MAX_SQL_PARAMETERS / 5)) this.ctx.storage.sql.exec(`INSERT OR REPLACE INTO pending_r2_tombstones
536
+ (key_hash, cache_key, object_key, revision, tombstone_sequence) VALUES ${entryBatch.map(() => "(?, ?, ?, ?, ?)").join(", ")}`, ...entryBatch.flatMap((row) => [
487
537
  row.key_hash,
488
538
  row.cache_key,
489
539
  row.object_key,
490
- row.latest_revision + 1
540
+ row.latest_revision + 1,
541
+ tombstoneSequence
491
542
  ]));
492
543
  const keyHashes = batch.map((row) => row.key_hash);
493
544
  const placeholders = keyHashes.map(() => "?").join(", ");
@@ -505,11 +556,12 @@ var CacheMetadata = class extends DurableObject {
505
556
  tombstoned = 1
506
557
  WHERE key_hash IN (${placeholders})`, ...keyHashes);
507
558
  this.ctx.storage.sql.exec(`DELETE FROM revalidation_claims WHERE key_hash IN (${placeholders})`, ...keyHashes);
559
+ this.ctx.storage.sql.exec(`DELETE FROM entry_tags WHERE key_hash IN (${placeholders})`, ...keyHashes);
508
560
  }
509
- const pendingTombstones = this.ctx.storage.sql.exec("SELECT COUNT(*) AS count FROM pending_r2_tombstones").one().count;
510
561
  return {
511
562
  backingStoreUpdated: matches.length > 0 || tags.length > 0,
512
- pendingTombstones
563
+ pendingTombstones: matches.filter((row) => row.object_key !== null).length,
564
+ tombstoneSequence
513
565
  };
514
566
  });
515
567
  if (reservation.pendingTombstones > 0) await this.scheduleCleanupAlarm(Date.now() + ORPHAN_CLEANUP_RETRY_MS).catch((error) => this.logCleanupFailure(error));
@@ -534,10 +586,11 @@ var CacheMetadata = class extends DurableObject {
534
586
  if (currentRevision !== void 0 && currentRevision >= entry.revision) return;
535
587
  throw new Error(`R2 revision ${entry.revision} could not be tombstoned after concurrent writes`);
536
588
  }
537
- finishCompletedTombstones() {
538
- this.ctx.storage.sql.exec("DELETE FROM pending_r2_tombstones WHERE r2_complete = 1 AND edge_purge_complete = 1");
589
+ finishCompletedTombstones(entries) {
590
+ for (const batch of batches(entries, MAX_SQL_PARAMETERS / 2)) this.ctx.storage.sql.exec(`DELETE FROM pending_r2_tombstones
591
+ WHERE r2_complete = 1 AND edge_purge_complete = 1 AND (${batch.map(() => "(key_hash = ? AND revision = ?)").join(" OR ")})`, ...batch.flatMap((entry) => [entry.keyHash, entry.revision]));
539
592
  }
540
- async drainPendingTombstones(limit, keyHash, afterKeyHash) {
593
+ async drainPendingTombstones(limit, keyHash, afterKeyHash, tombstoneSequence) {
541
594
  const filters = ["r2_complete = 0"];
542
595
  const parameters = [];
543
596
  if (keyHash !== void 0) {
@@ -548,6 +601,10 @@ var CacheMetadata = class extends DurableObject {
548
601
  filters.push("key_hash > ?");
549
602
  parameters.push(afterKeyHash);
550
603
  }
604
+ if (tombstoneSequence !== void 0) {
605
+ filters.push("tombstone_sequence = ?");
606
+ parameters.push(tombstoneSequence);
607
+ }
551
608
  parameters.push(limit);
552
609
  const rows = this.ctx.storage.sql.exec(`SELECT key_hash, cache_key, object_key, revision, r2_complete, edge_purge_complete
553
610
  FROM pending_r2_tombstones
@@ -559,14 +616,14 @@ var CacheMetadata = class extends DurableObject {
559
616
  objectKey: row.object_key,
560
617
  revision: row.revision
561
618
  }));
562
- const settled = await Promise.allSettled(pending.map(async (entry) => {
619
+ const settled = await mapSettledWithR2Concurrency(pending, async (entry) => {
563
620
  await this.writeR2Tombstone(entry);
564
621
  return entry;
565
- }));
622
+ });
566
623
  const purged = settled.flatMap((result) => result.status === "fulfilled" ? [result.value] : []);
567
624
  this.ctx.storage.transactionSync(() => {
568
625
  for (const batch of batches(purged, MAX_SQL_PARAMETERS / 2)) this.ctx.storage.sql.exec(`UPDATE pending_r2_tombstones SET r2_complete = 1 WHERE ${batch.map(() => "(key_hash = ? AND revision = ?)").join(" OR ")}`, ...batch.flatMap((entry) => [entry.keyHash, entry.revision]));
569
- this.finishCompletedTombstones();
626
+ this.finishCompletedTombstones(purged);
570
627
  });
571
628
  return {
572
629
  ...rows.length ? { cursor: rows.at(-1).key_hash } : {},
@@ -575,10 +632,12 @@ var CacheMetadata = class extends DurableObject {
575
632
  purged
576
633
  };
577
634
  }
578
- listPendingEdgePurges(limit) {
635
+ listPendingEdgePurges(limit, tombstoneSequence) {
636
+ const sequenceFilter = tombstoneSequence === void 0 ? "" : " AND tombstone_sequence = ?";
579
637
  return this.ctx.storage.sql.exec(`SELECT key_hash, cache_key, object_key, revision, r2_complete, edge_purge_complete
580
638
  FROM pending_r2_tombstones
581
- WHERE r2_complete = 1 AND edge_purge_complete = 0 ORDER BY key_hash LIMIT ?`, limit).toArray().map((row) => ({
639
+ WHERE r2_complete = 1 AND edge_purge_complete = 0${sequenceFilter}
640
+ ORDER BY key_hash LIMIT ?`, ...tombstoneSequence === void 0 ? [] : [tombstoneSequence], limit).toArray().map((row) => ({
582
641
  keyHash: row.key_hash,
583
642
  cacheKey: row.cache_key,
584
643
  objectKey: row.object_key,
@@ -588,9 +647,13 @@ var CacheMetadata = class extends DurableObject {
588
647
  markTombstonesEdgePurged(entries) {
589
648
  this.ctx.storage.transactionSync(() => {
590
649
  for (const batch of batches(entries, MAX_SQL_PARAMETERS / 2)) this.ctx.storage.sql.exec(`UPDATE pending_r2_tombstones SET edge_purge_complete = 1 WHERE ${batch.map(() => "(key_hash = ? AND revision = ?)").join(" OR ")}`, ...batch.flatMap((entry) => [entry.keyHash, entry.revision]));
591
- this.finishCompletedTombstones();
650
+ this.finishCompletedTombstones(entries);
592
651
  });
593
652
  }
653
+ markTombstonesEdgePurgedThrough(tombstoneSequence) {
654
+ this.ctx.storage.sql.exec(`DELETE FROM pending_r2_tombstones
655
+ WHERE r2_complete = 1 AND edge_purge_complete = 0 AND tombstone_sequence <= ?`, tombstoneSequence);
656
+ }
594
657
  inspect() {
595
658
  return storedEntriesFromRows(this.ctx.storage.sql.exec("SELECT * FROM entries ORDER BY cache_key").toArray());
596
659
  }
@@ -0,0 +1,3 @@
1
+ //#region src/r2-concurrency.d.ts
2
+ export declare function mapSettledWithR2Concurrency<T, R>(values: readonly T[], callback: (value: T) => Promise<R>): Promise<PromiseSettledResult<R>[]>;
3
+ //#endregion
@@ -0,0 +1,25 @@
1
+ //#region src/r2-concurrency.ts
2
+ const MAX_R2_CONCURRENCY = 6;
3
+ async function mapSettledWithR2Concurrency(values, callback) {
4
+ const results = [];
5
+ let nextIndex = 0;
6
+ await Promise.all(Array.from({ length: Math.min(MAX_R2_CONCURRENCY, values.length) }, async () => {
7
+ while (nextIndex < values.length) {
8
+ const index = nextIndex++;
9
+ try {
10
+ results[index] = {
11
+ status: "fulfilled",
12
+ value: await callback(values[index])
13
+ };
14
+ } catch (reason) {
15
+ results[index] = {
16
+ status: "rejected",
17
+ reason
18
+ };
19
+ }
20
+ }
21
+ }));
22
+ return results;
23
+ }
24
+ //#endregion
25
+ export { mapSettledWithR2Concurrency };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/workers-response-store",
3
- "version": "0.1.0-beta.1",
3
+ "version": "1.0.0",
4
4
  "description": "An R2-backed programmatic response store for Cloudflare Workers.",
5
5
  "homepage": "https://vinext.dev/",
6
6
  "license": "MIT",