@decocms/blocks 7.85.4 → 7.86.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/package.json +1 -1
- package/src/cms/blockSource.ts +19 -1
- package/src/cms/index.ts +2 -0
- package/src/sdk/abTesting.test.ts +21 -0
- package/src/sdk/abTesting.ts +33 -1
- package/src/sdk/cacheStorage.test.ts +22 -0
- package/src/sdk/responseCache.ts +24 -3
package/package.json
CHANGED
package/src/cms/blockSource.ts
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* model needs.
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
|
-
import { djb2Hex } from "../sdk/djb2";
|
|
23
|
+
import { djb2, djb2Hex } from "../sdk/djb2";
|
|
24
24
|
|
|
25
25
|
/** A fully-loaded decofile snapshot: the blocks map and its revision hash. */
|
|
26
26
|
export interface BlockSnapshot {
|
|
@@ -124,6 +124,7 @@ export interface KVNamespace {
|
|
|
124
124
|
// index:live pointer to the currently-live <id> (set post-deploy)
|
|
125
125
|
// index:deployments JSON [{id, ts}] (newest last) — GC bookkeeping
|
|
126
126
|
// redirect:<id>:<path> ONE key per exact redirect (see below)
|
|
127
|
+
// index:redirects:<id> sorted hashes of the paths that have one
|
|
127
128
|
// ---------------------------------------------------------------------------
|
|
128
129
|
|
|
129
130
|
/** KV key holding the full decofile JSON for deployment `id`. */
|
|
@@ -175,6 +176,23 @@ export function redirectKey(id: string, path: string): string {
|
|
|
175
176
|
return `${redirectPrefix(id)}${path}`;
|
|
176
177
|
}
|
|
177
178
|
|
|
179
|
+
/**
|
|
180
|
+
* KV key listing which paths of deployment `id` have an exact redirect: a JSON
|
|
181
|
+
* array of `redirectPathHash(path)`, sorted ascending. The worker loads it once
|
|
182
|
+
* per isolate and skips the `redirect:<id>:<path>` read for every path not in
|
|
183
|
+
* it, which is nearly all of them. Written by the sync after the rules; a
|
|
184
|
+
* deployment synced before this key existed has none and is read path by path.
|
|
185
|
+
*/
|
|
186
|
+
export function redirectIndexKey(id: string): string {
|
|
187
|
+
return `index:redirects:${id}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** The hash `redirectIndexKey` stores for a normalized path. A collision only
|
|
191
|
+
* costs one KV read that finds nothing. */
|
|
192
|
+
export function redirectPathHash(path: string): number {
|
|
193
|
+
return djb2(path);
|
|
194
|
+
}
|
|
195
|
+
|
|
178
196
|
/**
|
|
179
197
|
* One rule stored at a `redirect:<id>:<path>` key. The value is one rule, or an
|
|
180
198
|
* array of them when several sources fold to the same `path` (see
|
package/src/cms/index.ts
CHANGED
|
@@ -296,6 +296,27 @@ describe("withABTesting — outer-catch defense", () => {
|
|
|
296
296
|
// Ratio-fingerprinted cookie
|
|
297
297
|
// ---------------------------------------------------------------------------
|
|
298
298
|
|
|
299
|
+
describe("withABTesting — site config memo", () => {
|
|
300
|
+
afterEach(() => vi.useRealTimers());
|
|
301
|
+
|
|
302
|
+
it("reads the hostname's config once per isolate, a missing one included, until the TTL", async () => {
|
|
303
|
+
const handler: WorkerHandler = { fetch: vi.fn(async () => new Response("ok")) };
|
|
304
|
+
const kv = makeFakeKv(null);
|
|
305
|
+
const env = { KV: kv } as unknown as Record<string, unknown>;
|
|
306
|
+
const wrapped = withABTesting(handler, { kvBinding: "KV" });
|
|
307
|
+
|
|
308
|
+
await wrapped.fetch(new Request(`https://${REAL_HOST}/a`), env, makeCtx());
|
|
309
|
+
await wrapped.fetch(new Request(`https://${REAL_HOST}/b`), env, makeCtx());
|
|
310
|
+
expect(kv.get).toHaveBeenCalledTimes(1);
|
|
311
|
+
expect(handler.fetch).toHaveBeenCalledTimes(2);
|
|
312
|
+
|
|
313
|
+
vi.useFakeTimers();
|
|
314
|
+
vi.setSystemTime(Date.now() + 61_000);
|
|
315
|
+
await wrapped.fetch(new Request(`https://${REAL_HOST}/c`), env, makeCtx());
|
|
316
|
+
expect(kv.get).toHaveBeenCalledTimes(2);
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
299
320
|
describe("getStableBucket — ratio fingerprint", () => {
|
|
300
321
|
it("honors the cookie when its ratio fingerprint matches the current ratio", () => {
|
|
301
322
|
const request = new Request(`https://${REAL_HOST}/`, {
|
package/src/sdk/abTesting.ts
CHANGED
|
@@ -440,6 +440,38 @@ export async function proxyToFallback(
|
|
|
440
440
|
* When no KV binding is available or no config exists for the hostname,
|
|
441
441
|
* all traffic goes directly to the inner handler (no A/B).
|
|
442
442
|
*/
|
|
443
|
+
/**
|
|
444
|
+
* How long a hostname's config (or its absence) is trusted inside one isolate.
|
|
445
|
+
* Without it every request pays a KV read, and a host with no config pays a
|
|
446
|
+
* miss on every one, for a value that changes
|
|
447
|
+
* when someone edits an A/B ratio. The TTL is that edit's propagation delay.
|
|
448
|
+
*/
|
|
449
|
+
const SITE_CONFIG_TTL_MS = 60_000;
|
|
450
|
+
|
|
451
|
+
// Keyed by the binding, which is one object per isolate, so two namespaces
|
|
452
|
+
// (and two tests) never share entries.
|
|
453
|
+
const siteConfigs = new WeakMap<
|
|
454
|
+
KVNamespace,
|
|
455
|
+
Map<string, { value: SiteConfig | null; expiresAt: number }>
|
|
456
|
+
>();
|
|
457
|
+
|
|
458
|
+
async function readSiteConfig(
|
|
459
|
+
kv: KVNamespace,
|
|
460
|
+
hostname: string,
|
|
461
|
+
): Promise<SiteConfig | null> {
|
|
462
|
+
let byHost = siteConfigs.get(kv);
|
|
463
|
+
if (!byHost) {
|
|
464
|
+
byHost = new Map();
|
|
465
|
+
siteConfigs.set(kv, byHost);
|
|
466
|
+
}
|
|
467
|
+
const cached = byHost.get(hostname);
|
|
468
|
+
if (cached && Date.now() < cached.expiresAt) return cached.value;
|
|
469
|
+
// A failed read throws as before and is not remembered.
|
|
470
|
+
const value = await kv.get<SiteConfig>(hostname, "json");
|
|
471
|
+
byHost.set(hostname, { value, expiresAt: Date.now() + SITE_CONFIG_TTL_MS });
|
|
472
|
+
return value;
|
|
473
|
+
}
|
|
474
|
+
|
|
443
475
|
export function withABTesting(
|
|
444
476
|
handler: WorkerHandler,
|
|
445
477
|
config: ABTestConfig = {},
|
|
@@ -472,7 +504,7 @@ export function withABTesting(
|
|
|
472
504
|
return handler.fetch(request, env, ctx);
|
|
473
505
|
}
|
|
474
506
|
|
|
475
|
-
const siteConfig = await kv
|
|
507
|
+
const siteConfig = await readSiteConfig(kv, url.hostname);
|
|
476
508
|
if (!siteConfig?.fallbackOrigin) {
|
|
477
509
|
return handler.fetch(request, env, ctx);
|
|
478
510
|
}
|
|
@@ -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> {
|