@superbee/core 0.2.0-pre.6 → 0.2.0-pre.60
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/README.md +1425 -0
- package/dist/artifact-contract.d.ts +90 -0
- package/dist/artifact-contract.d.ts.map +1 -0
- package/dist/artifact-contract.js +205 -0
- package/dist/artifact-contract.js.map +1 -0
- package/dist/backend.d.ts +50 -2
- package/dist/backend.d.ts.map +1 -1
- package/dist/backend.js +311 -33
- package/dist/backend.js.map +1 -1
- package/dist/blob-contract.d.ts +82 -0
- package/dist/blob-contract.d.ts.map +1 -0
- package/dist/blob-contract.js +134 -0
- package/dist/blob-contract.js.map +1 -0
- package/dist/blob-journal.d.ts +303 -0
- package/dist/blob-journal.d.ts.map +1 -0
- package/dist/blob-journal.js +541 -0
- package/dist/blob-journal.js.map +1 -0
- package/dist/body-pages.d.ts +20 -0
- package/dist/body-pages.d.ts.map +1 -0
- package/dist/body-pages.js +56 -0
- package/dist/body-pages.js.map +1 -0
- package/dist/body-replace-guards.d.ts +81 -0
- package/dist/body-replace-guards.d.ts.map +1 -0
- package/dist/body-replace-guards.js +271 -0
- package/dist/body-replace-guards.js.map +1 -0
- package/dist/bounded-yaml.d.ts +28 -0
- package/dist/bounded-yaml.d.ts.map +1 -0
- package/dist/bounded-yaml.js +182 -0
- package/dist/bounded-yaml.js.map +1 -0
- package/dist/builtin-recipes/content.d.ts +93 -0
- package/dist/builtin-recipes/content.d.ts.map +1 -0
- package/dist/builtin-recipes/content.js +288 -0
- package/dist/builtin-recipes/content.js.map +1 -0
- package/dist/builtin-recipes/site.d.ts +5 -0
- package/dist/builtin-recipes/site.d.ts.map +1 -0
- package/dist/builtin-recipes/site.js +83 -0
- package/dist/builtin-recipes/site.js.map +1 -0
- package/dist/builtin-recipes.d.ts +13 -0
- package/dist/builtin-recipes.d.ts.map +1 -0
- package/dist/builtin-recipes.js +123 -0
- package/dist/builtin-recipes.js.map +1 -0
- package/dist/bundle-health/index.d.ts +14 -0
- package/dist/bundle-health/index.d.ts.map +1 -0
- package/dist/bundle-health/index.js +12 -0
- package/dist/bundle-health/index.js.map +1 -0
- package/dist/bundle-health/kind-findings.d.ts +45 -0
- package/dist/bundle-health/kind-findings.d.ts.map +1 -0
- package/dist/bundle-health/kind-findings.js +67 -0
- package/dist/bundle-health/kind-findings.js.map +1 -0
- package/dist/bundle-health/legacy-page.d.ts +21 -0
- package/dist/bundle-health/legacy-page.d.ts.map +1 -0
- package/dist/bundle-health/legacy-page.js +25 -0
- package/dist/bundle-health/legacy-page.js.map +1 -0
- package/dist/bundle-health/report.d.ts +191 -0
- package/dist/bundle-health/report.d.ts.map +1 -0
- package/dist/bundle-health/report.js +419 -0
- package/dist/bundle-health/report.js.map +1 -0
- package/dist/bundle-ops.d.ts +11 -5
- package/dist/bundle-ops.d.ts.map +1 -1
- package/dist/bundle-ops.js +17 -10
- package/dist/bundle-ops.js.map +1 -1
- package/dist/bundle.d.ts +2 -4
- package/dist/bundle.d.ts.map +1 -1
- package/dist/bundle.js +3 -12
- package/dist/bundle.js.map +1 -1
- package/dist/code-point-order.d.ts +8 -0
- package/dist/code-point-order.d.ts.map +1 -0
- package/dist/code-point-order.js +20 -0
- package/dist/code-point-order.js.map +1 -0
- package/dist/code-unit-order.d.ts +15 -0
- package/dist/code-unit-order.d.ts.map +1 -0
- package/dist/code-unit-order.js +17 -0
- package/dist/code-unit-order.js.map +1 -0
- package/dist/connector-request.d.ts +112 -0
- package/dist/connector-request.d.ts.map +1 -0
- package/dist/connector-request.js +495 -0
- package/dist/connector-request.js.map +1 -0
- package/dist/connectors.d.ts +297 -0
- package/dist/connectors.d.ts.map +1 -0
- package/dist/connectors.js +1233 -0
- package/dist/connectors.js.map +1 -0
- package/dist/convention-record.d.ts +9 -0
- package/dist/convention-record.d.ts.map +1 -0
- package/dist/convention-record.js +13 -0
- package/dist/convention-record.js.map +1 -0
- package/dist/cross-bundle.d.ts +135 -0
- package/dist/cross-bundle.d.ts.map +1 -0
- package/dist/cross-bundle.js +191 -0
- package/dist/cross-bundle.js.map +1 -0
- package/dist/dependencies.d.ts +208 -0
- package/dist/dependencies.d.ts.map +1 -0
- package/dist/dependencies.js +367 -0
- package/dist/dependencies.js.map +1 -0
- package/dist/document-codec.d.ts +4 -1
- package/dist/document-codec.d.ts.map +1 -1
- package/dist/document-codec.js +3 -1
- package/dist/document-codec.js.map +1 -1
- package/dist/document-field-actions.d.ts +34 -3
- package/dist/document-field-actions.d.ts.map +1 -1
- package/dist/document-field-actions.js +149 -43
- package/dist/document-field-actions.js.map +1 -1
- package/dist/document-mutation.d.ts +55 -18
- package/dist/document-mutation.d.ts.map +1 -1
- package/dist/document-mutation.js +210 -86
- package/dist/document-mutation.js.map +1 -1
- package/dist/document-persistence.d.ts +12 -0
- package/dist/document-persistence.d.ts.map +1 -0
- package/dist/document-persistence.js +44 -0
- package/dist/document-persistence.js.map +1 -0
- package/dist/document-replace-rules.d.ts +44 -0
- package/dist/document-replace-rules.d.ts.map +1 -0
- package/dist/document-replace-rules.js +41 -0
- package/dist/document-replace-rules.js.map +1 -0
- package/dist/document-scan.d.ts +26 -0
- package/dist/document-scan.d.ts.map +1 -0
- package/dist/document-scan.js +70 -0
- package/dist/document-scan.js.map +1 -0
- package/dist/document-transfer.d.ts +312 -0
- package/dist/document-transfer.d.ts.map +1 -0
- package/dist/document-transfer.js +1400 -0
- package/dist/document-transfer.js.map +1 -0
- package/dist/document-write-policy.d.ts +15 -4
- package/dist/document-write-policy.d.ts.map +1 -1
- package/dist/document-write-policy.js +41 -27
- package/dist/document-write-policy.js.map +1 -1
- package/dist/domain-model.d.ts +73 -0
- package/dist/domain-model.d.ts.map +1 -0
- package/dist/domain-model.js +169 -0
- package/dist/domain-model.js.map +1 -0
- package/dist/edge-harness/corpus.d.ts +9 -0
- package/dist/edge-harness/corpus.d.ts.map +1 -0
- package/dist/edge-harness/corpus.js +57 -0
- package/dist/edge-harness/corpus.js.map +1 -0
- package/dist/edge-harness/index.d.ts +21 -0
- package/dist/edge-harness/index.d.ts.map +1 -0
- package/dist/edge-harness/index.js +537 -0
- package/dist/edge-harness/index.js.map +1 -0
- package/dist/edges/find.d.ts +14 -0
- package/dist/edges/find.d.ts.map +1 -0
- package/dist/edges/find.js +46 -0
- package/dist/edges/find.js.map +1 -0
- package/dist/edges/index.d.ts +13 -0
- package/dist/edges/index.d.ts.map +1 -0
- package/dist/edges/index.js +5 -0
- package/dist/edges/index.js.map +1 -0
- package/dist/edges/memory-source.d.ts +8 -0
- package/dist/edges/memory-source.d.ts.map +1 -0
- package/dist/edges/memory-source.js +164 -0
- package/dist/edges/memory-source.js.map +1 -0
- package/dist/edges/read-bounds.d.ts +6 -0
- package/dist/edges/read-bounds.d.ts.map +1 -0
- package/dist/edges/read-bounds.js +13 -0
- package/dist/edges/read-bounds.js.map +1 -0
- package/dist/edges/source.d.ts +123 -0
- package/dist/edges/source.d.ts.map +1 -0
- package/dist/edges/source.js +2 -0
- package/dist/edges/source.js.map +1 -0
- package/dist/edges/walk.d.ts +7 -0
- package/dist/edges/walk.d.ts.map +1 -0
- package/dist/edges/walk.js +14 -0
- package/dist/edges/walk.js.map +1 -0
- package/dist/engine.d.ts +55 -19
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +141 -111
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +37 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +75 -3
- package/dist/errors.js.map +1 -1
- package/dist/file-journaled-backend-internal.d.ts +21 -0
- package/dist/file-journaled-backend-internal.d.ts.map +1 -0
- package/dist/file-journaled-backend-internal.js +12 -0
- package/dist/file-journaled-backend-internal.js.map +1 -0
- package/dist/file-journaled-backend.d.ts +160 -0
- package/dist/file-journaled-backend.d.ts.map +1 -0
- package/dist/file-journaled-backend.js +1345 -0
- package/dist/file-journaled-backend.js.map +1 -0
- package/dist/filesystem-identity.d.ts +116 -17
- package/dist/filesystem-identity.d.ts.map +1 -1
- package/dist/filesystem-identity.js +532 -51
- package/dist/filesystem-identity.js.map +1 -1
- package/dist/filesystem-lock.d.ts +19 -2
- package/dist/filesystem-lock.d.ts.map +1 -1
- package/dist/filesystem-lock.js +259 -17
- package/dist/filesystem-lock.js.map +1 -1
- package/dist/filesystem-push-role.d.ts +77 -0
- package/dist/filesystem-push-role.d.ts.map +1 -0
- package/dist/filesystem-push-role.js +165 -0
- package/dist/filesystem-push-role.js.map +1 -0
- package/dist/filesystem-temp-name.d.ts +9 -0
- package/dist/filesystem-temp-name.d.ts.map +1 -0
- package/dist/filesystem-temp-name.js +20 -0
- package/dist/filesystem-temp-name.js.map +1 -0
- package/dist/filesystem.d.ts +6 -0
- package/dist/filesystem.d.ts.map +1 -1
- package/dist/filesystem.js +2 -1
- package/dist/filesystem.js.map +1 -1
- package/dist/freshness.d.ts +16 -1
- package/dist/freshness.d.ts.map +1 -1
- package/dist/freshness.js +47 -17
- package/dist/freshness.js.map +1 -1
- package/dist/frontmatter-contract.d.ts +14 -0
- package/dist/frontmatter-contract.d.ts.map +1 -1
- package/dist/frontmatter-contract.js +22 -0
- package/dist/frontmatter-contract.js.map +1 -1
- package/dist/frontmatter-splitter.d.ts +6 -0
- package/dist/frontmatter-splitter.d.ts.map +1 -1
- package/dist/frontmatter-splitter.js +8 -0
- package/dist/frontmatter-splitter.js.map +1 -1
- package/dist/frontmatter.d.ts +17 -7
- package/dist/frontmatter.d.ts.map +1 -1
- package/dist/frontmatter.js +52 -11
- package/dist/frontmatter.js.map +1 -1
- package/dist/governed-body-write.d.ts +8 -1
- package/dist/governed-body-write.d.ts.map +1 -1
- package/dist/governed-body-write.js +22 -8
- package/dist/governed-body-write.js.map +1 -1
- package/dist/heads-digest.d.ts.map +1 -1
- package/dist/heads-digest.js +2 -1
- package/dist/heads-digest.js.map +1 -1
- package/dist/host-text.d.ts +31 -0
- package/dist/host-text.d.ts.map +1 -0
- package/dist/host-text.js +77 -0
- package/dist/host-text.js.map +1 -0
- package/dist/hosted-client/binding.d.ts +7 -0
- package/dist/hosted-client/binding.d.ts.map +1 -0
- package/dist/hosted-client/binding.js +10 -0
- package/dist/hosted-client/binding.js.map +1 -0
- package/dist/hosted-client/blob-delivery.d.ts +68 -0
- package/dist/hosted-client/blob-delivery.d.ts.map +1 -0
- package/dist/hosted-client/blob-delivery.js +170 -0
- package/dist/hosted-client/blob-delivery.js.map +1 -0
- package/dist/hosted-client/blob-write.d.ts +387 -0
- package/dist/hosted-client/blob-write.d.ts.map +1 -0
- package/dist/hosted-client/blob-write.js +770 -0
- package/dist/hosted-client/blob-write.js.map +1 -0
- package/dist/hosted-client/bundle-listing.d.ts +46 -0
- package/dist/hosted-client/bundle-listing.d.ts.map +1 -0
- package/dist/hosted-client/bundle-listing.js +36 -0
- package/dist/hosted-client/bundle-listing.js.map +1 -0
- package/dist/hosted-client/client.d.ts +249 -0
- package/dist/hosted-client/client.d.ts.map +1 -0
- package/dist/hosted-client/client.js +381 -0
- package/dist/hosted-client/client.js.map +1 -0
- package/dist/hosted-client/directory.d.ts +46 -0
- package/dist/hosted-client/directory.d.ts.map +1 -0
- package/dist/hosted-client/directory.js +74 -0
- package/dist/hosted-client/directory.js.map +1 -0
- package/dist/hosted-client/errors.d.ts +26 -0
- package/dist/hosted-client/errors.d.ts.map +1 -0
- package/dist/hosted-client/errors.js +35 -0
- package/dist/hosted-client/errors.js.map +1 -0
- package/dist/hosted-client/inbox.d.ts +320 -0
- package/dist/hosted-client/inbox.d.ts.map +1 -0
- package/dist/hosted-client/inbox.js +379 -0
- package/dist/hosted-client/inbox.js.map +1 -0
- package/dist/hosted-client/index.d.ts +31 -0
- package/dist/hosted-client/index.d.ts.map +1 -0
- package/dist/hosted-client/index.js +33 -0
- package/dist/hosted-client/index.js.map +1 -0
- package/dist/hosted-client/journal.d.ts +103 -0
- package/dist/hosted-client/journal.d.ts.map +1 -0
- package/dist/hosted-client/journal.js +464 -0
- package/dist/hosted-client/journal.js.map +1 -0
- package/dist/hosted-client/node.d.ts +74 -0
- package/dist/hosted-client/node.d.ts.map +1 -0
- package/dist/hosted-client/node.js +219 -0
- package/dist/hosted-client/node.js.map +1 -0
- package/dist/hosted-client/reference.d.ts +12 -0
- package/dist/hosted-client/reference.d.ts.map +1 -0
- package/dist/hosted-client/reference.js +29 -0
- package/dist/hosted-client/reference.js.map +1 -0
- package/dist/hosted-client/view-content.d.ts +184 -0
- package/dist/hosted-client/view-content.d.ts.map +1 -0
- package/dist/hosted-client/view-content.js +268 -0
- package/dist/hosted-client/view-content.js.map +1 -0
- package/dist/hosted-client/write.d.ts +219 -0
- package/dist/hosted-client/write.d.ts.map +1 -0
- package/dist/hosted-client/write.js +419 -0
- package/dist/hosted-client/write.js.map +1 -0
- package/dist/hosted-transport/answer-rows.d.ts +344 -0
- package/dist/hosted-transport/answer-rows.d.ts.map +1 -0
- package/dist/hosted-transport/answer-rows.js +596 -0
- package/dist/hosted-transport/answer-rows.js.map +1 -0
- package/dist/hosted-transport/attribution.d.ts +32 -0
- package/dist/hosted-transport/attribution.d.ts.map +1 -0
- package/dist/hosted-transport/attribution.js +38 -0
- package/dist/hosted-transport/attribution.js.map +1 -0
- package/dist/hosted-transport/blob-reads.d.ts +35 -0
- package/dist/hosted-transport/blob-reads.d.ts.map +1 -0
- package/dist/hosted-transport/blob-reads.js +179 -0
- package/dist/hosted-transport/blob-reads.js.map +1 -0
- package/dist/hosted-transport/blob-write.d.ts +34 -0
- package/dist/hosted-transport/blob-write.d.ts.map +1 -0
- package/dist/hosted-transport/blob-write.js +115 -0
- package/dist/hosted-transport/blob-write.js.map +1 -0
- package/dist/hosted-transport/carrier.d.ts +166 -0
- package/dist/hosted-transport/carrier.d.ts.map +1 -0
- package/dist/hosted-transport/carrier.js +285 -0
- package/dist/hosted-transport/carrier.js.map +1 -0
- package/dist/hosted-transport/document-backend.d.ts +74 -0
- package/dist/hosted-transport/document-backend.d.ts.map +1 -0
- package/dist/hosted-transport/document-backend.js +160 -0
- package/dist/hosted-transport/document-backend.js.map +1 -0
- package/dist/hosted-transport/history-pages.d.ts +55 -0
- package/dist/hosted-transport/history-pages.d.ts.map +1 -0
- package/dist/hosted-transport/history-pages.js +69 -0
- package/dist/hosted-transport/history-pages.js.map +1 -0
- package/dist/hosted-transport/index.d.ts +21 -0
- package/dist/hosted-transport/index.d.ts.map +1 -0
- package/dist/hosted-transport/index.js +21 -0
- package/dist/hosted-transport/index.js.map +1 -0
- package/dist/hosted-transport/operations.d.ts +91 -0
- package/dist/hosted-transport/operations.d.ts.map +1 -0
- package/dist/hosted-transport/operations.js +129 -0
- package/dist/hosted-transport/operations.js.map +1 -0
- package/dist/hosted-transport/paged-reads.d.ts +87 -0
- package/dist/hosted-transport/paged-reads.d.ts.map +1 -0
- package/dist/hosted-transport/paged-reads.js +238 -0
- package/dist/hosted-transport/paged-reads.js.map +1 -0
- package/dist/hosted-transport/port-run.d.ts +41 -0
- package/dist/hosted-transport/port-run.d.ts.map +1 -0
- package/dist/hosted-transport/port-run.js +92 -0
- package/dist/hosted-transport/port-run.js.map +1 -0
- package/dist/hosted-transport/read-adapter.d.ts +266 -0
- package/dist/hosted-transport/read-adapter.d.ts.map +1 -0
- package/dist/hosted-transport/read-adapter.js +570 -0
- package/dist/hosted-transport/read-adapter.js.map +1 -0
- package/dist/hosted-transport/root-write.d.ts +114 -0
- package/dist/hosted-transport/root-write.d.ts.map +1 -0
- package/dist/hosted-transport/root-write.js +174 -0
- package/dist/hosted-transport/root-write.js.map +1 -0
- package/dist/hosted-transport/whole-document-transport.d.ts +223 -0
- package/dist/hosted-transport/whole-document-transport.d.ts.map +1 -0
- package/dist/hosted-transport/whole-document-transport.js +500 -0
- package/dist/hosted-transport/whole-document-transport.js.map +1 -0
- package/dist/identity-fold.d.ts +16 -0
- package/dist/identity-fold.d.ts.map +1 -0
- package/dist/identity-fold.js +24 -0
- package/dist/identity-fold.js.map +1 -0
- package/dist/index-projection.d.ts +2 -1
- package/dist/index-projection.d.ts.map +1 -1
- package/dist/index-projection.js +38 -28
- package/dist/index-projection.js.map +1 -1
- package/dist/index.d.ts +65 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +54 -22
- package/dist/index.js.map +1 -1
- package/dist/indexeddb-backend.d.ts +6 -32
- package/dist/indexeddb-backend.d.ts.map +1 -1
- package/dist/indexeddb-backend.js +82 -8
- package/dist/indexeddb-backend.js.map +1 -1
- package/dist/inventory-guard.d.ts +58 -0
- package/dist/inventory-guard.d.ts.map +1 -0
- package/dist/inventory-guard.js +169 -0
- package/dist/inventory-guard.js.map +1 -0
- package/dist/journaled-backend.d.ts +86 -8
- package/dist/journaled-backend.d.ts.map +1 -1
- package/dist/journaled-backend.js +64 -69
- package/dist/journaled-backend.js.map +1 -1
- package/dist/json-data.d.ts +3 -0
- package/dist/json-data.d.ts.map +1 -0
- package/dist/json-data.js +19 -0
- package/dist/json-data.js.map +1 -0
- package/dist/kind-authoring.d.ts +200 -0
- package/dist/kind-authoring.d.ts.map +1 -0
- package/dist/kind-authoring.js +539 -0
- package/dist/kind-authoring.js.map +1 -0
- package/dist/kind-changes.d.ts +45 -0
- package/dist/kind-changes.d.ts.map +1 -0
- package/dist/kind-changes.js +96 -0
- package/dist/kind-changes.js.map +1 -0
- package/dist/kind-conformance-error.d.ts +16 -0
- package/dist/kind-conformance-error.d.ts.map +1 -0
- package/dist/kind-conformance-error.js +20 -0
- package/dist/kind-conformance-error.js.map +1 -0
- package/dist/kind-coverage.d.ts +100 -0
- package/dist/kind-coverage.d.ts.map +1 -0
- package/dist/kind-coverage.js +185 -0
- package/dist/kind-coverage.js.map +1 -0
- package/dist/kind-deprecation.d.ts +32 -0
- package/dist/kind-deprecation.d.ts.map +1 -0
- package/dist/kind-deprecation.js +141 -0
- package/dist/kind-deprecation.js.map +1 -0
- package/dist/kind-field-mutation.d.ts +7 -2
- package/dist/kind-field-mutation.d.ts.map +1 -1
- package/dist/kind-field-mutation.js +28 -3
- package/dist/kind-field-mutation.js.map +1 -1
- package/dist/kind-undeclared-fields.d.ts +72 -0
- package/dist/kind-undeclared-fields.d.ts.map +1 -0
- package/dist/kind-undeclared-fields.js +124 -0
- package/dist/kind-undeclared-fields.js.map +1 -0
- package/dist/kinds-load.d.ts.map +1 -1
- package/dist/kinds-load.js +8 -5
- package/dist/kinds-load.js.map +1 -1
- package/dist/kinds.d.ts +117 -6
- package/dist/kinds.d.ts.map +1 -1
- package/dist/kinds.js +416 -29
- package/dist/kinds.js.map +1 -1
- package/dist/link-add.d.ts +81 -0
- package/dist/link-add.d.ts.map +1 -0
- package/dist/link-add.js +94 -0
- package/dist/link-add.js.map +1 -0
- package/dist/link-emit.d.ts +29 -0
- package/dist/link-emit.d.ts.map +1 -0
- package/dist/link-emit.js +39 -0
- package/dist/link-emit.js.map +1 -0
- package/dist/link-findings.d.ts +132 -0
- package/dist/link-findings.d.ts.map +1 -0
- package/dist/link-findings.js +263 -0
- package/dist/link-findings.js.map +1 -0
- package/dist/link-types.d.ts +34 -0
- package/dist/link-types.d.ts.map +1 -0
- package/dist/link-types.js +79 -0
- package/dist/link-types.js.map +1 -0
- package/dist/links.d.ts +68 -9
- package/dist/links.d.ts.map +1 -1
- package/dist/links.js +373 -65
- package/dist/links.js.map +1 -1
- package/dist/local-search-index.d.ts +86 -0
- package/dist/local-search-index.d.ts.map +1 -0
- package/dist/local-search-index.js +1055 -0
- package/dist/local-search-index.js.map +1 -0
- package/dist/managed-document-metadata.d.ts +5 -0
- package/dist/managed-document-metadata.d.ts.map +1 -0
- package/dist/managed-document-metadata.js +10 -0
- package/dist/managed-document-metadata.js.map +1 -0
- package/dist/markdown-syntax.d.ts +39 -0
- package/dist/markdown-syntax.d.ts.map +1 -0
- package/dist/markdown-syntax.js +10170 -0
- package/dist/markdown-syntax.js.map +1 -0
- package/dist/memory-backend.d.ts +3 -0
- package/dist/memory-backend.d.ts.map +1 -1
- package/dist/memory-backend.js +40 -2
- package/dist/memory-backend.js.map +1 -1
- package/dist/mutation-attribution.d.ts +2 -1
- package/dist/mutation-attribution.d.ts.map +1 -1
- package/dist/mutation-attribution.js.map +1 -1
- package/dist/okf-actor.d.ts +14 -3
- package/dist/okf-actor.d.ts.map +1 -1
- package/dist/okf-actor.js +25 -4
- package/dist/okf-actor.js.map +1 -1
- package/dist/okf-authored-values.d.ts +2 -0
- package/dist/okf-authored-values.d.ts.map +1 -1
- package/dist/okf-authored-values.js +4 -0
- package/dist/okf-authored-values.js.map +1 -1
- package/dist/okf-authoring.d.ts +7 -0
- package/dist/okf-authoring.d.ts.map +1 -0
- package/dist/okf-authoring.js +30 -0
- package/dist/okf-authoring.js.map +1 -0
- package/dist/okf-standard-fields.d.ts +4 -0
- package/dist/okf-standard-fields.d.ts.map +1 -1
- package/dist/okf-standard-fields.js +24 -14
- package/dist/okf-standard-fields.js.map +1 -1
- package/dist/okf-timestamps.d.ts +2 -2
- package/dist/okf-timestamps.d.ts.map +1 -1
- package/dist/okf-timestamps.js +25 -15
- package/dist/okf-timestamps.js.map +1 -1
- package/dist/page.d.ts +3 -7
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +3 -7
- package/dist/page.js.map +1 -1
- package/dist/path-fold.d.ts +67 -0
- package/dist/path-fold.d.ts.map +1 -0
- package/dist/path-fold.js +107 -0
- package/dist/path-fold.js.map +1 -0
- package/dist/paths.d.ts +46 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +114 -1
- package/dist/paths.js.map +1 -1
- package/dist/platform.d.ts +22 -2
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +16 -0
- package/dist/platform.js.map +1 -1
- package/dist/portable-export-contract.d.ts +17 -0
- package/dist/portable-export-contract.d.ts.map +1 -0
- package/dist/portable-export-contract.js +59 -0
- package/dist/portable-export-contract.js.map +1 -0
- package/dist/portable-export.d.ts +111 -0
- package/dist/portable-export.d.ts.map +1 -0
- package/dist/portable-export.js +448 -0
- package/dist/portable-export.js.map +1 -0
- package/dist/portable-frontmatter.d.ts +17 -0
- package/dist/portable-frontmatter.d.ts.map +1 -1
- package/dist/portable-frontmatter.js +42 -5
- package/dist/portable-frontmatter.js.map +1 -1
- package/dist/publication-filesystem.d.ts +1 -1
- package/dist/publication-filesystem.d.ts.map +1 -1
- package/dist/publication-filesystem.js +1 -1
- package/dist/publication-filesystem.js.map +1 -1
- package/dist/qualified-links.d.ts +67 -0
- package/dist/qualified-links.d.ts.map +1 -0
- package/dist/qualified-links.js +121 -0
- package/dist/qualified-links.js.map +1 -0
- package/dist/query-filter.d.ts +16 -4
- package/dist/query-filter.d.ts.map +1 -1
- package/dist/query-filter.js +44 -2
- package/dist/query-filter.js.map +1 -1
- package/dist/query-order.d.ts +22 -0
- package/dist/query-order.d.ts.map +1 -0
- package/dist/query-order.js +43 -0
- package/dist/query-order.js.map +1 -0
- package/dist/recipe-lifecycle/errors.d.ts +96 -0
- package/dist/recipe-lifecycle/errors.d.ts.map +1 -0
- package/dist/recipe-lifecycle/errors.js +122 -0
- package/dist/recipe-lifecycle/errors.js.map +1 -0
- package/dist/recipe-lifecycle/evolution.d.ts +134 -0
- package/dist/recipe-lifecycle/evolution.d.ts.map +1 -0
- package/dist/recipe-lifecycle/evolution.js +1047 -0
- package/dist/recipe-lifecycle/evolution.js.map +1 -0
- package/dist/recipe-lifecycle/index.d.ts +38 -0
- package/dist/recipe-lifecycle/index.d.ts.map +1 -0
- package/dist/recipe-lifecycle/index.js +31 -0
- package/dist/recipe-lifecycle/index.js.map +1 -0
- package/dist/recipe-lifecycle/install.d.ts +172 -0
- package/dist/recipe-lifecycle/install.d.ts.map +1 -0
- package/dist/recipe-lifecycle/install.js +652 -0
- package/dist/recipe-lifecycle/install.js.map +1 -0
- package/dist/recipe-lifecycle/legacy-aliases.d.ts +4 -0
- package/dist/recipe-lifecycle/legacy-aliases.d.ts.map +1 -0
- package/dist/recipe-lifecycle/legacy-aliases.js +22 -0
- package/dist/recipe-lifecycle/legacy-aliases.js.map +1 -0
- package/dist/recipe-lifecycle/retirement-apply.d.ts +59 -0
- package/dist/recipe-lifecycle/retirement-apply.d.ts.map +1 -0
- package/dist/recipe-lifecycle/retirement-apply.js +165 -0
- package/dist/recipe-lifecycle/retirement-apply.js.map +1 -0
- package/dist/recipe-lifecycle/retirement.d.ts +89 -0
- package/dist/recipe-lifecycle/retirement.d.ts.map +1 -0
- package/dist/recipe-lifecycle/retirement.js +347 -0
- package/dist/recipe-lifecycle/retirement.js.map +1 -0
- package/dist/recipe-lifecycle/set-commit.d.ts +31 -0
- package/dist/recipe-lifecycle/set-commit.d.ts.map +1 -0
- package/dist/recipe-lifecycle/set-commit.js +37 -0
- package/dist/recipe-lifecycle/set-commit.js.map +1 -0
- package/dist/recipe-lifecycle/stamp.d.ts +146 -0
- package/dist/recipe-lifecycle/stamp.d.ts.map +1 -0
- package/dist/recipe-lifecycle/stamp.js +361 -0
- package/dist/recipe-lifecycle/stamp.js.map +1 -0
- package/dist/recipe-qualify.d.ts +80 -0
- package/dist/recipe-qualify.d.ts.map +1 -0
- package/dist/recipe-qualify.js +352 -0
- package/dist/recipe-qualify.js.map +1 -0
- package/dist/recipe-set-preflight.d.ts +139 -0
- package/dist/recipe-set-preflight.d.ts.map +1 -0
- package/dist/recipe-set-preflight.js +536 -0
- package/dist/recipe-set-preflight.js.map +1 -0
- package/dist/recipe-sources.d.ts +209 -0
- package/dist/recipe-sources.d.ts.map +1 -0
- package/dist/recipe-sources.js +607 -0
- package/dist/recipe-sources.js.map +1 -0
- package/dist/recipes.d.ts +45 -1
- package/dist/recipes.d.ts.map +1 -1
- package/dist/recipes.js +138 -1
- package/dist/recipes.js.map +1 -1
- package/dist/record-overlay/apply.d.ts +43 -0
- package/dist/record-overlay/apply.d.ts.map +1 -0
- package/dist/record-overlay/apply.js +523 -0
- package/dist/record-overlay/apply.js.map +1 -0
- package/dist/record-overlay/canonical.d.ts +22 -0
- package/dist/record-overlay/canonical.d.ts.map +1 -0
- package/dist/record-overlay/canonical.js +47 -0
- package/dist/record-overlay/canonical.js.map +1 -0
- package/dist/record-overlay/digest.d.ts +4 -0
- package/dist/record-overlay/digest.d.ts.map +1 -0
- package/dist/record-overlay/digest.js +11 -0
- package/dist/record-overlay/digest.js.map +1 -0
- package/dist/record-overlay/errors.d.ts +44 -0
- package/dist/record-overlay/errors.d.ts.map +1 -0
- package/dist/record-overlay/errors.js +55 -0
- package/dist/record-overlay/errors.js.map +1 -0
- package/dist/record-overlay/findings.d.ts +5 -0
- package/dist/record-overlay/findings.d.ts.map +1 -0
- package/dist/record-overlay/findings.js +86 -0
- package/dist/record-overlay/findings.js.map +1 -0
- package/dist/record-overlay/index.d.ts +23 -0
- package/dist/record-overlay/index.d.ts.map +1 -0
- package/dist/record-overlay/index.js +21 -0
- package/dist/record-overlay/index.js.map +1 -0
- package/dist/record-overlay/plan.d.ts +12 -0
- package/dist/record-overlay/plan.d.ts.map +1 -0
- package/dist/record-overlay/plan.js +285 -0
- package/dist/record-overlay/plan.js.map +1 -0
- package/dist/record-overlay/policy.d.ts +74 -0
- package/dist/record-overlay/policy.d.ts.map +1 -0
- package/dist/record-overlay/policy.js +235 -0
- package/dist/record-overlay/policy.js.map +1 -0
- package/dist/record-overlay/redact.d.ts +3 -0
- package/dist/record-overlay/redact.d.ts.map +1 -0
- package/dist/record-overlay/redact.js +27 -0
- package/dist/record-overlay/redact.js.map +1 -0
- package/dist/record-overlay/signal-kind.d.ts +30 -0
- package/dist/record-overlay/signal-kind.d.ts.map +1 -0
- package/dist/record-overlay/signal-kind.js +38 -0
- package/dist/record-overlay/signal-kind.js.map +1 -0
- package/dist/record-overlay/signal-text.d.ts +52 -0
- package/dist/record-overlay/signal-text.d.ts.map +1 -0
- package/dist/record-overlay/signal-text.js +126 -0
- package/dist/record-overlay/signal-text.js.map +1 -0
- package/dist/record-overlay/source-index.d.ts +19 -0
- package/dist/record-overlay/source-index.d.ts.map +1 -0
- package/dist/record-overlay/source-index.js +102 -0
- package/dist/record-overlay/source-index.js.map +1 -0
- package/dist/record-overlay/types.d.ts +291 -0
- package/dist/record-overlay/types.d.ts.map +1 -0
- package/dist/record-overlay/types.js +2 -0
- package/dist/record-overlay/types.js.map +1 -0
- package/dist/relationships.d.ts +71 -0
- package/dist/relationships.d.ts.map +1 -0
- package/dist/relationships.js +87 -0
- package/dist/relationships.js.map +1 -0
- package/dist/remote-backend.d.ts +67 -32
- package/dist/remote-backend.d.ts.map +1 -1
- package/dist/remote-backend.js +171 -39
- package/dist/remote-backend.js.map +1 -1
- package/dist/remote-document-codec.d.ts +2 -1
- package/dist/remote-document-codec.d.ts.map +1 -1
- package/dist/remote-document-codec.js +3 -2
- package/dist/remote-document-codec.js.map +1 -1
- package/dist/remote-error.d.ts +17 -1
- package/dist/remote-error.d.ts.map +1 -1
- package/dist/remote-error.js +32 -2
- package/dist/remote-error.js.map +1 -1
- package/dist/remote-operations.d.ts +0 -40
- package/dist/remote-operations.d.ts.map +1 -1
- package/dist/remote-operations.js +28 -6
- package/dist/remote-operations.js.map +1 -1
- package/dist/remote-parsers.d.ts +20 -1
- package/dist/remote-parsers.d.ts.map +1 -1
- package/dist/remote-parsers.js +1 -1
- package/dist/remote-parsers.js.map +1 -1
- package/dist/remote.d.ts +2 -4
- package/dist/remote.d.ts.map +1 -1
- package/dist/remote.js +2 -3
- package/dist/remote.js.map +1 -1
- package/dist/reserved-write.d.ts +9 -0
- package/dist/reserved-write.d.ts.map +1 -0
- package/dist/reserved-write.js +15 -0
- package/dist/reserved-write.js.map +1 -0
- package/dist/search/backend-source.d.ts +6 -0
- package/dist/search/backend-source.d.ts.map +1 -0
- package/dist/search/backend-source.js +57 -0
- package/dist/search/backend-source.js.map +1 -0
- package/dist/search/contract.d.ts +162 -0
- package/dist/search/contract.d.ts.map +1 -0
- package/dist/search/contract.js +82 -0
- package/dist/search/contract.js.map +1 -0
- package/dist/search/errors.d.ts +30 -0
- package/dist/search/errors.d.ts.map +1 -0
- package/dist/search/errors.js +50 -0
- package/dist/search/errors.js.map +1 -0
- package/dist/search/identity.d.ts +12 -0
- package/dist/search/identity.d.ts.map +1 -0
- package/dist/search/identity.js +90 -0
- package/dist/search/identity.js.map +1 -0
- package/dist/search/index.d.ts +16 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +15 -0
- package/dist/search/index.js.map +1 -0
- package/dist/search/memory-source.d.ts +36 -0
- package/dist/search/memory-source.d.ts.map +1 -0
- package/dist/search/memory-source.js +238 -0
- package/dist/search/memory-source.js.map +1 -0
- package/dist/search/rank.d.ts +29 -0
- package/dist/search/rank.d.ts.map +1 -0
- package/dist/search/rank.js +132 -0
- package/dist/search/rank.js.map +1 -0
- package/dist/search/row-text.d.ts +38 -0
- package/dist/search/row-text.d.ts.map +1 -0
- package/dist/search/row-text.js +88 -0
- package/dist/search/row-text.js.map +1 -0
- package/dist/search/search.d.ts +28 -0
- package/dist/search/search.d.ts.map +1 -0
- package/dist/search/search.js +265 -0
- package/dist/search/search.js.map +1 -0
- package/dist/search/source.d.ts +70 -0
- package/dist/search/source.d.ts.map +1 -0
- package/dist/search/source.js +2 -0
- package/dist/search/source.js.map +1 -0
- package/dist/search/text.d.ts +49 -0
- package/dist/search/text.d.ts.map +1 -0
- package/dist/search/text.js +165 -0
- package/dist/search/text.js.map +1 -0
- package/dist/search-index.d.ts +9 -0
- package/dist/search-index.d.ts.map +1 -0
- package/dist/search-index.js +9 -0
- package/dist/search-index.js.map +1 -0
- package/dist/sha256.d.ts.map +1 -1
- package/dist/sha256.js +3 -2
- package/dist/sha256.js.map +1 -1
- package/dist/site-capabilities.d.ts +345 -0
- package/dist/site-capabilities.d.ts.map +1 -0
- package/dist/site-capabilities.js +724 -0
- package/dist/site-capabilities.js.map +1 -0
- package/dist/site-manifest.d.ts +51 -0
- package/dist/site-manifest.d.ts.map +1 -0
- package/dist/site-manifest.js +115 -0
- package/dist/site-manifest.js.map +1 -0
- package/dist/skip-reasons.d.ts +12 -0
- package/dist/skip-reasons.d.ts.map +1 -0
- package/dist/skip-reasons.js +15 -0
- package/dist/skip-reasons.js.map +1 -0
- package/dist/source-engine/driver.d.ts +40 -0
- package/dist/source-engine/driver.d.ts.map +1 -0
- package/dist/source-engine/driver.js +513 -0
- package/dist/source-engine/driver.js.map +1 -0
- package/dist/source-engine/evidence.d.ts +39 -0
- package/dist/source-engine/evidence.d.ts.map +1 -0
- package/dist/source-engine/evidence.js +54 -0
- package/dist/source-engine/evidence.js.map +1 -0
- package/dist/source-engine/guard.d.ts +87 -0
- package/dist/source-engine/guard.d.ts.map +1 -0
- package/dist/source-engine/guard.js +427 -0
- package/dist/source-engine/guard.js.map +1 -0
- package/dist/source-engine/index.d.ts +35 -0
- package/dist/source-engine/index.d.ts.map +1 -0
- package/dist/source-engine/index.js +27 -0
- package/dist/source-engine/index.js.map +1 -0
- package/dist/source-engine/machine-state.d.ts +87 -0
- package/dist/source-engine/machine-state.d.ts.map +1 -0
- package/dist/source-engine/machine-state.js +94 -0
- package/dist/source-engine/machine-state.js.map +1 -0
- package/dist/source-engine/observe.d.ts +10 -0
- package/dist/source-engine/observe.d.ts.map +1 -0
- package/dist/source-engine/observe.js +276 -0
- package/dist/source-engine/observe.js.map +1 -0
- package/dist/source-engine/overlay.d.ts +50 -0
- package/dist/source-engine/overlay.d.ts.map +1 -0
- package/dist/source-engine/overlay.js +114 -0
- package/dist/source-engine/overlay.js.map +1 -0
- package/dist/source-engine/robots.d.ts +53 -0
- package/dist/source-engine/robots.d.ts.map +1 -0
- package/dist/source-engine/robots.js +158 -0
- package/dist/source-engine/robots.js.map +1 -0
- package/dist/source-engine/snapshot.d.ts +104 -0
- package/dist/source-engine/snapshot.d.ts.map +1 -0
- package/dist/source-engine/snapshot.js +240 -0
- package/dist/source-engine/snapshot.js.map +1 -0
- package/dist/source-engine/target-consumer.d.ts +12 -0
- package/dist/source-engine/target-consumer.d.ts.map +1 -0
- package/dist/source-engine/target-consumer.js +15 -0
- package/dist/source-engine/target-consumer.js.map +1 -0
- package/dist/source-engine/target-descriptor.d.ts +3 -0
- package/dist/source-engine/target-descriptor.d.ts.map +1 -0
- package/dist/source-engine/target-descriptor.js +26 -0
- package/dist/source-engine/target-descriptor.js.map +1 -0
- package/dist/source-engine/target.d.ts +82 -0
- package/dist/source-engine/target.d.ts.map +1 -0
- package/dist/source-engine/target.js +213 -0
- package/dist/source-engine/target.js.map +1 -0
- package/dist/source-engine/types.d.ts +296 -0
- package/dist/source-engine/types.d.ts.map +1 -0
- package/dist/source-engine/types.js +2 -0
- package/dist/source-engine/types.js.map +1 -0
- package/dist/source-harness/conformance.d.ts +16 -0
- package/dist/source-harness/conformance.d.ts.map +1 -0
- package/dist/source-harness/conformance.js +266 -0
- package/dist/source-harness/conformance.js.map +1 -0
- package/dist/source-harness/fixture.d.ts +97 -0
- package/dist/source-harness/fixture.d.ts.map +1 -0
- package/dist/source-harness/fixture.js +276 -0
- package/dist/source-harness/fixture.js.map +1 -0
- package/dist/source-harness/index.d.ts +22 -0
- package/dist/source-harness/index.d.ts.map +1 -0
- package/dist/source-harness/index.js +19 -0
- package/dist/source-harness/index.js.map +1 -0
- package/dist/source-harness/peer.d.ts +74 -0
- package/dist/source-harness/peer.d.ts.map +1 -0
- package/dist/source-harness/peer.js +106 -0
- package/dist/source-harness/peer.js.map +1 -0
- package/dist/source-profiles/index.d.ts +31 -0
- package/dist/source-profiles/index.d.ts.map +1 -0
- package/dist/source-profiles/index.js +206 -0
- package/dist/source-profiles/index.js.map +1 -0
- package/dist/source-profiles/link-header.d.ts +6 -0
- package/dist/source-profiles/link-header.d.ts.map +1 -0
- package/dist/source-profiles/link-header.js +131 -0
- package/dist/source-profiles/link-header.js.map +1 -0
- package/dist/storage-harness/contract.d.ts +109 -0
- package/dist/storage-harness/contract.d.ts.map +1 -0
- package/dist/storage-harness/contract.js +853 -0
- package/dist/storage-harness/contract.js.map +1 -0
- package/dist/storage-harness/index.d.ts +39 -0
- package/dist/storage-harness/index.d.ts.map +1 -0
- package/dist/storage-harness/index.js +55 -0
- package/dist/storage-harness/index.js.map +1 -0
- package/dist/storage.d.ts +10 -2
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +10 -2
- package/dist/storage.js.map +1 -1
- package/dist/string-identity-agreement.d.ts +35 -0
- package/dist/string-identity-agreement.d.ts.map +1 -0
- package/dist/string-identity-agreement.js +78 -0
- package/dist/string-identity-agreement.js.map +1 -0
- package/dist/system-documents.d.ts +82 -0
- package/dist/system-documents.d.ts.map +1 -0
- package/dist/system-documents.js +127 -0
- package/dist/system-documents.js.map +1 -0
- package/dist/types.d.ts +54 -8
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -10
- package/dist/types.js.map +1 -1
- package/dist/uncertain-write.d.ts +164 -17
- package/dist/uncertain-write.d.ts.map +1 -1
- package/dist/uncertain-write.js +86 -30
- package/dist/uncertain-write.js.map +1 -1
- package/dist/validation.d.ts +5 -0
- package/dist/validation.d.ts.map +1 -1
- package/dist/verification.d.ts +22 -10
- package/dist/verification.d.ts.map +1 -1
- package/dist/verification.js +87 -39
- package/dist/verification.js.map +1 -1
- package/dist/view-launch-contract.d.ts +14 -0
- package/dist/view-launch-contract.d.ts.map +1 -0
- package/dist/view-launch-contract.js +23 -0
- package/dist/view-launch-contract.js.map +1 -0
- package/dist/write-identity.d.ts +5 -0
- package/dist/write-identity.d.ts.map +1 -0
- package/dist/write-identity.js +9 -0
- package/dist/write-identity.js.map +1 -0
- package/package.json +202 -8
|
@@ -0,0 +1,1400 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Document transfer: copy chosen documents from one bundle into another with ids remapped, links
|
|
3
|
+
* rewritten, provenance extended and one Promotion record of the act.
|
|
4
|
+
*
|
|
5
|
+
* Plan and apply share one computation. A plan reads every source document, every governing
|
|
6
|
+
* convention, the target's `docs/dependencies` and the existence of every target id once, and
|
|
7
|
+
* binds all of it into a plan token; apply recomputes the plan from fresh reads and refuses
|
|
8
|
+
* unless the token is equal, so what applies is exactly what was shown.
|
|
9
|
+
*
|
|
10
|
+
* There is no multi-document transaction on any local backend, so apply never claims
|
|
11
|
+
* all-or-nothing. It writes the Promotion record first (`promotion_state: applying`, with the
|
|
12
|
+
* whole mapping) as the journal, then creates every document with an expect-absent write, then
|
|
13
|
+
* marks the record `applied`. A failed write stops the apply and leaves the record at `applying`;
|
|
14
|
+
* {@link resumeDocumentTransfer} is the one recovery path. Nothing here deletes a document, and only a
|
|
15
|
+
* refresh replaces one.
|
|
16
|
+
*
|
|
17
|
+
* A refresh ({@link planDocumentRefresh}, {@link applyDocumentRefresh}) brings the copies an
|
|
18
|
+
* applied record names up to date with their sources. The record's `source_version` and
|
|
19
|
+
* `target_version` per entry are the baseline: a changed source is an update, and a copy whose
|
|
20
|
+
* version differs from `target_version` was edited in the target and is a conflict unless the
|
|
21
|
+
* caller names it to overwrite or skip. The record is the journal again (`promotion_state:
|
|
22
|
+
* refreshing` with the pending rows), every copy is replaced under a hard compare-and-swap on the
|
|
23
|
+
* version the plan saw, and {@link resumeDocumentTransfer} finishes an interrupted refresh. A
|
|
24
|
+
* refresh never creates, deletes or picks up documents the record does not already name.
|
|
25
|
+
*
|
|
26
|
+
* v1 caveats: resume writes the remaining documents even if the target has since dropped a Kind
|
|
27
|
+
* that governed them. Reference-style link definitions (`[x]: ../f.md`) are copied unchanged and
|
|
28
|
+
* reported as `reference_definition`, so the caller sees links that may not resolve in the target.
|
|
29
|
+
*
|
|
30
|
+
* Storage-neutral: the source is a {@link BundleBinding} (any backend), the target is written
|
|
31
|
+
* through the shared document mutation policy, and no Node module is imported.
|
|
32
|
+
*/
|
|
33
|
+
import { computeDroppedLinks, outboundLinks } from "./body-replace-guards.js";
|
|
34
|
+
import { backendFor, readBundleOkfVersion, readDocVersioned } from "./bundle-ops.js";
|
|
35
|
+
import { BUNDLE_NAME_DOC_ID, declaredBundleId, DEPENDENCIES_DOC_ID, parseDependencies, } from "./dependencies.js";
|
|
36
|
+
import { DocumentNotFoundError, mutateDocument, prepareDocumentMutationCandidate } from "./document-mutation.js";
|
|
37
|
+
import { readManyExisting } from "./engine.js";
|
|
38
|
+
import { FilesystemIdentityAliasError, InvalidInputError, OkfActorError } from "./errors.js";
|
|
39
|
+
import { foldPath } from "./path-fold.js";
|
|
40
|
+
import { loadKinds } from "./kinds-load.js";
|
|
41
|
+
import { CONVENTIONS_PREFIX } from "./kinds.js";
|
|
42
|
+
import { emitLinkHref } from "./link-emit.js";
|
|
43
|
+
import { classifyConceptLink, extractMarkdownLinkSpans, maskMarkdownCode, rewriteMarkdownLinks } from "./links.js";
|
|
44
|
+
import { isOkfActor } from "./okf-actor.js";
|
|
45
|
+
import { SUPERBEE_UPDATED_BY_FIELD } from "./mutation-attribution.js";
|
|
46
|
+
import { assertCanonicalNewDocumentId, assertSafeConceptId, compareStorageKeys, isReservedFile, pathFromConceptId, trimTrailingSlashes } from "./paths.js";
|
|
47
|
+
import { isQualifiedHref, parseQualifiedHref, qualifiedHref } from "./qualified-links.js";
|
|
48
|
+
import { sha256HexOfUtf8 } from "./sha256.js";
|
|
49
|
+
import { OWNED_LOCATIONS, PROMOTION_TYPE, PROMOTIONS_PREFIX, sectionOf } from "./system-documents.js";
|
|
50
|
+
import { VersionConflict } from "./versioning.js";
|
|
51
|
+
export const DOCUMENT_TRANSFER_CONTRACT = "document-transfer/v1";
|
|
52
|
+
// The `type` of the record each transfer writes (not the `promote` command, which copies one file), and where it lives.
|
|
53
|
+
export { PROMOTION_TYPE, PROMOTIONS_PREFIX };
|
|
54
|
+
// ── Typed failures ─────────────────────────────────────────────────────────
|
|
55
|
+
/** Apply was asked for a plan that has conflicts. Nothing was written. */
|
|
56
|
+
export class TransferConflictError extends Error {
|
|
57
|
+
conflicts;
|
|
58
|
+
constructor(conflicts) {
|
|
59
|
+
const first = conflicts[0];
|
|
60
|
+
super(`the import plan has ${conflicts.length} conflict${conflicts.length === 1 ? "" : "s"}${first ? `; first: ${first.code}: ${first.detail}` : ""}. Nothing was written.`);
|
|
61
|
+
this.name = "TransferConflictError";
|
|
62
|
+
this.conflicts = conflicts;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** The plan recomputed at apply differs from the token the caller presented. Nothing was written. */
|
|
66
|
+
export class TransferPlanChanged extends Error {
|
|
67
|
+
expected;
|
|
68
|
+
current;
|
|
69
|
+
constructor(expected, current) {
|
|
70
|
+
super(`the import plan changed since it was shown (expected ${expected}, current ${current}). Nothing was written; inspect a fresh plan.`);
|
|
71
|
+
this.name = "TransferPlanChanged";
|
|
72
|
+
this.expected = expected;
|
|
73
|
+
this.current = current;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A write failed after the Promotion record was created. The record stays at `applying`; the
|
|
78
|
+
* listed documents were written and the pending ones were not. Resume the record to finish.
|
|
79
|
+
*/
|
|
80
|
+
export class TransferInterrupted extends Error {
|
|
81
|
+
recordId;
|
|
82
|
+
written;
|
|
83
|
+
pending;
|
|
84
|
+
/** The state the record was left in: `applying` for an import, `refreshing` for a refresh. */
|
|
85
|
+
state;
|
|
86
|
+
cause;
|
|
87
|
+
constructor(recordId, written, pending, cause, state = "applying") {
|
|
88
|
+
const why = cause instanceof Error ? cause.message : String(cause);
|
|
89
|
+
super(`the ${state === "applying" ? "import" : "refresh"} stopped after writing ${written.length} document${written.length === 1 ? "" : "s"}` +
|
|
90
|
+
`${pending.length > 0 ? ` (${pending.length} not written, first '${pending[0].target}')` : ""}: ${why}. ` +
|
|
91
|
+
`'${recordId}' stays at promotion_state ${state}; resume it to finish.`);
|
|
92
|
+
this.name = "TransferInterrupted";
|
|
93
|
+
this.recordId = recordId;
|
|
94
|
+
this.written = written;
|
|
95
|
+
this.pending = pending;
|
|
96
|
+
this.state = state;
|
|
97
|
+
this.cause = cause;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Resume met a record, source or target it cannot finish safely. Nothing further was written. */
|
|
101
|
+
export class TransferResumeConflict extends Error {
|
|
102
|
+
recordId;
|
|
103
|
+
id;
|
|
104
|
+
written;
|
|
105
|
+
constructor(recordId, detail, options = {}) {
|
|
106
|
+
super(`cannot ${options.verb ?? "resume"} '${recordId}': ${detail}`);
|
|
107
|
+
this.name = "TransferResumeConflict";
|
|
108
|
+
this.recordId = recordId;
|
|
109
|
+
if (options.id !== undefined)
|
|
110
|
+
this.id = options.id;
|
|
111
|
+
this.written = options.written ?? [];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// ── Snapshots ──────────────────────────────────────────────────────────────
|
|
115
|
+
/** Documents that describe the source bundle itself (the owned records at exact ids); they never transfer. */
|
|
116
|
+
const SOURCE_SELF_DOCS = new Set(OWNED_LOCATIONS.flatMap((owned) => owned.id === undefined ? [] : [owned.id]));
|
|
117
|
+
/** Only people's documents transfer; Superbee's own records stay behind. */
|
|
118
|
+
function transfersAsDocument(id, doc) {
|
|
119
|
+
return sectionOf({ id, frontmatter: doc.frontmatter }) === "documents";
|
|
120
|
+
}
|
|
121
|
+
function isAbsent(error) {
|
|
122
|
+
return error?.code === "ENOENT";
|
|
123
|
+
}
|
|
124
|
+
async function readOptional(bundle, id) {
|
|
125
|
+
try {
|
|
126
|
+
return await readDocVersioned(bundle, id);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
if (isAbsent(error))
|
|
130
|
+
return null;
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async function readSource(binding) {
|
|
135
|
+
const bundle = { root: binding.label, backend: binding.backend };
|
|
136
|
+
const listed = await binding.backend.list();
|
|
137
|
+
const malformed = new Map();
|
|
138
|
+
const results = await readManyExisting(binding.backend, listed, (skip) => malformed.set(skip.id, skip.reason));
|
|
139
|
+
const docs = new Map(results.map((result) => [result.doc.id, result]));
|
|
140
|
+
const ids = new Set([...docs.keys(), ...malformed.keys()]);
|
|
141
|
+
const dependencies = parseDependencies(docs.get(DEPENDENCIES_DOC_ID)?.doc ?? null, { selfBundleId: binding.identity });
|
|
142
|
+
const folds = new Set([...ids].map(foldPath));
|
|
143
|
+
return { ids, folds, docs, malformed, dependencies, registry: await loadKinds(bundle), bundle };
|
|
144
|
+
}
|
|
145
|
+
async function readTarget(target) {
|
|
146
|
+
const edition = (await readBundleOkfVersion(target)) ?? "0.1";
|
|
147
|
+
if (edition !== "0.1" && edition !== "0.2") {
|
|
148
|
+
throw new InvalidInputError(`Unsupported OKF version '${edition}' in the target bundle. This build can write 0.1 and 0.2 bundles.`);
|
|
149
|
+
}
|
|
150
|
+
const named = await readOptional(target, BUNDLE_NAME_DOC_ID);
|
|
151
|
+
const bundleId = declaredBundleId(named?.doc);
|
|
152
|
+
const deps = await readOptional(target, DEPENDENCIES_DOC_ID);
|
|
153
|
+
const ids = new Set(await backendFor(target).list());
|
|
154
|
+
const snapshot = {
|
|
155
|
+
okfVersion: edition,
|
|
156
|
+
dependencies: parseDependencies(deps?.doc ?? null, { selfBundleId: bundleId }),
|
|
157
|
+
dependenciesVersion: deps?.version ?? null,
|
|
158
|
+
ids,
|
|
159
|
+
registry: await loadKinds(target),
|
|
160
|
+
};
|
|
161
|
+
if (bundleId !== undefined)
|
|
162
|
+
snapshot.bundleId = bundleId;
|
|
163
|
+
return snapshot;
|
|
164
|
+
}
|
|
165
|
+
// ── Link rewrite ───────────────────────────────────────────────────────────
|
|
166
|
+
const ANY_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
|
|
167
|
+
function targetAliasFor(dependencies, bundleId) {
|
|
168
|
+
for (const entry of dependencies.dependencies.values())
|
|
169
|
+
if (entry.bundle_id === bundleId)
|
|
170
|
+
return entry.alias;
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
/** Decide one link of `sourceId` (written at `targetId`). Pushes a conflict when the link cannot be carried. */
|
|
174
|
+
function planLink(ctx, sourceId, targetId, span, conflicts) {
|
|
175
|
+
const href = span.href;
|
|
176
|
+
const base = { offset: span.hrefStart, from_href: href };
|
|
177
|
+
const keep = (reason, extra = {}) => ({ ...base, to_href: href, kind: "unchanged", reason, ...extra });
|
|
178
|
+
if (isQualifiedHref(href)) {
|
|
179
|
+
if (span.image)
|
|
180
|
+
return keep("image");
|
|
181
|
+
const parsed = parseQualifiedHref(href);
|
|
182
|
+
if (!parsed.ok)
|
|
183
|
+
return keep("invalid_qualified");
|
|
184
|
+
const declared = ctx.source.dependencies.dependencies.get(parsed.ref.alias);
|
|
185
|
+
if (!declared) {
|
|
186
|
+
conflicts.push({ code: "qualified_alias_mismatch", source: sourceId, target: targetId, detail: `'${href}' uses alias '${parsed.ref.alias}', which the source bundle does not declare` });
|
|
187
|
+
return keep("already_qualified");
|
|
188
|
+
}
|
|
189
|
+
if (ctx.target.bundleId !== undefined && declared.bundle_id === ctx.target.bundleId) {
|
|
190
|
+
try {
|
|
191
|
+
const to = emitLinkHref({ kind: "local", from: targetId, to: parsed.ref.id, ...(parsed.fragment === undefined ? {} : { fragment: parsed.fragment }) });
|
|
192
|
+
return { ...base, to_href: to, kind: "dequalified" };
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
if (!(error instanceof InvalidInputError))
|
|
196
|
+
throw error;
|
|
197
|
+
return keep("not_qualifiable");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const alias = targetAliasFor(ctx.target.dependencies, declared.bundle_id);
|
|
201
|
+
if (alias === undefined) {
|
|
202
|
+
conflicts.push({ code: "qualified_alias_mismatch", source: sourceId, target: targetId, detail: `'${href}' points into '${declared.bundle_id}', which the target bundle does not declare` });
|
|
203
|
+
return keep("already_qualified");
|
|
204
|
+
}
|
|
205
|
+
if (alias === parsed.ref.alias)
|
|
206
|
+
return keep("already_qualified");
|
|
207
|
+
const to = emitLinkHref({ kind: "qualified", alias, id: parsed.ref.id, ...(parsed.fragment === undefined ? {} : { fragment: parsed.fragment }) });
|
|
208
|
+
return { ...base, to_href: to, kind: "qualified" };
|
|
209
|
+
}
|
|
210
|
+
if (ANY_SCHEME.test(href) || href.startsWith("//"))
|
|
211
|
+
return keep("external");
|
|
212
|
+
if (href.startsWith("#"))
|
|
213
|
+
return keep("anchor");
|
|
214
|
+
if (span.image)
|
|
215
|
+
return keep("image");
|
|
216
|
+
const hash = href.indexOf("#");
|
|
217
|
+
const pathPart = hash < 0 ? href : href.slice(0, hash);
|
|
218
|
+
const fragment = hash < 0 ? undefined : href.slice(hash + 1);
|
|
219
|
+
if (pathPart.includes("?"))
|
|
220
|
+
return keep("has_query");
|
|
221
|
+
const target = classifyConceptLink(sourceId, pathPart);
|
|
222
|
+
if (target.kind !== "concept")
|
|
223
|
+
return keep(target.kind === "outside_bundle" ? "escapes_source_root" : "non_document");
|
|
224
|
+
const resolved = target.id;
|
|
225
|
+
const present = ctx.source.ids.has(resolved);
|
|
226
|
+
// A spelling variant of a source id names no document under the exact-identity rule; qualifying
|
|
227
|
+
// it would send a reader to an id the source bundle refuses to serve.
|
|
228
|
+
if (!present && ctx.source.folds.has(foldPath(resolved)))
|
|
229
|
+
return keep("case_variant", { source_target: resolved });
|
|
230
|
+
const withFragment = fragment === undefined ? {} : { fragment };
|
|
231
|
+
const mapped = ctx.mapping.get(resolved);
|
|
232
|
+
if (mapped !== undefined) {
|
|
233
|
+
try {
|
|
234
|
+
const to = emitLinkHref({ kind: "local", from: targetId, to: mapped, ...withFragment });
|
|
235
|
+
return { ...base, to_href: to, kind: "local", source_target: resolved, target_present_in_source: true };
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
if (!(error instanceof InvalidInputError))
|
|
239
|
+
throw error;
|
|
240
|
+
return keep("not_qualifiable", { source_target: resolved, target_present_in_source: true });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
const to = emitLinkHref({ kind: "qualified", alias: ctx.alias, id: resolved, ...withFragment });
|
|
245
|
+
return { ...base, to_href: to, kind: "qualified", source_target: resolved, target_present_in_source: present };
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
if (!(error instanceof InvalidInputError))
|
|
249
|
+
throw error;
|
|
250
|
+
return keep("not_qualifiable", { source_target: resolved, target_present_in_source: present });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const REFERENCE_DEFINITION = /^ {0,3}\[[^\]\n]+\]:[ \t]*<?([^\s>]+)/gm;
|
|
254
|
+
/** Reference-style link definitions outside code, which v1 copies unchanged and only reports. */
|
|
255
|
+
function referenceDefinitions(body) {
|
|
256
|
+
const masked = maskMarkdownCode(body);
|
|
257
|
+
const out = [];
|
|
258
|
+
for (const match of masked.matchAll(REFERENCE_DEFINITION)) {
|
|
259
|
+
const href = match[1];
|
|
260
|
+
const offset = match.index + match[0].length - href.length;
|
|
261
|
+
out.push({ offset, href: body.slice(offset, offset + href.length) });
|
|
262
|
+
}
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
/** Rewrite every link of one body. Links inside code are reported as `in_code` and left alone. */
|
|
266
|
+
function rewriteTransferBody(ctx, sourceId, targetId, body, conflicts) {
|
|
267
|
+
const spans = extractMarkdownLinkSpans(body, { skipCode: true });
|
|
268
|
+
const live = new Set(spans.map((span) => span.hrefStart));
|
|
269
|
+
const rewrites = [];
|
|
270
|
+
for (const span of extractMarkdownLinkSpans(body)) {
|
|
271
|
+
if (!live.has(span.hrefStart)) {
|
|
272
|
+
rewrites.push({ offset: span.hrefStart, from_href: span.href, to_href: span.href, kind: "unchanged", reason: "in_code" });
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const byOffset = new Map();
|
|
276
|
+
for (const span of spans) {
|
|
277
|
+
const rewrite = planLink(ctx, sourceId, targetId, span, conflicts);
|
|
278
|
+
byOffset.set(span.hrefStart, rewrite);
|
|
279
|
+
rewrites.push(rewrite);
|
|
280
|
+
}
|
|
281
|
+
for (const definition of referenceDefinitions(body)) {
|
|
282
|
+
rewrites.push({ offset: definition.offset, from_href: definition.href, to_href: definition.href, kind: "unchanged", reason: "reference_definition" });
|
|
283
|
+
}
|
|
284
|
+
rewrites.sort((a, b) => a.offset - b.offset);
|
|
285
|
+
const next = rewriteMarkdownLinks(body, spans, (span) => {
|
|
286
|
+
const rewrite = byOffset.get(span.hrefStart);
|
|
287
|
+
return rewrite.kind === "unchanged" ? undefined : rewrite.to_href;
|
|
288
|
+
});
|
|
289
|
+
return { body: next, rewrites };
|
|
290
|
+
}
|
|
291
|
+
function emptyRewriteCounts() {
|
|
292
|
+
return { local: 0, qualified: 0, dequalified: 0, unchanged: 0 };
|
|
293
|
+
}
|
|
294
|
+
function idMentions(value, selected, out) {
|
|
295
|
+
if (typeof value === "string") {
|
|
296
|
+
if (selected.has(value))
|
|
297
|
+
out.add(value);
|
|
298
|
+
}
|
|
299
|
+
else if (Array.isArray(value)) {
|
|
300
|
+
for (const item of value)
|
|
301
|
+
idMentions(item, selected, out);
|
|
302
|
+
}
|
|
303
|
+
else if (value !== null && typeof value === "object" && !(value instanceof Date)) {
|
|
304
|
+
for (const item of Object.values(value))
|
|
305
|
+
idMentions(item, selected, out);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function sourcesEntryTitle(bundleId, version) {
|
|
309
|
+
return `Imported from ${bundleId} at ${version}`;
|
|
310
|
+
}
|
|
311
|
+
function hashOf(value) {
|
|
312
|
+
return sha256HexOfUtf8(JSON.stringify(value));
|
|
313
|
+
}
|
|
314
|
+
function defaultRecordId(alias, bundleId, prefix, ids) {
|
|
315
|
+
const digest = hashOf({ bundle_id: bundleId, prefix: prefix ?? null, ids: [...ids].sort(compareStorageKeys) }).slice(0, 12);
|
|
316
|
+
return `${PROMOTIONS_PREFIX}${alias}-${digest}`;
|
|
317
|
+
}
|
|
318
|
+
function normalizePrefix(prefix) {
|
|
319
|
+
if (prefix === undefined)
|
|
320
|
+
return undefined;
|
|
321
|
+
const trimmed = trimTrailingSlashes(prefix.trim());
|
|
322
|
+
return trimmed === "" ? undefined : trimmed;
|
|
323
|
+
}
|
|
324
|
+
/** Every planned id must be safe, absent, unique and distinct under `foldPath`. */
|
|
325
|
+
const PROTECTED_TARGET_FOLDS = new Set([...SOURCE_SELF_DOCS].map(foldPath));
|
|
326
|
+
const folderFold = (prefix) => `${foldPath(prefix.replace(/\/$/, ""))}/`;
|
|
327
|
+
/**
|
|
328
|
+
* Folders that hold only Superbee's own files whatever their type (entries with no marker type),
|
|
329
|
+
* folded. Elsewhere a document is Superbee's only under its marker, which the source and target
|
|
330
|
+
* section checks decide, so a person's document in, say, `references/` still copies.
|
|
331
|
+
*/
|
|
332
|
+
const OWNED_TARGET_FOLDS = new Map(OWNED_LOCATIONS.flatMap((owned) => owned.prefix === undefined || owned.type !== undefined ? [] : [[folderFold(owned.prefix), owned.prefix]]));
|
|
333
|
+
const CONVENTIONS_FOLD = folderFold(CONVENTIONS_PREFIX);
|
|
334
|
+
/**
|
|
335
|
+
* Throw {@link InvalidInputError} unless an import may create `id`: a safe, unreserved concept id
|
|
336
|
+
* that is not the target's own identity or dependency declaration, not a Kind convention
|
|
337
|
+
* (installing Kinds is not part of v1) and not under a folder that holds only Superbee's own
|
|
338
|
+
* files. Compared under `foldPath`, so a case or normalization variant of a protected id is
|
|
339
|
+
* refused too.
|
|
340
|
+
*/
|
|
341
|
+
function assertImportableTargetId(id, newTarget) {
|
|
342
|
+
assertSafeConceptId(id);
|
|
343
|
+
// Only a planned target is a create; a stored record's targets may predate the canonical-id rule,
|
|
344
|
+
// and any create its resume still performs meets the rule in mutateDocument.
|
|
345
|
+
if (newTarget)
|
|
346
|
+
assertCanonicalNewDocumentId(id, newTarget.exists);
|
|
347
|
+
if (isReservedFile(pathFromConceptId(id)))
|
|
348
|
+
throw new InvalidInputError("index.md and log.md are reserved files, not documents");
|
|
349
|
+
const fold = foldPath(id);
|
|
350
|
+
if (PROTECTED_TARGET_FOLDS.has(fold))
|
|
351
|
+
throw new InvalidInputError("it is the bundle's own identity or dependency declaration");
|
|
352
|
+
if (fold.startsWith(CONVENTIONS_FOLD))
|
|
353
|
+
throw new InvalidInputError("an import never installs a Kind convention");
|
|
354
|
+
for (const [ownedFold, prefix] of OWNED_TARGET_FOLDS) {
|
|
355
|
+
if (fold.startsWith(ownedFold))
|
|
356
|
+
throw new InvalidInputError(`it is under Superbee's own '${prefix}' folder`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
function checkTargetIds(planned, existing, conflicts) {
|
|
360
|
+
const folds = new Map();
|
|
361
|
+
const remember = (id) => {
|
|
362
|
+
const keys = [[`${foldPath(id)}.md`, `${id}.md`]];
|
|
363
|
+
const segments = id.split("/");
|
|
364
|
+
for (let i = 1; i < segments.length; i++) {
|
|
365
|
+
const dir = segments.slice(0, i).join("/");
|
|
366
|
+
keys.push([`${foldPath(dir)}/`, `${dir}/`]);
|
|
367
|
+
}
|
|
368
|
+
return keys;
|
|
369
|
+
};
|
|
370
|
+
for (const id of existing)
|
|
371
|
+
for (const [fold, exact] of remember(id))
|
|
372
|
+
if (!folds.has(fold))
|
|
373
|
+
folds.set(fold, exact);
|
|
374
|
+
const seen = new Set();
|
|
375
|
+
for (const row of planned) {
|
|
376
|
+
const where = { ...(row.source === undefined ? {} : { source: row.source }), target: row.target };
|
|
377
|
+
try {
|
|
378
|
+
assertImportableTargetId(row.target, { exists: (candidate) => existing.has(candidate) });
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
if (!(error instanceof InvalidInputError))
|
|
382
|
+
throw error;
|
|
383
|
+
conflicts.push({ code: "unsafe_target_id", ...where, detail: `'${row.target}' is not a usable document id: ${error.message}` });
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
if (existing.has(row.target)) {
|
|
387
|
+
conflicts.push(row.record
|
|
388
|
+
? { code: "record_exists", target: row.target, detail: `the Promotion record '${row.target}' already exists` }
|
|
389
|
+
: { code: "target_exists", ...where, detail: `'${row.target}' already exists in the target bundle; import never overwrites` });
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (seen.has(row.target)) {
|
|
393
|
+
conflicts.push({ code: "duplicate_target", ...where, detail: `two documents map to '${row.target}'` });
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
seen.add(row.target);
|
|
397
|
+
let collided = false;
|
|
398
|
+
for (const [fold, exact] of remember(row.target)) {
|
|
399
|
+
const prior = folds.get(fold);
|
|
400
|
+
if (prior !== undefined && prior !== exact && !collided) {
|
|
401
|
+
conflicts.push({ code: "case_collision", ...where, detail: `'${exact}' and '${prior}' name one file on a case- or normalization-insensitive filesystem` });
|
|
402
|
+
collided = true;
|
|
403
|
+
}
|
|
404
|
+
if (prior === undefined)
|
|
405
|
+
folds.set(fold, exact);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function recordFrontmatter(plan, prefix, state, token, targetVersions) {
|
|
410
|
+
const n = plan.entries.length;
|
|
411
|
+
const frontmatter = {
|
|
412
|
+
type: PROMOTION_TYPE,
|
|
413
|
+
title: `Import of ${n} document${n === 1 ? "" : "s"} from ${plan.from.alias}`,
|
|
414
|
+
description: `${n} document${n === 1 ? "" : "s"} from ${plan.from.bundle_id} into ${prefix ?? "the same ids"}, ${plan.counts.qualified} link${plan.counts.qualified === 1 ? "" : "s"} qualified.`,
|
|
415
|
+
promotion_state: state,
|
|
416
|
+
from_alias: plan.from.alias,
|
|
417
|
+
from_bundle_id: plan.from.bundle_id,
|
|
418
|
+
from_label: plan.from.label,
|
|
419
|
+
};
|
|
420
|
+
if (plan.from.pin)
|
|
421
|
+
frontmatter.from_pin = plan.from.pin.value;
|
|
422
|
+
if (prefix !== undefined)
|
|
423
|
+
frontmatter.target_prefix = prefix;
|
|
424
|
+
if (token !== undefined)
|
|
425
|
+
frontmatter.plan_token = token;
|
|
426
|
+
frontmatter.counts = { ...plan.counts };
|
|
427
|
+
frontmatter.left_behind = { ...plan.left_behind };
|
|
428
|
+
frontmatter.entries = plan.entries.map((entry) => {
|
|
429
|
+
const row = { source: entry.source, source_version: entry.source_version, target: entry.target };
|
|
430
|
+
const version = targetVersions.get(entry.target);
|
|
431
|
+
if (version !== undefined)
|
|
432
|
+
row.target_version = version;
|
|
433
|
+
return row;
|
|
434
|
+
});
|
|
435
|
+
return frontmatter;
|
|
436
|
+
}
|
|
437
|
+
function recordBody(recordId, plan) {
|
|
438
|
+
const lines = [
|
|
439
|
+
`Imported ${plan.entries.length} document${plan.entries.length === 1 ? "" : "s"} from the bundle ` +
|
|
440
|
+
`\`${plan.from.bundle_id}\` (alias \`${plan.from.alias}\`). This record is the journal of the import: ` +
|
|
441
|
+
"`promotion_state` stays `applying` until every document is written, and resuming this record " +
|
|
442
|
+
"finishes an interrupted import. Each line links the imported document and its origin.",
|
|
443
|
+
"",
|
|
444
|
+
];
|
|
445
|
+
for (const entry of plan.entries) {
|
|
446
|
+
const parts = [];
|
|
447
|
+
try {
|
|
448
|
+
parts.push(`[imported](${emitLinkHref({ kind: "local", from: recordId, to: entry.target })})`);
|
|
449
|
+
}
|
|
450
|
+
catch (error) {
|
|
451
|
+
if (!(error instanceof InvalidInputError))
|
|
452
|
+
throw error;
|
|
453
|
+
}
|
|
454
|
+
try {
|
|
455
|
+
parts.push(`[from](${emitLinkHref({ kind: "qualified", alias: plan.from.alias, id: entry.source })})`);
|
|
456
|
+
}
|
|
457
|
+
catch (error) {
|
|
458
|
+
if (!(error instanceof InvalidInputError))
|
|
459
|
+
throw error;
|
|
460
|
+
}
|
|
461
|
+
lines.push(`- \`${entry.source}\` ${parts.join(" ")}`.trimEnd());
|
|
462
|
+
}
|
|
463
|
+
return `${lines.join("\n")}\n`;
|
|
464
|
+
}
|
|
465
|
+
/** Rewrite, provenance and validation for one selected document. Shared by plan and resume. */
|
|
466
|
+
function buildEntry(ctx, fromBundleId, recordId, read, targetId, selected, allowUngoverned, actor, now, conflicts) {
|
|
467
|
+
const { doc, version } = read;
|
|
468
|
+
const type = String(doc.frontmatter.type ?? "");
|
|
469
|
+
const { body, rewrites } = rewriteTransferBody(ctx, doc.id, targetId, doc.body, conflicts);
|
|
470
|
+
const counts = emptyRewriteCounts();
|
|
471
|
+
for (const rewrite of rewrites)
|
|
472
|
+
counts[rewrite.kind]++;
|
|
473
|
+
const mentions = new Set();
|
|
474
|
+
idMentions(doc.frontmatter, selected, mentions);
|
|
475
|
+
const frontmatter = structuredClone(doc.frontmatter);
|
|
476
|
+
let resource;
|
|
477
|
+
try {
|
|
478
|
+
resource = qualifiedHref({ alias: ctx.alias, id: doc.id });
|
|
479
|
+
}
|
|
480
|
+
catch (error) {
|
|
481
|
+
if (!(error instanceof InvalidInputError))
|
|
482
|
+
throw error;
|
|
483
|
+
resource = `okf://${ctx.alias}/${doc.id}.md`;
|
|
484
|
+
conflicts.push({ code: "source_malformed", source: doc.id, target: targetId, detail: `'${doc.id}' cannot be named by a cross-bundle href: ${error.message}` });
|
|
485
|
+
}
|
|
486
|
+
const provenance = { resource, id: recordId, title: sourcesEntryTitle(fromBundleId, version) };
|
|
487
|
+
if (frontmatter.sources === undefined)
|
|
488
|
+
frontmatter.sources = [provenance];
|
|
489
|
+
else if (Array.isArray(frontmatter.sources))
|
|
490
|
+
frontmatter.sources = [...frontmatter.sources, provenance];
|
|
491
|
+
else
|
|
492
|
+
conflicts.push({ code: "source_malformed", source: doc.id, target: targetId, detail: "its sources field is not a list, so no provenance entry can be added" });
|
|
493
|
+
const sourceGoverned = ctx.source.registry.kinds.has(type);
|
|
494
|
+
const targetGoverned = ctx.target.registry.kinds.has(type);
|
|
495
|
+
const kindState = targetGoverned
|
|
496
|
+
? sourceGoverned ? "governed_both" : "governed_target"
|
|
497
|
+
: sourceGoverned ? allowUngoverned ? "allowed_ungoverned" : "missing" : "ungoverned_both";
|
|
498
|
+
const candidate = { frontmatter, body };
|
|
499
|
+
let warnings = [];
|
|
500
|
+
try {
|
|
501
|
+
warnings = prepareDocumentMutationCandidate(undefined, structuredClone(candidate), {
|
|
502
|
+
id: targetId,
|
|
503
|
+
registry: ctx.target.registry,
|
|
504
|
+
strict: false,
|
|
505
|
+
okfVersion: ctx.target.okfVersion,
|
|
506
|
+
actor,
|
|
507
|
+
now,
|
|
508
|
+
seedGenerationClock: false,
|
|
509
|
+
}).warnings;
|
|
510
|
+
}
|
|
511
|
+
catch (error) {
|
|
512
|
+
if (error instanceof OkfActorError)
|
|
513
|
+
throw error;
|
|
514
|
+
conflicts.push({ code: "source_malformed", source: doc.id, target: targetId, detail: `the target would refuse this document: ${error instanceof Error ? error.message : String(error)}` });
|
|
515
|
+
}
|
|
516
|
+
return {
|
|
517
|
+
entry: {
|
|
518
|
+
source: doc.id,
|
|
519
|
+
source_version: version,
|
|
520
|
+
target: targetId,
|
|
521
|
+
type,
|
|
522
|
+
rewrites,
|
|
523
|
+
rewrite_counts: counts,
|
|
524
|
+
frontmatter_id_mentions: [...mentions].sort(compareStorageKeys),
|
|
525
|
+
kind_state: kindState,
|
|
526
|
+
validation_warnings: warnings,
|
|
527
|
+
},
|
|
528
|
+
candidate,
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
async function sourceIdentity(from, target, conflicts) {
|
|
532
|
+
const { alias, binding } = from;
|
|
533
|
+
const declared = target.dependencies.dependencies.get(alias);
|
|
534
|
+
const bundleId = declared?.bundle_id ?? binding.identity ?? "";
|
|
535
|
+
if (!declared) {
|
|
536
|
+
const rejected = target.dependencies.rejected.get(alias);
|
|
537
|
+
conflicts.push({ code: "alias_undeclared", detail: rejected === undefined
|
|
538
|
+
? `alias '${alias}' is not declared in the target's docs/dependencies`
|
|
539
|
+
: `alias '${alias}' is declared in the target's docs/dependencies but rejected (${rejected})` });
|
|
540
|
+
return { bundleId };
|
|
541
|
+
}
|
|
542
|
+
if (binding.identity === undefined) {
|
|
543
|
+
conflicts.push({ code: "alias_identity", detail: `${binding.label} declares no bundle_id; '${alias}' expects '${declared.bundle_id}'` });
|
|
544
|
+
}
|
|
545
|
+
else if (binding.identity !== declared.bundle_id) {
|
|
546
|
+
conflicts.push({ code: "alias_identity", detail: `${binding.label} is '${binding.identity}', not '${declared.bundle_id}' as '${alias}' declares` });
|
|
547
|
+
}
|
|
548
|
+
const pin = declared.pin;
|
|
549
|
+
if (pin === undefined)
|
|
550
|
+
return { bundleId };
|
|
551
|
+
if (pin.kind === "digest") {
|
|
552
|
+
conflicts.push({ code: "alias_identity", detail: "a digest pin is verified only by a hosted resolver" });
|
|
553
|
+
return { bundleId };
|
|
554
|
+
}
|
|
555
|
+
const commit = { kind: "commit", value: pin.value };
|
|
556
|
+
if (!binding.pin) {
|
|
557
|
+
conflicts.push({ code: "alias_identity", detail: `no pin check is available for ${binding.label}` });
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
560
|
+
const verdict = await binding.pin(commit).catch((error) => ({ ok: false, detail: `pin check failed: ${error instanceof Error ? error.message : String(error)}` }));
|
|
561
|
+
if (!verdict.ok)
|
|
562
|
+
conflicts.push({ code: "alias_identity", detail: verdict.detail });
|
|
563
|
+
}
|
|
564
|
+
return { bundleId, pin: commit };
|
|
565
|
+
}
|
|
566
|
+
function assertActor(actor, okfVersion) {
|
|
567
|
+
if (actor !== undefined && okfVersion === "0.2" && !isOkfActor(actor)) {
|
|
568
|
+
throw new OkfActorError(actor, `OKF v0.2 mutation actor '${actor}' must be human:<id>, process:<id>, or <producer>/<version>`);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
async function computeTransfer(request) {
|
|
572
|
+
const conflicts = [];
|
|
573
|
+
const now = request.now ?? (() => new Date().toISOString());
|
|
574
|
+
const target = await readTarget(request.target);
|
|
575
|
+
assertActor(request.actor, target.okfVersion);
|
|
576
|
+
const { bundleId, pin } = await sourceIdentity(request.from, target, conflicts);
|
|
577
|
+
const source = await readSource(request.from.binding);
|
|
578
|
+
const prefix = normalizePrefix(request.prefix);
|
|
579
|
+
// Selection: the union of ids, types and prefixes. Reserved files are never listed.
|
|
580
|
+
const selected = new Set();
|
|
581
|
+
for (const id of request.select.ids ?? []) {
|
|
582
|
+
if (SOURCE_SELF_DOCS.has(id)) {
|
|
583
|
+
conflicts.push({ code: "source_missing", source: id, detail: `'${id}' describes the source bundle itself and never transfers` });
|
|
584
|
+
}
|
|
585
|
+
else if (source.malformed.has(id)) {
|
|
586
|
+
conflicts.push({ code: "source_malformed", source: id, detail: `'${id}' is malformed in the source: ${source.malformed.get(id)}` });
|
|
587
|
+
}
|
|
588
|
+
else if (!source.docs.has(id)) {
|
|
589
|
+
conflicts.push({ code: "source_missing", source: id, detail: `'${id}' is not in ${request.from.binding.label}` });
|
|
590
|
+
}
|
|
591
|
+
else if (!transfersAsDocument(id, source.docs.get(id).doc)) {
|
|
592
|
+
conflicts.push({ code: "source_missing", source: id, detail: `'${id}' is Superbee's own record, not a document, and never transfers` });
|
|
593
|
+
}
|
|
594
|
+
else {
|
|
595
|
+
selected.add(id);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
const types = new Set(request.select.types ?? []);
|
|
599
|
+
const prefixes = (request.select.prefixes ?? []).map((p) => trimTrailingSlashes(p.trim())).filter((p) => p !== "");
|
|
600
|
+
const underPrefix = (id) => prefixes.some((p) => id === p || id.startsWith(`${p}/`));
|
|
601
|
+
for (const [id, { doc }] of source.docs) {
|
|
602
|
+
if (SOURCE_SELF_DOCS.has(id) || !transfersAsDocument(id, doc))
|
|
603
|
+
continue;
|
|
604
|
+
if (types.has(String(doc.frontmatter.type)) || underPrefix(id))
|
|
605
|
+
selected.add(id);
|
|
606
|
+
}
|
|
607
|
+
// An unreadable document's type is unknowable, so a type selection cannot rule it out: report it
|
|
608
|
+
// rather than let the copy look complete. Ids already reported above are not repeated.
|
|
609
|
+
const namedIds = new Set(request.select.ids ?? []);
|
|
610
|
+
for (const [id, reason] of source.malformed) {
|
|
611
|
+
if (!SOURCE_SELF_DOCS.has(id) && (underPrefix(id) || (types.size > 0 && !namedIds.has(id)))) {
|
|
612
|
+
conflicts.push({ code: "source_malformed", source: id, detail: `'${id}' is malformed in the source: ${reason}` });
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
if (selected.size === 0 && conflicts.every((c) => c.code !== "source_missing" && c.code !== "source_malformed")) {
|
|
616
|
+
conflicts.push({ code: "selection_empty", detail: "the selection matches no source document" });
|
|
617
|
+
}
|
|
618
|
+
const selectedIds = [...selected].sort(compareStorageKeys);
|
|
619
|
+
const mapping = new Map(selectedIds.map((id) => [id, prefix === undefined ? id : `${prefix}/${id}`]));
|
|
620
|
+
const recordId = request.recordId?.trim() || defaultRecordId(request.from.alias, bundleId, prefix, selectedIds);
|
|
621
|
+
checkTargetIds([...[...mapping].map(([s, t]) => ({ source: s, target: t })), { target: recordId, record: true }], target.ids, conflicts);
|
|
622
|
+
// A document copied under a new prefix must stay a document there: its type at the target id
|
|
623
|
+
// must not turn it into one of Superbee's own records (a Connector copied under connectors/).
|
|
624
|
+
for (const [sourceId, targetId] of mapping) {
|
|
625
|
+
if (sectionOf({ id: targetId, frontmatter: source.docs.get(sourceId).doc.frontmatter }) === "documents")
|
|
626
|
+
continue;
|
|
627
|
+
conflicts.push({ code: "unsafe_target_id", source: sourceId, target: targetId, detail: `'${targetId}' is not a usable document id: at that id its type makes it Superbee's own record` });
|
|
628
|
+
}
|
|
629
|
+
for (const conflict of conflicts) {
|
|
630
|
+
if (conflict.code !== "target_exists" || conflict.target === undefined)
|
|
631
|
+
continue;
|
|
632
|
+
const promotedBy = await promotingRecord(request.target, conflict.target, request.from.alias);
|
|
633
|
+
if (promotedBy === undefined)
|
|
634
|
+
continue;
|
|
635
|
+
conflict.record = promotedBy;
|
|
636
|
+
conflict.detail += `; it is a copy promoted by '${promotedBy}', which a refresh of that record updates`;
|
|
637
|
+
}
|
|
638
|
+
const ctx = { alias: request.from.alias, source, target, mapping };
|
|
639
|
+
const built = [];
|
|
640
|
+
for (const id of selectedIds) {
|
|
641
|
+
built.push(buildEntry(ctx, bundleId, recordId, source.docs.get(id), mapping.get(id), selected, request.allowUngoverned === true, request.actor, now, conflicts));
|
|
642
|
+
}
|
|
643
|
+
built.sort((a, b) => compareStorageKeys(a.entry.target, b.entry.target));
|
|
644
|
+
const missingKinds = new Map();
|
|
645
|
+
for (const { entry } of built)
|
|
646
|
+
if (entry.kind_state === "missing")
|
|
647
|
+
missingKinds.set(entry.type, (missingKinds.get(entry.type) ?? 0) + 1);
|
|
648
|
+
for (const [type, count] of missingKinds) {
|
|
649
|
+
conflicts.push({ code: "kind_missing", detail: `'${type}' is governed in the source but not in the target (${count} selected document${count === 1 ? "" : "s"}); declare the Kind in the target or allow ungoverned documents` });
|
|
650
|
+
}
|
|
651
|
+
const leftBehind = {};
|
|
652
|
+
for (const [id, { doc }] of [...source.docs].sort(([a], [b]) => compareStorageKeys(a, b))) {
|
|
653
|
+
if (selected.has(id) || SOURCE_SELF_DOCS.has(id))
|
|
654
|
+
continue;
|
|
655
|
+
const type = String(doc.frontmatter.type);
|
|
656
|
+
leftBehind[type] = (leftBehind[type] ?? 0) + 1;
|
|
657
|
+
}
|
|
658
|
+
const counts = { documents: built.length, local: 0, qualified: 0, dequalified: 0, unchanged: 0, dangling_in_source: 0 };
|
|
659
|
+
for (const { entry } of built) {
|
|
660
|
+
for (const rewrite of entry.rewrites) {
|
|
661
|
+
counts[rewrite.kind]++;
|
|
662
|
+
if (rewrite.kind === "qualified" && rewrite.target_present_in_source === false)
|
|
663
|
+
counts.dangling_in_source++;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
const from = { alias: request.from.alias, bundle_id: bundleId, label: request.from.label ?? request.from.binding.label };
|
|
667
|
+
if (pin)
|
|
668
|
+
from.pin = pin;
|
|
669
|
+
const targetFacts = { okf_version: target.okfVersion, dependencies_version: target.dependenciesVersion };
|
|
670
|
+
if (target.bundleId !== undefined)
|
|
671
|
+
targetFacts.bundle_id = target.bundleId;
|
|
672
|
+
// Every governing convention the selection's types touch, on both sides.
|
|
673
|
+
const conventionVersions = [];
|
|
674
|
+
for (const type of [...new Set(built.map((b) => b.entry.type))].sort(compareStorageKeys)) {
|
|
675
|
+
const sourceKind = source.registry.kinds.get(type);
|
|
676
|
+
const targetKind = target.registry.kinds.get(type);
|
|
677
|
+
conventionVersions.push({
|
|
678
|
+
type,
|
|
679
|
+
source: sourceKind ? (source.docs.get(sourceKind.id)?.version ?? null) : null,
|
|
680
|
+
target: targetKind ? ((await readOptional(request.target, targetKind.id))?.version ?? null) : null,
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
const entries = built.map((b) => b.entry);
|
|
684
|
+
const ready = conflicts.length === 0;
|
|
685
|
+
const partial = { from, counts, left_behind: leftBehind, entries };
|
|
686
|
+
let token;
|
|
687
|
+
if (ready) {
|
|
688
|
+
token = `${DOCUMENT_TRANSFER_CONTRACT}:sha256:${hashOf({
|
|
689
|
+
contract: DOCUMENT_TRANSFER_CONTRACT,
|
|
690
|
+
from: { bundle_id: bundleId, pin: pin?.value ?? null },
|
|
691
|
+
target: { okf_version: target.okfVersion, bundle_id: target.bundleId ?? null, dependencies_version: target.dependenciesVersion },
|
|
692
|
+
record: recordId,
|
|
693
|
+
prefix: prefix ?? null,
|
|
694
|
+
allow_ungoverned: request.allowUngoverned === true,
|
|
695
|
+
conventions: conventionVersions,
|
|
696
|
+
entries: built.map((b) => ({ source: b.entry.source, source_version: b.entry.source_version, target: b.entry.target, candidate: hashOf(b.candidate) })),
|
|
697
|
+
})}`;
|
|
698
|
+
}
|
|
699
|
+
const plan = {
|
|
700
|
+
contract: DOCUMENT_TRANSFER_CONTRACT,
|
|
701
|
+
from,
|
|
702
|
+
target: targetFacts,
|
|
703
|
+
entries,
|
|
704
|
+
record: { id: recordId, frontmatter: recordFrontmatter(partial, prefix, "applying", token, new Map()), body: recordBody(recordId, partial) },
|
|
705
|
+
left_behind: leftBehind,
|
|
706
|
+
counts,
|
|
707
|
+
conflicts,
|
|
708
|
+
ready,
|
|
709
|
+
};
|
|
710
|
+
if (token !== undefined)
|
|
711
|
+
plan.plan_token = token;
|
|
712
|
+
return { plan, candidates: new Map(built.map((b) => [b.entry.source, b.candidate])) };
|
|
713
|
+
}
|
|
714
|
+
/** Read-only: compute what an import would write, its conflicts and, when it has none, its plan token. */
|
|
715
|
+
export async function planDocumentTransfer(request) {
|
|
716
|
+
return (await computeTransfer(request)).plan;
|
|
717
|
+
}
|
|
718
|
+
// ── Writes ─────────────────────────────────────────────────────────────────
|
|
719
|
+
async function writeRecordState(target, registry, recordId, expectedVersion, state, versions, actor, now, edited = new Set()) {
|
|
720
|
+
const result = await mutateDocument({
|
|
721
|
+
bundle: target,
|
|
722
|
+
id: recordId,
|
|
723
|
+
mode: "replace-document",
|
|
724
|
+
onAbsent: "fail",
|
|
725
|
+
expectedVersion,
|
|
726
|
+
registry,
|
|
727
|
+
strict: false,
|
|
728
|
+
actor,
|
|
729
|
+
...(now ? { now } : {}),
|
|
730
|
+
buildCandidate: (existing) => {
|
|
731
|
+
const frontmatter = structuredClone(existing.frontmatter);
|
|
732
|
+
frontmatter.promotion_state = state;
|
|
733
|
+
if (Array.isArray(frontmatter.entries)) {
|
|
734
|
+
frontmatter.entries = frontmatter.entries.map((row) => {
|
|
735
|
+
if (row === null || typeof row !== "object")
|
|
736
|
+
return row;
|
|
737
|
+
const entry = { ...row };
|
|
738
|
+
const version = versions.get(String(entry.target));
|
|
739
|
+
if (version !== undefined)
|
|
740
|
+
entry.target_version = version;
|
|
741
|
+
if (edited.has(String(entry.target)))
|
|
742
|
+
entry.target_edited = true;
|
|
743
|
+
return entry;
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
return { frontmatter, body: existing.body };
|
|
747
|
+
},
|
|
748
|
+
});
|
|
749
|
+
return result.version;
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Recompute the plan from fresh reads, refuse unless it is ready and its token equals
|
|
753
|
+
* `planToken`, then write: the record (`applying`), each document create-only in plan order, and
|
|
754
|
+
* the record again (`applied`). A failed document write throws {@link TransferInterrupted} and
|
|
755
|
+
* leaves the record at `applying`; nothing is deleted or overwritten.
|
|
756
|
+
*/
|
|
757
|
+
export async function applyDocumentTransfer(request, planToken) {
|
|
758
|
+
const { plan, candidates } = await computeTransfer(request);
|
|
759
|
+
if (!plan.ready) {
|
|
760
|
+
// Idempotence: the same token applied again after it completed is a no-op success.
|
|
761
|
+
const prior = plan.conflicts.some((c) => c.code === "record_exists") ? await readOptional(request.target, plan.record.id) : null;
|
|
762
|
+
if (prior && prior.doc.frontmatter.type === PROMOTION_TYPE && prior.doc.frontmatter.plan_token === planToken.trim()) {
|
|
763
|
+
const record = parseRecord(plan.record.id, prior.doc);
|
|
764
|
+
if (record.state === "applied")
|
|
765
|
+
return appliedResult(plan.record.id, prior.version, record.entries, false);
|
|
766
|
+
throw new TransferConflictError([{ code: "record_exists", target: plan.record.id, detail: `'${plan.record.id}' from this plan is still ${record.state}; resume it to finish` }]);
|
|
767
|
+
}
|
|
768
|
+
throw new TransferConflictError(plan.conflicts);
|
|
769
|
+
}
|
|
770
|
+
if (plan.plan_token !== planToken.trim())
|
|
771
|
+
throw new TransferPlanChanged(planToken.trim(), plan.plan_token);
|
|
772
|
+
const registry = await loadKinds(request.target);
|
|
773
|
+
const recordId = plan.record.id;
|
|
774
|
+
let recordVersion;
|
|
775
|
+
try {
|
|
776
|
+
recordVersion = (await mutateDocument({
|
|
777
|
+
bundle: request.target,
|
|
778
|
+
id: recordId,
|
|
779
|
+
mode: "create-only",
|
|
780
|
+
registry,
|
|
781
|
+
strict: false,
|
|
782
|
+
actor: request.actor,
|
|
783
|
+
...(request.now ? { now: request.now } : {}),
|
|
784
|
+
buildCandidate: () => ({ frontmatter: structuredClone(plan.record.frontmatter), body: plan.record.body }),
|
|
785
|
+
})).version;
|
|
786
|
+
}
|
|
787
|
+
catch (error) {
|
|
788
|
+
if (error instanceof VersionConflict) {
|
|
789
|
+
throw new TransferConflictError([{ code: "record_exists", target: recordId, detail: `the Promotion record '${recordId}' was created by another writer` }]);
|
|
790
|
+
}
|
|
791
|
+
throw error;
|
|
792
|
+
}
|
|
793
|
+
const written = [];
|
|
794
|
+
const versions = new Map();
|
|
795
|
+
for (let index = 0; index < plan.entries.length; index++) {
|
|
796
|
+
const entry = plan.entries[index];
|
|
797
|
+
try {
|
|
798
|
+
const result = await mutateDocument({
|
|
799
|
+
bundle: request.target,
|
|
800
|
+
id: entry.target,
|
|
801
|
+
mode: "create-only",
|
|
802
|
+
registry,
|
|
803
|
+
strict: false,
|
|
804
|
+
actor: request.actor,
|
|
805
|
+
seedGenerationClock: false,
|
|
806
|
+
...(request.now ? { now: request.now } : {}),
|
|
807
|
+
buildCandidate: () => structuredClone(candidates.get(entry.source)),
|
|
808
|
+
});
|
|
809
|
+
written.push({ source: entry.source, target: entry.target, version: result.version });
|
|
810
|
+
versions.set(entry.target, result.version);
|
|
811
|
+
}
|
|
812
|
+
catch (cause) {
|
|
813
|
+
// Best effort: record the versions written so far; the record stays `applying` either way.
|
|
814
|
+
await writeRecordState(request.target, registry, recordId, recordVersion, "applying", versions, request.actor, request.now).catch(() => undefined);
|
|
815
|
+
throw new TransferInterrupted(recordId, written, plan.entries.slice(index).map((e) => ({ source: e.source, target: e.target })), cause);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
try {
|
|
819
|
+
recordVersion = await writeRecordState(request.target, registry, recordId, recordVersion, "applied", versions, request.actor, request.now);
|
|
820
|
+
}
|
|
821
|
+
catch (cause) {
|
|
822
|
+
throw new TransferInterrupted(recordId, written, [], cause);
|
|
823
|
+
}
|
|
824
|
+
return { record: { id: recordId, version: recordVersion, state: "applied" }, written, changed: true };
|
|
825
|
+
}
|
|
826
|
+
function parseRecord(recordId, doc, verb = "resume") {
|
|
827
|
+
const fm = doc.frontmatter;
|
|
828
|
+
const refuse = (detail) => { throw new TransferResumeConflict(recordId, detail, { verb }); };
|
|
829
|
+
if (String(fm.type).trim() !== PROMOTION_TYPE)
|
|
830
|
+
refuse(`it is not a ${PROMOTION_TYPE} record (type '${String(fm.type)}')`);
|
|
831
|
+
const state = fm.promotion_state;
|
|
832
|
+
if (state !== "applying" && state !== "applied" && state !== "refreshing")
|
|
833
|
+
refuse("promotion_state must be applying, applied or refreshing");
|
|
834
|
+
if (typeof fm.from_alias !== "string" || typeof fm.from_bundle_id !== "string")
|
|
835
|
+
refuse("from_alias and from_bundle_id are required");
|
|
836
|
+
if (!Array.isArray(fm.entries))
|
|
837
|
+
refuse("entries must be a list");
|
|
838
|
+
if (fm.target_prefix !== undefined && typeof fm.target_prefix !== "string")
|
|
839
|
+
refuse("target_prefix must be a string");
|
|
840
|
+
const prefix = normalizePrefix(fm.target_prefix);
|
|
841
|
+
const entries = [];
|
|
842
|
+
for (const row of fm.entries) {
|
|
843
|
+
const r = row;
|
|
844
|
+
if (!r || typeof r.source !== "string" || typeof r.source_version !== "string" || typeof r.target !== "string") {
|
|
845
|
+
refuse("every entry needs source, source_version and target");
|
|
846
|
+
}
|
|
847
|
+
const entry = { source: r.source, source_version: r.source_version, target: r.target };
|
|
848
|
+
if (typeof r.target_version === "string")
|
|
849
|
+
entry.target_version = r.target_version;
|
|
850
|
+
if (r.target_edited === true)
|
|
851
|
+
entry.target_edited = true;
|
|
852
|
+
// The record is a document anyone can edit: every target is recomputed from its source and
|
|
853
|
+
// the record's prefix, and must pass the same rule a plan applies, before resume trusts it.
|
|
854
|
+
if (SOURCE_SELF_DOCS.has(entry.source))
|
|
855
|
+
refuse(`'${entry.source}' describes the source bundle itself and never transfers`);
|
|
856
|
+
const expected = prefix === undefined ? entry.source : `${prefix}/${entry.source}`;
|
|
857
|
+
if (entry.target !== expected)
|
|
858
|
+
refuse(`entry '${entry.source}' targets '${entry.target}', not '${expected}' as the record's prefix requires`);
|
|
859
|
+
try {
|
|
860
|
+
assertSafeConceptId(entry.source);
|
|
861
|
+
assertImportableTargetId(entry.target);
|
|
862
|
+
}
|
|
863
|
+
catch (error) {
|
|
864
|
+
if (!(error instanceof InvalidInputError))
|
|
865
|
+
throw error;
|
|
866
|
+
refuse(`'${entry.target}' is not a target an import may write: ${error.message}`);
|
|
867
|
+
}
|
|
868
|
+
entries.push(entry);
|
|
869
|
+
}
|
|
870
|
+
const parsed = { state: state, alias: fm.from_alias, bundleId: fm.from_bundle_id, ...(prefix === undefined ? {} : { prefix }), entries };
|
|
871
|
+
if (state === "refreshing") {
|
|
872
|
+
const journal = fm.refresh;
|
|
873
|
+
if (!journal || typeof journal !== "object" || !Array.isArray(journal.entries))
|
|
874
|
+
refuse("a refreshing record needs refresh.entries");
|
|
875
|
+
const byTarget = new Map(entries.map((e) => [e.target, e]));
|
|
876
|
+
parsed.refresh = [];
|
|
877
|
+
for (const row of journal.entries) {
|
|
878
|
+
const r = row;
|
|
879
|
+
if (!r || typeof r.source !== "string" || typeof r.source_version !== "string" || typeof r.target !== "string" || typeof r.replaces_version !== "string") {
|
|
880
|
+
refuse("every refresh entry needs source, source_version, target and replaces_version");
|
|
881
|
+
}
|
|
882
|
+
// A pending row may only name an entry the record already holds, with the same source.
|
|
883
|
+
if (byTarget.get(r.target)?.source !== r.source)
|
|
884
|
+
refuse(`refresh entry '${String(r.target)}' is not an entry of this record`);
|
|
885
|
+
parsed.refresh.push({ source: r.source, source_version: r.source_version, target: r.target, replaces_version: r.replaces_version });
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
return parsed;
|
|
889
|
+
}
|
|
890
|
+
/** The result an already-applied record stands for: nothing is written again. */
|
|
891
|
+
function appliedResult(recordId, version, entries, changed) {
|
|
892
|
+
const written = [];
|
|
893
|
+
for (const entry of entries) {
|
|
894
|
+
if (entry.target_version === undefined)
|
|
895
|
+
throw new TransferResumeConflict(recordId, `it is applied but '${entry.target}' has no target_version`);
|
|
896
|
+
written.push({ source: entry.source, target: entry.target, version: entry.target_version });
|
|
897
|
+
}
|
|
898
|
+
return { record: { id: recordId, version, state: "applied" }, written, changed };
|
|
899
|
+
}
|
|
900
|
+
function provenanceEntries(doc) {
|
|
901
|
+
const sources = doc.frontmatter.sources;
|
|
902
|
+
return Array.isArray(sources) ? sources.filter((s) => s !== null && typeof s === "object" && !Array.isArray(s)) : [];
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* True when `doc` is what writing `candidate` over it would leave: the mutation service's own no-op
|
|
906
|
+
* test, attributed to whichever actor the copy names (the writer may not be the resuming actor).
|
|
907
|
+
*/
|
|
908
|
+
function isWrittenCopy(doc, candidate, registry, okfVersion) {
|
|
909
|
+
const attributed = doc.frontmatter[okfVersion === "0.1" ? "actor" : SUPERBEE_UPDATED_BY_FIELD];
|
|
910
|
+
try {
|
|
911
|
+
// Overwrite mode compares after attribution, so a v0.1 `actor` the writer stamped matches too.
|
|
912
|
+
return !prepareDocumentMutationCandidate(doc, structuredClone(candidate), {
|
|
913
|
+
mode: "overwrite",
|
|
914
|
+
id: doc.id,
|
|
915
|
+
registry,
|
|
916
|
+
strict: false,
|
|
917
|
+
okfVersion,
|
|
918
|
+
...(typeof attributed === "string" ? { actor: attributed } : {}),
|
|
919
|
+
seedGenerationClock: false,
|
|
920
|
+
}).changed;
|
|
921
|
+
}
|
|
922
|
+
catch {
|
|
923
|
+
return false;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
function hasProvenance(doc, recordId) {
|
|
927
|
+
return provenanceEntries(doc).some((s) => s.id === recordId);
|
|
928
|
+
}
|
|
929
|
+
/** The Promotion record an existing copy at `id` names as its import from `alias`, if any. */
|
|
930
|
+
async function promotingRecord(target, id, alias) {
|
|
931
|
+
const read = await readOptional(target, id).catch(() => null);
|
|
932
|
+
if (!read)
|
|
933
|
+
return undefined;
|
|
934
|
+
for (const entry of provenanceEntries(read.doc)) {
|
|
935
|
+
if (typeof entry.id !== "string" || !entry.id.startsWith(PROMOTIONS_PREFIX))
|
|
936
|
+
continue;
|
|
937
|
+
if (typeof entry.resource !== "string" || !entry.resource.startsWith(`okf://${alias}/`))
|
|
938
|
+
continue;
|
|
939
|
+
const record = await readOptional(target, entry.id).catch(() => null);
|
|
940
|
+
if (record && String(record.doc.frontmatter.type).trim() === PROMOTION_TYPE)
|
|
941
|
+
return entry.id;
|
|
942
|
+
}
|
|
943
|
+
return undefined;
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* Finish an `applying` Promotion record. For each entry: an absent target is created from the
|
|
947
|
+
* source at the recorded source version (a changed source refuses); a present target carrying a
|
|
948
|
+
* `sources` entry with this record's id is already done; anything else refuses. When every entry
|
|
949
|
+
* is done the record becomes `applied`. Running it again on an `applied` record is a no-op.
|
|
950
|
+
* A `refreshing` record is finished the same way by {@link finishRefresh}.
|
|
951
|
+
*/
|
|
952
|
+
export async function resumeDocumentTransfer(target, recordId, from, options = {}) {
|
|
953
|
+
const read = await readOptional(target, recordId);
|
|
954
|
+
if (!read)
|
|
955
|
+
throw new DocumentNotFoundError(recordId);
|
|
956
|
+
const record = parseRecord(recordId, read.doc);
|
|
957
|
+
if (record.alias !== from.alias)
|
|
958
|
+
throw new TransferResumeConflict(recordId, `it was written for alias '${record.alias}', not '${from.alias}'`);
|
|
959
|
+
if (from.binding.identity !== record.bundleId) {
|
|
960
|
+
throw new TransferResumeConflict(recordId, `${from.binding.label} is '${from.binding.identity ?? "undeclared"}', not '${record.bundleId}'`);
|
|
961
|
+
}
|
|
962
|
+
if (record.state === "applied")
|
|
963
|
+
return appliedResult(recordId, read.version, record.entries, false);
|
|
964
|
+
if (record.state === "refreshing")
|
|
965
|
+
return finishRefresh(target, recordId, read, record, from, options);
|
|
966
|
+
const snapshot = await readTarget(target);
|
|
967
|
+
assertActor(options.actor, snapshot.okfVersion);
|
|
968
|
+
// The same alias and identity check a plan runs: the target's declaration may have moved since.
|
|
969
|
+
const identity = [];
|
|
970
|
+
await sourceIdentity(from, snapshot, identity);
|
|
971
|
+
if (identity.length > 0)
|
|
972
|
+
throw new TransferResumeConflict(recordId, `${identity[0].code}: ${identity[0].detail}`);
|
|
973
|
+
const source = await readSource(from.binding);
|
|
974
|
+
const mapping = new Map(record.entries.map((e) => [e.source, e.target]));
|
|
975
|
+
const selected = new Set(mapping.keys());
|
|
976
|
+
const ctx = { alias: from.alias, source, target: snapshot, mapping };
|
|
977
|
+
const now = options.now ?? (() => new Date().toISOString());
|
|
978
|
+
const registry = snapshot.registry;
|
|
979
|
+
const written = [];
|
|
980
|
+
const versions = new Map();
|
|
981
|
+
const edited = new Set();
|
|
982
|
+
for (const entry of record.entries) {
|
|
983
|
+
let existing;
|
|
984
|
+
try {
|
|
985
|
+
existing = await readOptional(target, entry.target);
|
|
986
|
+
}
|
|
987
|
+
catch (error) {
|
|
988
|
+
if (error instanceof FilesystemIdentityAliasError) {
|
|
989
|
+
throw new TransferResumeConflict(recordId, `'${entry.target}' is spelled differently on disk: ${error.message}`, { id: entry.target, written });
|
|
990
|
+
}
|
|
991
|
+
throw error;
|
|
992
|
+
}
|
|
993
|
+
if (existing) {
|
|
994
|
+
if (!hasProvenance(existing.doc, recordId)) {
|
|
995
|
+
throw new TransferResumeConflict(recordId, `'${entry.target}' exists and was not written by this import`, { id: entry.target, written });
|
|
996
|
+
}
|
|
997
|
+
written.push({ source: entry.source, target: entry.target, version: existing.version });
|
|
998
|
+
versions.set(entry.target, existing.version);
|
|
999
|
+
// A copy edited after the interrupted write still carries the provenance; it is adopted, but
|
|
1000
|
+
// flagged so a refresh treats it as edited instead of as the import's own baseline.
|
|
1001
|
+
const sourceRead = source.docs.get(entry.source);
|
|
1002
|
+
const conflicts = [];
|
|
1003
|
+
const built = sourceRead?.version === entry.source_version
|
|
1004
|
+
? buildEntry(ctx, record.bundleId, recordId, sourceRead, entry.target, selected, true, options.actor, now, conflicts)
|
|
1005
|
+
: undefined;
|
|
1006
|
+
if (!built || conflicts.length > 0 || !isWrittenCopy(existing.doc, built.candidate, registry, snapshot.okfVersion))
|
|
1007
|
+
edited.add(entry.target);
|
|
1008
|
+
continue;
|
|
1009
|
+
}
|
|
1010
|
+
const sourceDoc = source.docs.get(entry.source);
|
|
1011
|
+
if (!sourceDoc || sourceDoc.version !== entry.source_version) {
|
|
1012
|
+
throw new TransferResumeConflict(recordId, `the source '${entry.source}' is ${sourceDoc ? "changed" : "missing"} since the import was planned`, { id: entry.target, written });
|
|
1013
|
+
}
|
|
1014
|
+
const conflicts = [];
|
|
1015
|
+
const built = buildEntry(ctx, record.bundleId, recordId, sourceDoc, entry.target, selected, true, options.actor, now, conflicts);
|
|
1016
|
+
if (conflicts.length > 0)
|
|
1017
|
+
throw new TransferResumeConflict(recordId, `${conflicts[0].code}: ${conflicts[0].detail}`, { id: entry.target, written });
|
|
1018
|
+
try {
|
|
1019
|
+
const result = await mutateDocument({
|
|
1020
|
+
bundle: target,
|
|
1021
|
+
id: entry.target,
|
|
1022
|
+
mode: "create-only",
|
|
1023
|
+
registry,
|
|
1024
|
+
strict: false,
|
|
1025
|
+
actor: options.actor,
|
|
1026
|
+
seedGenerationClock: false,
|
|
1027
|
+
...(options.now ? { now: options.now } : {}),
|
|
1028
|
+
buildCandidate: () => built.candidate,
|
|
1029
|
+
});
|
|
1030
|
+
written.push({ source: entry.source, target: entry.target, version: result.version });
|
|
1031
|
+
versions.set(entry.target, result.version);
|
|
1032
|
+
}
|
|
1033
|
+
catch (error) {
|
|
1034
|
+
if (error instanceof VersionConflict) {
|
|
1035
|
+
throw new TransferResumeConflict(recordId, `'${entry.target}' was created by another writer during the resume`, { id: entry.target, written });
|
|
1036
|
+
}
|
|
1037
|
+
if (error instanceof FilesystemIdentityAliasError) {
|
|
1038
|
+
throw new TransferResumeConflict(recordId, `'${entry.target}' is spelled differently on disk: ${error.message}`, { id: entry.target, written });
|
|
1039
|
+
}
|
|
1040
|
+
throw error;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
const version = await writeRecordState(target, registry, recordId, read.version, "applied", versions, options.actor, options.now, edited);
|
|
1044
|
+
return { record: { id: recordId, version, state: "applied" }, written, changed: true };
|
|
1045
|
+
}
|
|
1046
|
+
// ── Refresh ────────────────────────────────────────────────────────────────
|
|
1047
|
+
export const DOCUMENT_REFRESH_TOKEN_PREFIX = `${DOCUMENT_TRANSFER_CONTRACT}:refresh`;
|
|
1048
|
+
function matchesEntry(names, entry) {
|
|
1049
|
+
return names.has(entry.target) || names.has(entry.source);
|
|
1050
|
+
}
|
|
1051
|
+
async function computeRefresh(request) {
|
|
1052
|
+
const conflicts = [];
|
|
1053
|
+
const now = request.now ?? (() => new Date().toISOString());
|
|
1054
|
+
const read = await readOptional(request.target, request.recordId);
|
|
1055
|
+
if (!read)
|
|
1056
|
+
throw new DocumentNotFoundError(request.recordId);
|
|
1057
|
+
const record = parseRecord(request.recordId, read.doc, "refresh");
|
|
1058
|
+
const target = await readTarget(request.target);
|
|
1059
|
+
assertActor(request.actor, target.okfVersion);
|
|
1060
|
+
if (record.alias !== request.from.alias) {
|
|
1061
|
+
conflicts.push({ code: "alias_identity", detail: `'${request.recordId}' was written for alias '${record.alias}', not '${request.from.alias}'` });
|
|
1062
|
+
}
|
|
1063
|
+
const identity = await sourceIdentity(request.from, target, conflicts);
|
|
1064
|
+
if (identity.bundleId !== record.bundleId) {
|
|
1065
|
+
conflicts.push({ code: "alias_identity", detail: `'${request.recordId}' imported from '${record.bundleId}', but '${request.from.alias}' is '${identity.bundleId}'` });
|
|
1066
|
+
}
|
|
1067
|
+
if (record.state !== "applied") {
|
|
1068
|
+
conflicts.push({ code: "record_state", detail: `'${request.recordId}' is ${record.state}; resume it before a refresh` });
|
|
1069
|
+
}
|
|
1070
|
+
const overwrite = new Set(request.overwrite ?? []);
|
|
1071
|
+
const skip = new Set(request.skip ?? []);
|
|
1072
|
+
for (const name of [...overwrite, ...skip]) {
|
|
1073
|
+
if (!record.entries.some((e) => e.target === name || e.source === name)) {
|
|
1074
|
+
conflicts.push({ code: "not_in_record", target: name, detail: `'${name}' is not a copy '${request.recordId}' names` });
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
for (const name of overwrite) {
|
|
1078
|
+
if (skip.has(name))
|
|
1079
|
+
conflicts.push({ code: "not_in_record", target: name, detail: `'${name}' is named both to overwrite and to skip` });
|
|
1080
|
+
}
|
|
1081
|
+
const source = await readSource(request.from.binding);
|
|
1082
|
+
const mapping = new Map(record.entries.map((e) => [e.source, e.target]));
|
|
1083
|
+
const selected = new Set(mapping.keys());
|
|
1084
|
+
const ctx = { alias: request.from.alias, source, target, mapping };
|
|
1085
|
+
const candidates = new Map();
|
|
1086
|
+
const entries = [];
|
|
1087
|
+
for (const entry of record.entries) {
|
|
1088
|
+
let current;
|
|
1089
|
+
try {
|
|
1090
|
+
current = await readOptional(request.target, entry.target);
|
|
1091
|
+
}
|
|
1092
|
+
catch (error) {
|
|
1093
|
+
if (!(error instanceof FilesystemIdentityAliasError))
|
|
1094
|
+
throw error;
|
|
1095
|
+
current = null;
|
|
1096
|
+
conflicts.push({ code: "case_collision", source: entry.source, target: entry.target, detail: `'${entry.target}' is spelled differently on disk: ${error.message}` });
|
|
1097
|
+
}
|
|
1098
|
+
const sourceRead = source.docs.get(entry.source);
|
|
1099
|
+
const row = {
|
|
1100
|
+
source: entry.source,
|
|
1101
|
+
target: entry.target,
|
|
1102
|
+
action: "unchanged",
|
|
1103
|
+
recorded_source_version: entry.source_version,
|
|
1104
|
+
target_version: current?.version ?? null,
|
|
1105
|
+
edited: current !== null && (current.version !== entry.target_version || entry.target_edited === true),
|
|
1106
|
+
dropped_links: 0,
|
|
1107
|
+
};
|
|
1108
|
+
if (entry.target_version !== undefined)
|
|
1109
|
+
row.recorded_target_version = entry.target_version;
|
|
1110
|
+
if (sourceRead)
|
|
1111
|
+
row.source_version = sourceRead.version;
|
|
1112
|
+
entries.push(row);
|
|
1113
|
+
const where = { source: entry.source, target: entry.target };
|
|
1114
|
+
const block = (code, detail) => {
|
|
1115
|
+
row.action = "conflict";
|
|
1116
|
+
row.detail = detail;
|
|
1117
|
+
conflicts.push({ code, ...where, detail });
|
|
1118
|
+
};
|
|
1119
|
+
if (matchesEntry(skip, entry)) {
|
|
1120
|
+
row.action = "skip";
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
if (entry.target_version === undefined) {
|
|
1124
|
+
block("record_state", `the record holds no target_version for '${entry.target}', so a local edit cannot be told apart`);
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
if (source.malformed.has(entry.source)) {
|
|
1128
|
+
block("source_malformed", `'${entry.source}' is malformed in the source: ${source.malformed.get(entry.source)}`);
|
|
1129
|
+
continue;
|
|
1130
|
+
}
|
|
1131
|
+
const sourceChanged = sourceRead?.version !== entry.source_version;
|
|
1132
|
+
if (!sourceRead) {
|
|
1133
|
+
block("source_missing", `'${entry.source}' is no longer in ${request.from.binding.label}; a refresh never deletes the copy`);
|
|
1134
|
+
continue;
|
|
1135
|
+
}
|
|
1136
|
+
if (current === null) {
|
|
1137
|
+
if (sourceChanged)
|
|
1138
|
+
block("target_missing", `'${entry.target}' is gone from the target; a refresh never recreates it`);
|
|
1139
|
+
else
|
|
1140
|
+
row.detail = "the copy is gone from the target; its source has not changed";
|
|
1141
|
+
continue;
|
|
1142
|
+
}
|
|
1143
|
+
const overwriting = row.edited && matchesEntry(overwrite, entry);
|
|
1144
|
+
if (!sourceChanged && !overwriting) {
|
|
1145
|
+
if (row.edited)
|
|
1146
|
+
row.detail = "the copy was edited in the target; its source has not changed";
|
|
1147
|
+
continue;
|
|
1148
|
+
}
|
|
1149
|
+
if (row.edited && !overwriting) {
|
|
1150
|
+
block("target_edited", `'${entry.target}' was edited in the target since '${request.recordId}' wrote it; overwrite or skip it explicitly`);
|
|
1151
|
+
continue;
|
|
1152
|
+
}
|
|
1153
|
+
const built = buildEntry(ctx, record.bundleId, request.recordId, sourceRead, entry.target, selected, true, request.actor, now, conflicts);
|
|
1154
|
+
candidates.set(entry.source, built.candidate);
|
|
1155
|
+
row.action = overwriting ? "overwrite" : "update";
|
|
1156
|
+
row.dropped_links = computeDroppedLinks(outboundLinks(current.doc), outboundLinks({ ...current.doc, body: built.candidate.body })).length;
|
|
1157
|
+
}
|
|
1158
|
+
const counts = { update: 0, overwrite: 0, unchanged: 0, skip: 0, conflict: 0 };
|
|
1159
|
+
for (const row of entries)
|
|
1160
|
+
counts[row.action]++;
|
|
1161
|
+
const from = { alias: request.from.alias, bundle_id: identity.bundleId, label: request.from.label ?? request.from.binding.label };
|
|
1162
|
+
if (identity.pin)
|
|
1163
|
+
from.pin = identity.pin;
|
|
1164
|
+
const ready = conflicts.length === 0;
|
|
1165
|
+
const plan = {
|
|
1166
|
+
contract: DOCUMENT_TRANSFER_CONTRACT,
|
|
1167
|
+
record: { id: request.recordId, version: read.version, state: record.state },
|
|
1168
|
+
from,
|
|
1169
|
+
entries,
|
|
1170
|
+
counts,
|
|
1171
|
+
conflicts,
|
|
1172
|
+
ready,
|
|
1173
|
+
};
|
|
1174
|
+
if (ready && counts.update + counts.overwrite > 0) {
|
|
1175
|
+
plan.plan_token = `${DOCUMENT_REFRESH_TOKEN_PREFIX}:sha256:${hashOf({
|
|
1176
|
+
contract: DOCUMENT_REFRESH_TOKEN_PREFIX,
|
|
1177
|
+
record: { id: request.recordId, version: read.version },
|
|
1178
|
+
from: { bundle_id: identity.bundleId, pin: identity.pin?.value ?? null },
|
|
1179
|
+
target: { okf_version: target.okfVersion, bundle_id: target.bundleId ?? null, dependencies_version: target.dependenciesVersion },
|
|
1180
|
+
entries: entries.map((row) => ({
|
|
1181
|
+
source: row.source,
|
|
1182
|
+
target: row.target,
|
|
1183
|
+
action: row.action,
|
|
1184
|
+
source_version: row.source_version ?? null,
|
|
1185
|
+
target_version: row.target_version,
|
|
1186
|
+
candidate: candidates.has(row.source) ? hashOf(candidates.get(row.source)) : null,
|
|
1187
|
+
})),
|
|
1188
|
+
})}`;
|
|
1189
|
+
}
|
|
1190
|
+
return { plan, candidates, read };
|
|
1191
|
+
}
|
|
1192
|
+
/** Read-only: compute what a refresh of an applied Promotion record would write, and its conflicts. */
|
|
1193
|
+
export async function planDocumentRefresh(request) {
|
|
1194
|
+
return (await computeRefresh(request)).plan;
|
|
1195
|
+
}
|
|
1196
|
+
/** The record as applied, with each refreshed row's versions moved forward and the journal removed. */
|
|
1197
|
+
async function closeRefresh(target, registry, recordId, expectedVersion, done, token, pin, actor, now) {
|
|
1198
|
+
const result = await mutateDocument({
|
|
1199
|
+
bundle: target,
|
|
1200
|
+
id: recordId,
|
|
1201
|
+
mode: "replace-document",
|
|
1202
|
+
onAbsent: "fail",
|
|
1203
|
+
expectedVersion,
|
|
1204
|
+
registry,
|
|
1205
|
+
strict: false,
|
|
1206
|
+
actor,
|
|
1207
|
+
...(now ? { now } : {}),
|
|
1208
|
+
buildCandidate: (existing) => {
|
|
1209
|
+
const frontmatter = structuredClone(existing.frontmatter);
|
|
1210
|
+
frontmatter.promotion_state = "applied";
|
|
1211
|
+
delete frontmatter.refresh;
|
|
1212
|
+
if (token !== undefined)
|
|
1213
|
+
frontmatter.refresh_token = token;
|
|
1214
|
+
if (pin)
|
|
1215
|
+
frontmatter.from_pin = pin.value;
|
|
1216
|
+
frontmatter.entries = frontmatter.entries.map((row) => {
|
|
1217
|
+
const entry = { ...row };
|
|
1218
|
+
const moved = done.get(String(entry.target));
|
|
1219
|
+
if (moved) {
|
|
1220
|
+
Object.assign(entry, moved);
|
|
1221
|
+
delete entry.target_edited;
|
|
1222
|
+
}
|
|
1223
|
+
return entry;
|
|
1224
|
+
});
|
|
1225
|
+
return { frontmatter, body: existing.body };
|
|
1226
|
+
},
|
|
1227
|
+
});
|
|
1228
|
+
return result.version;
|
|
1229
|
+
}
|
|
1230
|
+
function refreshedResult(recordId, version, written, changed) {
|
|
1231
|
+
return { record: { id: recordId, version, state: "applied" }, written, changed };
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Recompute the refresh plan from fresh reads, refuse unless it is ready and its token equals
|
|
1235
|
+
* `planToken`, then write: the record (`refreshing`, with the pending rows), each copy replaced
|
|
1236
|
+
* under a hard compare-and-swap on the version the plan saw, and the record again (`applied`, its
|
|
1237
|
+
* rows moved forward). A failed write throws {@link TransferInterrupted} with state `refreshing`;
|
|
1238
|
+
* {@link resumeDocumentTransfer} finishes it. The same token applied again after it completed is
|
|
1239
|
+
* a no-op success.
|
|
1240
|
+
*/
|
|
1241
|
+
export async function applyDocumentRefresh(request, planToken) {
|
|
1242
|
+
const token = planToken.trim();
|
|
1243
|
+
const { plan, candidates, read } = await computeRefresh(request);
|
|
1244
|
+
if (!plan.ready)
|
|
1245
|
+
throw new TransferConflictError(plan.conflicts);
|
|
1246
|
+
if (plan.plan_token === undefined && read.doc.frontmatter.refresh_token === token) {
|
|
1247
|
+
return refreshedResult(request.recordId, read.version, [], false);
|
|
1248
|
+
}
|
|
1249
|
+
if (plan.plan_token !== token)
|
|
1250
|
+
throw new TransferPlanChanged(token, plan.plan_token ?? "(nothing to refresh)");
|
|
1251
|
+
const registry = await loadKinds(request.target);
|
|
1252
|
+
const pending = plan.entries.filter((row) => row.action === "update" || row.action === "overwrite");
|
|
1253
|
+
const journal = pending.map((row) => ({
|
|
1254
|
+
source: row.source,
|
|
1255
|
+
source_version: row.source_version,
|
|
1256
|
+
target: row.target,
|
|
1257
|
+
replaces_version: row.target_version,
|
|
1258
|
+
}));
|
|
1259
|
+
let recordVersion;
|
|
1260
|
+
try {
|
|
1261
|
+
recordVersion = (await mutateDocument({
|
|
1262
|
+
bundle: request.target,
|
|
1263
|
+
id: request.recordId,
|
|
1264
|
+
mode: "replace-document",
|
|
1265
|
+
onAbsent: "fail",
|
|
1266
|
+
expectedVersion: read.version,
|
|
1267
|
+
registry,
|
|
1268
|
+
strict: false,
|
|
1269
|
+
actor: request.actor,
|
|
1270
|
+
...(request.now ? { now: request.now } : {}),
|
|
1271
|
+
buildCandidate: (existing) => {
|
|
1272
|
+
const frontmatter = structuredClone(existing.frontmatter);
|
|
1273
|
+
frontmatter.promotion_state = "refreshing";
|
|
1274
|
+
frontmatter.refresh = { plan_token: token, entries: journal.map((row) => ({ ...row })) };
|
|
1275
|
+
return { frontmatter, body: existing.body };
|
|
1276
|
+
},
|
|
1277
|
+
})).version;
|
|
1278
|
+
}
|
|
1279
|
+
catch (error) {
|
|
1280
|
+
if (error instanceof VersionConflict)
|
|
1281
|
+
throw new TransferPlanChanged(token, `'${request.recordId}' changed during the refresh`);
|
|
1282
|
+
throw error;
|
|
1283
|
+
}
|
|
1284
|
+
const written = [];
|
|
1285
|
+
const done = new Map();
|
|
1286
|
+
for (let index = 0; index < journal.length; index++) {
|
|
1287
|
+
const row = journal[index];
|
|
1288
|
+
try {
|
|
1289
|
+
const result = await replaceCopy(request.target, registry, row, candidates.get(row.source), request.actor, request.now);
|
|
1290
|
+
written.push({ source: row.source, target: row.target, version: result });
|
|
1291
|
+
done.set(row.target, { source_version: row.source_version, target_version: result });
|
|
1292
|
+
}
|
|
1293
|
+
catch (cause) {
|
|
1294
|
+
throw new TransferInterrupted(request.recordId, written, journal.slice(index).map((e) => ({ source: e.source, target: e.target })), cause, "refreshing");
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
try {
|
|
1298
|
+
recordVersion = await closeRefresh(request.target, registry, request.recordId, recordVersion, done, token, plan.from.pin, request.actor, request.now);
|
|
1299
|
+
}
|
|
1300
|
+
catch (cause) {
|
|
1301
|
+
throw new TransferInterrupted(request.recordId, written, [], cause, "refreshing");
|
|
1302
|
+
}
|
|
1303
|
+
return refreshedResult(request.recordId, recordVersion, written, true);
|
|
1304
|
+
}
|
|
1305
|
+
/** Replace one copy, only if it is still exactly the version the refresh planned against. */
|
|
1306
|
+
async function replaceCopy(target, registry, row, candidate, actor, now) {
|
|
1307
|
+
const result = await mutateDocument({
|
|
1308
|
+
bundle: target,
|
|
1309
|
+
id: row.target,
|
|
1310
|
+
mode: "replace-document",
|
|
1311
|
+
onAbsent: "fail",
|
|
1312
|
+
expectedVersion: row.replaces_version,
|
|
1313
|
+
registry,
|
|
1314
|
+
strict: false,
|
|
1315
|
+
actor,
|
|
1316
|
+
seedGenerationClock: false,
|
|
1317
|
+
// The source is the authority for a promoted copy's body; the plan reported the links it drops.
|
|
1318
|
+
bodyReplace: { replaceLinks: true },
|
|
1319
|
+
...(now ? { now } : {}),
|
|
1320
|
+
buildCandidate: () => structuredClone(candidate),
|
|
1321
|
+
});
|
|
1322
|
+
return result.version;
|
|
1323
|
+
}
|
|
1324
|
+
/**
|
|
1325
|
+
* Finish a `refreshing` record. For each pending row: a copy whose provenance already names this
|
|
1326
|
+
* record at the row's source version is done; a copy still at `replaces_version` whose source is
|
|
1327
|
+
* still at the row's `source_version` is replaced; any other row (the copy was deleted or edited,
|
|
1328
|
+
* or the source moved on) is left as it is, keeps its old baseline in the record, and is reported,
|
|
1329
|
+
* so the record always closes without overwriting anything the plan did not show.
|
|
1330
|
+
*/
|
|
1331
|
+
async function finishRefresh(target, recordId, read, record, from, options) {
|
|
1332
|
+
const snapshot = await readTarget(target);
|
|
1333
|
+
assertActor(options.actor, snapshot.okfVersion);
|
|
1334
|
+
const identity = [];
|
|
1335
|
+
const { pin } = await sourceIdentity(from, snapshot, identity);
|
|
1336
|
+
if (identity.length > 0)
|
|
1337
|
+
throw new TransferResumeConflict(recordId, `${identity[0].code}: ${identity[0].detail}`);
|
|
1338
|
+
const source = await readSource(from.binding);
|
|
1339
|
+
const mapping = new Map(record.entries.map((e) => [e.source, e.target]));
|
|
1340
|
+
const ctx = { alias: from.alias, source, target: snapshot, mapping };
|
|
1341
|
+
const now = options.now ?? (() => new Date().toISOString());
|
|
1342
|
+
const registry = snapshot.registry;
|
|
1343
|
+
const token = read.doc.frontmatter.refresh.plan_token;
|
|
1344
|
+
const written = [];
|
|
1345
|
+
const left = [];
|
|
1346
|
+
const done = new Map();
|
|
1347
|
+
for (const row of record.refresh) {
|
|
1348
|
+
const leave = (detail) => { left.push({ source: row.source, target: row.target, detail }); };
|
|
1349
|
+
let current;
|
|
1350
|
+
try {
|
|
1351
|
+
current = await readOptional(target, row.target);
|
|
1352
|
+
}
|
|
1353
|
+
catch (error) {
|
|
1354
|
+
if (!(error instanceof FilesystemIdentityAliasError))
|
|
1355
|
+
throw error;
|
|
1356
|
+
leave(`'${row.target}' is spelled differently on disk: ${error.message}`);
|
|
1357
|
+
continue;
|
|
1358
|
+
}
|
|
1359
|
+
if (!current) {
|
|
1360
|
+
leave(`'${row.target}' was deleted during the refresh`);
|
|
1361
|
+
continue;
|
|
1362
|
+
}
|
|
1363
|
+
const sourceDoc = source.docs.get(row.source);
|
|
1364
|
+
if (!sourceDoc || sourceDoc.version !== row.source_version) {
|
|
1365
|
+
leave(`the source '${row.source}' is ${sourceDoc ? "changed" : "missing"} since the refresh was planned`);
|
|
1366
|
+
continue;
|
|
1367
|
+
}
|
|
1368
|
+
const conflicts = [];
|
|
1369
|
+
const built = buildEntry(ctx, record.bundleId, recordId, sourceDoc, row.target, new Set(mapping.keys()), true, options.actor, now, conflicts);
|
|
1370
|
+
if (conflicts.length > 0) {
|
|
1371
|
+
leave(`${conflicts[0].code}: ${conflicts[0].detail}`);
|
|
1372
|
+
continue;
|
|
1373
|
+
}
|
|
1374
|
+
if (current.version !== row.replaces_version) {
|
|
1375
|
+
// Done only when the copy is exactly what this refresh writes; provenance alone would also
|
|
1376
|
+
// match a copy edited after the write, and adopting it would make that edit the baseline.
|
|
1377
|
+
if (isWrittenCopy(current.doc, built.candidate, registry, snapshot.okfVersion)) {
|
|
1378
|
+
written.push({ source: row.source, target: row.target, version: current.version });
|
|
1379
|
+
done.set(row.target, { source_version: row.source_version, target_version: current.version });
|
|
1380
|
+
}
|
|
1381
|
+
else {
|
|
1382
|
+
leave(`'${row.target}' was changed by another writer during the refresh`);
|
|
1383
|
+
}
|
|
1384
|
+
continue;
|
|
1385
|
+
}
|
|
1386
|
+
try {
|
|
1387
|
+
const version = await replaceCopy(target, registry, row, built.candidate, options.actor, options.now);
|
|
1388
|
+
written.push({ source: row.source, target: row.target, version });
|
|
1389
|
+
done.set(row.target, { source_version: row.source_version, target_version: version });
|
|
1390
|
+
}
|
|
1391
|
+
catch (error) {
|
|
1392
|
+
if (!(error instanceof VersionConflict))
|
|
1393
|
+
throw error;
|
|
1394
|
+
leave(`'${row.target}' was changed by another writer during the resume`);
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
const version = await closeRefresh(target, registry, recordId, read.version, done, typeof token === "string" ? token : undefined, pin, options.actor, options.now);
|
|
1398
|
+
return { ...refreshedResult(recordId, version, written, true), ...(left.length > 0 ? { left } : {}) };
|
|
1399
|
+
}
|
|
1400
|
+
//# sourceMappingURL=document-transfer.js.map
|