@jam-overture/loom 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +743 -0
- package/dist/catalogue.d.ts +70 -0
- package/dist/catalogue.js +88 -0
- package/dist/cli/args.d.ts +17 -0
- package/dist/cli/args.js +83 -0
- package/dist/cli/filesystem.d.ts +15 -0
- package/dist/cli/filesystem.js +31 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/main.js +30 -0
- package/dist/cli/namespace.d.ts +40 -0
- package/dist/cli/namespace.js +40 -0
- package/dist/cli/plan.d.ts +84 -0
- package/dist/cli/plan.js +113 -0
- package/dist/cli/run.d.ts +17 -0
- package/dist/cli/run.js +75 -0
- package/dist/cli/templates.d.ts +43 -0
- package/dist/cli/templates.js +134 -0
- package/dist/closed-set.d.ts +34 -0
- package/dist/closed-set.js +34 -0
- package/dist/data/adapter.d.ts +123 -0
- package/dist/data/adapter.js +91 -0
- package/dist/data/binding.d.ts +48 -0
- package/dist/data/binding.js +39 -0
- package/dist/data/catalogue.d.ts +26 -0
- package/dist/data/catalogue.js +6 -0
- package/dist/data/index.d.ts +14 -0
- package/dist/data/index.js +14 -0
- package/dist/data/plan.d.ts +58 -0
- package/dist/data/plan.js +0 -0
- package/dist/data/resolution.d.ts +91 -0
- package/dist/data/resolution.js +96 -0
- package/dist/data/resolve.d.ts +46 -0
- package/dist/data/resolve.js +37 -0
- package/dist/data/source.d.ts +40 -0
- package/dist/data/source.js +45 -0
- package/dist/deadline.d.ts +57 -0
- package/dist/deadline.js +78 -0
- package/dist/frame/catalogue.d.ts +24 -0
- package/dist/frame/catalogue.js +5 -0
- package/dist/frame/index.d.ts +12 -0
- package/dist/frame/index.js +12 -0
- package/dist/frame/origin.d.ts +87 -0
- package/dist/frame/origin.js +114 -0
- package/dist/frame/resolution.d.ts +85 -0
- package/dist/frame/resolution.js +79 -0
- package/dist/grammar.d.ts +26 -0
- package/dist/grammar.js +28 -0
- package/dist/ids.d.ts +57 -0
- package/dist/ids.js +104 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +26 -0
- package/dist/interactivity.d.ts +50 -0
- package/dist/interactivity.js +22 -0
- package/dist/interpretation/anthropic.d.ts +22 -0
- package/dist/interpretation/anthropic.js +76 -0
- package/dist/interpretation/client.d.ts +83 -0
- package/dist/interpretation/client.js +2 -0
- package/dist/interpretation/draft.d.ts +234 -0
- package/dist/interpretation/draft.js +84 -0
- package/dist/interpretation/index.d.ts +15 -0
- package/dist/interpretation/index.js +15 -0
- package/dist/interpretation/interpreter.d.ts +62 -0
- package/dist/interpretation/interpreter.js +151 -0
- package/dist/interpretation/materialize.d.ts +42 -0
- package/dist/interpretation/materialize.js +107 -0
- package/dist/interpretation/prompt.d.ts +229 -0
- package/dist/interpretation/prompt.js +285 -0
- package/dist/interpretation/render.d.ts +76 -0
- package/dist/interpretation/render.js +272 -0
- package/dist/interpretation/schema.d.ts +49 -0
- package/dist/interpretation/schema.js +131 -0
- package/dist/json.d.ts +35 -0
- package/dist/json.js +11 -0
- package/dist/paging.d.ts +60 -0
- package/dist/paging.js +49 -0
- package/dist/primitive-type.d.ts +32 -0
- package/dist/primitive-type.js +39 -0
- package/dist/render/addressing.d.ts +59 -0
- package/dist/render/addressing.js +60 -0
- package/dist/render/anchor.d.ts +105 -0
- package/dist/render/anchor.js +84 -0
- package/dist/render/behaviour-adjust.d.ts +62 -0
- package/dist/render/behaviour-adjust.js +78 -0
- package/dist/render/behaviour-copy.d.ts +53 -0
- package/dist/render/behaviour-copy.js +75 -0
- package/dist/render/behaviour-disclose.d.ts +74 -0
- package/dist/render/behaviour-disclose.js +77 -0
- package/dist/render/behaviour-present.d.ts +123 -0
- package/dist/render/behaviour-present.js +173 -0
- package/dist/render/behaviour.d.ts +243 -0
- package/dist/render/behaviour.js +284 -0
- package/dist/render/control.d.ts +81 -0
- package/dist/render/control.js +80 -0
- package/dist/render/decorative.d.ts +62 -0
- package/dist/render/decorative.js +2 -0
- package/dist/render/diagnostics.d.ts +260 -0
- package/dist/render/diagnostics.js +61 -0
- package/dist/render/disclosed.d.ts +37 -0
- package/dist/render/disclosed.js +37 -0
- package/dist/render/editable.d.ts +34 -0
- package/dist/render/editable.js +35 -0
- package/dist/render/frame.d.ts +42 -0
- package/dist/render/frame.js +31 -0
- package/dist/render/index.d.ts +35 -0
- package/dist/render/index.js +35 -0
- package/dist/render/inline-variables.d.ts +77 -0
- package/dist/render/inline-variables.js +220 -0
- package/dist/render/presented.d.ts +82 -0
- package/dist/render/presented.js +82 -0
- package/dist/render/primitive.d.ts +228 -0
- package/dist/render/primitive.js +36 -0
- package/dist/render/props.d.ts +48 -0
- package/dist/render/props.js +2 -0
- package/dist/render/reads.d.ts +73 -0
- package/dist/render/reads.js +45 -0
- package/dist/render/render.d.ts +164 -0
- package/dist/render/render.js +571 -0
- package/dist/render/request.d.ts +111 -0
- package/dist/render/request.js +60 -0
- package/dist/render/text.d.ts +91 -0
- package/dist/render/text.js +44 -0
- package/dist/render/theme.d.ts +61 -0
- package/dist/render/theme.js +52 -0
- package/dist/reserved-props.d.ts +35 -0
- package/dist/reserved-props.js +45 -0
- package/dist/result.d.ts +27 -0
- package/dist/result.js +30 -0
- package/dist/role.d.ts +71 -0
- package/dist/role.js +22 -0
- package/dist/runtime/analysis.d.ts +149 -0
- package/dist/runtime/analysis.js +187 -0
- package/dist/runtime/assessment.d.ts +35 -0
- package/dist/runtime/assessment.js +13 -0
- package/dist/runtime/disposition.d.ts +102 -0
- package/dist/runtime/disposition.js +59 -0
- package/dist/runtime/events.d.ts +187 -0
- package/dist/runtime/events.js +7 -0
- package/dist/runtime/gate.d.ts +43 -0
- package/dist/runtime/gate.js +169 -0
- package/dist/runtime/index.d.ts +27 -0
- package/dist/runtime/index.js +27 -0
- package/dist/runtime/intent.d.ts +60 -0
- package/dist/runtime/intent.js +36 -0
- package/dist/runtime/interpreter.d.ts +117 -0
- package/dist/runtime/interpreter.js +46 -0
- package/dist/runtime/inverse-interpreter.d.ts +90 -0
- package/dist/runtime/inverse-interpreter.js +56 -0
- package/dist/runtime/inverse.d.ts +87 -0
- package/dist/runtime/inverse.js +48 -0
- package/dist/runtime/narration.d.ts +24 -0
- package/dist/runtime/narration.js +36 -0
- package/dist/runtime/nesting.d.ts +31 -0
- package/dist/runtime/nesting.js +46 -0
- package/dist/runtime/pipeline.d.ts +124 -0
- package/dist/runtime/pipeline.js +178 -0
- package/dist/runtime/policy-fingerprint.d.ts +29 -0
- package/dist/runtime/policy-fingerprint.js +119 -0
- package/dist/runtime/policy-source.d.ts +42 -0
- package/dist/runtime/policy-source.js +8 -0
- package/dist/runtime/policy.d.ts +156 -0
- package/dist/runtime/policy.js +107 -0
- package/dist/runtime/proposal.d.ts +423 -0
- package/dist/runtime/proposal.js +109 -0
- package/dist/runtime/redirection.d.ts +53 -0
- package/dist/runtime/redirection.js +38 -0
- package/dist/runtime/repointing.d.ts +71 -0
- package/dist/runtime/repointing.js +0 -0
- package/dist/runtime/reversibility.d.ts +36 -0
- package/dist/runtime/reversibility.js +39 -0
- package/dist/runtime/stake-level.d.ts +33 -0
- package/dist/runtime/stake-level.js +58 -0
- package/dist/runtime/stakes.d.ts +47 -0
- package/dist/runtime/stakes.js +305 -0
- package/dist/runtime/vocabulary.d.ts +122 -0
- package/dist/runtime/vocabulary.js +87 -0
- package/dist/sdk/audit.d.ts +195 -0
- package/dist/sdk/audit.js +128 -0
- package/dist/sdk/catalogue.d.ts +37 -0
- package/dist/sdk/catalogue.js +16 -0
- package/dist/sdk/conformance.d.ts +342 -0
- package/dist/sdk/conformance.js +494 -0
- package/dist/sdk/copy.d.ts +105 -0
- package/dist/sdk/copy.js +90 -0
- package/dist/sdk/definition.d.ts +242 -0
- package/dist/sdk/definition.js +64 -0
- package/dist/sdk/index.d.ts +19 -0
- package/dist/sdk/index.js +19 -0
- package/dist/sdk/interactivity.d.ts +29 -0
- package/dist/sdk/interactivity.js +34 -0
- package/dist/sdk/pairings.d.ts +124 -0
- package/dist/sdk/pairings.js +81 -0
- package/dist/sdk/registry.d.ts +162 -0
- package/dist/sdk/registry.js +329 -0
- package/dist/sdk/selection.d.ts +46 -0
- package/dist/sdk/selection.js +24 -0
- package/dist/sdk/text.d.ts +100 -0
- package/dist/sdk/text.js +105 -0
- package/dist/sdk/vocabulary.d.ts +39 -0
- package/dist/sdk/vocabulary.js +36 -0
- package/dist/signals/broadcast.d.ts +134 -0
- package/dist/signals/broadcast.js +451 -0
- package/dist/signals/collect.d.ts +184 -0
- package/dist/signals/collect.js +196 -0
- package/dist/signals/deliver.d.ts +64 -0
- package/dist/signals/deliver.js +85 -0
- package/dist/signals/fold.d.ts +112 -0
- package/dist/signals/fold.js +76 -0
- package/dist/signals/index.d.ts +31 -0
- package/dist/signals/index.js +31 -0
- package/dist/signals/ingest.d.ts +51 -0
- package/dist/signals/ingest.js +70 -0
- package/dist/signals/intake.d.ts +149 -0
- package/dist/signals/intake.js +154 -0
- package/dist/signals/journal.d.ts +94 -0
- package/dist/signals/journal.js +11 -0
- package/dist/signals/kinds.d.ts +22 -0
- package/dist/signals/kinds.js +22 -0
- package/dist/signals/ledger.d.ts +46 -0
- package/dist/signals/ledger.js +81 -0
- package/dist/signals/memory.d.ts +17 -0
- package/dist/signals/memory.js +99 -0
- package/dist/signals/migrate.d.ts +27 -0
- package/dist/signals/migrate.js +78 -0
- package/dist/signals/postgres.d.ts +7 -0
- package/dist/signals/postgres.js +295 -0
- package/dist/signals/rollup.d.ts +142 -0
- package/dist/signals/rollup.js +0 -0
- package/dist/signals/schema.d.ts +534 -0
- package/dist/signals/schema.js +92 -0
- package/dist/signals/signal.d.ts +381 -0
- package/dist/signals/signal.js +150 -0
- package/dist/signals/tally.d.ts +70 -0
- package/dist/signals/tally.js +2 -0
- package/dist/signals/view.d.ts +51 -0
- package/dist/signals/view.js +54 -0
- package/dist/store/attribution.d.ts +125 -0
- package/dist/store/attribution.js +116 -0
- package/dist/store/database.d.ts +14 -0
- package/dist/store/database.js +2 -0
- package/dist/store/driver.d.ts +29 -0
- package/dist/store/driver.js +39 -0
- package/dist/store/errors.d.ts +56 -0
- package/dist/store/errors.js +37 -0
- package/dist/store/index.d.ts +16 -0
- package/dist/store/index.js +16 -0
- package/dist/store/memory.d.ts +3 -0
- package/dist/store/memory.js +115 -0
- package/dist/store/migrate.d.ts +54 -0
- package/dist/store/migrate.js +122 -0
- package/dist/store/postgres-holds.d.ts +4 -0
- package/dist/store/postgres-holds.js +192 -0
- package/dist/store/postgres.d.ts +8 -0
- package/dist/store/postgres.js +262 -0
- package/dist/store/replay.d.ts +94 -0
- package/dist/store/replay.js +82 -0
- package/dist/store/revert.d.ts +116 -0
- package/dist/store/revert.js +243 -0
- package/dist/store/schema.d.ts +371 -0
- package/dist/store/schema.js +78 -0
- package/dist/store/source.d.ts +23 -0
- package/dist/store/source.js +37 -0
- package/dist/store/store.d.ts +231 -0
- package/dist/store/store.js +52 -0
- package/dist/store/undone.d.ts +18 -0
- package/dist/store/undone.js +89 -0
- package/dist/submit/catalogue.d.ts +20 -0
- package/dist/submit/catalogue.js +5 -0
- package/dist/submit/declaration.d.ts +39 -0
- package/dist/submit/declaration.js +30 -0
- package/dist/submit/endpoint.d.ts +131 -0
- package/dist/submit/endpoint.js +160 -0
- package/dist/submit/index.d.ts +13 -0
- package/dist/submit/index.js +13 -0
- package/dist/submit/plan.d.ts +49 -0
- package/dist/submit/plan.js +47 -0
- package/dist/submit/resolution.d.ts +61 -0
- package/dist/submit/resolution.js +64 -0
- package/dist/submit/resolve.d.ts +45 -0
- package/dist/submit/resolve.js +36 -0
- package/dist/telemetry/calibration.d.ts +132 -0
- package/dist/telemetry/calibration.js +151 -0
- package/dist/telemetry/episode.d.ts +140 -0
- package/dist/telemetry/episode.js +272 -0
- package/dist/telemetry/event.d.ts +2310 -0
- package/dist/telemetry/event.js +405 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +15 -0
- package/dist/telemetry/journal.d.ts +116 -0
- package/dist/telemetry/journal.js +11 -0
- package/dist/telemetry/memory.d.ts +24 -0
- package/dist/telemetry/memory.js +75 -0
- package/dist/telemetry/migrate.d.ts +26 -0
- package/dist/telemetry/migrate.js +40 -0
- package/dist/telemetry/postgres.d.ts +6 -0
- package/dist/telemetry/postgres.js +139 -0
- package/dist/telemetry/retention.d.ts +131 -0
- package/dist/telemetry/retention.js +217 -0
- package/dist/telemetry/schema.d.ts +113 -0
- package/dist/telemetry/schema.js +33 -0
- package/dist/telemetry/sink.d.ts +42 -0
- package/dist/telemetry/sink.js +61 -0
- package/dist/testing/contracts.d.ts +34 -0
- package/dist/testing/contracts.js +34 -0
- package/dist/testing/definitions.d.ts +15 -0
- package/dist/testing/definitions.js +48 -0
- package/dist/testing/doubles.d.ts +94 -0
- package/dist/testing/doubles.js +141 -0
- package/dist/testing/episode-harness.d.ts +49 -0
- package/dist/testing/episode-harness.js +65 -0
- package/dist/testing/filesystem.d.ts +17 -0
- package/dist/testing/filesystem.js +17 -0
- package/dist/testing/fixtures.d.ts +57 -0
- package/dist/testing/fixtures.js +84 -0
- package/dist/testing/hold-contract.d.ts +19 -0
- package/dist/testing/hold-contract.js +374 -0
- package/dist/testing/index.d.ts +41 -0
- package/dist/testing/index.js +41 -0
- package/dist/testing/journal-contract.d.ts +17 -0
- package/dist/testing/journal-contract.js +353 -0
- package/dist/testing/model-replies.d.ts +34 -0
- package/dist/testing/model-replies.js +101 -0
- package/dist/testing/primitives.d.ts +7 -0
- package/dist/testing/primitives.js +29 -0
- package/dist/testing/reader-signal-contract.d.ts +38 -0
- package/dist/testing/reader-signal-contract.js +318 -0
- package/dist/testing/row-security.d.ts +17 -0
- package/dist/testing/row-security.js +21 -0
- package/dist/testing/store-contract.d.ts +25 -0
- package/dist/testing/store-contract.js +500 -0
- package/dist/theme/apply.d.ts +17 -0
- package/dist/theme/apply.js +60 -0
- package/dist/theme/contrast.d.ts +197 -0
- package/dist/theme/contrast.js +259 -0
- package/dist/theme/derive.d.ts +77 -0
- package/dist/theme/derive.js +167 -0
- package/dist/theme/faces.d.ts +73 -0
- package/dist/theme/faces.js +119 -0
- package/dist/theme/font-packs.d.ts +25 -0
- package/dist/theme/font-packs.js +257 -0
- package/dist/theme/index.d.ts +20 -0
- package/dist/theme/index.js +20 -0
- package/dist/theme/lab.d.ts +30 -0
- package/dist/theme/lab.js +46 -0
- package/dist/theme/library.d.ts +114 -0
- package/dist/theme/library.js +300 -0
- package/dist/theme/measure.d.ts +144 -0
- package/dist/theme/measure.js +145 -0
- package/dist/theme/palettes.d.ts +64 -0
- package/dist/theme/palettes.js +497 -0
- package/dist/theme/registry.d.ts +59 -0
- package/dist/theme/registry.js +60 -0
- package/dist/theme/separation.d.ts +177 -0
- package/dist/theme/separation.js +203 -0
- package/dist/theme/style-presets.d.ts +60 -0
- package/dist/theme/style-presets.js +124 -0
- package/dist/theme/theme.d.ts +253 -0
- package/dist/theme/theme.js +171 -0
- package/dist/tree/apply.d.ts +16 -0
- package/dist/tree/apply.js +104 -0
- package/dist/tree/builders.d.ts +19 -0
- package/dist/tree/builders.js +20 -0
- package/dist/tree/compare.d.ts +68 -0
- package/dist/tree/compare.js +75 -0
- package/dist/tree/configuration.d.ts +25 -0
- package/dist/tree/configuration.js +108 -0
- package/dist/tree/delta.d.ts +288 -0
- package/dist/tree/delta.js +88 -0
- package/dist/tree/errors.d.ts +69 -0
- package/dist/tree/errors.js +34 -0
- package/dist/tree/identity.d.ts +117 -0
- package/dist/tree/identity.js +145 -0
- package/dist/tree/index.d.ts +21 -0
- package/dist/tree/index.js +21 -0
- package/dist/tree/inverse.d.ts +24 -0
- package/dist/tree/inverse.js +112 -0
- package/dist/tree/mutation.d.ts +18 -0
- package/dist/tree/mutation.js +49 -0
- package/dist/tree/naming.d.ts +4 -0
- package/dist/tree/naming.js +35 -0
- package/dist/tree/navigation.d.ts +38 -0
- package/dist/tree/navigation.js +83 -0
- package/dist/tree/node.d.ts +103 -0
- package/dist/tree/node.js +46 -0
- package/dist/tree/outline.d.ts +28 -0
- package/dist/tree/outline.js +10 -0
- package/dist/tree/tree.d.ts +83 -0
- package/dist/tree/tree.js +62 -0
- package/dist/write/commit.d.ts +124 -0
- package/dist/write/commit.js +234 -0
- package/dist/write/held.d.ts +674 -0
- package/dist/write/held.js +185 -0
- package/dist/write/index.d.ts +11 -0
- package/dist/write/index.js +11 -0
- package/dist/write/liveness.d.ts +132 -0
- package/dist/write/liveness.js +91 -0
- package/dist/write/revert.d.ts +79 -0
- package/dist/write/revert.js +134 -0
- package/package.json +160 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one value in the reader-signal seam that correlates anything.
|
|
3
|
+
*
|
|
4
|
+
* A view key is minted in the browser when a broadcast starts, rides every
|
|
5
|
+
* batch of that one page view, and is never seen again. It exists so a funnel
|
|
6
|
+
* can be answered — *of the views that reached the pricing band, how many
|
|
7
|
+
* pressed the button* — which per-node totals cannot answer and which is half
|
|
8
|
+
* of what the portal is for ([0146](../../decisions/0146-a-reader-signal-stays-anonymous-and-a-funnel-is-correlated-inside-one-page-view.md)).
|
|
9
|
+
*
|
|
10
|
+
* Everything else about it is a refusal. It is not a cookie and is not written
|
|
11
|
+
* to storage of any kind, so it does not survive a reload. It is not derived
|
|
12
|
+
* from the reader, their device, their address or their clock — it is random
|
|
13
|
+
* bytes and nothing else. It does not leave the aggregation step: rollup reads
|
|
14
|
+
* it and the counters it produces do not carry it, so it expires with the raw
|
|
15
|
+
* window.
|
|
16
|
+
*
|
|
17
|
+
* **The shape is strict for a reason that is not tidiness.** A free-form string
|
|
18
|
+
* here is an identity backdoor: a host wanting a visitor id could put one in
|
|
19
|
+
* this field and everything downstream would keep working. Thirty-two hex
|
|
20
|
+
* characters of nothing hold no room for a hash of an address, an account id,
|
|
21
|
+
* or a user agent, and a batch carrying one is refused at the parser rather
|
|
22
|
+
* than stored and noticed later.
|
|
23
|
+
*
|
|
24
|
+
* This module is loaded by the broadcaster, so it imports no package and
|
|
25
|
+
* touches no schema (0136). The schema built from the constants below lives in
|
|
26
|
+
* `signal.ts`, which a browser never loads.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* 128 bits, the same width a UUID spends on randomness.
|
|
30
|
+
*
|
|
31
|
+
* The number that matters is not the collision probability against all views
|
|
32
|
+
* ever, but against the views inside one raw window on one deployment — the
|
|
33
|
+
* only span in which two keys are ever compared. At 128 bits that is
|
|
34
|
+
* unreachable, and the field costs 32 bytes on a batch that already carries
|
|
35
|
+
* several signals.
|
|
36
|
+
*/
|
|
37
|
+
export const VIEW_KEY_BYTES = 16;
|
|
38
|
+
export const VIEW_KEY_LENGTH = VIEW_KEY_BYTES * 2;
|
|
39
|
+
export const viewKeyPattern = /^[0-9a-f]{32}$/;
|
|
40
|
+
/**
|
|
41
|
+
* `crypto.getRandomValues` is present wherever the broadcaster can run: it
|
|
42
|
+
* predates `IntersectionObserver`, which the broadcaster already requires, and
|
|
43
|
+
* unlike `crypto.subtle` it does not need a secure context. There is no
|
|
44
|
+
* `Math.random` fallback on purpose — a weak key is worse than no funnel,
|
|
45
|
+
* because nothing downstream can tell which one it got.
|
|
46
|
+
*/
|
|
47
|
+
const cryptoRandomBytes = (count) => globalThis.crypto.getRandomValues(new Uint8Array(count));
|
|
48
|
+
const hex = (byte) => byte.toString(16).padStart(2, "0");
|
|
49
|
+
/**
|
|
50
|
+
* A fresh view key. Called once per broadcast, never per batch — the whole
|
|
51
|
+
* point is that the batches of one page view agree.
|
|
52
|
+
*/
|
|
53
|
+
export const mintViewKey = (random = cryptoRandomBytes) => Array.from(random(VIEW_KEY_BYTES), hex).join("");
|
|
54
|
+
//# sourceMappingURL=view.js.map
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { NodeId } from "../ids.js";
|
|
2
|
+
import { type Result } from "../result.js";
|
|
3
|
+
import type { LoomTree } from "../tree/tree.js";
|
|
4
|
+
import type { StoreError } from "./errors.js";
|
|
5
|
+
import type { StoredRevision, TreeReader } from "./store.js";
|
|
6
|
+
/**
|
|
7
|
+
* Who put this node here.
|
|
8
|
+
*
|
|
9
|
+
* The log already answers it — every entry carries the provenance that produced
|
|
10
|
+
* it (0016) — but only per revision. A reviewer looking at a tree is not asking
|
|
11
|
+
* "what happened at revision 12", they are pointing at one node and asking who
|
|
12
|
+
* asked for it and who wrote it. Nothing joined those two readings, so a change
|
|
13
|
+
* was attributable in the aggregate and anonymous at the place you would
|
|
14
|
+
* actually query it.
|
|
15
|
+
*
|
|
16
|
+
* This module makes that join without storing anything. A node carries no
|
|
17
|
+
* authorship field and the tree schema is untouched: attribution is derived from
|
|
18
|
+
* the log the same way `identity.ts` derives id history (0038), because a fact
|
|
19
|
+
* the log already determines does not belong in the document as well — two
|
|
20
|
+
* copies of one fact is one fact and one opportunity to disagree.
|
|
21
|
+
*
|
|
22
|
+
* **The walk goes backwards, and stops.** For a node that is in the tree now,
|
|
23
|
+
* the first insert carrying it that a backwards walk meets *is* the one that put
|
|
24
|
+
* it there: anything that removed it afterwards would need a later insert to
|
|
25
|
+
* bring it back, and that later insert is the one the walk would have met first.
|
|
26
|
+
* So attribution costs a bounded read from the newest end of the log rather than
|
|
27
|
+
* a fold over all of it, and a recently placed node is answered by one page.
|
|
28
|
+
*/
|
|
29
|
+
/** What a revision did to a node that was already in the tree. */
|
|
30
|
+
export type NodeChange = "configured" | "moved";
|
|
31
|
+
/** What a revision did to a node that is in the tree now. */
|
|
32
|
+
export type NodeEffect = "placed" | NodeChange;
|
|
33
|
+
/**
|
|
34
|
+
* One revision that touched one node, and the whole entry it came from.
|
|
35
|
+
*
|
|
36
|
+
* The entry is carried rather than copied out field by field. A touch is a
|
|
37
|
+
* pointer into the log, and restating the provenance, the approver and the
|
|
38
|
+
* proposal id here would be a second copy of a record that is already the truth
|
|
39
|
+
* — free to drift, and no more readable.
|
|
40
|
+
*/
|
|
41
|
+
export type NodeTouch<Effect extends NodeEffect = NodeEffect> = {
|
|
42
|
+
readonly effect: Effect;
|
|
43
|
+
/**
|
|
44
|
+
* Whether the operation named this node, as opposed to carrying it in.
|
|
45
|
+
*
|
|
46
|
+
* An insert brings a whole subtree, so every node in it was placed by that
|
|
47
|
+
* revision — but only the subtree's root was asked for. "The model added a
|
|
48
|
+
* card" and "the model added the heading inside the card it added" are
|
|
49
|
+
* different sentences, and a reviewer deserves the one that is true.
|
|
50
|
+
*/
|
|
51
|
+
readonly named: boolean;
|
|
52
|
+
readonly entry: StoredRevision;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* What is known about how a node came to be in the tree.
|
|
56
|
+
*
|
|
57
|
+
* Three outcomes rather than two, because "no revision placed this" and "we
|
|
58
|
+
* stopped looking" are different facts and only one of them is about the tree.
|
|
59
|
+
* A bounded read that reported the second as the first would credit the seed
|
|
60
|
+
* with work somebody did.
|
|
61
|
+
*/
|
|
62
|
+
export type NodeAttribution = {
|
|
63
|
+
readonly outcome: "placed";
|
|
64
|
+
readonly nodeId: NodeId;
|
|
65
|
+
readonly placed: NodeTouch;
|
|
66
|
+
/**
|
|
67
|
+
* Every touch since it was placed, oldest first. Never a placement: the
|
|
68
|
+
* walk stops at the one that put the node here, so anything it collected
|
|
69
|
+
* on the way changed a node that was already there.
|
|
70
|
+
*/
|
|
71
|
+
readonly since: readonly NodeTouch<NodeChange>[];
|
|
72
|
+
}
|
|
73
|
+
/** The walk reached the start of the log without finding a placement. */
|
|
74
|
+
| {
|
|
75
|
+
readonly outcome: "seeded";
|
|
76
|
+
readonly nodeId: NodeId;
|
|
77
|
+
readonly since: readonly NodeTouch<NodeChange>[];
|
|
78
|
+
}
|
|
79
|
+
/** The walk ran out of budget first. `since` is what it saw, not all there is. */
|
|
80
|
+
| {
|
|
81
|
+
readonly outcome: "undetermined";
|
|
82
|
+
readonly nodeId: NodeId;
|
|
83
|
+
readonly since: readonly NodeTouch<NodeChange>[];
|
|
84
|
+
};
|
|
85
|
+
export type TreeAttribution = {
|
|
86
|
+
readonly nodes: ReadonlyMap<NodeId, NodeAttribution>;
|
|
87
|
+
/**
|
|
88
|
+
* The oldest revision the walk read, or null when it read none.
|
|
89
|
+
*
|
|
90
|
+
* Reported once here rather than on every `undetermined` node, because it is a
|
|
91
|
+
* fact about the read and not about any node. It is also the only thing that
|
|
92
|
+
* makes an `undetermined` actionable: the node was placed at or before this.
|
|
93
|
+
*/
|
|
94
|
+
readonly examinedTo: number | null;
|
|
95
|
+
/** True when the walk saw the whole log, which is what turns unplaced into seeded. */
|
|
96
|
+
readonly reachedStart: boolean;
|
|
97
|
+
};
|
|
98
|
+
export type AttributionRequest = {
|
|
99
|
+
/**
|
|
100
|
+
* How many pages of log to read before giving up on whatever is still
|
|
101
|
+
* unattributed. Every read in Loom is bounded and this one is no exception; a
|
|
102
|
+
* tree edited for long enough has a history no request path should fold.
|
|
103
|
+
*/
|
|
104
|
+
readonly pages?: number;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Five pages, which at the store's default page size is five hundred revisions.
|
|
108
|
+
*
|
|
109
|
+
* Chosen to cover the whole log of any tree in a portal today while still being
|
|
110
|
+
* a bound. The walk stops as soon as every node is accounted for, so this is the
|
|
111
|
+
* cost of a tree with genuinely ancient nodes, not the usual cost.
|
|
112
|
+
*/
|
|
113
|
+
export declare const DEFAULT_ATTRIBUTION_PAGES = 5;
|
|
114
|
+
/**
|
|
115
|
+
* Attributes every node in a tree, walking that tree's log backwards.
|
|
116
|
+
*
|
|
117
|
+
* Takes the tree rather than a tree id on purpose. The caller already has the
|
|
118
|
+
* tree it is describing — a portal has the one it rendered — and reading `head`
|
|
119
|
+
* again here could return a revision that arrived in between, so the attribution
|
|
120
|
+
* would describe nodes the caller is not showing. The same reasoning
|
|
121
|
+
* `auditSnapshot` uses when it reports both trees rather than making its caller
|
|
122
|
+
* re-read one.
|
|
123
|
+
*/
|
|
124
|
+
export declare const attributeTree: (reader: TreeReader, tree: LoomTree, request?: AttributionRequest) => Promise<Result<TreeAttribution, StoreError>>;
|
|
125
|
+
//# sourceMappingURL=attribution.d.ts.map
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { assertNever, ok } from "../result.js";
|
|
2
|
+
import { walkTree } from "../tree/navigation.js";
|
|
3
|
+
/**
|
|
4
|
+
* Five pages, which at the store's default page size is five hundred revisions.
|
|
5
|
+
*
|
|
6
|
+
* Chosen to cover the whole log of any tree in a portal today while still being
|
|
7
|
+
* a bound. The walk stops as soon as every node is accounted for, so this is the
|
|
8
|
+
* cost of a tree with genuinely ancient nodes, not the usual cost.
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_ATTRIBUTION_PAGES = 5;
|
|
11
|
+
const isPlacement = (effect) => effect === "placed";
|
|
12
|
+
/**
|
|
13
|
+
* Every node one operation touched, read from the operation alone.
|
|
14
|
+
*
|
|
15
|
+
* No tree state is consulted, and none is needed: an insert carries the subtree
|
|
16
|
+
* it is inserting, and the other three name their node. That is what keeps this
|
|
17
|
+
* function pure and the walk cheap.
|
|
18
|
+
*
|
|
19
|
+
* A `remove` yields nothing, and that is not an omission. This attributes nodes
|
|
20
|
+
* that are in the tree now, and a backwards walk stops at a live node's
|
|
21
|
+
* placement — so a removal of that node is always older than the insert that
|
|
22
|
+
* brought it back, and is never reached. A node the removal took for good is not
|
|
23
|
+
* in the tree being attributed at all.
|
|
24
|
+
*/
|
|
25
|
+
const effectsOf = (operation) => {
|
|
26
|
+
switch (operation.op) {
|
|
27
|
+
case "insert":
|
|
28
|
+
return Array.from(walkTree(operation.node), (node) => ({
|
|
29
|
+
nodeId: node.id,
|
|
30
|
+
effect: "placed",
|
|
31
|
+
named: node.id === operation.node.id,
|
|
32
|
+
}));
|
|
33
|
+
case "configure":
|
|
34
|
+
return [{ nodeId: operation.nodeId, effect: "configured", named: true }];
|
|
35
|
+
case "move":
|
|
36
|
+
return [{ nodeId: operation.nodeId, effect: "moved", named: true }];
|
|
37
|
+
case "remove":
|
|
38
|
+
return [];
|
|
39
|
+
default:
|
|
40
|
+
return assertNever(operation, "effectsOf");
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const effectsIn = (delta) => delta.operations.flatMap(effectsOf);
|
|
44
|
+
const attributionOf = (nodeId, pending, reachedStart) => {
|
|
45
|
+
const since = [...pending.since].reverse();
|
|
46
|
+
if (pending.placed)
|
|
47
|
+
return { outcome: "placed", nodeId, placed: pending.placed, since };
|
|
48
|
+
return reachedStart
|
|
49
|
+
? { outcome: "seeded", nodeId, since }
|
|
50
|
+
: { outcome: "undetermined", nodeId, since };
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Attributes every node in a tree, walking that tree's log backwards.
|
|
54
|
+
*
|
|
55
|
+
* Takes the tree rather than a tree id on purpose. The caller already has the
|
|
56
|
+
* tree it is describing — a portal has the one it rendered — and reading `head`
|
|
57
|
+
* again here could return a revision that arrived in between, so the attribution
|
|
58
|
+
* would describe nodes the caller is not showing. The same reasoning
|
|
59
|
+
* `auditSnapshot` uses when it reports both trees rather than making its caller
|
|
60
|
+
* re-read one.
|
|
61
|
+
*/
|
|
62
|
+
export const attributeTree = async (reader, tree, request = {}) => {
|
|
63
|
+
const budget = request.pages ?? DEFAULT_ATTRIBUTION_PAGES;
|
|
64
|
+
const pending = new Map(Array.from(walkTree(tree.root), (node) => [node.id, { placed: undefined, since: [] }]));
|
|
65
|
+
let unplaced = pending.size;
|
|
66
|
+
let examinedTo = null;
|
|
67
|
+
let reachedStart = false;
|
|
68
|
+
let cursor;
|
|
69
|
+
for (let page = 0; page < budget && unplaced > 0; page += 1) {
|
|
70
|
+
const read = await reader.revisions(tree.treeId, {
|
|
71
|
+
direction: "older",
|
|
72
|
+
...(cursor === undefined ? {} : { cursor }),
|
|
73
|
+
});
|
|
74
|
+
if (!read.ok)
|
|
75
|
+
return read;
|
|
76
|
+
/**
|
|
77
|
+
* A page is always ascending by revision whichever end it came from, so
|
|
78
|
+
* walking it backwards is what puts the whole read in newest-first order.
|
|
79
|
+
*/
|
|
80
|
+
for (const entry of [...read.value.revisions].reverse()) {
|
|
81
|
+
/**
|
|
82
|
+
* The log may have moved since the caller read its tree. Attributing a
|
|
83
|
+
* revision the caller's tree does not contain would credit a node with a
|
|
84
|
+
* placement that has not happened as far as anything on screen is
|
|
85
|
+
* concerned — so the walk describes the log *as of* this tree.
|
|
86
|
+
*/
|
|
87
|
+
if (entry.revision > tree.revision)
|
|
88
|
+
continue;
|
|
89
|
+
examinedTo = entry.revision;
|
|
90
|
+
for (const { nodeId, effect, named } of effectsIn(entry.delta)) {
|
|
91
|
+
const found = pending.get(nodeId);
|
|
92
|
+
/** Already placed, or not a node this tree holds: either way, done with it. */
|
|
93
|
+
if (!found || found.placed)
|
|
94
|
+
continue;
|
|
95
|
+
if (isPlacement(effect)) {
|
|
96
|
+
found.placed = { effect, named, entry };
|
|
97
|
+
unplaced -= 1;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
found.since.push({ effect, named, entry });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (read.value.older === null) {
|
|
105
|
+
reachedStart = true;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
cursor = read.value.older;
|
|
109
|
+
}
|
|
110
|
+
return ok({
|
|
111
|
+
nodes: new Map(Array.from(pending, ([nodeId, found]) => [nodeId, attributionOf(nodeId, found, reachedStart)])),
|
|
112
|
+
examinedTo,
|
|
113
|
+
reachedStart,
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
//# sourceMappingURL=attribution.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PgDatabase, PgQueryResultHKT } from "drizzle-orm/pg-core";
|
|
2
|
+
/**
|
|
3
|
+
* The database handle the postgres store and its migration share.
|
|
4
|
+
*
|
|
5
|
+
* One type, in a module of its own, so that neither of the two files that need
|
|
6
|
+
* it has to import the other.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The narrowest Drizzle type that both the postgres.js and PGlite drivers
|
|
10
|
+
* satisfy. It lives alone so that `migrate.ts` and `postgres.ts` can share it
|
|
11
|
+
* without importing each other.
|
|
12
|
+
*/
|
|
13
|
+
export type LoomDatabase = PgDatabase<PgQueryResultHKT>;
|
|
14
|
+
//# sourceMappingURL=database.d.ts.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two things every Postgres-backed store has to do with a driver's error.
|
|
3
|
+
*
|
|
4
|
+
* Shared rather than repeated, because both are answers to questions about the
|
|
5
|
+
* *driver* rather than about the store using it: what Postgres calls a duplicate
|
|
6
|
+
* key, and how much of a thrown thing is safe to put in a message. A second copy
|
|
7
|
+
* would be a second chance to get the `cause` walk subtly wrong, and the failure
|
|
8
|
+
* mode of getting it wrong is silent — a collision reported as an outage.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Postgres reports a primary-key collision as SQLSTATE 23505, but Drizzle wraps
|
|
12
|
+
* the driver's error, so the code sits somewhere down the `cause` chain rather
|
|
13
|
+
* than on the error that was thrown. Walking the chain is what makes this work
|
|
14
|
+
* across drivers instead of against whichever one was tested first.
|
|
15
|
+
*/
|
|
16
|
+
export declare const isUniqueViolation: (cause: unknown) => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* An outage, said in terms of what was being attempted.
|
|
19
|
+
*
|
|
20
|
+
* Generic in the error type because `StoreError` and `HoldError` are separate
|
|
21
|
+
* taxonomies that happen to agree on this one member, and they should stay
|
|
22
|
+
* separate: they are refusals of different things, and a store that could return
|
|
23
|
+
* either would be a store whose callers have to handle both.
|
|
24
|
+
*/
|
|
25
|
+
export declare const unavailable: (cause: unknown, detail: string) => {
|
|
26
|
+
readonly code: "unavailable";
|
|
27
|
+
readonly detail: string;
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=driver.d.ts.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two things every Postgres-backed store has to do with a driver's error.
|
|
3
|
+
*
|
|
4
|
+
* Shared rather than repeated, because both are answers to questions about the
|
|
5
|
+
* *driver* rather than about the store using it: what Postgres calls a duplicate
|
|
6
|
+
* key, and how much of a thrown thing is safe to put in a message. A second copy
|
|
7
|
+
* would be a second chance to get the `cause` walk subtly wrong, and the failure
|
|
8
|
+
* mode of getting it wrong is silent — a collision reported as an outage.
|
|
9
|
+
*/
|
|
10
|
+
const UNIQUE_VIOLATION = "23505";
|
|
11
|
+
/**
|
|
12
|
+
* Postgres reports a primary-key collision as SQLSTATE 23505, but Drizzle wraps
|
|
13
|
+
* the driver's error, so the code sits somewhere down the `cause` chain rather
|
|
14
|
+
* than on the error that was thrown. Walking the chain is what makes this work
|
|
15
|
+
* across drivers instead of against whichever one was tested first.
|
|
16
|
+
*/
|
|
17
|
+
export const isUniqueViolation = (cause) => {
|
|
18
|
+
for (let current = cause, depth = 0; current !== null && current !== undefined && depth < 8; depth++) {
|
|
19
|
+
if (typeof current !== "object")
|
|
20
|
+
return false;
|
|
21
|
+
if (current.code === UNIQUE_VIOLATION)
|
|
22
|
+
return true;
|
|
23
|
+
current = current.cause;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* An outage, said in terms of what was being attempted.
|
|
29
|
+
*
|
|
30
|
+
* Generic in the error type because `StoreError` and `HoldError` are separate
|
|
31
|
+
* taxonomies that happen to agree on this one member, and they should stay
|
|
32
|
+
* separate: they are refusals of different things, and a store that could return
|
|
33
|
+
* either would be a store whose callers have to handle both.
|
|
34
|
+
*/
|
|
35
|
+
export const unavailable = (cause, detail) => ({
|
|
36
|
+
code: "unavailable",
|
|
37
|
+
detail: `${detail}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=driver.js.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { TreeId } from "../ids.js";
|
|
2
|
+
import type { TreeError } from "../tree/errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* What persistence can refuse, and how to say it.
|
|
5
|
+
*
|
|
6
|
+
* Separated from the `TreeStore` contract because the taxonomy has a second
|
|
7
|
+
* consumer: the runtime's event stream narrates a change that was accepted and
|
|
8
|
+
* then failed to persist, and it should be able to say *why* without depending
|
|
9
|
+
* on the store interface. This module imports nothing from the runtime, so that
|
|
10
|
+
* direction stays acyclic.
|
|
11
|
+
*/
|
|
12
|
+
export type StoreError =
|
|
13
|
+
/** No tree under this id — distinct from a tree with an empty history. */
|
|
14
|
+
{
|
|
15
|
+
readonly code: "not-found";
|
|
16
|
+
readonly treeId: TreeId;
|
|
17
|
+
} | {
|
|
18
|
+
readonly code: "already-exists";
|
|
19
|
+
readonly treeId: TreeId;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The delta names a base revision that is not the current head. This is the
|
|
23
|
+
* whole of concurrency control: two writers racing at the same base means the
|
|
24
|
+
* second one is refused rather than silently applied to a tree it never saw.
|
|
25
|
+
*/
|
|
26
|
+
| {
|
|
27
|
+
readonly code: "revision-conflict";
|
|
28
|
+
readonly treeId: TreeId;
|
|
29
|
+
readonly expected: number;
|
|
30
|
+
readonly found: number;
|
|
31
|
+
} | {
|
|
32
|
+
readonly code: "delta-rejected";
|
|
33
|
+
readonly treeId: TreeId;
|
|
34
|
+
readonly error: TreeError;
|
|
35
|
+
} | {
|
|
36
|
+
readonly code: "unavailable";
|
|
37
|
+
readonly detail: string;
|
|
38
|
+
};
|
|
39
|
+
export type StoreErrorCode = StoreError["code"];
|
|
40
|
+
/**
|
|
41
|
+
* Every way persistence can refuse, in the order a reader meets them.
|
|
42
|
+
*
|
|
43
|
+
* A `switch` over a `StoreError` is exhaustive without this and always was. What
|
|
44
|
+
* needs a list is everything that *walks* the taxonomy rather than reacting to
|
|
45
|
+
* one member: a page that tells a host what it has to handle before going to
|
|
46
|
+
* production, a dashboard wanting a bucket per code, a conformance check written
|
|
47
|
+
* against the store contract. Each of those otherwise keeps its own copy, and a
|
|
48
|
+
* copy is a list that is silently wrong the day a sixth code lands.
|
|
49
|
+
*
|
|
50
|
+
* The order is the order a deployment meets them rather than one of severity:
|
|
51
|
+
* the two that are about a tree's identity, then the two a write is refused by,
|
|
52
|
+
* then the one that is about the storage itself rather than anything in it.
|
|
53
|
+
*/
|
|
54
|
+
export declare const STORE_ERROR_CODES: readonly StoreErrorCode[];
|
|
55
|
+
export declare const describeStoreError: (error: StoreError) => string;
|
|
56
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { everyMemberOf } from "../closed-set.js";
|
|
2
|
+
/**
|
|
3
|
+
* Every way persistence can refuse, in the order a reader meets them.
|
|
4
|
+
*
|
|
5
|
+
* A `switch` over a `StoreError` is exhaustive without this and always was. What
|
|
6
|
+
* needs a list is everything that *walks* the taxonomy rather than reacting to
|
|
7
|
+
* one member: a page that tells a host what it has to handle before going to
|
|
8
|
+
* production, a dashboard wanting a bucket per code, a conformance check written
|
|
9
|
+
* against the store contract. Each of those otherwise keeps its own copy, and a
|
|
10
|
+
* copy is a list that is silently wrong the day a sixth code lands.
|
|
11
|
+
*
|
|
12
|
+
* The order is the order a deployment meets them rather than one of severity:
|
|
13
|
+
* the two that are about a tree's identity, then the two a write is refused by,
|
|
14
|
+
* then the one that is about the storage itself rather than anything in it.
|
|
15
|
+
*/
|
|
16
|
+
export const STORE_ERROR_CODES = everyMemberOf()([
|
|
17
|
+
"not-found",
|
|
18
|
+
"already-exists",
|
|
19
|
+
"revision-conflict",
|
|
20
|
+
"delta-rejected",
|
|
21
|
+
"unavailable",
|
|
22
|
+
]);
|
|
23
|
+
export const describeStoreError = (error) => {
|
|
24
|
+
switch (error.code) {
|
|
25
|
+
case "not-found":
|
|
26
|
+
return `no tree stored under ${error.treeId}`;
|
|
27
|
+
case "already-exists":
|
|
28
|
+
return `${error.treeId} already exists; a tree is created once`;
|
|
29
|
+
case "revision-conflict":
|
|
30
|
+
return `${error.treeId} moved on: the delta applies to revision ${error.expected}, but head is ${error.found}`;
|
|
31
|
+
case "delta-rejected":
|
|
32
|
+
return `the delta did not apply to ${error.treeId}: ${error.error.code}`;
|
|
33
|
+
case "unavailable":
|
|
34
|
+
return `storage is unavailable: ${error.detail}`;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping trees, and the record of how they got that way.
|
|
3
|
+
*
|
|
4
|
+
* A store holds a snapshot and an append-only log beside it, so that any revision
|
|
5
|
+
* can be replayed, attributed, or undone. Two implementations, one contract.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./attribution.js";
|
|
8
|
+
export * from "./driver.js";
|
|
9
|
+
export * from "./errors.js";
|
|
10
|
+
export * from "./memory.js";
|
|
11
|
+
export * from "./replay.js";
|
|
12
|
+
export * from "./revert.js";
|
|
13
|
+
export * from "./source.js";
|
|
14
|
+
export * from "./store.js";
|
|
15
|
+
export * from "./undone.js";
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping trees, and the record of how they got that way.
|
|
3
|
+
*
|
|
4
|
+
* A store holds a snapshot and an append-only log beside it, so that any revision
|
|
5
|
+
* can be replayed, attributed, or undone. Two implementations, one contract.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./attribution.js";
|
|
8
|
+
export * from "./driver.js";
|
|
9
|
+
export * from "./errors.js";
|
|
10
|
+
export * from "./memory.js";
|
|
11
|
+
export * from "./replay.js";
|
|
12
|
+
export * from "./revert.js";
|
|
13
|
+
export * from "./source.js";
|
|
14
|
+
export * from "./store.js";
|
|
15
|
+
export * from "./undone.js";
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { cursorPosition, pageEnds } from "../paging.js";
|
|
2
|
+
import { err, ok } from "../result.js";
|
|
3
|
+
import { applyDelta } from "../tree/apply.js";
|
|
4
|
+
import { anchorBound, anchorResumes, clampListingLimit, clampRevisionLimit, } from "./store.js";
|
|
5
|
+
export const memoryTreeStore = () => {
|
|
6
|
+
const trees = new Map();
|
|
7
|
+
const entryOf = (treeId) => trees.get(treeId);
|
|
8
|
+
return {
|
|
9
|
+
create: (tree) => Promise.resolve(trees.has(tree.treeId)
|
|
10
|
+
? err({ code: "already-exists", treeId: tree.treeId })
|
|
11
|
+
: (() => {
|
|
12
|
+
trees.set(tree.treeId, { snapshot: tree, log: [] });
|
|
13
|
+
return ok(tree);
|
|
14
|
+
})()),
|
|
15
|
+
head: (treeId) => {
|
|
16
|
+
const entry = entryOf(treeId);
|
|
17
|
+
return Promise.resolve(entry ? ok(entry.snapshot) : err({ code: "not-found", treeId }));
|
|
18
|
+
},
|
|
19
|
+
revisions: (treeId, request) => {
|
|
20
|
+
const entry = entryOf(treeId);
|
|
21
|
+
if (!entry)
|
|
22
|
+
return Promise.resolve(err({ code: "not-found", treeId }));
|
|
23
|
+
const limit = clampRevisionLimit(request?.limit);
|
|
24
|
+
const direction = request?.direction ?? "newer";
|
|
25
|
+
const at = request?.at;
|
|
26
|
+
/**
|
|
27
|
+
* An anchor and a cursor arrive as the same exclusive bound, so the filter
|
|
28
|
+
* below never learns which one it was reading — only `resumed` does, and
|
|
29
|
+
* only because an anchor sits inside its own page.
|
|
30
|
+
*/
|
|
31
|
+
const from = at === undefined ? cursorPosition(request?.cursor) : anchorBound(at, direction);
|
|
32
|
+
const matching = entry.log.filter((stored) => from === undefined ||
|
|
33
|
+
(direction === "older" ? stored.revision < from : stored.revision > from));
|
|
34
|
+
/**
|
|
35
|
+
* `older` takes the page from the far end, which is a slice rather than a
|
|
36
|
+
* reverse: the entries that come back are still in applied order, because
|
|
37
|
+
* the contract says a page's order never depends on which end it came from.
|
|
38
|
+
*/
|
|
39
|
+
const page = direction === "older" ? matching.slice(-limit) : matching.slice(0, limit);
|
|
40
|
+
return Promise.resolve(ok({
|
|
41
|
+
revisions: page,
|
|
42
|
+
...pageEnds(page.map((stored) => stored.revision), direction, {
|
|
43
|
+
beyond: matching.length > page.length,
|
|
44
|
+
resumed: at === undefined
|
|
45
|
+
? from !== undefined
|
|
46
|
+
: anchorResumes(at, direction, entry.snapshot.revision),
|
|
47
|
+
}),
|
|
48
|
+
}));
|
|
49
|
+
},
|
|
50
|
+
/**
|
|
51
|
+
* Keyset pagination over the ids in ascending order. A `Map` iterates in
|
|
52
|
+
* insertion order, so the sort is not decoration — it is what makes this
|
|
53
|
+
* implementation obey the ordering the contract promises, and what a store
|
|
54
|
+
* with a real index would get from the index instead.
|
|
55
|
+
*/
|
|
56
|
+
list: (request) => {
|
|
57
|
+
const limit = clampListingLimit(request?.limit);
|
|
58
|
+
const cursor = request?.cursor;
|
|
59
|
+
const keys = Array.from(trees.keys())
|
|
60
|
+
.sort()
|
|
61
|
+
.filter((key) => cursor === undefined || key > cursor);
|
|
62
|
+
const page = keys.slice(0, limit);
|
|
63
|
+
const listings = page.flatMap((key) => {
|
|
64
|
+
const entry = trees.get(key);
|
|
65
|
+
return entry ? [{ treeId: entry.snapshot.treeId, revision: entry.snapshot.revision }] : [];
|
|
66
|
+
});
|
|
67
|
+
const last = page.at(-1);
|
|
68
|
+
return Promise.resolve(ok({
|
|
69
|
+
trees: listings,
|
|
70
|
+
cursor: last !== undefined && keys.length > page.length ? last : null,
|
|
71
|
+
}));
|
|
72
|
+
},
|
|
73
|
+
append: (treeId, request) => {
|
|
74
|
+
const entry = entryOf(treeId);
|
|
75
|
+
if (!entry)
|
|
76
|
+
return Promise.resolve(err({ code: "not-found", treeId }));
|
|
77
|
+
/**
|
|
78
|
+
* The base-revision check is the concurrency control, and it runs before
|
|
79
|
+
* the apply rather than relying on it: `applyDelta` would also refuse a
|
|
80
|
+
* stale delta, but it would report a tree error, and "someone else wrote
|
|
81
|
+
* first" deserves to be distinguishable from "this delta is malformed".
|
|
82
|
+
*/
|
|
83
|
+
if (request.delta.baseRevision !== entry.snapshot.revision) {
|
|
84
|
+
return Promise.resolve(err({
|
|
85
|
+
code: "revision-conflict",
|
|
86
|
+
treeId,
|
|
87
|
+
expected: request.delta.baseRevision,
|
|
88
|
+
found: entry.snapshot.revision,
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
const applied = applyDelta(entry.snapshot, request.delta);
|
|
92
|
+
if (!applied.ok) {
|
|
93
|
+
return Promise.resolve(err({ code: "delta-rejected", treeId, error: applied.error }));
|
|
94
|
+
}
|
|
95
|
+
const revision = {
|
|
96
|
+
treeId,
|
|
97
|
+
revision: applied.value.revision,
|
|
98
|
+
proposalId: request.proposalId,
|
|
99
|
+
delta: request.delta,
|
|
100
|
+
provenance: request.provenance,
|
|
101
|
+
appliedAt: request.appliedAt,
|
|
102
|
+
/**
|
|
103
|
+
* Omitted rather than stored as `undefined`, so an entry nobody had to
|
|
104
|
+
* approve has no key at all — the same shape a Postgres row with a null
|
|
105
|
+
* column parses back to.
|
|
106
|
+
*/
|
|
107
|
+
...(request.answeredBy === undefined ? {} : { answeredBy: request.answeredBy }),
|
|
108
|
+
};
|
|
109
|
+
/** One write, so the log and the snapshot cannot be seen disagreeing. */
|
|
110
|
+
trees.set(treeId, { snapshot: applied.value, log: [...entry.log, revision] });
|
|
111
|
+
return Promise.resolve(ok(applied.value));
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=memory.js.map
|