@decocms/blocks 8.9.1 → 8.10.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 (257) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/protocol/channel.d.ts +98 -0
  6. package/dist/protocol/channel.d.ts.map +1 -0
  7. package/dist/protocol/channel.js +58 -0
  8. package/dist/protocol/channel.js.map +1 -0
  9. package/dist/protocol/client.js +2 -2
  10. package/dist/protocol/client.js.map +1 -1
  11. package/dist/protocol/conformance/blockMetaVectors.json +214 -0
  12. package/dist/protocol/conformance/contentHashFixtures.d.ts +22 -0
  13. package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -1
  14. package/dist/protocol/conformance/contentHashFixtures.js +55 -0
  15. package/dist/protocol/conformance/contentHashFixtures.js.map +1 -1
  16. package/dist/protocol/conformance/index.d.ts +2 -1
  17. package/dist/protocol/conformance/index.d.ts.map +1 -1
  18. package/dist/protocol/conformance/index.js +2 -1
  19. package/dist/protocol/conformance/index.js.map +1 -1
  20. package/dist/protocol/conformance/vectors.d.ts +33 -0
  21. package/dist/protocol/conformance/vectors.d.ts.map +1 -0
  22. package/dist/protocol/conformance/vectors.js +3 -0
  23. package/dist/protocol/conformance/vectors.js.map +1 -0
  24. package/dist/protocol/index.d.ts +11 -1
  25. package/dist/protocol/index.d.ts.map +1 -1
  26. package/dist/protocol/index.js +11 -1
  27. package/dist/protocol/index.js.map +1 -1
  28. package/dist/protocol/keys.d.ts +1 -1
  29. package/dist/protocol/keys.js +1 -1
  30. package/dist/v8/background.d.ts +13 -0
  31. package/dist/v8/background.d.ts.map +1 -1
  32. package/dist/v8/background.js +41 -0
  33. package/dist/v8/background.js.map +1 -1
  34. package/dist/v8/blockMeta.d.ts +27 -0
  35. package/dist/v8/blockMeta.d.ts.map +1 -0
  36. package/dist/v8/blockMeta.js +75 -0
  37. package/dist/v8/blockMeta.js.map +1 -0
  38. package/dist/v8/blockStore.d.ts +43 -0
  39. package/dist/v8/blockStore.d.ts.map +1 -0
  40. package/dist/v8/blockStore.js +95 -0
  41. package/dist/v8/blockStore.js.map +1 -0
  42. package/dist/v8/boundedJson.d.ts +5 -0
  43. package/dist/v8/boundedJson.d.ts.map +1 -1
  44. package/dist/v8/boundedJson.js +10 -2
  45. package/dist/v8/boundedJson.js.map +1 -1
  46. package/dist/v8/boundedMap.d.ts +19 -0
  47. package/dist/v8/boundedMap.d.ts.map +1 -0
  48. package/dist/v8/boundedMap.js +52 -0
  49. package/dist/v8/boundedMap.js.map +1 -0
  50. package/dist/v8/builtins/data.d.ts +18 -2
  51. package/dist/v8/builtins/data.d.ts.map +1 -1
  52. package/dist/v8/builtins/data.js +17 -1
  53. package/dist/v8/builtins/data.js.map +1 -1
  54. package/dist/v8/builtins/index.d.ts.map +1 -1
  55. package/dist/v8/builtins/index.js +2 -1
  56. package/dist/v8/builtins/index.js.map +1 -1
  57. package/dist/v8/builtins/legacy.d.ts +6 -3
  58. package/dist/v8/builtins/legacy.d.ts.map +1 -1
  59. package/dist/v8/builtins/legacy.js +6 -3
  60. package/dist/v8/builtins/legacy.js.map +1 -1
  61. package/dist/v8/canonical.d.ts +8 -0
  62. package/dist/v8/canonical.d.ts.map +1 -1
  63. package/dist/v8/canonical.js +15 -0
  64. package/dist/v8/canonical.js.map +1 -1
  65. package/dist/v8/cli/builtins.d.ts.map +1 -1
  66. package/dist/v8/cli/builtins.js +1 -0
  67. package/dist/v8/cli/builtins.js.map +1 -1
  68. package/dist/v8/cli/check/index.d.ts +10 -0
  69. package/dist/v8/cli/check/index.d.ts.map +1 -1
  70. package/dist/v8/cli/check/index.js +91 -7
  71. package/dist/v8/cli/check/index.js.map +1 -1
  72. package/dist/v8/cli/content.d.ts +12 -25
  73. package/dist/v8/cli/content.d.ts.map +1 -1
  74. package/dist/v8/cli/content.js +46 -75
  75. package/dist/v8/cli/content.js.map +1 -1
  76. package/dist/v8/cli/index.d.ts +1 -2
  77. package/dist/v8/cli/index.d.ts.map +1 -1
  78. package/dist/v8/cli/index.js +1 -2
  79. package/dist/v8/cli/index.js.map +1 -1
  80. package/dist/v8/cli/remote/commands.d.ts +71 -0
  81. package/dist/v8/cli/remote/commands.d.ts.map +1 -0
  82. package/dist/v8/cli/remote/commands.js +644 -0
  83. package/dist/v8/cli/remote/commands.js.map +1 -0
  84. package/dist/v8/cli/remote/diff.d.ts +15 -0
  85. package/dist/v8/cli/remote/diff.d.ts.map +1 -0
  86. package/dist/v8/cli/remote/diff.js +102 -0
  87. package/dist/v8/cli/remote/diff.js.map +1 -0
  88. package/dist/v8/cli/remote/env.d.ts +35 -0
  89. package/dist/v8/cli/remote/env.d.ts.map +1 -0
  90. package/dist/v8/cli/remote/env.js +84 -0
  91. package/dist/v8/cli/remote/env.js.map +1 -0
  92. package/dist/v8/cli/remote/http.d.ts +149 -0
  93. package/dist/v8/cli/remote/http.d.ts.map +1 -0
  94. package/dist/v8/cli/remote/http.js +381 -0
  95. package/dist/v8/cli/remote/http.js.map +1 -0
  96. package/dist/v8/cli/remote/local.d.ts +90 -0
  97. package/dist/v8/cli/remote/local.d.ts.map +1 -0
  98. package/dist/v8/cli/remote/local.js +309 -0
  99. package/dist/v8/cli/remote/local.js.map +1 -0
  100. package/dist/v8/cli/root.d.ts +29 -0
  101. package/dist/v8/cli/root.d.ts.map +1 -1
  102. package/dist/v8/cli/root.js +28 -0
  103. package/dist/v8/cli/root.js.map +1 -1
  104. package/dist/v8/cli/run.d.ts +11 -1
  105. package/dist/v8/cli/run.d.ts.map +1 -1
  106. package/dist/v8/cli/run.js +281 -124
  107. package/dist/v8/cli/run.js.map +1 -1
  108. package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
  109. package/dist/v8/cli/schema/builtinSchemas.js +28 -10
  110. package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
  111. package/dist/v8/cli/schema/generate.js +1 -1
  112. package/dist/v8/cli/schema/generate.js.map +1 -1
  113. package/dist/v8/client.d.ts +9 -9
  114. package/dist/v8/client.d.ts.map +1 -1
  115. package/dist/v8/client.js +138 -50
  116. package/dist/v8/client.js.map +1 -1
  117. package/dist/v8/cms.d.ts +2 -1
  118. package/dist/v8/cms.d.ts.map +1 -1
  119. package/dist/v8/cms.js +55 -46
  120. package/dist/v8/cms.js.map +1 -1
  121. package/dist/v8/content.d.ts +39 -78
  122. package/dist/v8/content.d.ts.map +1 -1
  123. package/dist/v8/content.js +155 -195
  124. package/dist/v8/content.js.map +1 -1
  125. package/dist/v8/contentIndex.d.ts +71 -0
  126. package/dist/v8/contentIndex.d.ts.map +1 -0
  127. package/dist/v8/contentIndex.js +137 -0
  128. package/dist/v8/contentIndex.js.map +1 -0
  129. package/dist/v8/draft.d.ts +7 -7
  130. package/dist/v8/draft.d.ts.map +1 -1
  131. package/dist/v8/draft.js +10 -9
  132. package/dist/v8/draft.js.map +1 -1
  133. package/dist/v8/draftChanges.d.ts +59 -32
  134. package/dist/v8/draftChanges.d.ts.map +1 -1
  135. package/dist/v8/draftChanges.js +139 -75
  136. package/dist/v8/draftChanges.js.map +1 -1
  137. package/dist/v8/errors.d.ts +21 -0
  138. package/dist/v8/errors.d.ts.map +1 -1
  139. package/dist/v8/errors.js +22 -0
  140. package/dist/v8/errors.js.map +1 -1
  141. package/dist/v8/hostedSource.d.ts +57 -0
  142. package/dist/v8/hostedSource.d.ts.map +1 -0
  143. package/dist/v8/hostedSource.js +18 -0
  144. package/dist/v8/hostedSource.js.map +1 -0
  145. package/dist/v8/identity.d.ts +1 -1
  146. package/dist/v8/identity.js +1 -1
  147. package/dist/v8/index.d.ts +1 -0
  148. package/dist/v8/index.d.ts.map +1 -1
  149. package/dist/v8/index.js +1 -0
  150. package/dist/v8/index.js.map +1 -1
  151. package/dist/v8/matchRoute.js +7 -4
  152. package/dist/v8/matchRoute.js.map +1 -1
  153. package/dist/v8/pin.d.ts +34 -0
  154. package/dist/v8/pin.d.ts.map +1 -0
  155. package/dist/v8/pin.js +156 -0
  156. package/dist/v8/pin.js.map +1 -0
  157. package/dist/v8/preview/StudioBridge.d.ts +8 -8
  158. package/dist/v8/preview/StudioBridge.d.ts.map +1 -1
  159. package/dist/v8/preview/StudioBridge.js +8 -10
  160. package/dist/v8/preview/StudioBridge.js.map +1 -1
  161. package/dist/v8/preview/StudioChannel.d.ts +51 -0
  162. package/dist/v8/preview/StudioChannel.d.ts.map +1 -0
  163. package/dist/v8/preview/StudioChannel.js +104 -0
  164. package/dist/v8/preview/StudioChannel.js.map +1 -0
  165. package/dist/v8/preview/index.d.ts +3 -0
  166. package/dist/v8/preview/index.d.ts.map +1 -1
  167. package/dist/v8/preview/index.js +3 -0
  168. package/dist/v8/preview/index.js.map +1 -1
  169. package/dist/v8/remoteLoader.d.ts +42 -5
  170. package/dist/v8/remoteLoader.d.ts.map +1 -1
  171. package/dist/v8/remoteLoader.js +317 -259
  172. package/dist/v8/remoteLoader.js.map +1 -1
  173. package/dist/v8/resolver.d.ts +22 -8
  174. package/dist/v8/resolver.d.ts.map +1 -1
  175. package/dist/v8/resolver.js +37 -32
  176. package/dist/v8/resolver.js.map +1 -1
  177. package/dist/v8/revisionId.d.ts +11 -0
  178. package/dist/v8/revisionId.d.ts.map +1 -0
  179. package/dist/v8/revisionId.js +13 -0
  180. package/dist/v8/revisionId.js.map +1 -0
  181. package/dist/v8/settings.d.ts +6 -6
  182. package/dist/v8/settings.d.ts.map +1 -1
  183. package/dist/v8/settings.js +8 -7
  184. package/dist/v8/settings.js.map +1 -1
  185. package/dist/v8/telemetry.d.ts +4 -2
  186. package/dist/v8/telemetry.d.ts.map +1 -1
  187. package/dist/v8/telemetry.js +2 -2
  188. package/dist/v8/telemetry.js.map +1 -1
  189. package/dist/v8/types.d.ts +52 -40
  190. package/dist/v8/types.d.ts.map +1 -1
  191. package/dist/v8/variants.d.ts +7 -2
  192. package/dist/v8/variants.d.ts.map +1 -1
  193. package/dist/v8/variants.js +21 -24
  194. package/dist/v8/variants.js.map +1 -1
  195. package/dist/v8/vite/index.d.ts +2 -2
  196. package/dist/v8/vite/index.js +1 -1
  197. package/dist/v8/vite/index.js.map +1 -1
  198. package/package.json +1 -1
  199. package/src/index.ts +2 -0
  200. package/src/protocol/channel.ts +127 -0
  201. package/src/protocol/client.ts +2 -2
  202. package/src/protocol/conformance/blockMetaVectors.json +214 -0
  203. package/src/protocol/conformance/contentHashFixtures.ts +74 -0
  204. package/src/protocol/conformance/index.ts +9 -1
  205. package/src/protocol/conformance/vectors.ts +36 -0
  206. package/src/protocol/index.ts +32 -0
  207. package/src/protocol/keys.ts +1 -1
  208. package/src/v8/background.ts +42 -0
  209. package/src/v8/blockMeta.ts +100 -0
  210. package/src/v8/blockStore.ts +124 -0
  211. package/src/v8/boundedJson.ts +20 -2
  212. package/src/v8/boundedMap.ts +53 -0
  213. package/src/v8/builtins/data.ts +26 -2
  214. package/src/v8/builtins/index.ts +2 -1
  215. package/src/v8/builtins/legacy.ts +6 -3
  216. package/src/v8/canonical.ts +15 -0
  217. package/src/v8/cli/builtins.ts +1 -0
  218. package/src/v8/cli/check/index.ts +97 -7
  219. package/src/v8/cli/content.ts +49 -91
  220. package/src/v8/cli/index.ts +1 -2
  221. package/src/v8/cli/remote/commands.ts +834 -0
  222. package/src/v8/cli/remote/diff.ts +100 -0
  223. package/src/v8/cli/remote/env.ts +107 -0
  224. package/src/v8/cli/remote/http.ts +484 -0
  225. package/src/v8/cli/remote/local.ts +343 -0
  226. package/src/v8/cli/root.ts +40 -0
  227. package/src/v8/cli/run.ts +333 -136
  228. package/src/v8/cli/schema/builtinSchemas.ts +29 -10
  229. package/src/v8/cli/schema/generate.ts +1 -1
  230. package/src/v8/client.ts +162 -60
  231. package/src/v8/cms.ts +62 -53
  232. package/src/v8/content.ts +179 -229
  233. package/src/v8/contentIndex.ts +183 -0
  234. package/src/v8/draft.ts +15 -14
  235. package/src/v8/draftChanges.ts +151 -85
  236. package/src/v8/errors.ts +37 -0
  237. package/src/v8/hostedSource.ts +67 -0
  238. package/src/v8/identity.ts +1 -1
  239. package/src/v8/index.ts +1 -0
  240. package/src/v8/matchRoute.ts +7 -4
  241. package/src/v8/pin.ts +176 -0
  242. package/src/v8/preview/StudioBridge.ts +8 -10
  243. package/src/v8/preview/StudioChannel.ts +116 -0
  244. package/src/v8/preview/index.ts +3 -0
  245. package/src/v8/remoteLoader.ts +350 -286
  246. package/src/v8/resolver.ts +56 -33
  247. package/src/v8/revisionId.ts +14 -0
  248. package/src/v8/settings.ts +10 -9
  249. package/src/v8/telemetry.ts +3 -3
  250. package/src/v8/types.ts +52 -40
  251. package/src/v8/variants.ts +30 -26
  252. package/src/v8/vite/index.ts +3 -3
  253. package/dist/v8/cli/serve/server.d.ts +0 -29
  254. package/dist/v8/cli/serve/server.d.ts.map +0 -1
  255. package/dist/v8/cli/serve/server.js +0 -296
  256. package/dist/v8/cli/serve/server.js.map +0 -1
  257. package/src/v8/cli/serve/server.ts +0 -348
@@ -0,0 +1,67 @@
1
+ /**
2
+ * The internal interface between the CMS and hosted releases
3
+ * (`remoteLoader` with `site`): the current and previous index, the block
4
+ * store, release checks, revisions by id and the preview hosts. Not exported
5
+ * from the package.
6
+ */
7
+ import type { BlockStore } from "./blockStore.ts";
8
+ import type { IndexedContent } from "./contentIndex.ts";
9
+
10
+ /** `Symbol.for`, so a second copy of this module recognises the source. */
11
+ export const HOSTED: unique symbol = Symbol.for("decocms.blocks.hostedSource");
12
+
13
+ /**
14
+ * What the current release says about where it may be previewed (PO 2026-10-22):
15
+ * - a list: the hosts Studio wrote (`previewHosts`), enforced;
16
+ * - `"every"`: a remote site with no list published, so every host: no
17
+ * `latest.json` yet (a 404), or one without `previewHosts` (published
18
+ * before Studio wrote the list; its next publish writes one);
19
+ * - `"local"`: content with no remote site (no release metadata at all):
20
+ * `createCMS`'s `preview.hosts` when set, else every host;
21
+ * - `"unknown"`: the metadata couldn't be read (a network error, a 5xx, a
22
+ * malformed `latest.json`). That isn't "missing": previews stay closed.
23
+ */
24
+ export type ReleasePreviewHosts = readonly string[] | "every" | "local" | "unknown";
25
+
26
+ export interface HostedSource {
27
+ readonly [HOSTED]: true;
28
+ /** The `interval` the loader was created with; `createCMS` reads it. */
29
+ readonly interval: number | undefined;
30
+ /** Bodies by hash, shared by every request of the isolate. */
31
+ readonly blocks: BlockStore;
32
+ /**
33
+ * The current index: the one held, or the first load (shared by concurrent
34
+ * callers; a failure isn't kept and rejects with a `ContentUnavailableError`).
35
+ */
36
+ current(): Promise<IndexedContent>;
37
+ /** The current index from memory, `undefined` before the first load. */
38
+ peek(): IndexedContent | undefined;
39
+ /** The index that was current before the last adoption, if any. */
40
+ previous(): IndexedContent | undefined;
41
+ /**
42
+ * Reads `latest.json` again (with `If-None-Match` on the held ETag) and adopts
43
+ * it when its `sequence` is higher. Before the first load, it is that load.
44
+ */
45
+ poll(): Promise<{ updated: boolean }>;
46
+ /** A published revision's index by id (`revisions/<id>/index.json`), kept a few at a time. */
47
+ revision(id: string): Promise<IndexedContent>;
48
+ /** The preview hosts (see `ReleasePreviewHosts`); `peek` never reads the network. */
49
+ previewHosts(peek: boolean): Promise<ReleasePreviewHosts>;
50
+ }
51
+
52
+ export function isHostedSource(value: unknown): value is HostedSource {
53
+ return (
54
+ typeof value === "object" && value !== null && (value as Partial<HostedSource>)[HOSTED] === true
55
+ );
56
+ }
57
+
58
+ /**
59
+ * A release's `previewHosts` as Studio wrote them: the strings of a list;
60
+ * `undefined` when the field is absent (no list written); an empty list (no
61
+ * host) for a field that isn't a list, so a malformed one never opens previews.
62
+ */
63
+ export function previewHostsOf(value: unknown): string[] | undefined {
64
+ if (value === undefined) return undefined;
65
+ if (!Array.isArray(value)) return [];
66
+ return value.filter((host): host is string => typeof host === "string");
67
+ }
@@ -9,7 +9,7 @@ const OBJECT_IDS = Symbol.for("decocms.blocks.cms-loader-ids");
9
9
 
10
10
  /**
11
11
  * `module:<root>` for a content module, which names the `.deco` folder it was
12
- * generated from in a `root` field `deco content` writes (not part of the
12
+ * generated from in a `root` field the content module writer adds (not part of the
13
13
  * documented `Snapshot`); `loader#<id>`/`module#<id>` otherwise.
14
14
  */
15
15
  export function contentIdentity(content: Snapshot | Loader): string {
package/src/v8/index.ts CHANGED
@@ -8,6 +8,7 @@
8
8
  export { createCMS, resetForTests } from "./cms.ts";
9
9
  export { currentClient } from "./current.ts";
10
10
  export { formatDraftPointer, parseDraftPointer, parseVersion } from "./draft.ts";
11
+ export { ContentUnavailableError, type ContentUnavailableReason } from "./errors.ts";
11
12
  export { matchRoute } from "./matchRoute.ts";
12
13
  export { remoteLoader } from "./remoteLoader.ts";
13
14
  export type {
@@ -118,16 +118,19 @@ export function findRouteConflicts<T extends { path: string }>(entries: readonly
118
118
 
119
119
  /**
120
120
  * Both redirect shapes: the flat built-in `{ from, to, permanent, status?,
121
- * discardQueryParameters? }` and the site editor's legacy nested
122
- * `{ redirect: { from, to, type } }` (see /next/studio-compatibility), where
123
- * `permanent` is a 301 and anything else a 307.
121
+ * discardQueryParameters? }`, where a valid `status` wins, `permanent: true`
122
+ * is a 301 and a temporary redirect (`permanent` false or unset) a 307, and
123
+ * the site editor's legacy nested `{ redirect: { from, to, type } }` (see
124
+ * /next/studio-compatibility), where `permanent` is a 301 and anything else
125
+ * a 307. A temporary redirect is a 307, not a 302, so a client never turns a
126
+ * `POST` into a `GET` on the way.
124
127
  */
125
128
  function normalizeRedirect(input: unknown): NormalizedRedirect | null {
126
129
  if (typeof input !== "object" || input === null) return null;
127
130
  const nested = (input as Partial<LegacyRedirect>).redirect;
128
131
  const flat = (nested ?? input) as Partial<Redirect & LegacyRedirect["redirect"]>;
129
132
  if (typeof flat.from !== "string" || typeof flat.to !== "string") return null;
130
- let status: RedirectStatus = flat.permanent === true ? 301 : 302;
133
+ let status: RedirectStatus = flat.permanent === true ? 301 : 307;
131
134
  if (nested) status = flat.type === "permanent" ? 301 : 307;
132
135
  else if (STATUSES.has(flat.status as number)) status = flat.status as RedirectStatus;
133
136
  const { from, to, discardQueryParameters } = flat;
package/src/v8/pin.ts ADDED
@@ -0,0 +1,176 @@
1
+ /**
2
+ * What one request reads (see /next/hosted-releases-internals): the index it
3
+ * pinned and every body it fetched, for its whole life. Lazy and streamed
4
+ * sections and names read at run time use the same data, and LRU eviction
5
+ * can't pull a body out mid-request. Whether a name exists comes only from the
6
+ * pinned index; a body that isn't pinned is an awaited fetch by its hash.
7
+ *
8
+ * `choosePin` is the previous-index rule: after a publish, a request whose
9
+ * subtree under the new index has blocks missing, while its subtree under the
10
+ * previous index is all cached, is served on the previous index, and the
11
+ * missing hashes are fetched in the background. A draft never is.
12
+ */
13
+ import { runInBackground } from "./background.ts";
14
+ import {
15
+ bodyInMemory,
16
+ entryOf,
17
+ hasEntry,
18
+ type IndexedContent,
19
+ redirectsBlockOf,
20
+ walk,
21
+ } from "./contentIndex.ts";
22
+ import { errors } from "./errors.ts";
23
+ import type { ForcedVariant } from "./types.ts";
24
+ import { forceBody } from "./variants.ts";
25
+
26
+ /** What a client is handed to read: the current index, the previous one, a draft's forced variants. */
27
+ export interface Selection {
28
+ current: IndexedContent;
29
+ previous?: IndexedContent;
30
+ variants?: readonly ForcedVariant[];
31
+ }
32
+
33
+ export class Pin {
34
+ readonly content: IndexedContent;
35
+ readonly #variants: readonly ForcedVariant[] | undefined;
36
+ /** Bodies this request fetched or found cached, by hash. */
37
+ readonly #bodies = new Map<string, unknown>();
38
+ /** Fetches that failed in this request, by hash: every later use fails fast. */
39
+ readonly #failed = new Map<string, unknown>();
40
+ /** Bodies in flight or read, by name (forced variants applied once). */
41
+ readonly #byName = new Map<string, Promise<unknown>>();
42
+
43
+ constructor(content: IndexedContent, variants?: readonly ForcedVariant[]) {
44
+ this.content = content;
45
+ this.#variants = variants !== undefined && variants.length > 0 ? variants : undefined;
46
+ }
47
+
48
+ get aliases(): Readonly<Record<string, string>> | undefined {
49
+ return this.content.aliases;
50
+ }
51
+
52
+ has(name: string): boolean {
53
+ return hasEntry(this.content, name);
54
+ }
55
+
56
+ /** The body of a name the pinned index has. Rejects with `LOADER_FAILED` when it can't be fetched. */
57
+ body(name: string, path: (string | number)[] = []): Promise<unknown> {
58
+ let pending = this.#byName.get(name);
59
+ if (pending === undefined) {
60
+ pending = this.#read(name).then((body) =>
61
+ this.#variants === undefined ? body : forceBody(name, body, this.#variants, this.aliases),
62
+ );
63
+ this.#byName.set(name, pending);
64
+ }
65
+ return pending.catch((error: unknown) => {
66
+ throw errors.blockUnavailable(name, path, error);
67
+ });
68
+ }
69
+
70
+ /**
71
+ * One parallel wave of every body `names` need that isn't pinned yet: an
72
+ * LRU hit is pinned at once, the rest are fetched. Never rejects; a failure
73
+ * is remembered for the body's later use.
74
+ */
75
+ async wave(names: readonly string[]): Promise<void> {
76
+ const source = this.content.source;
77
+ if (source === undefined) return;
78
+ const fetches: Promise<unknown>[] = [];
79
+ for (const name of names) {
80
+ if (bodyInMemory(this.content, name)) continue;
81
+ const hash = entryOf(this.content, name)?.version;
82
+ if (hash === undefined || this.#bodies.has(hash) || this.#failed.has(hash)) continue;
83
+ const cached = source.peek(hash);
84
+ if (cached !== undefined) {
85
+ this.#bodies.set(hash, cached);
86
+ continue;
87
+ }
88
+ fetches.push(this.#fetch(hash, name).catch(() => {}));
89
+ }
90
+ await Promise.all(fetches);
91
+ }
92
+
93
+ async #read(name: string): Promise<unknown> {
94
+ const content = this.content;
95
+ if (content.held?.has(name)) return content.held.get(name);
96
+ const entry = entryOf(content, name);
97
+ if (entry === undefined) return undefined;
98
+ if (entry.data !== undefined) return entry.data;
99
+ const hash = entry.version;
100
+ if (this.#bodies.has(hash)) return this.#bodies.get(hash);
101
+ if (this.#failed.has(hash)) throw this.#failed.get(hash);
102
+ const source = content.source;
103
+ if (source === undefined) return undefined;
104
+ const cached = source.peek(hash);
105
+ if (cached !== undefined) {
106
+ this.#bodies.set(hash, cached);
107
+ return cached;
108
+ }
109
+ return this.#fetch(hash, name);
110
+ }
111
+
112
+ #fetch(hash: string, name: string): Promise<unknown> {
113
+ return (this.content.source as NonNullable<IndexedContent["source"]>).get(hash, name).then(
114
+ (body) => {
115
+ this.#bodies.set(hash, body);
116
+ return body;
117
+ },
118
+ (error: unknown) => {
119
+ this.#failed.set(hash, error);
120
+ throw error;
121
+ },
122
+ );
123
+ }
124
+ }
125
+
126
+ /** The walk a request starts from: its page (when known) plus the redirects block. */
127
+ export function rootsOf(content: IndexedContent, name: string | undefined): string[] {
128
+ const roots = name !== undefined ? [name] : [];
129
+ const redirects = redirectsBlockOf(content);
130
+ if (redirects !== null && redirects !== name) roots.push(redirects);
131
+ return roots;
132
+ }
133
+
134
+ /** Every body of `names` is in memory or in the LRU. */
135
+ function allCached(content: IndexedContent, names: readonly string[]): boolean {
136
+ const source = content.source;
137
+ return names.every((name) => {
138
+ if (bodyInMemory(content, name)) return true;
139
+ const hash = entryOf(content, name)?.version;
140
+ return hash !== undefined && source?.peek(hash) !== undefined;
141
+ });
142
+ }
143
+
144
+ /**
145
+ * The index a request pins (contract §3.4 rule 1): the current one when its
146
+ * walk is cached, when there is no previous index, or for a draft; the
147
+ * previous one when its walk is cached and the current one's isn't (and the
148
+ * page has the same path in both), warming the current one's missing blocks in
149
+ * the background.
150
+ */
151
+ export function choosePin(selection: Selection, name: string | undefined): Pin {
152
+ const { current, previous, variants } = selection;
153
+ const onCurrent = () => new Pin(current, variants);
154
+ if (current.draft || previous === undefined || current.source === undefined) return onCurrent();
155
+ const fresh = walk(current, rootsOf(current, name));
156
+ if (allCached(current, fresh)) return onCurrent();
157
+ if (name !== undefined) {
158
+ const now = entryOf(current, name);
159
+ const before = entryOf(previous, name);
160
+ if (now === undefined || before === undefined || now.meta?.path !== before.meta?.path) {
161
+ return onCurrent();
162
+ }
163
+ }
164
+ if (!allCached(previous, walk(previous, rootsOf(previous, name)))) return onCurrent();
165
+ const source = current.source;
166
+ const missing = fresh.filter((n) => !bodyInMemory(current, n));
167
+ runInBackground(() =>
168
+ Promise.all(
169
+ missing.map((n) => {
170
+ const hash = current.blocks[n]?.version as string;
171
+ return source.peek(hash) !== undefined ? undefined : source.get(hash, n).catch(() => {});
172
+ }),
173
+ ),
174
+ );
175
+ return new Pin(previous, variants);
176
+ }
@@ -2,11 +2,11 @@
2
2
  * The Studio bridge: what lets Deco Studio's site editor drive a page it
3
3
  * shows in an iframe (v7's `LiveControls`).
4
4
  *
5
- * When the frame loads, Studio posts `{ type: "editor::inject", args: {
6
- * script } }` to it; the bridge runs that script, which draws the section
7
- * overlay and handles Studio's `cms-editor::*` messages itself. The bridge also
8
- * answers v7's `scrollToComponent` and `editor::rerender` (same-origin
9
- * navigation only).
5
+ * The bridge answers v7's `scrollToComponent` and `editor::rerender`
6
+ * (same-origin navigation only). It runs no code Studio sends: Studio's own
7
+ * editor code (the section overlay, the HTML swap after a save) reaches the
8
+ * page through the Studio channel (`StudioChannel`: `deco:ready` /
9
+ * `deco:load`), which imports versioned modules from Studio's exact origins.
10
10
  *
11
11
  * In a frame it answers only messages from the parent window whose origin is
12
12
  * one of `trustedOrigins`. On a page that isn't framed it answers no message;
@@ -19,8 +19,8 @@
19
19
  * origin: under a test runner that proxies the site, the runner's own messages
20
20
  * would navigate the page.
21
21
  *
22
- * An inline script, not an effect: Studio posts on the frame's `load` event,
23
- * which can come before React hydrates, so the listener must exist as soon as
22
+ * An inline script, not an effect: Studio may post as soon as the frame
23
+ * loads, which can come before React hydrates, so the listener must exist as soon as
24
24
  * the HTML is parsed. No hooks, so it renders from a Next.js Server Component
25
25
  * as well as from a client tree. Its settings travel in `data-*` attributes;
26
26
  * the script itself is static.
@@ -103,9 +103,7 @@ if(window.self!==window.top){
103
103
  var d=e.data;
104
104
  if(!d||typeof d!=="object")return;
105
105
  var a=d.args||{};
106
- if(d.type==="editor::inject"){
107
- if(typeof a.script==="string"){try{(0,eval)(a.script)}catch(err){console.error("[deco] Studio script failed:",err)}}
108
- }else if(d.type==="scrollToComponent"){
106
+ if(d.type==="scrollToComponent"){
109
107
  var id=typeof a.id==="string"?a.id:"";
110
108
  if(!id)return;
111
109
  var el=null,all=document.querySelectorAll("[data-manifest-key]");
@@ -0,0 +1,116 @@
1
+ /**
2
+ * The Studio channel's bootstrap (protocol: `src/protocol/channel.ts`,
3
+ * exported for Studio from `@decocms/blocks/protocol`): a tiny inline script every page
4
+ * carries, production included, so code that belongs to Studio's site editor
5
+ * (the section wireframe, the HTML swap after a save) ships with Studio and
6
+ * runs inside the page only when Studio frames it. It is the only way Studio
7
+ * runs code in the page: the bridge evaluates no script Studio sends.
8
+ *
9
+ * The page trusts a list of Studio origins (`studioOrigins`), each one exact:
10
+ * no wildcard, no prefix. Framed (`window.parent !== window`), it posts
11
+ * `{ type: "deco:ready" }` with each trusted origin as the `targetOrigin`, so
12
+ * the browser delivers it only to a parent at one of them, and to no other
13
+ * (where the browser names the parent's origin, `location.ancestorOrigins`,
14
+ * the page posts only to that one, when it's trusted). It imports each module
15
+ * Studio names in `{ type: "deco:load", url }` once, when the message comes
16
+ * from the parent at exactly one of the trusted origins and the URL is on
17
+ * exactly the origin of that message; the module talks to that origin alone.
18
+ * Not framed, or with no valid origin, it does nothing (and a parent of
19
+ * another origin never gets the `deco:ready`, nor a message it sends an answer).
20
+ *
21
+ * An inline script, like `StudioBridge`: it listens before React hydrates, and
22
+ * renders from a Next.js Server Component. Studio's origins travel in a
23
+ * `data-*` attribute; the script itself is static. The binding owns the list
24
+ * (its option, its environment override, Studio's production and staging
25
+ * origins by default) and adds the same origins to the page's CSP
26
+ * `script-src` (the modules) and `frame-ancestors`; `@decocms/blocks` reads no
27
+ * environment. The inline script itself needs the site's CSP `nonce`.
28
+ */
29
+ import { createElement, type ReactNode } from "react";
30
+
31
+ export interface StudioChannelProps {
32
+ /**
33
+ * Studio's origins (`https://studio.decocms.com`): the only origins whose
34
+ * messages the page accepts and whose modules it imports, each compared
35
+ * exactly. The binding passes them; a value that isn't an http(s) origin is
36
+ * dropped, and with none left the channel is off.
37
+ */
38
+ studioOrigins: readonly string[];
39
+ /** The CSP nonce for the inline script, when the site sends one. */
40
+ nonce?: string;
41
+ }
42
+
43
+ const GLOBAL = "__decoStudioChannel";
44
+
45
+ /**
46
+ * The browser side. Static. `import(h)` is the one dynamic import (a classic
47
+ * script may `import()`); the module's default export gets the context.
48
+ */
49
+ export const STUDIO_CHANNEL_SCRIPT = `(function(){
50
+ var s=document.currentScript,w=window,p=w.parent,t;
51
+ if(!s||p===w||w.${GLOBAL})return;
52
+ try{t=JSON.parse(s.getAttribute("data-studio-origins")||"[]")}catch(e){}
53
+ if(!Array.isArray(t)||!t.length)return;
54
+ w.${GLOBAL}=true;
55
+ var seen={},fns=[];
56
+ function send(o,m){p.postMessage(m,o)}
57
+ w.addEventListener("message",function(e){
58
+ var d=e.data,o=e.origin;
59
+ if(t.indexOf(o)<0||e.source!==p||!d||typeof d.type!=="string")return;
60
+ if(d.type!=="deco:load"){
61
+ if(d.type.indexOf("deco:")===0)return;
62
+ fns.slice().forEach(function(l){if(l.o===o)try{l.f(d)}catch(err){console.error("[deco] Studio module failed:",err)}});
63
+ return}
64
+ var h;
65
+ if(typeof d.url!=="string")return;
66
+ try{h=new URL(d.url)}catch(err){return}
67
+ if(h.origin!==o)return;
68
+ h=h.href;
69
+ if(seen[h])return;
70
+ seen[h]=1;
71
+ import(h).then(function(m){
72
+ if(typeof m.default!=="function")throw new TypeError("no default export");
73
+ return m.default({studioOrigin:o,url:h,send:function(x){send(o,x)},on:function(f){var l={o:o,f:f};fns.push(l);return function(){var i=fns.indexOf(l);if(i>=0)fns.splice(i,1)}}})
74
+ }).then(function(){send(o,{type:"deco:loaded",url:h})},function(err){
75
+ delete seen[h];
76
+ send(o,{type:"deco:load-error",url:h,message:String(err&&err.message||err)})})
77
+ });
78
+ var a=w.location.ancestorOrigins;
79
+ (a&&a.length?t.indexOf(a[0])<0?[]:[a[0]]:t).forEach(function(o){send(o,{type:"deco:ready"})});
80
+ })();`;
81
+
82
+ /** `value` as an exact http(s) origin (`https://host[:port]`; no wildcard), or `""`. */
83
+ function asOrigin(value: unknown): string {
84
+ if (typeof value !== "string") return "";
85
+ try {
86
+ const url = new URL(value);
87
+ if (url.protocol !== "https:" && url.protocol !== "http:") return "";
88
+ // A host name or an IP address: a URL parser accepts `*` in a host, which a
89
+ // CSP source would read as a wildcard.
90
+ if (!/^(\[[0-9a-f:.]+\]|[a-z0-9_.-]+)$/.test(url.hostname)) return "";
91
+ return value === url.origin || value === `${url.origin}/` ? url.origin : "";
92
+ } catch {
93
+ return "";
94
+ }
95
+ }
96
+
97
+ /** The valid origins of `list`, normalized, each once. */
98
+ function origins(list: readonly string[] | undefined): string[] {
99
+ return [...new Set((Array.isArray(list) ? list : []).map(asOrigin).filter(Boolean))];
100
+ }
101
+
102
+ /**
103
+ * The Studio channel's bootstrap. Render it once, in the root layout's
104
+ * `<body>` (the bindings' documents do).
105
+ */
106
+ export function StudioChannel({ studioOrigins, nonce }: StudioChannelProps): ReactNode {
107
+ return createElement("script", {
108
+ "data-deco-studio-channel": "",
109
+ // Always rendered, even when off (`[]`), so the server's and the client's
110
+ // trees match when only one side sees the binding's environment override.
111
+ "data-studio-origins": JSON.stringify(origins(studioOrigins)),
112
+ nonce,
113
+ suppressHydrationWarning: true,
114
+ dangerouslySetInnerHTML: { __html: STUDIO_CHANNEL_SCRIPT },
115
+ });
116
+ }
@@ -8,6 +8,8 @@
8
8
  * - `StudioBridge`: lets Studio's site editor drive the page when Studio
9
9
  * frames it, and opens the page in Studio on "." when it isn't framed.
10
10
  * - `StudioPage`: what a page renders to tell the bridge which page it is.
11
+ * - `StudioChannel`: the bootstrap through which Studio loads its own modules
12
+ * into the page when it frames it (protocol: `@decocms/blocks/protocol`).
11
13
  */
12
14
  export { DraftBadge, type DraftBadgeProps } from "./DraftBadge.tsx";
13
15
  export {
@@ -16,3 +18,4 @@ export {
16
18
  StudioPage,
17
19
  type StudioPageInfo,
18
20
  } from "./StudioBridge.ts";
21
+ export { StudioChannel, type StudioChannelProps } from "./StudioChannel.ts";