@decocms/blocks 7.78.1 → 7.78.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "7.78.1",
3
+ "version": "7.78.2",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -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
- return async (props: TProps): Promise<TResult> => {
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.delete(cacheKey));
300
- inflightRequests.set(cacheKey, promise);
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 as Promise<TResult>;
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.delete(cacheKey));
460
+ .finally(() => releaseSlot(inflightRequests, cacheKey, promise));
428
461
 
429
- inflightRequests.set(cacheKey, promise);
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
- return (props: TProps, req?: Request): Promise<TResult> => {
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) return 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.delete(key);
536
+ const promise: Promise<TResult> = Promise.resolve(mod.default(props, req)).finally(() => {
537
+ releaseSlot(moduleInflight, key, promise);
498
538
  });
499
- moduleInflight.set(key, promise);
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
+ });
@@ -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
+ }