@cloudflare/workers-response-store 0.1.0-beta.0 → 0.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +250 -45
- package/dist/binding.d.ts +32 -1
- package/dist/binding.js +485 -80
- package/dist/index.d.ts +10 -2
- package/dist/index.js +12 -3
- package/dist/isolate-negative-cache.d.ts +11 -0
- package/dist/isolate-negative-cache.js +34 -0
- package/dist/metadata-do.d.ts +48 -11
- package/dist/metadata-do.js +348 -133
- package/dist/r2-concurrency.d.ts +3 -0
- package/dist/r2-concurrency.js +25 -0
- package/package.json +1 -1
package/dist/binding.js
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
import { deriveCachePolicy, edgeCacheControl, representationAge } from "./cache-policy.js";
|
|
2
|
+
import { IsolateNegativeCache } from "./isolate-negative-cache.js";
|
|
2
3
|
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
3
4
|
//#region src/binding.ts
|
|
5
|
+
var R2PublicationError = class extends Error {
|
|
6
|
+
constructor(cause) {
|
|
7
|
+
super("R2 response publication failed", { cause });
|
|
8
|
+
}
|
|
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
|
+
}
|
|
4
27
|
function getWorkersResponseStore(ctx, props = {}) {
|
|
5
28
|
const binding = Reflect.get(ctx.exports, "ResponseStoreBinding");
|
|
6
29
|
if (typeof binding !== "function") throw new Error("The ResponseStoreBinding entrypoint is not exported");
|
|
@@ -36,22 +59,111 @@ const MISS_HEADERS = {
|
|
|
36
59
|
};
|
|
37
60
|
const BACKGROUND_REVALIDATION_LEASE_MS = 3e4;
|
|
38
61
|
const CACHE_PURGE_BATCH_SIZE = 100;
|
|
62
|
+
const PURGE_TOMBSTONE_BATCH_SIZE = 400;
|
|
63
|
+
const REFRESH_CONCURRENCY = 6;
|
|
64
|
+
const ISOLATE_MISS_CACHE_CAPACITY = 1024;
|
|
65
|
+
const ISOLATE_MISS_CACHE_TTL_MS = 1e3;
|
|
66
|
+
const MAX_R2_CAS_ATTEMPTS = 3;
|
|
39
67
|
const MAX_CACHE_TAG_HEADER_BYTES = 16384;
|
|
68
|
+
const R2_CUSTOM_METADATA_SAFE_BYTES = 7168;
|
|
40
69
|
const NULL_BODY_STATUSES = /* @__PURE__ */ new Set([
|
|
41
70
|
204,
|
|
42
71
|
205,
|
|
43
72
|
304
|
|
44
73
|
]);
|
|
45
74
|
const AGE_BASIS_HEADER = "X-Workers-Response-Store-Age-Basis";
|
|
75
|
+
const STORAGE_LAYOUT_VERSION = "r2-v1";
|
|
46
76
|
const pendingPuts = /* @__PURE__ */ new Map();
|
|
77
|
+
const pendingR2Reads = /* @__PURE__ */ new Map();
|
|
78
|
+
const entryReads = new IsolateNegativeCache(ISOLATE_MISS_CACHE_CAPACITY, ISOLATE_MISS_CACHE_TTL_MS);
|
|
79
|
+
function validateResponseStoreShards(shards) {
|
|
80
|
+
if (shards !== void 0 && (!Number.isSafeInteger(shards) || shards <= 1)) throw new TypeError("Workers Response Store shards must be an integer greater than 1");
|
|
81
|
+
return shards;
|
|
82
|
+
}
|
|
47
83
|
function* batches(values, size) {
|
|
48
84
|
for (let offset = 0; offset < values.length; offset += size) yield values.slice(offset, offset + size);
|
|
49
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
|
+
}
|
|
50
108
|
function metadataInteger(value) {
|
|
51
109
|
if (!value || !/^\d+$/.test(value)) return;
|
|
52
110
|
const parsed = Number(value);
|
|
53
111
|
return Number.isSafeInteger(parsed) ? parsed : void 0;
|
|
54
112
|
}
|
|
113
|
+
function metadataJson(value) {
|
|
114
|
+
if (value === void 0) return void 0;
|
|
115
|
+
try {
|
|
116
|
+
return JSON.parse(value);
|
|
117
|
+
} catch {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function isHeaderEntries(value) {
|
|
122
|
+
return Array.isArray(value) && value.every((entry) => Array.isArray(entry) && entry.length === 2 && entry.every((item) => typeof item === "string"));
|
|
123
|
+
}
|
|
124
|
+
function customMetadataSize(metadata) {
|
|
125
|
+
const encoder = new TextEncoder();
|
|
126
|
+
return Object.entries(metadata).reduce((size, [key, value]) => size + encoder.encode(key).byteLength + encoder.encode(value).byteLength, 0);
|
|
127
|
+
}
|
|
128
|
+
async function readBodyPrefix(body, prefixLength) {
|
|
129
|
+
const reader = body.getReader();
|
|
130
|
+
const chunks = [];
|
|
131
|
+
let length = 0;
|
|
132
|
+
try {
|
|
133
|
+
while (length < prefixLength) {
|
|
134
|
+
const { done, value } = await reader.read();
|
|
135
|
+
if (done) throw new Error("R2 response metadata prefix is incomplete");
|
|
136
|
+
chunks.push(value);
|
|
137
|
+
length += value.byteLength;
|
|
138
|
+
}
|
|
139
|
+
} catch (error) {
|
|
140
|
+
await reader.cancel(error).catch(() => {});
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
const buffered = new Uint8Array(length);
|
|
144
|
+
let offset = 0;
|
|
145
|
+
for (const chunk of chunks) {
|
|
146
|
+
buffered.set(chunk, offset);
|
|
147
|
+
offset += chunk.byteLength;
|
|
148
|
+
}
|
|
149
|
+
const leftover = buffered.subarray(prefixLength);
|
|
150
|
+
return {
|
|
151
|
+
prefix: buffered.subarray(0, prefixLength),
|
|
152
|
+
remainder: new ReadableStream({
|
|
153
|
+
start(controller) {
|
|
154
|
+
if (leftover.byteLength) controller.enqueue(leftover);
|
|
155
|
+
},
|
|
156
|
+
async pull(controller) {
|
|
157
|
+
const { done, value } = await reader.read();
|
|
158
|
+
if (done) controller.close();
|
|
159
|
+
else controller.enqueue(value);
|
|
160
|
+
},
|
|
161
|
+
cancel(reason) {
|
|
162
|
+
return reader.cancel(reason);
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
};
|
|
166
|
+
}
|
|
55
167
|
function purgeTagForEntry(entry) {
|
|
56
168
|
return `runtime-cache-${entry.keyHash}`;
|
|
57
169
|
}
|
|
@@ -75,14 +187,42 @@ function cacheTagsFromResponse(response) {
|
|
|
75
187
|
return [...new Set((response.headers.get("Cache-Tag") ?? "").split(",").map((tag) => tag.trim()).filter(Boolean))];
|
|
76
188
|
}
|
|
77
189
|
var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
190
|
+
shardCount = validateResponseStoreShards(this.ctx.props?.shards) ?? 1;
|
|
78
191
|
getVersionId() {
|
|
79
192
|
const versionId = this.ctx.props?.versionId ?? this.env.CF_VERSION_METADATA?.id;
|
|
80
193
|
if (!versionId) throw new Error("Workers Response Store requires a version_metadata binding");
|
|
81
194
|
return versionId;
|
|
82
195
|
}
|
|
83
|
-
|
|
196
|
+
getMetadataShard(index) {
|
|
84
197
|
const locationHint = this.ctx.props?.locationHint;
|
|
85
|
-
|
|
198
|
+
const shards = this.shardCount;
|
|
199
|
+
const layoutName = `${this.getVersionId()}:${STORAGE_LAYOUT_VERSION}`;
|
|
200
|
+
const name = shards === 1 ? layoutName : `${layoutName}:metadata-shard:${index}-of-${shards}`;
|
|
201
|
+
return this.env.CACHE_METADATA.getByName(name, locationHint ? { locationHint } : void 0);
|
|
202
|
+
}
|
|
203
|
+
getMetadata(keyHash) {
|
|
204
|
+
const shards = this.shardCount;
|
|
205
|
+
const index = shards === 1 ? 0 : Number.parseInt(keyHash.slice(0, 8), 16) % shards;
|
|
206
|
+
return this.getMetadataShard(index);
|
|
207
|
+
}
|
|
208
|
+
getMetadataShards() {
|
|
209
|
+
return Array.from({ length: this.shardCount }, (_, index) => this.getMetadataShard(index));
|
|
210
|
+
}
|
|
211
|
+
entryReadKey(keyHash) {
|
|
212
|
+
return `${this.getVersionId()}:${this.shardCount}:${keyHash}`;
|
|
213
|
+
}
|
|
214
|
+
invalidateEntryRead(keyHash) {
|
|
215
|
+
const key = this.entryReadKey(keyHash);
|
|
216
|
+
entryReads.delete(key);
|
|
217
|
+
pendingR2Reads.delete(key);
|
|
218
|
+
}
|
|
219
|
+
getTagMetadata(tags) {
|
|
220
|
+
const shards = this.shardCount;
|
|
221
|
+
if (shards === 1) return this.getMetadataShard(0);
|
|
222
|
+
let hash = 2166136261;
|
|
223
|
+
const normalized = [...new Set(tags.map((tag) => tag.trim().toLowerCase()).filter(Boolean))].sort().join("\0");
|
|
224
|
+
for (let index = 0; index < normalized.length; index++) hash = Math.imul(hash ^ normalized.charCodeAt(index), 16777619);
|
|
225
|
+
return this.getMetadataShard((hash >>> 0) % shards);
|
|
86
226
|
}
|
|
87
227
|
async deriveCacheKey(request) {
|
|
88
228
|
if (request.method !== "GET") throw new TypeError("Workers Response Store keys must be GET requests");
|
|
@@ -95,6 +235,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
95
235
|
};
|
|
96
236
|
}
|
|
97
237
|
async purgeEdgeCache(options) {
|
|
238
|
+
if (this.env.WORKERS_RESPONSE_STORE_E2E_EDGE_PURGE_MODE === "disabled") return false;
|
|
98
239
|
if (!this.ctx.cache) {
|
|
99
240
|
console.error(JSON.stringify({
|
|
100
241
|
message: "Workers Response Store cache purge is unavailable",
|
|
@@ -119,18 +260,152 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
119
260
|
for (const batch of batches(tags, CACHE_PURGE_BATCH_SIZE)) if (!await this.purgeEdgeCache({ tags: batch })) accepted = false;
|
|
120
261
|
return accepted;
|
|
121
262
|
}
|
|
263
|
+
async purgeR2TombstoneEdges(entries) {
|
|
264
|
+
return await this.purgeEdgeCacheByTags(entries.map(purgeTagForEntry));
|
|
265
|
+
}
|
|
266
|
+
async purgePendingEdgeEntries(metadata, tombstoneSequence) {
|
|
267
|
+
for (;;) {
|
|
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;
|
|
271
|
+
await metadata.markTombstonesEdgePurged(entries);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
122
274
|
objectKeyRoot() {
|
|
123
|
-
|
|
275
|
+
const shards = this.shardCount;
|
|
276
|
+
return [
|
|
277
|
+
"runtime-cache",
|
|
278
|
+
this.getVersionId(),
|
|
279
|
+
STORAGE_LAYOUT_VERSION,
|
|
280
|
+
...shards === 1 ? [] : [`shards-${shards}`]
|
|
281
|
+
].join("/");
|
|
124
282
|
}
|
|
125
283
|
objectKeyPrefix(keyHash) {
|
|
126
284
|
return `${this.objectKeyRoot()}/${keyHash}`;
|
|
127
285
|
}
|
|
128
|
-
|
|
286
|
+
r2ObjectKey(keyHash) {
|
|
287
|
+
return `${this.objectKeyPrefix(keyHash)}/active`;
|
|
288
|
+
}
|
|
289
|
+
entryFromR2Metadata(keyHash, cacheKey, object) {
|
|
290
|
+
const metadata = object?.customMetadata;
|
|
291
|
+
if (!metadata || metadata.tombstoned === "1") return null;
|
|
292
|
+
const latestRevision = metadataInteger(metadata.latestRevision);
|
|
293
|
+
const freshUntil = metadataInteger(metadata.freshUntil);
|
|
294
|
+
const swrUntil = metadataInteger(metadata.swrUntil);
|
|
295
|
+
const responseMetadataBytes = metadataInteger(metadata.responseMetadataBytes);
|
|
296
|
+
const responseHeaders = metadataJson(metadata.responseHeaders);
|
|
297
|
+
const hasResponseMetadataPrefix = responseMetadataBytes !== void 0 && responseMetadataBytes > 0 && responseMetadataBytes <= object.size;
|
|
298
|
+
if (latestRevision === void 0 || freshUntil === void 0 || swrUntil === void 0 || !hasResponseMetadataPrefix && (typeof metadata.statusText !== "string" || !isHeaderEntries(responseHeaders))) return null;
|
|
129
299
|
return {
|
|
300
|
+
keyHash,
|
|
130
301
|
cacheKey,
|
|
302
|
+
objectKey: this.r2ObjectKey(keyHash),
|
|
303
|
+
statusText: hasResponseMetadataPrefix ? "" : metadata.statusText,
|
|
304
|
+
responseHeaders: hasResponseMetadataPrefix ? [] : responseHeaders,
|
|
305
|
+
freshUntil,
|
|
306
|
+
swrUntil,
|
|
307
|
+
revalidator: null,
|
|
308
|
+
cacheTags: [],
|
|
309
|
+
activeRevision: latestRevision,
|
|
310
|
+
latestRevision
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
async readR2Metadata(cacheKey) {
|
|
314
|
+
const object = await this.env.CACHE_BODIES.get(this.r2ObjectKey(cacheKey.keyHash));
|
|
315
|
+
const entry = this.entryFromR2Metadata(cacheKey.keyHash, cacheKey.cacheKey, object);
|
|
316
|
+
if (!entry && object) await object.body.cancel();
|
|
317
|
+
return {
|
|
318
|
+
entry,
|
|
319
|
+
...entry && object ? { object } : {}
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
async readR2MetadataCached(cacheKey) {
|
|
323
|
+
const readKey = this.entryReadKey(cacheKey.keyHash);
|
|
324
|
+
if (entryReads.has(readKey)) return { entry: null };
|
|
325
|
+
const existing = pendingR2Reads.get(readKey);
|
|
326
|
+
if (existing && !await existing) return { entry: null };
|
|
327
|
+
const read = this.readR2Metadata(cacheKey);
|
|
328
|
+
if (existing) return read;
|
|
329
|
+
let pending;
|
|
330
|
+
pending = read.then(({ entry }) => {
|
|
331
|
+
if (!entry && pendingR2Reads.get(readKey) === pending) entryReads.add(readKey);
|
|
332
|
+
return entry !== null;
|
|
333
|
+
}, () => true);
|
|
334
|
+
pendingR2Reads.set(readKey, pending);
|
|
335
|
+
try {
|
|
336
|
+
return await read;
|
|
337
|
+
} finally {
|
|
338
|
+
if (pendingR2Reads.get(readKey) === pending) pendingR2Reads.delete(readKey);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
async writeR2Revision(objectKey, revision, body, customMetadata, expectedEtag) {
|
|
342
|
+
let etag = expectedEtag;
|
|
343
|
+
for (let attempt = 0; attempt < MAX_R2_CAS_ATTEMPTS; attempt++) {
|
|
344
|
+
if (etag === void 0) {
|
|
345
|
+
const current = await this.env.CACHE_BODIES.head(objectKey);
|
|
346
|
+
const currentRevision = metadataInteger(current?.customMetadata?.latestRevision);
|
|
347
|
+
if (currentRevision !== void 0 && currentRevision >= revision) return false;
|
|
348
|
+
etag = current?.etag ?? null;
|
|
349
|
+
}
|
|
350
|
+
if (await this.env.CACHE_BODIES.put(objectKey, body, {
|
|
351
|
+
onlyIf: etag === null ? { etagDoesNotMatch: "*" } : { etagMatches: etag },
|
|
352
|
+
customMetadata
|
|
353
|
+
})) return true;
|
|
354
|
+
etag = void 0;
|
|
355
|
+
}
|
|
356
|
+
const currentRevision = metadataInteger((await this.env.CACHE_BODIES.head(objectKey))?.customMetadata?.latestRevision);
|
|
357
|
+
if (currentRevision !== void 0 && currentRevision >= revision) return false;
|
|
358
|
+
throw new Error(`R2 revision ${revision} could not be published after concurrent writes`);
|
|
359
|
+
}
|
|
360
|
+
async writeR2Response(entry, body, status, createdAt, initialAge, expectedEtag) {
|
|
361
|
+
this.invalidateEntryRead(entry.keyHash);
|
|
362
|
+
try {
|
|
363
|
+
const responseHeaders = JSON.stringify(entry.responseHeaders);
|
|
364
|
+
let storedBody = body;
|
|
365
|
+
let customMetadata = {
|
|
366
|
+
status: String(status),
|
|
367
|
+
createdAt: String(createdAt),
|
|
368
|
+
initialAge: String(initialAge),
|
|
369
|
+
statusText: entry.statusText,
|
|
370
|
+
responseHeaders,
|
|
371
|
+
freshUntil: String(entry.freshUntil),
|
|
372
|
+
swrUntil: String(entry.swrUntil),
|
|
373
|
+
latestRevision: String(entry.activeRevision)
|
|
374
|
+
};
|
|
375
|
+
if (customMetadataSize(customMetadata) > R2_CUSTOM_METADATA_SAFE_BYTES) {
|
|
376
|
+
const responseMetadata = new TextEncoder().encode(JSON.stringify({
|
|
377
|
+
statusText: entry.statusText,
|
|
378
|
+
responseHeaders: entry.responseHeaders
|
|
379
|
+
}));
|
|
380
|
+
storedBody = new Blob([responseMetadata, body]);
|
|
381
|
+
customMetadata = {
|
|
382
|
+
status: String(status),
|
|
383
|
+
createdAt: String(createdAt),
|
|
384
|
+
initialAge: String(initialAge),
|
|
385
|
+
responseMetadataBytes: String(responseMetadata.byteLength),
|
|
386
|
+
freshUntil: String(entry.freshUntil),
|
|
387
|
+
swrUntil: String(entry.swrUntil),
|
|
388
|
+
latestRevision: String(entry.activeRevision)
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
return await this.writeR2Revision(this.r2ObjectKey(entry.keyHash), entry.activeRevision, storedBody, customMetadata, expectedEtag);
|
|
392
|
+
} finally {
|
|
393
|
+
this.invalidateEntryRead(entry.keyHash);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
readableEntry(entry) {
|
|
397
|
+
return entry ? {
|
|
398
|
+
...entry,
|
|
399
|
+
cacheTags: [],
|
|
400
|
+
objectKey: this.r2ObjectKey(entry.keyHash)
|
|
401
|
+
} : entry;
|
|
402
|
+
}
|
|
403
|
+
async reserveWrite(metadata, cacheKey, cacheTags) {
|
|
404
|
+
const reservation = await metadata.reserveWrite(cacheKey.keyHash, cacheKey.cacheKey, this.objectKeyPrefix(cacheKey.keyHash), Date.now());
|
|
405
|
+
return {
|
|
406
|
+
...cacheKey,
|
|
131
407
|
fenceTags: cacheTags,
|
|
132
|
-
|
|
133
|
-
...await metadata.reserveWrite(keyHash, cacheKey, this.objectKeyPrefix(keyHash), Date.now())
|
|
408
|
+
...reservation
|
|
134
409
|
};
|
|
135
410
|
}
|
|
136
411
|
logCleanupFailure(objectKey, error) {
|
|
@@ -143,16 +418,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
143
418
|
async releaseFailedWrite(metadata, write) {
|
|
144
419
|
await metadata.releaseWrite(write.keyHash, write.objectKey, write.claimId).catch((error) => this.logCleanupFailure(write.objectKey, error));
|
|
145
420
|
}
|
|
146
|
-
|
|
147
|
-
const object = await this.env.CACHE_BODIES.get(entry.objectKey);
|
|
148
|
-
if (!object) return null;
|
|
149
|
-
const status = metadataInteger(object.customMetadata?.status);
|
|
150
|
-
const createdAt = metadataInteger(object.customMetadata?.createdAt);
|
|
151
|
-
const initialAge = metadataInteger(object.customMetadata?.initialAge);
|
|
152
|
-
if (status === void 0 || status < 200 || status > 599 || createdAt === void 0 || initialAge === void 0) {
|
|
153
|
-
await object.body.cancel();
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
421
|
+
createStoredResponse(entry, body, status, createdAt, initialAge, now = Date.now()) {
|
|
156
422
|
const headers = new Headers(entry.responseHeaders);
|
|
157
423
|
headers.set(AGE_BASIS_HEADER, `${createdAt}:${initialAge}`);
|
|
158
424
|
headers.set("Age", String(representationAge(createdAt, initialAge, now)));
|
|
@@ -161,29 +427,63 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
161
427
|
headers.set("X-Workers-Response-Store", now < entry.freshUntil ? "BLOB-FRESH" : "BLOB-STALE");
|
|
162
428
|
headers.set("X-Workers-Response-Store-Revision", String(entry.activeRevision));
|
|
163
429
|
headers.set("X-Workers-Response-Store-Binding-Invocation", crypto.randomUUID());
|
|
164
|
-
const body = NULL_BODY_STATUSES.has(status) ? null : object.body;
|
|
165
|
-
if (!body) await object.body.cancel();
|
|
166
430
|
return new Response(body, {
|
|
167
431
|
status,
|
|
168
432
|
statusText: entry.statusText,
|
|
169
433
|
headers
|
|
170
434
|
});
|
|
171
435
|
}
|
|
172
|
-
async
|
|
436
|
+
async readStoredResponse(entry, now = Date.now(), prefetchedObject) {
|
|
437
|
+
const object = prefetchedObject ?? await this.env.CACHE_BODIES.get(entry.objectKey);
|
|
438
|
+
if (!object) return null;
|
|
439
|
+
const status = metadataInteger(object.customMetadata?.status);
|
|
440
|
+
const createdAt = metadataInteger(object.customMetadata?.createdAt);
|
|
441
|
+
const initialAge = metadataInteger(object.customMetadata?.initialAge);
|
|
442
|
+
if (status === void 0 || status < 200 || status > 599 || createdAt === void 0 || initialAge === void 0) {
|
|
443
|
+
await object.body.cancel();
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
let storedEntry = entry;
|
|
447
|
+
let storedBody = object.body;
|
|
448
|
+
const responseMetadataBytes = metadataInteger(object.customMetadata?.responseMetadataBytes);
|
|
449
|
+
if (responseMetadataBytes !== void 0) try {
|
|
450
|
+
const { prefix, remainder } = await readBodyPrefix(storedBody, responseMetadataBytes);
|
|
451
|
+
const responseMetadata = metadataJson(new TextDecoder().decode(prefix));
|
|
452
|
+
if (typeof responseMetadata !== "object" || responseMetadata === null || !("statusText" in responseMetadata) || typeof responseMetadata.statusText !== "string" || !("responseHeaders" in responseMetadata) || !isHeaderEntries(responseMetadata.responseHeaders)) {
|
|
453
|
+
await remainder.cancel();
|
|
454
|
+
return null;
|
|
455
|
+
}
|
|
456
|
+
storedEntry = {
|
|
457
|
+
...entry,
|
|
458
|
+
statusText: responseMetadata.statusText,
|
|
459
|
+
responseHeaders: responseMetadata.responseHeaders
|
|
460
|
+
};
|
|
461
|
+
storedBody = remainder;
|
|
462
|
+
} catch {
|
|
463
|
+
await storedBody.cancel().catch(() => {});
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
const body = NULL_BODY_STATUSES.has(status) ? null : storedBody;
|
|
467
|
+
if (!body) await storedBody.cancel();
|
|
468
|
+
return this.createStoredResponse(storedEntry, body, status, createdAt, initialAge, now);
|
|
469
|
+
}
|
|
470
|
+
async storeResponse(metadata, request, response, revalidator, reservation, cacheTags = cacheTagsFromResponse(response), expectedR2Etag) {
|
|
173
471
|
const cacheKey = reservation ?? await this.deriveCacheKey(request);
|
|
174
|
-
const write = reservation ?? await this.reserveWrite(metadata, cacheKey
|
|
175
|
-
const { keyHash, objectKey, revision } = write;
|
|
472
|
+
const write = reservation ?? await this.reserveWrite(metadata, cacheKey, cacheTags);
|
|
473
|
+
const { keyHash, objectKey: reservationObjectKey, revision } = write;
|
|
176
474
|
let publication;
|
|
475
|
+
let body;
|
|
476
|
+
let policy;
|
|
177
477
|
try {
|
|
178
478
|
const now = Date.now();
|
|
179
|
-
|
|
479
|
+
policy = deriveCachePolicy(response.headers, now);
|
|
180
480
|
const responseHeaders = [...response.headers].filter(([name]) => {
|
|
181
481
|
const lower = name.toLowerCase();
|
|
182
|
-
return lower !== "age" && lower !== "cf-cache-status" && lower !== "content-length";
|
|
482
|
+
return lower !== "age" && lower !== "cache-tag" && lower !== "cf-cache-status" && lower !== "content-length";
|
|
183
483
|
});
|
|
184
484
|
const candidate = {
|
|
185
485
|
fenceTags: [.../* @__PURE__ */ new Set([...write.fenceTags, ...cacheTags])],
|
|
186
|
-
objectKey,
|
|
486
|
+
objectKey: this.r2ObjectKey(keyHash),
|
|
187
487
|
statusText: response.statusText,
|
|
188
488
|
responseHeaders,
|
|
189
489
|
freshUntil: policy.freshUntil,
|
|
@@ -191,30 +491,56 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
191
491
|
revalidator: revalidator ?? null,
|
|
192
492
|
cacheTags
|
|
193
493
|
};
|
|
194
|
-
|
|
195
|
-
await
|
|
196
|
-
status: String(response.status),
|
|
197
|
-
createdAt: String(policy.createdAt),
|
|
198
|
-
initialAge: String(policy.initialAge)
|
|
199
|
-
} });
|
|
200
|
-
publication = await metadata.publish(keyHash, revision, candidate, write.claimId);
|
|
494
|
+
body = response.body ? await response.arrayBuffer() : /* @__PURE__ */ new ArrayBuffer(0);
|
|
495
|
+
publication = await metadata.publish(keyHash, revision, candidate, write.claimId, reservationObjectKey);
|
|
201
496
|
} catch (error) {
|
|
202
497
|
await this.releaseFailedWrite(metadata, write);
|
|
203
498
|
throw error;
|
|
204
499
|
}
|
|
205
500
|
if (!publication.published) return {
|
|
501
|
+
edgePurgeRequired: false,
|
|
206
502
|
published: false,
|
|
207
|
-
entry: publication.entry
|
|
503
|
+
entry: this.readableEntry(publication.entry)
|
|
208
504
|
};
|
|
505
|
+
let stored = false;
|
|
506
|
+
if (publication.entry) try {
|
|
507
|
+
stored = await this.writeR2Response(publication.entry, body, response.status, policy.createdAt, policy.initialAge, expectedR2Etag !== void 0 ? expectedR2Etag : write.r2ObjectAbsent ? null : void 0);
|
|
508
|
+
} catch (error) {
|
|
509
|
+
const reconciliation = await metadata.invalidatePublishedRevision(publication.entry.keyHash, publication.entry.activeRevision).catch((reconciliationError) => ({
|
|
510
|
+
failures: [reconciliationError instanceof Error ? reconciliationError.message : String(reconciliationError)],
|
|
511
|
+
pending: [],
|
|
512
|
+
purged: []
|
|
513
|
+
}));
|
|
514
|
+
const reconciliationFailures = reconciliation.failures.map((failure) => new Error(failure));
|
|
515
|
+
if (reconciliation.purged.length) try {
|
|
516
|
+
await this.purgeEdgeCacheByTags(reconciliation.purged.map(purgeTagForEntry));
|
|
517
|
+
await metadata.markTombstonesEdgePurged(reconciliation.purged);
|
|
518
|
+
} catch (reconciliationError) {
|
|
519
|
+
reconciliationFailures.push(reconciliationError instanceof Error ? reconciliationError : new Error(String(reconciliationError)));
|
|
520
|
+
}
|
|
521
|
+
if (reconciliationFailures.length) {
|
|
522
|
+
const failure = new AggregateError([error, ...reconciliationFailures], "R2 response publication and reconciliation failed");
|
|
523
|
+
if (reconciliation.pending.length) throw new R2PublicationError(failure);
|
|
524
|
+
throw failure;
|
|
525
|
+
}
|
|
526
|
+
if (reconciliation.pending.length) throw new R2PublicationError(error);
|
|
527
|
+
throw error;
|
|
528
|
+
}
|
|
529
|
+
const entry = this.readableEntry(publication.entry);
|
|
209
530
|
return {
|
|
531
|
+
edgePurgeRequired: publication.edgePurgeRequired,
|
|
210
532
|
published: true,
|
|
211
|
-
entry
|
|
533
|
+
entry,
|
|
534
|
+
...stored && entry ? { response: this.createStoredResponse(entry, NULL_BODY_STATUSES.has(response.status) ? null : body, response.status, policy.createdAt, policy.initialAge) } : {}
|
|
212
535
|
};
|
|
213
536
|
}
|
|
214
|
-
async regenerateEntry(metadata, entry, reason, reservation) {
|
|
537
|
+
async regenerateEntry(metadata, entry, reason, reservation, expectedR2Etag) {
|
|
215
538
|
if (!entry.revalidator) throw new Error("Cache entry has no configured revalidator");
|
|
216
539
|
const cacheRequest = new Request(`https://runtime-cache.invalid${entry.cacheKey}`);
|
|
217
|
-
const writeReservation = reservation ?? await this.reserveWrite(metadata,
|
|
540
|
+
const writeReservation = reservation ?? await this.reserveWrite(metadata, {
|
|
541
|
+
cacheKey: entry.cacheKey,
|
|
542
|
+
keyHash: entry.keyHash
|
|
543
|
+
}, entry.cacheTags);
|
|
218
544
|
let response;
|
|
219
545
|
try {
|
|
220
546
|
const origin = this.ctx.props?.revalidator ?? Reflect.get(this.ctx.exports, "ResponseStoreRevalidator");
|
|
@@ -229,21 +555,20 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
229
555
|
await this.releaseFailedWrite(metadata, writeReservation);
|
|
230
556
|
throw error;
|
|
231
557
|
}
|
|
232
|
-
return this.storeResponse(metadata, cacheRequest, response, entry.revalidator, writeReservation);
|
|
558
|
+
return this.storeResponse(metadata, cacheRequest, response, entry.revalidator, writeReservation, void 0, expectedR2Etag);
|
|
233
559
|
}
|
|
234
|
-
async revalidateEntryInBackground(metadata, entry) {
|
|
235
|
-
if (!entry.revalidator) return;
|
|
560
|
+
async revalidateEntryInBackground(metadata, entry, expectedR2Etag) {
|
|
236
561
|
const claim = await metadata.claimRevalidation(entry.keyHash, entry.activeRevision, entry.cacheKey, this.objectKeyPrefix(entry.keyHash), Date.now(), BACKGROUND_REVALIDATION_LEASE_MS);
|
|
237
562
|
if (!claim) return;
|
|
238
563
|
try {
|
|
239
|
-
await this.regenerateEntry(metadata, entry, "swr", {
|
|
240
|
-
cacheKey: entry.cacheKey,
|
|
564
|
+
await this.regenerateEntry(metadata, claim.entry, "swr", {
|
|
565
|
+
cacheKey: claim.entry.cacheKey,
|
|
241
566
|
claimId: claim.claimId,
|
|
242
|
-
fenceTags: entry.cacheTags,
|
|
243
|
-
keyHash: entry.keyHash,
|
|
567
|
+
fenceTags: claim.entry.cacheTags,
|
|
568
|
+
keyHash: claim.entry.keyHash,
|
|
244
569
|
objectKey: claim.objectKey,
|
|
245
570
|
revision: claim.revision
|
|
246
|
-
});
|
|
571
|
+
}, expectedR2Etag);
|
|
247
572
|
} catch (error) {
|
|
248
573
|
console.error(JSON.stringify({
|
|
249
574
|
message: "Workers Response Store SWR regeneration failed",
|
|
@@ -253,61 +578,97 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
253
578
|
}
|
|
254
579
|
}
|
|
255
580
|
async fetch(request) {
|
|
256
|
-
const
|
|
257
|
-
const
|
|
258
|
-
const
|
|
581
|
+
const cacheKey = await this.deriveCacheKey(request);
|
|
582
|
+
const { keyHash } = cacheKey;
|
|
583
|
+
const r2Read = await this.readR2MetadataCached(cacheKey);
|
|
584
|
+
const entry = r2Read.entry;
|
|
585
|
+
const expectedR2Etag = r2Read.object?.etag;
|
|
259
586
|
if (!entry) return new Response("Workers Response Store miss", {
|
|
260
587
|
status: 404,
|
|
261
588
|
headers: MISS_HEADERS
|
|
262
589
|
});
|
|
263
590
|
const now = Date.now();
|
|
264
591
|
if (now < entry.swrUntil) {
|
|
265
|
-
const stored = await this.readStoredResponse(entry, now);
|
|
592
|
+
const stored = await this.readStoredResponse(entry, now, r2Read?.object);
|
|
266
593
|
if (stored) {
|
|
267
594
|
if (now < entry.freshUntil) return stored;
|
|
268
|
-
this.ctx.waitUntil(
|
|
595
|
+
this.ctx.waitUntil(Promise.resolve().then(async () => {
|
|
596
|
+
const metadata = this.getMetadata(keyHash);
|
|
597
|
+
await this.revalidateEntryInBackground(metadata, entry, expectedR2Etag);
|
|
598
|
+
}));
|
|
269
599
|
return stored;
|
|
270
600
|
}
|
|
271
601
|
}
|
|
272
|
-
|
|
602
|
+
await r2Read?.object?.body.cancel().catch(() => {});
|
|
603
|
+
const metadata = this.getMetadata(keyHash);
|
|
604
|
+
const regeneration = await metadata.reserveRegeneration(keyHash, cacheKey.cacheKey, this.objectKeyPrefix(keyHash), now);
|
|
605
|
+
if (!regeneration) return new Response("Workers Response Store miss", {
|
|
606
|
+
status: 404,
|
|
607
|
+
headers: MISS_HEADERS
|
|
608
|
+
});
|
|
609
|
+
const regenerated = await this.regenerateEntry(metadata, regeneration.entry, now >= entry.swrUntil ? "expired" : "missing", regeneration.reservation ? {
|
|
610
|
+
...cacheKey,
|
|
611
|
+
fenceTags: regeneration.entry.cacheTags,
|
|
612
|
+
...regeneration.reservation
|
|
613
|
+
} : void 0, expectedR2Etag);
|
|
273
614
|
if (!regenerated.entry) throw new Error("Regeneration was superseded and no active entry remains");
|
|
274
|
-
|
|
615
|
+
let response = regenerated.response;
|
|
616
|
+
if (!response) {
|
|
617
|
+
const winner = await this.readR2Metadata(cacheKey);
|
|
618
|
+
if (winner.entry) response = await this.readStoredResponse(winner.entry, Date.now(), winner.object) ?? void 0;
|
|
619
|
+
}
|
|
275
620
|
if (!response) throw new Error("The committed cache body is unavailable");
|
|
276
621
|
return response;
|
|
277
622
|
}
|
|
278
623
|
getTagExpiration(tags) {
|
|
279
|
-
return this.
|
|
624
|
+
return this.getTagMetadata(tags).getTagExpiration(tags);
|
|
280
625
|
}
|
|
281
626
|
async put(request, response, options = {}) {
|
|
282
|
-
const
|
|
283
|
-
const {
|
|
627
|
+
const cacheKey = await this.deriveCacheKey(request);
|
|
628
|
+
const { keyHash } = cacheKey;
|
|
629
|
+
const metadata = this.getMetadata(keyHash);
|
|
284
630
|
const cacheTags = cacheTagsFromResponse(response);
|
|
285
|
-
const pendingPutKey = `${this.getVersionId()}:${keyHash}:${Boolean(options.purgeExisting)}`;
|
|
631
|
+
const pendingPutKey = `${this.getVersionId()}:${this.shardCount}:${keyHash}:${Boolean(options.purgeExisting)}`;
|
|
286
632
|
let reservation;
|
|
287
633
|
if (options.coalesce) for (;;) {
|
|
288
634
|
const pending = pendingPuts.get(pendingPutKey);
|
|
289
635
|
if (!pending) break;
|
|
290
|
-
reservation ??= await this.reserveWrite(metadata,
|
|
636
|
+
reservation ??= await this.reserveWrite(metadata, cacheKey, cacheTags);
|
|
291
637
|
let result;
|
|
292
638
|
try {
|
|
293
639
|
result = await pending;
|
|
294
|
-
} catch {
|
|
640
|
+
} catch (error) {
|
|
641
|
+
if (error instanceof R2PublicationError && reservation) {
|
|
642
|
+
const replacement = await metadata.replaceFailedWrite(keyHash, cacheKey.cacheKey, this.objectKeyPrefix(keyHash), reservation.objectKey, cacheTags, Date.now());
|
|
643
|
+
reservation = replacement ? {
|
|
644
|
+
...cacheKey,
|
|
645
|
+
fenceTags: cacheTags,
|
|
646
|
+
...replacement
|
|
647
|
+
} : void 0;
|
|
648
|
+
}
|
|
295
649
|
if (pendingPuts.get(pendingPutKey) === pending) pendingPuts.delete(pendingPutKey);
|
|
650
|
+
if (error instanceof R2PublicationError && !reservation) {
|
|
651
|
+
response.body?.cancel().catch(() => {});
|
|
652
|
+
return {
|
|
653
|
+
backingStoreUpdated: false,
|
|
654
|
+
edgePurgeAccepted: false
|
|
655
|
+
};
|
|
656
|
+
}
|
|
296
657
|
continue;
|
|
297
658
|
}
|
|
298
|
-
if (result
|
|
659
|
+
if (result?.published && result.entry) {
|
|
299
660
|
const objectKey = reservation.objectKey;
|
|
300
661
|
await metadata.finishPendingObjects([objectKey]).catch((error) => this.logCleanupFailure(objectKey, error));
|
|
301
662
|
response.body?.cancel().catch(() => {});
|
|
302
663
|
return {
|
|
303
664
|
backingStoreUpdated: true,
|
|
304
|
-
edgePurgeAccepted: options.purgeExisting ? await this.purgeEdgeCacheByTags([purgeTagForEntry(result.entry)]) : true
|
|
665
|
+
edgePurgeAccepted: options.purgeExisting && result.edgePurgeRequired ? await this.purgeEdgeCacheByTags([purgeTagForEntry(result.entry)]) : true
|
|
305
666
|
};
|
|
306
667
|
}
|
|
307
668
|
if (pendingPuts.get(pendingPutKey) === pending) pendingPuts.delete(pendingPutKey);
|
|
308
669
|
}
|
|
309
670
|
const write = (async () => {
|
|
310
|
-
reservation ??= await this.reserveWrite(metadata,
|
|
671
|
+
reservation ??= await this.reserveWrite(metadata, cacheKey, cacheTags);
|
|
311
672
|
return this.storeResponse(metadata, request, response, options.revalidator, reservation, cacheTags);
|
|
312
673
|
})();
|
|
313
674
|
if (options.coalesce) pendingPuts.set(pendingPutKey, write);
|
|
@@ -319,7 +680,7 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
319
680
|
};
|
|
320
681
|
return {
|
|
321
682
|
backingStoreUpdated: true,
|
|
322
|
-
edgePurgeAccepted: options.purgeExisting ? await this.purgeEdgeCacheByTags([purgeTagForEntry(result.entry)]) : true
|
|
683
|
+
edgePurgeAccepted: options.purgeExisting && result.edgePurgeRequired ? await this.purgeEdgeCacheByTags([purgeTagForEntry(result.entry)]) : true
|
|
323
684
|
};
|
|
324
685
|
} finally {
|
|
325
686
|
if (options.coalesce && pendingPuts.get(pendingPutKey) === write) pendingPuts.delete(pendingPutKey);
|
|
@@ -327,23 +688,35 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
327
688
|
}
|
|
328
689
|
async refresh(options) {
|
|
329
690
|
if (!options.tags?.length && !options.pathPrefixes?.length) throw new TypeError("refresh() requires tags or pathPrefixes");
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
691
|
+
const reserved = await Promise.allSettled(this.getMetadataShards().map(async (metadata) => ({
|
|
692
|
+
candidates: await metadata.findRefreshCandidates(options, "reservation"),
|
|
693
|
+
metadata
|
|
694
|
+
})));
|
|
695
|
+
const failures = reserved.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
696
|
+
const candidates = reserved.flatMap((result) => result.status === "fulfilled" ? [result.value] : []).flatMap(({ candidates, metadata }) => candidates.map((entry) => ({
|
|
697
|
+
entry,
|
|
698
|
+
metadata
|
|
699
|
+
})));
|
|
700
|
+
if (candidates.length === 0) {
|
|
701
|
+
if (failures.length) throw new AggregateError(failures, "One or more cache entries failed to refresh");
|
|
702
|
+
return {
|
|
703
|
+
backingStoreUpdated: false,
|
|
704
|
+
edgePurgeAccepted: false
|
|
705
|
+
};
|
|
706
|
+
}
|
|
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
|
+
});
|
|
343
717
|
return result.published ? result.entry : null;
|
|
344
|
-
})
|
|
718
|
+
});
|
|
345
719
|
const refreshed = [];
|
|
346
|
-
const failures = [];
|
|
347
720
|
for (const result of settled) if (result.status === "rejected") failures.push(result.reason);
|
|
348
721
|
else if (result.value) refreshed.push(result.value);
|
|
349
722
|
const edgePurgeAccepted = refreshed.length ? await this.purgeEdgeCacheByTags(refreshed.map((entry) => purgeTagForEntry(entry))) : false;
|
|
@@ -355,15 +728,47 @@ var ResponseStoreBinding = class extends WorkerEntrypoint {
|
|
|
355
728
|
}
|
|
356
729
|
async purge(options) {
|
|
357
730
|
if (!options.purgeEverything && !options.tags?.length && !options.pathPrefixes?.length) throw new TypeError("purge() requires tags, pathPrefixes, or purgeEverything");
|
|
358
|
-
const
|
|
731
|
+
const invalidatedAt = Date.now();
|
|
732
|
+
const settled = await Promise.allSettled(this.getMetadataShards().map((metadata) => metadata.purgeMatching(options, invalidatedAt)));
|
|
733
|
+
const failures = settled.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
734
|
+
const reservations = settled.flatMap((result, index) => result.status === "fulfilled" ? [{
|
|
735
|
+
metadata: this.getMetadataShard(index),
|
|
736
|
+
reservation: result.value
|
|
737
|
+
}] : []);
|
|
738
|
+
for (const { metadata, reservation } of reservations) {
|
|
739
|
+
const batchCount = Math.ceil(reservation.pendingTombstones / PURGE_TOMBSTONE_BATCH_SIZE);
|
|
740
|
+
let cursor;
|
|
741
|
+
for (let batch = 0; batch < batchCount; batch++) try {
|
|
742
|
+
const drained = await metadata.drainPendingTombstones(PURGE_TOMBSTONE_BATCH_SIZE, void 0, cursor, reservation.tombstoneSequence);
|
|
743
|
+
cursor = drained.cursor;
|
|
744
|
+
failures.push(...drained.failures.map((failure) => new Error(failure)));
|
|
745
|
+
} catch (error) {
|
|
746
|
+
failures.push(error);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
359
749
|
let edgePurgeAccepted = true;
|
|
360
|
-
if (options.purgeEverything
|
|
361
|
-
|
|
750
|
+
if (options.purgeEverything && failures.length === 0) try {
|
|
751
|
+
edgePurgeAccepted = await this.purgeEdgeCache({ purgeEverything: true });
|
|
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
|
+
}
|
|
756
|
+
} catch (error) {
|
|
757
|
+
edgePurgeAccepted = false;
|
|
758
|
+
failures.push(error);
|
|
759
|
+
}
|
|
760
|
+
else {
|
|
761
|
+
if (options.purgeEverything) edgePurgeAccepted = false;
|
|
762
|
+
const acknowledged = await Promise.allSettled(reservations.filter(({ reservation }) => reservation.pendingTombstones > 0).map(({ metadata, reservation }) => this.purgePendingEdgeEntries(metadata, reservation.tombstoneSequence)));
|
|
763
|
+
for (const result of acknowledged) if (result.status === "rejected") failures.push(result.reason);
|
|
764
|
+
else if (!result.value) edgePurgeAccepted = false;
|
|
765
|
+
}
|
|
766
|
+
if (failures.length) throw new AggregateError(failures, "One or more metadata shards failed to purge");
|
|
362
767
|
return {
|
|
363
|
-
backingStoreUpdated:
|
|
768
|
+
backingStoreUpdated: reservations.some(({ reservation }) => reservation.backingStoreUpdated || reservation.pendingTombstones > 0),
|
|
364
769
|
edgePurgeAccepted
|
|
365
770
|
};
|
|
366
771
|
}
|
|
367
772
|
};
|
|
368
773
|
//#endregion
|
|
369
|
-
export { ResponseStoreBinding, ResponseStoreService, getWorkersResponseStore };
|
|
774
|
+
export { ResponseStoreBinding, ResponseStoreService, getWorkersResponseStore, validateResponseStoreLocationHint, validateResponseStoreShards };
|