@decocms/blocks 7.78.1 → 7.79.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 +28 -0
- package/src/sdk/cacheStorage.ts +11 -1
- package/src/sdk/cachedLoader.ts +56 -16
- package/src/sdk/cachedLoaderCancelledOwner.test.ts +199 -0
- package/src/sdk/fetchCache.test.ts +45 -3
- package/src/sdk/fetchCache.ts +7 -1
- package/src/sdk/inflightTimeout.ts +40 -0
package/package.json
CHANGED
|
@@ -111,6 +111,34 @@ it("hydrates another store and isolates namespace, site and deployment", async (
|
|
|
111
111
|
).toBeUndefined();
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
+
it("data stores key by dataScope and survive a deploy; build stores do not", async () => {
|
|
115
|
+
const storage = createKVCacheStorage(fakeKV());
|
|
116
|
+
const bind = (build: string, fn: () => unknown) =>
|
|
117
|
+
RequestContext.run(new Request("https://site.test/"), () => {
|
|
118
|
+
bindCacheStorage({
|
|
119
|
+
storage,
|
|
120
|
+
scope: `site:${build}`,
|
|
121
|
+
dataScope: "site:data:1",
|
|
122
|
+
waitUntil: (work) => pending.push(work),
|
|
123
|
+
});
|
|
124
|
+
return fn();
|
|
125
|
+
});
|
|
126
|
+
const data = createCacheStore<{ n: number }>("loaders", 200, undefined, undefined, "data");
|
|
127
|
+
const html = createCacheStore<{ n: number }>("responses");
|
|
128
|
+
await bind("build-A", async () => {
|
|
129
|
+
data.set(data.key("item"), { n: 1 }, Date.now() + 60_000);
|
|
130
|
+
html.set(html.key("item"), { n: 2 }, Date.now() + 60_000);
|
|
131
|
+
await flush();
|
|
132
|
+
});
|
|
133
|
+
clearLoaderCache();
|
|
134
|
+
const data2 = createCacheStore<{ n: number }>("loaders", 200, undefined, undefined, "data");
|
|
135
|
+
const html2 = createCacheStore<{ n: number }>("responses");
|
|
136
|
+
expect(await bind("build-B", () => data2.read(data2.key("item")))).toEqual({ n: 1 });
|
|
137
|
+
expect(await bind("build-B", () => html2.read(html2.key("item")))).toBeUndefined();
|
|
138
|
+
// Without dataScope a data store falls back to the build scope.
|
|
139
|
+
expect(await run(storage, "site:build-B", () => data2.read(data2.key("item")))).toBeUndefined();
|
|
140
|
+
});
|
|
141
|
+
|
|
114
142
|
it("does not switch bindings between concurrent requests", async () => {
|
|
115
143
|
const a = createMemoryCacheStorage();
|
|
116
144
|
const b = createMemoryCacheStorage();
|
package/src/sdk/cacheStorage.ts
CHANGED
|
@@ -11,6 +11,12 @@ export interface CacheStorageContext {
|
|
|
11
11
|
storage: CacheStorage | null;
|
|
12
12
|
/** Include site, deployment and content revision. Never use a tenant-wide global binding. */
|
|
13
13
|
scope: string;
|
|
14
|
+
/**
|
|
15
|
+
* Scope for build-independent DATA caches (`createFetchCache`): raw upstream
|
|
16
|
+
* responses, keyed with a site-chosen data version instead of the build hash
|
|
17
|
+
* and CMS revision so they survive deploys and publishes. Defaults to `scope`.
|
|
18
|
+
*/
|
|
19
|
+
dataScope?: string;
|
|
14
20
|
/** Bypass both memory and shared storage for previews or private requests. */
|
|
15
21
|
disabled?: boolean;
|
|
16
22
|
waitUntil?: (work: Promise<unknown>) => void;
|
|
@@ -186,6 +192,8 @@ export function createCacheStore<T>(
|
|
|
186
192
|
maxEntries = 200,
|
|
187
193
|
maxBytes = 4 * 1024 * 1024,
|
|
188
194
|
sizeOf?: (value: T) => number,
|
|
195
|
+
/** `"data"`: key by `dataScope` — only for values that don't depend on the build. */
|
|
196
|
+
kind: "build" | "data" = "build",
|
|
189
197
|
) {
|
|
190
198
|
const local = new Map<string, { value: T; expiresAt: number; bytes: number }>();
|
|
191
199
|
let bytes = 0;
|
|
@@ -225,7 +233,9 @@ export function createCacheStore<T>(
|
|
|
225
233
|
}
|
|
226
234
|
return {
|
|
227
235
|
key(raw: string): string {
|
|
228
|
-
|
|
236
|
+
const context = getCacheStorageContext();
|
|
237
|
+
const scope = (kind === "data" ? context?.dataScope : undefined) ?? context?.scope;
|
|
238
|
+
return JSON.stringify([scope ?? "local", namespace, raw]);
|
|
229
239
|
},
|
|
230
240
|
get,
|
|
231
241
|
async read(key: string): Promise<T | undefined> {
|
package/src/sdk/cachedLoader.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
} from "../middleware/observability";
|
|
21
21
|
import { type CacheProfileName, loaderCacheOptions } from "./cacheHeaders";
|
|
22
22
|
import { cacheBackground, createCacheStore, getCacheStorageContext } from "./cacheStorage";
|
|
23
|
-
import { withInflightTimeout } from "./inflightTimeout";
|
|
23
|
+
import { joinInflight, withInflightTimeout } from "./inflightTimeout";
|
|
24
24
|
import { RequestContext } from "./requestContext";
|
|
25
25
|
|
|
26
26
|
// Build-time constant injected by `decoVitePlugin()` (see @decocms/tanstack's
|
|
@@ -200,6 +200,32 @@ function evictIfNeeded() {
|
|
|
200
200
|
|
|
201
201
|
const inflightRequests = new Map<string, Promise<unknown>>();
|
|
202
202
|
|
|
203
|
+
/** Free a dedup slot only while it still holds `promise` — never a newer flight. */
|
|
204
|
+
function releaseSlot(map: Map<string, Promise<unknown>>, key: string, promise: Promise<unknown>) {
|
|
205
|
+
if (map.get(key) === promise) map.delete(key);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Start shared work: keep it alive past its own request and publish it in the
|
|
210
|
+
* dedup map.
|
|
211
|
+
*
|
|
212
|
+
* On Workers a Promise's subrequests belong to the request that created it.
|
|
213
|
+
* When that client disconnects the runtime cancels its pending I/O, the
|
|
214
|
+
* Promise never settles, and every caller that joined it would wait forever.
|
|
215
|
+
* `cacheBackground` registers it with the owner's `ctx.waitUntil`, so the
|
|
216
|
+
* owner's invocation stays alive until the work settles and the joiners get
|
|
217
|
+
* the answer. Inert outside the worker entry (no `waitUntil` bound).
|
|
218
|
+
*/
|
|
219
|
+
function publishInflight<T>(
|
|
220
|
+
map: Map<string, Promise<unknown>>,
|
|
221
|
+
key: string,
|
|
222
|
+
promise: Promise<T>,
|
|
223
|
+
): Promise<T> {
|
|
224
|
+
cacheBackground(promise);
|
|
225
|
+
map.set(key, promise);
|
|
226
|
+
return promise;
|
|
227
|
+
}
|
|
228
|
+
|
|
203
229
|
function resolveOptions(
|
|
204
230
|
optionsOrProfile: CachedLoaderOptions | CacheProfileName,
|
|
205
231
|
): CachedLoaderOptions {
|
|
@@ -256,8 +282,16 @@ export function createCachedLoader<TProps, TResult>(
|
|
|
256
282
|
|
|
257
283
|
if (policy === "no-store") return loaderFn;
|
|
258
284
|
|
|
259
|
-
|
|
285
|
+
const cachedFn = async (props: TProps): Promise<TResult> => {
|
|
260
286
|
const cacheKey = cache.key(`${BUILD}::${name}::${keyFn(props)}`);
|
|
287
|
+
// Joining another caller's flight is bounded by a timer of THIS caller's
|
|
288
|
+
// request (see `joinInflight`): if the owner's request was cancelled and
|
|
289
|
+
// its Promise will never settle, evict the dead slot and run again.
|
|
290
|
+
const join = (shared: Promise<unknown>): Promise<TResult> =>
|
|
291
|
+
joinInflight(shared as Promise<TResult>, () => {
|
|
292
|
+
releaseSlot(inflightRequests, cacheKey, shared);
|
|
293
|
+
return cachedFn(props);
|
|
294
|
+
});
|
|
261
295
|
const retention =
|
|
262
296
|
maxAge +
|
|
263
297
|
Math.max(staleIfError, policy === "stale-while-revalidate" ? staleWhileRevalidate : 0);
|
|
@@ -270,7 +304,7 @@ export function createCachedLoader<TProps, TResult>(
|
|
|
270
304
|
// Treat in-flight dedup as a cache hit — avoided the origin call.
|
|
271
305
|
recordCacheMetric(true, name, undefined, "cachedLoader");
|
|
272
306
|
const start = performance.now();
|
|
273
|
-
return inflight.then((r) => {
|
|
307
|
+
return join(inflight).then((r) => {
|
|
274
308
|
recordLoaderMetric(name, performance.now() - start, "HIT");
|
|
275
309
|
return r as TResult;
|
|
276
310
|
});
|
|
@@ -296,9 +330,8 @@ export function createCachedLoader<TProps, TResult>(
|
|
|
296
330
|
recordLoaderError(name);
|
|
297
331
|
throw err;
|
|
298
332
|
})
|
|
299
|
-
.finally(() => inflightRequests
|
|
300
|
-
inflightRequests
|
|
301
|
-
return promise;
|
|
333
|
+
.finally(() => releaseSlot(inflightRequests, cacheKey, promise));
|
|
334
|
+
return publishInflight(inflightRequests, cacheKey, promise);
|
|
302
335
|
}
|
|
303
336
|
|
|
304
337
|
let entry = cache.get(cacheKey) as CacheEntry<TResult> | undefined;
|
|
@@ -308,7 +341,7 @@ export function createCachedLoader<TProps, TResult>(
|
|
|
308
341
|
}
|
|
309
342
|
// Another caller may have started the origin fetch while storage was being read.
|
|
310
343
|
const pending = inflightRequests.get(cacheKey);
|
|
311
|
-
if (pending) return pending
|
|
344
|
+
if (pending) return join(pending);
|
|
312
345
|
const now = Date.now();
|
|
313
346
|
const isStale = entry ? now - entry.createdAt > maxAge : true;
|
|
314
347
|
|
|
@@ -424,11 +457,11 @@ export function createCachedLoader<TProps, TResult>(
|
|
|
424
457
|
recordLoaderError(name);
|
|
425
458
|
throw err;
|
|
426
459
|
})
|
|
427
|
-
.finally(() => inflightRequests
|
|
460
|
+
.finally(() => releaseSlot(inflightRequests, cacheKey, promise));
|
|
428
461
|
|
|
429
|
-
inflightRequests
|
|
430
|
-
return promise;
|
|
462
|
+
return publishInflight(inflightRequests, cacheKey, promise);
|
|
431
463
|
};
|
|
464
|
+
return cachedFn;
|
|
432
465
|
}
|
|
433
466
|
|
|
434
467
|
// ---------------------------------------------------------------------------
|
|
@@ -474,7 +507,7 @@ export function createCachedLoaderFromModule<TProps, TResult>(
|
|
|
474
507
|
|
|
475
508
|
const cacheKeyFn = mod.cacheKey;
|
|
476
509
|
|
|
477
|
-
|
|
510
|
+
const dedupedFn = (props: TProps, req?: Request): Promise<TResult> => {
|
|
478
511
|
const request = req ?? RequestContext.current?.request ?? undefined;
|
|
479
512
|
|
|
480
513
|
let keyPart: string | null;
|
|
@@ -491,14 +524,21 @@ export function createCachedLoaderFromModule<TProps, TResult>(
|
|
|
491
524
|
|
|
492
525
|
const key = cache.key(`${BUILD}::${name}::${keyPart}`);
|
|
493
526
|
const existing = moduleInflight.get(key) as Promise<TResult> | undefined;
|
|
494
|
-
if (existing)
|
|
527
|
+
if (existing) {
|
|
528
|
+
// Bounded by this caller's own timer: a flight whose owner request was
|
|
529
|
+
// cancelled never settles — evict it and run again (see `joinInflight`).
|
|
530
|
+
return joinInflight(existing, () => {
|
|
531
|
+
releaseSlot(moduleInflight, key, existing);
|
|
532
|
+
return dedupedFn(props, req);
|
|
533
|
+
});
|
|
534
|
+
}
|
|
495
535
|
|
|
496
|
-
const promise = Promise.resolve(mod.default(props, req)).finally(() => {
|
|
497
|
-
moduleInflight
|
|
536
|
+
const promise: Promise<TResult> = Promise.resolve(mod.default(props, req)).finally(() => {
|
|
537
|
+
releaseSlot(moduleInflight, key, promise);
|
|
498
538
|
});
|
|
499
|
-
moduleInflight
|
|
500
|
-
return promise;
|
|
539
|
+
return publishInflight(moduleInflight, key, promise);
|
|
501
540
|
};
|
|
541
|
+
return dedupedFn;
|
|
502
542
|
}
|
|
503
543
|
|
|
504
544
|
/**
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
clearLoaderCache,
|
|
4
|
+
createCachedLoader,
|
|
5
|
+
createCachedLoaderFromModule,
|
|
6
|
+
type LoaderModule,
|
|
7
|
+
} from "./cachedLoader";
|
|
8
|
+
import { bindCacheStorage } from "./cacheStorage";
|
|
9
|
+
import { DEFAULT_INFLIGHT_TIMEOUT_MS, joinInflight } from "./inflightTimeout";
|
|
10
|
+
import { RequestContext } from "./requestContext";
|
|
11
|
+
|
|
12
|
+
// On Cloudflare Workers, a Promise's subrequests and timers belong to the
|
|
13
|
+
// request that created it. When that client disconnects, the runtime cancels
|
|
14
|
+
// them: the shared Promise left in a dedup map never settles, and the owner's
|
|
15
|
+
// `withInflightTimeout` never fires. These tests reproduce that by never
|
|
16
|
+
// resolving the owner's work and, for `createCachedLoader`, clearing the
|
|
17
|
+
// owner's timers (what the runtime does to a cancelled request).
|
|
18
|
+
|
|
19
|
+
const never = <T>() => new Promise<T>(() => {});
|
|
20
|
+
|
|
21
|
+
/** Run `fn` as a worker request would: inside a RequestContext with `waitUntil` bound. */
|
|
22
|
+
function inRequest<T>(waitUntil: (p: Promise<unknown>) => void, fn: () => T): T {
|
|
23
|
+
return RequestContext.run(new Request("https://store.test/"), () => {
|
|
24
|
+
bindCacheStorage({ storage: null, scope: "test", waitUntil });
|
|
25
|
+
return fn();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
clearLoaderCache();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
clearLoaderCache();
|
|
35
|
+
vi.useRealTimers();
|
|
36
|
+
vi.restoreAllMocks();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("joinInflight", () => {
|
|
40
|
+
it("answers with the shared result when it settles in time", async () => {
|
|
41
|
+
vi.useFakeTimers();
|
|
42
|
+
const onStall = vi.fn(async () => "fresh");
|
|
43
|
+
const joined = joinInflight(Promise.resolve("shared"), onStall);
|
|
44
|
+
await expect(joined).resolves.toBe("shared");
|
|
45
|
+
await vi.advanceTimersByTimeAsync(DEFAULT_INFLIGHT_TIMEOUT_MS);
|
|
46
|
+
expect(onStall).not.toHaveBeenCalled();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("propagates the shared rejection", async () => {
|
|
50
|
+
const joined = joinInflight(Promise.reject(new Error("upstream")), async () => "fresh");
|
|
51
|
+
await expect(joined).rejects.toThrow("upstream");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("falls back to its own work when the shared Promise never settles", async () => {
|
|
55
|
+
vi.useFakeTimers();
|
|
56
|
+
const joined = joinInflight(never<string>(), async () => "fresh");
|
|
57
|
+
await vi.advanceTimersByTimeAsync(DEFAULT_INFLIGHT_TIMEOUT_MS);
|
|
58
|
+
await expect(joined).resolves.toBe("fresh");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("rejects instead of hanging when the fallback throws synchronously", async () => {
|
|
62
|
+
vi.useFakeTimers();
|
|
63
|
+
const joined = joinInflight(never<string>(), () => {
|
|
64
|
+
throw new Error("sync");
|
|
65
|
+
});
|
|
66
|
+
const settled = expect(joined).rejects.toThrow("sync");
|
|
67
|
+
await vi.advanceTimersByTimeAsync(DEFAULT_INFLIGHT_TIMEOUT_MS);
|
|
68
|
+
await settled;
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("createCachedLoaderFromModule — owner request cancelled", () => {
|
|
73
|
+
it("a joiner of a flight that never settles runs again after the timeout", async () => {
|
|
74
|
+
vi.useFakeTimers();
|
|
75
|
+
let calls = 0;
|
|
76
|
+
const fn = vi.fn(async () => {
|
|
77
|
+
calls += 1;
|
|
78
|
+
// First call: its request was cancelled, the Promise never settles.
|
|
79
|
+
if (calls === 1) return never<string>();
|
|
80
|
+
return "minicart";
|
|
81
|
+
});
|
|
82
|
+
const mod: LoaderModule = { default: fn, cache: "stale-while-revalidate" };
|
|
83
|
+
const loader = createCachedLoaderFromModule("site/loaders/minicart", mod);
|
|
84
|
+
|
|
85
|
+
void loader({ cart: "abc" }); // owner — abandoned
|
|
86
|
+
const joiner = loader({ cart: "abc" });
|
|
87
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
88
|
+
|
|
89
|
+
await vi.advanceTimersByTimeAsync(DEFAULT_INFLIGHT_TIMEOUT_MS);
|
|
90
|
+
await expect(joiner).resolves.toBe("minicart");
|
|
91
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
92
|
+
|
|
93
|
+
// The dead slot is gone: the next call runs (and settles) instead of hanging.
|
|
94
|
+
await expect(loader({ cart: "abc" })).resolves.toBe("minicart");
|
|
95
|
+
expect(fn).toHaveBeenCalledTimes(3);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("an old flight settling late does not evict the newer one", async () => {
|
|
99
|
+
vi.useFakeTimers();
|
|
100
|
+
let releaseOld!: (v: string) => void;
|
|
101
|
+
let releaseNew!: (v: string) => void;
|
|
102
|
+
const fn = vi
|
|
103
|
+
.fn()
|
|
104
|
+
.mockImplementationOnce(() => new Promise<string>((r) => (releaseOld = r)))
|
|
105
|
+
.mockImplementationOnce(() => new Promise<string>((r) => (releaseNew = r)))
|
|
106
|
+
.mockImplementation(async () => "third");
|
|
107
|
+
const mod: LoaderModule = { default: fn, cache: "stale-while-revalidate" };
|
|
108
|
+
const loader = createCachedLoaderFromModule("site/loaders/late", mod);
|
|
109
|
+
|
|
110
|
+
void loader({ k: 1 });
|
|
111
|
+
const joiner = loader({ k: 1 });
|
|
112
|
+
await vi.advanceTimersByTimeAsync(DEFAULT_INFLIGHT_TIMEOUT_MS); // joiner starts flight #2
|
|
113
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
114
|
+
|
|
115
|
+
releaseOld("old"); // flight #1 settles late
|
|
116
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
117
|
+
// The joiner was still listening to flight #1: whichever settles first answers it.
|
|
118
|
+
await expect(joiner).resolves.toBe("old");
|
|
119
|
+
|
|
120
|
+
// A new caller still joins flight #2 rather than starting a third.
|
|
121
|
+
const another = loader({ k: 1 });
|
|
122
|
+
releaseNew("new");
|
|
123
|
+
await expect(another).resolves.toBe("new");
|
|
124
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("registers the owner's work with waitUntil, once per flight", async () => {
|
|
128
|
+
let release!: (v: string) => void;
|
|
129
|
+
const fn = vi.fn(() => new Promise<string>((r) => (release = r)));
|
|
130
|
+
const mod: LoaderModule = { default: fn, cache: "stale-while-revalidate" };
|
|
131
|
+
const loader = createCachedLoaderFromModule("site/loaders/kept", mod);
|
|
132
|
+
const ownerWaitUntil = vi.fn();
|
|
133
|
+
const joinerWaitUntil = vi.fn();
|
|
134
|
+
|
|
135
|
+
const owner = inRequest(ownerWaitUntil, () => loader({ k: 1 }));
|
|
136
|
+
const joiner = inRequest(joinerWaitUntil, () => loader({ k: 1 }));
|
|
137
|
+
release("ok");
|
|
138
|
+
await expect(Promise.all([owner, joiner])).resolves.toEqual(["ok", "ok"]);
|
|
139
|
+
|
|
140
|
+
expect(ownerWaitUntil).toHaveBeenCalledTimes(1);
|
|
141
|
+
expect(joinerWaitUntil).not.toHaveBeenCalled();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("createCachedLoader — owner request cancelled", () => {
|
|
146
|
+
it("a joiner recovers when the owner's Promise and its timeout are both dead", async () => {
|
|
147
|
+
vi.useFakeTimers();
|
|
148
|
+
let calls = 0;
|
|
149
|
+
const loaderFn = vi.fn(async (_p: { id: number }) => {
|
|
150
|
+
calls += 1;
|
|
151
|
+
if (calls === 1) return never<{ v: number }>();
|
|
152
|
+
return { v: 2 };
|
|
153
|
+
});
|
|
154
|
+
const cached = createCachedLoader("t/cancelled-owner", loaderFn, {
|
|
155
|
+
policy: "stale-while-revalidate",
|
|
156
|
+
maxAge: 60_000,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
void cached({ id: 1 });
|
|
160
|
+
// The runtime cancelled the owner's request: its inflight timeout is gone too.
|
|
161
|
+
vi.clearAllTimers();
|
|
162
|
+
|
|
163
|
+
const joiner = cached({ id: 1 });
|
|
164
|
+
await vi.advanceTimersByTimeAsync(DEFAULT_INFLIGHT_TIMEOUT_MS);
|
|
165
|
+
await expect(joiner).resolves.toEqual({ v: 2 });
|
|
166
|
+
expect(loaderFn).toHaveBeenCalledTimes(2);
|
|
167
|
+
|
|
168
|
+
// Cached by the joiner's own flight: the next call is a HIT.
|
|
169
|
+
await expect(cached({ id: 1 })).resolves.toEqual({ v: 2 });
|
|
170
|
+
expect(loaderFn).toHaveBeenCalledTimes(2);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("joiners of a live owner still share its single upstream call", async () => {
|
|
174
|
+
let release!: (v: { v: number }) => void;
|
|
175
|
+
const loaderFn = vi.fn(() => new Promise<{ v: number }>((r) => (release = r)));
|
|
176
|
+
const cached = createCachedLoader("t/live-owner", loaderFn, {
|
|
177
|
+
policy: "stale-while-revalidate",
|
|
178
|
+
maxAge: 60_000,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const a = cached({ id: 1 });
|
|
182
|
+
const b = cached({ id: 1 });
|
|
183
|
+
release({ v: 1 });
|
|
184
|
+
await expect(Promise.all([a, b])).resolves.toEqual([{ v: 1 }, { v: 1 }]);
|
|
185
|
+
expect(loaderFn).toHaveBeenCalledTimes(1);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("registers the owner's miss with waitUntil", async () => {
|
|
189
|
+
const loaderFn = vi.fn(async (p: { id: number }) => ({ v: p.id }));
|
|
190
|
+
const cached = createCachedLoader("t/kept", loaderFn, {
|
|
191
|
+
policy: "stale-while-revalidate",
|
|
192
|
+
maxAge: 60_000,
|
|
193
|
+
});
|
|
194
|
+
const waitUntil = vi.fn();
|
|
195
|
+
|
|
196
|
+
await inRequest(waitUntil, () => cached({ id: 1 }));
|
|
197
|
+
expect(waitUntil).toHaveBeenCalledTimes(1);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
9
9
|
import { configureMeter, type MeterAdapter } from "../middleware/observability";
|
|
10
|
+
import { bindCacheStorage, createMemoryCacheStorage } from "./cacheStorage";
|
|
10
11
|
import { createFetchCache } from "./fetchCache";
|
|
12
|
+
import { RequestContext } from "./requestContext";
|
|
11
13
|
|
|
12
14
|
interface Counter {
|
|
13
15
|
name: string;
|
|
@@ -54,9 +56,7 @@ afterEach(() => {
|
|
|
54
56
|
});
|
|
55
57
|
|
|
56
58
|
const statuses = () =>
|
|
57
|
-
counters
|
|
58
|
-
.filter((c) => c.name === "deco.cache.requests")
|
|
59
|
-
.map((c) => c.labels?.["status"]);
|
|
59
|
+
counters.filter((c) => c.name === "deco.cache.requests").map((c) => c.labels?.["status"]);
|
|
60
60
|
|
|
61
61
|
describe("createFetchCache — behavior", () => {
|
|
62
62
|
it("cold call is a MISS, second call is a HIT with the cached body", async () => {
|
|
@@ -162,3 +162,45 @@ describe("createFetchCache — telemetry labels", () => {
|
|
|
162
162
|
).resolves.toEqual({});
|
|
163
163
|
});
|
|
164
164
|
});
|
|
165
|
+
|
|
166
|
+
describe("createFetchCache — shared storage scope", () => {
|
|
167
|
+
const pending: Promise<unknown>[] = [];
|
|
168
|
+
const bind = <T>(
|
|
169
|
+
storage: ReturnType<typeof createMemoryCacheStorage>,
|
|
170
|
+
scope: string,
|
|
171
|
+
dataScope: string | undefined,
|
|
172
|
+
fn: () => Promise<T>,
|
|
173
|
+
) =>
|
|
174
|
+
RequestContext.run(new Request("https://site.test/"), () => {
|
|
175
|
+
bindCacheStorage({ storage, scope, dataScope, waitUntil: (w) => pending.push(w) });
|
|
176
|
+
return fn();
|
|
177
|
+
});
|
|
178
|
+
const flush = () => Promise.all(pending.splice(0));
|
|
179
|
+
|
|
180
|
+
// A fresh cache instance per "deploy" so the local tier can't answer.
|
|
181
|
+
async function acrossDeploy(dataScope: string | undefined) {
|
|
182
|
+
const storage = createMemoryCacheStorage();
|
|
183
|
+
let calls = 0;
|
|
184
|
+
const doFetch = () => {
|
|
185
|
+
calls++;
|
|
186
|
+
return Promise.resolve(jsonResponse({ ok: true }));
|
|
187
|
+
};
|
|
188
|
+
const before = createFetchCache({ provider: "vtex", ...KNOBS });
|
|
189
|
+
await bind(storage, "site:build-A", dataScope, () => before.fetchWithCache("k", doFetch));
|
|
190
|
+
await flush();
|
|
191
|
+
const after = createFetchCache({ provider: "vtex", ...KNOBS });
|
|
192
|
+
const body = await bind(storage, "site:build-B", dataScope, () =>
|
|
193
|
+
after.fetchWithCache("k", doFetch),
|
|
194
|
+
);
|
|
195
|
+
await flush();
|
|
196
|
+
return { body, calls };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
it("survives a build change when dataScope is bound", async () => {
|
|
200
|
+
expect(await acrossDeploy("site:data:")).toEqual({ body: { ok: true }, calls: 1 });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("misses after a build change without dataScope", async () => {
|
|
204
|
+
expect(await acrossDeploy(undefined)).toEqual({ body: { ok: true }, calls: 2 });
|
|
205
|
+
});
|
|
206
|
+
});
|
package/src/sdk/fetchCache.ts
CHANGED
|
@@ -130,7 +130,13 @@ function withTimeout<T>(work: Promise<T>, ms: number, label: string): Promise<T>
|
|
|
130
130
|
export function createFetchCache(config: FetchCacheConfig): FetchCache {
|
|
131
131
|
const { provider, maxEntries, freshTtlMs, staleIfErrorMs, inflightBackstopMs } = config;
|
|
132
132
|
|
|
133
|
-
const store = createCacheStore<CacheEntry>(
|
|
133
|
+
const store = createCacheStore<CacheEntry>(
|
|
134
|
+
`fetch:${provider}`,
|
|
135
|
+
maxEntries,
|
|
136
|
+
undefined,
|
|
137
|
+
undefined,
|
|
138
|
+
"data",
|
|
139
|
+
);
|
|
134
140
|
const inflight = new Map<string, Promise<CacheEntry>>();
|
|
135
141
|
|
|
136
142
|
function freshTtlForStatus(status: number): number {
|
|
@@ -51,3 +51,43 @@ export function withInflightTimeout<T>(
|
|
|
51
51
|
if (timer !== undefined) clearTimeout(timer);
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Join a shared inflight Promise with a timeout of the JOINER's own.
|
|
57
|
+
*
|
|
58
|
+
* `withInflightTimeout` arms its timer in the owner's request. On Cloudflare
|
|
59
|
+
* Workers that request's I/O — and its timers — are cancelled when its client
|
|
60
|
+
* disconnects (reload, navigation, closed tab), so a Promise the owner left in
|
|
61
|
+
* a module-level dedup map may never settle, and the owner's timeout never
|
|
62
|
+
* fires either. Every later caller that joins it would hang for the life of
|
|
63
|
+
* the isolate.
|
|
64
|
+
*
|
|
65
|
+
* The timer here is created by the caller that joins, so it lives as long as
|
|
66
|
+
* that caller's request. If `shared` has not settled within `ms`, `onStall`
|
|
67
|
+
* runs and its result answers the joiner instead — callers use it to evict the
|
|
68
|
+
* dead slot (when it still holds `shared`) and start their own work. If
|
|
69
|
+
* `shared` settles first, the joiner gets its result, as before.
|
|
70
|
+
*/
|
|
71
|
+
export function joinInflight<T>(
|
|
72
|
+
shared: Promise<T>,
|
|
73
|
+
onStall: () => Promise<T>,
|
|
74
|
+
ms: number = DEFAULT_INFLIGHT_TIMEOUT_MS,
|
|
75
|
+
): Promise<T> {
|
|
76
|
+
return new Promise<T>((resolve, reject) => {
|
|
77
|
+
const timer = setTimeout(() => {
|
|
78
|
+
// `onStall` may throw synchronously (a non-async loader wrapper); route it
|
|
79
|
+
// into the Promise chain so the joiner rejects instead of hanging.
|
|
80
|
+
Promise.resolve().then(onStall).then(resolve, reject);
|
|
81
|
+
}, ms);
|
|
82
|
+
shared.then(
|
|
83
|
+
(value) => {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
resolve(value);
|
|
86
|
+
},
|
|
87
|
+
(error) => {
|
|
88
|
+
clearTimeout(timer);
|
|
89
|
+
reject(error);
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
}
|