@decocms/blocks 7.72.0 → 8.0.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.
Files changed (148) hide show
  1. package/bin/deco.js +13 -0
  2. package/package.json +32 -3
  3. package/src/cms/schema.ts +1 -1
  4. package/src/hooks/LiveControls.tsx +2 -0
  5. package/src/hooks/useLoadMore.ts +2 -0
  6. package/src/index.ts +37 -0
  7. package/src/protocol/assets.ts +124 -0
  8. package/src/protocol/canonical.ts +7 -0
  9. package/src/protocol/ciphertext.ts +6 -0
  10. package/src/protocol/client.ts +170 -0
  11. package/src/protocol/conformance/cases/assets.ts +81 -0
  12. package/src/protocol/conformance/cases/guards.ts +314 -0
  13. package/src/protocol/conformance/cases/reads.ts +168 -0
  14. package/src/protocol/conformance/cases/types.ts +9 -0
  15. package/src/protocol/conformance/cases/wire.ts +138 -0
  16. package/src/protocol/conformance/cases/writes.ts +345 -0
  17. package/src/protocol/conformance/contentHashFixtures.ts +79 -0
  18. package/src/protocol/conformance/context.ts +222 -0
  19. package/src/protocol/conformance/index.ts +140 -0
  20. package/src/protocol/errors.ts +142 -0
  21. package/src/protocol/index.ts +39 -0
  22. package/src/protocol/keys.ts +279 -0
  23. package/src/protocol/params.ts +67 -0
  24. package/src/protocol/secrets.ts +231 -0
  25. package/src/protocol/server/assets.ts +119 -0
  26. package/src/protocol/server/auth.ts +49 -0
  27. package/src/protocol/server/bodyCache.ts +84 -0
  28. package/src/protocol/server/content.ts +175 -0
  29. package/src/protocol/server/core.ts +112 -0
  30. package/src/protocol/server/handler.ts +102 -0
  31. package/src/protocol/server/http.ts +139 -0
  32. package/src/protocol/server/index.ts +11 -0
  33. package/src/protocol/server/methods/apply.ts +341 -0
  34. package/src/protocol/server/methods/read.ts +156 -0
  35. package/src/protocol/server/rpc.ts +136 -0
  36. package/src/protocol/storage/fs/hash.ts +21 -0
  37. package/src/protocol/storage/fs/index.ts +338 -0
  38. package/src/protocol/storage/fs/lock.ts +68 -0
  39. package/src/protocol/storage/fs/transaction.ts +183 -0
  40. package/src/protocol/storage/fs/unsupported.ts +15 -0
  41. package/src/protocol/storage/memory.ts +238 -0
  42. package/src/protocol/storage.ts +182 -0
  43. package/src/protocol/types.ts +172 -0
  44. package/src/v8/analytics.ts +50 -0
  45. package/src/v8/background.ts +107 -0
  46. package/src/v8/builtins/data.ts +23 -0
  47. package/src/v8/builtins/index.ts +26 -0
  48. package/src/v8/builtins/lazy.ts +23 -0
  49. package/src/v8/builtins/legacy.ts +65 -0
  50. package/src/v8/builtins/matchers.ts +31 -0
  51. package/src/v8/builtins/multivariate.ts +25 -0
  52. package/src/v8/builtins/secret.ts +82 -0
  53. package/src/v8/canonical.ts +117 -0
  54. package/src/v8/ciphertext.ts +158 -0
  55. package/src/v8/cli/builtins.ts +20 -0
  56. package/src/v8/cli/check/index.ts +774 -0
  57. package/src/v8/cli/check/messages.ts +168 -0
  58. package/src/v8/cli/content.ts +287 -0
  59. package/src/v8/cli/index.ts +17 -0
  60. package/src/v8/cli/log.ts +18 -0
  61. package/src/v8/cli/main.ts +12 -0
  62. package/src/v8/cli/root.ts +66 -0
  63. package/src/v8/cli/run.ts +236 -0
  64. package/src/v8/cli/schema/builtinSchemas.ts +236 -0
  65. package/src/v8/cli/schema/generate.ts +507 -0
  66. package/src/v8/cli/schema/index.ts +63 -0
  67. package/src/v8/cli/schema/tsProgram.ts +497 -0
  68. package/src/v8/cli/schema/typeToSchema.ts +689 -0
  69. package/src/v8/cli/serve/server.ts +303 -0
  70. package/src/v8/cli/watch.ts +86 -0
  71. package/src/v8/client.ts +147 -0
  72. package/src/v8/cms.ts +237 -0
  73. package/src/v8/content.ts +195 -0
  74. package/src/v8/draft.ts +136 -0
  75. package/src/v8/errors.ts +59 -0
  76. package/src/v8/fetch.ts +152 -0
  77. package/src/v8/identity.ts +65 -0
  78. package/src/v8/index.ts +39 -0
  79. package/src/v8/json.ts +74 -0
  80. package/src/v8/matchRoute.ts +204 -0
  81. package/src/v8/remoteLoader.ts +282 -0
  82. package/src/v8/resolver.ts +278 -0
  83. package/src/v8/secrets.ts +17 -0
  84. package/src/v8/telemetry.ts +533 -0
  85. package/src/v8/types.ts +240 -0
  86. package/src/cms/blockSource.test.ts +0 -76
  87. package/src/cms/client.browserBundle.test.ts +0 -44
  88. package/src/cms/deferredTrigger.test.ts +0 -32
  89. package/src/cms/draftSource.test.ts +0 -519
  90. package/src/cms/layoutCacheRace.test.ts +0 -181
  91. package/src/cms/loadDecofileDirectory.test.ts +0 -109
  92. package/src/cms/loader.test.ts +0 -557
  93. package/src/cms/registry.test.ts +0 -110
  94. package/src/cms/renderJson.test.ts +0 -167
  95. package/src/cms/resolve.test.ts +0 -1492
  96. package/src/cms/schema.test.ts +0 -356
  97. package/src/cms/sectionLoaderContext.test.ts +0 -111
  98. package/src/cms/sectionLoaders.test.ts +0 -625
  99. package/src/cms/sectionMixins.test.ts +0 -167
  100. package/src/cms/stickyFlags.test.ts +0 -117
  101. package/src/flags/flags.test.ts +0 -148
  102. package/src/hooks/Image.test.ts +0 -239
  103. package/src/hooks/RenderSection.test.tsx +0 -81
  104. package/src/hooks/Stats.test.ts +0 -112
  105. package/src/hooks/useLoadMore.test.ts +0 -205
  106. package/src/matchers/builtins.test.ts +0 -318
  107. package/src/matchers/override.test.ts +0 -205
  108. package/src/middleware/hydrationContext.test.ts +0 -61
  109. package/src/middleware/observability.test.ts +0 -335
  110. package/src/middleware/validateSection.test.ts +0 -147
  111. package/src/preview/DraftPreviewBadge.render.test.tsx +0 -67
  112. package/src/preview/DraftPreviewBadge.test.ts +0 -55
  113. package/src/sdk/abTesting.test.ts +0 -381
  114. package/src/sdk/cacheHeaders.test.ts +0 -258
  115. package/src/sdk/cacheStorage.test.ts +0 -242
  116. package/src/sdk/cachedLoader.test.ts +0 -122
  117. package/src/sdk/cachedLoaderMaxBytes.test.ts +0 -23
  118. package/src/sdk/cachedLoaderModule.test.ts +0 -209
  119. package/src/sdk/cachedLoaderSizeMetric.test.ts +0 -93
  120. package/src/sdk/cn.test.ts +0 -34
  121. package/src/sdk/composite.test.ts +0 -121
  122. package/src/sdk/cookie.test.ts +0 -108
  123. package/src/sdk/crypto.test.ts +0 -65
  124. package/src/sdk/deepOmit.test.ts +0 -45
  125. package/src/sdk/encoding.test.ts +0 -71
  126. package/src/sdk/env.test.ts +0 -33
  127. package/src/sdk/experiments.test.ts +0 -328
  128. package/src/sdk/fetchCache.test.ts +0 -164
  129. package/src/sdk/flags.test.ts +0 -91
  130. package/src/sdk/http.test.ts +0 -71
  131. package/src/sdk/inflightTimeout.test.ts +0 -35
  132. package/src/sdk/instrumentedFetch.test.ts +0 -559
  133. package/src/sdk/invoke.test.ts +0 -115
  134. package/src/sdk/logger.test.ts +0 -432
  135. package/src/sdk/otel.test.ts +0 -691
  136. package/src/sdk/otelAdapters.test.ts +0 -89
  137. package/src/sdk/otelHttpLog.test.ts +0 -457
  138. package/src/sdk/otelHttpMeter.test.ts +0 -395
  139. package/src/sdk/otelHttpTracer.test.ts +0 -474
  140. package/src/sdk/replaceStream.test.ts +0 -117
  141. package/src/sdk/requestContextStorage.browser.test.ts +0 -29
  142. package/src/sdk/splitExactRedirects.test.ts +0 -154
  143. package/src/sdk/urlRedaction.test.ts +0 -73
  144. package/src/sdk/urlUtils.test.ts +0 -34
  145. package/src/sdk/useDevice.test.ts +0 -130
  146. package/src/sdk/useScript.test.ts +0 -128
  147. package/src/sdk/useSuggestions.test.ts +0 -230
  148. package/tsconfig.json +0 -7
@@ -0,0 +1,282 @@
1
+ /**
2
+ * `remoteLoader`: hosted releases and drafts from the Deco API, over a
3
+ * fallback (the content module, or any loader). See
4
+ * /next/hosted-releases-internals and /next/hosted-publishing.
5
+ *
6
+ * - `load()` never touches the network: it returns the newest release this
7
+ * process fetched, or the fallback's content until one is fetched.
8
+ * - `update()` asks the delivery API for the small production channel
9
+ * manifest; the CMS calls it on first use and then every interval, at an
10
+ * idle moment. A manifest older than the newest observed generation is
11
+ * ignored; a revision equal to the fallback's is served from the fallback
12
+ * without a download; anything else is fetched, verified against its
13
+ * content hash and swapped in whole. Any error keeps memory as it was.
14
+ * - `load(pointer)` fetches the draft a pointer names and waits for it. Only
15
+ * pointers to the delivery host are fetched; anything else is refused.
16
+ * - In development (`NODE_ENV=development`), releases stay on the fallback so
17
+ * local files win; drafts still load.
18
+ * - A release or draft larger than `MAX_SNAPSHOT_BYTES` is refused while it
19
+ * downloads, before it's buffered whole (a Worker isolate has 128 MB).
20
+ * - Without `site` or `token` it's a plain loader over the fallback: no
21
+ * releases, no drafts beyond what the fallback serves.
22
+ *
23
+ * Instances are process-wide singletons, like `createCMS`'s.
24
+ */
25
+ import { computeContentRevision } from "./canonical";
26
+ import { isSnapshot } from "./content";
27
+ import { parseDraftPointer } from "./draft";
28
+ import { clearGlobals, contentIdentity, fnv1a } from "./identity";
29
+ import { isPlainObject } from "./json";
30
+ import type { Loader, Snapshot } from "./types";
31
+
32
+ /** The hosted delivery origin: channel manifests, release assets and drafts. */
33
+ const HOSTED_DELIVERY_ORIGIN = "https://delivery.decocms.com";
34
+
35
+ const INSTANCE_PREFIX = "decocms.blocks.remote:";
36
+ const MANIFEST_FORMAT = 1;
37
+ const FETCH_TIMEOUT_MS = 10_000;
38
+ /** The largest release or draft accepted, in bytes of JSON. */
39
+ const MAX_SNAPSHOT_BYTES = 64 * 1024 * 1024;
40
+
41
+ interface RemoteLoaderOptions {
42
+ site?: string;
43
+ token?: string;
44
+ /** ms between release checks, used when `createCMS` has no `interval` of its own. */
45
+ interval?: number;
46
+ }
47
+
48
+ interface Manifest {
49
+ generation: number;
50
+ revision: string;
51
+ snapshot: string;
52
+ }
53
+
54
+ class RemoteLoader implements Loader {
55
+ readonly #site: string;
56
+ readonly #token: string;
57
+ /** The `interval` this loader was created with; `createCMS` reads it. */
58
+ readonly interval: number | undefined;
59
+ #fallback: Snapshot | Loader;
60
+ #fallbackRevision: string | undefined;
61
+ #current: Snapshot | undefined;
62
+ #generation = -1;
63
+
64
+ constructor(
65
+ fallback: Snapshot | Loader,
66
+ options: { site: string; token: string; interval?: number },
67
+ ) {
68
+ this.#fallback = fallback;
69
+ this.#site = options.site;
70
+ this.#token = options.token;
71
+ this.interval = options.interval;
72
+ }
73
+
74
+ /** A hot reload hands the same instance new fallback content. */
75
+ adopt(fallback: Snapshot | Loader): void {
76
+ if (fallback === this.#fallback) return;
77
+ this.#fallback = fallback;
78
+ this.#fallbackRevision = undefined;
79
+ }
80
+
81
+ load(pointer?: string | null): Promise<Snapshot> {
82
+ if (pointer) return this.#draft(pointer);
83
+ if (this.#current !== undefined) return Promise.resolve(this.#current);
84
+ return this.#loadFallback();
85
+ }
86
+
87
+ async update(): Promise<{ updated: boolean }> {
88
+ if (isDevelopment()) return { updated: false };
89
+ const manifest = await this.#manifest();
90
+ if (manifest.generation < this.#generation) return { updated: false };
91
+
92
+ // Best effort: a fallback that can't load (a missing kvLoader key) is
93
+ // fixed by downloading the release, not by failing the check.
94
+ const fallbackRevision = await this.#fallbackRevisionNow().catch(() => undefined);
95
+ const served = this.#current !== undefined ? this.#current.revision : fallbackRevision;
96
+ if (served !== undefined && manifest.revision === served) {
97
+ // A new generation of the same content (a re-promotion, a rollback to
98
+ // it) still signals, so caches keyed on the release are invalidated.
99
+ const changed = this.#generation !== -1 && manifest.generation > this.#generation;
100
+ this.#generation = manifest.generation;
101
+ return { updated: changed };
102
+ }
103
+ let next: Snapshot | undefined;
104
+ if (fallbackRevision === undefined || manifest.revision !== fallbackRevision) {
105
+ next = await this.#release(manifest);
106
+ }
107
+ // A slower, earlier check must not undo a newer publish or rollback.
108
+ if (manifest.generation < this.#generation) return { updated: false };
109
+ this.#generation = manifest.generation;
110
+ this.#current = next;
111
+ return { updated: true };
112
+ }
113
+
114
+ async #loadFallback(): Promise<Snapshot> {
115
+ const fallback = this.#fallback;
116
+ const snapshot = isSnapshot(fallback) ? fallback : await (fallback as Loader).load();
117
+ if (fallback === this.#fallback) this.#fallbackRevision = snapshot.revision;
118
+ return snapshot;
119
+ }
120
+
121
+ async #fallbackRevisionNow(): Promise<string> {
122
+ return this.#fallbackRevision ?? (await this.#loadFallback()).revision;
123
+ }
124
+
125
+ async #manifest(): Promise<Manifest> {
126
+ const site = encodeURIComponent(this.#site);
127
+ const response = await fetchWithTimeout(
128
+ `${HOSTED_DELIVERY_ORIGIN}/sites/${site}/channels/production.json`,
129
+ this.#auth(),
130
+ );
131
+ if (!response.ok) throw new Error(`channel manifest: HTTP ${response.status}`);
132
+ const body: unknown = await response.json();
133
+ const prefix = `/sites/${site}/revisions/`;
134
+ if (
135
+ !isPlainObject(body) ||
136
+ body.format !== MANIFEST_FORMAT ||
137
+ !Number.isSafeInteger(body.generation) ||
138
+ typeof body.revision !== "string" ||
139
+ typeof body.snapshot !== "string" ||
140
+ !body.snapshot.startsWith(prefix) ||
141
+ !/^[\w.-]+\.json$/.test(body.snapshot.slice(prefix.length)) ||
142
+ body.snapshot.includes("..")
143
+ ) {
144
+ throw new Error("channel manifest: unexpected format or snapshot path");
145
+ }
146
+ return body as unknown as Manifest;
147
+ }
148
+
149
+ async #release(manifest: Manifest): Promise<Snapshot> {
150
+ const response = await fetchWithTimeout(
151
+ `${HOSTED_DELIVERY_ORIGIN}${manifest.snapshot}`,
152
+ this.#auth(),
153
+ );
154
+ if (!response.ok) throw new Error(`release ${manifest.revision}: HTTP ${response.status}`);
155
+ const snapshot = await readBoundedJson(response, `release ${manifest.revision}`);
156
+ if (
157
+ !isSnapshot(snapshot) ||
158
+ snapshot.revision !== manifest.revision ||
159
+ (await computeContentRevision(snapshot.blocks)) !== manifest.revision
160
+ ) {
161
+ throw new Error(`release ${manifest.revision}: content doesn't match its revision`);
162
+ }
163
+ return snapshot;
164
+ }
165
+
166
+ async #draft(raw: string): Promise<Snapshot> {
167
+ const pointer = parseDraftPointer(raw);
168
+ if (pointer === null) throw new Error("invalid draft pointer");
169
+ if (pointer.host !== new URL(HOSTED_DELIVERY_ORIGIN).host) {
170
+ throw new Error(`draft pointer names an unexpected host "${pointer.host}"`);
171
+ }
172
+ // If-Match: serve exactly this version or fail, never the branch's newer head.
173
+ const response = await fetchWithTimeout(`https://${pointer.host}${pointer.path}`, {
174
+ ...this.#auth(),
175
+ "if-match": pointer.version,
176
+ });
177
+ if (!response.ok) throw new Error(`draft: HTTP ${response.status}`);
178
+ const snapshot = await readBoundedJson(response, "draft");
179
+ if (!isSnapshot(snapshot)) throw new Error("draft: not a snapshot");
180
+ return snapshot;
181
+ }
182
+
183
+ #auth(): Record<string, string> {
184
+ return { authorization: `Bearer ${this.#token}` };
185
+ }
186
+ }
187
+
188
+ /** A loader over the fallback alone: `remoteLoader` without `site` or `token`. */
189
+ class LocalLoader implements Loader {
190
+ #fallback: Snapshot | Loader;
191
+
192
+ constructor(fallback: Snapshot | Loader) {
193
+ this.#fallback = fallback;
194
+ }
195
+
196
+ adopt(fallback: Snapshot | Loader): void {
197
+ this.#fallback = fallback;
198
+ }
199
+
200
+ load(pointer?: string | null): Promise<Snapshot> {
201
+ const fallback = this.#fallback;
202
+ return isSnapshot(fallback) ? Promise.resolve(fallback) : fallback.load(pointer);
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Hosted releases and drafts over a fallback. `createCMS` builds it for you
208
+ * when `site` and `token` are set. With either unset (a dev or test
209
+ * environment without the variables), it's a loader over the fallback alone.
210
+ */
211
+ export function remoteLoader(fallback: Snapshot | Loader, options: RemoteLoaderOptions): Loader {
212
+ const { site, token } = options ?? {};
213
+ const hosted = Boolean(site && token);
214
+ const key = Symbol.for(
215
+ `${INSTANCE_PREFIX}${contentIdentity(fallback)}` +
216
+ (hosted ? `|site:${site}|token:${fnv1a(token!)}` : "|local"),
217
+ );
218
+ const store = globalThis as unknown as Record<symbol, RemoteLoader | LocalLoader | undefined>;
219
+ const existing = store[key];
220
+ if (existing instanceof Object && typeof existing.adopt === "function") {
221
+ if (existing instanceof RemoteLoader && existing.interval !== options.interval) {
222
+ console.warn(
223
+ "[decocms/blocks] remoteLoader was called again for the same site with different options " +
224
+ "(interval); keeping the first instance's options.",
225
+ );
226
+ }
227
+ existing.adopt(fallback);
228
+ return existing;
229
+ }
230
+ const instance = hosted
231
+ ? new RemoteLoader(fallback, { site: site!, token: token!, interval: options.interval })
232
+ : new LocalLoader(fallback);
233
+ store[key] = instance;
234
+ return instance;
235
+ }
236
+
237
+ export function resetRemoteLoaders(): void {
238
+ clearGlobals(INSTANCE_PREFIX);
239
+ }
240
+
241
+ /** Parses a JSON body, refusing it as soon as it's larger than `MAX_SNAPSHOT_BYTES`. */
242
+ async function readBoundedJson(response: Response, label: string): Promise<unknown> {
243
+ const tooLarge = () => new Error(`${label}: larger than ${MAX_SNAPSHOT_BYTES} bytes`);
244
+ if (Number(response.headers.get("content-length")) > MAX_SNAPSHOT_BYTES) {
245
+ await response.body?.cancel();
246
+ throw tooLarge();
247
+ }
248
+ if (response.body === null) return response.json();
249
+ const reader = response.body.getReader();
250
+ const decoder = new TextDecoder();
251
+ const parts: string[] = [];
252
+ let size = 0;
253
+ for (;;) {
254
+ const { done, value } = await reader.read();
255
+ if (done) break;
256
+ size += value.byteLength;
257
+ if (size > MAX_SNAPSHOT_BYTES) {
258
+ await reader.cancel();
259
+ throw tooLarge();
260
+ }
261
+ parts.push(decoder.decode(value, { stream: true }));
262
+ }
263
+ parts.push(decoder.decode());
264
+ return JSON.parse(parts.join(""));
265
+ }
266
+
267
+ function fetchWithTimeout(url: string, headers: Record<string, string>): Promise<Response> {
268
+ const signal =
269
+ typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function"
270
+ ? AbortSignal.timeout(FETCH_TIMEOUT_MS)
271
+ : undefined;
272
+ return fetch(url, { headers, signal });
273
+ }
274
+
275
+ function isDevelopment(): boolean {
276
+ try {
277
+ // Written out so bundlers that define process.env.NODE_ENV replace it.
278
+ return process.env.NODE_ENV === "development";
279
+ } catch {
280
+ return false;
281
+ }
282
+ }
@@ -0,0 +1,278 @@
1
+ /**
2
+ * The lookup rule and its evaluation (see /next/how-resolution-works).
3
+ *
4
+ * One registry, later keys win: `{ ...savedBlocks, ...builtIns, ...blocks }`.
5
+ * A function runs with its inputs resolved first (inside out, siblings
6
+ * concurrently); an alias (the snapshot's table, then the built-in legacy one
7
+ * in ./builtins/legacy) is a second key for a type, so like a function it wins
8
+ * over a saved block of the same name; a saved block is replaced by
9
+ * `{ ...saved, ...arguments }` and looked up again; anything else is
10
+ * `UNKNOWN_BLOCK`. The built-in `lazy` is
11
+ * the one special case: its `value` is not resolved first.
12
+ *
13
+ * A `Resolver` belongs to one client, so its memo (results per block function
14
+ * and canonical inputs) never outlives a request. Within the request the memo
15
+ * hands every occurrence of the same block the same result object, so results
16
+ * are read-only (see `Client` in ./types): mutating one in place would change
17
+ * the others, the class of bug behind the v7 layout-cache race.
18
+ */
19
+ import { RESERVED_NAMES } from "./builtins/index";
20
+ import { isLazyBuiltin } from "./builtins/lazy";
21
+ import { LEGACY_ALIASES, wrapLegacyVariants } from "./builtins/legacy";
22
+ import { errors, isResolutionError } from "./errors";
23
+ import { canonicalKey, isPlainObject, type JsonObject, own } from "./json";
24
+ import type { BlockFunction, Blocks, Lazy, Snapshot } from "./types";
25
+
26
+ type Path = (string | number)[];
27
+
28
+ interface ResolverOptions {
29
+ snapshot: Snapshot;
30
+ blocks: Blocks;
31
+ /** The built-ins, with this CMS's `secret`. */
32
+ builtIns: Readonly<Blocks>;
33
+ /** Called once per name a saved block shares with a function. */
34
+ onCollision?: (name: string) => void;
35
+ /** Called after each block function runs (telemetry: spans and failures). */
36
+ onBlock?: (type: string, start: number, end: number, error?: unknown) => void;
37
+ }
38
+
39
+ export class Resolver {
40
+ readonly #snapshot: Snapshot;
41
+ readonly #blocks: Blocks;
42
+ readonly #builtIns: Readonly<Blocks>;
43
+ readonly #onCollision?: (name: string) => void;
44
+ readonly #onBlock?: ResolverOptions["onBlock"];
45
+ readonly #memo = new WeakMap<BlockFunction, Map<string, Promise<unknown>>>();
46
+ readonly #keys = new WeakMap<object, string | null>();
47
+
48
+ constructor({ snapshot, blocks, builtIns, onCollision, onBlock }: ResolverOptions) {
49
+ this.#snapshot = snapshot;
50
+ this.#blocks = blocks;
51
+ this.#builtIns = builtIns;
52
+ this.#onCollision = onCollision;
53
+ this.#onBlock = onBlock;
54
+ }
55
+
56
+ /** The saved entry with this name, or `undefined`. */
57
+ entry(name: string): unknown {
58
+ return own(this.#snapshot.blocks, name);
59
+ }
60
+
61
+ /** The type an alias points at, or the type itself. */
62
+ canonicalType(type: string): string {
63
+ return this.#alias(type) ?? type;
64
+ }
65
+
66
+ /** The snapshot's alias table first, then the built-in legacy one. */
67
+ #alias(type: string): string | undefined {
68
+ return own(this.#snapshot.aliases, type) ?? own(LEGACY_ALIASES, type);
69
+ }
70
+
71
+ /** Resolves a saved entry by name (the target is known to exist). */
72
+ resolveEntry(name: string): Promise<unknown> {
73
+ return this.resolve(this.entry(name), [], [name], true);
74
+ }
75
+
76
+ /** Expands a saved entry by name without running anything. */
77
+ expandEntry(name: string): unknown {
78
+ return this.expand(this.entry(name), [], [name], true);
79
+ }
80
+
81
+ /**
82
+ * Resolves any value. `chain` is the saved entries expanded on the current
83
+ * path (for `CYCLE`); `fromContent` marks values that live in the snapshot,
84
+ * which are always copied so no caller can mutate shared content.
85
+ */
86
+ async resolve(
87
+ node: unknown,
88
+ path: Path,
89
+ chain: string[],
90
+ fromContent: boolean,
91
+ ): Promise<unknown> {
92
+ if (Array.isArray(node)) {
93
+ const results = await Promise.all(
94
+ node.map((item, i) => this.resolve(item, [...path, i], chain, fromContent)),
95
+ );
96
+ const kept = results.filter((value) => value !== undefined);
97
+ return !fromContent && sameItems(node, kept) ? node : kept;
98
+ }
99
+ if (!isPlainObject(node)) return node;
100
+ if (typeof node.__resolveType === "string") {
101
+ return this.#resolveBlock(node, node.__resolveType, path, chain, fromContent);
102
+ }
103
+ return this.#resolveFields(node, path, chain, fromContent);
104
+ }
105
+
106
+ /** Expands saved blocks in place without running any function (`{ run: false }`). */
107
+ expand(node: unknown, path: Path, chain: string[], fromContent: boolean): unknown {
108
+ if (Array.isArray(node)) {
109
+ const items = node.map((item, i) => this.expand(item, [...path, i], chain, fromContent));
110
+ return !fromContent && sameItems(node, items) ? node : items;
111
+ }
112
+ if (!isPlainObject(node)) return node;
113
+ const type = node.__resolveType;
114
+ if (
115
+ typeof type === "string" &&
116
+ this.#functionFor(type) === undefined &&
117
+ this.#aliasFor(type) === undefined
118
+ ) {
119
+ const saved = this.entry(type);
120
+ if (saved !== undefined) {
121
+ const next = [...chain, type];
122
+ if (chain.includes(type)) throw errors.cycle(next);
123
+ return this.expand(mergeReference(saved, node), path, next, true);
124
+ }
125
+ }
126
+ let changed = fromContent;
127
+ const out: JsonObject = {};
128
+ for (const [key, value] of Object.entries(node)) {
129
+ const expanded = this.expand(value, [...path, key], chain, fromContent);
130
+ if (expanded !== value) changed = true;
131
+ out[key] = expanded;
132
+ }
133
+ return changed ? out : node;
134
+ }
135
+
136
+ async #resolveFields(
137
+ node: JsonObject,
138
+ path: Path,
139
+ chain: string[],
140
+ fromContent: boolean,
141
+ ): Promise<JsonObject> {
142
+ const keys = Object.keys(node);
143
+ const values = await Promise.all(
144
+ keys.map((key) => this.resolve(node[key], [...path, key], chain, fromContent)),
145
+ );
146
+ let changed = fromContent;
147
+ const out: JsonObject = {};
148
+ keys.forEach((key, i) => {
149
+ if (values[i] !== node[key]) changed = true;
150
+ out[key] = values[i];
151
+ });
152
+ return changed ? out : node;
153
+ }
154
+
155
+ #resolveBlock(
156
+ node: JsonObject,
157
+ type: string,
158
+ path: Path,
159
+ chain: string[],
160
+ fromContent: boolean,
161
+ viaAlias = false,
162
+ ): Promise<unknown> {
163
+ const fn = this.#functionFor(type);
164
+ if (fn !== undefined) {
165
+ if (isLazyBuiltin(fn)) {
166
+ return Promise.resolve(this.#lazy(node.value, [...path, "value"], chain, fromContent));
167
+ }
168
+ return this.#run(fn, type, node, path, chain);
169
+ }
170
+
171
+ // An alias is a second key for a type, so like a function it wins over a
172
+ // saved block of the same name (with a warning; deco check rejects it).
173
+ const alias = viaAlias ? undefined : this.#aliasFor(type);
174
+ const saved = alias === undefined ? this.entry(type) : undefined;
175
+ if (saved !== undefined) {
176
+ const next = [...chain, type];
177
+ if (chain.includes(type)) return Promise.reject(errors.cycle(next));
178
+ if (RESERVED_NAMES.has(type)) this.#onCollision?.(type);
179
+ return this.resolve(mergeReference(saved, node), path, next, true);
180
+ }
181
+
182
+ if (alias !== undefined) {
183
+ return this.#resolveBlock(
184
+ wrapLegacyVariants(type, node),
185
+ alias,
186
+ path,
187
+ chain,
188
+ fromContent,
189
+ true,
190
+ );
191
+ }
192
+ return Promise.reject(errors.unknownBlock(type, path));
193
+ }
194
+
195
+ /** The type an alias names (never itself); a saved block of the same name loses (with a warning). */
196
+ #aliasFor(type: string): string | undefined {
197
+ const alias = this.#alias(type);
198
+ if (alias === undefined || alias === type) return undefined;
199
+ if (this.entry(type) !== undefined) this.#onCollision?.(type);
200
+ return alias;
201
+ }
202
+
203
+ /** Block map first, then built-ins; a saved block of the same name loses (with a warning). */
204
+ #functionFor(type: string): BlockFunction | undefined {
205
+ const fn = own(this.#blocks, type) ?? own(this.#builtIns, type);
206
+ if (typeof fn !== "function") return undefined;
207
+ if (this.entry(type) !== undefined) this.#onCollision?.(type);
208
+ return fn;
209
+ }
210
+
211
+ #run(
212
+ fn: BlockFunction,
213
+ type: string,
214
+ node: JsonObject,
215
+ path: Path,
216
+ chain: string[],
217
+ ): Promise<unknown> {
218
+ const key = canonicalKey(node, this.#keys);
219
+ let results = this.#memo.get(fn);
220
+ const hit = key === null ? undefined : results?.get(key);
221
+ if (hit !== undefined) return hit;
222
+
223
+ const pending = this.#call(fn, type, node, path, chain);
224
+ if (key !== null) {
225
+ if (results === undefined) {
226
+ results = new Map();
227
+ this.#memo.set(fn, results);
228
+ }
229
+ results.set(key, pending);
230
+ }
231
+ return pending;
232
+ }
233
+
234
+ async #call(
235
+ fn: BlockFunction,
236
+ type: string,
237
+ node: JsonObject,
238
+ path: Path,
239
+ chain: string[],
240
+ ): Promise<unknown> {
241
+ // Inputs are always fresh objects: a function may mutate its props freely.
242
+ const { __resolveType: _type, ...inputs } = node;
243
+ const resolved = await this.#resolveFields(inputs, path, chain, true);
244
+ const start = Date.now();
245
+ try {
246
+ const value = await fn(resolved);
247
+ this.#onBlock?.(type, start, Date.now());
248
+ return value;
249
+ } catch (error) {
250
+ this.#onBlock?.(type, start, Date.now(), error);
251
+ // A lazy block's failure escaping its caller keeps its own code and path.
252
+ if (isResolutionError(error)) throw error;
253
+ throw errors.blockFailed(type, path, error);
254
+ }
255
+ }
256
+
257
+ #lazy(value: unknown, path: Path, chain: string[], fromContent: boolean): Lazy<unknown> {
258
+ let pending: Promise<unknown> | undefined;
259
+ return () => {
260
+ pending ??= this.resolve(value, path, chain, fromContent);
261
+ return pending;
262
+ };
263
+ }
264
+ }
265
+
266
+ /** `{ ...saved, ...arguments }`: shallow, and the saved block's type wins. */
267
+ function mergeReference(saved: unknown, reference: JsonObject): unknown {
268
+ if (!isPlainObject(saved)) return saved;
269
+ const { __resolveType: _ref, ...overrides } = reference;
270
+ const merged: JsonObject = { ...saved, ...overrides };
271
+ if ("__resolveType" in saved) merged.__resolveType = saved.__resolveType;
272
+ else delete merged.__resolveType;
273
+ return merged;
274
+ }
275
+
276
+ function sameItems(a: unknown[], b: unknown[]): boolean {
277
+ return a.length === b.length && a.every((item, i) => item === b[i]);
278
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * `@decocms/blocks/secrets`: encrypts a value for a `secret` block, for a
3
+ * script or an AI agent that writes content (see /next/api-reference#secrets).
4
+ * Web Crypto only, so it runs in browsers, on Workers and on Node.
5
+ */
6
+ import { encryptToCiphertext } from "./ciphertext";
7
+
8
+ /**
9
+ * Encrypts `value` with your public key (the contents of `.deco/secrets.pub`)
10
+ * and returns a ready `secret` block.
11
+ */
12
+ export async function encryptSecret(
13
+ publicKey: string,
14
+ value: string,
15
+ ): Promise<{ __resolveType: "secret"; ciphertext: string }> {
16
+ return { __resolveType: "secret", ciphertext: await encryptToCiphertext(publicKey, value) };
17
+ }