@decocms/tanstack 7.79.8 → 7.80.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/tanstack",
3
- "version": "7.79.8",
3
+ "version": "7.80.1",
4
4
  "type": "module",
5
5
  "description": "Deco framework binding for TanStack Start + Cloudflare Workers",
6
6
  "repository": {
@@ -27,9 +27,9 @@
27
27
  "lint:unused": "knip"
28
28
  },
29
29
  "dependencies": {
30
- "@decocms/blocks": "7.79.8",
31
- "@decocms/blocks-admin": "7.79.8",
32
- "@decocms/blocks-cli": "7.79.8",
30
+ "@decocms/blocks": "7.80.1",
31
+ "@decocms/blocks-admin": "7.80.1",
32
+ "@decocms/blocks-cli": "7.80.1",
33
33
  "@deco-cx/warp-node": "^0.3.16",
34
34
  "fast-json-patch": "^3.1.0",
35
35
  "ws": "^8.18.0"
@@ -1,4 +1,4 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
2
  import {
3
3
  computeRevision,
4
4
  getRevision,
@@ -12,6 +12,7 @@ import {
12
12
  __resetKvHydrationStateForTests,
13
13
  ensureBlocksHydrated,
14
14
  getDeploymentId,
15
+ HYDRATION_STALE_MS,
15
16
  isFastDeployEnabled,
16
17
  maybePollRevision,
17
18
  } from "./kvHydration";
@@ -158,6 +159,155 @@ describe("ensureBlocksHydrated", () => {
158
159
  });
159
160
  });
160
161
 
162
+ /**
163
+ * KV whose reads hang until `release()`: a load whose creating request is gone
164
+ * never settles on Workers, which is what a never-resolving get() models.
165
+ */
166
+ function makeHangingKV(blocks: Record<string, unknown>) {
167
+ const store = new Map<string, string>([
168
+ [SNAP, JSON.stringify(blocks)],
169
+ [REV, computeRevision(blocks)],
170
+ ]);
171
+ let hang = true;
172
+ let getCalls = 0;
173
+ const kv: KVNamespace = {
174
+ get: (k) => {
175
+ getCalls++;
176
+ return hang ? new Promise(() => {}) : Promise.resolve(store.get(k) ?? null);
177
+ },
178
+ put: () => Promise.resolve(),
179
+ delete: () => Promise.resolve(),
180
+ };
181
+ return {
182
+ env: { DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID },
183
+ release: () => {
184
+ hang = false;
185
+ },
186
+ getCalls: () => getCalls,
187
+ };
188
+ }
189
+
190
+ describe("ensureBlocksHydrated when the in-flight load never settles", () => {
191
+ afterEach(() => {
192
+ vi.useRealTimers();
193
+ });
194
+
195
+ it("hands the load to ctx.waitUntil so the creating request outlives it", async () => {
196
+ const { env } = snapshotEnv({ Site: { name: "from-kv" } });
197
+ const { ctx, settle } = makeCtx();
198
+ const waitUntil = vi.spyOn(ctx, "waitUntil");
199
+ await ensureBlocksHydrated(env, ctx);
200
+ expect(waitUntil).toHaveBeenCalledTimes(1);
201
+ await settle();
202
+ });
203
+
204
+ it("a waiting request gives up after HYDRATION_STALE_MS and loads on its own", async () => {
205
+ vi.useFakeTimers();
206
+ const kvBlocks = { Site: { name: "from-kv" } };
207
+ const { env, release, getCalls } = makeHangingKV(kvBlocks);
208
+
209
+ void ensureBlocksHydrated(env); // the creator, whose load never settles
210
+ const waiter = ensureBlocksHydrated(env);
211
+ const done = vi.fn();
212
+ void waiter.then(done);
213
+
214
+ await vi.advanceTimersByTimeAsync(HYDRATION_STALE_MS - 1);
215
+ expect(done).not.toHaveBeenCalled();
216
+
217
+ release(); // KV is fine again; only the dead load is stuck
218
+ await vi.advanceTimersByTimeAsync(1);
219
+ await waiter;
220
+ expect(loadBlocks()).toEqual(kvBlocks);
221
+ expect(getCalls()).toBeGreaterThan(1);
222
+ });
223
+
224
+ it("a request arriving after HYDRATION_STALE_MS does not join the dead load", async () => {
225
+ vi.useFakeTimers();
226
+ const kvBlocks = { Site: { name: "from-kv" } };
227
+ const { env, release } = makeHangingKV(kvBlocks);
228
+
229
+ void ensureBlocksHydrated(env);
230
+ await vi.advanceTimersByTimeAsync(HYDRATION_STALE_MS);
231
+ release();
232
+ await ensureBlocksHydrated(env);
233
+ expect(loadBlocks()).toEqual(kvBlocks);
234
+ });
235
+
236
+ it("a dropped load that settles late does not clobber the load that replaced it", async () => {
237
+ vi.useFakeTimers();
238
+ let resolveOld: ((v: string | null) => void) | undefined;
239
+ const fresh = { Site: { name: "fresh" } };
240
+ let calls = 0;
241
+ const kv: KVNamespace = {
242
+ get: (k) => {
243
+ calls++;
244
+ if (calls > 2) return new Promise(() => {}); // the replacement load is still in flight
245
+ if (k !== SNAP) return Promise.resolve(null);
246
+ return new Promise((r) => {
247
+ resolveOld = r;
248
+ });
249
+ },
250
+ put: () => Promise.resolve(),
251
+ delete: () => Promise.resolve(),
252
+ };
253
+ const env = { DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID };
254
+
255
+ void ensureBlocksHydrated(env);
256
+ await vi.advanceTimersByTimeAsync(HYDRATION_STALE_MS);
257
+ const replacement = ensureBlocksHydrated(env); // drops the old load, starts its own
258
+ const done = vi.fn();
259
+ void replacement.then(done);
260
+
261
+ resolveOld?.(JSON.stringify(fresh)); // the old load finishes late
262
+ await vi.advanceTimersByTimeAsync(0);
263
+
264
+ // Its finally must not mark the isolate hydrated nor null the replacement:
265
+ // a third request still joins the replacement instead of returning at once.
266
+ const third = vi.fn();
267
+ void ensureBlocksHydrated(env).then(third);
268
+ await vi.advanceTimersByTimeAsync(0);
269
+ expect(third).not.toHaveBeenCalled();
270
+ expect(done).not.toHaveBeenCalled();
271
+ });
272
+ });
273
+
274
+ describe("ensureBlocksHydrated when a dropped load settles late", () => {
275
+ afterEach(() => {
276
+ vi.useRealTimers();
277
+ });
278
+
279
+ it("does not swap its snapshot over the one the replacement loaded", async () => {
280
+ vi.useFakeTimers();
281
+ const old = { Site: { name: "old-revision" } };
282
+ const fresh = { Site: { name: "new-revision" } };
283
+ let resolveOld: ((v: string | null) => void) | undefined;
284
+ let firstLoad = true;
285
+ const kv: KVNamespace = {
286
+ get: (k) => {
287
+ if (firstLoad && k === SNAP) {
288
+ return new Promise((r) => {
289
+ resolveOld = r;
290
+ });
291
+ }
292
+ return Promise.resolve(k === SNAP ? JSON.stringify(fresh) : computeRevision(fresh));
293
+ },
294
+ put: () => Promise.resolve(),
295
+ delete: () => Promise.resolve(),
296
+ };
297
+ const env = { DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID };
298
+
299
+ void ensureBlocksHydrated(env);
300
+ await vi.advanceTimersByTimeAsync(HYDRATION_STALE_MS);
301
+ firstLoad = false;
302
+ await ensureBlocksHydrated(env); // drops the old load and hydrates with `fresh`
303
+ expect(loadBlocks()).toEqual(fresh);
304
+
305
+ resolveOld?.(JSON.stringify(old)); // the old load finishes after a CMS publish
306
+ await vi.advanceTimersByTimeAsync(0);
307
+ expect(loadBlocks()).toEqual(fresh);
308
+ });
309
+ });
310
+
161
311
  describe("getDeploymentId", () => {
162
312
  it("prefers DECO_DEPLOYMENT_ID", () => {
163
313
  expect(getDeploymentId({ DECO_DEPLOYMENT_ID: "a", BUILD_HASH: "b" })).toBe("a");
@@ -32,6 +32,16 @@ import { setSpanAttribute } from "@decocms/blocks/sdk/observability";
32
32
  /** How often (ms) an isolate re-probes its `index:revision:<id>`. */
33
33
  export const POLL_INTERVAL_MS = 10_000;
34
34
 
35
+ /**
36
+ * Age (ms) past which a request stops waiting on an in-flight cold-start load
37
+ * and starts its own. A Workers promise can only settle while the request that
38
+ * created it is alive; if that request is gone, the load never settles (its
39
+ * `finally` included) and every request awaiting it hangs. `waitUntil` keeps
40
+ * the creator alive, but only for 30 s after its response, and a KV read has
41
+ * no deadline. Calibration knob: a healthy ~7 MB read takes up to ~10 s.
42
+ */
43
+ export const HYDRATION_STALE_MS = 25_000;
44
+
35
45
  /** KV binding name expected on the Worker `env`. */
36
46
  export const KV_BINDING = "DECO_KV";
37
47
 
@@ -50,6 +60,7 @@ const G = globalThis as unknown as {
50
60
  __deco?: {
51
61
  kvHydrated?: boolean;
52
62
  kvHydration?: Promise<void> | null;
63
+ kvHydrationStartedAt?: number;
53
64
  kvLastPolledAt?: number;
54
65
  dataVersion?: string | null;
55
66
  dataVersionAt?: number;
@@ -139,7 +150,7 @@ function bundleHasNoDecofile(): boolean {
139
150
  * a 5xx is the only honest answer; hydration is left unlatched so the next
140
151
  * request retries instead of pinning the isolate to an empty decofile.
141
152
  */
142
- export function ensureBlocksHydrated(env: Env, _ctx?: ExecutionContextLike): Promise<void> {
153
+ export function ensureBlocksHydrated(env: Env, ctx?: ExecutionContextLike): Promise<void> {
143
154
  if (!isFastDeployEnabled(env)) {
144
155
  // A stubbed bundle with fast-deploy off would serve an empty 200 the edge caches.
145
156
  return bundleHasNoDecofile()
@@ -151,7 +162,15 @@ export function ensureBlocksHydrated(env: Env, _ctx?: ExecutionContextLike): Pro
151
162
  : Promise.resolve();
152
163
  }
153
164
  if (G.__deco!.kvHydrated) return Promise.resolve();
154
- if (G.__deco!.kvHydration) return G.__deco!.kvHydration;
165
+ const inFlight = G.__deco!.kvHydration;
166
+ if (inFlight) {
167
+ // The load belongs to another request. Wait for it, but never past
168
+ // HYDRATION_STALE_MS: this request's own timer still fires if the creator
169
+ // is gone, and then it drops the dead load and starts one of its own.
170
+ const left = (G.__deco!.kvHydrationStartedAt ?? 0) + HYDRATION_STALE_MS - Date.now();
171
+ if (left > 0) return waitOrRestart(inFlight, left, env, ctx);
172
+ G.__deco!.kvHydration = null;
173
+ }
155
174
 
156
175
  const kv = getKV(env);
157
176
  if (!kv) return Promise.resolve();
@@ -172,6 +191,8 @@ export function ensureBlocksHydrated(env: Env, _ctx?: ExecutionContextLike): Pro
172
191
  return Promise.resolve();
173
192
  }
174
193
 
194
+ const startedAt = Date.now();
195
+ G.__deco!.kvHydrationStartedAt = startedAt;
175
196
  const load = (async () => {
176
197
  // Held rather than rethrown inline so the `finally` below can decide
177
198
  // whether to latch — latching on a fatal path would serve an empty
@@ -179,6 +200,9 @@ export function ensureBlocksHydrated(env: Env, _ctx?: ExecutionContextLike): Pro
179
200
  let fatal: Error | null = null;
180
201
  try {
181
202
  const snapshot = await new KVBlockSource(kv, deploymentId).loadSnapshot();
203
+ // Dropped as stale while reading: the load that replaced it owns the
204
+ // decofile now, and this snapshot may be an older revision than its.
205
+ if (G.__deco!.kvHydrationStartedAt !== startedAt) return;
182
206
  // An empty snapshot is treated as a failure, not a success: with no
183
207
  // bundled decofile it would otherwise latch and serve exactly the empty,
184
208
  // edge-cached site this guard exists to prevent. A site legitimately has
@@ -204,16 +228,41 @@ export function ensureBlocksHydrated(env: Env, _ctx?: ExecutionContextLike): Pro
204
228
  fatal = e instanceof Error ? e : new Error(String(e));
205
229
  }
206
230
  } finally {
207
- G.__deco!.kvHydrated = !fatal;
208
- G.__deco!.kvHydration = null;
231
+ // A load dropped as stale must not clobber the one that replaced it.
232
+ if (G.__deco!.kvHydrationStartedAt === startedAt) {
233
+ G.__deco!.kvHydrated = !fatal;
234
+ G.__deco!.kvHydration = null;
235
+ }
209
236
  }
210
237
  if (fatal) throw fatal;
211
238
  })();
212
239
 
213
240
  G.__deco!.kvHydration = load;
241
+ // Keep this request alive until the load settles: the other requests of the
242
+ // isolate await it, and it can only settle while its creator is alive.
243
+ ctx?.waitUntil(load.catch(() => {}));
214
244
  return load;
215
245
  }
216
246
 
247
+ function waitOrRestart(
248
+ inFlight: Promise<void>,
249
+ ms: number,
250
+ env: Env,
251
+ ctx?: ExecutionContextLike,
252
+ ): Promise<void> {
253
+ let timer: ReturnType<typeof setTimeout> | undefined;
254
+ const stale = new Promise<"stale">((resolve) => {
255
+ timer = setTimeout(() => resolve("stale"), ms);
256
+ });
257
+ return Promise.race([inFlight, stale])
258
+ .finally(() => clearTimeout(timer))
259
+ .then((r) => {
260
+ if (r !== "stale") return;
261
+ if (G.__deco!.kvHydration === inFlight) G.__deco!.kvHydration = null;
262
+ return ensureBlocksHydrated(env, ctx);
263
+ });
264
+ }
265
+
217
266
  /**
218
267
  * Opportunistic revision poll. Throttled to once per `POLL_INTERVAL_MS` and run
219
268
  * through `ctx.waitUntil` so it never adds latency to the response. Reloads the
@@ -312,6 +361,7 @@ export async function getDataCacheVersion(env: Env, ctx?: ExecutionContextLike):
312
361
  export function __resetKvHydrationStateForTests(): void {
313
362
  G.__deco!.kvHydrated = false;
314
363
  G.__deco!.kvHydration = null;
364
+ G.__deco!.kvHydrationStartedAt = 0;
315
365
  G.__deco!.kvLastPolledAt = 0;
316
366
  G.__deco!.dataVersion = null;
317
367
  G.__deco!.dataVersionAt = 0;
@@ -42,7 +42,7 @@
42
42
  */
43
43
  import { exec, execFileSync } from "node:child_process";
44
44
  import { createHash } from "node:crypto";
45
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
45
+ import { existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
46
46
  import path from "node:path";
47
47
 
48
48
  /**
@@ -227,6 +227,27 @@ function isSeededDeployPipeline() {
227
227
  return v === "1" || v === "true";
228
228
  }
229
229
 
230
+ /**
231
+ * `deco.sectionPackages` from the site's package.json → the realpath of each
232
+ * package's src/ (Vite reports realpaths for symlinked `file:` deps). Mirrors
233
+ * blocks-cli's readSectionPackages, which owns the "package missing" error.
234
+ * ponytail: only the site's own node_modules, no hoisted-workspace lookup.
235
+ */
236
+ function readSectionPackageSrcDirs(cwd) {
237
+ try {
238
+ const { deco } = JSON.parse(readFileSync(path.join(cwd, "package.json"), "utf8"));
239
+ return Object.entries(deco?.sectionPackages ?? {}).flatMap(([namespace, name]) => {
240
+ try {
241
+ return [{ namespace, srcDir: realpathSync(path.join(cwd, "node_modules", name, "src")) }];
242
+ } catch {
243
+ return [];
244
+ }
245
+ });
246
+ } catch {
247
+ return [];
248
+ }
249
+ }
250
+
230
251
  /**
231
252
  * @param {object} [opts]
232
253
  * @param {boolean | "auto"} [opts.fastDeploy="auto"] Stub `blocks.gen` out of
@@ -276,6 +297,7 @@ function isSeededDeployPipeline() {
276
297
  * @returns {import("vite").PluginOption}
277
298
  */
278
299
  export function decoVitePlugin({ fastDeploy = "auto", metaAsset = false } = {}) {
300
+ const sectionPackages = readSectionPackageSrcDirs(process.cwd());
279
301
  // Resolved once: the pipeline's answer cannot change mid-build.
280
302
  const stubBlocks = fastDeploy === "auto" ? isSeededDeployPipeline() : fastDeploy === true;
281
303
  // Set from the `config` hook (which Vite always runs before `load`). The
@@ -752,7 +774,8 @@ export function decoVitePlugin({ fastDeploy = "auto", metaAsset = false } = {})
752
774
  const isSchemaSource = (file) => {
753
775
  const rel = path.relative(cwd, file);
754
776
  return (
755
- schemaWatchDirs.some((d) => rel.startsWith(d + path.sep)) &&
777
+ (schemaWatchDirs.some((d) => rel.startsWith(d + path.sep)) ||
778
+ sectionPackages.some((p) => file.startsWith(p.srcDir + path.sep))) &&
756
779
  (rel.endsWith(".tsx") || rel.endsWith(".ts"))
757
780
  );
758
781
  };
@@ -1105,7 +1128,15 @@ export function decoVitePlugin({ fastDeploy = "auto", metaAsset = false } = {})
1105
1128
  const map = {};
1106
1129
  for (const [fileName, chunk] of Object.entries(bundle)) {
1107
1130
  if (chunk.type === "chunk" && chunk.facadeModuleId) {
1108
- const match = chunk.facadeModuleId.match(/\/(sections\/.+\.tsx)$/);
1131
+ const id = chunk.facadeModuleId;
1132
+ const pkg = sectionPackages.find((p) =>
1133
+ id.startsWith(path.join(p.srcDir, "sections") + path.sep),
1134
+ );
1135
+ if (pkg) {
1136
+ map[`${pkg.namespace}/${path.relative(pkg.srcDir, id)}`] = fileName;
1137
+ continue;
1138
+ }
1139
+ const match = id.match(/\/(sections\/.+\.tsx)$/);
1109
1140
  if (match) {
1110
1141
  map["site/" + match[1]] = fileName;
1111
1142
  }