@homepages/concrete 12.0.0 → 13.0.1

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 +192 -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 +15 -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,29 @@
1
+ import { probeOperation } from "../jobs/probe.js";
2
+ import { downloadOperation } from "./download.js";
3
+ import { laneWidths } from "../jobs/lanes.js";
4
+ import { createJobRunner } from "../jobs/runner.js";
5
+ import { availableParallelism, totalmem } from "node:os";
6
+
7
+ //#region ../../internal/playground/dist/server/jobs.js
8
+ function processMachine() {
9
+ const cap = process.constrainedMemory();
10
+ const total = totalmem();
11
+ return {
12
+ cores: availableParallelism(),
13
+ memoryBytes: cap > 0 ? Math.min(cap, total) : total
14
+ };
15
+ }
16
+ function playgroundJobRunner(options = {}) {
17
+ const operations = [probeOperation()];
18
+ if (options.download !== void 0) operations.push(downloadOperation(options.download));
19
+ if (options.verification !== void 0) operations.push(options.verification.operation);
20
+ if (options.ladders !== void 0) operations.push(options.ladders.operation);
21
+ return createJobRunner({
22
+ operations,
23
+ widths: laneWidths(processMachine()),
24
+ ...options.timeCap === void 0 ? {} : { timeCap: options.timeCap }
25
+ });
26
+ }
27
+
28
+ //#endregion
29
+ export { playgroundJobRunner, processMachine };
@@ -1,13 +1,6 @@
1
1
  import { createServer } from "node:net";
2
2
 
3
- //#region src/cli/dev/listen.ts
4
- const HMR_PORT_OFFSET = 100;
5
- const MAX_PORT = 65535;
6
- const MAX_DEV_PORT = MAX_PORT - 100;
7
- function hmrPortFor(port) {
8
- if (port > 65435) throw new Error(`port ${String(port)} leaves no room for the editor's module-reload socket at port + ${String(100)} — pick one no higher than ${String(MAX_DEV_PORT)} with --port <n>`);
9
- return port + 100;
10
- }
3
+ //#region ../../internal/playground/dist/server/listen.js
11
4
  function probe(port) {
12
5
  return new Promise((resolveProbe) => {
13
6
  const server = createServer();
@@ -21,9 +14,9 @@ function probe(port) {
21
14
  async function freeDevPort() {
22
15
  for (let attempt = 0; attempt < 20; attempt += 1) {
23
16
  const port = await probe(0);
24
- if (port !== null && port <= 65435 && await probe(hmrPortFor(port)) !== null) return port;
17
+ if (port !== null) return port;
25
18
  }
26
- throw new Error("no free port pair for the dev server: every probed port's module-reload socket was taken");
19
+ throw new Error("no free port for the dev server");
27
20
  }
28
21
  function listenRefusal(err, what) {
29
22
  if (err.code === "EADDRINUSE") return /* @__PURE__ */ new Error(`${what} is already in use — pick another with --port <n>`);
@@ -42,4 +35,4 @@ function listenOn(server, port, what) {
42
35
  }
43
36
 
44
37
  //#endregion
45
- export { MAX_DEV_PORT, freeDevPort, hmrPortFor, listenOn, listenRefusal };
38
+ export { freeDevPort, listenOn, listenRefusal };
@@ -0,0 +1,424 @@
1
+ import { TEMPLATE_FILES, browserFiles, builtTreePath, templatePath } from "../../../template-artifact/dist/layout.js";
2
+ import { carriesFixedAddresses, fillBuildFile } from "../../../static-assets-binding/dist/section-asset-url.js";
3
+ import { playgroundClientDir } from "../layout.js";
4
+ import { parseDownloadPath } from "../host/download.js";
5
+ import { OUTPUT_FOLDERS, ROUTES, parseOutputPath } from "../wire.js";
6
+ import { createPlayground } from "../host/program.js";
7
+ import { CROP_PARAM, parseCropParam } from "../../../crop-region/dist/index.js";
8
+ import { cropFingerprint, croppedMediaUrl, cutRegion, shareImageCutter } from "./crop.js";
9
+ import { RequestError, contentTypeFor, readBody, sendBody, sendBytes, sendHtml, sendText } from "./http.js";
10
+ import { DOWNLOAD_OPERATION, DOWNLOAD_WAIT_MS, answerDownload, downloadFingerprint, downloadOperation, storedBytes } from "./download.js";
11
+ import { changeChannel } from "./changes.js";
12
+ import { playgroundJobRunner, processMachine } from "./jobs.js";
13
+ import { buildFingerprint } from "../build-fingerprint.js";
14
+ import { importsFrom, verifyBuildCode } from "./verify-build.js";
15
+ import { VERIFY_OPERATION, buildVerification, verdictFingerprint } from "./verification.js";
16
+ import { listenOn } from "./listen.js";
17
+ import { isInsideRoot } from "../platform.js";
18
+ import { respondToHandlerError } from "./request-error.js";
19
+ import { LADDER_MOUNT, sizeLadders } from "./size-ladders.js";
20
+ import { DERIVED_FILE_IDLE_MS, derivedFolder } from "./derived-files.js";
21
+ import { existsSync, statSync } from "node:fs";
22
+ import { join, resolve } from "node:path";
23
+ import { pathToFileURL } from "node:url";
24
+ import { readFile } from "node:fs/promises";
25
+ import { createRequire } from "node:module";
26
+ import { createServer } from "node:http";
27
+
28
+ //#region ../../internal/playground/dist/server/playground-server.js
29
+ const SERVED_TEMPLATE = "template";
30
+ const MAX_BODY_BYTES = 1024 * 1024;
31
+ const BUILD_MOUNT = ROUTES.build;
32
+ const OUTPUT_ASSETS = "assets";
33
+ function stampBase(html, base) {
34
+ const tag = `<base href="${base}">`;
35
+ return /<head[^>]*>/i.test(html) ? html.replace(/<head[^>]*>/i, (head) => `${head}\n ${tag}`) : `${tag}${html}`;
36
+ }
37
+ function isShellPath(path) {
38
+ if (path === "/") return true;
39
+ const [head, property, kind, content, view, ...rest] = path.split("/").filter(Boolean);
40
+ if (head !== "properties" || property === void 0) return false;
41
+ if (kind === void 0) return true;
42
+ if (kind === "facts") return content === void 0;
43
+ return kind === "content" && content !== void 0 && (view === "canvas" || view === "live") && rest.length <= 1;
44
+ }
45
+ async function workspaceReact(root) {
46
+ const require = createRequire(join(root, "package.json"));
47
+ const load = async (specifier) => {
48
+ const mod = await import(pathToFileURL(require.resolve(specifier)).href);
49
+ return mod.default ?? mod;
50
+ };
51
+ const [react, server] = await Promise.all([load("react"), load("react-dom/server")]);
52
+ const createElement = react.createElement;
53
+ const renderToString = server.renderToString;
54
+ return {
55
+ createElement: (type, props) => createElement(type, props),
56
+ renderToString: (element, options) => renderToString(element, options)
57
+ };
58
+ }
59
+ async function browserFilePath(ctx, view, filename) {
60
+ const binding = await view.readText(builtTreePath(templatePath(TEMPLATE_FILES.staticAssets)));
61
+ if (binding === null) return null;
62
+ if (ctx.browserFiles?.fingerprint !== binding.fingerprint) ctx.browserFiles = {
63
+ fingerprint: binding.fingerprint,
64
+ files: browserFiles(JSON.parse(binding.text))
65
+ };
66
+ return ctx.browserFiles.files.pathOf(filename);
67
+ }
68
+ function servedUrlUnder(base) {
69
+ return (path, transform) => {
70
+ const url = /^([a-z][a-z0-9+.-]*:|\/)/i.test(path) ? path : `${base}${path}`;
71
+ return transform === void 0 ? url : croppedMediaUrl(url, transform.crop);
72
+ };
73
+ }
74
+ const servedUrl = servedUrlUnder("/");
75
+ function checkBase(base) {
76
+ if (!base.startsWith("/") || !base.endsWith("/")) throw new Error(`A playground's base starts and ends with "/", not "${base}".`);
77
+ return base;
78
+ }
79
+ async function createPlaygroundHandler(options) {
80
+ const { root, name, log, files, media } = options;
81
+ const base = checkBase(options.base ?? "/");
82
+ const clientDir = playgroundClientDir(options.playgroundDir);
83
+ if (!existsSync(join(clientDir, "index.html"))) log.write(`${name}: the playground client is not built (${clientDir}) — run the package's build.\n`);
84
+ const react = await workspaceReact(root);
85
+ const cut = await shareImageCutter();
86
+ const sizes = options.ladders;
87
+ const ladders = sizes?.at(options.fixtureMediaPath, options.jobs);
88
+ const host = createPlayground({
89
+ files,
90
+ fixtures: true,
91
+ async media(view, path) {
92
+ const served = await media.file(view, path);
93
+ return served === null || ladders === void 0 ? served : ladders.served(view, path, served);
94
+ },
95
+ ...sizes === void 0 ? {} : { mediaVersion: () => sizes.version() },
96
+ url: servedUrlUnder(base),
97
+ loadModule: options.loadModule,
98
+ react,
99
+ ...cut === null ? {} : { shareImage: {
100
+ async read(url) {
101
+ const asset = media.asset(url);
102
+ return asset === void 0 ? null : (await (await files.current()).readBytes(asset.path))?.bytes ?? null;
103
+ },
104
+ cut
105
+ } },
106
+ base,
107
+ fixtureMediaPath: options.fixtureMediaPath,
108
+ downloads: options.jobs?.mark(DOWNLOAD_OPERATION) !== void 0,
109
+ ...options.jobs === void 0 ? {} : { jobs: options.jobs }
110
+ });
111
+ const ctx = {
112
+ base,
113
+ changes: options.changes,
114
+ clientDir,
115
+ name,
116
+ log,
117
+ files,
118
+ media,
119
+ fixtureMediaPath: options.fixtureMediaPath,
120
+ ladders,
121
+ derived: options.derived,
122
+ downloadCode: options.downloadCode,
123
+ cutting: /* @__PURE__ */ new Map(),
124
+ host,
125
+ jobs: options.jobs,
126
+ verification: options.verification,
127
+ bodyLimit: options.bodyLimit ?? (() => 1048576),
128
+ browserFiles: null
129
+ };
130
+ return {
131
+ base,
132
+ downloads: templateDownloads(ctx),
133
+ handle: (req, res) => handle(req, res, ctx).catch((err) => {
134
+ if (err instanceof RequestError && !res.headersSent) {
135
+ sendText(res, err.status, err.message);
136
+ return;
137
+ }
138
+ respondToHandlerError(err, req, res);
139
+ })
140
+ };
141
+ }
142
+ async function startPlaygroundServer(options) {
143
+ const { port, browser, jobs, platform, cutsLadders, ...handled } = options;
144
+ if (cutsLadders === true && jobs !== void 0) throw new Error("A server cuts size ladders on a runner of its own; hand the runner's ladders to createPlaygroundHandler instead.");
145
+ const ladders = cutsLadders === true ? sizeLadders({
146
+ derived: handled.derived,
147
+ view: () => handled.files.current()
148
+ }) : void 0;
149
+ const origin = `http://localhost:${String(port)}/`;
150
+ const verification = handled.verification ?? (platform === void 0 ? void 0 : buildVerification({
151
+ platform,
152
+ code: {
153
+ react: await workspaceReact(handled.root),
154
+ importExternal: importsFrom(handled.root)
155
+ },
156
+ derived: handled.derived,
157
+ view: () => handled.files.current()
158
+ }));
159
+ let handler;
160
+ const runner = jobs ?? playgroundJobRunner({
161
+ ...browser === void 0 ? {} : { download: {
162
+ browser,
163
+ derived: handled.derived,
164
+ served: () => Promise.resolve(handler === void 0 ? null : handler.downloads)
165
+ } },
166
+ verification,
167
+ ladders
168
+ });
169
+ const channel = changeChannel();
170
+ const unfollow = channel.follow(runner);
171
+ const changes = channel.scope(SERVED_TEMPLATE);
172
+ const stop = async () => {
173
+ unfollow();
174
+ channel.close();
175
+ if (jobs === void 0) await runner.close();
176
+ await browser?.close();
177
+ };
178
+ let created;
179
+ try {
180
+ created = handler = await createPlaygroundHandler({
181
+ ...handled,
182
+ jobs: runner.scope(SERVED_TEMPLATE),
183
+ changes,
184
+ ...verification === void 0 ? {} : { verification },
185
+ ...ladders === void 0 ? {} : { ladders }
186
+ });
187
+ } catch (err) {
188
+ await stop();
189
+ throw err;
190
+ }
191
+ const server = createServer((req, res) => void created.handle(req, res));
192
+ try {
193
+ await listenOn(server, port, `port ${String(port)}`);
194
+ } catch (err) {
195
+ await stop();
196
+ throw err;
197
+ }
198
+ return {
199
+ url: origin,
200
+ port,
201
+ changes,
202
+ close: async () => {
203
+ unfollow();
204
+ channel.close();
205
+ if (jobs === void 0) await runner.close();
206
+ await new Promise((done) => server.close(() => done()));
207
+ await browser?.close();
208
+ }
209
+ };
210
+ }
211
+ async function toFetchRequest(req, url, maxBytes) {
212
+ const method = req.method ?? "GET";
213
+ const headers = new Headers();
214
+ for (const [name, value] of Object.entries(req.headers)) if (typeof value === "string") headers.set(name, value);
215
+ else if (Array.isArray(value)) for (const item of value) headers.append(name, item);
216
+ const body = method === "GET" || method === "HEAD" ? void 0 : await readBody(req, maxBytes);
217
+ return new Request(url, {
218
+ method,
219
+ headers,
220
+ ...body === void 0 ? {} : { body: new Uint8Array(body) }
221
+ });
222
+ }
223
+ async function sendFetchResponse(res, response) {
224
+ res.statusCode = response.status;
225
+ response.headers.forEach((value, name) => res.setHeader(name, value));
226
+ res.end(Buffer.from(await response.arrayBuffer()));
227
+ }
228
+ async function handle(req, res, ctx) {
229
+ const url = new URL(req.url ?? "/", "http://localhost");
230
+ if (!url.pathname.startsWith(ctx.base)) {
231
+ if (`${url.pathname}/` === ctx.base) {
232
+ res.statusCode = 302;
233
+ res.setHeader("location", `${ctx.base}${url.search}`);
234
+ res.end();
235
+ } else sendText(res, 404, "not found");
236
+ return;
237
+ }
238
+ const path = `/${url.pathname.slice(ctx.base.length)}`;
239
+ if (isShellPath(path)) return sendClientShell(ctx, res);
240
+ if (path === `/${ROUTES.changes}`) return ctx.changes.attach(res);
241
+ if (await sendTemplateFile(ctx, req, url, res)) return;
242
+ const output = parseOutputPath(path.slice(1));
243
+ if (output?.rest[0] === OUTPUT_ASSETS && output.output !== OUTPUT_FOLDERS.site) return sendContentFile(ctx, req, url, res);
244
+ if (output?.output === OUTPUT_FOLDERS.download && ctx.jobs?.mark("download") !== void 0) {
245
+ const named = parseDownloadPath(output.rest);
246
+ if (named === null) return sendText(res, 404, `There is no download at ${url.pathname}.`);
247
+ const ask = {
248
+ property: output.property,
249
+ fixture: output.fixture,
250
+ format: url.searchParams.get("format"),
251
+ page: named.page
252
+ };
253
+ return answerDownload({
254
+ downloads: templateDownloads(ctx),
255
+ jobs: ctx.jobs,
256
+ derived: ctx.derived,
257
+ version: async () => (await ctx.files.current()).version
258
+ }, req.method ?? "GET", url, ask, res);
259
+ }
260
+ if (path === `/${ROUTES.verify}`) return sendVerdict(ctx, req, res);
261
+ const answered = await ctx.host.handle(await toFetchRequest(req, url, await ctx.bodyLimit(req)));
262
+ if (answered !== null) return sendFetchResponse(res, answered);
263
+ if (output?.rest[0] === OUTPUT_ASSETS) return sendContentFile(ctx, req, url, res);
264
+ if (await sendClientAsset(ctx, path, res)) return;
265
+ sendText(res, 404, "not found");
266
+ }
267
+ async function sendTemplateFile(ctx, req, url, res, buildBase = `${ctx.base}${BUILD_MOUNT}`) {
268
+ const path = `/${url.pathname.slice(ctx.base.length)}`;
269
+ if (url.pathname.startsWith(ctx.base) && path.startsWith(`/${BUILD_MOUNT}`)) {
270
+ await sendBuiltFile(ctx, decodeURIComponent(path.slice(BUILD_MOUNT.length + 1)), res, buildBase);
271
+ return true;
272
+ }
273
+ if (url.pathname.startsWith(`${ctx.fixtureMediaPath}/`)) {
274
+ await sendFixtureMedia(ctx, req, url, res);
275
+ return true;
276
+ }
277
+ return false;
278
+ }
279
+ async function sendContentFile(ctx, req, url, res) {
280
+ if (req.method !== "GET" && req.method !== "HEAD") throw new RequestError(405, `${url.pathname} only reads.`);
281
+ const file = await ctx.host.contentFile(url.pathname);
282
+ const buildBase = `${ctx.base}${url.pathname.slice(ctx.base.length).split("/").slice(0, 6).join("/")}/`;
283
+ if (file === null || !await sendTemplateFile(ctx, req, new URL(file.url, url), res, buildBase)) sendText(res, 404, "not found");
284
+ }
285
+ async function sendVerdict(ctx, req, res) {
286
+ if (req.method !== "POST") throw new RequestError(405, "POST to ask for the build's verdict.");
287
+ if (ctx.verification === void 0 || ctx.jobs === void 0) throw new RequestError(501, "This playground verifies no builds.");
288
+ const answer = await ctx.verification.ask(ctx.jobs, await ctx.files.current());
289
+ res.statusCode = "verdict" in answer ? 200 : 202;
290
+ res.setHeader("content-type", "application/json; charset=utf-8");
291
+ res.end(JSON.stringify(answer));
292
+ }
293
+ async function sendClientShell(ctx, res) {
294
+ let html;
295
+ try {
296
+ html = await readFile(join(ctx.clientDir, "index.html"), "utf8");
297
+ } catch {
298
+ sendText(res, 500, "the playground client is not built — run the package's build");
299
+ return;
300
+ }
301
+ sendHtml(res, stampBase(html, ctx.base));
302
+ }
303
+ async function sendClientAsset(ctx, path, res) {
304
+ const file = resolve(ctx.clientDir, `.${decodeURIComponent(path)}`);
305
+ if (!isInsideRoot(ctx.clientDir, file)) return false;
306
+ try {
307
+ if (!statSync(file).isFile()) return false;
308
+ } catch {
309
+ return false;
310
+ }
311
+ sendBytes(res, contentTypeFor(file), await readFile(file));
312
+ return true;
313
+ }
314
+ async function sendBuiltFile(ctx, filename, res, buildBase = `${ctx.base}${BUILD_MOUNT}`) {
315
+ const view = await ctx.files.current();
316
+ const at = await browserFilePath(ctx, view, filename).catch(() => null);
317
+ const read = at === null ? null : await view.readBytes(builtTreePath(at)).catch(() => null);
318
+ if (at === null || read === null) {
319
+ sendText(res, 404, "not found");
320
+ return;
321
+ }
322
+ res.setHeader("cache-control", "no-cache");
323
+ const bytes = carriesFixedAddresses(filename) ? new TextEncoder().encode(fillBuildFile(filename, new TextDecoder().decode(read.bytes), buildBase)) : read.bytes;
324
+ sendBytes(res, contentTypeFor(at), bytes);
325
+ }
326
+ async function sendFixtureMedia(ctx, req, url, res) {
327
+ if (req.method !== "GET" && req.method !== "HEAD") {
328
+ sendText(res, 405, "fixture media only reads; an upload saves through the playground.");
329
+ return;
330
+ }
331
+ const answer = await fixtureMediaAnswer(ctx, url);
332
+ if (answer.kind === "file") sendBody(req, res, answer.contentType, answer.bytes);
333
+ else {
334
+ if (answer.kind === "crop") res.setHeader("cache-control", "no-cache");
335
+ sendBytes(res, answer.contentType, answer.bytes);
336
+ }
337
+ }
338
+ async function fixtureMediaAnswer(ctx, url) {
339
+ const path = decodeURIComponent(url.pathname);
340
+ const asset = ctx.media.asset(path);
341
+ const cropValue = url.searchParams.get(CROP_PARAM);
342
+ if (asset === void 0) {
343
+ const rung = await ctx.ladders?.rung(path) ?? null;
344
+ if (rung === null) throw new RequestError(404, "not found");
345
+ if (cropValue !== null) throw new RequestError(400, `${url.pathname} is one rung of an image's ladder; crop the image itself.`);
346
+ return {
347
+ kind: "rung",
348
+ contentType: rung.contentType,
349
+ bytes: rung.bytes
350
+ };
351
+ }
352
+ const read = await (await ctx.files.current()).readBytes(asset.path);
353
+ if (read === null) throw new RequestError(404, "not found");
354
+ if (cropValue === null) return {
355
+ kind: "file",
356
+ contentType: contentTypeFor(asset.path),
357
+ bytes: read.bytes
358
+ };
359
+ const rect = parseCropParam(cropValue);
360
+ if (rect === null) throw new RequestError(400, `"${cropValue}" is not a crop of the image.`);
361
+ if (!asset.derived) throw new RequestError(400, `${url.pathname} is not a raster image and cannot be cropped.`);
362
+ const fingerprint = cropFingerprint(read.fingerprint, rect);
363
+ let cut = ctx.cutting.get(fingerprint);
364
+ if (cut === void 0) {
365
+ cut = keptOrCut(ctx, fingerprint, () => cutRegion(read.bytes, rect)).finally(() => ctx.cutting.delete(fingerprint));
366
+ ctx.cutting.set(fingerprint, cut);
367
+ }
368
+ const image = await cut;
369
+ if (image === null) {
370
+ ctx.log.write(`${ctx.name}: sharp did not load, so a crop of ${url.pathname} serves the whole image.\n`);
371
+ return {
372
+ kind: "file",
373
+ contentType: contentTypeFor(asset.path),
374
+ bytes: read.bytes
375
+ };
376
+ }
377
+ return {
378
+ kind: "crop",
379
+ contentType: image.contentType,
380
+ bytes: image.bytes
381
+ };
382
+ }
383
+ async function fixtureMediaFingerprint(ctx, url) {
384
+ const path = decodeURIComponent(url.pathname);
385
+ const asset = ctx.media.asset(path);
386
+ if (asset === void 0) return (await ctx.ladders?.rung(path) ?? null) === null ? null : `rung:${path}`;
387
+ const [entry] = await (await ctx.files.current()).list(asset.path);
388
+ if (entry === void 0) return null;
389
+ const rect = parseCropParam(url.searchParams.get("crop") ?? "");
390
+ return rect === null ? entry.fingerprint : cropFingerprint(entry.fingerprint, rect);
391
+ }
392
+ function templateDownloads(ctx) {
393
+ const mediaUrl = (key) => new URL(key, "http://localhost");
394
+ return {
395
+ input: (ask) => ctx.host.downloadInput(ask),
396
+ async fingerprint(input) {
397
+ return downloadFingerprint(input, (await Promise.all(input.media.map((file) => fixtureMediaFingerprint(ctx, mediaUrl(file.url))))).map((fingerprint, index) => fingerprint ?? `url:${input.media[index].url}`), ctx.downloadCode);
398
+ },
399
+ reads: {
400
+ async buildFile(path) {
401
+ const read = await (await ctx.files.current()).readBytes(builtTreePath(path));
402
+ return read === null ? null : storedBytes(read.bytes);
403
+ },
404
+ async mediaFile(key) {
405
+ const url = mediaUrl(key);
406
+ if (!url.pathname.startsWith(`${ctx.fixtureMediaPath}/`)) throw new Error(`${key} is not a fixture media file this playground serves.`);
407
+ const answer = await fixtureMediaAnswer(ctx, url);
408
+ return storedBytes(answer.bytes, answer.contentType);
409
+ }
410
+ }
411
+ };
412
+ }
413
+ async function keptOrCut(ctx, fingerprint, make) {
414
+ const kept = await ctx.derived.kept(fingerprint);
415
+ if (kept !== null) return kept;
416
+ const made = await make();
417
+ if (made !== null) await ctx.derived.keep(fingerprint, made).catch((err) => {
418
+ ctx.log.write(`${ctx.name}: a derived file could not be kept (${err instanceof Error ? err.message : String(err)}); it is made again when next asked for.\n`);
419
+ });
420
+ return made;
421
+ }
422
+
423
+ //#endregion
424
+ export { MAX_BODY_BYTES, createPlaygroundHandler, derivedFolder, isShellPath, servedUrlUnder, stampBase, startPlaygroundServer, workspaceReact };
@@ -1,4 +1,4 @@
1
- //#region src/cli/dev/request-error.ts
1
+ //#region ../../internal/playground/dist/server/request-error.js
2
2
  function respondToHandlerError(err, req, res, fixStacktrace) {
3
3
  const error = err instanceof Error ? err : new Error(String(err));
4
4
  fixStacktrace?.(error);
@@ -0,0 +1,170 @@
1
+ import { LADDER_OPERATION } from "../wire.js";
2
+ import { fingerprintOf } from "../../../file-store/dist/files.js";
3
+ import { JobCancelled } from "../jobs/runner.js";
4
+
5
+ //#region ../../internal/playground/dist/server/size-ladders.js
6
+ const LADDER_MOUNT = "ladders";
7
+ const LADDER_ENTRY = "size-ladder/1";
8
+ const LADDER_TYPE = "application/json";
9
+ const appCut = {
10
+ policy: async () => (await import("../../../image-ladder/dist/policy.js")).LADDER_POLICY_FINGERPRINT,
11
+ async cut(bytes) {
12
+ let sharp;
13
+ try {
14
+ sharp = (await import("sharp")).default;
15
+ } catch {
16
+ return null;
17
+ }
18
+ const { encodeRungs, rungsFor } = await import("../../../image-ladder/dist/index.js");
19
+ const source = Buffer.from(bytes);
20
+ const meta = await sharp(source).metadata();
21
+ const longEdge = Math.max(meta.width ?? 0, meta.height ?? 0);
22
+ if (longEdge === 0) throw new Error("sharp read no size off the photo.");
23
+ return (await encodeRungs(source, rungsFor(longEdge))).map((rung) => ({
24
+ edge: rung.edge,
25
+ width: rung.width,
26
+ height: rung.height,
27
+ contentType: rung.contentType,
28
+ ext: rung.ext,
29
+ bytes: new Uint8Array(rung.bytes)
30
+ }));
31
+ }
32
+ };
33
+ const nameOf = (path) => path.slice(path.lastIndexOf("/") + 1);
34
+ function sizeLadders(host) {
35
+ const { derived } = host;
36
+ const cutter = host.cutter ?? appCut;
37
+ const known = /* @__PURE__ */ new Map();
38
+ const failed = /* @__PURE__ */ new Set();
39
+ let landings = 0;
40
+ let policy = null;
41
+ async function ladderFingerprint(fingerprint) {
42
+ policy ??= cutter.policy();
43
+ return fingerprintOf(new TextEncoder().encode(JSON.stringify([
44
+ LADDER_ENTRY,
45
+ await policy,
46
+ fingerprint
47
+ ])));
48
+ }
49
+ async function keptLadder(fingerprint) {
50
+ const held = known.get(fingerprint);
51
+ if (held !== void 0) return held;
52
+ const file = await derived.kept(fingerprint).catch(() => null);
53
+ if (file === null) return null;
54
+ try {
55
+ const ladder = JSON.parse(new TextDecoder().decode(file.bytes));
56
+ if (ladder.entry !== LADDER_ENTRY || !Array.isArray(ladder.rungs) || ladder.rungs.length === 0) return null;
57
+ known.set(fingerprint, ladder);
58
+ return ladder;
59
+ } catch {
60
+ return null;
61
+ }
62
+ }
63
+ return {
64
+ operation: {
65
+ name: LADDER_OPERATION,
66
+ takes: "image-cutter",
67
+ mark: "Cutting image sizes",
68
+ joins: true,
69
+ async run({ job, report }) {
70
+ const path = job.subject.file;
71
+ if (path === void 0) throw new Error("A size ladder's job names no photo.");
72
+ const view = await host.view(job.template, job.version);
73
+ if (view === null) throw new Error(`The template has no version ${job.version}.`);
74
+ const read = await view.readBytes(path);
75
+ if (read === null) throw new Error(`Version ${job.version} has no photo at ${path}.`);
76
+ const fingerprint = await ladderFingerprint(read.fingerprint);
77
+ if (await keptLadder(fingerprint) !== null) return { file: fingerprint };
78
+ try {
79
+ await report({ progress: `Cutting ${nameOf(path)}` });
80
+ const rungs = await cutter.cut(read.bytes);
81
+ if (rungs === null) throw new Error("sharp did not load here, so photos show whole, without their sizes.");
82
+ if (rungs.length === 0) throw new Error(`The cut made no size of ${nameOf(path)}.`);
83
+ const kept = [];
84
+ for (const rung of rungs) {
85
+ const file = await fingerprintOf(rung.bytes);
86
+ await derived.keep(file, {
87
+ contentType: rung.contentType,
88
+ bytes: rung.bytes
89
+ });
90
+ kept.push({
91
+ name: `${String(rung.edge)}.${rung.ext}`,
92
+ width: rung.width,
93
+ height: rung.height,
94
+ file
95
+ });
96
+ }
97
+ await report({ progress: `Cut ${String(kept.length)} ${kept.length === 1 ? "size" : "sizes"}` });
98
+ const ladder = {
99
+ entry: LADDER_ENTRY,
100
+ contentType: rungs[0].contentType,
101
+ rungs: kept.sort((a, b) => a.width - b.width)
102
+ };
103
+ await derived.keep(fingerprint, {
104
+ contentType: LADDER_TYPE,
105
+ bytes: new TextEncoder().encode(`${JSON.stringify(ladder)}\n`)
106
+ });
107
+ known.set(fingerprint, ladder);
108
+ landings += 1;
109
+ return { file: fingerprint };
110
+ } catch (err) {
111
+ if (!(err instanceof JobCancelled)) failed.add(fingerprint);
112
+ throw err;
113
+ }
114
+ }
115
+ },
116
+ version: () => landings,
117
+ at(fixtureMediaPath, jobs) {
118
+ const prefix = `${fixtureMediaPath}/${LADDER_MOUNT}/`;
119
+ const answered = /* @__PURE__ */ new Set();
120
+ const responsiveOf = (fingerprint, ladder) => {
121
+ const top = ladder.rungs[ladder.rungs.length - 1];
122
+ return {
123
+ sources: [{
124
+ type: ladder.contentType,
125
+ srcset: ladder.rungs.map((rung) => `${prefix}${fingerprint}/${rung.name} ${String(rung.width)}w`).join(", ")
126
+ }],
127
+ width: top.width,
128
+ height: top.height
129
+ };
130
+ };
131
+ return {
132
+ async served(view, path, served) {
133
+ const { file } = served;
134
+ if (typeof file === "string" || file.responsive !== null) return served;
135
+ const [entry] = await view.list(path);
136
+ if (entry?.path !== path) return served;
137
+ const fingerprint = await ladderFingerprint(entry.fingerprint);
138
+ const ladder = await keptLadder(fingerprint);
139
+ if (ladder !== null) {
140
+ answered.add(fingerprint);
141
+ return {
142
+ ...served,
143
+ file: {
144
+ ...file,
145
+ responsive: responsiveOf(fingerprint, ladder)
146
+ }
147
+ };
148
+ }
149
+ if (jobs !== void 0 && !failed.has(fingerprint)) await jobs.start({
150
+ operation: LADDER_OPERATION,
151
+ subject: { file: path },
152
+ version: view.version,
153
+ inputs: fingerprint
154
+ }).catch(() => void 0);
155
+ return served;
156
+ },
157
+ async rung(url) {
158
+ if (!url.startsWith(prefix)) return null;
159
+ const [fingerprint, name, ...rest] = url.slice(prefix.length).split("/");
160
+ if (fingerprint === void 0 || name === void 0 || rest.length > 0 || !answered.has(fingerprint)) return null;
161
+ const rung = (await keptLadder(fingerprint))?.rungs.find((r) => r.name === name);
162
+ return rung === void 0 ? null : derived.kept(rung.file);
163
+ }
164
+ };
165
+ }
166
+ };
167
+ }
168
+
169
+ //#endregion
170
+ export { LADDER_MOUNT, sizeLadders };