@cloudflare/workers-response-store 1.1.5 → 1.2.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 +28 -12
- package/dist/binding.d.ts +27 -13
- package/dist/binding.js +84 -33
- package/dist/cache-policy.d.ts +1 -3
- package/dist/cache-policy.js +2 -3
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -3
- package/dist/isolate-negative-cache.d.ts +1 -3
- package/dist/isolate-negative-cache.js +1 -2
- package/dist/metadata-do.d.ts +28 -9
- package/dist/metadata-do.js +114 -23
- package/dist/r2-concurrency.d.ts +1 -3
- package/dist/r2-concurrency.js +1 -2
- package/dist/service.d.ts +1 -3
- package/dist/service.js +2 -3
- package/package.json +2 -2
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"`. 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,
|
|
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, for a replaced entry once it is stale, or for a purged entry once its edge purge completed (unless a `purge()` or `invalidate()` edge update 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
|
|
|
@@ -197,7 +197,7 @@ type ResponseStoreMutationResult = {
|
|
|
197
197
|
};
|
|
198
198
|
```
|
|
199
199
|
|
|
200
|
-
`edgePurgeAccepted` reports whether Workers Cache accepted the edge update: a purge for `put()` and `purge()`, or an invalidation for `refresh()` and `invalidate()` (which purges entries without a revalidator). Local Miniflare does not implement `ctx.cache.invalidate()` yet, so `refresh()` and `invalidate()` purge there instead.
|
|
200
|
+
`edgePurgeAccepted` reports whether Workers Cache accepted the edge update: a purge for `put()` and `purge()`, or an invalidation for `refresh()` and `invalidate()` (which purges entries without a revalidator). Local Miniflare does not implement `ctx.cache.invalidate()` yet, so `refresh()` and `invalidate()` purge there instead. With [`purgeBatchWindow`](#batched-edge-purges), it reports that the update was queued for the window's batch, except for `refresh()` and `purgeEverything`, which still send theirs.
|
|
201
201
|
|
|
202
202
|
### Cache keys
|
|
203
203
|
|
|
@@ -231,7 +231,7 @@ The `regenerate` callback receives the stored `id` and `args`, a canonical cache
|
|
|
231
231
|
| Header | Value | Meaning |
|
|
232
232
|
| --------------------------------------------- | --------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
233
233
|
| `X-Workers-Response-Store` | `MISS`, `BLOB-FRESH`, or `BLOB-STALE` | Whether metadata was absent or the R2 response was fresh or stale when the binding read it. A miss is a `404` with `Cache-Control: no-store`. |
|
|
234
|
-
| `X-Workers-Response-Store-Revision` |
|
|
234
|
+
| `X-Workers-Response-Store-Revision` | Non-negative integer | The active stored revision returned to the caller. On a miss read from R2, the revision it missed: `0` for a key never written, or that of its tombstone or expired entry; otherwise absent on a miss. |
|
|
235
235
|
| `X-Workers-Response-Store-Binding-Invocation` | UUID | Identifies the binding invocation that loaded the response from R2. Workers Cache reuses the value from its cached fill, making it useful for diagnosing whether the binding ran. It is absent on a miss. |
|
|
236
236
|
| `X-Workers-Response-Store-Age-Basis` | `<created-at-ms>:<initial-age-seconds>` | The response creation timestamp and its original `Age`. Calculate the current age as `initialAgeSeconds + max(0, floor((Date.now() - createdAtMs) / 1000))`. |
|
|
237
237
|
|
|
@@ -271,6 +271,21 @@ const responseStore = createWorkersResponseStore<Env>({
|
|
|
271
271
|
|
|
272
272
|
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.
|
|
273
273
|
|
|
274
|
+
### Batched edge purges
|
|
275
|
+
|
|
276
|
+
Workers Cache purges and invalidations always use the Free-tier [purge rate limits](https://developers.cloudflare.com/workers/cache/purge/#rate-limits): 5 requests a minute, with a burst of 25 and up to 100 tags per request, shared by every Worker in the account. An application that invalidates often can exhaust them. A refused `put()` purge leaves the replaced response at the edge until its edge `max-age` ends; `invalidate()` and `purge()` retry a refused update about once a minute.
|
|
277
|
+
|
|
278
|
+
To spend fewer requests, collect edge updates for a window of seconds:
|
|
279
|
+
|
|
280
|
+
```ts
|
|
281
|
+
const responseStore = createWorkersResponseStore<Env>({
|
|
282
|
+
purgeBatchWindow: 60,
|
|
283
|
+
regenerate,
|
|
284
|
+
});
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
`purgeBatchWindow` must be a positive integer, and works in both deployment modes. With it, `put()`, `invalidate()`, and `purge()` update R2 and the metadata as before, but queue their edge purges and invalidations in the metadata Durable Object instead of sending them. When the window opened by the first of them ends, or an earlier metadata alarm runs, it sends everything queued in as few requests as it can: purges and invalidations are separate requests of up to 100 tags each. A refused request is retried about once a minute, so it is eventually sent; requests that went through aren't sent again. Edge responses can therefore stay fresh for up to the window (plus any retries) after the call that changed them; the Response Store itself serves the change at once to any read that reaches it. Each metadata shard sends its own batch. `refresh()` and `purge({ purgeEverything: true })` still send their edge update at once.
|
|
288
|
+
|
|
274
289
|
## Storage and operational behavior
|
|
275
290
|
|
|
276
291
|
```text
|
|
@@ -285,11 +300,12 @@ application Worker
|
|
|
285
300
|
|
|
286
301
|
- Responses use `runtime-cache/<version-id>/r2-v2/[shards-<count>/]<digest>/active`. The layout segment isolates this R2-backed implementation from older cache-Worker versions during service-binding rollbacks and rolling deployments.
|
|
287
302
|
- SQLite revisions and conditional publication prevent slow writes from replacing newer writes or resurrecting purged entries. User RPC, R2, and cache-purge I/O run outside SQLite transactions.
|
|
288
|
-
- Purging replaces the active R2 object with a higher-revision tombstone. A later put can replace that tombstone, but an older delayed write cannot recreate purged content.
|
|
303
|
+
- Purging replaces the active R2 object with a higher-revision tombstone. A later put can replace that tombstone, but an older delayed write cannot recreate purged content. Once the tombstone's edge purge completed, that put needs no edge purge of its own: the edge holds no copy of the key. A read that fetched the purged response before the purge and stored it at the edge after it can leave that copy there until it goes stale, as with any purge racing a cache fill.
|
|
289
304
|
- 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
305
|
- 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
306
|
- 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
|
|
307
|
+
- 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. Until that replacement, every stale read reaches the binding and R2 instead of Workers Cache.
|
|
308
|
+
- An edge copy's freshness is rounded down, so it never goes stale later than its entry. A `put()` replacing a stale entry therefore needs no edge purge, unless an `invalidate()` made it stale and its edge update is still queued: an edge copy without a revalidator has expired, and Workers Cache revalidates one with a revalidator on its next read, which returns the replacement. 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), Workers Cache can serve that copy until it expires, or until Workers Cache revalidates it. As when the Store regenerates an entry itself, which never purges, an edge copy with a revalidator gets Workers Cache's default [stale-if-error](https://developers.cloudflare.com/workers/cache/configuration/#serve-stale-on-error-with-stale-if-error): while revalidating it with the binding fails, the edge keeps serving the replaced copy instead of an error.
|
|
293
309
|
- 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.
|
|
294
310
|
- 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.
|
|
295
311
|
- 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
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
2
|
-
//#region src/binding.d.ts
|
|
3
2
|
type RevalidatorDescriptor = {
|
|
4
3
|
id: string;
|
|
5
4
|
args: SerializableValue[];
|
|
@@ -11,12 +10,12 @@ export type ResponseStorePutOptions = {
|
|
|
11
10
|
/** @internal Collapse overlapping framework writes for the same cache key. */
|
|
12
11
|
coalesce?: boolean;
|
|
13
12
|
/**
|
|
14
|
-
* @internal The revision a stale read returned
|
|
15
|
-
* that this write refreshes. While another
|
|
16
|
-
* fresh, and the key hasn't been invalidated or
|
|
17
|
-
* of replacing (and purging) it, and returns
|
|
13
|
+
* @internal The revision a stale read or a miss returned
|
|
14
|
+
* (`X-Workers-Response-Store-Revision`) that this write refreshes or fills. While another
|
|
15
|
+
* write of that revision is published and fresh, and the key hasn't been invalidated or
|
|
16
|
+
* purged since, the write is dropped instead of replacing (and purging) it, and returns
|
|
18
17
|
* `{ backingStoreUpdated: false, edgePurgeAccepted: false }`. A value that isn't a
|
|
19
|
-
*
|
|
18
|
+
* non-negative integer is ignored.
|
|
20
19
|
*/
|
|
21
20
|
replacesRevision?: number;
|
|
22
21
|
/**
|
|
@@ -123,6 +122,7 @@ export type WorkersResponseStoreEnv = {
|
|
|
123
122
|
export type WorkersResponseStoreProps = {
|
|
124
123
|
versionId?: string;
|
|
125
124
|
locationHint?: ResponseStoreLocationHint;
|
|
125
|
+
purgeBatchWindow?: number;
|
|
126
126
|
revalidator?: RevalidationService;
|
|
127
127
|
shards?: number;
|
|
128
128
|
};
|
|
@@ -133,6 +133,7 @@ export type ResponseStoreServiceInvocation = {
|
|
|
133
133
|
versionId: string;
|
|
134
134
|
revalidator: RevalidationService;
|
|
135
135
|
locationHint?: ResponseStoreLocationHint;
|
|
136
|
+
purgeBatchWindow?: number;
|
|
136
137
|
shards?: number;
|
|
137
138
|
};
|
|
138
139
|
export type ResponseStoreExecutionContext = Pick<ExecutionContext, "exports">;
|
|
@@ -147,9 +148,19 @@ export declare class ResponseStoreService extends WorkerEntrypoint<WorkersRespon
|
|
|
147
148
|
purge(options: ResponseStorePurgeOptions, invocation: ResponseStoreServiceInvocation): Promise<ResponseStoreMutationResult>;
|
|
148
149
|
}
|
|
149
150
|
export type ResponseStoreServiceBinding = Pick<ResponseStoreService, "read" | "getTagExpiration" | "put" | "refresh" | "invalidate" | "purge">;
|
|
151
|
+
export declare function validateResponseStorePurgeBatchWindow(purgeBatchWindow: number | undefined): number | undefined;
|
|
150
152
|
export declare function validateResponseStoreShards(shards: number | undefined): number | undefined;
|
|
151
153
|
export declare class ResponseStoreBinding extends WorkerEntrypoint<WorkersResponseStoreEnv, WorkersResponseStoreProps> {
|
|
152
154
|
private readonly shardCount;
|
|
155
|
+
private readonly purgeBatchWindowMs;
|
|
156
|
+
/** With `purgeBatchWindow`, when the metadata DO sends edge work queued now. */
|
|
157
|
+
private edgeFlushAt;
|
|
158
|
+
/**
|
|
159
|
+
* Purge the edge copy a put replaced. With `purgeBatchWindow`, queue the purge for the
|
|
160
|
+
* metadata DO to send with the rest of the window's edge work instead; a failed queue sends it
|
|
161
|
+
* now.
|
|
162
|
+
*/
|
|
163
|
+
private purgeReplacedEdgeCopy;
|
|
153
164
|
private getVersionId;
|
|
154
165
|
private getMetadataShard;
|
|
155
166
|
private getMetadata;
|
|
@@ -166,12 +177,13 @@ export declare class ResponseStoreBinding extends WorkerEntrypoint<WorkersRespon
|
|
|
166
177
|
private updateEdgeCache;
|
|
167
178
|
private updateEdgeCacheByTags;
|
|
168
179
|
/**
|
|
169
|
-
* Update drained entries' edge responses
|
|
170
|
-
* are invalidated, so Workers Cache
|
|
171
|
-
* Tombstones are purged, as are stale entries the Store
|
|
172
|
-
* so their next read reaches R2 and sees `BLOB-STALE`.
|
|
180
|
+
* Update drained entries' edge responses, and return the entries whose request was accepted, so
|
|
181
|
+
* a retry sends only the rest. Stale entries with a revalidator are invalidated, so Workers Cache
|
|
182
|
+
* keeps serving them while it refetches. Tombstones are purged, as are stale entries the Store
|
|
183
|
+
* cannot regenerate, so their next read reaches R2 and sees `BLOB-STALE`.
|
|
173
184
|
*/
|
|
174
|
-
purgeR2TombstoneEdges(entries: PurgedEntry[]): Promise<
|
|
185
|
+
purgeR2TombstoneEdges(entries: PurgedEntry[]): Promise<PurgedEntry[]>;
|
|
186
|
+
private updateEntryEdges;
|
|
175
187
|
private purgePendingEdgeEntries;
|
|
176
188
|
private objectKeyRoot;
|
|
177
189
|
private objectKeyPrefix;
|
|
@@ -203,9 +215,11 @@ export declare class ResponseStoreBinding extends WorkerEntrypoint<WorkersRespon
|
|
|
203
215
|
invalidate(options: ResponseStoreInvalidateOptions): Promise<ResponseStoreMutationResult>;
|
|
204
216
|
/** Write each shard's queued R2 tombstones and stale rewrites in bounded batches. */
|
|
205
217
|
private drainReservations;
|
|
206
|
-
/**
|
|
218
|
+
/**
|
|
219
|
+
* Update the edge responses of drained entries, collecting failures. With `purgeBatchWindow`, a
|
|
220
|
+
* metadata DO that scheduled its alarm sends its own with the window's batch instead.
|
|
221
|
+
*/
|
|
207
222
|
private updatePendingEdges;
|
|
208
223
|
purge(options: ResponseStorePurgeOptions): Promise<ResponseStoreMutationResult>;
|
|
209
224
|
}
|
|
210
|
-
//#endregion
|
|
211
225
|
export {};
|
package/dist/binding.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { deriveCachePolicy, edgeCacheControl, representationAge } from "./cache-policy.js";
|
|
2
2
|
import { IsolateNegativeCache } from "./isolate-negative-cache.js";
|
|
3
3
|
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
4
|
-
|
|
4
|
+
// src/binding.ts
|
|
5
5
|
var R2PublicationError = class extends Error {
|
|
6
6
|
constructor(cause) {
|
|
7
7
|
super("R2 response publication failed", { cause });
|
|
@@ -60,6 +60,15 @@ const MISS_HEADERS = {
|
|
|
60
60
|
"Content-Type": "text/plain; charset=utf-8",
|
|
61
61
|
"X-Workers-Response-Store": "MISS"
|
|
62
62
|
};
|
|
63
|
+
function missResponse(revision) {
|
|
64
|
+
return new Response("Workers Response Store miss", {
|
|
65
|
+
status: 404,
|
|
66
|
+
headers: revision === void 0 ? MISS_HEADERS : {
|
|
67
|
+
...MISS_HEADERS,
|
|
68
|
+
"X-Workers-Response-Store-Revision": String(revision)
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
63
72
|
const BACKGROUND_REVALIDATION_LEASE_MS = 3e4;
|
|
64
73
|
const CACHE_PURGE_BATCH_SIZE = 100;
|
|
65
74
|
const PURGE_TOMBSTONE_BATCH_SIZE = 400;
|
|
@@ -93,6 +102,10 @@ const STORAGE_LAYOUT_VERSION = "r2-v2";
|
|
|
93
102
|
const pendingPuts = /* @__PURE__ */ new Map();
|
|
94
103
|
const pendingR2Reads = /* @__PURE__ */ new Map();
|
|
95
104
|
const entryReads = new IsolateNegativeCache(ISOLATE_MISS_CACHE_CAPACITY, ISOLATE_MISS_CACHE_TTL_MS);
|
|
105
|
+
function validateResponseStorePurgeBatchWindow(purgeBatchWindow) {
|
|
106
|
+
if (purgeBatchWindow !== void 0 && (!Number.isSafeInteger(purgeBatchWindow) || purgeBatchWindow <= 0)) throw new TypeError("Workers Response Store purgeBatchWindow must be a positive integer");
|
|
107
|
+
return purgeBatchWindow;
|
|
108
|
+
}
|
|
96
109
|
function validateResponseStoreShards(shards) {
|
|
97
110
|
if (shards !== void 0 && (!Number.isSafeInteger(shards) || shards <= 1)) throw new TypeError("Workers Response Store shards must be an integer greater than 1");
|
|
98
111
|
return shards;
|
|
@@ -208,6 +221,42 @@ function cacheTagsFromResponse(response) {
|
|
|
208
221
|
}
|
|
209
222
|
var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
210
223
|
shardCount = validateResponseStoreShards(this.ctx.props?.shards) ?? 1;
|
|
224
|
+
purgeBatchWindowMs = (() => {
|
|
225
|
+
const seconds = validateResponseStorePurgeBatchWindow(this.ctx.props?.purgeBatchWindow);
|
|
226
|
+
return seconds === void 0 ? void 0 : seconds * 1e3;
|
|
227
|
+
})();
|
|
228
|
+
/** With `purgeBatchWindow`, when the metadata DO sends edge work queued now. */
|
|
229
|
+
edgeFlushAt() {
|
|
230
|
+
return this.purgeBatchWindowMs === void 0 ? void 0 : Date.now() + this.purgeBatchWindowMs;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Purge the edge copy a put replaced. With `purgeBatchWindow`, queue the purge for the
|
|
234
|
+
* metadata DO to send with the rest of the window's edge work instead; a failed queue sends it
|
|
235
|
+
* now.
|
|
236
|
+
*/
|
|
237
|
+
async purgeReplacedEdgeCopy(metadata, entry) {
|
|
238
|
+
const edgeFlushAt = this.edgeFlushAt();
|
|
239
|
+
if (edgeFlushAt === void 0) return this.updateEdgeCacheByTags([purgeTagForEntry(entry)]);
|
|
240
|
+
let scheduled;
|
|
241
|
+
try {
|
|
242
|
+
scheduled = await metadata.queueEdgePurge(entry.keyHash, entry.activeRevision, edgeFlushAt);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.error(JSON.stringify({
|
|
245
|
+
message: "Workers Response Store edge purge could not be queued",
|
|
246
|
+
error: error instanceof Error ? error.message : String(error)
|
|
247
|
+
}));
|
|
248
|
+
return this.updateEdgeCacheByTags([purgeTagForEntry(entry)]);
|
|
249
|
+
}
|
|
250
|
+
if (scheduled) return true;
|
|
251
|
+
if (!await this.updateEdgeCacheByTags([purgeTagForEntry(entry)])) return false;
|
|
252
|
+
await metadata.markTombstonesEdgePurged([{
|
|
253
|
+
keyHash: entry.keyHash,
|
|
254
|
+
cacheKey: entry.cacheKey,
|
|
255
|
+
objectKey: "",
|
|
256
|
+
revision: entry.activeRevision
|
|
257
|
+
}]).catch(() => {});
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
211
260
|
getVersionId() {
|
|
212
261
|
const versionId = this.ctx.props?.versionId ?? this.env.CF_VERSION_METADATA?.id;
|
|
213
262
|
if (!versionId) throw new Error("Workers Response Store requires a version_metadata binding");
|
|
@@ -287,22 +336,26 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
287
336
|
return accepted;
|
|
288
337
|
}
|
|
289
338
|
/**
|
|
290
|
-
* Update drained entries' edge responses
|
|
291
|
-
* are invalidated, so Workers Cache
|
|
292
|
-
* Tombstones are purged, as are stale entries the Store
|
|
293
|
-
* so their next read reaches R2 and sees `BLOB-STALE`.
|
|
339
|
+
* Update drained entries' edge responses, and return the entries whose request was accepted, so
|
|
340
|
+
* a retry sends only the rest. Stale entries with a revalidator are invalidated, so Workers Cache
|
|
341
|
+
* keeps serving them while it refetches. Tombstones are purged, as are stale entries the Store
|
|
342
|
+
* cannot regenerate, so their next read reaches R2 and sees `BLOB-STALE`.
|
|
294
343
|
*/
|
|
295
344
|
async purgeR2TombstoneEdges(entries) {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
345
|
+
return (await Promise.all([this.updateEntryEdges(entries.filter((entry) => !entry.edgeInvalidate)), this.updateEntryEdges(entries.filter((entry) => entry.edgeInvalidate), "invalidate")])).flat();
|
|
346
|
+
}
|
|
347
|
+
async updateEntryEdges(entries, operation = "purge") {
|
|
348
|
+
const accepted = [];
|
|
349
|
+
for (const batch of batches(entries, CACHE_PURGE_BATCH_SIZE)) if (await this.updateEdgeCache({ tags: batch.map(purgeTagForEntry) }, operation)) accepted.push(...batch);
|
|
350
|
+
return accepted;
|
|
299
351
|
}
|
|
300
352
|
async purgePendingEdgeEntries(metadata, tombstoneSequence) {
|
|
301
353
|
for (;;) {
|
|
302
354
|
const entries = await metadata.listPendingEdgePurges(PURGE_TOMBSTONE_BATCH_SIZE, tombstoneSequence);
|
|
303
355
|
if (!entries.length) return true;
|
|
304
|
-
|
|
305
|
-
await metadata.markTombstonesEdgePurged(
|
|
356
|
+
const accepted = await this.purgeR2TombstoneEdges(entries);
|
|
357
|
+
if (accepted.length) await metadata.markTombstonesEdgePurged(accepted);
|
|
358
|
+
if (accepted.length < entries.length) return false;
|
|
306
359
|
}
|
|
307
360
|
}
|
|
308
361
|
objectKeyRoot() {
|
|
@@ -350,8 +403,10 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
350
403
|
const object = await this.env.CACHE_BODIES.get(this.r2ObjectKey(cacheKey.keyHash));
|
|
351
404
|
const entry = this.entryFromR2Metadata(cacheKey.keyHash, cacheKey.cacheKey, object);
|
|
352
405
|
if (!entry && object) await object.body.cancel();
|
|
406
|
+
const missRevision = !object ? 0 : !entry && object.customMetadata?.tombstoned === "1" ? metadataInteger(object.customMetadata.latestRevision) : void 0;
|
|
353
407
|
return {
|
|
354
408
|
entry,
|
|
409
|
+
...!entry && missRevision !== void 0 ? { missRevision } : {},
|
|
355
410
|
...entry && object ? { object } : {}
|
|
356
411
|
};
|
|
357
412
|
}
|
|
@@ -556,13 +611,14 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
556
611
|
if (publication.entry) try {
|
|
557
612
|
stored = await this.writeR2Response(publication.entry, body, response.status, policy.createdAt, policy.initialAge, expectedR2Etag !== void 0 ? expectedR2Etag : write.r2ObjectAbsent ? null : void 0);
|
|
558
613
|
} catch (error) {
|
|
559
|
-
const
|
|
614
|
+
const edgeFlushAt = this.edgeFlushAt();
|
|
615
|
+
const reconciliation = await metadata.invalidatePublishedRevision(publication.entry.keyHash, publication.entry.activeRevision, edgeFlushAt).catch((reconciliationError) => ({
|
|
560
616
|
failures: [reconciliationError instanceof Error ? reconciliationError.message : String(reconciliationError)],
|
|
561
617
|
pending: [],
|
|
562
618
|
purged: []
|
|
563
619
|
}));
|
|
564
620
|
const reconciliationFailures = reconciliation.failures.map((failure) => new Error(failure));
|
|
565
|
-
if (reconciliation.purged.length) try {
|
|
621
|
+
if (reconciliation.purged.length && !reconciliation.edgeFlushScheduled) try {
|
|
566
622
|
if (await this.updateEdgeCacheByTags(reconciliation.purged.map(purgeTagForEntry))) await metadata.markTombstonesEdgePurged(reconciliation.purged);
|
|
567
623
|
} catch (reconciliationError) {
|
|
568
624
|
reconciliationFailures.push(reconciliationError instanceof Error ? reconciliationError : new Error(String(reconciliationError)));
|
|
@@ -640,10 +696,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
640
696
|
const r2Read = await this.readR2MetadataCached(cacheKey);
|
|
641
697
|
const entry = r2Read.entry;
|
|
642
698
|
const expectedR2Etag = r2Read.object?.etag;
|
|
643
|
-
if (!entry) return
|
|
644
|
-
status: 404,
|
|
645
|
-
headers: MISS_HEADERS
|
|
646
|
-
});
|
|
699
|
+
if (!entry) return missResponse(r2Read.missRevision);
|
|
647
700
|
const now = Date.now();
|
|
648
701
|
const revalidateStale = hasRevalidator(entry) && now >= entry.freshUntil && (request.headers.has("If-None-Match") || request.headers.has("If-Modified-Since"));
|
|
649
702
|
if (now < entry.swrUntil && !revalidateStale) {
|
|
@@ -658,10 +711,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
658
711
|
}
|
|
659
712
|
}
|
|
660
713
|
await r2Read?.object?.body.cancel().catch(() => {});
|
|
661
|
-
if (now >= entry.swrUntil && entry.expiryBehavior === "miss") return
|
|
662
|
-
status: 404,
|
|
663
|
-
headers: MISS_HEADERS
|
|
664
|
-
});
|
|
714
|
+
if (now >= entry.swrUntil && entry.expiryBehavior === "miss") return missResponse(entry.activeRevision);
|
|
665
715
|
const metadata = this.getMetadata(keyHash);
|
|
666
716
|
let regenerated;
|
|
667
717
|
if (revalidateStale && now < entry.swrUntil) {
|
|
@@ -670,10 +720,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
670
720
|
regenerated = result;
|
|
671
721
|
} else {
|
|
672
722
|
const regeneration = await metadata.reserveRegeneration(keyHash, cacheKey.cacheKey, this.objectKeyPrefix(keyHash), now);
|
|
673
|
-
if (!regeneration) return
|
|
674
|
-
status: 404,
|
|
675
|
-
headers: MISS_HEADERS
|
|
676
|
-
});
|
|
723
|
+
if (!regeneration) return missResponse();
|
|
677
724
|
regenerated = await this.regenerateEntry(metadata, regeneration.entry, now >= entry.swrUntil ? "expired" : "missing", regeneration.reservation ? {
|
|
678
725
|
...cacheKey,
|
|
679
726
|
fenceTags: regeneration.entry.cacheTags,
|
|
@@ -701,7 +748,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
701
748
|
const pendingPutKey = `${this.getVersionId()}:${this.shardCount}:${keyHash}:${Boolean(options.purgeExisting)}`;
|
|
702
749
|
const tagExpiration = options.replacesTagExpiration;
|
|
703
750
|
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
|
|
751
|
+
const replacesRevision = Number.isSafeInteger(options.replacesRevision) && options.replacesRevision >= 0 && validTagExpiration ? options.replacesRevision : void 0;
|
|
705
752
|
const replaces = replacesRevision === void 0 ? void 0 : {
|
|
706
753
|
revision: replacesRevision,
|
|
707
754
|
...tagExpiration ? {
|
|
@@ -744,7 +791,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
744
791
|
response.body?.cancel().catch(() => {});
|
|
745
792
|
return {
|
|
746
793
|
backingStoreUpdated: true,
|
|
747
|
-
edgePurgeAccepted: options.purgeExisting && result.edgePurgeRequired ? await this.
|
|
794
|
+
edgePurgeAccepted: options.purgeExisting && result.edgePurgeRequired ? await this.purgeReplacedEdgeCopy(metadata, result.entry) : true
|
|
748
795
|
};
|
|
749
796
|
}
|
|
750
797
|
if (pendingPuts.get(pendingPutKey) === pending) pendingPuts.delete(pendingPutKey);
|
|
@@ -768,7 +815,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
768
815
|
};
|
|
769
816
|
return {
|
|
770
817
|
backingStoreUpdated: true,
|
|
771
|
-
edgePurgeAccepted: options.purgeExisting && result.edgePurgeRequired ? await this.
|
|
818
|
+
edgePurgeAccepted: options.purgeExisting && result.edgePurgeRequired ? await this.purgeReplacedEdgeCopy(metadata, result.entry) : true
|
|
772
819
|
};
|
|
773
820
|
} finally {
|
|
774
821
|
if (options.coalesce && pendingPuts.get(pendingPutKey) === write) pendingPuts.delete(pendingPutKey);
|
|
@@ -829,7 +876,8 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
829
876
|
tags: options.tags,
|
|
830
877
|
pathPrefixes: options.pathPrefixes
|
|
831
878
|
};
|
|
832
|
-
const
|
|
879
|
+
const edgeFlushAt = this.edgeFlushAt();
|
|
880
|
+
const { failures, reservations } = await this.drainReservations(await Promise.allSettled(this.getMetadataShards().map((metadata) => metadata.invalidateMatching(selectors, invalidatedAt, expiresAt, edgeFlushAt))));
|
|
833
881
|
const edgePurgeAccepted = await this.updatePendingEdges(reservations, failures);
|
|
834
882
|
if (failures.length) throw new AggregateError(failures, "One or more metadata shards failed to invalidate");
|
|
835
883
|
return {
|
|
@@ -860,10 +908,13 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
860
908
|
reservations
|
|
861
909
|
};
|
|
862
910
|
}
|
|
863
|
-
/**
|
|
911
|
+
/**
|
|
912
|
+
* Update the edge responses of drained entries, collecting failures. With `purgeBatchWindow`, a
|
|
913
|
+
* metadata DO that scheduled its alarm sends its own with the window's batch instead.
|
|
914
|
+
*/
|
|
864
915
|
async updatePendingEdges(reservations, failures) {
|
|
865
916
|
let edgePurgeAccepted = true;
|
|
866
|
-
const acknowledged = await Promise.allSettled(reservations.filter(({ reservation }) => reservation.pendingTombstones > 0).map(({ metadata, reservation }) => this.purgePendingEdgeEntries(metadata, reservation.tombstoneSequence)));
|
|
917
|
+
const acknowledged = await Promise.allSettled(reservations.filter(({ reservation }) => reservation.pendingTombstones > 0 && !reservation.edgeFlushScheduled).map(({ metadata, reservation }) => this.purgePendingEdgeEntries(metadata, reservation.tombstoneSequence)));
|
|
867
918
|
for (const result of acknowledged) if (result.status === "rejected") failures.push(result.reason);
|
|
868
919
|
else if (!result.value) edgePurgeAccepted = false;
|
|
869
920
|
return edgePurgeAccepted;
|
|
@@ -871,7 +922,8 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
871
922
|
async purge(options) {
|
|
872
923
|
if (!options.purgeEverything && !options.tags?.length && !options.pathPrefixes?.length) throw new TypeError("purge() requires tags, pathPrefixes, or purgeEverything");
|
|
873
924
|
const invalidatedAt = Date.now();
|
|
874
|
-
const
|
|
925
|
+
const edgeFlushAt = options.purgeEverything ? void 0 : this.edgeFlushAt();
|
|
926
|
+
const { failures, reservations } = await this.drainReservations(await Promise.allSettled(this.getMetadataShards().map((metadata) => metadata.purgeMatching(options, invalidatedAt, edgeFlushAt))));
|
|
875
927
|
let edgePurgeAccepted = true;
|
|
876
928
|
if (options.purgeEverything && failures.length === 0) try {
|
|
877
929
|
edgePurgeAccepted = await this.updateEdgeCache({ purgeEverything: true });
|
|
@@ -891,5 +943,4 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
891
943
|
};
|
|
892
944
|
}
|
|
893
945
|
};
|
|
894
|
-
|
|
895
|
-
export { ResponseStoreBinding, ResponseStoreService, getWorkersResponseStore, validateResponseStoreLocationHint, validateResponseStoreShards };
|
|
946
|
+
export { ResponseStoreBinding, ResponseStoreService, getWorkersResponseStore, validateResponseStoreLocationHint, validateResponseStorePurgeBatchWindow, validateResponseStoreShards };
|
package/dist/cache-policy.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
//#region src/cache-policy.d.ts
|
|
2
1
|
export type CachePolicy = {
|
|
3
2
|
createdAt: number;
|
|
4
3
|
initialAge: number;
|
|
@@ -11,5 +10,4 @@ export type CachePolicy = {
|
|
|
11
10
|
*/
|
|
12
11
|
export declare function deriveCachePolicy(headers: Headers, now?: number, generatedAt?: number): CachePolicy;
|
|
13
12
|
export declare function representationAge(createdAt: number, initialAge: number, now?: number): number;
|
|
14
|
-
export declare function edgeCacheControl(freshUntil: number, swrUntil: number, now?: number, hasRevalidator?: boolean): string;
|
|
15
|
-
//#endregion
|
|
13
|
+
export declare function edgeCacheControl(freshUntil: number, swrUntil: number, now?: number, hasRevalidator?: boolean): string;
|
package/dist/cache-policy.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/cache-policy.ts
|
|
2
2
|
function parseSeconds(value) {
|
|
3
3
|
if (value === void 0) return;
|
|
4
4
|
const match = /^(?:"(\d+)"|(\d+))$/.exec(value);
|
|
@@ -38,8 +38,7 @@ function representationAge(createdAt, initialAge, now = Date.now()) {
|
|
|
38
38
|
}
|
|
39
39
|
function edgeCacheControl(freshUntil, swrUntil, now = Date.now(), hasRevalidator = true) {
|
|
40
40
|
if (!hasRevalidator) return `max-age=${Math.max(0, Math.floor((freshUntil - now) / 1e3))}, stale-while-revalidate=0, stale-if-error=0`;
|
|
41
|
-
if (now < freshUntil) return `max-age=${Math.max(0, Math.
|
|
41
|
+
if (now < freshUntil) return `max-age=${Math.max(0, Math.floor((freshUntil - now) / 1e3))}, stale-while-revalidate=${Math.max(0, Math.ceil((swrUntil - freshUntil) / 1e3))}`;
|
|
42
42
|
return `max-age=0, stale-while-revalidate=${Math.max(0, Math.ceil((swrUntil - now) / 1e3))}`;
|
|
43
43
|
}
|
|
44
|
-
//#endregion
|
|
45
44
|
export { deriveCachePolicy, edgeCacheControl, representationAge };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ExpiryBehavior, ResponseStoreBinding, ResponseStoreInvalidateOptions, 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
|
-
//#region src/index.d.ts
|
|
5
4
|
export type ResponseStoreRevalidationContext<Env> = {
|
|
6
5
|
env: Env;
|
|
7
6
|
ctx: ExecutionContext;
|
|
@@ -28,6 +27,12 @@ export type WorkersResponseStoreOptions<Env> = {
|
|
|
28
27
|
* cache-cold deployment change.
|
|
29
28
|
*/
|
|
30
29
|
locationHint?: ResponseStoreLocationHint;
|
|
30
|
+
/**
|
|
31
|
+
* Seconds to collect edge purges and invalidations for, so the metadata Durable Object sends
|
|
32
|
+
* each window's together. Edge copies update up to this much later. Without it, each mutation
|
|
33
|
+
* sends its own.
|
|
34
|
+
*/
|
|
35
|
+
purgeBatchWindow?: number;
|
|
31
36
|
/** Split version-scoped metadata across this many Durable Objects. */
|
|
32
37
|
shards?: number;
|
|
33
38
|
regenerate(input: RevalidationInput, context: ResponseStoreRevalidationContext<Env>): Response | Promise<Response>;
|
|
@@ -38,5 +43,4 @@ export type WorkersResponseStoreClientEnv = {
|
|
|
38
43
|
CF_VERSION_METADATA: WorkerVersionMetadata;
|
|
39
44
|
};
|
|
40
45
|
export declare function createWorkersResponseStoreClient<Env extends WorkersResponseStoreClientEnv = WorkersResponseStoreClientEnv>(options: WorkersResponseStoreOptions<Env>): WorkersResponseStoreClientDefinition<Env>;
|
|
41
|
-
//#endregion
|
|
42
46
|
export type { ExpiryBehavior, ResponseStoreInvalidateOptions, ResponseStoreLocationHint, ResponseStoreMutationResult, ResponseStorePurgeOptions, ResponseStorePutOptions, ResponseStoreRefreshOptions, RevalidationInput, RevalidationReason, SerializableValue, WorkersResponseStore, WorkersResponseStoreEnv };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ResponseStoreBinding, getWorkersResponseStore, validateResponseStoreLocationHint, validateResponseStoreShards } from "./binding.js";
|
|
1
|
+
import { ResponseStoreBinding, getWorkersResponseStore, validateResponseStoreLocationHint, validateResponseStorePurgeBatchWindow, validateResponseStoreShards } from "./binding.js";
|
|
2
2
|
import { CacheMetadata } from "./metadata-do.js";
|
|
3
3
|
import { WorkerEntrypoint, exports } from "cloudflare:workers";
|
|
4
|
-
|
|
4
|
+
// src/index.ts
|
|
5
5
|
function createRevalidatorEntrypoint(options) {
|
|
6
6
|
return class ResponseStoreRevalidator extends WorkerEntrypoint {
|
|
7
7
|
async regenerate(input) {
|
|
@@ -24,10 +24,12 @@ function createStoreFacade(getStore) {
|
|
|
24
24
|
}
|
|
25
25
|
function createWorkersResponseStore(options) {
|
|
26
26
|
const locationHint = validateResponseStoreLocationHint(options.locationHint);
|
|
27
|
+
const purgeBatchWindow = validateResponseStorePurgeBatchWindow(options.purgeBatchWindow);
|
|
27
28
|
const shards = validateResponseStoreShards(options.shards);
|
|
28
29
|
const ResponseStoreRevalidator = createRevalidatorEntrypoint(options);
|
|
29
30
|
const getStore = () => getWorkersResponseStore({ exports }, {
|
|
30
31
|
locationHint,
|
|
32
|
+
purgeBatchWindow,
|
|
31
33
|
shards
|
|
32
34
|
});
|
|
33
35
|
return {
|
|
@@ -41,6 +43,7 @@ function createWorkersResponseStore(options) {
|
|
|
41
43
|
}
|
|
42
44
|
function createWorkersResponseStoreClient(options) {
|
|
43
45
|
const locationHint = validateResponseStoreLocationHint(options.locationHint);
|
|
46
|
+
const purgeBatchWindow = validateResponseStorePurgeBatchWindow(options.purgeBatchWindow);
|
|
44
47
|
const shards = validateResponseStoreShards(options.shards);
|
|
45
48
|
const ResponseStoreRevalidator = createRevalidatorEntrypoint(options);
|
|
46
49
|
class ResponseStoreClient extends WorkerEntrypoint {
|
|
@@ -56,6 +59,7 @@ function createWorkersResponseStoreClient(options) {
|
|
|
56
59
|
versionId,
|
|
57
60
|
revalidator: factory({ props: {} }),
|
|
58
61
|
...locationHint === void 0 ? {} : { locationHint },
|
|
62
|
+
...purgeBatchWindow === void 0 ? {} : { purgeBatchWindow },
|
|
59
63
|
...shards === void 0 ? {} : { shards }
|
|
60
64
|
};
|
|
61
65
|
}
|
|
@@ -91,5 +95,4 @@ function createWorkersResponseStoreClient(options) {
|
|
|
91
95
|
...createStoreFacade(getClient)
|
|
92
96
|
};
|
|
93
97
|
}
|
|
94
|
-
//#endregion
|
|
95
98
|
export { createWorkersResponseStore, createWorkersResponseStoreClient };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
//#region src/isolate-negative-cache.d.ts
|
|
2
1
|
export declare class IsolateNegativeCache<K> {
|
|
3
2
|
private readonly capacity;
|
|
4
3
|
private readonly ttlMs;
|
|
@@ -7,5 +6,4 @@ export declare class IsolateNegativeCache<K> {
|
|
|
7
6
|
has(key: K): boolean;
|
|
8
7
|
add(key: K): void;
|
|
9
8
|
delete(key: K): void;
|
|
10
|
-
}
|
|
11
|
-
//#endregion
|
|
9
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/isolate-negative-cache.ts
|
|
2
2
|
var IsolateNegativeCache = class {
|
|
3
3
|
capacity;
|
|
4
4
|
ttlMs;
|
|
@@ -30,5 +30,4 @@ var IsolateNegativeCache = class {
|
|
|
30
30
|
this.misses.delete(key);
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
|
-
//#endregion
|
|
34
33
|
export { IsolateNegativeCache };
|
package/dist/metadata-do.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CandidateMetadata, PurgedEntry, RefreshCandidate, ResponseStoreInvalidateOptions, ResponseStorePurgeOptions, ResponseStoreRefreshOptions, StoredEntry } from "./binding.js";
|
|
2
2
|
import { DurableObject } from "cloudflare:workers";
|
|
3
|
-
//#region src/metadata-do.d.ts
|
|
4
3
|
type RevalidationClaim = {
|
|
5
4
|
claimId: string;
|
|
6
5
|
entry: StoredEntry;
|
|
@@ -28,6 +27,8 @@ type RegenerationReservation = {
|
|
|
28
27
|
};
|
|
29
28
|
export type PurgeReservation = {
|
|
30
29
|
backingStoreUpdated: boolean;
|
|
30
|
+
/** The alarm sends the queued edge work at the requested flush time, not the caller. */
|
|
31
|
+
edgeFlushScheduled?: true;
|
|
31
32
|
pendingTombstones: number;
|
|
32
33
|
tombstoneSequence: number;
|
|
33
34
|
};
|
|
@@ -37,8 +38,10 @@ export type RefreshedRevision = {
|
|
|
37
38
|
tags?: string[];
|
|
38
39
|
tagExpiration?: number;
|
|
39
40
|
};
|
|
40
|
-
type TombstoneDrainResult = {
|
|
41
|
+
export type TombstoneDrainResult = {
|
|
41
42
|
cursor?: string;
|
|
43
|
+
/** The alarm sends the purged entries' edge purges at the requested flush time. */
|
|
44
|
+
edgeFlushScheduled?: true;
|
|
42
45
|
failures: string[];
|
|
43
46
|
pending: PurgedEntry[];
|
|
44
47
|
purged: PurgedEntry[];
|
|
@@ -55,13 +58,14 @@ export type CacheMetadataStub = DurableObjectStub & {
|
|
|
55
58
|
listExpiredPendingObjects(cutoff: number, limit: number): Promise<string[]>;
|
|
56
59
|
sweepExpiredPendingObjects(cutoff?: number): Promise<number>;
|
|
57
60
|
publish(keyHash: string, revision: number, metadata: CandidateMetadata, claimId?: string, reservationObjectKey?: string, replaces?: RefreshedRevision): Promise<PublicationResult>;
|
|
58
|
-
invalidatePublishedRevision(keyHash: string, revision: number): Promise<TombstoneDrainResult>;
|
|
61
|
+
invalidatePublishedRevision(keyHash: string, revision: number, edgeFlushAt?: number): Promise<TombstoneDrainResult>;
|
|
62
|
+
queueEdgePurge(keyHash: string, revision: number, edgeFlushAt: number): Promise<boolean>;
|
|
59
63
|
getEntry(keyHash: string): Promise<StoredEntry | null>;
|
|
60
64
|
getTagExpiration(tags: string[]): Promise<number>;
|
|
61
65
|
findRefreshCandidates(options: ResponseStoreRefreshOptions): Promise<StoredEntry[]>;
|
|
62
66
|
findRefreshCandidates(options: ResponseStoreRefreshOptions, projection: "reservation"): Promise<RefreshCandidate[]>;
|
|
63
|
-
purgeMatching(options: ResponseStorePurgeOptions, invalidatedAt?: number): Promise<PurgeReservation>;
|
|
64
|
-
invalidateMatching(options: Pick<ResponseStoreInvalidateOptions, "tags" | "pathPrefixes">, invalidatedAt: number, expiresAt?: number): Promise<PurgeReservation>;
|
|
67
|
+
purgeMatching(options: ResponseStorePurgeOptions, invalidatedAt?: number, edgeFlushAt?: number): Promise<PurgeReservation>;
|
|
68
|
+
invalidateMatching(options: Pick<ResponseStoreInvalidateOptions, "tags" | "pathPrefixes">, invalidatedAt: number, expiresAt?: number, edgeFlushAt?: number): Promise<PurgeReservation>;
|
|
65
69
|
drainPendingTombstones(limit: number, keyHash?: string, afterKeyHash?: string, tombstoneSequence?: number): Promise<TombstoneDrainResult>;
|
|
66
70
|
retryPendingTombstones(): Promise<boolean>;
|
|
67
71
|
listPendingEdgePurges(limit: number, tombstoneSequence?: number): Promise<PurgedEntry[]>;
|
|
@@ -77,6 +81,11 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
|
|
|
77
81
|
constructor(ctx: DurableObjectState, env: CacheMetadataEnv);
|
|
78
82
|
private ensureCleanupAlarm;
|
|
79
83
|
private scheduleCleanupAlarm;
|
|
84
|
+
/**
|
|
85
|
+
* Schedule the alarm that retries queued R2 and edge work, or with `edgeFlushAt` sends the edge
|
|
86
|
+
* work then. Returns whether the alarm sends it, so the caller sends it otherwise.
|
|
87
|
+
*/
|
|
88
|
+
private scheduleEdgeFlush;
|
|
80
89
|
private logCleanupFailure;
|
|
81
90
|
private tombstoneSequence;
|
|
82
91
|
private getR2TombstoneEdgePurger;
|
|
@@ -90,13 +99,24 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
|
|
|
90
99
|
sweepExpiredPendingObjects(cutoff?: number): Promise<number>;
|
|
91
100
|
alarm(): Promise<void>;
|
|
92
101
|
retryPendingTombstones(): Promise<boolean>;
|
|
102
|
+
/** Whether R2 or edge work is left once this run's batch is flushed, and why. */
|
|
103
|
+
private flushPendingTombstones;
|
|
93
104
|
claimRevalidation(keyHash: string, activeRevision: number, cacheKey: string, objectKeyPrefix: string, now: number, leaseMs: number): Promise<RevalidationClaim | null>;
|
|
94
105
|
private reserveRevision;
|
|
95
106
|
reserveWrite(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number): Promise<WriteReservation>;
|
|
96
107
|
replaceFailedWrite(keyHash: string, cacheKey: string, objectKeyPrefix: string, failedObjectKey: string, fenceTags: string[], createdAt: number): Promise<WriteReservation | null>;
|
|
97
108
|
reserveRegeneration(keyHash: string, cacheKey: string, objectKeyPrefix: string, createdAt: number, expectedActiveRevision?: number, expectedLatestRevision?: number): Promise<RegenerationReservation | null>;
|
|
98
109
|
publish(keyHash: string, revision: number, metadata: CandidateMetadata, claimId?: string, reservationObjectKey?: string, replaces?: RefreshedRevision): Promise<PublicationResult>;
|
|
99
|
-
invalidatePublishedRevision(keyHash: string, revision: number): Promise<TombstoneDrainResult>;
|
|
110
|
+
invalidatePublishedRevision(keyHash: string, revision: number, edgeFlushAt?: number): Promise<TombstoneDrainResult>;
|
|
111
|
+
/**
|
|
112
|
+
* Queue a purge of the key's edge copy, which a put replacing the entry needs, for the alarm
|
|
113
|
+
* to send at `edgeFlushAt` with the rest of the queued edge work. Called once the
|
|
114
|
+
* replacement is in R2, so the edge can only refill with it. Work already queued for the
|
|
115
|
+
* key purges instead of invalidating, and gets this revision, so a flush that listed it
|
|
116
|
+
* before can't mark this purge sent. Returns whether the alarm will send it; if not, the
|
|
117
|
+
* caller sends it and marks it sent.
|
|
118
|
+
*/
|
|
119
|
+
queueEdgePurge(keyHash: string, revision: number, edgeFlushAt: number): Promise<boolean>;
|
|
100
120
|
getEntry(keyHash: string): StoredEntry | null;
|
|
101
121
|
private getTagInvalidationMaximum;
|
|
102
122
|
/** The earliest `expire` deadline still ahead of `now` among `tags`, or Infinity. */
|
|
@@ -115,14 +135,14 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
|
|
|
115
135
|
* leave it unchanged.
|
|
116
136
|
*/
|
|
117
137
|
private recordInvalidations;
|
|
118
|
-
purgeMatching(options: ResponseStorePurgeOptions, invalidatedAt?: number): Promise<PurgeReservation>;
|
|
138
|
+
purgeMatching(options: ResponseStorePurgeOptions, invalidatedAt?: number, edgeFlushAt?: number): Promise<PurgeReservation>;
|
|
119
139
|
/**
|
|
120
140
|
* Mark matching entries stale without deleting them. Each entry moves to a
|
|
121
141
|
* new revision with its freshness ended and its SWR window capped at
|
|
122
142
|
* `expiresAt`, and its R2 object is queued for the same rewrite, so writes
|
|
123
143
|
* reserved before the invalidation cannot publish over it.
|
|
124
144
|
*/
|
|
125
|
-
invalidateMatching(options: Pick<ResponseStoreInvalidateOptions, "tags" | "pathPrefixes">, invalidatedAt: number, expiresAt?: number): Promise<PurgeReservation>;
|
|
145
|
+
invalidateMatching(options: Pick<ResponseStoreInvalidateOptions, "tags" | "pathPrefixes">, invalidatedAt: number, expiresAt?: number, edgeFlushAt?: number): Promise<PurgeReservation>;
|
|
126
146
|
private writeR2Update;
|
|
127
147
|
private finishCompletedTombstones;
|
|
128
148
|
drainPendingTombstones(limit: number, keyHash?: string, afterKeyHash?: string, tombstoneSequence?: number): Promise<TombstoneDrainResult>;
|
|
@@ -131,5 +151,4 @@ export declare class CacheMetadata extends DurableObject<CacheMetadataEnv> {
|
|
|
131
151
|
markTombstonesEdgePurgedThrough(tombstoneSequence: number): void;
|
|
132
152
|
inspect(): StoredEntry[];
|
|
133
153
|
}
|
|
134
|
-
//#endregion
|
|
135
154
|
export {};
|
package/dist/metadata-do.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { mapSettledWithR2Concurrency } from "./r2-concurrency.js";
|
|
2
2
|
import { DurableObject } from "cloudflare:workers";
|
|
3
|
-
|
|
3
|
+
// src/metadata-do.ts
|
|
4
4
|
const REFRESHABLE_ENTRY = `tombstoned = 0 AND active_revision IS NOT NULL
|
|
5
5
|
AND object_key IS NOT NULL AND response_headers IS NOT NULL
|
|
6
6
|
AND fresh_until IS NOT NULL AND swr_until IS NOT NULL`;
|
|
@@ -9,6 +9,8 @@ const MAX_SQL_PARAMETERS = 100;
|
|
|
9
9
|
const ORPHAN_RETENTION_MS = 36e5;
|
|
10
10
|
const ORPHAN_CLEANUP_LIMIT = 100;
|
|
11
11
|
const ORPHAN_CLEANUP_RETRY_MS = 6e4;
|
|
12
|
+
const PENDING_TOMBSTONE_FLUSH_LIMIT = 400;
|
|
13
|
+
const EDGE_FLUSH_BACKLOG_KEY = "edgeFlushBacklog";
|
|
12
14
|
const MAX_R2_CAS_ATTEMPTS = 3;
|
|
13
15
|
function metadataInteger(value) {
|
|
14
16
|
if (value === void 0 || !/^\d+$/.test(value)) return void 0;
|
|
@@ -224,6 +226,26 @@ var CacheMetadata = class extends DurableObject {
|
|
|
224
226
|
if (current === null || current > scheduledTime) await this.ctx.storage.setAlarm(scheduledTime);
|
|
225
227
|
this.cleanupAlarmKnown = true;
|
|
226
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Schedule the alarm that retries queued R2 and edge work, or with `edgeFlushAt` sends the edge
|
|
231
|
+
* work then. Returns whether the alarm sends it, so the caller sends it otherwise.
|
|
232
|
+
*/
|
|
233
|
+
async scheduleEdgeFlush(edgeFlushAt) {
|
|
234
|
+
try {
|
|
235
|
+
if (edgeFlushAt !== void 0) {
|
|
236
|
+
const backlog = await this.ctx.storage.get(EDGE_FLUSH_BACKLOG_KEY);
|
|
237
|
+
if (backlog && (backlog.flushAt ?? Infinity) > edgeFlushAt) await this.ctx.storage.put(EDGE_FLUSH_BACKLOG_KEY, {
|
|
238
|
+
...backlog,
|
|
239
|
+
flushAt: edgeFlushAt
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
await this.scheduleCleanupAlarm(edgeFlushAt ?? Date.now() + ORPHAN_CLEANUP_RETRY_MS);
|
|
243
|
+
return edgeFlushAt !== void 0;
|
|
244
|
+
} catch (error) {
|
|
245
|
+
this.logCleanupFailure(error);
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
227
249
|
logCleanupFailure(error) {
|
|
228
250
|
console.error(JSON.stringify({
|
|
229
251
|
message: "Workers Response Store cleanup scheduling failed",
|
|
@@ -329,30 +351,61 @@ var CacheMetadata = class extends DurableObject {
|
|
|
329
351
|
}
|
|
330
352
|
async alarm() {
|
|
331
353
|
try {
|
|
332
|
-
|
|
354
|
+
const remaining = await this.flushPendingTombstones();
|
|
355
|
+
if (remaining.state === "backlog") {
|
|
333
356
|
await this.ctx.storage.setAlarm(Date.now());
|
|
334
357
|
this.cleanupAlarmKnown = true;
|
|
335
358
|
return;
|
|
336
359
|
}
|
|
360
|
+
if (remaining.state === "queued") {
|
|
361
|
+
const flushAt = Math.min(remaining.flushAt ?? Infinity, await this.ctx.storage.getAlarm() ?? Infinity);
|
|
362
|
+
const r2Pending = this.ctx.storage.sql.exec("SELECT EXISTS(SELECT 1 FROM pending_r2_tombstones WHERE r2_complete = 0) AS pending").one().pending === 1;
|
|
363
|
+
await this.scheduleCleanupAlarm(flushAt === Infinity || r2Pending ? Math.min(flushAt, Date.now() + ORPHAN_CLEANUP_RETRY_MS) : flushAt);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
337
366
|
await this.sweepExpiredPendingObjects();
|
|
338
367
|
} catch (error) {
|
|
339
368
|
console.error(JSON.stringify({
|
|
340
369
|
message: "Workers Response Store orphan cleanup failed",
|
|
341
370
|
error: error instanceof Error ? error.message : String(error)
|
|
342
371
|
}));
|
|
343
|
-
await this.ctx.storage.
|
|
344
|
-
this.
|
|
372
|
+
const backlog = await this.ctx.storage.get(EDGE_FLUSH_BACKLOG_KEY);
|
|
373
|
+
await this.scheduleCleanupAlarm(Math.min(backlog?.flushAt ?? Infinity, Date.now() + ORPHAN_CLEANUP_RETRY_MS));
|
|
345
374
|
}
|
|
346
375
|
}
|
|
347
376
|
async retryPendingTombstones() {
|
|
348
|
-
|
|
377
|
+
return (await this.flushPendingTombstones()).state !== "none";
|
|
378
|
+
}
|
|
379
|
+
/** Whether R2 or edge work is left once this run's batch is flushed, and why. */
|
|
380
|
+
async flushPendingTombstones() {
|
|
381
|
+
const backlog = await this.ctx.storage.get(EDGE_FLUSH_BACKLOG_KEY);
|
|
382
|
+
const queuedThrough = backlog?.queuedThrough ?? this.tombstoneSequence();
|
|
383
|
+
const drained = await this.drainPendingTombstones(PENDING_TOMBSTONE_FLUSH_LIMIT);
|
|
349
384
|
if (drained.failures.length) throw new AggregateError(drained.failures.map((failure) => new Error(failure)), "R2 tombstone cleanup failed");
|
|
350
|
-
const
|
|
385
|
+
const edgeBatches = [0, 1].map((edgeInvalidate) => this.ctx.storage.sql.exec(`SELECT * FROM pending_r2_tombstones
|
|
386
|
+
WHERE r2_complete = 1 AND edge_purge_complete = 0 AND edge_invalidate = ?
|
|
387
|
+
AND tombstone_sequence <= ?
|
|
388
|
+
ORDER BY key_hash LIMIT ?`, edgeInvalidate, queuedThrough, PENDING_TOMBSTONE_FLUSH_LIMIT).toArray().map(pendingEntryFromRow));
|
|
389
|
+
const edgeEntries = edgeBatches.flat();
|
|
351
390
|
if (edgeEntries.length) {
|
|
352
|
-
|
|
353
|
-
this.markTombstonesEdgePurged(
|
|
391
|
+
const accepted = await this.getR2TombstoneEdgePurger().purgeR2TombstoneEdges(edgeEntries);
|
|
392
|
+
if (accepted.length) this.markTombstonesEdgePurged(accepted);
|
|
393
|
+
if (accepted.length < edgeEntries.length) throw new Error("Workers Response Store cache purge is unavailable");
|
|
394
|
+
}
|
|
395
|
+
if (drained.pending.length === PENDING_TOMBSTONE_FLUSH_LIMIT || edgeBatches.some((batch) => batch.length === PENDING_TOMBSTONE_FLUSH_LIMIT)) {
|
|
396
|
+
const scheduled = await this.ctx.storage.getAlarm() ?? Infinity;
|
|
397
|
+
const flushAt = Math.min(backlog?.flushAt ?? Infinity, scheduled);
|
|
398
|
+
await this.ctx.storage.put(EDGE_FLUSH_BACKLOG_KEY, {
|
|
399
|
+
queuedThrough,
|
|
400
|
+
...flushAt === Infinity ? {} : { flushAt }
|
|
401
|
+
});
|
|
402
|
+
return { state: "backlog" };
|
|
354
403
|
}
|
|
355
|
-
|
|
404
|
+
if (backlog) await this.ctx.storage.delete(EDGE_FLUSH_BACKLOG_KEY);
|
|
405
|
+
return {
|
|
406
|
+
state: this.ctx.storage.sql.exec("SELECT EXISTS(SELECT 1 FROM pending_r2_tombstones) AS pending").one().pending === 1 ? "queued" : "none",
|
|
407
|
+
...backlog?.flushAt === void 0 ? {} : { flushAt: backlog.flushAt }
|
|
408
|
+
};
|
|
356
409
|
}
|
|
357
410
|
async claimRevalidation(keyHash, activeRevision, cacheKey, objectKeyPrefix, now, leaseMs) {
|
|
358
411
|
const claim = this.ctx.storage.transactionSync(() => {
|
|
@@ -504,7 +557,7 @@ var CacheMetadata = class extends DurableObject {
|
|
|
504
557
|
END
|
|
505
558
|
WHERE key_hash = ? AND latest_revision >= ?
|
|
506
559
|
AND (active_revision IS NULL OR active_revision < ?)
|
|
507
|
-
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 &&
|
|
560
|
+
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 && replaces.revision <= (current.active_revision ?? 0) ? replaces.revision : null, keyHash, revision, revision).toArray().length === 1;
|
|
508
561
|
if (published) this.replaceEntryTags(keyHash, metadata.cacheTags);
|
|
509
562
|
this.ctx.storage.sql.exec("DELETE FROM pending_objects WHERE object_key = ?", reservationObjectKey);
|
|
510
563
|
if (claimId) this.ctx.storage.sql.exec("DELETE FROM revalidation_claims WHERE key_hash = ? AND claim_id = ?", keyHash, claimId);
|
|
@@ -522,16 +575,16 @@ var CacheMetadata = class extends DurableObject {
|
|
|
522
575
|
expiryBehavior: metadata.expiryBehavior,
|
|
523
576
|
cacheTags: metadata.cacheTags
|
|
524
577
|
};
|
|
525
|
-
const
|
|
578
|
+
const replacedEdgeCopyGone = (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;
|
|
526
579
|
return {
|
|
527
|
-
edgePurgeRequired: published && current.active_revision !== null && !
|
|
580
|
+
edgePurgeRequired: published && current.active_revision !== null && !replacedEdgeCopyGone,
|
|
528
581
|
entry: published ? entry : null,
|
|
529
582
|
published,
|
|
530
583
|
...published && (startedBeforeInvalidation || freshUntil <= now) ? { publishedStale: true } : {}
|
|
531
584
|
};
|
|
532
585
|
});
|
|
533
586
|
}
|
|
534
|
-
async invalidatePublishedRevision(keyHash, revision) {
|
|
587
|
+
async invalidatePublishedRevision(keyHash, revision, edgeFlushAt) {
|
|
535
588
|
if (!this.ctx.storage.transactionSync(() => {
|
|
536
589
|
const current = this.ctx.storage.sql.exec("SELECT * FROM entries WHERE key_hash = ?", keyHash).toArray()[0];
|
|
537
590
|
if (!current || current.active_revision !== revision || current.object_key === null) return false;
|
|
@@ -554,8 +607,37 @@ var CacheMetadata = class extends DurableObject {
|
|
|
554
607
|
pending: [],
|
|
555
608
|
purged: []
|
|
556
609
|
};
|
|
557
|
-
|
|
558
|
-
return
|
|
610
|
+
const edgeFlushScheduled = await this.scheduleEdgeFlush(edgeFlushAt);
|
|
611
|
+
return {
|
|
612
|
+
...await this.drainPendingTombstones(1, keyHash),
|
|
613
|
+
...edgeFlushScheduled ? { edgeFlushScheduled: true } : {}
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Queue a purge of the key's edge copy, which a put replacing the entry needs, for the alarm
|
|
618
|
+
* to send at `edgeFlushAt` with the rest of the queued edge work. Called once the
|
|
619
|
+
* replacement is in R2, so the edge can only refill with it. Work already queued for the
|
|
620
|
+
* key purges instead of invalidating, and gets this revision, so a flush that listed it
|
|
621
|
+
* before can't mark this purge sent. Returns whether the alarm will send it; if not, the
|
|
622
|
+
* caller sends it and marks it sent.
|
|
623
|
+
*/
|
|
624
|
+
async queueEdgePurge(keyHash, revision, edgeFlushAt) {
|
|
625
|
+
this.ctx.storage.transactionSync(() => this.ctx.storage.sql.exec(`INSERT INTO pending_r2_tombstones
|
|
626
|
+
(key_hash, cache_key, object_key, revision, r2_complete, tombstone_sequence)
|
|
627
|
+
SELECT key_hash, cache_key, COALESCE(object_key, ''), ?, 1, ?
|
|
628
|
+
FROM entries WHERE key_hash = ?
|
|
629
|
+
ON CONFLICT(key_hash) DO UPDATE SET
|
|
630
|
+
revision = CASE
|
|
631
|
+
WHEN pending_r2_tombstones.r2_complete = 1 THEN excluded.revision
|
|
632
|
+
ELSE pending_r2_tombstones.revision
|
|
633
|
+
END,
|
|
634
|
+
tombstone_sequence = CASE
|
|
635
|
+
WHEN pending_r2_tombstones.r2_complete = 1 THEN excluded.tombstone_sequence
|
|
636
|
+
ELSE pending_r2_tombstones.tombstone_sequence
|
|
637
|
+
END,
|
|
638
|
+
edge_invalidate = 0,
|
|
639
|
+
edge_purge_complete = 0`, revision, this.tombstoneSequence(true), keyHash));
|
|
640
|
+
return this.scheduleEdgeFlush(edgeFlushAt);
|
|
559
641
|
}
|
|
560
642
|
getEntry(keyHash) {
|
|
561
643
|
const row = this.ctx.storage.sql.exec("SELECT * FROM entries WHERE key_hash = ?", keyHash).toArray()[0];
|
|
@@ -632,7 +714,7 @@ var CacheMetadata = class extends DurableObject {
|
|
|
632
714
|
staleAt
|
|
633
715
|
]));
|
|
634
716
|
}
|
|
635
|
-
async purgeMatching(options, invalidatedAt = Date.now()) {
|
|
717
|
+
async purgeMatching(options, invalidatedAt = Date.now(), edgeFlushAt) {
|
|
636
718
|
const reservation = this.ctx.storage.transactionSync(() => {
|
|
637
719
|
const matches = this.findMatchingEntryRows(options, "purge");
|
|
638
720
|
const tombstoneSequence = this.tombstoneSequence(matches.some((row) => row.object_key !== null));
|
|
@@ -674,7 +756,10 @@ var CacheMetadata = class extends DurableObject {
|
|
|
674
756
|
tombstoneSequence
|
|
675
757
|
};
|
|
676
758
|
});
|
|
677
|
-
if (reservation.pendingTombstones > 0
|
|
759
|
+
if (reservation.pendingTombstones > 0 && await this.scheduleEdgeFlush(edgeFlushAt)) return {
|
|
760
|
+
...reservation,
|
|
761
|
+
edgeFlushScheduled: true
|
|
762
|
+
};
|
|
678
763
|
return reservation;
|
|
679
764
|
}
|
|
680
765
|
/**
|
|
@@ -683,7 +768,7 @@ var CacheMetadata = class extends DurableObject {
|
|
|
683
768
|
* `expiresAt`, and its R2 object is queued for the same rewrite, so writes
|
|
684
769
|
* reserved before the invalidation cannot publish over it.
|
|
685
770
|
*/
|
|
686
|
-
async invalidateMatching(options, invalidatedAt, expiresAt) {
|
|
771
|
+
async invalidateMatching(options, invalidatedAt, expiresAt, edgeFlushAt) {
|
|
687
772
|
const reservation = this.ctx.storage.transactionSync(() => {
|
|
688
773
|
const rows = this.findMatchingEntryRows(options, "invalidate");
|
|
689
774
|
const matches = rows.filter((row) => Boolean(row.refreshable));
|
|
@@ -693,7 +778,8 @@ var CacheMetadata = class extends DurableObject {
|
|
|
693
778
|
this.recordInvalidations(rows.map((row) => row.key_hash), tags, 0, expiresAt ?? null, invalidatedAt);
|
|
694
779
|
for (const batch of batches(reserved, MAX_SQL_PARAMETERS)) this.ctx.storage.sql.exec(`UPDATE entries SET
|
|
695
780
|
latest_revision = latest_revision + 1,
|
|
696
|
-
active_revision = latest_revision + 1
|
|
781
|
+
active_revision = latest_revision + 1,
|
|
782
|
+
invalidated_revision = latest_revision + 1
|
|
697
783
|
WHERE tombstoned = 1 AND key_hash IN (${batch.map(() => "?").join(", ")})`, ...batch);
|
|
698
784
|
for (const batch of batches(matches, MAX_SQL_PARAMETERS / 10)) this.ctx.storage.sql.exec(`INSERT INTO pending_r2_tombstones
|
|
699
785
|
(key_hash, cache_key, object_key, revision, tombstone_sequence,
|
|
@@ -743,7 +829,10 @@ var CacheMetadata = class extends DurableObject {
|
|
|
743
829
|
tombstoneSequence
|
|
744
830
|
};
|
|
745
831
|
});
|
|
746
|
-
if (reservation.pendingTombstones > 0
|
|
832
|
+
if (reservation.pendingTombstones > 0 && await this.scheduleEdgeFlush(edgeFlushAt)) return {
|
|
833
|
+
...reservation,
|
|
834
|
+
edgeFlushScheduled: true
|
|
835
|
+
};
|
|
747
836
|
return reservation;
|
|
748
837
|
}
|
|
749
838
|
async writeR2Update(entry) {
|
|
@@ -818,13 +907,16 @@ var CacheMetadata = class extends DurableObject {
|
|
|
818
907
|
this.ctx.storage.transactionSync(() => {
|
|
819
908
|
for (const edgeInvalidate of [true, false]) {
|
|
820
909
|
const completed = purged.filter((entry) => Boolean(entry.edgeInvalidate) === edgeInvalidate);
|
|
821
|
-
for (const batch of batches(completed, MAX_SQL_PARAMETERS / 2 - 1)) this.ctx.storage.sql.exec(`UPDATE pending_r2_tombstones SET r2_complete = 1,
|
|
910
|
+
for (const batch of batches(completed, MAX_SQL_PARAMETERS / 2 - 1)) this.ctx.storage.sql.exec(`UPDATE pending_r2_tombstones SET r2_complete = 1,
|
|
911
|
+
edge_invalidate = MIN(edge_invalidate, ?) WHERE ${batch.map(() => "(key_hash = ? AND revision = ?)").join(" OR ")}`, Number(edgeInvalidate), ...batch.flatMap((entry) => [entry.keyHash, entry.revision]));
|
|
822
912
|
}
|
|
823
913
|
this.finishCompletedTombstones(purged);
|
|
824
914
|
});
|
|
915
|
+
const failures = settled.flatMap((result) => result.status === "rejected" ? [result.reason instanceof Error ? result.reason.message : String(result.reason)] : []);
|
|
916
|
+
if (failures.length) await this.scheduleCleanupAlarm(Date.now() + ORPHAN_CLEANUP_RETRY_MS).catch((error) => this.logCleanupFailure(error));
|
|
825
917
|
return {
|
|
826
918
|
...rows.length ? { cursor: rows.at(-1).key_hash } : {},
|
|
827
|
-
failures
|
|
919
|
+
failures,
|
|
828
920
|
pending,
|
|
829
921
|
purged
|
|
830
922
|
};
|
|
@@ -849,5 +941,4 @@ var CacheMetadata = class extends DurableObject {
|
|
|
849
941
|
return storedEntriesFromRows(this.ctx.storage.sql.exec("SELECT * FROM entries ORDER BY cache_key").toArray());
|
|
850
942
|
}
|
|
851
943
|
};
|
|
852
|
-
//#endregion
|
|
853
944
|
export { CacheMetadata };
|
package/dist/r2-concurrency.d.ts
CHANGED
|
@@ -1,3 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function mapSettledWithR2Concurrency<T, R>(values: readonly T[], callback: (value: T) => Promise<R>): Promise<PromiseSettledResult<R>[]>;
|
|
3
|
-
//#endregion
|
|
1
|
+
export declare function mapSettledWithR2Concurrency<T, R>(values: readonly T[], callback: (value: T) => Promise<R>): Promise<PromiseSettledResult<R>[]>;
|
package/dist/r2-concurrency.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/r2-concurrency.ts
|
|
2
2
|
const MAX_R2_CONCURRENCY = 6;
|
|
3
3
|
async function mapSettledWithR2Concurrency(values, callback) {
|
|
4
4
|
const results = [];
|
|
@@ -21,5 +21,4 @@ async function mapSettledWithR2Concurrency(values, callback) {
|
|
|
21
21
|
}));
|
|
22
22
|
return results;
|
|
23
23
|
}
|
|
24
|
-
//#endregion
|
|
25
24
|
export { mapSettledWithR2Concurrency };
|
package/dist/service.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { ResponseStoreBinding, ResponseStoreService } from "./binding.js";
|
|
2
1
|
import { CacheMetadata } from "./metadata-do.js";
|
|
3
|
-
|
|
2
|
+
import { ResponseStoreBinding, ResponseStoreService } from "./binding.js";
|
|
4
3
|
declare const _default: {
|
|
5
4
|
fetch(): Response;
|
|
6
5
|
};
|
|
7
|
-
//#endregion
|
|
8
6
|
export { CacheMetadata, ResponseStoreBinding, ResponseStoreService, _default as default };
|
package/dist/service.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { ResponseStoreBinding, ResponseStoreService } from "./binding.js";
|
|
2
1
|
import { CacheMetadata } from "./metadata-do.js";
|
|
3
|
-
|
|
2
|
+
import { ResponseStoreBinding, ResponseStoreService } from "./binding.js";
|
|
3
|
+
// src/service.ts
|
|
4
4
|
var service_default = { fetch() {
|
|
5
5
|
return new Response("Use the ResponseStoreService service binding entrypoint.", { status: 404 });
|
|
6
6
|
} };
|
|
7
|
-
//#endregion
|
|
8
7
|
export { CacheMetadata, ResponseStoreBinding, ResponseStoreService, service_default as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/workers-response-store",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "An R2-backed programmatic response store for Cloudflare Workers.",
|
|
5
5
|
"homepage": "https://vinext.dev/",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@types/node": "^25.9.2",
|
|
29
29
|
"miniflare": "5.20260730.0-alpha",
|
|
30
30
|
"typescript": "^7.0.0",
|
|
31
|
-
"vite-plus": "1.
|
|
31
|
+
"vite-plus": "1.5.1",
|
|
32
32
|
"wrangler": "^4.153.0"
|
|
33
33
|
},
|
|
34
34
|
"engines": {
|