@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.
- package/CHANGELOG.md +184 -0
- package/README.md +8 -10
- package/codemods/fixtures-are-data.mjs +7 -0
- package/codemods/fixtures-list-properties.mjs +6 -0
- package/codemods/manifest.json +11 -0
- package/codemods/steps/fixtures-list-properties.mjs +16 -0
- package/codemods/steps/fixtures-to-data.mjs +41 -0
- package/dist/artifact/playground/client/assets/archivo-latin-ext-wght-normal-C4zznr8T.woff2 +0 -0
- package/dist/artifact/playground/client/assets/archivo-latin-wght-normal-E0tuGl4L.woff2 +0 -0
- package/dist/artifact/playground/client/assets/archivo-vietnamese-wght-normal-XAtsl5Q_.woff2 +0 -0
- package/dist/artifact/playground/client/assets/index-Bmvcss4B.js +1338 -0
- package/dist/artifact/playground/client/assets/index-C01UCAWD.css +1 -0
- package/dist/artifact/playground/client/index.html +2 -2
- package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
- package/dist/artifact/playground/host.mjs +110 -59
- package/dist/base.css +2 -2
- package/dist/cli/build/icons.js +1 -1
- package/dist/cli/build/index.js +11 -10
- package/dist/cli/build/manifest.js +44 -0
- package/dist/cli/build/record.js +123 -0
- package/dist/cli/build/sections.js +28 -18
- package/dist/cli/build/source.js +1 -1
- package/dist/cli/build/static.js +8 -5
- package/dist/cli/build/template-build.js +614 -0
- package/dist/cli/check/analyze.js +18 -1
- package/dist/cli/check/load-workspace.js +2 -2
- package/dist/cli/check/portable-names.js +1 -1
- package/dist/cli/check/render/corpus.js +119 -0
- package/dist/cli/check/render/invariants.js +1 -0
- package/dist/cli/check/stages/bundles.js +15 -18
- package/dist/cli/check/stages/composition.js +8 -25
- package/dist/cli/check/stages/fixtures.js +14 -10
- package/dist/cli/check/stages/logic.js +7 -23
- package/dist/cli/check/stages/render.js +12 -14
- package/dist/cli/check/stages/sections.js +22 -66
- package/dist/cli/check/stages/stylesheet.js +4 -26
- package/dist/cli/check/stages/workspace.js +27 -22
- package/dist/cli/commands.js +51 -23
- package/dist/cli/currency/preamble.js +1 -1
- package/dist/cli/currency/resolve-latest.js +3 -2
- package/dist/cli/dev/capture-browser.js +26 -0
- package/dist/cli/dev/fixture-schema.js +441 -0
- package/dist/cli/dev/folder-files.js +244 -0
- package/dist/cli/dev/index.js +7 -3
- package/dist/cli/dev/playground-server.js +34 -0
- package/dist/cli/dev/section-ladders.js +76 -0
- package/dist/cli/dev/server.js +120 -304
- package/dist/cli/install/background.js +16 -0
- package/dist/cli/install/index.js +45 -0
- package/dist/cli/install/tools.js +27 -0
- package/dist/cli/media/derive.js +19 -13
- package/dist/cli/media/deriver-plugin.js +14 -3
- package/dist/cli/property/fixture-list.js +46 -0
- package/dist/cli/property/index.js +12 -9
- package/dist/cli/property/library.js +12 -10
- package/dist/cli/publish/artifact.js +90 -0
- package/dist/cli/publish/captures.js +61 -0
- package/dist/cli/publish/fixtures.js +17 -0
- package/dist/cli/publish/index.js +10 -18
- package/dist/cli/{build → publish}/playground.js +3 -3
- package/dist/cli/publish/report.js +15 -33
- package/dist/cli/upgrade/codemod.d.ts +14 -0
- package/dist/cli/upgrade/fixture-conversion.d.ts +11 -0
- package/dist/cli/upgrade/fixture-conversion.js +54 -0
- package/dist/cli/upgrade/fixture-modules.js +125 -0
- package/dist/cli/upgrade/fixtures-to-data.d.ts +40 -0
- package/dist/cli/upgrade/fixtures-to-data.js +498 -0
- package/dist/cli/upgrade/index.js +18 -2
- package/dist/cli/upgrade/legacy-fixtures.d.ts +34 -0
- package/dist/cli/upgrade/legacy-fixtures.js +22 -0
- package/dist/cli/upgrade/stage.js +1 -1
- package/dist/cli/workspace-fixtures.js +80 -0
- package/dist/cli.js +1 -0
- package/dist/css/compile.js +9 -3
- package/dist/fixtures/bundle-rule.js +16 -16
- package/dist/fixtures/media-plugin.js +12 -33
- package/dist/fixtures/synthesize-empties.js +3 -2
- package/dist/index.d.ts +7 -7
- package/dist/{playground-island-runtime.d.ts → island-runtime.d.ts} +1 -2
- package/dist/{playground-island-runtime.js → island-runtime.js} +1 -2
- package/dist/islands/client-module.js +1 -1
- package/dist/{cli/dev → islands}/tsconfig-paths.js +2 -14
- package/dist/islands/wrap.js +40 -0
- package/dist/node_modules/.pnpm/acorn@8.18.0/node_modules/acorn/dist/acorn.js +5119 -0
- package/dist/packages/external/concrete/package.js +1 -1
- package/dist/packages/internal/bundle-evaluation/dist/cache.js +36 -0
- package/dist/packages/internal/bundle-evaluation/dist/evaluate.js +174 -0
- package/dist/packages/internal/bundle-evaluation/dist/index.js +5 -0
- package/dist/packages/internal/bundle-evaluation/dist/section-code.js +10 -0
- package/dist/packages/internal/capture-settle/dist/index.js +2 -2
- package/dist/packages/internal/capture-settle/dist/settle.js +88 -13
- package/dist/packages/internal/code-fingerprint/dist/index.js +57 -0
- package/dist/packages/internal/content-doc-ops/dist/index.js +176 -0
- package/dist/packages/internal/content-doc-ops/dist/made-rows.js +15 -0
- package/dist/packages/internal/deliverable-reads/dist/bootstrap.js +81 -0
- package/dist/packages/internal/deliverable-reads/dist/display.js +21 -0
- package/dist/packages/internal/deliverable-reads/dist/format.js +15 -0
- package/dist/packages/internal/deliverable-reads/dist/index.js +7 -0
- package/dist/packages/internal/deliverable-reads/dist/pages.js +29 -0
- package/dist/packages/internal/deliverable-reads/dist/requirements.js +72 -0
- package/dist/{cli/capture → packages/internal/download-files/dist}/bundle.js +8 -4
- package/dist/packages/internal/download-files/dist/capture-origin.js +126 -0
- package/dist/packages/internal/download-files/dist/capture-routes.js +139 -0
- package/dist/packages/internal/download-files/dist/capture-shell.js +38 -0
- package/dist/packages/internal/download-files/dist/capture.js +74 -0
- package/dist/packages/internal/download-files/dist/egress-watch.js +38 -0
- package/dist/packages/internal/download-files/dist/files.js +7 -0
- package/dist/packages/internal/download-files/dist/finish.js +35 -0
- package/dist/packages/internal/download-files/dist/fleet.js +94 -0
- package/dist/packages/internal/download-files/dist/format.js +40 -0
- package/dist/packages/internal/download-files/dist/make.js +105 -0
- package/dist/packages/internal/download-files/dist/master-tally.js +32 -0
- package/dist/packages/internal/download-files/dist/names.js +24 -0
- package/dist/packages/internal/download-files/dist/pdf-boxes.js +33 -0
- package/dist/packages/internal/download-files/dist/pdf-image-placements.js +194 -0
- package/dist/packages/internal/download-files/dist/pdf-images.js +289 -0
- package/dist/packages/internal/file-store/dist/files.js +8 -0
- package/dist/packages/internal/file-store/dist/lru.js +40 -0
- package/dist/packages/internal/file-store/dist/refusal.js +12 -0
- package/dist/packages/internal/file-store/dist/trees.js +151 -0
- package/dist/packages/internal/fill-bake/dist/bake-property-ref.js +1 -1
- package/dist/packages/internal/fill-bake/dist/compute-reads.js +47 -21
- package/dist/packages/internal/fill-bake/dist/index.js +5 -1
- package/dist/packages/internal/fill-bake/dist/item-merge.js +4 -3
- package/dist/packages/internal/fill-bake/dist/resolve-instance.js +9 -0
- package/dist/packages/internal/fill-bake/dist/resolve.js +3 -0
- package/dist/packages/internal/fill-bake/dist/round-trip.js +2 -0
- package/dist/packages/internal/fill-bake/dist/strip.js +6 -0
- package/dist/packages/internal/fill-vocabulary/dist/holder.js +1 -1
- package/dist/packages/internal/fill-vocabulary/dist/resolved-decision.js +1 -1
- package/dist/packages/internal/fill-vocabulary/dist/verbs.js +1 -1
- package/dist/packages/internal/island-runtime/dist/index.js +1 -10
- package/dist/packages/internal/island-runtime/dist/use-capture-ready.d.ts +5 -0
- package/dist/packages/internal/lead-fields/dist/ingest.js +72 -0
- package/dist/packages/internal/media-resolution/dist/video.js +6 -2
- package/dist/packages/internal/page-render/dist/addresses.js +57 -0
- package/dist/packages/internal/page-render/dist/index.js +6 -0
- package/dist/packages/internal/page-render/dist/kept.js +30 -0
- package/dist/packages/internal/page-render/dist/nav.js +42 -0
- package/dist/packages/internal/page-render/dist/render.js +152 -0
- package/dist/packages/internal/page-shell/dist/assemble.js +161 -0
- package/dist/packages/internal/page-shell/dist/escape.js +10 -0
- package/dist/packages/internal/page-shell/dist/frame.js +23 -0
- package/dist/packages/internal/page-shell/dist/index.js +26 -0
- package/dist/packages/internal/playground/dist/build-fingerprint.js +18 -0
- package/dist/packages/internal/playground/dist/fixture-conversions/list-names-properties.js +76 -0
- package/dist/packages/internal/playground/dist/fixture-formats.d.ts +37 -0
- package/dist/packages/internal/playground/dist/fixture-formats.js +20 -0
- package/dist/packages/internal/playground/dist/host/bootstrap.js +108 -0
- package/dist/packages/internal/playground/dist/host/built.js +202 -0
- package/dist/packages/internal/playground/dist/host/catalog-fixture.js +53 -0
- package/dist/packages/internal/playground/dist/host/download.js +34 -0
- package/dist/packages/internal/playground/dist/host/env.js +11 -0
- package/dist/packages/internal/playground/dist/host/everywhere-saves.js +353 -0
- package/dist/packages/internal/playground/dist/host/fixture-addresses.js +52 -0
- package/dist/packages/internal/playground/dist/host/fixture-documents.js +494 -0
- package/dist/packages/internal/playground/dist/host/fixture-operations.js +364 -0
- package/dist/packages/internal/playground/dist/host/fixture-units.js +93 -0
- package/dist/{fixtures → packages/internal/playground/dist/host}/image-dimensions.js +1 -1
- package/dist/packages/internal/playground/dist/host/leads.js +41 -0
- package/dist/packages/internal/playground/dist/host/media-saves.js +309 -0
- package/dist/{fixtures → packages/internal/playground/dist/host}/media-store.js +79 -26
- package/dist/packages/internal/playground/dist/host/overridable.js +9 -0
- package/dist/packages/internal/playground/dist/host/owned-saves.js +203 -0
- package/dist/{playground → packages/internal/playground/dist}/host/page-state.js +1 -1
- package/dist/packages/internal/playground/dist/host/program.js +54 -0
- package/dist/packages/internal/playground/dist/host/property-derive.js +216 -0
- package/dist/packages/internal/playground/dist/host/property-saves.js +74 -0
- package/dist/{fixtures → packages/internal/playground/dist/host}/property.js +88 -229
- package/dist/packages/internal/playground/dist/host/provenance.js +49 -0
- package/dist/packages/internal/playground/dist/host/resolve.js +553 -0
- package/dist/packages/internal/playground/dist/host/row-images.js +41 -0
- package/dist/{fixtures → packages/internal/playground/dist/host}/rows.js +1 -1
- package/dist/packages/internal/playground/dist/host/save.js +429 -0
- package/dist/packages/internal/playground/dist/host/serve.js +952 -0
- package/dist/packages/internal/playground/dist/host/site.js +79 -0
- package/dist/packages/internal/playground/dist/host/slots.js +202 -0
- package/dist/packages/internal/playground/dist/host/stated-content.js +93 -0
- package/dist/packages/internal/playground/dist/host/stored-fixtures.js +135 -0
- package/dist/packages/internal/playground/dist/host/violations.js +7 -0
- package/dist/packages/internal/playground/dist/jobs/lanes.js +24 -0
- package/dist/packages/internal/playground/dist/jobs/memory-queue.js +112 -0
- package/dist/packages/internal/playground/dist/jobs/probe.js +37 -0
- package/dist/packages/internal/playground/dist/jobs/queue.js +28 -0
- package/dist/packages/internal/playground/dist/jobs/runner.js +207 -0
- package/dist/packages/internal/playground/dist/layout.js +14 -0
- package/dist/packages/internal/playground/dist/platform.js +33 -0
- package/dist/packages/internal/playground/dist/server/changes.js +57 -0
- package/dist/packages/internal/playground/dist/server/crop.js +47 -0
- package/dist/packages/internal/playground/dist/server/derived-files.js +88 -0
- package/dist/packages/internal/playground/dist/server/download.js +158 -0
- package/dist/{cli/dev → packages/internal/playground/dist/server}/http.js +4 -12
- package/dist/packages/internal/playground/dist/server/jobs.js +29 -0
- package/dist/{cli/dev → packages/internal/playground/dist/server}/listen.js +4 -11
- package/dist/packages/internal/playground/dist/server/playground-server.js +424 -0
- package/dist/{cli/dev → packages/internal/playground/dist/server}/request-error.js +1 -1
- package/dist/packages/internal/playground/dist/server/size-ladders.js +170 -0
- package/dist/packages/internal/playground/dist/server/verification.js +75 -0
- package/dist/packages/internal/playground/dist/server/verify-build.js +139 -0
- package/dist/packages/internal/playground/dist/wire.js +78 -0
- package/dist/packages/internal/property-facts/dist/registry-facts.js +1 -1
- package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +1 -1
- package/dist/packages/internal/section-contracts/dist/reads-acyclic-contract.js +1 -1
- package/dist/packages/internal/section-contracts/dist/row-fill-contract.js +1 -1
- package/dist/packages/internal/site-files/dist/head.js +59 -0
- package/dist/packages/internal/site-files/dist/index.js +4 -0
- package/dist/packages/internal/site-files/dist/site.js +211 -0
- package/dist/packages/internal/static-assets-binding/dist/index.js +32 -0
- package/dist/packages/internal/static-assets-binding/dist/section-asset-url.js +38 -5
- package/dist/packages/internal/template-artifact/dist/asset-mime.js +8 -1
- package/dist/packages/internal/template-artifact/dist/build.js +148 -0
- package/dist/packages/internal/template-artifact/dist/composition.js +62 -0
- package/dist/packages/internal/template-artifact/dist/index.js +2 -2
- package/dist/packages/internal/template-artifact/dist/layout.js +48 -12
- package/dist/packages/internal/template-artifact/dist/manifest.js +3 -10
- package/dist/packages/internal/template-artifact/dist/verify.js +51 -31
- package/dist/packages/internal/template-contracts/dist/address-value.js +6 -1
- package/dist/packages/internal/template-contracts/dist/content-categories.js +10 -1
- package/dist/packages/internal/template-contracts/dist/fact-compute.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/fact-keys.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/fact-registry.d.ts +3 -3
- package/dist/packages/internal/template-contracts/dist/fact-registry.js +59 -32
- package/dist/packages/internal/template-contracts/dist/fill-spec.js +14 -1
- package/dist/packages/internal/template-contracts/dist/list.d.ts +3 -3
- package/dist/packages/internal/template-contracts/dist/media-candidates.js +67 -2
- package/dist/packages/internal/template-contracts/dist/media-offer.js +180 -0
- package/dist/packages/internal/template-contracts/dist/object.d.ts +2 -2
- package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +9 -2
- package/dist/packages/internal/template-contracts/dist/read-graph.js +36 -2
- package/dist/packages/internal/template-contracts/dist/ref-locate.js +16 -1
- package/dist/packages/internal/template-contracts/dist/registry-facts.d.ts +9 -10
- package/dist/packages/internal/template-contracts/dist/registry-facts.js +12 -1
- package/dist/packages/internal/template-contracts/dist/registry-graph.js +112 -0
- package/dist/packages/internal/template-contracts/dist/registry-shape.js +15 -2
- package/dist/packages/internal/template-contracts/dist/require-graph.js +300 -0
- package/dist/packages/internal/template-contracts/dist/row-ref.d.ts +3 -3
- package/dist/packages/internal/template-contracts/dist/row.d.ts +3 -3
- package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +1 -1
- package/dist/{schema → packages/internal/template-contracts/dist}/section-nav.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/slot-builders.d.ts +4 -4
- package/dist/packages/internal/template-contracts/dist/slot-catalog.d.ts +3 -1
- package/dist/packages/internal/template-contracts/dist/slot-catalog.js +24 -1
- package/dist/packages/internal/template-contracts/dist/slot-overrides.js +26 -1
- package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/slot-types.js +1 -1
- package/dist/packages/internal/template-contracts/dist/slot-value.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/table-members.js +7 -2
- package/dist/packages/internal/template-contracts/dist/video-value.d.ts +1 -1
- package/dist/packages/internal/upload-formats/dist/heic.js +35 -0
- package/dist/packages/internal/upload-formats/dist/index.js +80 -0
- package/dist/packages/internal/upload-formats/dist/magic-bytes.js +293 -0
- package/dist/platform/paths.js +1 -0
- package/dist/platform/process.js +49 -1
- package/dist/rules/registry.js +27 -3
- package/dist/schema/fact.d.ts +2 -2
- package/dist/schema/facts-view.d.ts +1 -1
- package/dist/schema/section-props.d.ts +1 -1
- package/dist/schema/section-schema.d.ts +1 -1
- package/dist/schema/section-schema.js +1 -0
- package/dist/section/declare.d.ts +1 -1
- package/dist/section/discovery.js +6 -2
- package/dist/section/publish-shape.js +38 -0
- package/dist/section/section.d.ts +1 -1
- package/dist/template/define-template.d.ts +14 -4
- package/dist/template/define-template.js +2 -0
- package/dist/template/logic-shim.d.ts +1 -1
- package/dist/template/materialize.js +1 -1
- package/dist/template/running-copy.js +0 -1
- package/dist/template/section-reads.js +1 -1
- package/guide/reference/cli/INDEX.md +6 -5
- package/guide/reference/cli/build.md +10 -8
- package/guide/reference/cli/check.md +4 -2
- package/guide/reference/cli/dev.md +6 -2
- package/guide/reference/cli/install.md +41 -0
- package/guide/reference/cli/property-add.md +5 -4
- package/guide/reference/cli/property-list.md +2 -2
- package/guide/reference/cli/publish.md +5 -5
- package/guide/reference/cli/upgrade.md +1 -1
- package/guide/reference/rules/INDEX.md +8 -4
- package/package.json +14 -30
- package/dist/artifact/playground/client/assets/index-BcLGpWLR.js +0 -1266
- package/dist/artifact/playground/client/assets/index-CxWPYOH2.css +0 -1
- package/dist/cli/build/artifact.js +0 -220
- package/dist/cli/build/captures.js +0 -48
- package/dist/cli/build/fixtures.js +0 -92
- package/dist/cli/capture/capture-browser.js +0 -37
- package/dist/cli/capture/capture.js +0 -115
- package/dist/cli/check/render/inputs.js +0 -60
- package/dist/cli/check/render/nav.js +0 -31
- package/dist/cli/dev/crop.js +0 -20
- package/dist/cli/dev/fixture-ladders.js +0 -96
- package/dist/cli/dev/hmr.js +0 -26
- package/dist/cli/dev/islands.js +0 -234
- package/dist/cli/dev/load-template.js +0 -52
- package/dist/cli/dev/vite.js +0 -114
- package/dist/cli/dev/workspace-reader.js +0 -249
- package/dist/cli/publish/freshness.js +0 -64
- package/dist/fixtures/brand.d.ts +0 -4
- package/dist/fixtures/brand.js +0 -8
- package/dist/fixtures/load.js +0 -159
- package/dist/fixtures/resolve.js +0 -392
- package/dist/fixtures.d.ts +0 -429
- package/dist/fixtures.js +0 -23
- package/dist/packages/internal/canvas-core/dist/island-host.js +0 -62
- package/dist/packages/internal/canvas-core/dist/island-shields.js +0 -30
- package/dist/playground/canvas-island-host.d.ts +0 -11
- package/dist/playground/canvas-island-host.js +0 -29
- package/dist/playground/canvas-markers.js +0 -14
- package/dist/playground/host/paths.js +0 -8
- package/dist/playground/layout.js +0 -17
- 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
|
-
|
|
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
|
-
|
|
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
|
|
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) ||
|
|
186
|
-
|
|
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
|
-
|
|
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
|
-
|
|
195
|
-
if (holder !== void 0) settle(holder);
|
|
218
|
+
reach(path === void 0 ? void 0 : declarations.get(nodeId(decl.instanceId, path)));
|
|
196
219
|
}
|
|
197
|
-
|
|
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,
|
|
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)
|
|
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 =
|
|
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 ===
|
|
234
|
-
inst.variant =
|
|
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.
|
|
63
|
+
return current.flatMap((cur) => {
|
|
64
64
|
const id = cur[idKey];
|
|
65
|
-
|
|
66
|
-
|
|
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";
|