@decocms/tanstack 7.86.2 → 7.86.3
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 +4 -4
- package/src/cms/kvBlockSource.test.ts +20 -10
- package/src/cms/kvBlockSource.ts +11 -1
- package/src/sdk/kvHydration.ts +4 -4
- package/src/sdk/workerEntry.test.ts +18 -1
- package/src/sdk/workerEntry.ts +14 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/tanstack",
|
|
3
|
-
"version": "7.86.
|
|
3
|
+
"version": "7.86.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Deco framework binding for TanStack Start + Cloudflare Workers",
|
|
6
6
|
"repository": {
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"lint:unused": "knip"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@decocms/blocks": "7.86.
|
|
30
|
-
"@decocms/blocks-admin": "7.86.
|
|
31
|
-
"@decocms/blocks-cli": "7.86.
|
|
29
|
+
"@decocms/blocks": "7.86.3",
|
|
30
|
+
"@decocms/blocks-admin": "7.86.3",
|
|
31
|
+
"@decocms/blocks-cli": "7.86.3"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"@tanstack/react-query": ">=5.0.0",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
1
|
import { computeRevision, type KVNamespace, revisionKey, snapshotKey } from "@decocms/blocks/cms";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
3
|
import { KVBlockSource } from "./kvBlockSource";
|
|
4
4
|
|
|
5
5
|
const ID = "sha-abc123";
|
|
@@ -37,16 +37,26 @@ describe("KVBlockSource.loadSnapshot", () => {
|
|
|
37
37
|
await expect(src.loadSnapshot()).resolves.toBeNull();
|
|
38
38
|
});
|
|
39
39
|
|
|
40
|
-
it("
|
|
40
|
+
it("uses the stored revision when it matches the snapshot, without re-serializing it", async () => {
|
|
41
41
|
const blocks = { Site: { name: "x" } };
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
const kv = makeKV({ [SNAP]: JSON.stringify(blocks), [REV]: computeRevision(blocks) });
|
|
43
|
+
const stringify = vi.spyOn(JSON, "stringify");
|
|
44
|
+
const snapshot = await new KVBlockSource(kv, ID).loadSnapshot();
|
|
45
|
+
expect(stringify).not.toHaveBeenCalled();
|
|
46
|
+
stringify.mockRestore();
|
|
47
|
+
expect(snapshot).toEqual({ blocks, revision: computeRevision(blocks) });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("ignores a stored revision from another write (torn read) and hashes what it loaded", async () => {
|
|
51
|
+
// KV keys are independent: a PoP can return the new revision with the old snapshot.
|
|
52
|
+
// Trusting it would pin the isolate to stale content under the new revision's cache scope.
|
|
53
|
+
const oldBlocks = { Site: { name: "old" } };
|
|
54
|
+
const newBlocks = { Site: { name: "new" } };
|
|
55
|
+
const kv = makeKV({ [SNAP]: JSON.stringify(oldBlocks), [REV]: computeRevision(newBlocks) });
|
|
56
|
+
await expect(new KVBlockSource(kv, ID).loadSnapshot()).resolves.toEqual({
|
|
57
|
+
blocks: oldBlocks,
|
|
58
|
+
revision: computeRevision(oldBlocks),
|
|
59
|
+
});
|
|
50
60
|
});
|
|
51
61
|
|
|
52
62
|
it("recomputes the revision when only the snapshot is stored", async () => {
|
package/src/cms/kvBlockSource.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
revisionKey,
|
|
23
23
|
snapshotKey,
|
|
24
24
|
} from "@decocms/blocks/cms";
|
|
25
|
+
import { djb2Hex } from "@decocms/blocks/sdk/djb2";
|
|
25
26
|
|
|
26
27
|
export class KVBlockSource implements BlockSource {
|
|
27
28
|
private readonly snapshotKey: string;
|
|
@@ -57,8 +58,17 @@ export class KVBlockSource implements BlockSource {
|
|
|
57
58
|
}
|
|
58
59
|
const blocks = parsed as Record<string, unknown>;
|
|
59
60
|
|
|
61
|
+
// Hash the raw text instead of re-serializing the parsed blocks (~21 MB of garbage
|
|
62
|
+
// on a 10 MB decofile). Writers store `JSON.stringify(blocks)`, so this equals
|
|
63
|
+
// `computeRevision(blocks)`. The stored revision is a separate key, so it is only
|
|
64
|
+
// trusted when it matches: a read that pairs a new revision with an older snapshot
|
|
65
|
+
// falls back to the revision of what was actually loaded, and the poller retries.
|
|
60
66
|
const storedRevision = await this.kv.get(this.revisionKey);
|
|
61
|
-
|
|
67
|
+
const revision =
|
|
68
|
+
storedRevision !== null && storedRevision === djb2Hex(raw)
|
|
69
|
+
? storedRevision
|
|
70
|
+
: computeRevision(blocks);
|
|
71
|
+
return { blocks, revision };
|
|
62
72
|
}
|
|
63
73
|
|
|
64
74
|
/** Cheap revision probe for change detection (no full snapshot transfer). */
|
package/src/sdk/kvHydration.ts
CHANGED
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
* `DecofileProvider` pattern from the deco-cx/deco Fresh runtime.
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
|
-
import { DEPLOYMENT_ID_ENV, getDeploymentId, getRevision, setBlocks } from "@decocms/blocks/cms";
|
|
28
|
-
import { KVBlockSource } from "../cms/kvBlockSource";
|
|
29
27
|
import type { KVNamespace } from "@decocms/blocks/cms";
|
|
28
|
+
import { DEPLOYMENT_ID_ENV, getDeploymentId, getRevision, setBlocks } from "@decocms/blocks/cms";
|
|
30
29
|
import { setSpanAttribute } from "@decocms/blocks/sdk/observability";
|
|
30
|
+
import { KVBlockSource } from "../cms/kvBlockSource";
|
|
31
31
|
|
|
32
32
|
/** How often (ms) an isolate re-probes its `index:revision:<id>`. */
|
|
33
33
|
export const POLL_INTERVAL_MS = 10_000;
|
|
@@ -209,7 +209,7 @@ export function ensureBlocksHydrated(env: Env, ctx?: ExecutionContextLike): Prom
|
|
|
209
209
|
// blocks; `{}` means the seed wrote nothing.
|
|
210
210
|
const empty = !snapshot || Object.keys(snapshot.blocks).length === 0;
|
|
211
211
|
if (!empty) {
|
|
212
|
-
setBlocks(snapshot!.blocks);
|
|
212
|
+
setBlocks(snapshot!.blocks, snapshot!.revision);
|
|
213
213
|
setSpanAttribute("deco.block.source", "kv");
|
|
214
214
|
} else if (!bundleHasNoDecofile()) {
|
|
215
215
|
setSpanAttribute("deco.block.source", "bundled");
|
|
@@ -297,7 +297,7 @@ async function pollRevisionOnce(kv: KVNamespace, deploymentId: string): Promise<
|
|
|
297
297
|
|
|
298
298
|
const snapshot = await source.loadSnapshot();
|
|
299
299
|
if (snapshot) {
|
|
300
|
-
setBlocks(snapshot.blocks);
|
|
300
|
+
setBlocks(snapshot.blocks, snapshot.revision);
|
|
301
301
|
console.info(`[CMS/KV] decofile refreshed → revision ${snapshot.revision}`);
|
|
302
302
|
}
|
|
303
303
|
} catch (e) {
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
__resetAutoconfigStateForTests,
|
|
11
11
|
autoconfigApps,
|
|
12
12
|
} from "@decocms/blocks-admin/apps/autoconfig";
|
|
13
|
-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
13
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
14
14
|
import { segmentToken } from "./cdnSegment";
|
|
15
15
|
import { __resetKvHydrationStateForTests } from "./kvHydration";
|
|
16
16
|
import { clearRedirectCache } from "./kvRedirects";
|
|
@@ -195,6 +195,23 @@ describe("detectLocationMatcher", () => {
|
|
|
195
195
|
expect(detectLocationMatcher({})).toBe(false);
|
|
196
196
|
});
|
|
197
197
|
|
|
198
|
+
it("finds the matcher inside arrays and does not serialize the decofile", () => {
|
|
199
|
+
const stringify = vi.spyOn(JSON, "stringify");
|
|
200
|
+
const blocks = {
|
|
201
|
+
"pages/home.json": {
|
|
202
|
+
sections: [{ variants: [{ rule: { __resolveType: "website/matchers/location.ts" } }] }],
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
expect(detectLocationMatcher(blocks)).toBe(true);
|
|
206
|
+
expect(stringify).not.toHaveBeenCalled();
|
|
207
|
+
stringify.mockRestore();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("ignores the path when it only appears inside a content string", () => {
|
|
211
|
+
const blocks = { "pages/a.json": { text: '"__resolveType":"website/matchers/location.ts"' } };
|
|
212
|
+
expect(detectLocationMatcher(blocks)).toBe(false);
|
|
213
|
+
});
|
|
214
|
+
|
|
198
215
|
it("returns true when the matcher is nested deeply", () => {
|
|
199
216
|
const blocks = {
|
|
200
217
|
"pages/home.json": {
|
package/src/sdk/workerEntry.ts
CHANGED
|
@@ -777,7 +777,20 @@ function buildPreviewShell(): string {
|
|
|
777
777
|
* the path.
|
|
778
778
|
*/
|
|
779
779
|
export function detectLocationMatcher(blocks: Record<string, unknown>): boolean {
|
|
780
|
-
|
|
780
|
+
// Walk instead of `JSON.stringify(blocks).includes(...)`: stringifying a 10 MB
|
|
781
|
+
// decofile allocated ~21 MB on every isolate boot just to find one resolver.
|
|
782
|
+
// ponytail: no visited set — a decofile is a JSON tree (the old stringify threw on cycles).
|
|
783
|
+
const stack: object[] = [blocks];
|
|
784
|
+
while (stack.length) {
|
|
785
|
+
const value = stack.pop() as Record<string, unknown>;
|
|
786
|
+
if (value.__resolveType === "website/matchers/location.ts" && !Array.isArray(value))
|
|
787
|
+
return true;
|
|
788
|
+
for (const key in value) {
|
|
789
|
+
const child = value[key];
|
|
790
|
+
if (child && typeof child === "object") stack.push(child);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return false;
|
|
781
794
|
}
|
|
782
795
|
|
|
783
796
|
/**
|