@decocms/blocks 8.9.1 → 8.11.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 (409) 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 +2 -2
  77. package/dist/v8/cli/index.d.ts.map +1 -1
  78. package/dist/v8/cli/index.js +2 -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 +13 -1
  105. package/dist/v8/cli/run.d.ts.map +1 -1
  106. package/dist/v8/cli/run.js +310 -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/cli/skills.d.ts +25 -0
  114. package/dist/v8/cli/skills.d.ts.map +1 -0
  115. package/dist/v8/cli/skills.js +70 -0
  116. package/dist/v8/cli/skills.js.map +1 -0
  117. package/dist/v8/client.d.ts +9 -9
  118. package/dist/v8/client.d.ts.map +1 -1
  119. package/dist/v8/client.js +138 -50
  120. package/dist/v8/client.js.map +1 -1
  121. package/dist/v8/cms.d.ts +2 -1
  122. package/dist/v8/cms.d.ts.map +1 -1
  123. package/dist/v8/cms.js +55 -46
  124. package/dist/v8/cms.js.map +1 -1
  125. package/dist/v8/content.d.ts +39 -78
  126. package/dist/v8/content.d.ts.map +1 -1
  127. package/dist/v8/content.js +155 -195
  128. package/dist/v8/content.js.map +1 -1
  129. package/dist/v8/contentIndex.d.ts +71 -0
  130. package/dist/v8/contentIndex.d.ts.map +1 -0
  131. package/dist/v8/contentIndex.js +137 -0
  132. package/dist/v8/contentIndex.js.map +1 -0
  133. package/dist/v8/draft.d.ts +7 -7
  134. package/dist/v8/draft.d.ts.map +1 -1
  135. package/dist/v8/draft.js +10 -9
  136. package/dist/v8/draft.js.map +1 -1
  137. package/dist/v8/draftChanges.d.ts +59 -32
  138. package/dist/v8/draftChanges.d.ts.map +1 -1
  139. package/dist/v8/draftChanges.js +139 -75
  140. package/dist/v8/draftChanges.js.map +1 -1
  141. package/dist/v8/errors.d.ts +21 -0
  142. package/dist/v8/errors.d.ts.map +1 -1
  143. package/dist/v8/errors.js +22 -0
  144. package/dist/v8/errors.js.map +1 -1
  145. package/dist/v8/hostedSource.d.ts +57 -0
  146. package/dist/v8/hostedSource.d.ts.map +1 -0
  147. package/dist/v8/hostedSource.js +18 -0
  148. package/dist/v8/hostedSource.js.map +1 -0
  149. package/dist/v8/identity.d.ts +1 -1
  150. package/dist/v8/identity.js +1 -1
  151. package/dist/v8/index.d.ts +1 -0
  152. package/dist/v8/index.d.ts.map +1 -1
  153. package/dist/v8/index.js +1 -0
  154. package/dist/v8/index.js.map +1 -1
  155. package/dist/v8/matchRoute.js +7 -4
  156. package/dist/v8/matchRoute.js.map +1 -1
  157. package/dist/v8/pin.d.ts +34 -0
  158. package/dist/v8/pin.d.ts.map +1 -0
  159. package/dist/v8/pin.js +156 -0
  160. package/dist/v8/pin.js.map +1 -0
  161. package/dist/v8/preview/StudioBridge.d.ts +8 -8
  162. package/dist/v8/preview/StudioBridge.d.ts.map +1 -1
  163. package/dist/v8/preview/StudioBridge.js +8 -10
  164. package/dist/v8/preview/StudioBridge.js.map +1 -1
  165. package/dist/v8/preview/StudioChannel.d.ts +51 -0
  166. package/dist/v8/preview/StudioChannel.d.ts.map +1 -0
  167. package/dist/v8/preview/StudioChannel.js +104 -0
  168. package/dist/v8/preview/StudioChannel.js.map +1 -0
  169. package/dist/v8/preview/index.d.ts +3 -0
  170. package/dist/v8/preview/index.d.ts.map +1 -1
  171. package/dist/v8/preview/index.js +3 -0
  172. package/dist/v8/preview/index.js.map +1 -1
  173. package/dist/v8/remoteLoader.d.ts +42 -5
  174. package/dist/v8/remoteLoader.d.ts.map +1 -1
  175. package/dist/v8/remoteLoader.js +317 -259
  176. package/dist/v8/remoteLoader.js.map +1 -1
  177. package/dist/v8/resolver.d.ts +22 -8
  178. package/dist/v8/resolver.d.ts.map +1 -1
  179. package/dist/v8/resolver.js +37 -32
  180. package/dist/v8/resolver.js.map +1 -1
  181. package/dist/v8/revisionId.d.ts +11 -0
  182. package/dist/v8/revisionId.d.ts.map +1 -0
  183. package/dist/v8/revisionId.js +13 -0
  184. package/dist/v8/revisionId.js.map +1 -0
  185. package/dist/v8/settings.d.ts +6 -6
  186. package/dist/v8/settings.d.ts.map +1 -1
  187. package/dist/v8/settings.js +8 -7
  188. package/dist/v8/settings.js.map +1 -1
  189. package/dist/v8/telemetry.d.ts +4 -2
  190. package/dist/v8/telemetry.d.ts.map +1 -1
  191. package/dist/v8/telemetry.js +2 -2
  192. package/dist/v8/telemetry.js.map +1 -1
  193. package/dist/v8/types.d.ts +52 -40
  194. package/dist/v8/types.d.ts.map +1 -1
  195. package/dist/v8/variants.d.ts +7 -2
  196. package/dist/v8/variants.d.ts.map +1 -1
  197. package/dist/v8/variants.js +21 -24
  198. package/dist/v8/variants.js.map +1 -1
  199. package/dist/v8/vite/index.d.ts +2 -2
  200. package/dist/v8/vite/index.js +1 -1
  201. package/dist/v8/vite/index.js.map +1 -1
  202. package/package.json +3 -2
  203. package/skills/deco-apps/SKILL.md +82 -0
  204. package/skills/deco-apps/references/algolia.md +51 -0
  205. package/skills/deco-apps/references/commerce-types.md +132 -0
  206. package/skills/deco-apps/references/instrumented-fetch.md +144 -0
  207. package/skills/deco-apps/references/magento.md +55 -0
  208. package/skills/deco-apps/references/messaging.md +76 -0
  209. package/skills/deco-apps/references/shopify.md +69 -0
  210. package/skills/deco-apps/references/vendored-loaders.md +135 -0
  211. package/skills/deco-apps/references/vtex-cookies-auth.md +146 -0
  212. package/skills/deco-apps/references/vtex.md +150 -0
  213. package/skills/deco-apps/references/wake.md +75 -0
  214. package/skills/deco-cms-schema/SKILL.md +80 -0
  215. package/skills/deco-cms-schema/references/annotations.md +147 -0
  216. package/skills/deco-cms-schema/references/content-hygiene.md +91 -0
  217. package/skills/deco-cms-schema/references/formats-and-widgets.md +112 -0
  218. package/skills/deco-cms-schema/references/shapes-that-break-the-form.md +165 -0
  219. package/skills/deco-cms-schema/references/types-to-fields.md +112 -0
  220. package/skills/deco-go-live/SKILL.md +127 -0
  221. package/skills/deco-go-live/references/content-releases.md +90 -0
  222. package/skills/deco-go-live/references/process.md +106 -0
  223. package/skills/deco-go-live/references/proxy-headers-checkout.md +139 -0
  224. package/skills/deco-go-live/references/qa-checklist-template.md +150 -0
  225. package/skills/deco-go-live/references/qa-inventory.md +120 -0
  226. package/skills/deco-go-live/references/react-spa.md +183 -0
  227. package/skills/deco-go-live/references/seo-analytics.md +114 -0
  228. package/skills/deco-go-live/references/traffic-ramp.md +87 -0
  229. package/skills/deco-go-live/references/worker-runtime.md +93 -0
  230. package/skills/deco-go-live/scripts/preflight.sh +221 -0
  231. package/skills/deco-images-media/SKILL.md +64 -0
  232. package/skills/deco-images-media/references/asset-upload.md +191 -0
  233. package/skills/deco-images-media/references/components-per-binding.md +167 -0
  234. package/skills/deco-images-media/references/providers-and-cdn.md +145 -0
  235. package/skills/deco-images-media/references/quality-and-lcp.md +93 -0
  236. package/skills/deco-images-media/references/troubleshooting.md +147 -0
  237. package/skills/deco-images-media/references/video.md +122 -0
  238. package/skills/deco-make-editable/SKILL.md +94 -0
  239. package/skills/deco-make-editable/references/a11y-and-e2e.md +104 -0
  240. package/skills/deco-make-editable/references/content-shapes.md +198 -0
  241. package/skills/deco-make-editable/references/from-figma.md +110 -0
  242. package/skills/deco-make-editable/references/i18n-fields.md +112 -0
  243. package/skills/deco-make-editable/references/inventory.md +79 -0
  244. package/skills/deco-make-editable/references/pixel-parity.md +175 -0
  245. package/skills/deco-make-editable/references/seeding-content.md +119 -0
  246. package/skills/deco-nextjs-site/SKILL.md +74 -0
  247. package/skills/deco-nextjs-site/references/caching.md +116 -0
  248. package/skills/deco-nextjs-site/references/metadata-seo.md +100 -0
  249. package/skills/deco-nextjs-site/references/proxy-and-drafts.md +177 -0
  250. package/skills/deco-nextjs-site/references/rendering.md +171 -0
  251. package/skills/deco-nextjs-site/references/setup.md +190 -0
  252. package/skills/deco-nextjs-site/references/studio-bridge.md +130 -0
  253. package/skills/deco-site-authoring/SKILL.md +75 -0
  254. package/skills/deco-site-authoring/references/lazy-blocks.md +155 -0
  255. package/skills/deco-site-authoring/references/loaders.md +192 -0
  256. package/skills/deco-site-authoring/references/pages.md +156 -0
  257. package/skills/deco-site-authoring/references/review-before-publish.md +95 -0
  258. package/skills/deco-site-authoring/references/sections.md +158 -0
  259. package/skills/deco-site-authoring/references/seo.md +116 -0
  260. package/skills/deco-site-workflow/SKILL.md +92 -0
  261. package/skills/deco-site-workflow/references/check.md +116 -0
  262. package/skills/deco-site-workflow/references/cli-reference.md +104 -0
  263. package/skills/deco-site-workflow/references/content-layout.md +93 -0
  264. package/skills/deco-site-workflow/references/content-sync.md +146 -0
  265. package/skills/deco-site-workflow/references/delivery.md +51 -0
  266. package/skills/deco-site-workflow/references/deploy.md +87 -0
  267. package/skills/deco-site-workflow/references/schema.md +173 -0
  268. package/skills/deco-site-workflow/references/serve-and-studio.md +138 -0
  269. package/skills/deco-storefront-caching/SKILL.md +67 -0
  270. package/skills/deco-storefront-caching/references/cache-keys.md +129 -0
  271. package/skills/deco-storefront-caching/references/case-study.md +82 -0
  272. package/skills/deco-storefront-caching/references/egress-traps.md +153 -0
  273. package/skills/deco-storefront-caching/references/nextjs.md +60 -0
  274. package/skills/deco-storefront-caching/references/perf-guardrails-e2e.md +123 -0
  275. package/skills/deco-storefront-caching/references/verification.md +136 -0
  276. package/skills/deco-storefront-caching/references/worker-page-cache.md +160 -0
  277. package/skills/deco-storefront-caching/scripts/html-anatomy.mjs +97 -0
  278. package/skills/deco-storefront-performance/SKILL.md +81 -0
  279. package/skills/deco-storefront-performance/references/cls-third-party.md +90 -0
  280. package/skills/deco-storefront-performance/references/hydration.md +116 -0
  281. package/skills/deco-storefront-performance/references/loaders-n-plus-1.md +107 -0
  282. package/skills/deco-storefront-performance/references/navigation-ux.md +130 -0
  283. package/skills/deco-storefront-performance/references/payload-trim.md +163 -0
  284. package/skills/deco-storefront-performance/references/render-location.md +152 -0
  285. package/skills/deco-storefront-performance/references/request-dedupe.md +147 -0
  286. package/skills/deco-storefront-performance/references/trace-reading.md +79 -0
  287. package/skills/deco-storefront-performance/references/worker-memory.md +176 -0
  288. package/skills/deco-tanstack-site/SKILL.md +68 -0
  289. package/skills/deco-tanstack-site/references/document-and-security.md +155 -0
  290. package/skills/deco-tanstack-site/references/router-and-navigation.md +137 -0
  291. package/skills/deco-tanstack-site/references/routes-and-pages.md +183 -0
  292. package/skills/deco-tanstack-site/references/server-functions.md +123 -0
  293. package/skills/deco-tanstack-site/references/setup.md +153 -0
  294. package/skills/deco-tanstack-site/references/worker-entry.md +152 -0
  295. package/skills/deco-v7-to-v8-migration/SKILL.md +122 -0
  296. package/skills/deco-v7-to-v8-migration/references/caching-and-privacy.md +10 -0
  297. package/skills/deco-v7-to-v8-migration/references/content.md +25 -0
  298. package/skills/deco-v7-to-v8-migration/references/editor-forms.md +27 -0
  299. package/skills/deco-v7-to-v8-migration/references/faststore.md +40 -0
  300. package/skills/deco-v7-to-v8-migration/references/framework-code.md +53 -0
  301. package/skills/deco-v7-to-v8-migration/references/hosted-releases.md +16 -0
  302. package/skills/deco-v7-to-v8-migration/references/import-map.md +81 -0
  303. package/skills/deco-v7-to-v8-migration/references/legacy-names.md +33 -0
  304. package/skills/deco-v7-to-v8-migration/references/manual-steps.md +10 -0
  305. package/skills/deco-v7-to-v8-migration/references/native-apps.md +43 -0
  306. package/skills/deco-v7-to-v8-migration/references/nextjs.md +19 -0
  307. package/skills/deco-v7-to-v8-migration/references/parity.md +66 -0
  308. package/skills/deco-v7-to-v8-migration/references/rendering.md +13 -0
  309. package/skills/deco-v7-to-v8-migration/references/running-the-script.md +29 -0
  310. package/skills/deco-v7-to-v8-migration/references/script-steps.md +13 -0
  311. package/skills/deco-v7-to-v8-migration/references/server-functions.md +15 -0
  312. package/skills/deco-v7-to-v8-migration/references/sibling-port.md +11 -0
  313. package/skills/deco-v7-to-v8-migration/references/site-code.md +15 -0
  314. package/skills/deco-v7-to-v8-migration/references/telemetry.md +10 -0
  315. package/skills/deco-v7-to-v8-migration/references/verify.md +20 -0
  316. package/skills/deco-v7-to-v8-migration/scripts/apps.ts +297 -0
  317. package/skills/deco-v7-to-v8-migration/scripts/asyncRendering.ts +73 -0
  318. package/skills/deco-v7-to-v8-migration/scripts/blockMap.ts +584 -0
  319. package/skills/deco-v7-to-v8-migration/scripts/ciSchemaCheck.ts +145 -0
  320. package/skills/deco-v7-to-v8-migration/scripts/compat.ts +357 -0
  321. package/skills/deco-v7-to-v8-migration/scripts/content.ts +121 -0
  322. package/skills/deco-v7-to-v8-migration/scripts/experiments.ts +81 -0
  323. package/skills/deco-v7-to-v8-migration/scripts/framework.ts +61 -0
  324. package/skills/deco-v7-to-v8-migration/scripts/imageCopy.ts +276 -0
  325. package/skills/deco-v7-to-v8-migration/scripts/imageModule.ts +285 -0
  326. package/skills/deco-v7-to-v8-migration/scripts/imports.ts +942 -0
  327. package/skills/deco-v7-to-v8-migration/scripts/invoke.ts +624 -0
  328. package/skills/deco-v7-to-v8-migration/scripts/legacyNames.ts +45 -0
  329. package/skills/deco-v7-to-v8-migration/scripts/main.ts +59 -0
  330. package/skills/deco-v7-to-v8-migration/scripts/matchers.ts +41 -0
  331. package/skills/deco-v7-to-v8-migration/scripts/migrate.ts +176 -0
  332. package/skills/deco-v7-to-v8-migration/scripts/redirects.ts +455 -0
  333. package/skills/deco-v7-to-v8-migration/scripts/report.ts +50 -0
  334. package/skills/deco-v7-to-v8-migration/scripts/scaffold.ts +1119 -0
  335. package/skills/deco-v7-to-v8-migration/scripts/secrets.ts +35 -0
  336. package/skills/deco-v7-to-v8-migration/scripts/sectionLoaders.ts +216 -0
  337. package/skills/deco-v7-to-v8-migration/scripts/siteSettings.ts +261 -0
  338. package/skills/deco-v7-to-v8-migration/scripts/stubs.ts +388 -0
  339. package/skills/deco-v7-to-v8-migration/scripts/tsconfig.json +7 -0
  340. package/skills/deco-v7-to-v8-migration/scripts/vendor.ts +249 -0
  341. package/skills/deco-v7-to-v8-migration/scripts/vtexInvoke.ts +240 -0
  342. package/skills/deco-v7-to-v8-migration/scripts/walk.ts +30 -0
  343. package/skills/deco-variants/SKILL.md +57 -0
  344. package/skills/deco-variants/references/caching-variants.md +63 -0
  345. package/skills/deco-variants/references/custom-matchers.md +83 -0
  346. package/skills/deco-variants/references/draft-preview.md +59 -0
  347. package/skills/deco-variants/references/experiments-and-campaigns.md +104 -0
  348. package/skills/deco-variants/references/multivariate.md +85 -0
  349. package/skills/deco-variants/references/v7-matchers.md +71 -0
  350. package/src/index.ts +2 -0
  351. package/src/protocol/channel.ts +127 -0
  352. package/src/protocol/client.ts +2 -2
  353. package/src/protocol/conformance/blockMetaVectors.json +214 -0
  354. package/src/protocol/conformance/contentHashFixtures.ts +74 -0
  355. package/src/protocol/conformance/index.ts +9 -1
  356. package/src/protocol/conformance/vectors.ts +36 -0
  357. package/src/protocol/index.ts +32 -0
  358. package/src/protocol/keys.ts +1 -1
  359. package/src/v8/background.ts +42 -0
  360. package/src/v8/blockMeta.ts +100 -0
  361. package/src/v8/blockStore.ts +124 -0
  362. package/src/v8/boundedJson.ts +20 -2
  363. package/src/v8/boundedMap.ts +53 -0
  364. package/src/v8/builtins/data.ts +26 -2
  365. package/src/v8/builtins/index.ts +2 -1
  366. package/src/v8/builtins/legacy.ts +6 -3
  367. package/src/v8/canonical.ts +15 -0
  368. package/src/v8/cli/builtins.ts +1 -0
  369. package/src/v8/cli/check/index.ts +97 -7
  370. package/src/v8/cli/content.ts +49 -91
  371. package/src/v8/cli/index.ts +2 -2
  372. package/src/v8/cli/remote/commands.ts +834 -0
  373. package/src/v8/cli/remote/diff.ts +100 -0
  374. package/src/v8/cli/remote/env.ts +107 -0
  375. package/src/v8/cli/remote/http.ts +484 -0
  376. package/src/v8/cli/remote/local.ts +343 -0
  377. package/src/v8/cli/root.ts +40 -0
  378. package/src/v8/cli/run.ts +366 -135
  379. package/src/v8/cli/schema/builtinSchemas.ts +29 -10
  380. package/src/v8/cli/schema/generate.ts +1 -1
  381. package/src/v8/cli/skills.ts +91 -0
  382. package/src/v8/client.ts +162 -60
  383. package/src/v8/cms.ts +62 -53
  384. package/src/v8/content.ts +179 -229
  385. package/src/v8/contentIndex.ts +183 -0
  386. package/src/v8/draft.ts +15 -14
  387. package/src/v8/draftChanges.ts +151 -85
  388. package/src/v8/errors.ts +37 -0
  389. package/src/v8/hostedSource.ts +67 -0
  390. package/src/v8/identity.ts +1 -1
  391. package/src/v8/index.ts +1 -0
  392. package/src/v8/matchRoute.ts +7 -4
  393. package/src/v8/pin.ts +176 -0
  394. package/src/v8/preview/StudioBridge.ts +8 -10
  395. package/src/v8/preview/StudioChannel.ts +116 -0
  396. package/src/v8/preview/index.ts +3 -0
  397. package/src/v8/remoteLoader.ts +350 -286
  398. package/src/v8/resolver.ts +56 -33
  399. package/src/v8/revisionId.ts +14 -0
  400. package/src/v8/settings.ts +10 -9
  401. package/src/v8/telemetry.ts +3 -3
  402. package/src/v8/types.ts +52 -40
  403. package/src/v8/variants.ts +30 -26
  404. package/src/v8/vite/index.ts +3 -3
  405. package/dist/v8/cli/serve/server.d.ts +0 -29
  406. package/dist/v8/cli/serve/server.d.ts.map +0 -1
  407. package/dist/v8/cli/serve/server.js +0 -296
  408. package/dist/v8/cli/serve/server.js.map +0 -1
  409. package/src/v8/cli/serve/server.ts +0 -348
@@ -0,0 +1,834 @@
1
+ /**
2
+ * The remote content commands (spec: cli › The deco CLI): `login`, `pull`,
3
+ * `diff`, `push`, `publish`, `drafts`, `releases`, the remote sources of
4
+ * `check`, and `schema push`. The site is `DECO_SITE` (`resolveSite`). Writes go through the Studio API;
5
+ * only public reads go straight to the CDN. Nothing here retries and nothing
6
+ * publishes as a side effect: only `deco publish` goes live.
7
+ */
8
+ import fs from "node:fs";
9
+ import { blockVersion } from "../../../protocol/canonical.ts";
10
+ import { ErrorCode } from "../../../protocol/errors.ts";
11
+ import { serializeBlock } from "../../../protocol/keys.ts";
12
+ import {
13
+ MAX_BLOCK_BYTES,
14
+ MAX_OPS_PER_APPLY,
15
+ MAX_REQUEST_BYTES,
16
+ } from "../../../protocol/server/core.ts";
17
+ import { TOMBSTONE_VERSION } from "../../../protocol/storage.ts";
18
+ import { isRevisionId } from "../../revisionId.ts";
19
+ import { paint } from "../color.ts";
20
+ import type { SavedBlocks } from "../content.ts";
21
+ import type { Reporter } from "../log.ts";
22
+ import { CliError, type DecoPaths, EXIT, usageError } from "../root.ts";
23
+ import { formatBlockDiff } from "./diff.ts";
24
+ import {
25
+ type CommandRunner,
26
+ deliveryUrl,
27
+ type Env,
28
+ loginCommand,
29
+ resolveToken,
30
+ studioUrl,
31
+ } from "./env.ts";
32
+ import {
33
+ type ApplyParams,
34
+ applyRequestBody,
35
+ Delivery,
36
+ type DraftBody,
37
+ exitCodeForRpc,
38
+ type Fetch,
39
+ layer,
40
+ RpcError,
41
+ Studio,
42
+ toMap,
43
+ } from "./http.ts";
44
+ import {
45
+ draftVersion,
46
+ type LocalChange,
47
+ localChanges,
48
+ mirrorBlocks,
49
+ type PullEntry,
50
+ type PullState,
51
+ readLocal,
52
+ readPull,
53
+ resolveSite,
54
+ writeLocalBlock,
55
+ writePull,
56
+ } from "./local.ts";
57
+
58
+ /** What a command hands back: its exit code and its `--json` result. */
59
+ export interface Outcome {
60
+ exitCode: number;
61
+ result: Record<string, unknown>;
62
+ }
63
+
64
+ const ok = (result: Record<string, unknown>): Outcome => ({ exitCode: EXIT.ok, result });
65
+
66
+ export interface RemoteContext {
67
+ paths: DecoPaths;
68
+ reporter: Reporter;
69
+ env: Env;
70
+ fetch: Fetch;
71
+ runCommand?: CommandRunner;
72
+ }
73
+
74
+ function services(ctx: RemoteContext): { site: string; studio: Studio; delivery: Delivery } {
75
+ const { site } = resolveSite(ctx.paths, ctx.env);
76
+ let token: Promise<string> | undefined;
77
+ const studio = new Studio({
78
+ base: studioUrl(ctx.env),
79
+ site,
80
+ fetch: ctx.fetch,
81
+ token: () => {
82
+ token ??= resolveToken(ctx.env, ctx.runCommand);
83
+ return token;
84
+ },
85
+ });
86
+ return { site, studio, delivery: new Delivery(deliveryUrl(ctx.env), site, ctx.fetch) };
87
+ }
88
+
89
+ function plural(n: number, word: string): string {
90
+ return `${n} ${word}${n === 1 ? "" : "s"}`;
91
+ }
92
+
93
+ function at(seconds: number | undefined): string {
94
+ return typeof seconds === "number" ? new Date(seconds * 1000).toISOString() : "an unknown time";
95
+ }
96
+
97
+ // ---------------------------------------------------------------------------
98
+ // login
99
+ // ---------------------------------------------------------------------------
100
+
101
+ export function login(ctx: { env: Env; reporter: Reporter }): Outcome {
102
+ const command = loginCommand(ctx.env);
103
+ ctx.reporter.info(
104
+ `The deco CLI uses Studio's login. Log in once with:\n\n ${command}\n\n` +
105
+ "In CI or a sandbox, set DECO_TOKEN to a Studio API key instead.",
106
+ );
107
+ return ok({ command });
108
+ }
109
+
110
+ // ---------------------------------------------------------------------------
111
+ // Reading content: a draft or production, as blocks
112
+ // ---------------------------------------------------------------------------
113
+
114
+ interface Source {
115
+ draft: string | null;
116
+ revision: string | null;
117
+ blocks: Record<string, unknown>;
118
+ /** The draft object, when the source is a draft. */
119
+ body?: DraftBody;
120
+ }
121
+
122
+ async function readSource(
123
+ studio: Studio,
124
+ delivery: Delivery,
125
+ draft: string | null,
126
+ ): Promise<Source> {
127
+ if (draft === null) {
128
+ const production = await delivery.production();
129
+ return { draft, ...production };
130
+ }
131
+ // Resolve first: a missing login or draft is reported before the CDN is read.
132
+ const resolved = await studio.resolveDraft(draft);
133
+ const [production, body] = await Promise.all([
134
+ delivery.production(),
135
+ delivery.draft(resolved.slug, resolved.etag),
136
+ ]);
137
+ return { draft, revision: production.revision, blocks: layer(production.blocks, body), body };
138
+ }
139
+
140
+ /**
141
+ * Latest ⊕ a draft for a few names only: the live index and the draft's
142
+ * folder, then the production bodies of `names` (plus every name the draft
143
+ * touches when `names` is omitted). Push and publish never read the rest.
144
+ */
145
+ async function readOverlay(
146
+ delivery: Delivery,
147
+ resolved: { slug: string; etag: string | null },
148
+ names?: readonly string[],
149
+ ): Promise<{ revision: string | null; before: Record<string, unknown>; body: DraftBody }> {
150
+ const [index, body] = await Promise.all([
151
+ delivery.latestIndex(),
152
+ delivery.draft(resolved.slug, resolved.etag),
153
+ ]);
154
+ const wanted = names ?? [...Object.keys(body.set), ...body.delete];
155
+ const before = index === null ? Object.create(null) : await delivery.bodies(index, wanted);
156
+ return { revision: index?.revision ?? null, before, body };
157
+ }
158
+
159
+ function describeSource(source: { draft: string | null; revision: string | null }): string {
160
+ const revision = source.revision ? `revision ${source.revision}` : "no release yet";
161
+ return source.draft === null
162
+ ? `production (${revision})`
163
+ : `draft ${source.draft} (over ${revision})`;
164
+ }
165
+
166
+ /**
167
+ * What `deco diff` compared against. A draft is read as it is now (pull.json
168
+ * keeps only hashes), which can differ from what was pulled: the label says so.
169
+ */
170
+ function describeDiffSource(source: { draft: string | null; revision: string | null }): string {
171
+ if (source.draft === null) return describeSource(source);
172
+ const revision = source.revision ? `revision ${source.revision}` : "no release yet";
173
+ return `draft ${source.draft} as it is now (over ${revision})`;
174
+ }
175
+
176
+ // ---------------------------------------------------------------------------
177
+ // pull
178
+ // ---------------------------------------------------------------------------
179
+
180
+ export async function pull(
181
+ ctx: RemoteContext,
182
+ options: { draft: string | null; force: boolean },
183
+ ): Promise<Outcome> {
184
+ const { studio, delivery } = services(ctx);
185
+ if (!options.force) {
186
+ const previous = readPull(ctx.paths);
187
+ const changes = await localChanges(readLocal(ctx.paths), previous);
188
+ if (changes.length > 0) {
189
+ throw new CliError(
190
+ `.deco/blocks has ${plural(changes.length, "change")} not pushed (${changes
191
+ .slice(0, 5)
192
+ .map((c) => c.name)
193
+ .join(
194
+ ", ",
195
+ )}${changes.length > 5 ? ", …" : ""}); push them, or pull with --force to overwrite them`,
196
+ { code: "unpushed_changes", details: { changes: changes.map(changeRow) } },
197
+ );
198
+ }
199
+ }
200
+ const source = await readSource(studio, delivery, options.draft);
201
+ const blocks: Record<string, PullEntry> = Object.create(null);
202
+ for (const name of Object.keys(source.blocks)) {
203
+ blocks[name] = {
204
+ hash: await blockVersion(source.blocks[name]),
205
+ ifMatch: source.body ? await draftVersion(source.body, name) : null,
206
+ };
207
+ }
208
+ for (const name of source.body?.delete ?? []) {
209
+ if (!Object.hasOwn(blocks, name)) blocks[name] = { hash: null, ifMatch: TOMBSTONE_VERSION };
210
+ }
211
+ mirrorBlocks(ctx.paths, source.blocks);
212
+ writePull(ctx.paths, {
213
+ format: 1,
214
+ draft: source.draft,
215
+ revision: source.revision,
216
+ pulledAt: new Date().toISOString(),
217
+ blocks,
218
+ });
219
+ const count = Object.keys(source.blocks).length;
220
+ ctx.reporter.info(`pulled ${plural(count, "block")} from ${describeSource(source)}`);
221
+ return ok({ draft: source.draft, revision: source.revision, blocks: count });
222
+ }
223
+
224
+ function changeRow(change: LocalChange) {
225
+ return { name: change.name, status: change.status };
226
+ }
227
+
228
+ const STATUS_MARK: Record<LocalChange["status"], string> = { changed: "M", new: "A", deleted: "D" };
229
+
230
+ function printChange(
231
+ reporter: Reporter,
232
+ name: string,
233
+ status: LocalChange["status"],
234
+ from: unknown,
235
+ to: unknown,
236
+ ) {
237
+ const c = paint(reporter.color);
238
+ const body = formatBlockDiff(from, to, { color: reporter.color });
239
+ reporter.info(`${c.bold(`${STATUS_MARK[status]} ${name}`)}${body ? `\n${body}` : ""}`);
240
+ }
241
+
242
+ // ---------------------------------------------------------------------------
243
+ // diff
244
+ // ---------------------------------------------------------------------------
245
+
246
+ /** What differs between two sets of blocks, in name order (`to` is the newer side). */
247
+ async function compare(
248
+ from: Record<string, unknown>,
249
+ to: Record<string, unknown>,
250
+ ): Promise<{ name: string; status: LocalChange["status"] }[]> {
251
+ const rows: { name: string; status: LocalChange["status"] }[] = [];
252
+ for (const name of [...new Set([...Object.keys(from), ...Object.keys(to)])].sort()) {
253
+ const inFrom = Object.hasOwn(from, name);
254
+ const inTo = Object.hasOwn(to, name);
255
+ if (inFrom && inTo) {
256
+ if ((await blockVersion(from[name])) !== (await blockVersion(to[name])))
257
+ rows.push({ name, status: "changed" });
258
+ } else rows.push({ name, status: inTo ? "new" : "deleted" });
259
+ }
260
+ return rows;
261
+ }
262
+
263
+ export async function diff(
264
+ ctx: RemoteContext,
265
+ options: { draft: string | null },
266
+ ): Promise<Outcome> {
267
+ const saved = readLocal(ctx.paths);
268
+ const local = saved.blocks as Record<string, unknown>;
269
+ const { studio, delivery } = services(ctx);
270
+ let rows: { name: string; status: LocalChange["status"] }[];
271
+ let source: { draft: string | null; revision: string | null; blocks: Record<string, unknown> };
272
+ if (options.draft !== null) {
273
+ source = await readSource(studio, delivery, options.draft);
274
+ rows = await compare(source.blocks, local);
275
+ } else {
276
+ const state = readPull(ctx.paths);
277
+ if (!state) throw new CliError("nothing pulled yet: run deco pull", { code: "not_pulled" });
278
+ rows = (await localChanges(saved, state)).map(changeRow);
279
+ if (state.draft !== null) {
280
+ source = await readSource(studio, delivery, state.draft);
281
+ } else {
282
+ const blocks =
283
+ state.revision === null ? Object.create(null) : await delivery.revision(state.revision);
284
+ if (blocks === null) {
285
+ throw new CliError(`the CDN has no revision ${state.revision}`, {
286
+ code: "revision_not_found",
287
+ });
288
+ }
289
+ source = { draft: null, revision: state.revision, blocks };
290
+ }
291
+ }
292
+ for (const row of rows) {
293
+ printChange(ctx.reporter, row.name, row.status, source.blocks[row.name], local[row.name]);
294
+ }
295
+ ctx.reporter.info(
296
+ rows.length === 0
297
+ ? `no changes against ${describeDiffSource(source)}`
298
+ : `${plural(rows.length, "change")} against ${describeDiffSource(source)}`,
299
+ );
300
+ return ok({ draft: source.draft, revision: source.revision, changes: rows });
301
+ }
302
+
303
+ // ---------------------------------------------------------------------------
304
+ // push
305
+ // ---------------------------------------------------------------------------
306
+
307
+ interface Item {
308
+ name: string;
309
+ /** The local value, or undefined for a deletion. */
310
+ block?: Record<string, unknown>;
311
+ hash: string | null;
312
+ ifMatch: string | null;
313
+ }
314
+
315
+ const ENVELOPE_BYTES = Buffer.byteLength(applyRequestBody({ set: {}, delete: [], ifMatch: {} }));
316
+
317
+ function itemBytes(item: Item): { set: number; delete: number; ifMatch: number } {
318
+ const key = JSON.stringify(item.name);
319
+ return {
320
+ set: item.block === undefined ? 0 : Buffer.byteLength(`${key}:${JSON.stringify(item.block)}`),
321
+ delete: item.block === undefined ? Buffer.byteLength(key) : 0,
322
+ ifMatch: Buffer.byteLength(`${key}:${JSON.stringify(item.ifMatch)}`),
323
+ };
324
+ }
325
+
326
+ /**
327
+ * Packs items, in order, into batches whose serialized `blocks.apply`
328
+ * request stays at most `limit` bytes and that hold at most `maxNames`
329
+ * names (set plus delete): the two caps the content handler puts on one
330
+ * call. The byte count is exact: the size of a JSON object is its entries
331
+ * plus one comma between each two. A block whose stored form is over the
332
+ * handler's per-entry cap (`MAX_BLOCK_BYTES`) can never be saved, so it
333
+ * fails here, before anything is sent.
334
+ */
335
+ export function packBatches(
336
+ items: Item[],
337
+ limit = MAX_REQUEST_BYTES,
338
+ maxNames = MAX_OPS_PER_APPLY,
339
+ ): Item[][] {
340
+ const batches: Item[][] = [];
341
+ let current: Item[] = [];
342
+ let size = ENVELOPE_BYTES;
343
+ let counts = { set: 0, delete: 0, ifMatch: 0 };
344
+ const added = (bytes: ReturnType<typeof itemBytes>, c: typeof counts) =>
345
+ (bytes.set ? bytes.set + (c.set > 0 ? 1 : 0) : 0) +
346
+ (bytes.delete ? bytes.delete + (c.delete > 0 ? 1 : 0) : 0) +
347
+ bytes.ifMatch +
348
+ (c.ifMatch > 0 ? 1 : 0);
349
+ for (const item of items) {
350
+ if (
351
+ item.block !== undefined &&
352
+ Buffer.byteLength(serializeBlock(item.block)) > MAX_BLOCK_BYTES
353
+ ) {
354
+ throw new CliError(
355
+ `block "${item.name}" is larger than a save can carry (${MAX_BLOCK_BYTES} bytes a block)`,
356
+ { code: "too_large", details: { name: item.name } },
357
+ );
358
+ }
359
+ const bytes = itemBytes(item);
360
+ if (ENVELOPE_BYTES + added(bytes, { set: 0, delete: 0, ifMatch: 0 }) > limit) {
361
+ throw new CliError(
362
+ `block "${item.name}" alone is larger than a save can carry (${limit} bytes)`,
363
+ { code: "too_large", details: { name: item.name } },
364
+ );
365
+ }
366
+ if (current.length >= maxNames || (current.length > 0 && size + added(bytes, counts) > limit)) {
367
+ batches.push(current);
368
+ current = [];
369
+ size = ENVELOPE_BYTES;
370
+ counts = { set: 0, delete: 0, ifMatch: 0 };
371
+ }
372
+ size += added(bytes, counts);
373
+ current.push(item);
374
+ counts = {
375
+ set: counts.set + (bytes.set ? 1 : 0),
376
+ delete: counts.delete + (bytes.delete ? 1 : 0),
377
+ ifMatch: counts.ifMatch + 1,
378
+ };
379
+ }
380
+ if (current.length > 0) batches.push(current);
381
+ return batches;
382
+ }
383
+
384
+ function applyParams(items: Item[]): ApplyParams {
385
+ const set: Record<string, unknown> = {};
386
+ const deleted: string[] = [];
387
+ const ifMatch: Record<string, string | null> = {};
388
+ for (const item of items) {
389
+ // defineProperty, so a block named "__proto__" is sent as a name.
390
+ if (item.block !== undefined)
391
+ Object.defineProperty(set, item.name, {
392
+ value: item.block,
393
+ enumerable: true,
394
+ writable: true,
395
+ configurable: true,
396
+ });
397
+ else deleted.push(item.name);
398
+ Object.defineProperty(ifMatch, item.name, {
399
+ value: item.ifMatch,
400
+ enumerable: true,
401
+ writable: true,
402
+ configurable: true,
403
+ });
404
+ }
405
+ return { set, delete: deleted, ifMatch };
406
+ }
407
+
408
+ interface ConflictEntry {
409
+ expected: string | null;
410
+ actual: string | null;
411
+ theirs: unknown;
412
+ editor: { userId: string; at: number } | null;
413
+ }
414
+
415
+ function readConflicts(data: unknown): Record<string, ConflictEntry> {
416
+ const d = (data ?? {}) as {
417
+ entries?: Record<string, { expected: string | null; actual: string | null }>;
418
+ draft?: Record<
419
+ string,
420
+ { block: unknown; version: string; editor: ConflictEntry["editor"] } | null
421
+ >;
422
+ };
423
+ const out: Record<string, ConflictEntry> = Object.create(null);
424
+ for (const [name, entry] of Object.entries(d.entries ?? {})) {
425
+ const held = d.draft && Object.hasOwn(d.draft, name) ? d.draft[name] : null;
426
+ out[name] = {
427
+ expected: entry?.expected ?? null,
428
+ actual: entry?.actual ?? null,
429
+ theirs: held ? held.block : null,
430
+ editor: held?.editor ?? null,
431
+ };
432
+ }
433
+ return out;
434
+ }
435
+
436
+ /** The versions that landed before a save stopped (`-32008` with `data.landed`), or `null`. */
437
+ function landedOf(error: RpcError): Record<string, string | null> | null {
438
+ if (error.code !== ErrorCode.Unavailable) return null;
439
+ const landed = (error.data as { landed?: unknown } | null)?.landed;
440
+ if (typeof landed !== "object" || landed === null || Array.isArray(landed)) return null;
441
+ const out: Record<string, string | null> = Object.create(null);
442
+ for (const [name, version] of Object.entries(landed)) {
443
+ if (typeof version === "string") out[name] = version;
444
+ }
445
+ return out;
446
+ }
447
+
448
+ export async function push(
449
+ ctx: RemoteContext,
450
+ options: { draft: string; side: "ours" | "theirs" | null; prune?: boolean },
451
+ ): Promise<Outcome> {
452
+ const { studio, delivery } = services(ctx);
453
+ let saved = readLocal(ctx.paths);
454
+ const pulled = readPull(ctx.paths);
455
+ // Pushing to a draft other than the one pulled: every precondition is
456
+ // relative to the pulled draft, so none of them hold for this one.
457
+ let state: PullState;
458
+ if (pulled && pulled.draft === options.draft) state = pulled;
459
+ else {
460
+ const blocks: Record<string, PullEntry> = Object.create(null);
461
+ for (const [name, entry] of Object.entries(pulled?.blocks ?? {}))
462
+ blocks[name] = { hash: entry.hash, ifMatch: null };
463
+ state = {
464
+ format: 1,
465
+ draft: options.draft,
466
+ revision: pulled?.revision ?? null,
467
+ pulledAt: pulled?.pulledAt ?? new Date().toISOString(),
468
+ blocks,
469
+ };
470
+ if (pulled) writePull(ctx.paths, state);
471
+ }
472
+ // (a) The names changed locally since the pull; deletes only with --prune.
473
+ const local = await localChanges(saved, state);
474
+ const changes = local.filter((change) => change.block !== undefined || options.prune);
475
+ const unsentDeletes = options.prune
476
+ ? []
477
+ : local.filter((change) => change.block === undefined).map((change) => change.name);
478
+ const ensured = await studio.ensureDraft(options.draft);
479
+ if (ensured.created) ctx.reporter.info(`created draft ${options.draft}`);
480
+ // (b) Of those, the ones that differ from latest ⊕ the draft now, with the
481
+ // draft's versions read now as their preconditions.
482
+ const now = await readOverlay(
483
+ delivery,
484
+ ensured,
485
+ changes.map((change) => change.name),
486
+ );
487
+ const current = layer(now.before, now.body);
488
+ const items: Item[] = [];
489
+ for (const change of changes) {
490
+ const there = Object.hasOwn(current, change.name) ? current[change.name] : undefined;
491
+ const same =
492
+ change.block === undefined
493
+ ? there === undefined
494
+ : there !== undefined && (await blockVersion(there)) === change.hash;
495
+ if (same) {
496
+ // Already what the draft shows: in sync, nothing to send.
497
+ state.blocks[change.name] = {
498
+ hash: change.block === undefined ? null : (change.hash ?? null),
499
+ ifMatch: await draftVersion(now.body, change.name),
500
+ };
501
+ continue;
502
+ }
503
+ items.push({
504
+ name: change.name,
505
+ block: change.block,
506
+ hash: change.hash ?? null,
507
+ ifMatch: await draftVersion(now.body, change.name),
508
+ });
509
+ }
510
+ // One change, one save: a push that doesn't fit one blocks.apply fails.
511
+ if (items.length < changes.length) writePull(ctx.paths, state);
512
+ const batches = packBatches(items);
513
+ if (batches.length > 1) {
514
+ throw new CliError(
515
+ `the ${plural(items.length, "changed block")} don't fit one save (${MAX_REQUEST_BYTES} bytes, ${MAX_OPS_PER_APPLY} names); push fewer at a time`,
516
+ { code: "too_large", details: { names: items.map((item) => item.name) } },
517
+ );
518
+ }
519
+
520
+ // Production's bodies of `names` only, from the latest index now: push reads the names it pushes.
521
+ const productionBodies = async (names: readonly string[]): Promise<Record<string, unknown>> => {
522
+ const index = await delivery.latestIndex();
523
+ return index === null ? Object.create(null) : delivery.bodies(index, names);
524
+ };
525
+ const record = (batch: Item[], versions: Record<string, string | null>) => {
526
+ for (const item of batch) {
527
+ state.blocks[item.name] = {
528
+ hash: item.block === undefined ? null : item.hash,
529
+ ifMatch: Object.hasOwn(versions, item.name) ? (versions[item.name] ?? null) : null,
530
+ };
531
+ }
532
+ writePull(ctx.paths, state);
533
+ };
534
+
535
+ let pushed = 0;
536
+ let taken = 0;
537
+ let resent = false;
538
+ for (const [index, original] of batches.entries()) {
539
+ let batch = original;
540
+ // --ours or --theirs is applied to one conflict; a re-send after files landed is no conflict retry.
541
+ let conflictRetried = false;
542
+ for (;;) {
543
+ try {
544
+ const result = await studio.apply(options.draft, applyRequestBody(applyParams(batch)));
545
+ record(batch, result.versions ?? {});
546
+ pushed += batch.length;
547
+ break;
548
+ } catch (error) {
549
+ if (!(error instanceof RpcError)) throw error;
550
+ const landed = landedOf(error);
551
+ if (landed !== null) {
552
+ // Some files landed before the save stopped: keep them, send the rest once more.
553
+ const done = batch.filter((item) => Object.hasOwn(landed, item.name));
554
+ record(done, landed);
555
+ pushed += done.length;
556
+ batch = batch.filter((item) => !Object.hasOwn(landed, item.name));
557
+ if (batch.length === 0) break;
558
+ if (!resent) {
559
+ resent = true;
560
+ continue;
561
+ }
562
+ }
563
+ if (error.code !== ErrorCode.Conflict) {
564
+ const busy = error.code === ErrorCode.Unavailable;
565
+ throw new CliError(
566
+ busy
567
+ ? `Studio is busy (${error.message}); ${pushed > 0 ? `${plural(pushed, "block")} saved, the rest not` : "nothing was saved"}, run deco push again`
568
+ : `Studio refused the save: ${error.message}`,
569
+ {
570
+ exitCode: exitCodeForRpc(error.code),
571
+ code: busy ? "unavailable" : `rpc_${-error.code}`,
572
+ details: { pushed, batch: index + 1, batches: batches.length, rpc: error.data },
573
+ },
574
+ );
575
+ }
576
+ const conflicts = readConflicts(error.data);
577
+ if (options.side === null || conflictRetried) {
578
+ reportConflicts(ctx.reporter, conflicts, saved);
579
+ throw new CliError(
580
+ `${plural(Object.keys(conflicts).length, "block")} changed in draft ${options.draft} while you pushed; nothing of this save was written. ` +
581
+ "Pull again, or push with --ours (keep yours) or --theirs (take theirs)",
582
+ {
583
+ exitCode: EXIT.conflict,
584
+ code: "conflict",
585
+ details: { pushed, batch: index + 1, batches: batches.length, conflicts },
586
+ },
587
+ );
588
+ }
589
+ conflictRetried = true;
590
+ if (options.side === "ours") {
591
+ batch = batch.map((item) =>
592
+ Object.hasOwn(conflicts, item.name)
593
+ ? { ...item, ifMatch: conflicts[item.name]!.actual }
594
+ : item,
595
+ );
596
+ continue;
597
+ }
598
+ // --theirs: take each conflicting block into .deco/blocks as the draft holds it.
599
+ const fromProduction = Object.entries(conflicts)
600
+ .filter(([, c]) => c.theirs === null && c.actual === null)
601
+ .map(([name]) => name);
602
+ const prod = fromProduction.length > 0 ? await productionBodies(fromProduction) : null;
603
+ for (const [name, conflict] of Object.entries(conflicts)) {
604
+ let value: unknown;
605
+ let hash: string | null = null;
606
+ if (conflict.actual === TOMBSTONE_VERSION) value = undefined;
607
+ else if (conflict.theirs !== null) value = conflict.theirs;
608
+ else value = prod && Object.hasOwn(prod, name) ? prod[name] : undefined;
609
+ if (value !== undefined) hash = await blockVersion(value);
610
+ writeLocalBlock(ctx.paths, saved, name, value);
611
+ state.blocks[name] = { hash, ifMatch: conflict.actual };
612
+ taken++;
613
+ }
614
+ writePull(ctx.paths, state);
615
+ saved = readLocal(ctx.paths);
616
+ batch = batch.filter((item) => !Object.hasOwn(conflicts, item.name));
617
+ if (batch.length === 0) break;
618
+ }
619
+ }
620
+ }
621
+ const resolved = await studio.resolveDraft(options.draft);
622
+ const link = resolved.previewUrl ?? resolved.pointer;
623
+ ctx.reporter.info(
624
+ `pushed ${plural(pushed, "block")} to draft ${options.draft}` +
625
+ (taken > 0 ? `; took theirs for ${plural(taken, "block")}` : ""),
626
+ );
627
+ if (unsentDeletes.length > 0) {
628
+ ctx.reporter.info(
629
+ `not sent: ${plural(unsentDeletes.length, "deleted block")} (${unsentDeletes.join(", ")}); push with --prune to send deletes`,
630
+ );
631
+ }
632
+ if (link) ctx.reporter.info(`preview: ${link}`);
633
+ return ok({
634
+ draft: options.draft,
635
+ pushed,
636
+ batches: batches.length,
637
+ tookTheirs: taken,
638
+ previewUrl: resolved.previewUrl,
639
+ pointer: resolved.pointer,
640
+ });
641
+ }
642
+
643
+ function reportConflicts(
644
+ reporter: Reporter,
645
+ conflicts: Record<string, ConflictEntry>,
646
+ saved: SavedBlocks,
647
+ ) {
648
+ const c = paint(reporter.color);
649
+ for (const [name, conflict] of Object.entries(conflicts)) {
650
+ const who = conflict.editor
651
+ ? `changed by ${conflict.editor.userId} at ${at(conflict.editor.at)}`
652
+ : "changed by someone else";
653
+ const theirs =
654
+ conflict.actual === TOMBSTONE_VERSION
655
+ ? "theirs deletes it"
656
+ : conflict.theirs === null
657
+ ? "the draft no longer holds it (production's value applies)"
658
+ : "theirs (-) against yours (+)";
659
+ const yours = Object.hasOwn(saved.blocks, name) ? saved.blocks[name] : undefined;
660
+ const body =
661
+ conflict.theirs === null
662
+ ? ""
663
+ : formatBlockDiff(conflict.theirs, yours, { color: reporter.color });
664
+ reporter.error(`${c.bold(`conflict: ${name}`)}: ${who}; ${theirs}${body ? `\n${body}` : ""}`);
665
+ }
666
+ }
667
+
668
+ // ---------------------------------------------------------------------------
669
+ // publish
670
+ // ---------------------------------------------------------------------------
671
+
672
+ export async function publish(
673
+ ctx: RemoteContext,
674
+ options: { draft: string; message: string | undefined },
675
+ ): Promise<Outcome> {
676
+ const { studio, delivery } = services(ctx);
677
+ const resolved = await studio.resolveDraft(options.draft);
678
+ // The draft's files against those names' production bodies: nothing else is read.
679
+ const { revision, before, body } = await readOverlay(delivery, resolved);
680
+ const after = layer(before, body);
681
+ const rows = await compare(before, after);
682
+ for (const row of rows) {
683
+ printChange(ctx.reporter, row.name, row.status, before[row.name], after[row.name]);
684
+ }
685
+ ctx.reporter.info(
686
+ `draft ${options.draft} changes ${plural(rows.length, "block")} of ${describeSource({ draft: null, revision })}`,
687
+ );
688
+ const result = await studio.publish(options.draft, options.message);
689
+ if (result.result === "published") {
690
+ ctx.reporter.info(`published revision ${result.revision}; it is live`);
691
+ } else {
692
+ ctx.reporter.info(
693
+ `nothing to publish: production already has every block of draft ${options.draft}`,
694
+ );
695
+ }
696
+ if (!result.draftEmptied && result.result === "published") {
697
+ ctx.reporter.warn(
698
+ `draft ${options.draft} was saved during the publish, so it wasn't emptied: it still holds its blocks`,
699
+ );
700
+ }
701
+ return ok({ draft: options.draft, ...result, changes: rows });
702
+ }
703
+
704
+ // ---------------------------------------------------------------------------
705
+ // drafts, releases
706
+ // ---------------------------------------------------------------------------
707
+
708
+ export async function drafts(
709
+ ctx: RemoteContext,
710
+ options: { action: string | undefined; name: string | null },
711
+ ): Promise<Outcome> {
712
+ const { studio } = services(ctx);
713
+ const need = () => {
714
+ if (options.name === null) throw usageError(`deco drafts ${options.action} needs a draft name`);
715
+ return options.name;
716
+ };
717
+ switch (options.action) {
718
+ case "list": {
719
+ if (options.name !== null) throw usageError("deco drafts list takes no name");
720
+ const rows = await studio.listDrafts();
721
+ if (rows.length === 0) ctx.reporter.info("no drafts");
722
+ for (const row of rows) {
723
+ ctx.reporter.info(`${row.name} ${row.createdBy} via ${row.via}, ${row.createdAt}`);
724
+ }
725
+ return ok({ drafts: rows });
726
+ }
727
+ case "new": {
728
+ const draft = await studio.ensureDraft(need());
729
+ ctx.reporter.info(
730
+ draft.created ? `created draft ${draft.name}` : `draft ${draft.name} already exists`,
731
+ );
732
+ if (draft.previewUrl) ctx.reporter.info(`preview: ${draft.previewUrl}`);
733
+ return ok({ draft });
734
+ }
735
+ case "discard": {
736
+ const name = need();
737
+ await studio.discardDraft(name);
738
+ ctx.reporter.info(`discarded draft ${name}`);
739
+ return ok({ draft: name, discarded: true });
740
+ }
741
+ case "url": {
742
+ const draft = await studio.resolveDraft(need());
743
+ if (draft.previewUrl) ctx.reporter.info(draft.previewUrl);
744
+ else if (draft.pointer)
745
+ ctx.reporter.info(
746
+ `${draft.pointer}\n(the site has no preview address in Studio: open the site with ?__draft=<this pointer>)`,
747
+ );
748
+ else ctx.reporter.info(`draft ${draft.name} has no object yet: push to it first`);
749
+ return ok({ draft: draft.name, previewUrl: draft.previewUrl, pointer: draft.pointer });
750
+ }
751
+ default:
752
+ throw usageError(
753
+ `deco drafts needs list, new <name>, discard <name> or url <name>${options.action ? `, not "${options.action}"` : ""}`,
754
+ );
755
+ }
756
+ }
757
+
758
+ export async function releases(
759
+ ctx: RemoteContext,
760
+ options: { cursor: string | undefined },
761
+ ): Promise<Outcome> {
762
+ const { studio } = services(ctx);
763
+ const page = await studio.releases(options.cursor);
764
+ const c = paint(ctx.reporter.color);
765
+ if (page.revisions.length === 0) ctx.reporter.info("no releases yet");
766
+ for (const row of page.revisions) {
767
+ // Its meta.json couldn't be read: the id is all there is.
768
+ if (row.status === "unreadable") {
769
+ ctx.reporter.info(`${row.id} ${c.yellow("unreadable")} (its meta.json couldn't be read)`);
770
+ continue;
771
+ }
772
+ const meta = row.meta ?? {};
773
+ const what =
774
+ typeof meta.reapplyOf === "string"
775
+ ? `rollback to ${meta.reapplyOf}`
776
+ : typeof meta.draft === "string"
777
+ ? `draft ${meta.draft}`
778
+ : "";
779
+ ctx.reporter.info(
780
+ `${row.id} ${row.status === "current" ? `${c.green("current")} ` : ""}${meta.publishedAt ?? ""} ${meta.author ?? ""} via ${meta.via ?? "?"}` +
781
+ `${what ? ` ${what}` : ""}${meta.message ? ` ${JSON.stringify(meta.message)}` : ""}`,
782
+ );
783
+ }
784
+ if (page.nextCursor) ctx.reporter.info(`more: deco releases --cursor=${page.nextCursor}`);
785
+ return ok({ ...page });
786
+ }
787
+
788
+ // ---------------------------------------------------------------------------
789
+ // check's remote sources, schema push
790
+ // ---------------------------------------------------------------------------
791
+
792
+ /** The blocks `deco check --draft` or `--revision` validates. */
793
+ export async function remoteBlocks(
794
+ ctx: RemoteContext,
795
+ options: { draft: string | null; revision: string | null },
796
+ ): Promise<{ label: string; blocks: Record<string, unknown> }> {
797
+ const { studio, delivery } = services(ctx);
798
+ if (options.revision !== null) {
799
+ if (!isRevisionId(options.revision)) {
800
+ throw usageError(`"${options.revision}" isn't a revision id (deco releases lists them)`);
801
+ }
802
+ const blocks = await delivery.revision(options.revision);
803
+ if (blocks === null) {
804
+ throw new CliError(`no revision ${options.revision} on the CDN`, {
805
+ code: "revision_not_found",
806
+ });
807
+ }
808
+ return { label: `revision ${options.revision}`, blocks };
809
+ }
810
+ const source = await readSource(studio, delivery, options.draft);
811
+ return { label: describeSource(source), blocks: toMap(source.blocks) };
812
+ }
813
+
814
+ export async function schemaPush(ctx: RemoteContext): Promise<Outcome> {
815
+ const { studio } = services(ctx);
816
+ let text: string;
817
+ try {
818
+ text = fs.readFileSync(ctx.paths.schema, "utf8");
819
+ } catch {
820
+ throw new CliError("no .deco/schema.gen.json; run deco schema first", { code: "no_schema" });
821
+ }
822
+ try {
823
+ JSON.parse(text);
824
+ } catch (error) {
825
+ throw new CliError(`.deco/schema.gen.json: invalid JSON: ${(error as Error).message}`, {
826
+ code: "invalid_schema",
827
+ });
828
+ }
829
+ const result = await studio.pushSchema(text);
830
+ ctx.reporter.info(
831
+ `pushed .deco/schema.gen.json (${result.blocksMajor === 8 ? "a v8 schema" : "not a v8 schema"})`,
832
+ );
833
+ return ok({ etag: result.etag, blocksMajor: result.blocksMajor });
834
+ }