@decocms/tanstack 8.0.0 → 8.4.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/README.md +79 -0
- package/dist/draft.d.ts +33 -0
- package/dist/draft.d.ts.map +1 -0
- package/dist/draft.js +41 -0
- package/dist/draft.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/react/DecoDocument.d.ts +61 -0
- package/dist/react/DecoDocument.d.ts.map +1 -0
- package/dist/react/DecoDocument.js +96 -0
- package/dist/react/DecoDocument.js.map +1 -0
- package/dist/react/Image.d.ts +96 -0
- package/dist/react/Image.d.ts.map +1 -0
- package/dist/react/Image.js +79 -0
- package/dist/react/Image.js.map +1 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +7 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/pageValues.d.ts +10 -0
- package/dist/react/pageValues.d.ts.map +1 -0
- package/dist/react/pageValues.js +29 -0
- package/dist/react/pageValues.js.map +1 -0
- package/dist/react/scripts.d.ts +14 -0
- package/dist/react/scripts.d.ts.map +1 -0
- package/dist/react/scripts.js +96 -0
- package/dist/react/scripts.js.map +1 -0
- package/dist/router/index.d.ts +38 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/index.js +60 -0
- package/dist/router/index.js.map +1 -0
- package/dist/scope/browser.d.ts +11 -0
- package/dist/scope/browser.d.ts.map +1 -0
- package/dist/scope/browser.js +8 -0
- package/dist/scope/browser.js.map +1 -0
- package/dist/scope/server.d.ts +13 -0
- package/dist/scope/server.d.ts.map +1 -0
- package/dist/scope/server.js +26 -0
- package/dist/scope/server.js.map +1 -0
- package/dist/server/index.d.ts +27 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +31 -0
- package/dist/server/index.js.map +1 -0
- package/dist/shared.d.ts +26 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +26 -0
- package/dist/shared.js.map +1 -0
- package/dist/start/index.d.ts +9 -0
- package/dist/start/index.d.ts.map +1 -0
- package/dist/start/index.js +33 -0
- package/dist/start/index.js.map +1 -0
- package/dist/studio.d.ts +28 -0
- package/dist/studio.d.ts.map +1 -0
- package/dist/studio.js +53 -0
- package/dist/studio.js.map +1 -0
- package/dist/vite/index.d.ts +15 -0
- package/dist/vite/index.d.ts.map +1 -0
- package/dist/vite/index.js +95 -0
- package/dist/vite/index.js.map +1 -0
- package/dist/worker/cacheProfiles.d.ts +43 -0
- package/dist/worker/cacheProfiles.d.ts.map +1 -0
- package/dist/worker/cacheProfiles.js +162 -0
- package/dist/worker/cacheProfiles.js.map +1 -0
- package/dist/worker/cookies.d.ts +20 -0
- package/dist/worker/cookies.d.ts.map +1 -0
- package/dist/worker/cookies.js +69 -0
- package/dist/worker/cookies.js.map +1 -0
- package/dist/worker/drafts.d.ts +59 -0
- package/dist/worker/drafts.d.ts.map +1 -0
- package/dist/worker/drafts.js +140 -0
- package/dist/worker/drafts.js.map +1 -0
- package/dist/worker/index.d.ts +107 -0
- package/dist/worker/index.d.ts.map +1 -0
- package/dist/worker/index.js +511 -0
- package/dist/worker/index.js.map +1 -0
- package/dist/worker/security.d.ts +14 -0
- package/dist/worker/security.d.ts.map +1 -0
- package/dist/worker/security.js +48 -0
- package/dist/worker/security.js.map +1 -0
- package/dist/worker/segment.d.ts +21 -0
- package/dist/worker/segment.d.ts.map +1 -0
- package/dist/worker/segment.js +43 -0
- package/dist/worker/segment.js.map +1 -0
- package/package.json +86 -31
- package/src/draft.ts +43 -0
- package/src/index.ts +8 -44
- package/src/react/DecoDocument.tsx +234 -0
- package/src/react/Image.tsx +213 -0
- package/src/react/index.ts +24 -0
- package/src/react/pageValues.ts +35 -0
- package/src/react/scripts.ts +98 -0
- package/src/router/index.ts +80 -0
- package/src/scope/browser.ts +18 -0
- package/src/scope/server.ts +37 -0
- package/src/server/index.ts +43 -0
- package/src/shared.ts +29 -0
- package/src/start/index.ts +31 -0
- package/src/studio.ts +52 -0
- package/src/vite/index.ts +105 -0
- package/src/worker/cacheProfiles.ts +180 -0
- package/src/worker/cookies.ts +65 -0
- package/src/worker/drafts.ts +157 -0
- package/src/worker/index.ts +648 -0
- package/src/worker/security.ts +63 -0
- package/src/worker/segment.ts +49 -0
- package/src/cms/kvBlockSource.test.ts +0 -82
- package/src/cms/kvBlockSource.ts +0 -68
- package/src/daemon/auth.ts +0 -204
- package/src/daemon/fs.ts +0 -238
- package/src/daemon/index.ts +0 -8
- package/src/daemon/middleware.ts +0 -156
- package/src/daemon/tunnel.ts +0 -129
- package/src/daemon/volumes.ts +0 -366
- package/src/daemon/watch.ts +0 -272
- package/src/hooks/DecoPageRenderer.tsx +0 -688
- package/src/hooks/DecoRootLayout.tsx +0 -111
- package/src/hooks/NavigationProgress.tsx +0 -21
- package/src/hooks/PreviewProviders.tsx +0 -26
- package/src/hooks/StableOutlet.tsx +0 -30
- package/src/hooks/index.ts +0 -9
- package/src/routes/adminRoutes.test.ts +0 -99
- package/src/routes/adminRoutes.ts +0 -153
- package/src/routes/cmsRoute.test.ts +0 -100
- package/src/routes/cmsRoute.ts +0 -944
- package/src/routes/components.tsx +0 -57
- package/src/routes/index.ts +0 -24
- package/src/routes/pageUrl.test.ts +0 -70
- package/src/routes/pageUrl.ts +0 -54
- package/src/routes/withSiteGlobals.test.ts +0 -206
- package/src/routes/withSiteGlobals.ts +0 -222
- package/src/sdk/cookiePassthrough.ts +0 -58
- package/src/sdk/createInvoke.ts +0 -77
- package/src/sdk/deferredSectionLoader.test.ts +0 -94
- package/src/sdk/deferredSectionLoader.ts +0 -46
- package/src/sdk/kvHydration.test.ts +0 -215
- package/src/sdk/kvHydration.ts +0 -193
- package/src/sdk/outboundHeaders.test.ts +0 -113
- package/src/sdk/outboundHeaders.ts +0 -81
- package/src/sdk/router.ts +0 -92
- package/src/sdk/useHydrated.ts +0 -19
- package/src/sdk/workerEntry.test.ts +0 -495
- package/src/sdk/workerEntry.ts +0 -2307
- package/src/setupFastDeploy.ts +0 -13
- package/src/vite/plugin-scaffold-gen.test.js +0 -106
- package/src/vite/plugin.js +0 -850
- package/src/vite/plugin.test.js +0 -113
- package/src/vite/plugin.test.ts +0 -52
- package/tsconfig.json +0 -7
package/src/sdk/createInvoke.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type-only marker for apps-* `invoke.ts` codegen templates (e.g.
|
|
3
|
-
* `@decocms/apps-vtex/invoke.ts`) — NOT a real, callable createServerFn
|
|
4
|
-
* wrapper. Deliberately does not call `createServerFn` at runtime.
|
|
5
|
-
*
|
|
6
|
-
* `invoke.ts` files are never imported/executed by any real site — they
|
|
7
|
-
* are a source-of-truth that `blocks-cli`'s `generate-invoke.ts` statically
|
|
8
|
-
* parses (via AST inspection of each `createInvokeFn(action, opts)` call
|
|
9
|
-
* site's arguments) to emit real, literal top-level `createServerFn(...)`
|
|
10
|
-
* declarations into each site's own `src/server/invoke.gen.ts`. This
|
|
11
|
-
* function's ONLY job is to give those template files a correctly-typed
|
|
12
|
-
* call site to author against.
|
|
13
|
-
*
|
|
14
|
-
* It must not contain a real `createServerFn(...)` call: TanStack Start's
|
|
15
|
-
* compiler statically scans every file reachable by a site's SSR bundle for
|
|
16
|
-
* that literal pattern and throws "createServerFn must be assigned to a
|
|
17
|
-
* variable!" on ANY occurrence that isn't a top-level declarator — even one
|
|
18
|
-
* inside a function body that's never actually invoked. `invoke.ts` isn't
|
|
19
|
-
* supposed to be reachable by a site's bundler at all (it's excluded from
|
|
20
|
-
* every apps-* package's public exports map), but Vite's `optimizeDeps`
|
|
21
|
-
* pre-bundling has been observed sweeping it in regardless of that
|
|
22
|
-
* exports-map restriction — so the only fully robust fix is for this file
|
|
23
|
-
* to never contain the literal pattern the compiler is looking for, full
|
|
24
|
-
* stop, regardless of what does or doesn't end up bundled.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```ts
|
|
28
|
-
* import { createInvokeFn } from "@decocms/tanstack/sdk/createInvoke";
|
|
29
|
-
* import { addItemsToCart } from "./actions/checkout";
|
|
30
|
-
*
|
|
31
|
-
* export const invoke = {
|
|
32
|
-
* vtex: {
|
|
33
|
-
* actions: {
|
|
34
|
-
* addItemsToCart: createInvokeFn(
|
|
35
|
-
* (input: { orderFormId: string; orderItems: CartItem[] }) =>
|
|
36
|
-
* addItemsToCart(input.orderFormId, input.orderItems),
|
|
37
|
-
* { unwrap: true },
|
|
38
|
-
* ),
|
|
39
|
-
* },
|
|
40
|
-
* },
|
|
41
|
-
* };
|
|
42
|
-
* ```
|
|
43
|
-
*
|
|
44
|
-
* The real, callable version of each action above only exists in the
|
|
45
|
-
* site's generated `src/server/invoke.gen.ts` (run `npm run generate:invoke`),
|
|
46
|
-
* which uses real top-level `createServerFn(...)` calls directly — never
|
|
47
|
-
* this function.
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
export interface InvokeFnOpts {
|
|
51
|
-
/**
|
|
52
|
-
* When true, extracts `.data` from the result before returning.
|
|
53
|
-
* Use for VTEX checkout functions that return VtexFetchResult<T>
|
|
54
|
-
* (i.e. `{ data: T, setCookies: string[] }`).
|
|
55
|
-
*/
|
|
56
|
-
unwrap?: boolean;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Template-only — see the module doc comment above. Throws if actually
|
|
61
|
-
* called at runtime, since that would mean something imported an
|
|
62
|
-
* `invoke.ts` template file directly instead of using the site's generated
|
|
63
|
-
* `invoke.gen.ts`, which is itself a bug worth surfacing loudly rather than
|
|
64
|
-
* silently returning wrong behavior.
|
|
65
|
-
*/
|
|
66
|
-
export function createInvokeFn<TInput, TOutput>(
|
|
67
|
-
_action: (input: TInput) => Promise<TOutput>,
|
|
68
|
-
_opts?: InvokeFnOpts,
|
|
69
|
-
): (ctx: { data: TInput }) => Promise<TOutput> {
|
|
70
|
-
return () => {
|
|
71
|
-
throw new Error(
|
|
72
|
-
"createInvokeFn() is a codegen-time-only template marker and was never meant to be called at " +
|
|
73
|
-
"runtime. If you're seeing this, something imported an apps-*/invoke.ts template file directly " +
|
|
74
|
-
"instead of using the site's generated src/server/invoke.gen.ts (run `npm run generate:invoke`).",
|
|
75
|
-
);
|
|
76
|
-
};
|
|
77
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Contract test for the public `deferredSectionLoader` wrapper
|
|
3
|
-
* (`@decocms/tanstack/sdk/deferredSectionLoader`).
|
|
4
|
-
*
|
|
5
|
-
* Before 7.7 this wrapper was unreachable from any public subpath, so
|
|
6
|
-
* migrated sites (lebiscuit, miess, granadobr, casaevideo) each carried a
|
|
7
|
-
* byte-identical local shim wrapping the public `loadDeferredSection`
|
|
8
|
-
* export. The cases below are derived from how those sites call it: it is
|
|
9
|
-
* passed verbatim as `<DecoPageRenderer loadDeferredSectionFn={...} />`,
|
|
10
|
-
* so it must (a) accept the renderer's flat argument object, (b) wrap it
|
|
11
|
-
* into the server function's `{ data }` envelope, and (c) pass the result
|
|
12
|
-
* (section or null) straight through.
|
|
13
|
-
*/
|
|
14
|
-
import type { ComponentProps } from "react";
|
|
15
|
-
import { beforeEach, describe, expect, expectTypeOf, it, vi } from "vitest";
|
|
16
|
-
|
|
17
|
-
vi.mock("../routes/cmsRoute", () => ({
|
|
18
|
-
loadDeferredSection: vi.fn(),
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
import type { DecoPageRenderer } from "../hooks/DecoPageRenderer";
|
|
22
|
-
import { loadDeferredSection } from "../routes/cmsRoute";
|
|
23
|
-
import { deferredSectionLoader } from "./deferredSectionLoader";
|
|
24
|
-
|
|
25
|
-
const mockedLoad = loadDeferredSection as unknown as ReturnType<typeof vi.fn>;
|
|
26
|
-
|
|
27
|
-
describe("deferredSectionLoader", () => {
|
|
28
|
-
beforeEach(() => {
|
|
29
|
-
mockedLoad.mockReset();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("wraps the flat argument object into the server function's { data } envelope", async () => {
|
|
33
|
-
const section = { component: "site/sections/Newsletter.tsx", props: {} };
|
|
34
|
-
mockedLoad.mockResolvedValue(section);
|
|
35
|
-
|
|
36
|
-
const result = await deferredSectionLoader({
|
|
37
|
-
component: "site/sections/Newsletter.tsx",
|
|
38
|
-
rawProps: { title: "Hi" },
|
|
39
|
-
pagePath: "/",
|
|
40
|
-
pageUrl: "https://example.com/?utm=x",
|
|
41
|
-
index: 4,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
expect(mockedLoad).toHaveBeenCalledTimes(1);
|
|
45
|
-
expect(mockedLoad).toHaveBeenCalledWith({
|
|
46
|
-
data: {
|
|
47
|
-
component: "site/sections/Newsletter.tsx",
|
|
48
|
-
rawProps: { title: "Hi" },
|
|
49
|
-
pagePath: "/",
|
|
50
|
-
pageUrl: "https://example.com/?utm=x",
|
|
51
|
-
index: 4,
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
expect(result).toBe(section);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("accepts the minimal call shape (component + pagePath only)", async () => {
|
|
58
|
-
mockedLoad.mockResolvedValue(null);
|
|
59
|
-
|
|
60
|
-
await deferredSectionLoader({
|
|
61
|
-
component: "site/sections/Footer.tsx",
|
|
62
|
-
pagePath: "/collections/sale",
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
expect(mockedLoad).toHaveBeenCalledWith({
|
|
66
|
-
data: {
|
|
67
|
-
component: "site/sections/Footer.tsx",
|
|
68
|
-
rawProps: undefined,
|
|
69
|
-
pagePath: "/collections/sale",
|
|
70
|
-
pageUrl: undefined,
|
|
71
|
-
index: undefined,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("passes a null resolution (cache miss / unresolvable section) straight through", async () => {
|
|
77
|
-
mockedLoad.mockResolvedValue(null);
|
|
78
|
-
|
|
79
|
-
const result = await deferredSectionLoader({
|
|
80
|
-
component: "site/sections/Missing.tsx",
|
|
81
|
-
pagePath: "/",
|
|
82
|
-
index: 0,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
expect(result).toBeNull();
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("matches DecoPageRenderer's loadDeferredSectionFn prop signature", () => {
|
|
89
|
-
// The whole point of the export: sites pass it verbatim as
|
|
90
|
-
// `<DecoPageRenderer loadDeferredSectionFn={deferredSectionLoader} />`.
|
|
91
|
-
type LoadFn = NonNullable<ComponentProps<typeof DecoPageRenderer>["loadDeferredSectionFn"]>;
|
|
92
|
-
expectTypeOf(deferredSectionLoader).toExtend<LoadFn>();
|
|
93
|
-
});
|
|
94
|
-
});
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pre-wrapped deferred section loader for IntersectionObserver-based
|
|
3
|
-
* rendering.
|
|
4
|
-
*
|
|
5
|
-
* Convenience wrapper around `loadDeferredSection` (the POST server
|
|
6
|
-
* function defined in `../routes/cmsRoute.ts`) that matches the
|
|
7
|
-
* `loadDeferredSectionFn` prop signature of `DecoPageRenderer`.
|
|
8
|
-
*
|
|
9
|
-
* Pass this directly to
|
|
10
|
-
* `<DecoPageRenderer loadDeferredSectionFn={deferredSectionLoader} />`
|
|
11
|
-
* to enable IntersectionObserver-based lazy loading of deferred sections.
|
|
12
|
-
*
|
|
13
|
-
* Publicly exported at `@decocms/tanstack/sdk/deferredSectionLoader`.
|
|
14
|
-
* Before 7.7 this wrapper existed only inside the package (exported from
|
|
15
|
-
* the internal `./routes` barrel but reachable from no public subpath), so
|
|
16
|
-
* migrated sites each carried a byte-identical local shim re-implementing
|
|
17
|
-
* it against the public `loadDeferredSection` export. Those shims can be
|
|
18
|
-
* deleted in favor of this subpath.
|
|
19
|
-
*
|
|
20
|
-
* @example Site's `src/routes/$.tsx`:
|
|
21
|
-
* ```tsx
|
|
22
|
-
* import { deferredSectionLoader } from "@decocms/tanstack/sdk/deferredSectionLoader";
|
|
23
|
-
*
|
|
24
|
-
* <DecoPageRenderer loadDeferredSectionFn={deferredSectionLoader} />
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
import type { ResolvedSection } from "@decocms/blocks/cms";
|
|
28
|
-
import { loadDeferredSection } from "../routes/cmsRoute";
|
|
29
|
-
|
|
30
|
-
export const deferredSectionLoader = async ({
|
|
31
|
-
component,
|
|
32
|
-
rawProps,
|
|
33
|
-
pagePath,
|
|
34
|
-
pageUrl,
|
|
35
|
-
index,
|
|
36
|
-
}: {
|
|
37
|
-
component: string;
|
|
38
|
-
rawProps?: Record<string, unknown>;
|
|
39
|
-
pagePath: string;
|
|
40
|
-
pageUrl?: string;
|
|
41
|
-
index?: number;
|
|
42
|
-
}): Promise<ResolvedSection | null> => {
|
|
43
|
-
return loadDeferredSection({
|
|
44
|
-
data: { component, rawProps, pagePath, pageUrl, index },
|
|
45
|
-
});
|
|
46
|
-
};
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
computeRevision,
|
|
4
|
-
getRevision,
|
|
5
|
-
type KVNamespace,
|
|
6
|
-
loadBlocks,
|
|
7
|
-
revisionKey,
|
|
8
|
-
setBlocks,
|
|
9
|
-
snapshotKey,
|
|
10
|
-
} from "@decocms/blocks/cms";
|
|
11
|
-
import {
|
|
12
|
-
__resetKvHydrationStateForTests,
|
|
13
|
-
ensureBlocksHydrated,
|
|
14
|
-
getDeploymentId,
|
|
15
|
-
isFastDeployEnabled,
|
|
16
|
-
maybePollRevision,
|
|
17
|
-
} from "./kvHydration";
|
|
18
|
-
|
|
19
|
-
const BUNDLED = { Site: { name: "bundled" } };
|
|
20
|
-
|
|
21
|
-
/** Deployment id used across the hydration tests. */
|
|
22
|
-
const ID = "sha-deadbeef";
|
|
23
|
-
const SNAP = snapshotKey(ID);
|
|
24
|
-
const REV = revisionKey(ID);
|
|
25
|
-
|
|
26
|
-
/** KV stub that counts get() calls so we can assert throttling / single-load. */
|
|
27
|
-
function makeKV(initial: Record<string, string> = {}) {
|
|
28
|
-
const store = new Map<string, string>(Object.entries(initial));
|
|
29
|
-
let getCalls = 0;
|
|
30
|
-
const kv: KVNamespace = {
|
|
31
|
-
get: (k) => {
|
|
32
|
-
getCalls++;
|
|
33
|
-
return Promise.resolve(store.get(k) ?? null);
|
|
34
|
-
},
|
|
35
|
-
put: (k, v) => {
|
|
36
|
-
store.set(k, v);
|
|
37
|
-
return Promise.resolve();
|
|
38
|
-
},
|
|
39
|
-
delete: (k) => {
|
|
40
|
-
store.delete(k);
|
|
41
|
-
return Promise.resolve();
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
return { kv, store, getCalls: () => getCalls };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function snapshotEnv(blocks: Record<string, unknown>, extra: Record<string, unknown> = {}) {
|
|
48
|
-
const { kv, ...rest } = makeKV({
|
|
49
|
-
[SNAP]: JSON.stringify(blocks),
|
|
50
|
-
[REV]: computeRevision(blocks),
|
|
51
|
-
});
|
|
52
|
-
// DECO_FAST_DEPLOY="1" is the explicit opt-in required alongside the binding;
|
|
53
|
-
// DECO_DEPLOYMENT_ID scopes the read to this deployment's keyed snapshot.
|
|
54
|
-
return {
|
|
55
|
-
env: { DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID, ...extra },
|
|
56
|
-
kv,
|
|
57
|
-
...rest,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Collects ctx.waitUntil promises so tests can await background polls. */
|
|
62
|
-
function makeCtx() {
|
|
63
|
-
const promises: Promise<unknown>[] = [];
|
|
64
|
-
return {
|
|
65
|
-
ctx: { waitUntil: (p: Promise<unknown>) => promises.push(p) },
|
|
66
|
-
settle: () => Promise.allSettled(promises),
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
beforeEach(() => {
|
|
71
|
-
__resetKvHydrationStateForTests();
|
|
72
|
-
setBlocks({ ...BUNDLED }); // reset in-memory decofile to a known bundled state
|
|
73
|
-
vi.restoreAllMocks();
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
describe("isFastDeployEnabled", () => {
|
|
77
|
-
it("is false without a KV binding even when the flag is set", () => {
|
|
78
|
-
expect(isFastDeployEnabled({ DECO_FAST_DEPLOY: "1" })).toBe(false);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("is false when a non-KV value is named DECO_KV", () => {
|
|
82
|
-
expect(isFastDeployEnabled({ DECO_KV: "some-secret-string", DECO_FAST_DEPLOY: "1" })).toBe(false);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("is false when bound but DECO_FAST_DEPLOY is not set (explicit opt-in required)", () => {
|
|
86
|
-
const { kv } = makeKV();
|
|
87
|
-
expect(isFastDeployEnabled({ DECO_KV: kv })).toBe(false);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("is true when bound AND DECO_FAST_DEPLOY=1", () => {
|
|
91
|
-
const { kv } = makeKV();
|
|
92
|
-
expect(isFastDeployEnabled({ DECO_KV: kv, DECO_FAST_DEPLOY: "1" })).toBe(true);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("accepts DECO_FAST_DEPLOY=true as well", () => {
|
|
96
|
-
const { kv } = makeKV();
|
|
97
|
-
expect(isFastDeployEnabled({ DECO_KV: kv, DECO_FAST_DEPLOY: "true" })).toBe(true);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("is false when DECO_FAST_DEPLOY=0 even if bound", () => {
|
|
101
|
-
const { kv } = makeKV();
|
|
102
|
-
expect(isFastDeployEnabled({ DECO_KV: kv, DECO_FAST_DEPLOY: "0" })).toBe(false);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
describe("ensureBlocksHydrated", () => {
|
|
107
|
-
it("is a no-op when fast-deploy is disabled (keeps bundled blocks)", async () => {
|
|
108
|
-
await ensureBlocksHydrated({});
|
|
109
|
-
expect(loadBlocks()).toEqual(BUNDLED);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it("swaps the in-memory decofile with the KV snapshot", async () => {
|
|
113
|
-
const kvBlocks = { Site: { name: "from-kv" }, "pages-home": { path: "/" } };
|
|
114
|
-
const { env } = snapshotEnv(kvBlocks);
|
|
115
|
-
await ensureBlocksHydrated(env);
|
|
116
|
-
expect(loadBlocks()).toEqual(kvBlocks);
|
|
117
|
-
expect(getRevision()).toBe(computeRevision(kvBlocks));
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it("loads only once even across concurrent first requests", async () => {
|
|
121
|
-
const kvBlocks = { Site: { name: "from-kv" } };
|
|
122
|
-
const { env, getCalls } = snapshotEnv(kvBlocks);
|
|
123
|
-
await Promise.all([
|
|
124
|
-
ensureBlocksHydrated(env),
|
|
125
|
-
ensureBlocksHydrated(env),
|
|
126
|
-
ensureBlocksHydrated(env),
|
|
127
|
-
]);
|
|
128
|
-
// SNAPSHOT + REVISION = 2 gets for a single load (not 6).
|
|
129
|
-
expect(getCalls()).toBe(2);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it("keeps the bundled snapshot when this deployment's snapshot key is absent", async () => {
|
|
133
|
-
const { kv } = makeKV({ [REV]: "r" }); // revision present, but no SNAPSHOT
|
|
134
|
-
await ensureBlocksHydrated({ DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID });
|
|
135
|
-
expect(loadBlocks()).toEqual(BUNDLED);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it("keeps bundled (never touches KV) when no deployment id resolves", async () => {
|
|
139
|
-
const kvBlocks = { Site: { name: "from-kv" } };
|
|
140
|
-
const { kv, getCalls } = makeKV({ [SNAP]: JSON.stringify(kvBlocks) });
|
|
141
|
-
// Fast-deploy enabled + bound, but no DECO_DEPLOYMENT_ID / BUILD_HASH.
|
|
142
|
-
await ensureBlocksHydrated({ DECO_KV: kv, DECO_FAST_DEPLOY: "1" });
|
|
143
|
-
expect(loadBlocks()).toEqual(BUNDLED);
|
|
144
|
-
expect(getCalls()).toBe(0);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("falls back to bundled (and does not throw) when KV errors", async () => {
|
|
148
|
-
const kv: KVNamespace = {
|
|
149
|
-
get: () => Promise.reject(new Error("KV down")),
|
|
150
|
-
put: () => Promise.resolve(),
|
|
151
|
-
delete: () => Promise.resolve(),
|
|
152
|
-
};
|
|
153
|
-
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
154
|
-
await expect(
|
|
155
|
-
ensureBlocksHydrated({ DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID }),
|
|
156
|
-
).resolves.toBeUndefined();
|
|
157
|
-
expect(loadBlocks()).toEqual(BUNDLED);
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
describe("getDeploymentId", () => {
|
|
162
|
-
it("prefers DECO_DEPLOYMENT_ID", () => {
|
|
163
|
-
expect(getDeploymentId({ DECO_DEPLOYMENT_ID: "a", BUILD_HASH: "b" })).toBe("a");
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it("falls back to BUILD_HASH", () => {
|
|
167
|
-
expect(getDeploymentId({ BUILD_HASH: "b" })).toBe("b");
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it("is null when nothing resolves", () => {
|
|
171
|
-
expect(getDeploymentId({})).toBeNull();
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
describe("maybePollRevision", () => {
|
|
176
|
-
it("is a no-op before cold-start hydration has run", async () => {
|
|
177
|
-
const { env, getCalls } = snapshotEnv({ Site: { name: "x" } });
|
|
178
|
-
const { ctx, settle } = makeCtx();
|
|
179
|
-
maybePollRevision(env, ctx); // kvHydrated is false
|
|
180
|
-
await settle();
|
|
181
|
-
expect(getCalls()).toBe(0);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it("reloads the decofile when the KV revision changed", async () => {
|
|
185
|
-
const initial = { Site: { name: "v1" } };
|
|
186
|
-
const { env, store } = snapshotEnv(initial);
|
|
187
|
-
await ensureBlocksHydrated(env);
|
|
188
|
-
expect(loadBlocks()).toEqual(initial);
|
|
189
|
-
|
|
190
|
-
// Simulate a publish from another isolate: KV now holds v2.
|
|
191
|
-
const updated = { Site: { name: "v2" }, "pages-x": { path: "/x" } };
|
|
192
|
-
store.set(SNAP, JSON.stringify(updated));
|
|
193
|
-
store.set(REV, computeRevision(updated));
|
|
194
|
-
|
|
195
|
-
const { ctx, settle } = makeCtx();
|
|
196
|
-
maybePollRevision(env, ctx);
|
|
197
|
-
await settle();
|
|
198
|
-
expect(loadBlocks()).toEqual(updated);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it("throttles to one probe per interval", async () => {
|
|
202
|
-
const { env, getCalls } = snapshotEnv({ Site: { name: "x" } });
|
|
203
|
-
await ensureBlocksHydrated(env);
|
|
204
|
-
const callsAfterHydrate = getCalls();
|
|
205
|
-
|
|
206
|
-
const { ctx, settle } = makeCtx();
|
|
207
|
-
maybePollRevision(env, ctx); // fires (revision unchanged → 1 get)
|
|
208
|
-
maybePollRevision(env, ctx); // throttled → no get
|
|
209
|
-
maybePollRevision(env, ctx); // throttled → no get
|
|
210
|
-
await settle();
|
|
211
|
-
|
|
212
|
-
// Exactly one extra getRevision() beyond the hydrate calls.
|
|
213
|
-
expect(getCalls()).toBe(callsAfterHydrate + 1);
|
|
214
|
-
});
|
|
215
|
-
});
|
package/src/sdk/kvHydration.ts
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Fast-deploy KV hydration — the runtime read path.
|
|
3
|
-
*
|
|
4
|
-
* Bridges a Cloudflare KV namespace to the in-memory decofile so CMS content
|
|
5
|
-
* edits propagate WITHOUT a `wrangler deploy`. Two entry points, both called
|
|
6
|
-
* from `workerEntry.ts`:
|
|
7
|
-
*
|
|
8
|
-
* - `ensureBlocksHydrated(env, ctx)` — on the FIRST request per isolate, load
|
|
9
|
-
* the whole snapshot from KV and swap it in via `setBlocks()`. Awaited, so it
|
|
10
|
-
* adds one ~10-30ms cold-start hit per isolate but guarantees fresh content
|
|
11
|
-
* (the bundled `blocks.gen` snapshot is frozen at the last code deploy).
|
|
12
|
-
*
|
|
13
|
-
* - `maybePollRevision(env, ctx)` — on EVERY request, opportunistically (gated
|
|
14
|
-
* to once per `POLL_INTERVAL_MS`) probe `index:revision` via `ctx.waitUntil`
|
|
15
|
-
* so it never blocks the response; reload + swap when it changed.
|
|
16
|
-
*
|
|
17
|
-
* Everything is a no-op unless fast-deploy is enabled (`isFastDeployEnabled`),
|
|
18
|
-
* so non-migrated sites behave exactly as before.
|
|
19
|
-
*
|
|
20
|
-
* Why whole-snapshot swap (not per-block async): the resolver reads
|
|
21
|
-
* `loadBlocks()` synchronously in dozens of places. Loading the entire decofile
|
|
22
|
-
* once and swapping the map keeps that hot path synchronous — KV is touched
|
|
23
|
-
* only on cold start and during the throttled poll. Mirrors the
|
|
24
|
-
* `DecofileProvider` pattern from the deco-cx/deco Fresh runtime.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import { getDeploymentId, getRevision, setBlocks } from "@decocms/blocks/cms";
|
|
28
|
-
import { KVBlockSource } from "../cms/kvBlockSource";
|
|
29
|
-
import type { KVNamespace } from "@decocms/blocks/cms";
|
|
30
|
-
import { setSpanAttribute } from "@decocms/blocks/sdk/observability";
|
|
31
|
-
|
|
32
|
-
/** How often (ms) an isolate re-probes its `index:revision:<id>`. */
|
|
33
|
-
export const POLL_INTERVAL_MS = 10_000;
|
|
34
|
-
|
|
35
|
-
/** KV binding name expected on the Worker `env`. */
|
|
36
|
-
export const KV_BINDING = "DECO_KV";
|
|
37
|
-
|
|
38
|
-
/** Opt-in env var — set to "1" (or "true") to enable fast-deploy. The DECO_KV
|
|
39
|
-
* binding must also be present. */
|
|
40
|
-
export const FAST_DEPLOY_ENV = "DECO_FAST_DEPLOY";
|
|
41
|
-
|
|
42
|
-
// Deployment-id resolution lives in `@decocms/blocks/cms` (next to the key
|
|
43
|
-
// builders it feeds) so the runtime read path and the admin write-through
|
|
44
|
-
// resolve it identically. Re-exported for the existing call sites/tests.
|
|
45
|
-
export { getDeploymentId };
|
|
46
|
-
|
|
47
|
-
// globalThis-backed state so all Vite server-function split-module copies share
|
|
48
|
-
// the same hydration flags (same pattern as `loader.ts`).
|
|
49
|
-
const G = globalThis as unknown as {
|
|
50
|
-
__deco?: {
|
|
51
|
-
kvHydrated?: boolean;
|
|
52
|
-
kvHydration?: Promise<void> | null;
|
|
53
|
-
kvLastPolledAt?: number;
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
if (!G.__deco) G.__deco = {};
|
|
57
|
-
|
|
58
|
-
/** Minimal Cloudflare ExecutionContext shape (matches workerEntry.ts). */
|
|
59
|
-
interface ExecutionContextLike {
|
|
60
|
-
waitUntil(promise: Promise<unknown>): void;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
type Env = Record<string, unknown>;
|
|
64
|
-
|
|
65
|
-
function getKV(env: Env): KVNamespace | null {
|
|
66
|
-
const binding = env[KV_BINDING];
|
|
67
|
-
// Duck-type the binding: a real KVNamespace has get/put. Guards against a
|
|
68
|
-
// string/secret accidentally named DECO_KV.
|
|
69
|
-
if (binding && typeof (binding as KVNamespace).get === "function") {
|
|
70
|
-
return binding as KVNamespace;
|
|
71
|
-
}
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Fast-deploy is active only when BOTH hold: `DECO_FAST_DEPLOY` is set to "1"
|
|
77
|
-
* (or "true") — an explicit, per-site opt-in — AND the `DECO_KV` binding is
|
|
78
|
-
* present. Either missing ⇒ bundled-snapshot behavior, identical to
|
|
79
|
-
* pre-fast-deploy. Requiring the explicit flag means simply binding a KV
|
|
80
|
-
* namespace can't silently flip a site onto the KV read/write path.
|
|
81
|
-
*/
|
|
82
|
-
export function isFastDeployEnabled(env: Env): boolean {
|
|
83
|
-
const flag = env[FAST_DEPLOY_ENV];
|
|
84
|
-
if (flag !== "1" && flag !== "true") return false;
|
|
85
|
-
return getKV(env) !== null;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Resolve the KV namespace for the write-through path, or `null` when
|
|
90
|
-
* fast-deploy is disabled. Shared by `decofile.ts` so the enablement rule
|
|
91
|
-
* lives in exactly one place.
|
|
92
|
-
*/
|
|
93
|
-
export function getFastDeployKV(env: Env): KVNamespace | null {
|
|
94
|
-
if (!isFastDeployEnabled(env)) return null;
|
|
95
|
-
return getKV(env);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Cold-start hydration. Awaits the KV snapshot once per isolate and swaps it
|
|
100
|
-
* into the in-memory block map. Concurrent first requests share a single
|
|
101
|
-
* in-flight load. On any error (KV outage, bad JSON) we keep the bundled
|
|
102
|
-
* snapshot and mark hydration done — recovery happens via `maybePollRevision`
|
|
103
|
-
* once KV is reachable again (its revision will differ from the bundled one).
|
|
104
|
-
*/
|
|
105
|
-
export function ensureBlocksHydrated(env: Env, _ctx?: ExecutionContextLike): Promise<void> {
|
|
106
|
-
if (!isFastDeployEnabled(env)) return Promise.resolve();
|
|
107
|
-
if (G.__deco!.kvHydrated) return Promise.resolve();
|
|
108
|
-
if (G.__deco!.kvHydration) return G.__deco!.kvHydration;
|
|
109
|
-
|
|
110
|
-
const kv = getKV(env);
|
|
111
|
-
if (!kv) return Promise.resolve();
|
|
112
|
-
|
|
113
|
-
// No resolvable deployment id ⇒ keep the bundled snapshot (this build's own
|
|
114
|
-
// content). Never read another deployment's key.
|
|
115
|
-
const deploymentId = getDeploymentId(env);
|
|
116
|
-
if (!deploymentId) {
|
|
117
|
-
setSpanAttribute("deco.block.source", "bundled");
|
|
118
|
-
G.__deco!.kvHydrated = true;
|
|
119
|
-
return Promise.resolve();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const load = (async () => {
|
|
123
|
-
try {
|
|
124
|
-
const snapshot = await new KVBlockSource(kv, deploymentId).loadSnapshot();
|
|
125
|
-
if (snapshot) {
|
|
126
|
-
setBlocks(snapshot.blocks);
|
|
127
|
-
setSpanAttribute("deco.block.source", "kv");
|
|
128
|
-
} else {
|
|
129
|
-
setSpanAttribute("deco.block.source", "bundled");
|
|
130
|
-
}
|
|
131
|
-
} catch (e) {
|
|
132
|
-
// Non-fatal: serve the bundled snapshot. The poll loop recovers later.
|
|
133
|
-
console.warn("[CMS/KV] cold-start hydration failed, using bundled snapshot:", e);
|
|
134
|
-
setSpanAttribute("deco.block.source", "bundled");
|
|
135
|
-
} finally {
|
|
136
|
-
G.__deco!.kvHydrated = true;
|
|
137
|
-
G.__deco!.kvHydration = null;
|
|
138
|
-
}
|
|
139
|
-
})();
|
|
140
|
-
|
|
141
|
-
G.__deco!.kvHydration = load;
|
|
142
|
-
return load;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Opportunistic revision poll. Throttled to once per `POLL_INTERVAL_MS` and run
|
|
147
|
-
* through `ctx.waitUntil` so it never adds latency to the response. Reloads the
|
|
148
|
-
* snapshot and swaps it in when KV's revision differs from the in-memory one.
|
|
149
|
-
*/
|
|
150
|
-
export function maybePollRevision(env: Env, ctx?: ExecutionContextLike): void {
|
|
151
|
-
if (!isFastDeployEnabled(env)) return;
|
|
152
|
-
if (!G.__deco!.kvHydrated) return; // wait until cold-start hydration finished
|
|
153
|
-
|
|
154
|
-
const now = Date.now();
|
|
155
|
-
if (now - (G.__deco!.kvLastPolledAt ?? 0) < POLL_INTERVAL_MS) return;
|
|
156
|
-
G.__deco!.kvLastPolledAt = now;
|
|
157
|
-
|
|
158
|
-
const kv = getKV(env);
|
|
159
|
-
if (!kv) return;
|
|
160
|
-
|
|
161
|
-
const deploymentId = getDeploymentId(env);
|
|
162
|
-
if (!deploymentId) return; // bundled-only; nothing to poll
|
|
163
|
-
|
|
164
|
-
const poll = pollRevisionOnce(kv, deploymentId);
|
|
165
|
-
// Prefer waitUntil so the work outlives the response; fall back to a
|
|
166
|
-
// fire-and-forget promise (dev / tests) with its rejection swallowed.
|
|
167
|
-
if (ctx?.waitUntil) ctx.waitUntil(poll);
|
|
168
|
-
else void poll.catch(() => {});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async function pollRevisionOnce(kv: KVNamespace, deploymentId: string): Promise<void> {
|
|
172
|
-
try {
|
|
173
|
-
const source = new KVBlockSource(kv, deploymentId);
|
|
174
|
-
const remoteRevision = await source.getRevision();
|
|
175
|
-
if (!remoteRevision || remoteRevision === getRevision()) return;
|
|
176
|
-
|
|
177
|
-
const snapshot = await source.loadSnapshot();
|
|
178
|
-
if (snapshot) {
|
|
179
|
-
setBlocks(snapshot.blocks);
|
|
180
|
-
console.info(`[CMS/KV] decofile refreshed → revision ${snapshot.revision}`);
|
|
181
|
-
}
|
|
182
|
-
} catch (e) {
|
|
183
|
-
// Swallow — a failed poll must never affect the request. Next tick retries.
|
|
184
|
-
console.warn("[CMS/KV] revision poll failed:", e);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/** Test-only: reset the isolate-level hydration flags. */
|
|
189
|
-
export function __resetKvHydrationStateForTests(): void {
|
|
190
|
-
G.__deco!.kvHydrated = false;
|
|
191
|
-
G.__deco!.kvHydration = null;
|
|
192
|
-
G.__deco!.kvLastPolledAt = 0;
|
|
193
|
-
}
|