@decocms/blocks 7.85.4 → 7.86.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/package.json +1 -1
- package/src/sdk/cacheStorage.test.ts +22 -0
- package/src/sdk/responseCache.ts +24 -3
package/package.json
CHANGED
|
@@ -240,6 +240,28 @@ it("refuses to serialize private HTTP responses or cookies", async () => {
|
|
|
240
240
|
expect(await cache.match(req)).toBeUndefined();
|
|
241
241
|
});
|
|
242
242
|
|
|
243
|
+
it("reports what each response put did, without changing what it stores", async () => {
|
|
244
|
+
const req = new Request("https://site.test/");
|
|
245
|
+
const publicHeaders = { "cache-control": "public, max-age=60" };
|
|
246
|
+
const cache = createResponseCache(createMemoryCacheStorage(), "site");
|
|
247
|
+
expect(await cache.put(req, new Response("ok", { headers: publicHeaders }))).toBe("stored");
|
|
248
|
+
expect(
|
|
249
|
+
await cache.put(
|
|
250
|
+
req,
|
|
251
|
+
new Response("p", { headers: { "cache-control": "private, max-age=60" } }),
|
|
252
|
+
),
|
|
253
|
+
).toBe("skipped:uncacheable");
|
|
254
|
+
const huge = new Uint8Array(8 * 1024 * 1024 + 1);
|
|
255
|
+
expect(await cache.put(req, new Response(huge, { headers: publicHeaders }))).toBe(
|
|
256
|
+
"skipped:too-large",
|
|
257
|
+
);
|
|
258
|
+
const failure = vi.fn(async () => {
|
|
259
|
+
throw new Error("storage down");
|
|
260
|
+
});
|
|
261
|
+
const broken = createResponseCache({ get: failure, set: failure, delete: failure }, "site");
|
|
262
|
+
expect(await broken.put(req, new Response("ok", { headers: publicHeaders }))).toBe("error");
|
|
263
|
+
});
|
|
264
|
+
|
|
243
265
|
it("bounds SWR separately from stale-if-error and stops serving at the final TTL", async () => {
|
|
244
266
|
vi.useFakeTimers();
|
|
245
267
|
const storage = createKVCacheStorage(fakeKV());
|
package/src/sdk/responseCache.ts
CHANGED
|
@@ -8,6 +8,25 @@ interface StoredResponse {
|
|
|
8
8
|
body: string;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* What `put` did. Reported for observability only (the edge-cache telemetry in
|
|
13
|
+
* `@decocms/tanstack`); every outcome but `stored` used to be silent. The
|
|
14
|
+
* write semantics are the same as before: nothing here throws.
|
|
15
|
+
*
|
|
16
|
+
* - `skipped:uncacheable` — no `max-age`, a `private`/`no-store`/`no-cache`
|
|
17
|
+
* `Cache-Control`, or a `Set-Cookie` on the copy to store.
|
|
18
|
+
* - `skipped:too-large` — the body passed the 8 MiB buffering bound.
|
|
19
|
+
* - `error` — the storage adapter threw.
|
|
20
|
+
*
|
|
21
|
+
* `stored` means the adapter accepted the write; an adapter may still drop it
|
|
22
|
+
* on its own limits (an already-expired entry, the KV value size).
|
|
23
|
+
*/
|
|
24
|
+
export type ResponseCachePutResult =
|
|
25
|
+
| "stored"
|
|
26
|
+
| "skipped:uncacheable"
|
|
27
|
+
| "skipped:too-large"
|
|
28
|
+
| "error";
|
|
29
|
+
|
|
11
30
|
/** HTTP serialization belongs to the cache consumer, independent of its storage adapter. */
|
|
12
31
|
export function createResponseCache(storage: CacheStorage, scope: string) {
|
|
13
32
|
const keyFor = (request: Request) => JSON.stringify([scope, "responses", request.url]);
|
|
@@ -29,11 +48,11 @@ export function createResponseCache(storage: CacheStorage, scope: string) {
|
|
|
29
48
|
return undefined;
|
|
30
49
|
}
|
|
31
50
|
},
|
|
32
|
-
async put(request: Request, response: Response): Promise<
|
|
51
|
+
async put(request: Request, response: Response): Promise<ResponseCachePutResult> {
|
|
33
52
|
const cc = response.headers.get("Cache-Control") ?? "";
|
|
34
53
|
const ttl = Number(/(?:^|[,\s])max-age=(\d+)/i.exec(cc)?.[1] ?? 0);
|
|
35
54
|
if (!ttl || /(?:private|no-store|no-cache)/i.test(cc) || response.headers.has("set-cookie"))
|
|
36
|
-
return;
|
|
55
|
+
return "skipped:uncacheable";
|
|
37
56
|
const expiresAt = Date.now() + ttl * 1000;
|
|
38
57
|
// Bound buffering before serialization; an oversized page can still stream to its caller.
|
|
39
58
|
const chunks: Uint8Array[] = [];
|
|
@@ -46,7 +65,7 @@ export function createResponseCache(storage: CacheStorage, scope: string) {
|
|
|
46
65
|
size += value.byteLength;
|
|
47
66
|
if (size > 8 * 1024 * 1024) {
|
|
48
67
|
void reader.cancel().catch(() => {});
|
|
49
|
-
return;
|
|
68
|
+
return "skipped:too-large";
|
|
50
69
|
}
|
|
51
70
|
chunks.push(value);
|
|
52
71
|
}
|
|
@@ -69,8 +88,10 @@ export function createResponseCache(storage: CacheStorage, scope: string) {
|
|
|
69
88
|
};
|
|
70
89
|
try {
|
|
71
90
|
await storage.set(keyFor(request), JSON.stringify(value), expiresAt);
|
|
91
|
+
return "stored";
|
|
72
92
|
} catch {
|
|
73
93
|
/* Cache writes must not fail the response or its waitUntil task. */
|
|
94
|
+
return "error";
|
|
74
95
|
}
|
|
75
96
|
},
|
|
76
97
|
async delete(request: Request): Promise<boolean> {
|