@homepages/concrete 12.0.0 → 13.0.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 (311) hide show
  1. package/CHANGELOG.md +184 -0
  2. package/README.md +8 -10
  3. package/codemods/fixtures-are-data.mjs +7 -0
  4. package/codemods/fixtures-list-properties.mjs +6 -0
  5. package/codemods/manifest.json +11 -0
  6. package/codemods/steps/fixtures-list-properties.mjs +16 -0
  7. package/codemods/steps/fixtures-to-data.mjs +41 -0
  8. package/dist/artifact/playground/client/assets/archivo-latin-ext-wght-normal-C4zznr8T.woff2 +0 -0
  9. package/dist/artifact/playground/client/assets/archivo-latin-wght-normal-E0tuGl4L.woff2 +0 -0
  10. package/dist/artifact/playground/client/assets/archivo-vietnamese-wght-normal-XAtsl5Q_.woff2 +0 -0
  11. package/dist/artifact/playground/client/assets/index-Bmvcss4B.js +1338 -0
  12. package/dist/artifact/playground/client/assets/index-C01UCAWD.css +1 -0
  13. package/dist/artifact/playground/client/index.html +2 -2
  14. package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
  15. package/dist/artifact/playground/host.mjs +110 -59
  16. package/dist/base.css +2 -2
  17. package/dist/cli/build/icons.js +1 -1
  18. package/dist/cli/build/index.js +11 -10
  19. package/dist/cli/build/manifest.js +44 -0
  20. package/dist/cli/build/record.js +123 -0
  21. package/dist/cli/build/sections.js +28 -18
  22. package/dist/cli/build/source.js +1 -1
  23. package/dist/cli/build/static.js +8 -5
  24. package/dist/cli/build/template-build.js +614 -0
  25. package/dist/cli/check/analyze.js +18 -1
  26. package/dist/cli/check/load-workspace.js +2 -2
  27. package/dist/cli/check/portable-names.js +1 -1
  28. package/dist/cli/check/render/corpus.js +119 -0
  29. package/dist/cli/check/render/invariants.js +1 -0
  30. package/dist/cli/check/stages/bundles.js +15 -18
  31. package/dist/cli/check/stages/composition.js +8 -25
  32. package/dist/cli/check/stages/fixtures.js +14 -10
  33. package/dist/cli/check/stages/logic.js +7 -23
  34. package/dist/cli/check/stages/render.js +12 -14
  35. package/dist/cli/check/stages/sections.js +22 -66
  36. package/dist/cli/check/stages/stylesheet.js +4 -26
  37. package/dist/cli/check/stages/workspace.js +27 -22
  38. package/dist/cli/commands.js +51 -23
  39. package/dist/cli/currency/preamble.js +1 -1
  40. package/dist/cli/currency/resolve-latest.js +3 -2
  41. package/dist/cli/dev/capture-browser.js +26 -0
  42. package/dist/cli/dev/fixture-schema.js +441 -0
  43. package/dist/cli/dev/folder-files.js +244 -0
  44. package/dist/cli/dev/index.js +7 -3
  45. package/dist/cli/dev/playground-server.js +34 -0
  46. package/dist/cli/dev/section-ladders.js +76 -0
  47. package/dist/cli/dev/server.js +120 -304
  48. package/dist/cli/install/background.js +16 -0
  49. package/dist/cli/install/index.js +45 -0
  50. package/dist/cli/install/tools.js +27 -0
  51. package/dist/cli/media/derive.js +19 -13
  52. package/dist/cli/media/deriver-plugin.js +14 -3
  53. package/dist/cli/property/fixture-list.js +46 -0
  54. package/dist/cli/property/index.js +12 -9
  55. package/dist/cli/property/library.js +12 -10
  56. package/dist/cli/publish/artifact.js +90 -0
  57. package/dist/cli/publish/captures.js +61 -0
  58. package/dist/cli/publish/fixtures.js +17 -0
  59. package/dist/cli/publish/index.js +10 -18
  60. package/dist/cli/{build → publish}/playground.js +3 -3
  61. package/dist/cli/publish/report.js +15 -33
  62. package/dist/cli/upgrade/codemod.d.ts +14 -0
  63. package/dist/cli/upgrade/fixture-conversion.d.ts +11 -0
  64. package/dist/cli/upgrade/fixture-conversion.js +54 -0
  65. package/dist/cli/upgrade/fixture-modules.js +125 -0
  66. package/dist/cli/upgrade/fixtures-to-data.d.ts +40 -0
  67. package/dist/cli/upgrade/fixtures-to-data.js +498 -0
  68. package/dist/cli/upgrade/index.js +18 -2
  69. package/dist/cli/upgrade/legacy-fixtures.d.ts +34 -0
  70. package/dist/cli/upgrade/legacy-fixtures.js +22 -0
  71. package/dist/cli/upgrade/stage.js +1 -1
  72. package/dist/cli/workspace-fixtures.js +80 -0
  73. package/dist/cli.js +1 -0
  74. package/dist/css/compile.js +9 -3
  75. package/dist/fixtures/bundle-rule.js +16 -16
  76. package/dist/fixtures/media-plugin.js +12 -33
  77. package/dist/fixtures/synthesize-empties.js +3 -2
  78. package/dist/index.d.ts +7 -7
  79. package/dist/{playground-island-runtime.d.ts → island-runtime.d.ts} +1 -2
  80. package/dist/{playground-island-runtime.js → island-runtime.js} +1 -2
  81. package/dist/islands/client-module.js +1 -1
  82. package/dist/{cli/dev → islands}/tsconfig-paths.js +2 -14
  83. package/dist/islands/wrap.js +40 -0
  84. package/dist/node_modules/.pnpm/acorn@8.18.0/node_modules/acorn/dist/acorn.js +5119 -0
  85. package/dist/packages/external/concrete/package.js +1 -1
  86. package/dist/packages/internal/bundle-evaluation/dist/cache.js +36 -0
  87. package/dist/packages/internal/bundle-evaluation/dist/evaluate.js +174 -0
  88. package/dist/packages/internal/bundle-evaluation/dist/index.js +5 -0
  89. package/dist/packages/internal/bundle-evaluation/dist/section-code.js +10 -0
  90. package/dist/packages/internal/capture-settle/dist/index.js +2 -2
  91. package/dist/packages/internal/capture-settle/dist/settle.js +88 -13
  92. package/dist/packages/internal/code-fingerprint/dist/index.js +57 -0
  93. package/dist/packages/internal/content-doc-ops/dist/index.js +176 -0
  94. package/dist/packages/internal/content-doc-ops/dist/made-rows.js +15 -0
  95. package/dist/packages/internal/deliverable-reads/dist/bootstrap.js +81 -0
  96. package/dist/packages/internal/deliverable-reads/dist/display.js +21 -0
  97. package/dist/packages/internal/deliverable-reads/dist/format.js +15 -0
  98. package/dist/packages/internal/deliverable-reads/dist/index.js +7 -0
  99. package/dist/packages/internal/deliverable-reads/dist/pages.js +29 -0
  100. package/dist/packages/internal/deliverable-reads/dist/requirements.js +72 -0
  101. package/dist/{cli/capture → packages/internal/download-files/dist}/bundle.js +8 -4
  102. package/dist/packages/internal/download-files/dist/capture-origin.js +126 -0
  103. package/dist/packages/internal/download-files/dist/capture-routes.js +139 -0
  104. package/dist/packages/internal/download-files/dist/capture-shell.js +38 -0
  105. package/dist/packages/internal/download-files/dist/capture.js +74 -0
  106. package/dist/packages/internal/download-files/dist/egress-watch.js +38 -0
  107. package/dist/packages/internal/download-files/dist/files.js +7 -0
  108. package/dist/packages/internal/download-files/dist/finish.js +35 -0
  109. package/dist/packages/internal/download-files/dist/fleet.js +94 -0
  110. package/dist/packages/internal/download-files/dist/format.js +40 -0
  111. package/dist/packages/internal/download-files/dist/make.js +105 -0
  112. package/dist/packages/internal/download-files/dist/master-tally.js +32 -0
  113. package/dist/packages/internal/download-files/dist/names.js +24 -0
  114. package/dist/packages/internal/download-files/dist/pdf-boxes.js +33 -0
  115. package/dist/packages/internal/download-files/dist/pdf-image-placements.js +194 -0
  116. package/dist/packages/internal/download-files/dist/pdf-images.js +289 -0
  117. package/dist/packages/internal/file-store/dist/files.js +8 -0
  118. package/dist/packages/internal/file-store/dist/lru.js +40 -0
  119. package/dist/packages/internal/file-store/dist/refusal.js +12 -0
  120. package/dist/packages/internal/file-store/dist/trees.js +151 -0
  121. package/dist/packages/internal/fill-bake/dist/bake-property-ref.js +1 -1
  122. package/dist/packages/internal/fill-bake/dist/compute-reads.js +47 -21
  123. package/dist/packages/internal/fill-bake/dist/index.js +5 -1
  124. package/dist/packages/internal/fill-bake/dist/item-merge.js +4 -3
  125. package/dist/packages/internal/fill-bake/dist/resolve-instance.js +9 -0
  126. package/dist/packages/internal/fill-bake/dist/resolve.js +3 -0
  127. package/dist/packages/internal/fill-bake/dist/round-trip.js +2 -0
  128. package/dist/packages/internal/fill-bake/dist/strip.js +6 -0
  129. package/dist/packages/internal/fill-vocabulary/dist/holder.js +1 -1
  130. package/dist/packages/internal/fill-vocabulary/dist/resolved-decision.js +1 -1
  131. package/dist/packages/internal/fill-vocabulary/dist/verbs.js +1 -1
  132. package/dist/packages/internal/island-runtime/dist/index.js +1 -10
  133. package/dist/packages/internal/island-runtime/dist/use-capture-ready.d.ts +5 -0
  134. package/dist/packages/internal/lead-fields/dist/ingest.js +72 -0
  135. package/dist/packages/internal/media-resolution/dist/video.js +6 -2
  136. package/dist/packages/internal/page-render/dist/addresses.js +57 -0
  137. package/dist/packages/internal/page-render/dist/index.js +6 -0
  138. package/dist/packages/internal/page-render/dist/kept.js +30 -0
  139. package/dist/packages/internal/page-render/dist/nav.js +42 -0
  140. package/dist/packages/internal/page-render/dist/render.js +152 -0
  141. package/dist/packages/internal/page-shell/dist/assemble.js +161 -0
  142. package/dist/packages/internal/page-shell/dist/escape.js +10 -0
  143. package/dist/packages/internal/page-shell/dist/frame.js +23 -0
  144. package/dist/packages/internal/page-shell/dist/index.js +26 -0
  145. package/dist/packages/internal/playground/dist/build-fingerprint.js +18 -0
  146. package/dist/packages/internal/playground/dist/fixture-conversions/list-names-properties.js +76 -0
  147. package/dist/packages/internal/playground/dist/fixture-formats.d.ts +37 -0
  148. package/dist/packages/internal/playground/dist/fixture-formats.js +20 -0
  149. package/dist/packages/internal/playground/dist/host/bootstrap.js +108 -0
  150. package/dist/packages/internal/playground/dist/host/built.js +202 -0
  151. package/dist/packages/internal/playground/dist/host/catalog-fixture.js +53 -0
  152. package/dist/packages/internal/playground/dist/host/download.js +34 -0
  153. package/dist/packages/internal/playground/dist/host/env.js +11 -0
  154. package/dist/packages/internal/playground/dist/host/everywhere-saves.js +353 -0
  155. package/dist/packages/internal/playground/dist/host/fixture-addresses.js +52 -0
  156. package/dist/packages/internal/playground/dist/host/fixture-documents.js +494 -0
  157. package/dist/packages/internal/playground/dist/host/fixture-operations.js +364 -0
  158. package/dist/packages/internal/playground/dist/host/fixture-units.js +93 -0
  159. package/dist/{fixtures → packages/internal/playground/dist/host}/image-dimensions.js +1 -1
  160. package/dist/packages/internal/playground/dist/host/leads.js +41 -0
  161. package/dist/packages/internal/playground/dist/host/media-saves.js +309 -0
  162. package/dist/{fixtures → packages/internal/playground/dist/host}/media-store.js +79 -26
  163. package/dist/packages/internal/playground/dist/host/overridable.js +9 -0
  164. package/dist/packages/internal/playground/dist/host/owned-saves.js +203 -0
  165. package/dist/{playground → packages/internal/playground/dist}/host/page-state.js +1 -1
  166. package/dist/packages/internal/playground/dist/host/program.js +54 -0
  167. package/dist/packages/internal/playground/dist/host/property-derive.js +216 -0
  168. package/dist/packages/internal/playground/dist/host/property-saves.js +74 -0
  169. package/dist/{fixtures → packages/internal/playground/dist/host}/property.js +88 -229
  170. package/dist/packages/internal/playground/dist/host/provenance.js +49 -0
  171. package/dist/packages/internal/playground/dist/host/resolve.js +553 -0
  172. package/dist/packages/internal/playground/dist/host/row-images.js +41 -0
  173. package/dist/{fixtures → packages/internal/playground/dist/host}/rows.js +1 -1
  174. package/dist/packages/internal/playground/dist/host/save.js +429 -0
  175. package/dist/packages/internal/playground/dist/host/serve.js +952 -0
  176. package/dist/packages/internal/playground/dist/host/site.js +79 -0
  177. package/dist/packages/internal/playground/dist/host/slots.js +202 -0
  178. package/dist/packages/internal/playground/dist/host/stated-content.js +93 -0
  179. package/dist/packages/internal/playground/dist/host/stored-fixtures.js +135 -0
  180. package/dist/packages/internal/playground/dist/host/violations.js +7 -0
  181. package/dist/packages/internal/playground/dist/jobs/lanes.js +24 -0
  182. package/dist/packages/internal/playground/dist/jobs/memory-queue.js +112 -0
  183. package/dist/packages/internal/playground/dist/jobs/probe.js +37 -0
  184. package/dist/packages/internal/playground/dist/jobs/queue.js +28 -0
  185. package/dist/packages/internal/playground/dist/jobs/runner.js +207 -0
  186. package/dist/packages/internal/playground/dist/layout.js +14 -0
  187. package/dist/packages/internal/playground/dist/platform.js +33 -0
  188. package/dist/packages/internal/playground/dist/server/changes.js +57 -0
  189. package/dist/packages/internal/playground/dist/server/crop.js +47 -0
  190. package/dist/packages/internal/playground/dist/server/derived-files.js +88 -0
  191. package/dist/packages/internal/playground/dist/server/download.js +158 -0
  192. package/dist/{cli/dev → packages/internal/playground/dist/server}/http.js +4 -12
  193. package/dist/packages/internal/playground/dist/server/jobs.js +29 -0
  194. package/dist/{cli/dev → packages/internal/playground/dist/server}/listen.js +4 -11
  195. package/dist/packages/internal/playground/dist/server/playground-server.js +424 -0
  196. package/dist/{cli/dev → packages/internal/playground/dist/server}/request-error.js +1 -1
  197. package/dist/packages/internal/playground/dist/server/size-ladders.js +170 -0
  198. package/dist/packages/internal/playground/dist/server/verification.js +75 -0
  199. package/dist/packages/internal/playground/dist/server/verify-build.js +139 -0
  200. package/dist/packages/internal/playground/dist/wire.js +78 -0
  201. package/dist/packages/internal/property-facts/dist/registry-facts.js +1 -1
  202. package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +1 -1
  203. package/dist/packages/internal/section-contracts/dist/reads-acyclic-contract.js +1 -1
  204. package/dist/packages/internal/section-contracts/dist/row-fill-contract.js +1 -1
  205. package/dist/packages/internal/site-files/dist/head.js +59 -0
  206. package/dist/packages/internal/site-files/dist/index.js +4 -0
  207. package/dist/packages/internal/site-files/dist/site.js +211 -0
  208. package/dist/packages/internal/static-assets-binding/dist/index.js +32 -0
  209. package/dist/packages/internal/static-assets-binding/dist/section-asset-url.js +38 -5
  210. package/dist/packages/internal/template-artifact/dist/asset-mime.js +8 -1
  211. package/dist/packages/internal/template-artifact/dist/build.js +148 -0
  212. package/dist/packages/internal/template-artifact/dist/composition.js +62 -0
  213. package/dist/packages/internal/template-artifact/dist/index.js +2 -2
  214. package/dist/packages/internal/template-artifact/dist/layout.js +48 -12
  215. package/dist/packages/internal/template-artifact/dist/manifest.js +3 -10
  216. package/dist/packages/internal/template-artifact/dist/verify.js +51 -31
  217. package/dist/packages/internal/template-contracts/dist/address-value.js +6 -1
  218. package/dist/packages/internal/template-contracts/dist/content-categories.js +10 -1
  219. package/dist/packages/internal/template-contracts/dist/fact-compute.d.ts +1 -1
  220. package/dist/packages/internal/template-contracts/dist/fact-keys.d.ts +1 -1
  221. package/dist/packages/internal/template-contracts/dist/fact-registry.d.ts +3 -3
  222. package/dist/packages/internal/template-contracts/dist/fact-registry.js +59 -32
  223. package/dist/packages/internal/template-contracts/dist/fill-spec.js +14 -1
  224. package/dist/packages/internal/template-contracts/dist/list.d.ts +3 -3
  225. package/dist/packages/internal/template-contracts/dist/media-candidates.js +67 -2
  226. package/dist/packages/internal/template-contracts/dist/media-offer.js +180 -0
  227. package/dist/packages/internal/template-contracts/dist/object.d.ts +2 -2
  228. package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +9 -2
  229. package/dist/packages/internal/template-contracts/dist/read-graph.js +36 -2
  230. package/dist/packages/internal/template-contracts/dist/ref-locate.js +16 -1
  231. package/dist/packages/internal/template-contracts/dist/registry-facts.d.ts +9 -10
  232. package/dist/packages/internal/template-contracts/dist/registry-facts.js +12 -1
  233. package/dist/packages/internal/template-contracts/dist/registry-graph.js +112 -0
  234. package/dist/packages/internal/template-contracts/dist/registry-shape.js +15 -2
  235. package/dist/packages/internal/template-contracts/dist/require-graph.js +300 -0
  236. package/dist/packages/internal/template-contracts/dist/row-ref.d.ts +3 -3
  237. package/dist/packages/internal/template-contracts/dist/row.d.ts +3 -3
  238. package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +1 -1
  239. package/dist/{schema → packages/internal/template-contracts/dist}/section-nav.d.ts +1 -1
  240. package/dist/packages/internal/template-contracts/dist/slot-builders.d.ts +4 -4
  241. package/dist/packages/internal/template-contracts/dist/slot-catalog.d.ts +3 -1
  242. package/dist/packages/internal/template-contracts/dist/slot-catalog.js +24 -1
  243. package/dist/packages/internal/template-contracts/dist/slot-overrides.js +26 -1
  244. package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +1 -1
  245. package/dist/packages/internal/template-contracts/dist/slot-types.js +1 -1
  246. package/dist/packages/internal/template-contracts/dist/slot-value.d.ts +1 -1
  247. package/dist/packages/internal/template-contracts/dist/table-members.js +7 -2
  248. package/dist/packages/internal/template-contracts/dist/video-value.d.ts +1 -1
  249. package/dist/packages/internal/upload-formats/dist/heic.js +35 -0
  250. package/dist/packages/internal/upload-formats/dist/index.js +80 -0
  251. package/dist/packages/internal/upload-formats/dist/magic-bytes.js +293 -0
  252. package/dist/platform/paths.js +1 -0
  253. package/dist/platform/process.js +49 -1
  254. package/dist/rules/registry.js +27 -3
  255. package/dist/schema/fact.d.ts +2 -2
  256. package/dist/schema/facts-view.d.ts +1 -1
  257. package/dist/schema/section-props.d.ts +1 -1
  258. package/dist/schema/section-schema.d.ts +1 -1
  259. package/dist/schema/section-schema.js +1 -0
  260. package/dist/section/declare.d.ts +1 -1
  261. package/dist/section/discovery.js +6 -2
  262. package/dist/section/publish-shape.js +38 -0
  263. package/dist/section/section.d.ts +1 -1
  264. package/dist/template/define-template.d.ts +14 -4
  265. package/dist/template/define-template.js +2 -0
  266. package/dist/template/logic-shim.d.ts +1 -1
  267. package/dist/template/materialize.js +1 -1
  268. package/dist/template/running-copy.js +0 -1
  269. package/dist/template/section-reads.js +1 -1
  270. package/guide/reference/cli/INDEX.md +6 -5
  271. package/guide/reference/cli/build.md +10 -8
  272. package/guide/reference/cli/check.md +4 -2
  273. package/guide/reference/cli/dev.md +6 -2
  274. package/guide/reference/cli/install.md +41 -0
  275. package/guide/reference/cli/property-add.md +5 -4
  276. package/guide/reference/cli/property-list.md +2 -2
  277. package/guide/reference/cli/publish.md +5 -5
  278. package/guide/reference/cli/upgrade.md +1 -1
  279. package/guide/reference/rules/INDEX.md +8 -4
  280. package/package.json +14 -30
  281. package/dist/artifact/playground/client/assets/index-BcLGpWLR.js +0 -1266
  282. package/dist/artifact/playground/client/assets/index-CxWPYOH2.css +0 -1
  283. package/dist/cli/build/artifact.js +0 -220
  284. package/dist/cli/build/captures.js +0 -48
  285. package/dist/cli/build/fixtures.js +0 -92
  286. package/dist/cli/capture/capture-browser.js +0 -37
  287. package/dist/cli/capture/capture.js +0 -115
  288. package/dist/cli/check/render/inputs.js +0 -60
  289. package/dist/cli/check/render/nav.js +0 -31
  290. package/dist/cli/dev/crop.js +0 -20
  291. package/dist/cli/dev/fixture-ladders.js +0 -96
  292. package/dist/cli/dev/hmr.js +0 -26
  293. package/dist/cli/dev/islands.js +0 -234
  294. package/dist/cli/dev/load-template.js +0 -52
  295. package/dist/cli/dev/vite.js +0 -114
  296. package/dist/cli/dev/workspace-reader.js +0 -249
  297. package/dist/cli/publish/freshness.js +0 -64
  298. package/dist/fixtures/brand.d.ts +0 -4
  299. package/dist/fixtures/brand.js +0 -8
  300. package/dist/fixtures/load.js +0 -159
  301. package/dist/fixtures/resolve.js +0 -392
  302. package/dist/fixtures.d.ts +0 -429
  303. package/dist/fixtures.js +0 -23
  304. package/dist/packages/internal/canvas-core/dist/island-host.js +0 -62
  305. package/dist/packages/internal/canvas-core/dist/island-shields.js +0 -30
  306. package/dist/playground/canvas-island-host.d.ts +0 -11
  307. package/dist/playground/canvas-island-host.js +0 -29
  308. package/dist/playground/canvas-markers.js +0 -14
  309. package/dist/playground/host/paths.js +0 -8
  310. package/dist/playground/layout.js +0 -17
  311. package/dist/playground/wire.js +0 -28
@@ -0,0 +1,289 @@
1
+ import { measureImagePlacements } from "./pdf-image-placements.js";
2
+ import { createHash } from "node:crypto";
3
+ import { deflateSync } from "node:zlib";
4
+ import { PDFArray, PDFDict, PDFDocument, PDFName, PDFNumber, PDFRawStream, PDFRef, decodePDFRawStream } from "pdf-lib";
5
+ import sharp from "sharp";
6
+
7
+ //#region ../../internal/download-files/dist/pdf-images.js
8
+ sharp.cache(false);
9
+ const MIN_PIXELS_PER_CSS_PX = 3;
10
+ const JPEG_QUALITY = 92;
11
+ const PHOTO_MIN_BITS_PER_PIXEL = 1;
12
+ const PHOTO_MAX_JPEG_RATIO = .25;
13
+ const RESAMPLE_BELOW_SCALE = .8;
14
+ const JPEG_RESAMPLE_MAX_RATIO = .8;
15
+ function imageCeilingPixelsPerPoint(geometry) {
16
+ const cssPxPerPoint = geometry.viewportWidth / geometry.pointWidth;
17
+ return Math.max(geometry.deviceScaleFactor, MIN_PIXELS_PER_CSS_PX) * cssPxPerPoint;
18
+ }
19
+ async function optimizePdfImages(bytes, args) {
20
+ const doc = await PDFDocument.load(bytes, { updateMetadata: false });
21
+ const deduplicated = deduplicateImages(doc);
22
+ const placements = measureImagePlacements(doc);
23
+ const softMasks = pairSoftMasks(doc);
24
+ let reencoded = 0;
25
+ let resampled = 0;
26
+ for (const [ref, image] of imagesOf(doc)) {
27
+ const key = ref.toString();
28
+ if (softMasks.paired.has(key)) continue;
29
+ const placement = placementOf(doc, key, placements);
30
+ const softMask = softMasks.byImage.get(key);
31
+ const outcome = await optimizeImage(image, placement, args.pixelsPerPoint, softMask?.stream);
32
+ if (outcome) {
33
+ doc.context.assign(ref, outcome.stream);
34
+ if (outcome.reencoded) reencoded += 1;
35
+ if (outcome.resampled) resampled += 1;
36
+ if (outcome.softMask && softMask) {
37
+ doc.context.assign(softMask.ref, outcome.softMask);
38
+ resampled += 1;
39
+ }
40
+ }
41
+ }
42
+ const out = deduplicated + reencoded + resampled > 0 ? Buffer.from(await doc.save()) : bytes;
43
+ return {
44
+ bytes: out,
45
+ report: {
46
+ bytesBefore: bytes.byteLength,
47
+ bytesAfter: out.byteLength,
48
+ deduplicated,
49
+ reencoded,
50
+ resampled,
51
+ placementsMeasured: placements.complete
52
+ }
53
+ };
54
+ }
55
+ async function finishPdfImages(bytes, args) {
56
+ const { bytes: out, report } = await optimizePdfImages(bytes, { pixelsPerPoint: imageCeilingPixelsPerPoint(args.geometry) });
57
+ args.log.info({
58
+ bytes_before: report.bytesBefore,
59
+ bytes_after: report.bytesAfter,
60
+ images_deduplicated: report.deduplicated,
61
+ images_reencoded: report.reencoded,
62
+ images_resampled: report.resampled,
63
+ placements_measured: report.placementsMeasured
64
+ }, "download: optimized embedded PDF images");
65
+ return out;
66
+ }
67
+ function imagesOf(doc) {
68
+ const images = [];
69
+ for (const [ref, object] of doc.context.enumerateIndirectObjects()) if (object instanceof PDFRawStream && object.dict.get(PDFName.of("Subtype")) === PDFName.of("Image")) images.push([ref, object]);
70
+ return images;
71
+ }
72
+ function placementOf(doc, key, placements) {
73
+ const { largest, unmeasured, complete } = placements;
74
+ if (!complete || unmeasured.has(key)) return void 0;
75
+ const direct = largest.get(key);
76
+ if (direct) return direct;
77
+ let widthPt = 0;
78
+ let heightPt = 0;
79
+ for (const [ref, image] of imagesOf(doc)) {
80
+ const mask = image.dict.get(PDFName.of("SMask"));
81
+ if (mask instanceof PDFRef && mask.toString() === key) {
82
+ const parent = unmeasured.has(ref.toString()) ? void 0 : largest.get(ref.toString());
83
+ if (!parent) return void 0;
84
+ widthPt = Math.max(widthPt, parent.widthPt);
85
+ heightPt = Math.max(heightPt, parent.heightPt);
86
+ }
87
+ }
88
+ return widthPt > 0 ? {
89
+ widthPt,
90
+ heightPt
91
+ } : void 0;
92
+ }
93
+ function pairSoftMasks(doc) {
94
+ const images = imagesOf(doc);
95
+ const maskUses = /* @__PURE__ */ new Map();
96
+ for (const [, image] of images) {
97
+ const mask = image.dict.get(PDFName.of("SMask"));
98
+ if (mask instanceof PDFRef) maskUses.set(mask.toString(), (maskUses.get(mask.toString()) ?? 0) + 1);
99
+ }
100
+ const byImage = /* @__PURE__ */ new Map();
101
+ const paired = /* @__PURE__ */ new Set();
102
+ for (const [ref, image] of images) {
103
+ const maskRef = image.dict.get(PDFName.of("SMask"));
104
+ if (!(maskRef instanceof PDFRef) || maskUses.get(maskRef.toString()) !== 1) continue;
105
+ const mask = doc.context.lookup(maskRef);
106
+ if (!(mask instanceof PDFRawStream) || mask.dict.has(PDFName.of("Matte"))) continue;
107
+ const imageShape = rewritableShape(image);
108
+ const maskShape = rewritableShape(mask);
109
+ if (imageShape?.filter !== "FlateDecode" || imageShape.channels !== 3 || maskShape?.filter !== "FlateDecode" || maskShape.channels !== 1 || maskShape.width !== imageShape.width || maskShape.height !== imageShape.height) continue;
110
+ byImage.set(ref.toString(), {
111
+ ref: maskRef,
112
+ stream: mask
113
+ });
114
+ paired.add(maskRef.toString());
115
+ }
116
+ return {
117
+ byImage,
118
+ paired
119
+ };
120
+ }
121
+ function deduplicateImages(doc) {
122
+ let total = 0;
123
+ for (;;) {
124
+ const firstByHash = /* @__PURE__ */ new Map();
125
+ const replace = /* @__PURE__ */ new Map();
126
+ for (const [ref, image] of imagesOf(doc)) {
127
+ const hash = imageHash(image);
128
+ const first = firstByHash.get(hash);
129
+ if (first) replace.set(ref.toString(), first);
130
+ else firstByHash.set(hash, ref);
131
+ }
132
+ if (replace.size === 0) return total;
133
+ for (const [, object] of doc.context.enumerateIndirectObjects()) replaceRefs(object instanceof PDFRawStream ? object.dict : object, replace);
134
+ for (const [ref] of doc.context.enumerateIndirectObjects()) if (replace.has(ref.toString())) doc.context.delete(ref);
135
+ total += replace.size;
136
+ }
137
+ }
138
+ function imageHash(image) {
139
+ const hash = createHash("sha256");
140
+ const entries = image.dict.entries().filter(([name]) => name !== PDFName.of("Length")).map(([name, value]) => `${name.toString()} ${value.toString()}`).sort();
141
+ hash.update(entries.join("\n"));
142
+ hash.update(image.contents);
143
+ return hash.digest("hex");
144
+ }
145
+ function replaceRefs(object, replace) {
146
+ if (object instanceof PDFDict) for (const [name, value] of object.entries()) {
147
+ const target = value instanceof PDFRef ? replace.get(value.toString()) : void 0;
148
+ if (target) object.set(name, target);
149
+ else replaceRefs(value, replace);
150
+ }
151
+ else if (object instanceof PDFArray) for (let i = 0; i < object.size(); i += 1) {
152
+ const value = object.get(i);
153
+ const target = value instanceof PDFRef ? replace.get(value.toString()) : void 0;
154
+ if (target) object.set(i, target);
155
+ else replaceRefs(value, replace);
156
+ }
157
+ }
158
+ async function optimizeImage(image, placement, pixelsPerPoint, softMask) {
159
+ const shape = rewritableShape(image);
160
+ if (!shape) return void 0;
161
+ const scale = placement ? resampleScale(shape, placement, pixelsPerPoint) : 1;
162
+ const resample = scale < RESAMPLE_BELOW_SCALE;
163
+ const width = resample ? Math.max(1, Math.round(shape.width * scale)) : shape.width;
164
+ const height = resample ? Math.max(1, Math.round(shape.height * scale)) : shape.height;
165
+ if (shape.filter === "DCTDecode") {
166
+ if (!resample) return void 0;
167
+ const jpeg = await encodeJpeg(sharp(image.contents), shape.channels, width, height);
168
+ if (jpeg.byteLength > image.contents.byteLength * JPEG_RESAMPLE_MAX_RATIO) return void 0;
169
+ return {
170
+ stream: rewritten(image, "DCTDecode", jpeg, width, height),
171
+ reencoded: false,
172
+ resampled: true
173
+ };
174
+ }
175
+ const bitsPerPixel = image.contents.byteLength * 8 / (shape.width * shape.height);
176
+ const photographic = shape.channels === 3 && !shape.hasSoftMask && bitsPerPixel > PHOTO_MIN_BITS_PER_PIXEL;
177
+ if (!photographic && !resample) return void 0;
178
+ const raw = Buffer.from(decodePDFRawStream(image).decode());
179
+ if (raw.byteLength !== shape.width * shape.height * shape.channels) return void 0;
180
+ const pixels = () => sharp(raw, { raw: {
181
+ width: shape.width,
182
+ height: shape.height,
183
+ channels: shape.channels
184
+ } });
185
+ if (photographic) {
186
+ const jpeg = await encodeJpeg(pixels(), shape.channels, width, height);
187
+ if (jpeg.byteLength <= image.contents.byteLength * PHOTO_MAX_JPEG_RATIO) return {
188
+ stream: rewritten(image, "DCTDecode", jpeg, width, height),
189
+ reencoded: true,
190
+ resampled: resample
191
+ };
192
+ if (!resample) return void 0;
193
+ }
194
+ if (softMask) return resampleCutout(image, raw, softMask, shape, width, height);
195
+ const resized = await pixels().resize(width, height, { kernel: "lanczos3" }).toColourspace(shape.channels === 1 ? "b-w" : "srgb").raw().toBuffer();
196
+ if (resized.byteLength !== width * height * shape.channels) return void 0;
197
+ return {
198
+ stream: rewritten(image, "FlateDecode", deflateSync(resized), width, height),
199
+ reencoded: false,
200
+ resampled: true
201
+ };
202
+ }
203
+ async function resampleCutout(image, colour, softMask, shape, width, height) {
204
+ const alpha = Buffer.from(decodePDFRawStream(softMask).decode());
205
+ const sourcePixels = shape.width * shape.height;
206
+ if (alpha.byteLength !== sourcePixels) return void 0;
207
+ const joined = Buffer.alloc(sourcePixels * 4);
208
+ for (let p = 0; p < sourcePixels; p += 1) {
209
+ colour.copy(joined, p * 4, p * 3, p * 3 + 3);
210
+ joined[p * 4 + 3] = alpha[p];
211
+ }
212
+ const resized = await sharp(joined, { raw: {
213
+ width: shape.width,
214
+ height: shape.height,
215
+ channels: 4
216
+ } }).resize(width, height, { kernel: "lanczos3" }).toColourspace("srgb").raw().toBuffer();
217
+ const pixels = width * height;
218
+ if (resized.byteLength !== pixels * 4) return void 0;
219
+ const resizedColour = Buffer.alloc(pixels * 3);
220
+ const resizedAlpha = Buffer.alloc(pixels);
221
+ for (let p = 0; p < pixels; p += 1) {
222
+ resized.copy(resizedColour, p * 3, p * 4, p * 4 + 3);
223
+ resizedAlpha[p] = resized[p * 4 + 3];
224
+ }
225
+ return {
226
+ stream: rewritten(image, "FlateDecode", deflateSync(resizedColour), width, height),
227
+ softMask: rewritten(softMask, "FlateDecode", deflateSync(resizedAlpha), width, height),
228
+ reencoded: false,
229
+ resampled: true
230
+ };
231
+ }
232
+ function resampleScale(shape, placement, pixelsPerPoint) {
233
+ const neededWidth = placement.widthPt * pixelsPerPoint;
234
+ const neededHeight = placement.heightPt * pixelsPerPoint;
235
+ return Math.min(1, Math.max(neededWidth / shape.width, neededHeight / shape.height));
236
+ }
237
+ async function encodeJpeg(input, channels, width, height) {
238
+ return input.resize(width, height, { kernel: "lanczos3" }).toColourspace(channels === 1 ? "b-w" : "srgb").jpeg({
239
+ quality: JPEG_QUALITY,
240
+ chromaSubsampling: "4:4:4",
241
+ mozjpeg: true
242
+ }).toBuffer();
243
+ }
244
+ function rewritableShape(image) {
245
+ const dict = image.dict;
246
+ const context = dict.context;
247
+ const width = numberOf(dict.get(PDFName.of("Width")));
248
+ const height = numberOf(dict.get(PDFName.of("Height")));
249
+ if (!width || !height || numberOf(dict.get(PDFName.of("BitsPerComponent"))) !== 8) return void 0;
250
+ if (dict.has(PDFName.of("Decode")) || dict.has(PDFName.of("ImageMask")) || dict.has(PDFName.of("Mask"))) return;
251
+ let filter = dict.get(PDFName.of("Filter"));
252
+ if (filter instanceof PDFArray && filter.size() === 1) filter = filter.get(0);
253
+ const filterName = filter === PDFName.of("FlateDecode") ? "FlateDecode" : filter === PDFName.of("DCTDecode") ? "DCTDecode" : void 0;
254
+ if (!filterName) return void 0;
255
+ const channels = colorChannels(context.lookup(dict.get(PDFName.of("ColorSpace"))), dict);
256
+ if (!channels) return void 0;
257
+ return {
258
+ width,
259
+ height,
260
+ channels,
261
+ filter: filterName,
262
+ hasSoftMask: dict.has(PDFName.of("SMask"))
263
+ };
264
+ }
265
+ function colorChannels(colorSpace, dict) {
266
+ if (colorSpace === PDFName.of("DeviceGray")) return 1;
267
+ if (colorSpace === PDFName.of("DeviceRGB")) return 3;
268
+ if (colorSpace instanceof PDFArray && colorSpace.get(0) === PDFName.of("ICCBased")) {
269
+ const profile = dict.context.lookup(colorSpace.get(1));
270
+ const n = profile instanceof PDFRawStream ? numberOf(profile.dict.get(PDFName.of("N"))) : void 0;
271
+ return n === 1 ? 1 : n === 3 ? 3 : void 0;
272
+ }
273
+ }
274
+ function numberOf(object) {
275
+ return object instanceof PDFNumber ? object.asNumber() : void 0;
276
+ }
277
+ function rewritten(image, filter, contents, width, height) {
278
+ const dict = image.dict.clone(image.dict.context);
279
+ dict.delete(PDFName.of("DecodeParms"));
280
+ dict.delete(PDFName.of("ColorTransform"));
281
+ dict.delete(PDFName.of("Length"));
282
+ dict.set(PDFName.of("Filter"), PDFName.of(filter));
283
+ dict.set(PDFName.of("Width"), PDFNumber.of(width));
284
+ dict.set(PDFName.of("Height"), PDFNumber.of(height));
285
+ return PDFRawStream.of(dict, contents);
286
+ }
287
+
288
+ //#endregion
289
+ export { finishPdfImages, imageCeilingPixelsPerPoint, optimizePdfImages };
@@ -0,0 +1,8 @@
1
+ //#region ../../internal/file-store/dist/files.js
2
+ async function fingerprintOf(bytes) {
3
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
4
+ return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
5
+ }
6
+
7
+ //#endregion
8
+ export { fingerprintOf };
@@ -0,0 +1,40 @@
1
+ //#region ../../internal/file-store/dist/lru.js
2
+ var Lru = class {
3
+ capacity;
4
+ cost;
5
+ entries = /* @__PURE__ */ new Map();
6
+ held = 0;
7
+ constructor(capacity, cost = () => 1) {
8
+ this.capacity = capacity;
9
+ this.cost = cost;
10
+ }
11
+ get(key) {
12
+ const entry = this.entries.get(key);
13
+ if (entry === void 0) return void 0;
14
+ this.entries.delete(key);
15
+ this.entries.set(key, entry);
16
+ return entry.value;
17
+ }
18
+ set(key, value) {
19
+ const cost = this.cost(value);
20
+ if (cost > this.capacity) return;
21
+ const old = this.entries.get(key);
22
+ if (old !== void 0) {
23
+ this.held -= old.cost;
24
+ this.entries.delete(key);
25
+ }
26
+ this.entries.set(key, {
27
+ value,
28
+ cost
29
+ });
30
+ this.held += cost;
31
+ for (const [oldest, entry] of this.entries) {
32
+ if (this.held <= this.capacity) break;
33
+ this.entries.delete(oldest);
34
+ this.held -= entry.cost;
35
+ }
36
+ }
37
+ };
38
+
39
+ //#endregion
40
+ export { Lru };
@@ -0,0 +1,12 @@
1
+ //#region ../../internal/file-store/dist/refusal.js
2
+ var StoreRefusal = class extends Error {
3
+ status;
4
+ constructor(status, message) {
5
+ super(message);
6
+ this.status = status;
7
+ this.name = "StoreRefusal";
8
+ }
9
+ };
10
+
11
+ //#endregion
12
+ export { StoreRefusal };
@@ -0,0 +1,151 @@
1
+ import { fingerprintOf } from "./files.js";
2
+ import { StoreRefusal } from "./refusal.js";
3
+ import { Lru } from "./lru.js";
4
+
5
+ //#region ../../internal/file-store/dist/trees.js
6
+ const byCodeUnit = (a, b) => a < b ? -1 : a > b ? 1 : 0;
7
+ async function treeFingerprint(entries) {
8
+ const pairs = Object.keys(entries).sort(byCodeUnit).map((name) => {
9
+ const entry = entries[name];
10
+ return [name, "t" in entry ? { t: entry.t } : {
11
+ c: entry.c,
12
+ s: entry.s
13
+ }];
14
+ });
15
+ return fingerprintOf(new TextEncoder().encode(JSON.stringify(pairs)));
16
+ }
17
+ const EMPTY_TREE = "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945";
18
+ var TreeReader = class {
19
+ load;
20
+ cache;
21
+ constructor(load, capacity = 2e5) {
22
+ this.load = load;
23
+ this.cache = new Lru(capacity, (entries) => Object.keys(entries).length + 1);
24
+ }
25
+ async trees(fingerprints) {
26
+ const found = /* @__PURE__ */ new Map();
27
+ const missing = [];
28
+ for (const fp of new Set(fingerprints)) {
29
+ const cached = fp === "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945" ? {} : this.cache.get(fp);
30
+ if (cached === void 0) missing.push(fp);
31
+ else found.set(fp, cached);
32
+ }
33
+ if (missing.length > 0) {
34
+ const loaded = await this.load(missing);
35
+ for (const fp of missing) {
36
+ const entries = loaded.get(fp);
37
+ if (entries === void 0) throw new Error(`the store has no tree ${fp}`);
38
+ this.cache.set(fp, entries);
39
+ found.set(fp, entries);
40
+ }
41
+ }
42
+ return found;
43
+ }
44
+ async tree(fingerprint) {
45
+ return (await this.trees([fingerprint])).get(fingerprint);
46
+ }
47
+ async entry(root, path) {
48
+ const segments = path.split("/");
49
+ let tree = await this.tree(root);
50
+ for (const [i, name] of segments.entries()) {
51
+ const entry = Object.hasOwn(tree, name) ? tree[name] : null;
52
+ if (entry === null || i === segments.length - 1) return entry;
53
+ if (!("t" in entry)) return null;
54
+ tree = await this.tree(entry.t);
55
+ }
56
+ return null;
57
+ }
58
+ async file(root, path) {
59
+ const entry = path === "" ? null : await this.entry(root, path);
60
+ return entry !== null && "c" in entry ? {
61
+ fingerprint: entry.c,
62
+ size: entry.s
63
+ } : null;
64
+ }
65
+ async list(root, path) {
66
+ let start;
67
+ if (path === "") start = root;
68
+ else {
69
+ const entry = await this.entry(root, path);
70
+ if (entry === null) return [];
71
+ if ("c" in entry) return [{
72
+ path,
73
+ fingerprint: entry.c,
74
+ size: entry.s
75
+ }];
76
+ start = entry.t;
77
+ }
78
+ const files = [];
79
+ let level = [{
80
+ fingerprint: start,
81
+ prefix: path === "" ? "" : `${path}/`
82
+ }];
83
+ while (level.length > 0) {
84
+ const trees = await this.trees(level.map((l) => l.fingerprint));
85
+ const next = [];
86
+ for (const { fingerprint, prefix } of level) for (const [name, entry] of Object.entries(trees.get(fingerprint))) if ("c" in entry) files.push({
87
+ path: `${prefix}${name}`,
88
+ fingerprint: entry.c,
89
+ size: entry.s
90
+ });
91
+ else next.push({
92
+ fingerprint: entry.t,
93
+ prefix: `${prefix}${name}/`
94
+ });
95
+ level = next;
96
+ }
97
+ return files.sort((a, b) => byCodeUnit(a.path, b.path));
98
+ }
99
+ };
100
+ async function applyToTree(reader, root, changes) {
101
+ const made = /* @__PURE__ */ new Map();
102
+ async function apply(fingerprint, here, prefix) {
103
+ const entries = fingerprint === null ? {} : { ...await reader.tree(fingerprint) };
104
+ const direct = /* @__PURE__ */ new Map();
105
+ const nested = /* @__PURE__ */ new Map();
106
+ for (const [path, file] of here) {
107
+ const slash = path.indexOf("/");
108
+ if (slash < 0) direct.set(path, file);
109
+ else {
110
+ const name = path.slice(0, slash);
111
+ if (!nested.has(name)) nested.set(name, /* @__PURE__ */ new Map());
112
+ nested.get(name).set(path.slice(slash + 1), file);
113
+ }
114
+ }
115
+ for (const [name, below] of nested) {
116
+ const existing = Object.hasOwn(entries, name) ? entries[name] : null;
117
+ const isFile = existing !== null && "c" in existing;
118
+ if (![...below.values()].some((file) => file !== null)) {
119
+ if (existing === null || isFile) continue;
120
+ } else if (direct.has(name) ? direct.get(name) !== null : isFile) throw new StoreRefusal(400, `A change writes under "${prefix}${name}", which is a file.`);
121
+ const child = await apply(existing !== null && "t" in existing ? existing.t : null, below, `${prefix}${name}/`);
122
+ if (child === null) delete entries[name];
123
+ else entries[name] = { t: child };
124
+ }
125
+ for (const [name, file] of direct) {
126
+ const existing = Object.hasOwn(entries, name) ? entries[name] : null;
127
+ if (file === null) {
128
+ if (existing !== null && "c" in existing) delete entries[name];
129
+ } else {
130
+ if (existing !== null && "t" in existing) throw new StoreRefusal(400, `A change writes "${prefix}${name}", which is a folder.`);
131
+ entries[name] = {
132
+ c: file.fingerprint,
133
+ s: file.size
134
+ };
135
+ }
136
+ }
137
+ if (Object.keys(entries).length === 0) return null;
138
+ const next = await treeFingerprint(entries);
139
+ if (next !== fingerprint) made.set(next, entries);
140
+ return next;
141
+ }
142
+ const next = await apply(root, changes, "") ?? "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945";
143
+ if (next === "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945") made.set(EMPTY_TREE, {});
144
+ return {
145
+ root: next,
146
+ made
147
+ };
148
+ }
149
+
150
+ //#endregion
151
+ export { EMPTY_TREE, TreeReader, applyToTree, treeFingerprint };
@@ -5,8 +5,8 @@ import { tableOf } from "../../template-contracts/dist/registry-shape.js";
5
5
  import { attributedRows, heldRowTableOf, holderOf, isHolderKind, listTableOf, rowTablesOf } from "../../template-contracts/dist/ref-locate.js";
6
6
  import "../../template-contracts/dist/table-members.js";
7
7
  import { elementRowKeys, listSourceOf, memberSourceOf } from "../../template-contracts/dist/row-sources.js";
8
- import { objectFieldOf, objectFieldSlotId } from "../../template-contracts/dist/slot-overrides.js";
9
8
  import { projectTableRows, readFactRef, tableRowIds } from "../../source-resolution/dist/index.js";
9
+ import { objectFieldOf, objectFieldSlotId } from "../../template-contracts/dist/slot-overrides.js";
10
10
  import { callDerived, readsAnySlot } from "./derive-source.js";
11
11
  import { declaredDisplay } from "./display.js";
12
12
  import { computedRow, contentIdentities, decisionProducing, isGrammarList, nestedItemOf, objectElementFields, projectElements, projectNestedList, readResolvedRowMember, rowIdentity, storedElements } from "./list-grammar.js";
@@ -30,7 +30,8 @@ function applyComputeReads(input) {
30
30
  if (declarations.size === 0) return {
31
31
  contentDoc: input.contentDoc,
32
32
  changed: false,
33
- changes: []
33
+ changes: [],
34
+ faults: []
34
35
  };
35
36
  const working = new Map(input.contentDoc.instances.map((inst) => [inst.instance_id, { ...inst }]));
36
37
  const cloned = /* @__PURE__ */ new Set();
@@ -167,34 +168,56 @@ function applyComputeReads(input) {
167
168
  const fresh = resolved === null ? isList ? [] : null : resolved.value ?? (isList ? [] : null);
168
169
  return { value: mergeLinkedValue(storedRefValue(inst, decl.key), fresh) };
169
170
  };
171
+ const faults = [];
172
+ const cyclic = /* @__PURE__ */ new Set();
173
+ const fault = (decl, kind) => {
174
+ faults.push({
175
+ instance_id: decl.instanceId,
176
+ path: decl.key,
177
+ kind
178
+ });
179
+ };
170
180
  const write = (decl) => {
171
181
  const inst = working.get(decl.instanceId);
172
182
  if (inst === void 0) return false;
173
- if (decl.kind === "display") return writeDisplay(inst, decl, facts, (ref) => readStored(targetOf(decl, ref), ref.slot), rowReaderOf(decl));
183
+ const onCycle = cyclic.has(nodeId(decl.instanceId, decl.key));
184
+ if (decl.kind === "display") {
185
+ const readSlot = (ref) => readStored(targetOf(decl, ref), ref.slot);
186
+ return writeDisplay(inst, decl, onCycle ? null : {
187
+ facts,
188
+ readSlot,
189
+ readRow: rowReaderOf(decl)
190
+ }, fault);
191
+ }
174
192
  if (decl.kind !== "mirror" && isSlotOverridden(inst.slot_overrides, decl.key)) return false;
175
- const settled = valueOf(inst, decl);
193
+ if (onCycle) fault(decl, "compute_cycle");
194
+ const settled = onCycle ? { value: decl.kind !== "reader" && decl.def.type === "list" ? [] : null } : valueOf(inst, decl);
176
195
  if (settled === void 0 || sameValue(storedRefValue(inst, decl.key), settled.value)) return false;
177
196
  writeSlot(inst, decl.key, settled.value);
178
197
  return true;
179
198
  };
180
199
  const changes = [];
181
200
  const settled = /* @__PURE__ */ new Set();
182
- const onWalk = /* @__PURE__ */ new Set();
201
+ const walk = [];
202
+ const reach = (upstream) => {
203
+ if (upstream === void 0) return;
204
+ const at = walk.indexOf(nodeId(upstream.instanceId, upstream.key));
205
+ if (at === -1) settle(upstream);
206
+ else for (const node of walk.slice(at)) cyclic.add(node);
207
+ };
183
208
  const settle = (decl) => {
184
209
  const id = nodeId(decl.instanceId, decl.key);
185
- if (settled.has(id) || onWalk.has(id)) return;
186
- onWalk.add(id);
210
+ if (settled.has(id) || walk.includes(id)) return;
211
+ walk.push(id);
187
212
  for (const ref of decl.slotRefs) {
188
213
  const target = targetOf(decl, ref);
189
- const upstream = target === void 0 ? void 0 : declarations.get(nodeId(target, ref.slot));
190
- if (upstream !== void 0) settle(upstream);
214
+ reach(target === void 0 ? void 0 : declarations.get(nodeId(target, ref.slot)));
191
215
  }
192
216
  if (decl.kind === "derived" || decl.kind === "display") for (const slot of memberReadSlots(decl.source)) {
193
217
  const path = holderPath(decl, slot);
194
- const holder = path === void 0 ? void 0 : declarations.get(nodeId(decl.instanceId, path));
195
- if (holder !== void 0) settle(holder);
218
+ reach(path === void 0 ? void 0 : declarations.get(nodeId(decl.instanceId, path)));
196
219
  }
197
- onWalk.delete(id);
220
+ walk.pop();
198
221
  settled.add(id);
199
222
  if (write(decl)) changes.push({
200
223
  instance_id: decl.instanceId,
@@ -205,7 +228,8 @@ function applyComputeReads(input) {
205
228
  if (changes.length === 0) return {
206
229
  contentDoc: input.contentDoc,
207
230
  changed: false,
208
- changes
231
+ changes,
232
+ faults
209
233
  };
210
234
  const instances = input.contentDoc.instances.map((inst) => working.get(inst.instance_id) ?? inst);
211
235
  return {
@@ -214,24 +238,26 @@ function applyComputeReads(input) {
214
238
  instances
215
239
  },
216
240
  changed: true,
217
- changes
241
+ changes,
242
+ faults
218
243
  };
219
244
  }
220
- function writeDisplay(inst, decl, facts, readSlot, readRow) {
245
+ function writeDisplay(inst, decl, reads, fault) {
221
246
  if (isDisplayOverridden(inst.display_overrides, decl.displayEntry)) return false;
222
- const resolved = resolveDerivedHandle(decl.transform, decl.source, facts, decl.section.bakeContext, { display_entry: decl.displayEntry }, readSlot, readRow);
223
- if (resolved === null) return false;
247
+ const resolved = reads === null ? null : resolveDerivedHandle(decl.transform, decl.source, reads.facts, decl.section.bakeContext, { display_entry: decl.displayEntry }, reads.readSlot, reads.readRow);
224
248
  const def = declaredDisplay(decl.section.schema)[decl.displayEntry];
225
- const answer = def === void 0 ? null : inDeclaredCases(def, decl.displayEntry, resolved.value, decl.section.bakeContext);
226
- if (answer === null) return false;
249
+ const answer = resolved === null || def === void 0 ? null : inDeclaredCases(def, decl.displayEntry, resolved.value, decl.section.bakeContext);
250
+ if (answer === null) fault(decl, reads === null ? "compute_cycle" : "display_unresolved");
251
+ const decided = answer ?? def?.cases?.[0];
252
+ if (decided === void 0) return false;
227
253
  if (decl.displayEntry === "visibility") {
228
- const enabled = answer !== "hidden";
254
+ const enabled = decided !== "hidden";
229
255
  if (inst.enabled === enabled) return false;
230
256
  inst.enabled = enabled;
231
257
  return true;
232
258
  }
233
- if (inst.variant === answer) return false;
234
- inst.variant = answer;
259
+ if (inst.variant === decided) return false;
260
+ inst.variant = decided;
235
261
  return true;
236
262
  }
237
263
  function collectDeclarations(sections) {
@@ -8,6 +8,10 @@ import { mergeSourcedItems } from "./item-merge.js";
8
8
  import { applyComputeReads, storedRefValue } from "./compute-reads.js";
9
9
  import "./held-rows.js";
10
10
  import "./item-overrides.js";
11
+ import "./resolve-instance.js";
12
+ import "./resolve.js";
13
+ import "./strip.js";
14
+ import "./round-trip.js";
11
15
  import "./stored-row-reader.js";
12
16
 
13
- export { applyComputeReads, bakeDisplayEntries, bakePropertyRefSlots };
17
+ export { applyComputeReads, bakeDisplayEntries, bakePropertyRefSlots, mergeSourcedItems };
@@ -60,10 +60,11 @@ function mergeSourcedItems(input) {
60
60
  const id = fresh[idKey];
61
61
  if (typeof id === "string") freshById.set(id, fresh);
62
62
  }
63
- return current.map((cur) => {
63
+ return current.flatMap((cur) => {
64
64
  const id = cur[idKey];
65
- const fresh = typeof id === "string" ? freshById.get(id) : void 0;
66
- return fresh === void 0 ? cur : mergeItem(cur, fresh, overrides?.[id], generatedFields, idKey, true);
65
+ if (typeof id !== "string") return [cur];
66
+ const fresh = freshById.get(id);
67
+ return fresh === void 0 ? [] : [mergeItem(cur, fresh, overrides?.[id], generatedFields, idKey, true)];
67
68
  });
68
69
  }
69
70
  function mergeOrderedSourceItems(current, freshSource, input, idKey) {
@@ -0,0 +1,9 @@
1
+ import "../../template-contracts/dist/slot-catalog.js";
2
+ import "../../template-contracts/dist/table-members.js";
3
+ import "../../template-contracts/dist/row-sources.js";
4
+ import "./derive-source.js";
5
+ import "./list-grammar.js";
6
+ import "./bake-property-ref.js";
7
+ import "./linked-merge.js";
8
+ import "./item-merge.js";
9
+ import "./held-rows.js";
@@ -0,0 +1,3 @@
1
+ import "../../template-contracts/dist/ref-locate.js";
2
+ import "./compute-reads.js";
3
+ import "./resolve-instance.js";