@decocms/blocks 8.0.0 → 8.1.0-next.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/bin/deco.js +13 -0
- package/package.json +39 -3
- package/src/cms/applySectionConventions.ts +26 -2
- package/src/cms/blockSource.ts +37 -0
- package/src/cms/client.ts +5 -0
- package/src/cms/deferredTrigger.ts +58 -0
- package/src/cms/draftSource.ts +171 -57
- package/src/cms/index.ts +32 -3
- package/src/cms/loader.ts +318 -59
- package/src/cms/registry.ts +22 -0
- package/src/cms/renderJson.ts +179 -0
- package/src/cms/resolve.ts +396 -95
- package/src/cms/schema.ts +9 -5
- package/src/cms/sectionLoaders.ts +106 -56
- package/src/hooks/Image.tsx +70 -6
- package/src/hooks/LiveControls.tsx +30 -8
- package/src/hooks/Stats.tsx +134 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useLoadMore.ts +100 -0
- package/src/index.ts +37 -0
- package/src/middleware/observability.ts +173 -16
- package/src/preview/DraftPreviewBadge.tsx +306 -0
- package/src/preview/decoMark.ts +11 -0
- package/src/preview/index.ts +8 -0
- package/src/protocol/assets.ts +124 -0
- package/src/protocol/canonical.ts +7 -0
- package/src/protocol/ciphertext.ts +6 -0
- package/src/protocol/client.ts +170 -0
- package/src/protocol/conformance/cases/assets.ts +81 -0
- package/src/protocol/conformance/cases/guards.ts +314 -0
- package/src/protocol/conformance/cases/reads.ts +168 -0
- package/src/protocol/conformance/cases/types.ts +9 -0
- package/src/protocol/conformance/cases/wire.ts +138 -0
- package/src/protocol/conformance/cases/writes.ts +345 -0
- package/src/protocol/conformance/contentHashFixtures.ts +79 -0
- package/src/protocol/conformance/context.ts +222 -0
- package/src/protocol/conformance/index.ts +140 -0
- package/src/protocol/errors.ts +142 -0
- package/src/protocol/index.ts +39 -0
- package/src/protocol/keys.ts +279 -0
- package/src/protocol/params.ts +67 -0
- package/src/protocol/secrets.ts +231 -0
- package/src/protocol/server/assets.ts +119 -0
- package/src/protocol/server/auth.ts +49 -0
- package/src/protocol/server/bodyCache.ts +84 -0
- package/src/protocol/server/content.ts +175 -0
- package/src/protocol/server/core.ts +112 -0
- package/src/protocol/server/handler.ts +102 -0
- package/src/protocol/server/http.ts +139 -0
- package/src/protocol/server/index.ts +11 -0
- package/src/protocol/server/methods/apply.ts +341 -0
- package/src/protocol/server/methods/read.ts +156 -0
- package/src/protocol/server/rpc.ts +136 -0
- package/src/protocol/storage/fs/hash.ts +21 -0
- package/src/protocol/storage/fs/index.ts +338 -0
- package/src/protocol/storage/fs/lock.ts +68 -0
- package/src/protocol/storage/fs/transaction.ts +183 -0
- package/src/protocol/storage/fs/unsupported.ts +15 -0
- package/src/protocol/storage/memory.ts +238 -0
- package/src/protocol/storage.ts +182 -0
- package/src/protocol/types.ts +172 -0
- package/src/sdk/abTesting.ts +60 -3
- package/src/sdk/analytics.ts +72 -2
- package/src/sdk/cacheHeaders.ts +210 -10
- package/src/sdk/cacheStorage.ts +287 -0
- package/src/sdk/cachedLoader.ts +143 -90
- package/src/sdk/crypto.ts +16 -2
- package/src/sdk/deepOmit.ts +52 -0
- package/src/sdk/encoding.ts +2 -5
- package/src/sdk/experiments.ts +314 -0
- package/src/sdk/fetchCache.ts +269 -0
- package/src/sdk/fetchTimeout.ts +58 -0
- package/src/sdk/flags.ts +38 -8
- package/src/sdk/index.ts +7 -0
- package/src/sdk/instrumentedFetch.ts +25 -2
- package/src/sdk/nonce.ts +46 -0
- package/src/sdk/normalizeUrls.ts +2 -2
- package/src/sdk/observability.ts +8 -1
- package/src/sdk/otel.ts +131 -25
- package/src/sdk/otelHttpLog.ts +4 -4
- package/src/sdk/otelHttpMeter.ts +39 -13
- package/src/sdk/otelHttpTracer.ts +4 -4
- package/src/sdk/redirects.ts +113 -5
- package/src/sdk/responseCache.ts +82 -0
- package/src/sdk/urlUtils.ts +49 -0
- package/src/sdk/useSuggestions.ts +1 -1
- package/src/v8/analytics.ts +50 -0
- package/src/v8/background.ts +107 -0
- package/src/v8/builtins/data.ts +23 -0
- package/src/v8/builtins/index.ts +26 -0
- package/src/v8/builtins/lazy.ts +23 -0
- package/src/v8/builtins/legacy.ts +65 -0
- package/src/v8/builtins/matchers.ts +31 -0
- package/src/v8/builtins/multivariate.ts +25 -0
- package/src/v8/builtins/secret.ts +82 -0
- package/src/v8/canonical.ts +117 -0
- package/src/v8/ciphertext.ts +158 -0
- package/src/v8/cli/builtins.ts +20 -0
- package/src/v8/cli/check/index.ts +774 -0
- package/src/v8/cli/check/messages.ts +168 -0
- package/src/v8/cli/content.ts +287 -0
- package/src/v8/cli/index.ts +17 -0
- package/src/v8/cli/log.ts +18 -0
- package/src/v8/cli/main.ts +12 -0
- package/src/v8/cli/root.ts +66 -0
- package/src/v8/cli/run.ts +236 -0
- package/src/v8/cli/schema/builtinSchemas.ts +236 -0
- package/src/v8/cli/schema/generate.ts +507 -0
- package/src/v8/cli/schema/index.ts +63 -0
- package/src/v8/cli/schema/tsProgram.ts +497 -0
- package/src/v8/cli/schema/typeToSchema.ts +689 -0
- package/src/v8/cli/serve/server.ts +303 -0
- package/src/v8/cli/watch.ts +86 -0
- package/src/v8/client.ts +147 -0
- package/src/v8/cms.ts +237 -0
- package/src/v8/content.ts +195 -0
- package/src/v8/draft.ts +136 -0
- package/src/v8/errors.ts +59 -0
- package/src/v8/fetch.ts +152 -0
- package/src/v8/identity.ts +65 -0
- package/src/v8/index.ts +39 -0
- package/src/v8/json.ts +74 -0
- package/src/v8/matchRoute.ts +204 -0
- package/src/v8/remoteLoader.ts +282 -0
- package/src/v8/resolver.ts +278 -0
- package/src/v8/secrets.ts +17 -0
- package/src/v8/telemetry.ts +533 -0
- package/src/v8/types.ts +240 -0
- package/src/cms/blockSource.test.ts +0 -76
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/draftSource.test.ts +0 -343
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loader.test.ts +0 -200
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/resolve.test.ts +0 -980
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaders.test.ts +0 -533
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/flags.test.ts +0 -148
- package/src/hooks/Image.test.ts +0 -100
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/override.test.ts +0 -205
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/observability.test.ts +0 -242
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/sdk/abTesting.test.ts +0 -326
- package/src/sdk/cacheHeaders.test.ts +0 -115
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/otel.test.ts +0 -526
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpMeter.test.ts +0 -292
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/tsconfig.json +0 -7
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* renderJson — structured JSON projection of a resolved page for the mobile app.
|
|
3
|
+
*
|
|
4
|
+
* A section controls how it renders to JSON through a recognized named export
|
|
5
|
+
* `renderJson` (same convention family as `loader`/`action`/`LoadingFallback`):
|
|
6
|
+
*
|
|
7
|
+
* - `export const renderJson = false` — drop the section from the JSON entirely
|
|
8
|
+
* (web-only: theme/analytics/SEO/scripts). It also short-circuits the loader
|
|
9
|
+
* in {@link runSectionLoaders} (no data fetch for a dropped section).
|
|
10
|
+
* - `export const renderJson = (props) => projected` — a pure projection applied
|
|
11
|
+
* to the resolved props before serialization (typically {@link deepOmit} of
|
|
12
|
+
* internal/SEO/store-config fields).
|
|
13
|
+
* - no export — the section serializes with its full resolved props.
|
|
14
|
+
*
|
|
15
|
+
* The whole-page envelope (`?renderJson`) is `{ name, path, sections }`, where
|
|
16
|
+
* each section is `{ component, props }`. This is the lean shape — no admin
|
|
17
|
+
* `resolveChain`/metadata (unlike the legacy `?asJson`).
|
|
18
|
+
*
|
|
19
|
+
* Ported from the deco-runtime fork's `serialize-section.ts`, adapted for the
|
|
20
|
+
* TanStack pipeline: sections are resolved EAGERLY before serialization, so this
|
|
21
|
+
* serializer has no lazy-placeholder (`{ component, lazyUrl }`) branch — the
|
|
22
|
+
* mobile app receives the whole page in one request.
|
|
23
|
+
*
|
|
24
|
+
* ponytail: no nested section-in-section recursion (composite/layout sections).
|
|
25
|
+
* deepOmit handles trimming within a single section's data; add a recursive walk
|
|
26
|
+
* here if a composite section must project its child sections.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
// deno-lint-ignore-style any: a projection accepts the section's own resolved props.
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
export type RenderJson = ((props: any) => Record<string, unknown>) | false;
|
|
32
|
+
|
|
33
|
+
export interface RenderJsonModule {
|
|
34
|
+
renderJson?: RenderJson;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A resolved section after its loader ran: component id + enriched props. */
|
|
38
|
+
export interface SerializableSection {
|
|
39
|
+
component: string;
|
|
40
|
+
props?: Record<string, unknown>;
|
|
41
|
+
/** Position in the page's flat section list (used to interleave with lazy ones). */
|
|
42
|
+
index?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A deferred section that isn't resolved eagerly — emitted as a lazy placeholder. */
|
|
46
|
+
export interface DeferredRef {
|
|
47
|
+
component: string;
|
|
48
|
+
index: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** An eager section carries `props`; a lazy one carries a `lazyUrl` to fetch it. */
|
|
52
|
+
export type SerializedSection =
|
|
53
|
+
| { component: string; props: Record<string, unknown> }
|
|
54
|
+
| { component: string; lazyUrl: string };
|
|
55
|
+
|
|
56
|
+
export interface SerializeOptions {
|
|
57
|
+
/**
|
|
58
|
+
* Resolves a section module by resolveType so the serializer can honor its
|
|
59
|
+
* `renderJson` export. When omitted, every section keeps its full props.
|
|
60
|
+
*/
|
|
61
|
+
getSectionModule?: (component: string) => RenderJsonModule | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* App-owned sections excluded from the response, matched by resolveType
|
|
64
|
+
* suffix (e.g. "SeoV2.tsx"). Blank entries are ignored — a "" suffix would
|
|
65
|
+
* endsWith-match every section.
|
|
66
|
+
*/
|
|
67
|
+
sectionsToIgnore?: string[];
|
|
68
|
+
/**
|
|
69
|
+
* Deferred sections to emit as `{ component, lazyUrl }` placeholders,
|
|
70
|
+
* interleaved with the eager ones by `index`. Dropped sections
|
|
71
|
+
* (`renderJson === false` / `sectionsToIgnore`) are omitted here too. When
|
|
72
|
+
* set, the output is ordered by `index`.
|
|
73
|
+
*/
|
|
74
|
+
deferred?: DeferredRef[];
|
|
75
|
+
/** Builds the lazy-fetch URL for a deferred section. Required when `deferred` is set. */
|
|
76
|
+
lazyUrlFor?: (ref: DeferredRef) => string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Projects a page's sections into the lean renderJson shape. Eager sections
|
|
81
|
+
* become `{ component, props }` (a `renderJson` function projects the props;
|
|
82
|
+
* otherwise props pass through). Deferred sections become
|
|
83
|
+
* `{ component, lazyUrl }`, interleaved by `index`. A section is dropped when
|
|
84
|
+
* its resolveType matches a `sectionsToIgnore` suffix OR its module exports
|
|
85
|
+
* `renderJson === false` (for a deferred section, dropping avoids emitting a
|
|
86
|
+
* lazyUrl the app would fetch for nothing).
|
|
87
|
+
*/
|
|
88
|
+
/**
|
|
89
|
+
* Keys the section loader / mixins inject into props at request time — page
|
|
90
|
+
* context (`__pageUrl`/`__pagePath`, `runSectionLoaders`) and device
|
|
91
|
+
* (`device`/`isMobile`/`currentSearchParam`, the withDevice/withMobile/
|
|
92
|
+
* withSearchParam mixins). They are request-derived framework internals, NOT
|
|
93
|
+
* content, so they are stripped from the JSON: they would bust the ETag per
|
|
94
|
+
* request/device and leak internals the mobile app shouldn't cache. Any
|
|
95
|
+
* `__`-prefixed key is treated as internal by convention (mirrors `__section`).
|
|
96
|
+
*/
|
|
97
|
+
const FRAMEWORK_INJECTED_KEYS = new Set(["device", "isMobile", "currentSearchParam"]);
|
|
98
|
+
|
|
99
|
+
function stripFrameworkKeys(props: Record<string, unknown>): Record<string, unknown> {
|
|
100
|
+
let out: Record<string, unknown> | null = null;
|
|
101
|
+
for (const k of Object.keys(props)) {
|
|
102
|
+
if (k.startsWith("__") || FRAMEWORK_INJECTED_KEYS.has(k)) {
|
|
103
|
+
out ??= { ...props };
|
|
104
|
+
delete out[k];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return out ?? props;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function serializeRenderJson(
|
|
111
|
+
sections: SerializableSection[],
|
|
112
|
+
opts: SerializeOptions = {},
|
|
113
|
+
): SerializedSection[] {
|
|
114
|
+
const ignore = (opts.sectionsToIgnore ?? [])
|
|
115
|
+
.map((s) => s.trim())
|
|
116
|
+
.filter((s) => s.length > 0);
|
|
117
|
+
|
|
118
|
+
const renderJsonOf = (component: string): RenderJson | undefined =>
|
|
119
|
+
opts.getSectionModule?.(component)?.renderJson;
|
|
120
|
+
|
|
121
|
+
const isDropped = (component: string): boolean =>
|
|
122
|
+
ignore.some((suffix) => component.endsWith(suffix)) ||
|
|
123
|
+
renderJsonOf(component) === false;
|
|
124
|
+
|
|
125
|
+
const eager = sections
|
|
126
|
+
.filter((s) => !isDropped(s.component))
|
|
127
|
+
.map((section) => {
|
|
128
|
+
const rj = renderJsonOf(section.component);
|
|
129
|
+
const projected = typeof rj === "function" ? rj(section.props ?? {}) : section.props ?? {};
|
|
130
|
+
const props = stripFrameworkKeys(projected);
|
|
131
|
+
return { index: section.index, out: { component: section.component, props } as SerializedSection };
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// No deferred sections → preserve input order (backward compatible).
|
|
135
|
+
if (!opts.deferred || opts.deferred.length === 0) {
|
|
136
|
+
return eager.map((e) => e.out);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const lazyUrlFor = opts.lazyUrlFor;
|
|
140
|
+
const lazy = opts.deferred
|
|
141
|
+
.filter((ref) => !isDropped(ref.component) && !!lazyUrlFor)
|
|
142
|
+
.map((ref) => ({
|
|
143
|
+
index: ref.index,
|
|
144
|
+
out: { component: ref.component, lazyUrl: lazyUrlFor!(ref) } as SerializedSection,
|
|
145
|
+
}));
|
|
146
|
+
|
|
147
|
+
// Interleave eager + lazy by their position in the page's flat section list.
|
|
148
|
+
return [...eager, ...lazy].sort((a, b) => (a.index ?? 0) - (b.index ?? 0)).map((e) => e.out);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* A resolved Deco `Secret` is `{ get: () => string | null }` (apps-website
|
|
153
|
+
* `loaders/secret.ts`) — the plaintext lives behind a function, so JSON.stringify
|
|
154
|
+
* already drops it to `{}`. An unresolved secret block is `{ __resolveType:
|
|
155
|
+
* ".../secret.ts", encrypted, ... }`. Neither has any place in a page-as-JSON
|
|
156
|
+
* payload, so we remove the whole field. Matches by shape, so no content field
|
|
157
|
+
* is caught by accident.
|
|
158
|
+
*
|
|
159
|
+
* Limitation: a *plaintext* secret a loader extracted into a string prop (the
|
|
160
|
+
* deprecated `SecretString` or a manual `secret.get()`) is indistinguishable from
|
|
161
|
+
* content — keep secrets as `Secret`, never resolve them into a serialized prop.
|
|
162
|
+
*/
|
|
163
|
+
export function isSecretValue(value: unknown): boolean {
|
|
164
|
+
if (!value || typeof value !== "object") return false;
|
|
165
|
+
const o = value as Record<string, unknown>;
|
|
166
|
+
// Resolved Secret: an object whose only own key is a `get` function.
|
|
167
|
+
if (typeof o.get === "function" && Object.keys(o).length === 1) return true;
|
|
168
|
+
// Unresolved secret block (defensive — resolved props usually drop __resolveType).
|
|
169
|
+
const rt = o.__resolveType;
|
|
170
|
+
return typeof rt === "string" && /secret/i.test(rt);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* `JSON.stringify` that drops any Secret-shaped value ({@link isSecretValue})
|
|
175
|
+
* anywhere in the tree. Used for both `?renderJson` and `?asJson` bodies.
|
|
176
|
+
*/
|
|
177
|
+
export function stringifyWithoutSecrets(value: unknown): string {
|
|
178
|
+
return JSON.stringify(value, (_key, v) => (isSecretValue(v) ? undefined : v));
|
|
179
|
+
}
|