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