@enbox/dwn-sdk-js 0.3.9 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/browser.mjs +11 -11
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/generated/precompiled-validators.js +783 -1206
- package/dist/esm/generated/precompiled-validators.js.map +1 -1
- package/dist/esm/src/core/dwn-constant.js +5 -0
- package/dist/esm/src/core/dwn-constant.js.map +1 -1
- package/dist/esm/src/core/dwn-error.js +13 -7
- package/dist/esm/src/core/dwn-error.js.map +1 -1
- package/dist/esm/src/core/grant-authorization.js +9 -18
- package/dist/esm/src/core/grant-authorization.js.map +1 -1
- package/dist/esm/src/core/message-reply.js.map +1 -1
- package/dist/esm/src/core/messages-grant-authorization.js +28 -61
- package/dist/esm/src/core/messages-grant-authorization.js.map +1 -1
- package/dist/esm/src/core/protocol-authorization-action.js +25 -27
- package/dist/esm/src/core/protocol-authorization-action.js.map +1 -1
- package/dist/esm/src/core/protocol-authorization-validation.js +31 -69
- package/dist/esm/src/core/protocol-authorization-validation.js.map +1 -1
- package/dist/esm/src/core/protocol-authorization.js +44 -118
- package/dist/esm/src/core/protocol-authorization.js.map +1 -1
- package/dist/esm/src/core/protocols-grant-authorization.js +5 -5
- package/dist/esm/src/core/protocols-grant-authorization.js.map +1 -1
- package/dist/esm/src/core/recording-validation-state-reader.js +84 -0
- package/dist/esm/src/core/recording-validation-state-reader.js.map +1 -0
- package/dist/esm/src/core/records-grant-authorization.js +11 -11
- package/dist/esm/src/core/records-grant-authorization.js.map +1 -1
- package/dist/esm/src/core/replication-apply.js +295 -0
- package/dist/esm/src/core/replication-apply.js.map +1 -0
- package/dist/esm/src/core/resumable-task-manager.js +5 -4
- package/dist/esm/src/core/resumable-task-manager.js.map +1 -1
- package/dist/esm/src/core/validation-state-reader.js +237 -0
- package/dist/esm/src/core/validation-state-reader.js.map +1 -0
- package/dist/esm/src/dwn.js +261 -16
- package/dist/esm/src/dwn.js.map +1 -1
- package/dist/esm/src/enums/dwn-interface-method.js +0 -1
- package/dist/esm/src/enums/dwn-interface-method.js.map +1 -1
- package/dist/esm/src/event-stream/durable-event-log.js +365 -0
- package/dist/esm/src/event-stream/durable-event-log.js.map +1 -0
- package/dist/esm/src/event-stream/event-emitter-wake-publisher.js +25 -0
- package/dist/esm/src/event-stream/event-emitter-wake-publisher.js.map +1 -0
- package/dist/esm/src/handlers/messages-query.js +159 -0
- package/dist/esm/src/handlers/messages-query.js.map +1 -0
- package/dist/esm/src/handlers/messages-read.js +5 -5
- package/dist/esm/src/handlers/messages-read.js.map +1 -1
- package/dist/esm/src/handlers/messages-subscribe.js +8 -8
- package/dist/esm/src/handlers/messages-subscribe.js.map +1 -1
- package/dist/esm/src/handlers/protocols-configure.js +30 -49
- package/dist/esm/src/handlers/protocols-configure.js.map +1 -1
- package/dist/esm/src/handlers/protocols-query.js +1 -1
- package/dist/esm/src/handlers/protocols-query.js.map +1 -1
- package/dist/esm/src/handlers/records-count.js +20 -11
- package/dist/esm/src/handlers/records-count.js.map +1 -1
- package/dist/esm/src/handlers/records-delete.js +20 -16
- package/dist/esm/src/handlers/records-delete.js.map +1 -1
- package/dist/esm/src/handlers/records-query.js +35 -11
- package/dist/esm/src/handlers/records-query.js.map +1 -1
- package/dist/esm/src/handlers/records-read.js +52 -42
- package/dist/esm/src/handlers/records-read.js.map +1 -1
- package/dist/esm/src/handlers/records-subscribe.js +107 -11
- package/dist/esm/src/handlers/records-subscribe.js.map +1 -1
- package/dist/esm/src/handlers/records-write.js +62 -116
- package/dist/esm/src/handlers/records-write.js.map +1 -1
- package/dist/esm/src/index.js +7 -8
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/interfaces/messages-query.js +49 -0
- package/dist/esm/src/interfaces/messages-query.js.map +1 -0
- package/dist/esm/src/interfaces/protocols-configure.js +7 -3
- package/dist/esm/src/interfaces/protocols-configure.js.map +1 -1
- package/dist/esm/src/interfaces/protocols-query.js +3 -4
- package/dist/esm/src/interfaces/protocols-query.js.map +1 -1
- package/dist/esm/src/interfaces/records-count.js +4 -3
- package/dist/esm/src/interfaces/records-count.js.map +1 -1
- package/dist/esm/src/interfaces/records-delete.js +21 -4
- package/dist/esm/src/interfaces/records-delete.js.map +1 -1
- package/dist/esm/src/interfaces/records-query.js +4 -3
- package/dist/esm/src/interfaces/records-query.js.map +1 -1
- package/dist/esm/src/interfaces/records-read.js +3 -3
- package/dist/esm/src/interfaces/records-read.js.map +1 -1
- package/dist/esm/src/interfaces/records-subscribe.js +4 -3
- package/dist/esm/src/interfaces/records-subscribe.js.map +1 -1
- package/dist/esm/src/interfaces/records-write.js +27 -13
- package/dist/esm/src/interfaces/records-write.js.map +1 -1
- package/dist/esm/src/protocols/permissions.js +27 -34
- package/dist/esm/src/protocols/permissions.js.map +1 -1
- package/dist/esm/src/store/index-level.js +24 -9
- package/dist/esm/src/store/index-level.js.map +1 -1
- package/dist/esm/src/store/level-wrapper.js +7 -0
- package/dist/esm/src/store/level-wrapper.js.map +1 -1
- package/dist/esm/src/store/message-store-level.js +536 -42
- package/dist/esm/src/store/message-store-level.js.map +1 -1
- package/dist/esm/src/store/storage-controller.js +58 -49
- package/dist/esm/src/store/storage-controller.js.map +1 -1
- package/dist/esm/src/types/message-types.js.map +1 -1
- package/dist/esm/src/types/validation-state-reader.js +2 -0
- package/dist/esm/src/types/validation-state-reader.js.map +1 -0
- package/dist/esm/src/utils/messages.js +17 -0
- package/dist/esm/src/utils/messages.js.map +1 -1
- package/dist/esm/src/utils/record-limit-occupancy.js +244 -0
- package/dist/esm/src/utils/record-limit-occupancy.js.map +1 -0
- package/dist/esm/src/utils/records.js +50 -14
- package/dist/esm/src/utils/records.js.map +1 -1
- package/dist/esm/src/utils/replication.js +85 -0
- package/dist/esm/src/utils/replication.js.map +1 -0
- package/dist/esm/tests/core/grant-authorization.spec.js +4 -4
- package/dist/esm/tests/core/grant-authorization.spec.js.map +1 -1
- package/dist/esm/tests/core/process-message-parity.spec.js +222 -0
- package/dist/esm/tests/core/process-message-parity.spec.js.map +1 -0
- package/dist/esm/tests/core/protocol-authorization.spec.js +5 -2
- package/dist/esm/tests/core/protocol-authorization.spec.js.map +1 -1
- package/dist/esm/tests/core/records-grant-authorization.spec.js +5 -5
- package/dist/esm/tests/core/records-grant-authorization.spec.js.map +1 -1
- package/dist/esm/tests/core/replication-apply.spec.js +274 -0
- package/dist/esm/tests/core/replication-apply.spec.js.map +1 -0
- package/dist/esm/tests/core/replication-replay-property.spec.js +350 -0
- package/dist/esm/tests/core/replication-replay-property.spec.js.map +1 -0
- package/dist/esm/tests/core/validation-read-closure.spec.js +469 -0
- package/dist/esm/tests/core/validation-read-closure.spec.js.map +1 -0
- package/dist/esm/tests/core/validation-state-reader.spec.js +716 -0
- package/dist/esm/tests/core/validation-state-reader.spec.js.map +1 -0
- package/dist/esm/tests/durable-event-log.spec.js +373 -0
- package/dist/esm/tests/durable-event-log.spec.js.map +1 -0
- package/dist/esm/tests/dwn.spec.js +620 -14
- package/dist/esm/tests/dwn.spec.js.map +1 -1
- package/dist/esm/tests/features/author-delegated-grant.spec.js +9 -6
- package/dist/esm/tests/features/author-delegated-grant.spec.js.map +1 -1
- package/dist/esm/tests/features/owner-delegated-grant.spec.js +1 -4
- package/dist/esm/tests/features/owner-delegated-grant.spec.js.map +1 -1
- package/dist/esm/tests/features/owner-signature.spec.js +1 -4
- package/dist/esm/tests/features/owner-signature.spec.js.map +1 -1
- package/dist/esm/tests/features/permissions.spec.js +165 -4
- package/dist/esm/tests/features/permissions.spec.js.map +1 -1
- package/dist/esm/tests/features/protocol-composition.spec.js +8 -11
- package/dist/esm/tests/features/protocol-composition.spec.js.map +1 -1
- package/dist/esm/tests/features/protocol-create-action.spec.js +1 -4
- package/dist/esm/tests/features/protocol-create-action.spec.js.map +1 -1
- package/dist/esm/tests/features/protocol-delete-action.spec.js +3 -5
- package/dist/esm/tests/features/protocol-delete-action.spec.js.map +1 -1
- package/dist/esm/tests/features/protocol-update-action.spec.js +3 -6
- package/dist/esm/tests/features/protocol-update-action.spec.js.map +1 -1
- package/dist/esm/tests/features/records-delivery.spec.js +1 -4
- package/dist/esm/tests/features/records-delivery.spec.js.map +1 -1
- package/dist/esm/tests/features/records-immutable.spec.js +1 -4
- package/dist/esm/tests/features/records-immutable.spec.js.map +1 -1
- package/dist/esm/tests/features/records-nested-query-scope.spec.js +281 -0
- package/dist/esm/tests/features/records-nested-query-scope.spec.js.map +1 -0
- package/dist/esm/tests/features/records-prune-cross-protocol.spec.js +3 -7
- package/dist/esm/tests/features/records-prune-cross-protocol.spec.js.map +1 -1
- package/dist/esm/tests/features/records-prune.spec.js +11 -22
- package/dist/esm/tests/features/records-prune.spec.js.map +1 -1
- package/dist/esm/tests/features/records-record-limit.spec.js +441 -231
- package/dist/esm/tests/features/records-record-limit.spec.js.map +1 -1
- package/dist/esm/tests/features/records-squash.spec.js +6 -4
- package/dist/esm/tests/features/records-squash.spec.js.map +1 -1
- package/dist/esm/tests/features/records-tags.spec.js +1 -4
- package/dist/esm/tests/features/records-tags.spec.js.map +1 -1
- package/dist/esm/tests/features/resumable-tasks.spec.js +3 -5
- package/dist/esm/tests/features/resumable-tasks.spec.js.map +1 -1
- package/dist/esm/tests/fuzz/message-store.fuzz.spec.js +1 -2
- package/dist/esm/tests/fuzz/message-store.fuzz.spec.js.map +1 -1
- package/dist/esm/tests/fuzz/process-message.fuzz.spec.js +2 -4
- package/dist/esm/tests/fuzz/process-message.fuzz.spec.js.map +1 -1
- package/dist/esm/tests/fuzz/schema-validation.fuzz.spec.js +1 -1
- package/dist/esm/tests/fuzz/schema-validation.fuzz.spec.js.map +1 -1
- package/dist/esm/tests/handlers/messages-query.spec.js +246 -0
- package/dist/esm/tests/handlers/messages-query.spec.js.map +1 -0
- package/dist/esm/tests/handlers/messages-read.spec.js +2 -5
- package/dist/esm/tests/handlers/messages-read.spec.js.map +1 -1
- package/dist/esm/tests/handlers/messages-subscribe.spec.js +3 -14
- package/dist/esm/tests/handlers/messages-subscribe.spec.js.map +1 -1
- package/dist/esm/tests/handlers/protocols-configure.spec.js +27 -26
- package/dist/esm/tests/handlers/protocols-configure.spec.js.map +1 -1
- package/dist/esm/tests/handlers/protocols-query.spec.js +1 -4
- package/dist/esm/tests/handlers/protocols-query.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-count.spec.js +1 -4
- package/dist/esm/tests/handlers/records-count.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-delete.spec.js +312 -30
- package/dist/esm/tests/handlers/records-delete.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-query.spec.js +32 -9
- package/dist/esm/tests/handlers/records-query.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-read.spec.js +4 -4
- package/dist/esm/tests/handlers/records-read.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-subscribe.spec.js +33 -14
- package/dist/esm/tests/handlers/records-subscribe.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-write.spec.js +84 -38
- package/dist/esm/tests/handlers/records-write.spec.js.map +1 -1
- package/dist/esm/tests/interfaces/records-delete.spec.js +69 -2
- package/dist/esm/tests/interfaces/records-delete.spec.js.map +1 -1
- package/dist/esm/tests/interfaces/records-write.spec.js +4 -3
- package/dist/esm/tests/interfaces/records-write.spec.js.map +1 -1
- package/dist/esm/tests/protocols/permissions.spec.js +55 -6
- package/dist/esm/tests/protocols/permissions.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/aggregator.spec.js +1 -4
- package/dist/esm/tests/scenarios/aggregator.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/deleted-record.spec.js +1 -4
- package/dist/esm/tests/scenarios/deleted-record.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/end-to-end-tests.spec.js +1 -4
- package/dist/esm/tests/scenarios/end-to-end-tests.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/nested-roles.spec.js +1 -4
- package/dist/esm/tests/scenarios/nested-roles.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/subscriptions.spec.js +1 -4
- package/dist/esm/tests/scenarios/subscriptions.spec.js.map +1 -1
- package/dist/esm/tests/store/message-store-level.spec.js +361 -5
- package/dist/esm/tests/store/message-store-level.spec.js.map +1 -1
- package/dist/esm/tests/store/message-store.spec.js +60 -0
- package/dist/esm/tests/store/message-store.spec.js.map +1 -1
- package/dist/esm/tests/test-event-stream.js +7 -3
- package/dist/esm/tests/test-event-stream.js.map +1 -1
- package/dist/esm/tests/test-stores.js +19 -9
- package/dist/esm/tests/test-stores.js.map +1 -1
- package/dist/esm/tests/test-suite.js +4 -4
- package/dist/esm/tests/test-suite.js.map +1 -1
- package/dist/esm/tests/utils/test-data-generator.js +25 -0
- package/dist/esm/tests/utils/test-data-generator.js.map +1 -1
- package/dist/esm/tests/utils/test-stub-generator.js.map +1 -1
- package/dist/esm/tests/utils/test-validation-state-reader.js +16 -0
- package/dist/esm/tests/utils/test-validation-state-reader.js.map +1 -0
- package/dist/types/generated/precompiled-validators.d.ts +6 -6
- package/dist/types/generated/precompiled-validators.d.ts.map +1 -1
- package/dist/types/src/core/core-protocol.d.ts +3 -3
- package/dist/types/src/core/core-protocol.d.ts.map +1 -1
- package/dist/types/src/core/dwn-constant.d.ts +5 -0
- package/dist/types/src/core/dwn-constant.d.ts.map +1 -1
- package/dist/types/src/core/dwn-error.d.ts +13 -7
- package/dist/types/src/core/dwn-error.d.ts.map +1 -1
- package/dist/types/src/core/grant-authorization.d.ts +5 -5
- package/dist/types/src/core/grant-authorization.d.ts.map +1 -1
- package/dist/types/src/core/message-reply.d.ts +5 -4
- package/dist/types/src/core/message-reply.d.ts.map +1 -1
- package/dist/types/src/core/messages-grant-authorization.d.ts +12 -15
- package/dist/types/src/core/messages-grant-authorization.d.ts.map +1 -1
- package/dist/types/src/core/protocol-authorization-action.d.ts +4 -5
- package/dist/types/src/core/protocol-authorization-action.d.ts.map +1 -1
- package/dist/types/src/core/protocol-authorization-validation.d.ts +13 -16
- package/dist/types/src/core/protocol-authorization-validation.d.ts.map +1 -1
- package/dist/types/src/core/protocol-authorization.d.ts +8 -33
- package/dist/types/src/core/protocol-authorization.d.ts.map +1 -1
- package/dist/types/src/core/protocols-grant-authorization.d.ts +4 -4
- package/dist/types/src/core/protocols-grant-authorization.d.ts.map +1 -1
- package/dist/types/src/core/recording-validation-state-reader.d.ts +75 -0
- package/dist/types/src/core/recording-validation-state-reader.d.ts.map +1 -0
- package/dist/types/src/core/records-grant-authorization.d.ts +8 -8
- package/dist/types/src/core/records-grant-authorization.d.ts.map +1 -1
- package/dist/types/src/core/replication-apply.d.ts +129 -0
- package/dist/types/src/core/replication-apply.d.ts.map +1 -0
- package/dist/types/src/core/resumable-task-manager.d.ts +1 -1
- package/dist/types/src/core/resumable-task-manager.d.ts.map +1 -1
- package/dist/types/src/core/validation-state-reader.d.ts +79 -0
- package/dist/types/src/core/validation-state-reader.d.ts.map +1 -0
- package/dist/types/src/dwn.d.ts +47 -13
- package/dist/types/src/dwn.d.ts.map +1 -1
- package/dist/types/src/enums/dwn-interface-method.d.ts +0 -1
- package/dist/types/src/enums/dwn-interface-method.d.ts.map +1 -1
- package/dist/types/src/event-stream/durable-event-log.d.ts +69 -0
- package/dist/types/src/event-stream/durable-event-log.d.ts.map +1 -0
- package/dist/types/src/event-stream/event-emitter-wake-publisher.d.ts +13 -0
- package/dist/types/src/event-stream/event-emitter-wake-publisher.d.ts.map +1 -0
- package/dist/types/src/handlers/messages-query.d.ts +20 -0
- package/dist/types/src/handlers/messages-query.d.ts.map +1 -0
- package/dist/types/src/handlers/messages-read.d.ts +1 -1
- package/dist/types/src/handlers/messages-read.d.ts.map +1 -1
- package/dist/types/src/handlers/messages-subscribe.d.ts.map +1 -1
- package/dist/types/src/handlers/protocols-configure.d.ts +0 -5
- package/dist/types/src/handlers/protocols-configure.d.ts.map +1 -1
- package/dist/types/src/handlers/records-count.d.ts +2 -1
- package/dist/types/src/handlers/records-count.d.ts.map +1 -1
- package/dist/types/src/handlers/records-delete.d.ts +2 -2
- package/dist/types/src/handlers/records-delete.d.ts.map +1 -1
- package/dist/types/src/handlers/records-query.d.ts +1 -1
- package/dist/types/src/handlers/records-query.d.ts.map +1 -1
- package/dist/types/src/handlers/records-read.d.ts +2 -1
- package/dist/types/src/handlers/records-read.d.ts.map +1 -1
- package/dist/types/src/handlers/records-subscribe.d.ts +4 -5
- package/dist/types/src/handlers/records-subscribe.d.ts.map +1 -1
- package/dist/types/src/handlers/records-write.d.ts +3 -11
- package/dist/types/src/handlers/records-write.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +16 -18
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/interfaces/messages-query.d.ts +23 -0
- package/dist/types/src/interfaces/messages-query.d.ts.map +1 -0
- package/dist/types/src/interfaces/protocols-configure.d.ts +3 -3
- package/dist/types/src/interfaces/protocols-configure.d.ts.map +1 -1
- package/dist/types/src/interfaces/protocols-query.d.ts +2 -2
- package/dist/types/src/interfaces/protocols-query.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-count.d.ts +3 -3
- package/dist/types/src/interfaces/records-count.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-delete.d.ts +11 -3
- package/dist/types/src/interfaces/records-delete.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-query.d.ts +3 -3
- package/dist/types/src/interfaces/records-query.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-read.d.ts +3 -3
- package/dist/types/src/interfaces/records-read.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-subscribe.d.ts +3 -3
- package/dist/types/src/interfaces/records-subscribe.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-write.d.ts +15 -7
- package/dist/types/src/interfaces/records-write.d.ts.map +1 -1
- package/dist/types/src/protocols/permissions.d.ts +9 -12
- package/dist/types/src/protocols/permissions.d.ts.map +1 -1
- package/dist/types/src/store/index-level.d.ts +10 -1
- package/dist/types/src/store/index-level.d.ts.map +1 -1
- package/dist/types/src/store/level-wrapper.d.ts +5 -0
- package/dist/types/src/store/level-wrapper.d.ts.map +1 -1
- package/dist/types/src/store/message-store-level.d.ts +94 -14
- package/dist/types/src/store/message-store-level.d.ts.map +1 -1
- package/dist/types/src/store/storage-controller.d.ts +17 -14
- package/dist/types/src/store/storage-controller.d.ts.map +1 -1
- package/dist/types/src/types/message-store.d.ts +29 -1
- package/dist/types/src/types/message-store.d.ts.map +1 -1
- package/dist/types/src/types/message-types.d.ts +2 -0
- package/dist/types/src/types/message-types.d.ts.map +1 -1
- package/dist/types/src/types/messages-types.d.ts +21 -55
- package/dist/types/src/types/messages-types.d.ts.map +1 -1
- package/dist/types/src/types/method-handler.d.ts +2 -2
- package/dist/types/src/types/method-handler.d.ts.map +1 -1
- package/dist/types/src/types/permission-types.d.ts +1 -1
- package/dist/types/src/types/subscriptions.d.ts +50 -39
- package/dist/types/src/types/subscriptions.d.ts.map +1 -1
- package/dist/types/src/types/validation-state-reader.d.ts +116 -0
- package/dist/types/src/types/validation-state-reader.d.ts.map +1 -0
- package/dist/types/src/utils/messages.d.ts +10 -0
- package/dist/types/src/utils/messages.d.ts.map +1 -1
- package/dist/types/src/utils/record-limit-occupancy.d.ts +40 -0
- package/dist/types/src/utils/record-limit-occupancy.d.ts.map +1 -0
- package/dist/types/src/utils/records.d.ts +25 -3
- package/dist/types/src/utils/records.d.ts.map +1 -1
- package/dist/types/src/utils/replication.d.ts +22 -0
- package/dist/types/src/utils/replication.d.ts.map +1 -0
- package/dist/types/tests/core/process-message-parity.spec.d.ts +2 -0
- package/dist/types/tests/core/process-message-parity.spec.d.ts.map +1 -0
- package/dist/types/tests/core/replication-apply.spec.d.ts +2 -0
- package/dist/types/tests/core/replication-apply.spec.d.ts.map +1 -0
- package/dist/types/tests/core/replication-replay-property.spec.d.ts +2 -0
- package/dist/types/tests/core/replication-replay-property.spec.d.ts.map +1 -0
- package/dist/types/tests/core/validation-read-closure.spec.d.ts +2 -0
- package/dist/types/tests/core/validation-read-closure.spec.d.ts.map +1 -0
- package/dist/types/tests/core/validation-state-reader.spec.d.ts +2 -0
- package/dist/types/tests/core/validation-state-reader.spec.d.ts.map +1 -0
- package/dist/types/tests/durable-event-log.spec.d.ts +2 -0
- package/dist/types/tests/durable-event-log.spec.d.ts.map +1 -0
- package/dist/types/tests/dwn.spec.d.ts.map +1 -1
- package/dist/types/tests/features/author-delegated-grant.spec.d.ts.map +1 -1
- package/dist/types/tests/features/owner-delegated-grant.spec.d.ts.map +1 -1
- package/dist/types/tests/features/owner-signature.spec.d.ts.map +1 -1
- package/dist/types/tests/features/permissions.spec.d.ts.map +1 -1
- package/dist/types/tests/features/protocol-composition.spec.d.ts.map +1 -1
- package/dist/types/tests/features/protocol-create-action.spec.d.ts.map +1 -1
- package/dist/types/tests/features/protocol-delete-action.spec.d.ts.map +1 -1
- package/dist/types/tests/features/protocol-update-action.spec.d.ts.map +1 -1
- package/dist/types/tests/features/records-delivery.spec.d.ts.map +1 -1
- package/dist/types/tests/features/records-immutable.spec.d.ts.map +1 -1
- package/dist/types/tests/features/records-nested-query-scope.spec.d.ts +2 -0
- package/dist/types/tests/features/records-nested-query-scope.spec.d.ts.map +1 -0
- package/dist/types/tests/features/records-prune-cross-protocol.spec.d.ts.map +1 -1
- package/dist/types/tests/features/records-prune.spec.d.ts.map +1 -1
- package/dist/types/tests/features/records-record-limit.spec.d.ts.map +1 -1
- package/dist/types/tests/features/records-squash.spec.d.ts.map +1 -1
- package/dist/types/tests/features/records-tags.spec.d.ts.map +1 -1
- package/dist/types/tests/features/resumable-tasks.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/messages-query.spec.d.ts +2 -0
- package/dist/types/tests/handlers/messages-query.spec.d.ts.map +1 -0
- package/dist/types/tests/handlers/messages-read.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/messages-subscribe.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/protocols-configure.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/protocols-query.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-count.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-delete.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-query.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-read.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-subscribe.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-write.spec.d.ts.map +1 -1
- package/dist/types/tests/scenarios/deleted-record.spec.d.ts.map +1 -1
- package/dist/types/tests/scenarios/end-to-end-tests.spec.d.ts.map +1 -1
- package/dist/types/tests/scenarios/nested-roles.spec.d.ts.map +1 -1
- package/dist/types/tests/scenarios/subscriptions.spec.d.ts.map +1 -1
- package/dist/types/tests/store/message-store.spec.d.ts.map +1 -1
- package/dist/types/tests/test-event-stream.d.ts +1 -1
- package/dist/types/tests/test-event-stream.d.ts.map +1 -1
- package/dist/types/tests/test-stores.d.ts +5 -4
- package/dist/types/tests/test-stores.d.ts.map +1 -1
- package/dist/types/tests/test-suite.d.ts +1 -2
- package/dist/types/tests/test-suite.d.ts.map +1 -1
- package/dist/types/tests/utils/test-data-generator.d.ts +20 -1
- package/dist/types/tests/utils/test-data-generator.d.ts.map +1 -1
- package/dist/types/tests/utils/test-validation-state-reader.d.ts +15 -0
- package/dist/types/tests/utils/test-validation-state-reader.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/core/core-protocol.ts +3 -3
- package/src/core/dwn-constant.ts +7 -1
- package/src/core/dwn-error.ts +13 -7
- package/src/core/grant-authorization.ts +11 -20
- package/src/core/message-reply.ts +6 -5
- package/src/core/messages-grant-authorization.ts +37 -100
- package/src/core/protocol-authorization-action.ts +29 -38
- package/src/core/protocol-authorization-validation.ts +41 -98
- package/src/core/protocol-authorization.ts +56 -202
- package/src/core/protocols-grant-authorization.ts +9 -9
- package/src/core/recording-validation-state-reader.ts +130 -0
- package/src/core/records-grant-authorization.ts +16 -16
- package/src/core/replication-apply.ts +412 -0
- package/src/core/resumable-task-manager.ts +10 -8
- package/src/core/validation-state-reader.ts +350 -0
- package/src/dwn.ts +417 -30
- package/src/enums/dwn-interface-method.ts +0 -1
- package/src/event-stream/durable-event-log.ts +509 -0
- package/src/event-stream/event-emitter-wake-publisher.ts +34 -0
- package/src/handlers/messages-query.ts +203 -0
- package/src/handlers/messages-read.ts +9 -10
- package/src/handlers/messages-subscribe.ts +12 -13
- package/src/handlers/protocols-configure.ts +37 -58
- package/src/handlers/protocols-query.ts +1 -1
- package/src/handlers/records-count.ts +24 -17
- package/src/handlers/records-delete.ts +29 -27
- package/src/handlers/records-query.ts +38 -17
- package/src/handlers/records-read.ts +63 -50
- package/src/handlers/records-subscribe.ts +132 -19
- package/src/handlers/records-write.ts +77 -168
- package/src/index.ts +16 -20
- package/src/interfaces/messages-query.ts +70 -0
- package/src/interfaces/protocols-configure.ts +12 -4
- package/src/interfaces/protocols-query.ts +4 -5
- package/src/interfaces/records-count.ts +9 -4
- package/src/interfaces/records-delete.ts +25 -5
- package/src/interfaces/records-query.ts +9 -4
- package/src/interfaces/records-read.ts +4 -4
- package/src/interfaces/records-subscribe.ts +9 -4
- package/src/interfaces/records-write.ts +41 -13
- package/src/protocols/permissions.ts +32 -52
- package/src/store/index-level.ts +30 -9
- package/src/store/level-wrapper.ts +9 -1
- package/src/store/message-store-level.ts +757 -47
- package/src/store/storage-controller.ts +74 -63
- package/src/types/message-store.ts +45 -2
- package/src/types/message-types.ts +3 -1
- package/src/types/messages-types.ts +26 -65
- package/src/types/method-handler.ts +3 -3
- package/src/types/permission-types.ts +1 -1
- package/src/types/subscriptions.ts +53 -42
- package/src/types/validation-state-reader.ts +127 -0
- package/src/utils/messages.ts +25 -1
- package/src/utils/record-limit-occupancy.ts +377 -0
- package/src/utils/records.ts +69 -13
- package/src/utils/replication.ts +122 -0
- package/dist/esm/src/core/record-chain.js +0 -64
- package/dist/esm/src/core/record-chain.js.map +0 -1
- package/dist/esm/src/event-stream/event-emitter-event-log.js +0 -334
- package/dist/esm/src/event-stream/event-emitter-event-log.js.map +0 -1
- package/dist/esm/src/handlers/messages-sync.js +0 -581
- package/dist/esm/src/handlers/messages-sync.js.map +0 -1
- package/dist/esm/src/interfaces/messages-sync.js +0 -54
- package/dist/esm/src/interfaces/messages-sync.js.map +0 -1
- package/dist/esm/src/smt/smt-store-level.js +0 -103
- package/dist/esm/src/smt/smt-store-level.js.map +0 -1
- package/dist/esm/src/smt/smt-store-memory.js +0 -41
- package/dist/esm/src/smt/smt-store-memory.js.map +0 -1
- package/dist/esm/src/smt/smt-utils.js +0 -129
- package/dist/esm/src/smt/smt-utils.js.map +0 -1
- package/dist/esm/src/smt/sparse-merkle-tree.js +0 -577
- package/dist/esm/src/smt/sparse-merkle-tree.js.map +0 -1
- package/dist/esm/src/state-index/state-index-level.js +0 -191
- package/dist/esm/src/state-index/state-index-level.js.map +0 -1
- package/dist/esm/src/sync/records-projection.js +0 -228
- package/dist/esm/src/sync/records-projection.js.map +0 -1
- package/dist/esm/src/types/smt-types.js +0 -5
- package/dist/esm/src/types/smt-types.js.map +0 -1
- package/dist/esm/src/types/state-index.js +0 -2
- package/dist/esm/src/types/state-index.js.map +0 -1
- package/dist/esm/tests/event-emitter-event-log.spec.js +0 -499
- package/dist/esm/tests/event-emitter-event-log.spec.js.map +0 -1
- package/dist/esm/tests/handlers/messages-sync.spec.js +0 -1771
- package/dist/esm/tests/handlers/messages-sync.spec.js.map +0 -1
- package/dist/esm/tests/smt/smt-store-level.spec.js +0 -132
- package/dist/esm/tests/smt/smt-store-level.spec.js.map +0 -1
- package/dist/esm/tests/smt/sparse-merkle-tree.spec.js +0 -732
- package/dist/esm/tests/smt/sparse-merkle-tree.spec.js.map +0 -1
- package/dist/esm/tests/state-index/state-index-level.spec.js +0 -245
- package/dist/esm/tests/state-index/state-index-level.spec.js.map +0 -1
- package/dist/esm/tests/sync/records-projection.spec.js +0 -245
- package/dist/esm/tests/sync/records-projection.spec.js.map +0 -1
- package/dist/types/src/core/record-chain.d.ts +0 -24
- package/dist/types/src/core/record-chain.d.ts.map +0 -1
- package/dist/types/src/event-stream/event-emitter-event-log.d.ts +0 -80
- package/dist/types/src/event-stream/event-emitter-event-log.d.ts.map +0 -1
- package/dist/types/src/handlers/messages-sync.d.ts +0 -83
- package/dist/types/src/handlers/messages-sync.d.ts.map +0 -1
- package/dist/types/src/interfaces/messages-sync.d.ts +0 -23
- package/dist/types/src/interfaces/messages-sync.d.ts.map +0 -1
- package/dist/types/src/smt/smt-store-level.d.ts +0 -32
- package/dist/types/src/smt/smt-store-level.d.ts.map +0 -1
- package/dist/types/src/smt/smt-store-memory.d.ts +0 -22
- package/dist/types/src/smt/smt-store-memory.d.ts.map +0 -1
- package/dist/types/src/smt/smt-utils.d.ts +0 -58
- package/dist/types/src/smt/smt-utils.d.ts.map +0 -1
- package/dist/types/src/smt/sparse-merkle-tree.d.ts +0 -124
- package/dist/types/src/smt/sparse-merkle-tree.d.ts.map +0 -1
- package/dist/types/src/state-index/state-index-level.d.ts +0 -83
- package/dist/types/src/state-index/state-index-level.d.ts.map +0 -1
- package/dist/types/src/sync/records-projection.d.ts +0 -98
- package/dist/types/src/sync/records-projection.d.ts.map +0 -1
- package/dist/types/src/types/smt-types.d.ts +0 -81
- package/dist/types/src/types/smt-types.d.ts.map +0 -1
- package/dist/types/src/types/state-index.d.ts +0 -90
- package/dist/types/src/types/state-index.d.ts.map +0 -1
- package/dist/types/tests/event-emitter-event-log.spec.d.ts +0 -2
- package/dist/types/tests/event-emitter-event-log.spec.d.ts.map +0 -1
- package/dist/types/tests/handlers/messages-sync.spec.d.ts +0 -2
- package/dist/types/tests/handlers/messages-sync.spec.d.ts.map +0 -1
- package/dist/types/tests/smt/smt-store-level.spec.d.ts +0 -2
- package/dist/types/tests/smt/smt-store-level.spec.d.ts.map +0 -1
- package/dist/types/tests/smt/sparse-merkle-tree.spec.d.ts +0 -2
- package/dist/types/tests/smt/sparse-merkle-tree.spec.d.ts.map +0 -1
- package/dist/types/tests/state-index/state-index-level.spec.d.ts +0 -2
- package/dist/types/tests/state-index/state-index-level.spec.d.ts.map +0 -1
- package/dist/types/tests/sync/records-projection.spec.d.ts +0 -2
- package/dist/types/tests/sync/records-projection.spec.d.ts.map +0 -1
- package/src/core/record-chain.ts +0 -99
- package/src/event-stream/event-emitter-event-log.ts +0 -430
- package/src/handlers/messages-sync.ts +0 -896
- package/src/interfaces/messages-sync.ts +0 -86
- package/src/smt/smt-store-level.ts +0 -143
- package/src/smt/smt-store-memory.ts +0 -53
- package/src/smt/smt-utils.ts +0 -149
- package/src/smt/sparse-merkle-tree.ts +0 -698
- package/src/state-index/state-index-level.ts +0 -239
- package/src/sync/records-projection.ts +0 -328
- package/src/types/smt-types.ts +0 -95
- package/src/types/state-index.ts +0 -100
|
@@ -1,896 +0,0 @@
|
|
|
1
|
-
import type { GenericMessage } from '../types/message-types.js';
|
|
2
|
-
import type { MessageStore } from '../types/message-store.js';
|
|
3
|
-
import type { StateIndex } from '../types/state-index.js';
|
|
4
|
-
import type { HandlerDependencies, MethodHandler } from '../types/method-handler.js';
|
|
5
|
-
import type { MessagesSyncDependencyEntry, MessagesSyncDiffEntry, MessagesSyncMessage, MessagesSyncReply } from '../types/messages-types.js';
|
|
6
|
-
import type { RecordsProjectionScope, RecordsProjectionSnapshot } from '../sync/records-projection.js';
|
|
7
|
-
|
|
8
|
-
import { authenticate } from '../core/auth.js';
|
|
9
|
-
import { DwnConstant } from '../core/dwn-constant.js';
|
|
10
|
-
import { Encoder } from '../utils/encoder.js';
|
|
11
|
-
import { hashToHex } from '../smt/smt-utils.js';
|
|
12
|
-
import { Message } from '../core/message.js';
|
|
13
|
-
import { messageReplyFromError } from '../core/message-reply.js';
|
|
14
|
-
import { MessagesGrantAuthorization } from '../core/messages-grant-authorization.js';
|
|
15
|
-
import { MessagesSync } from '../interfaces/messages-sync.js';
|
|
16
|
-
import { Records } from '../utils/records.js';
|
|
17
|
-
import { RecordsProjection } from '../sync/records-projection.js';
|
|
18
|
-
import { RecordsWrite } from '../interfaces/records-write.js';
|
|
19
|
-
import { SortDirection } from '../types/query-types.js';
|
|
20
|
-
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
21
|
-
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Maximum inline data size for diff responses — aligned with the
|
|
25
|
-
* {@link DwnConstant.maxDataSizeAllowedToBeEncoded} threshold (30 KB).
|
|
26
|
-
* RecordsWrite data payloads smaller than this are base64url-encoded and
|
|
27
|
-
* included directly in the diff reply. Larger payloads must be fetched
|
|
28
|
-
* separately via MessagesRead.
|
|
29
|
-
*/
|
|
30
|
-
const DEFAULT_MAX_INLINE_DATA_SIZE = DwnConstant.maxDataSizeAllowedToBeEncoded;
|
|
31
|
-
|
|
32
|
-
type StoredMessageWithEncodedData = GenericMessage & { encodedData?: string };
|
|
33
|
-
type StoredMessageWithInternalFields = GenericMessage & {
|
|
34
|
-
encodedData?: unknown;
|
|
35
|
-
initialWrite?: unknown;
|
|
36
|
-
};
|
|
37
|
-
type RecordsWriteProtocolDescriptor = GenericMessage['descriptor'] & { protocol?: unknown };
|
|
38
|
-
type RecordsWriteProtocolMetadata = { protocol: string; messageTimestamp: string };
|
|
39
|
-
type ProtocolsConfigureDefinitionDescriptor = GenericMessage['descriptor'] & {
|
|
40
|
-
definition?: {
|
|
41
|
-
protocol?: unknown;
|
|
42
|
-
uses?: Record<string, unknown>;
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
type ProjectionScopes = readonly [RecordsProjectionScope, ...RecordsProjectionScope[]];
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
export class MessagesSyncHandler implements MethodHandler {
|
|
49
|
-
|
|
50
|
-
constructor(private readonly deps: HandlerDependencies) { }
|
|
51
|
-
|
|
52
|
-
public async handle({
|
|
53
|
-
tenant,
|
|
54
|
-
message
|
|
55
|
-
}: { tenant: string, message: MessagesSyncMessage }): Promise<MessagesSyncReply> {
|
|
56
|
-
let messagesSync: MessagesSync;
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
messagesSync = await MessagesSync.parse(message);
|
|
60
|
-
} catch (e) {
|
|
61
|
-
return messageReplyFromError(e, 400);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
await authenticate(message.authorization, this.deps.didResolver);
|
|
66
|
-
await MessagesSyncHandler.authorizeMessagesSync(tenant, messagesSync, this.deps.messageStore);
|
|
67
|
-
} catch (e) {
|
|
68
|
-
return messageReplyFromError(e, 401);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const { action } = message.descriptor;
|
|
72
|
-
const projectionScopes = MessagesSyncHandler.getProjectionScopes(message);
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
switch (action) {
|
|
76
|
-
case 'root': {
|
|
77
|
-
return await this.handleRoot(tenant, message, projectionScopes);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
case 'subtree': {
|
|
81
|
-
return await this.handleSubtree(tenant, message, projectionScopes);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
case 'leaves': {
|
|
85
|
-
return await this.handleLeaves(tenant, message, projectionScopes);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
case 'diff': {
|
|
89
|
-
return await this.handleDiff(tenant, message);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
default: {
|
|
93
|
-
return {
|
|
94
|
-
status: { code: 400, detail: `Unknown action: ${action as string}` },
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
} catch (e) {
|
|
99
|
-
return messageReplyFromError(e, 500);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
private async handleRoot(
|
|
104
|
-
tenant: string,
|
|
105
|
-
message: MessagesSyncMessage,
|
|
106
|
-
projectionScopes: ProjectionScopes | undefined
|
|
107
|
-
): Promise<MessagesSyncReply> {
|
|
108
|
-
const root = await this.getRootHex(tenant, message.descriptor.protocol, projectionScopes);
|
|
109
|
-
return {
|
|
110
|
-
status : { code: 200, detail: 'OK' },
|
|
111
|
-
root : root,
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
private async getRootHex(
|
|
116
|
-
tenant: string,
|
|
117
|
-
protocol: string | undefined,
|
|
118
|
-
projectionScopes: ProjectionScopes | undefined
|
|
119
|
-
): Promise<string> {
|
|
120
|
-
if (projectionScopes === undefined) {
|
|
121
|
-
const rootHash = await this.getIndexedRootHash(tenant, protocol);
|
|
122
|
-
return hashToHex(rootHash);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return this.withProjectionSnapshot(tenant, projectionScopes, snapshot => snapshot.getRootHex());
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
private async getIndexedRootHash(
|
|
129
|
-
tenant: string,
|
|
130
|
-
protocol: string | undefined
|
|
131
|
-
): Promise<Uint8Array> {
|
|
132
|
-
if (protocol === undefined) {
|
|
133
|
-
return this.deps.stateIndex!.getRoot(tenant);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return this.deps.stateIndex!.getProtocolRoot(tenant, protocol);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
private async handleSubtree(
|
|
140
|
-
tenant: string,
|
|
141
|
-
message: MessagesSyncMessage,
|
|
142
|
-
projectionScopes: ProjectionScopes | undefined
|
|
143
|
-
): Promise<MessagesSyncReply> {
|
|
144
|
-
const bitPath = MessagesSyncHandler.parseBitPrefix(message.descriptor.prefix!);
|
|
145
|
-
const hash = await this.getSubtreeHash(tenant, message.descriptor.protocol, projectionScopes, bitPath);
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
status : { code: 200, detail: 'OK' },
|
|
149
|
-
hash : hashToHex(hash),
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
private async handleLeaves(
|
|
154
|
-
tenant: string,
|
|
155
|
-
message: MessagesSyncMessage,
|
|
156
|
-
projectionScopes: ProjectionScopes | undefined
|
|
157
|
-
): Promise<MessagesSyncReply> {
|
|
158
|
-
const bitPath = MessagesSyncHandler.parseBitPrefix(message.descriptor.prefix!);
|
|
159
|
-
const leaves = await this.getLeaves(tenant, message.descriptor.protocol, projectionScopes, bitPath);
|
|
160
|
-
|
|
161
|
-
return {
|
|
162
|
-
status : { code: 200, detail: 'OK' },
|
|
163
|
-
entries : leaves,
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
private async getSubtreeHash(
|
|
168
|
-
tenant: string,
|
|
169
|
-
protocol: string | undefined,
|
|
170
|
-
projectionScopes: ProjectionScopes | undefined,
|
|
171
|
-
bitPath: boolean[]
|
|
172
|
-
): Promise<Uint8Array> {
|
|
173
|
-
if (projectionScopes === undefined) {
|
|
174
|
-
return this.getIndexedSubtreeHash(tenant, protocol, bitPath);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return this.withProjectionSnapshot(tenant, projectionScopes, snapshot => snapshot.getSubtreeHash(bitPath));
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
private async getLeaves(
|
|
181
|
-
tenant: string,
|
|
182
|
-
protocol: string | undefined,
|
|
183
|
-
projectionScopes: ProjectionScopes | undefined,
|
|
184
|
-
bitPath: boolean[]
|
|
185
|
-
): Promise<string[]> {
|
|
186
|
-
if (projectionScopes === undefined) {
|
|
187
|
-
return this.getIndexedLeaves(tenant, protocol, bitPath);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return this.withProjectionSnapshot(tenant, projectionScopes, snapshot => snapshot.getLeaves(bitPath));
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
private async getIndexedSubtreeHash(
|
|
194
|
-
tenant: string,
|
|
195
|
-
protocol: string | undefined,
|
|
196
|
-
bitPath: boolean[]
|
|
197
|
-
): Promise<Uint8Array> {
|
|
198
|
-
return MessagesSyncHandler.getIndexedSubtreeHashFromStateIndex(this.deps.stateIndex!, tenant, protocol, bitPath);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
private async getIndexedLeaves(
|
|
202
|
-
tenant: string,
|
|
203
|
-
protocol: string | undefined,
|
|
204
|
-
bitPath: boolean[]
|
|
205
|
-
): Promise<string[]> {
|
|
206
|
-
return MessagesSyncHandler.getIndexedLeavesFromStateIndex(this.deps.stateIndex!, tenant, protocol, bitPath);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Handle the 'diff' action: the client sends its subtree hashes at a given
|
|
211
|
-
* depth, and the server compares them against its own tree to compute the
|
|
212
|
-
* set difference in a single round-trip.
|
|
213
|
-
*
|
|
214
|
-
* Response includes:
|
|
215
|
-
* - `onlyRemote`: messages the server has that the client doesn't, with
|
|
216
|
-
* inline data for small payloads.
|
|
217
|
-
* - `onlyLocal`: bit prefixes where the client has entries the server
|
|
218
|
-
* doesn't (client can enumerate its own leaves for these prefixes).
|
|
219
|
-
*/
|
|
220
|
-
private async handleDiff(
|
|
221
|
-
tenant: string,
|
|
222
|
-
message: MessagesSyncMessage,
|
|
223
|
-
): Promise<MessagesSyncReply> {
|
|
224
|
-
const { protocol, hashes: clientHashes, depth } = message.descriptor;
|
|
225
|
-
const projectionScopes = MessagesSyncHandler.getProjectionScopes(message);
|
|
226
|
-
|
|
227
|
-
if (!clientHashes || depth === undefined) {
|
|
228
|
-
return {
|
|
229
|
-
status: { code: 400, detail: 'diff action requires hashes and depth' },
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const stateIndex = this.deps.stateIndex!;
|
|
234
|
-
const projectionSnapshot = await this.createProjectionSnapshot(tenant, projectionScopes);
|
|
235
|
-
const onlyRemoteCids: string[] = [];
|
|
236
|
-
const onlyLocalPrefixes: string[] = [];
|
|
237
|
-
|
|
238
|
-
try {
|
|
239
|
-
// Get the default (empty subtree) hash at the given depth so we can
|
|
240
|
-
// filter out client entries that represent empty subtrees.
|
|
241
|
-
const defaultHashHex = await this.getDefaultHashHex(depth);
|
|
242
|
-
|
|
243
|
-
// Build the set of all prefixes at the given depth that either side has.
|
|
244
|
-
// Filter out client prefixes whose hash equals the default (empty subtree)
|
|
245
|
-
// hash — these represent empty subtrees and should be treated the same as
|
|
246
|
-
// omitted prefixes.
|
|
247
|
-
const allPrefixes = new Set<string>();
|
|
248
|
-
for (const [pfx, hash] of Object.entries(clientHashes)) {
|
|
249
|
-
if (hash !== defaultHashHex) {
|
|
250
|
-
allPrefixes.add(pfx);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Enumerate server-side non-empty prefixes by walking the server's
|
|
255
|
-
// tree to the given depth and collecting leaf prefixes.
|
|
256
|
-
const serverHashes = await this.collectSubtreeHashes(tenant, protocol, projectionSnapshot, depth);
|
|
257
|
-
for (const prefix of Object.keys(serverHashes)) {
|
|
258
|
-
allPrefixes.add(prefix);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// Compare each prefix's hash between client and server.
|
|
262
|
-
for (const pfx of allPrefixes) {
|
|
263
|
-
const clientHash = clientHashes[pfx]; // undefined if client has empty subtree
|
|
264
|
-
const serverHash = serverHashes[pfx]; // undefined if server has empty subtree
|
|
265
|
-
|
|
266
|
-
if (clientHash === serverHash) {
|
|
267
|
-
// Identical subtree — skip.
|
|
268
|
-
continue;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (serverHash === undefined) {
|
|
272
|
-
// Client has entries the server doesn't.
|
|
273
|
-
onlyLocalPrefixes.push(pfx);
|
|
274
|
-
continue;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const bitPath = MessagesSyncHandler.parseBitPrefix(pfx);
|
|
278
|
-
const serverLeaves = await MessagesSyncHandler.getServerLeaves(
|
|
279
|
-
stateIndex,
|
|
280
|
-
tenant,
|
|
281
|
-
protocol,
|
|
282
|
-
projectionSnapshot,
|
|
283
|
-
bitPath
|
|
284
|
-
);
|
|
285
|
-
|
|
286
|
-
onlyRemoteCids.push(...serverLeaves);
|
|
287
|
-
if (clientHash !== undefined) {
|
|
288
|
-
// Both sides have entries but they differ. The client will enumerate
|
|
289
|
-
// its own leaves for this prefix and de-duplicate server leaves.
|
|
290
|
-
onlyLocalPrefixes.push(pfx);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
} finally {
|
|
294
|
-
await projectionSnapshot?.close();
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// Build response entries with inline message data where possible.
|
|
298
|
-
const onlyRemote = await this.buildDiffEntries(tenant, onlyRemoteCids);
|
|
299
|
-
const dependencies = projectionScopes === undefined
|
|
300
|
-
? []
|
|
301
|
-
: await this.buildProjectedDependencyEntries(tenant, onlyRemote);
|
|
302
|
-
|
|
303
|
-
return {
|
|
304
|
-
status : { code: 200, detail: 'OK' },
|
|
305
|
-
onlyRemote,
|
|
306
|
-
onlyLocal : onlyLocalPrefixes,
|
|
307
|
-
...(dependencies.length > 0 ? { dependencies } : {}),
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Walk the server's SMT to the given depth and collect all non-empty
|
|
313
|
-
* subtree hashes as a `{ prefix: hexHash }` map.
|
|
314
|
-
*/
|
|
315
|
-
private async collectSubtreeHashes(
|
|
316
|
-
tenant: string,
|
|
317
|
-
protocol: string | undefined,
|
|
318
|
-
projectionSnapshot: RecordsProjectionSnapshot | undefined,
|
|
319
|
-
depth: number,
|
|
320
|
-
): Promise<Record<string, string>> {
|
|
321
|
-
const stateIndex = this.deps.stateIndex!;
|
|
322
|
-
const result: Record<string, string> = {};
|
|
323
|
-
|
|
324
|
-
const walk = async (prefix: string, currentDepth: number): Promise<void> => {
|
|
325
|
-
const bitPath = MessagesSyncHandler.parseBitPrefix(prefix);
|
|
326
|
-
const hash = await MessagesSyncHandler.getServerSubtreeHash(
|
|
327
|
-
stateIndex,
|
|
328
|
-
tenant,
|
|
329
|
-
protocol,
|
|
330
|
-
projectionSnapshot,
|
|
331
|
-
bitPath
|
|
332
|
-
);
|
|
333
|
-
const hexHash = hashToHex(hash);
|
|
334
|
-
const defaultHashHex = await this.getDefaultHashHex(currentDepth);
|
|
335
|
-
|
|
336
|
-
if (hexHash === defaultHashHex) {
|
|
337
|
-
// Empty subtree — don't include in the result.
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
if (currentDepth >= depth) {
|
|
342
|
-
// Reached target depth with a non-empty subtree.
|
|
343
|
-
result[prefix] = hexHash;
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
// Recurse into children.
|
|
348
|
-
await Promise.all([
|
|
349
|
-
walk(prefix + '0', currentDepth + 1),
|
|
350
|
-
walk(prefix + '1', currentDepth + 1),
|
|
351
|
-
]);
|
|
352
|
-
};
|
|
353
|
-
|
|
354
|
-
await walk('', 0);
|
|
355
|
-
return result;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
private async createProjectionSnapshot(
|
|
359
|
-
tenant: string,
|
|
360
|
-
projectionScopes: ProjectionScopes | undefined
|
|
361
|
-
): Promise<RecordsProjectionSnapshot | undefined> {
|
|
362
|
-
if (projectionScopes === undefined) {
|
|
363
|
-
return undefined;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
return RecordsProjection.createSnapshot({
|
|
367
|
-
tenant,
|
|
368
|
-
messageStore : this.deps.messageStore,
|
|
369
|
-
scopes : projectionScopes,
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
private static async getServerLeaves(
|
|
374
|
-
stateIndex: StateIndex,
|
|
375
|
-
tenant: string,
|
|
376
|
-
protocol: string | undefined,
|
|
377
|
-
projectionSnapshot: RecordsProjectionSnapshot | undefined,
|
|
378
|
-
bitPath: boolean[]
|
|
379
|
-
): Promise<string[]> {
|
|
380
|
-
if (projectionSnapshot === undefined) {
|
|
381
|
-
return MessagesSyncHandler.getIndexedLeavesFromStateIndex(stateIndex, tenant, protocol, bitPath);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
return projectionSnapshot.getLeaves(bitPath);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
private static async getServerSubtreeHash(
|
|
388
|
-
stateIndex: StateIndex,
|
|
389
|
-
tenant: string,
|
|
390
|
-
protocol: string | undefined,
|
|
391
|
-
projectionSnapshot: RecordsProjectionSnapshot | undefined,
|
|
392
|
-
bitPath: boolean[]
|
|
393
|
-
): Promise<Uint8Array> {
|
|
394
|
-
if (projectionSnapshot === undefined) {
|
|
395
|
-
return MessagesSyncHandler.getIndexedSubtreeHashFromStateIndex(stateIndex, tenant, protocol, bitPath);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
return projectionSnapshot.getSubtreeHash(bitPath);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
private static async getIndexedLeavesFromStateIndex(
|
|
402
|
-
stateIndex: StateIndex,
|
|
403
|
-
tenant: string,
|
|
404
|
-
protocol: string | undefined,
|
|
405
|
-
bitPath: boolean[]
|
|
406
|
-
): Promise<string[]> {
|
|
407
|
-
if (protocol === undefined) {
|
|
408
|
-
return stateIndex.getLeaves(tenant, bitPath);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
return stateIndex.getProtocolLeaves(tenant, protocol, bitPath);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
private static async getIndexedSubtreeHashFromStateIndex(
|
|
415
|
-
stateIndex: StateIndex,
|
|
416
|
-
tenant: string,
|
|
417
|
-
protocol: string | undefined,
|
|
418
|
-
bitPath: boolean[]
|
|
419
|
-
): Promise<Uint8Array> {
|
|
420
|
-
if (protocol === undefined) {
|
|
421
|
-
return stateIndex.getSubtreeHash(tenant, bitPath);
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
return stateIndex.getProtocolSubtreeHash(tenant, protocol, bitPath);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
* Get the hex-encoded default hash for a given depth. Lazily cached.
|
|
429
|
-
*/
|
|
430
|
-
private _defaultHashHexCache?: Map<number, string>;
|
|
431
|
-
private async getDefaultHashHex(depth: number): Promise<string> {
|
|
432
|
-
if (this._defaultHashHexCache === undefined) {
|
|
433
|
-
const { initDefaultHashes } = await import('../smt/smt-utils.js');
|
|
434
|
-
const defaults = await initDefaultHashes();
|
|
435
|
-
this._defaultHashHexCache = new Map<number, string>();
|
|
436
|
-
for (let d = 0; d <= 256; d++) {
|
|
437
|
-
this._defaultHashHexCache.set(d, hashToHex(defaults[d]));
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
return this._defaultHashHexCache.get(depth) ?? '';
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* Build diff response entries for the given messageCids.
|
|
445
|
-
* Reads each message from the MessageStore and, for small RecordsWrite
|
|
446
|
-
* payloads, inlines the data as base64url.
|
|
447
|
-
*/
|
|
448
|
-
private async buildDiffEntries(
|
|
449
|
-
tenant: string,
|
|
450
|
-
messageCids: string[],
|
|
451
|
-
): Promise<MessagesSyncDiffEntry[]> {
|
|
452
|
-
const entries: MessagesSyncDiffEntry[] = [];
|
|
453
|
-
|
|
454
|
-
for (const messageCid of messageCids) {
|
|
455
|
-
const { message, encodedData: inlineData, data } = await this.readMessageByCid(tenant, messageCid);
|
|
456
|
-
if (!message) {
|
|
457
|
-
// Message was deleted between diff computation and read — skip.
|
|
458
|
-
continue;
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
const entry: MessagesSyncDiffEntry = { messageCid, message };
|
|
462
|
-
|
|
463
|
-
// Use inline data from the MessageStore if available (small payloads).
|
|
464
|
-
if (inlineData) {
|
|
465
|
-
entry.encodedData = inlineData;
|
|
466
|
-
} else if (data) {
|
|
467
|
-
// Data is in the DataStore — inline it if small enough.
|
|
468
|
-
const bytes = await MessagesSyncHandler.streamToBytes(data);
|
|
469
|
-
if (bytes.byteLength <= DEFAULT_MAX_INLINE_DATA_SIZE) {
|
|
470
|
-
entry.encodedData = Encoder.bytesToBase64Url(bytes);
|
|
471
|
-
}
|
|
472
|
-
// Large payloads are NOT inlined — client fetches via MessagesRead.
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
entries.push(entry);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
return entries;
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
private async buildProjectedDependencyEntries(
|
|
482
|
-
tenant: string,
|
|
483
|
-
primaryEntries: MessagesSyncDiffEntry[],
|
|
484
|
-
): Promise<MessagesSyncDependencyEntry[]> {
|
|
485
|
-
const dependenciesByCid = new Map<string, MessagesSyncDependencyEntry>();
|
|
486
|
-
const configsByProtocol = new Map<string, GenericMessage[]>();
|
|
487
|
-
|
|
488
|
-
for (const primaryEntry of primaryEntries) {
|
|
489
|
-
const protocolMetadata = MessagesSyncHandler.recordsWriteProtocolMetadata(primaryEntry.message);
|
|
490
|
-
if (protocolMetadata !== undefined) {
|
|
491
|
-
await this.addProtocolConfigClosureDependencies(
|
|
492
|
-
tenant,
|
|
493
|
-
protocolMetadata.protocol,
|
|
494
|
-
protocolMetadata.messageTimestamp,
|
|
495
|
-
primaryEntry.messageCid,
|
|
496
|
-
configsByProtocol,
|
|
497
|
-
dependenciesByCid,
|
|
498
|
-
);
|
|
499
|
-
continue;
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
const initialWrite = await this.readRecordsDeleteInitialWrite(tenant, primaryEntry.message);
|
|
503
|
-
if (initialWrite === undefined) {
|
|
504
|
-
continue;
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
await MessagesSyncHandler.addRecordsInitialWriteDependency(
|
|
508
|
-
primaryEntry.messageCid,
|
|
509
|
-
initialWrite,
|
|
510
|
-
dependenciesByCid,
|
|
511
|
-
);
|
|
512
|
-
|
|
513
|
-
const initialWriteMetadata = MessagesSyncHandler.recordsWriteProtocolMetadata(initialWrite);
|
|
514
|
-
if (initialWriteMetadata === undefined) {
|
|
515
|
-
continue;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
await this.addProtocolConfigClosureDependencies(
|
|
519
|
-
tenant,
|
|
520
|
-
initialWriteMetadata.protocol,
|
|
521
|
-
initialWriteMetadata.messageTimestamp,
|
|
522
|
-
primaryEntry.messageCid,
|
|
523
|
-
configsByProtocol,
|
|
524
|
-
dependenciesByCid,
|
|
525
|
-
);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
return [...dependenciesByCid.values()];
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
private async readRecordsDeleteInitialWrite(
|
|
532
|
-
tenant: string,
|
|
533
|
-
message: GenericMessage | undefined,
|
|
534
|
-
): Promise<GenericMessage | undefined> {
|
|
535
|
-
const recordId = MessagesSyncHandler.recordsDeleteRecordId(message);
|
|
536
|
-
if (recordId === undefined) {
|
|
537
|
-
return undefined;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
const { messages } = await this.deps.messageStore.query(tenant, [{
|
|
541
|
-
interface : DwnInterfaceName.Records,
|
|
542
|
-
method : DwnMethodName.Write,
|
|
543
|
-
recordId,
|
|
544
|
-
}]);
|
|
545
|
-
const initialWrite = await RecordsWrite.getInitialWrite(messages);
|
|
546
|
-
return initialWrite === undefined ? undefined : MessagesSyncHandler.toWireMessage(initialWrite);
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
private static async addRecordsInitialWriteDependency(
|
|
550
|
-
rootMessageCid: string,
|
|
551
|
-
dependency: GenericMessage,
|
|
552
|
-
dependenciesByCid: Map<string, MessagesSyncDependencyEntry>,
|
|
553
|
-
): Promise<void> {
|
|
554
|
-
const dependencyCid = await Message.getCid(dependency);
|
|
555
|
-
if (dependenciesByCid.has(dependencyCid)) {
|
|
556
|
-
return;
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
dependenciesByCid.set(dependencyCid, {
|
|
560
|
-
dependencyClass : 'recordsInitialWrite',
|
|
561
|
-
messageCid : dependencyCid,
|
|
562
|
-
message : dependency,
|
|
563
|
-
rootMessageCid,
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
private async addProtocolConfigClosureDependencies(
|
|
568
|
-
tenant: string,
|
|
569
|
-
rootProtocol: string,
|
|
570
|
-
rootMessageTimestamp: string,
|
|
571
|
-
rootMessageCid: string,
|
|
572
|
-
configsByProtocol: Map<string, GenericMessage[]>,
|
|
573
|
-
dependenciesByCid: Map<string, MessagesSyncDependencyEntry>,
|
|
574
|
-
): Promise<void> {
|
|
575
|
-
// Dependency hints are not part of the projected root; they are advisory
|
|
576
|
-
// bootstrap data for the receiver. Bound each closure to the primary
|
|
577
|
-
// RecordsWrite timestamp because protocol authorization uses the definition
|
|
578
|
-
// active when that record was created, not whatever config is newest today.
|
|
579
|
-
const visitedProtocols = new Set<string>();
|
|
580
|
-
const pendingProtocols = [rootProtocol];
|
|
581
|
-
|
|
582
|
-
for (
|
|
583
|
-
let protocol = MessagesSyncHandler.takeNextUnvisitedProtocol(pendingProtocols, visitedProtocols);
|
|
584
|
-
protocol !== undefined;
|
|
585
|
-
protocol = MessagesSyncHandler.takeNextUnvisitedProtocol(pendingProtocols, visitedProtocols)
|
|
586
|
-
) {
|
|
587
|
-
const configs = await this.getCachedGoverningProtocolsConfigure(
|
|
588
|
-
tenant,
|
|
589
|
-
protocol,
|
|
590
|
-
rootMessageTimestamp,
|
|
591
|
-
configsByProtocol,
|
|
592
|
-
);
|
|
593
|
-
await MessagesSyncHandler.addProtocolConfigDependencies({
|
|
594
|
-
configs,
|
|
595
|
-
rootMessageCid,
|
|
596
|
-
visitedProtocols,
|
|
597
|
-
pendingProtocols,
|
|
598
|
-
dependenciesByCid,
|
|
599
|
-
});
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
private async getCachedGoverningProtocolsConfigure(
|
|
604
|
-
tenant: string,
|
|
605
|
-
protocol: string,
|
|
606
|
-
messageTimestamp: string,
|
|
607
|
-
configsByProtocol: Map<string, GenericMessage[]>,
|
|
608
|
-
): Promise<GenericMessage[]> {
|
|
609
|
-
const configCacheKey = JSON.stringify([protocol, messageTimestamp]);
|
|
610
|
-
const cachedConfigs = configsByProtocol.get(configCacheKey);
|
|
611
|
-
if (cachedConfigs !== undefined) {
|
|
612
|
-
return cachedConfigs;
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
const configs = await this.readGoverningProtocolsConfigure(tenant, protocol, messageTimestamp);
|
|
616
|
-
configsByProtocol.set(configCacheKey, configs);
|
|
617
|
-
return configs;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
private static async addProtocolConfigDependencies({
|
|
621
|
-
configs,
|
|
622
|
-
rootMessageCid,
|
|
623
|
-
visitedProtocols,
|
|
624
|
-
pendingProtocols,
|
|
625
|
-
dependenciesByCid,
|
|
626
|
-
}: {
|
|
627
|
-
configs: GenericMessage[];
|
|
628
|
-
rootMessageCid: string;
|
|
629
|
-
visitedProtocols: Set<string>;
|
|
630
|
-
pendingProtocols: string[];
|
|
631
|
-
dependenciesByCid: Map<string, MessagesSyncDependencyEntry>;
|
|
632
|
-
}): Promise<void> {
|
|
633
|
-
for (const dependency of configs) {
|
|
634
|
-
await MessagesSyncHandler.addProtocolConfigDependency(rootMessageCid, dependency, dependenciesByCid);
|
|
635
|
-
MessagesSyncHandler.queueUnvisitedProtocols(
|
|
636
|
-
MessagesSyncHandler.protocolsConfigureUses(dependency),
|
|
637
|
-
visitedProtocols,
|
|
638
|
-
pendingProtocols,
|
|
639
|
-
);
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
private static async addProtocolConfigDependency(
|
|
644
|
-
rootMessageCid: string,
|
|
645
|
-
dependency: GenericMessage,
|
|
646
|
-
dependenciesByCid: Map<string, MessagesSyncDependencyEntry>,
|
|
647
|
-
): Promise<void> {
|
|
648
|
-
const dependencyCid = await Message.getCid(dependency);
|
|
649
|
-
if (dependenciesByCid.has(dependencyCid)) {
|
|
650
|
-
return;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
dependenciesByCid.set(dependencyCid, {
|
|
654
|
-
dependencyClass : 'protocolsConfigure',
|
|
655
|
-
messageCid : dependencyCid,
|
|
656
|
-
message : dependency,
|
|
657
|
-
rootMessageCid,
|
|
658
|
-
});
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
private static takeNextUnvisitedProtocol(
|
|
662
|
-
pendingProtocols: string[],
|
|
663
|
-
visitedProtocols: Set<string>,
|
|
664
|
-
): string | undefined {
|
|
665
|
-
while (pendingProtocols.length > 0) {
|
|
666
|
-
const protocol = pendingProtocols.shift()!;
|
|
667
|
-
if (visitedProtocols.has(protocol)) {
|
|
668
|
-
continue;
|
|
669
|
-
}
|
|
670
|
-
visitedProtocols.add(protocol);
|
|
671
|
-
return protocol;
|
|
672
|
-
}
|
|
673
|
-
return undefined;
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
private static queueUnvisitedProtocols(
|
|
677
|
-
protocols: string[],
|
|
678
|
-
visitedProtocols: Set<string>,
|
|
679
|
-
pendingProtocols: string[],
|
|
680
|
-
): void {
|
|
681
|
-
for (const protocol of protocols) {
|
|
682
|
-
if (!visitedProtocols.has(protocol)) {
|
|
683
|
-
pendingProtocols.push(protocol);
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
private static protocolsConfigureUses(message: GenericMessage): string[] {
|
|
689
|
-
if (
|
|
690
|
-
message.descriptor.interface !== DwnInterfaceName.Protocols ||
|
|
691
|
-
message.descriptor.method !== DwnMethodName.Configure
|
|
692
|
-
) {
|
|
693
|
-
return [];
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
const uses = (message.descriptor as ProtocolsConfigureDefinitionDescriptor).definition?.uses;
|
|
697
|
-
return uses === undefined
|
|
698
|
-
? []
|
|
699
|
-
: Object.values(uses).filter((protocol): protocol is string => typeof protocol === 'string');
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
private async readGoverningProtocolsConfigure(
|
|
703
|
-
tenant: string,
|
|
704
|
-
protocol: string,
|
|
705
|
-
messageTimestamp: string,
|
|
706
|
-
): Promise<GenericMessage[]> {
|
|
707
|
-
const { messages } = await this.deps.messageStore.query(
|
|
708
|
-
tenant,
|
|
709
|
-
[{
|
|
710
|
-
interface : DwnInterfaceName.Protocols,
|
|
711
|
-
method : DwnMethodName.Configure,
|
|
712
|
-
protocol,
|
|
713
|
-
messageTimestamp : { lte: messageTimestamp },
|
|
714
|
-
}],
|
|
715
|
-
{ messageTimestamp: SortDirection.Descending },
|
|
716
|
-
);
|
|
717
|
-
|
|
718
|
-
const governingMessage = await Message.getNewestMessage(messages);
|
|
719
|
-
return governingMessage === undefined ? [] : [governingMessage];
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
private static recordsWriteProtocolMetadata(message: GenericMessage | undefined): RecordsWriteProtocolMetadata | undefined {
|
|
723
|
-
if (
|
|
724
|
-
message?.descriptor.interface !== DwnInterfaceName.Records ||
|
|
725
|
-
message.descriptor.method !== DwnMethodName.Write
|
|
726
|
-
) {
|
|
727
|
-
return undefined;
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
const protocol = (message.descriptor as RecordsWriteProtocolDescriptor).protocol;
|
|
731
|
-
return typeof protocol === 'string'
|
|
732
|
-
? { protocol, messageTimestamp: message.descriptor.messageTimestamp }
|
|
733
|
-
: undefined;
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
private static recordsDeleteRecordId(message: GenericMessage | undefined): string | undefined {
|
|
737
|
-
if (
|
|
738
|
-
message?.descriptor.interface !== DwnInterfaceName.Records ||
|
|
739
|
-
message.descriptor.method !== DwnMethodName.Delete
|
|
740
|
-
) {
|
|
741
|
-
return undefined;
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
const recordId = (message.descriptor as Record<string, unknown>).recordId;
|
|
745
|
-
return typeof recordId === 'string' ? recordId : undefined;
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
private static toWireMessage(message: GenericMessage): GenericMessage {
|
|
749
|
-
const { encodedData: _encodedData, initialWrite: _initialWrite, ...wireMessage } = message as StoredMessageWithInternalFields;
|
|
750
|
-
return wireMessage;
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
/**
|
|
754
|
-
* Read a message and its data from the MessageStore + DataStore by CID.
|
|
755
|
-
*/
|
|
756
|
-
private async readMessageByCid(
|
|
757
|
-
tenant: string,
|
|
758
|
-
messageCid: string,
|
|
759
|
-
): Promise<{ message?: GenericMessage; encodedData?: string; data?: ReadableStream<Uint8Array> }> {
|
|
760
|
-
const storedMessage = await this.deps.messageStore.get(tenant, messageCid);
|
|
761
|
-
if (!storedMessage) {
|
|
762
|
-
return {};
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
// Extract and strip `encodedData` from the stored message.
|
|
766
|
-
// `encodedData` is an internal storage optimization for small payloads
|
|
767
|
-
// that are stored inline in the MessageStore rather than in the DataStore.
|
|
768
|
-
// It must not be included in the wire-format message (the recipient's
|
|
769
|
-
// DWN would reject it as an unexpected top-level property).
|
|
770
|
-
let inlineEncodedData: string | undefined;
|
|
771
|
-
if (MessagesSyncHandler.hasEncodedData(storedMessage)) {
|
|
772
|
-
inlineEncodedData = storedMessage.encodedData;
|
|
773
|
-
delete storedMessage.encodedData;
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
let data: ReadableStream<Uint8Array> | undefined;
|
|
777
|
-
|
|
778
|
-
// Check if this is a RecordsWrite with data that is small enough to inline.
|
|
779
|
-
if (inlineEncodedData === undefined && Records.isRecordsWrite(storedMessage)) {
|
|
780
|
-
const { dataCid, dataSize } = storedMessage.descriptor;
|
|
781
|
-
if (dataSize <= DEFAULT_MAX_INLINE_DATA_SIZE) {
|
|
782
|
-
// Some stores may have small payload bytes in DataStore instead of encodedData.
|
|
783
|
-
if (this.deps.dataStore) {
|
|
784
|
-
// DataStore uses recordId, not messageCid. For RecordsWrite,
|
|
785
|
-
// recordId is a top-level message property.
|
|
786
|
-
const dataResult = await this.deps.dataStore.get(tenant, storedMessage.recordId, dataCid);
|
|
787
|
-
if (dataResult?.dataStream) {
|
|
788
|
-
data = dataResult.dataStream;
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
|
|
794
|
-
return { message: storedMessage, encodedData: inlineEncodedData, data };
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
private static hasEncodedData(message: GenericMessage): message is StoredMessageWithEncodedData {
|
|
798
|
-
return 'encodedData' in message && typeof message.encodedData === 'string';
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
private static getProjectionScopes(
|
|
802
|
-
message: MessagesSyncMessage,
|
|
803
|
-
): ProjectionScopes | undefined {
|
|
804
|
-
const { projectionScopes } = message.descriptor;
|
|
805
|
-
if (projectionScopes === undefined) {
|
|
806
|
-
return undefined;
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
return projectionScopes as [RecordsProjectionScope, ...RecordsProjectionScope[]];
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
private async withProjectionSnapshot<T>(
|
|
813
|
-
tenant: string,
|
|
814
|
-
scopes: readonly [RecordsProjectionScope, ...RecordsProjectionScope[]],
|
|
815
|
-
fn: (snapshot: RecordsProjectionSnapshot) => Promise<T>,
|
|
816
|
-
): Promise<T> {
|
|
817
|
-
const snapshot = await RecordsProjection.createSnapshot({
|
|
818
|
-
tenant : tenant,
|
|
819
|
-
messageStore : this.deps.messageStore,
|
|
820
|
-
scopes : scopes,
|
|
821
|
-
});
|
|
822
|
-
|
|
823
|
-
try {
|
|
824
|
-
return await fn(snapshot);
|
|
825
|
-
} finally {
|
|
826
|
-
await snapshot.close();
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
/**
|
|
831
|
-
* Read a ReadableStream to completion and return the bytes.
|
|
832
|
-
*/
|
|
833
|
-
private static async streamToBytes(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> {
|
|
834
|
-
const reader = stream.getReader();
|
|
835
|
-
const chunks: Uint8Array[] = [];
|
|
836
|
-
let totalSize = 0;
|
|
837
|
-
|
|
838
|
-
for (;;) {
|
|
839
|
-
const { done, value } = await reader.read();
|
|
840
|
-
if (done) { break; }
|
|
841
|
-
chunks.push(value);
|
|
842
|
-
totalSize += value.byteLength;
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
const result = new Uint8Array(totalSize);
|
|
846
|
-
let offset = 0;
|
|
847
|
-
for (const chunk of chunks) {
|
|
848
|
-
result.set(chunk, offset);
|
|
849
|
-
offset += chunk.byteLength;
|
|
850
|
-
}
|
|
851
|
-
return result;
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
/**
|
|
855
|
-
* Parse a bit prefix string (e.g. "0110101") into a boolean array.
|
|
856
|
-
*/
|
|
857
|
-
private static parseBitPrefix(prefix: string): boolean[] {
|
|
858
|
-
if (!/^[01]*$/.test(prefix)) {
|
|
859
|
-
throw new DwnError(
|
|
860
|
-
DwnErrorCode.MessagesSyncInvalidPrefix,
|
|
861
|
-
`Invalid prefix: must contain only '0' and '1' characters, got: ${prefix}`
|
|
862
|
-
);
|
|
863
|
-
}
|
|
864
|
-
if (prefix.length > 256) {
|
|
865
|
-
throw new DwnError(
|
|
866
|
-
DwnErrorCode.MessagesSyncInvalidPrefix,
|
|
867
|
-
`Invalid prefix: length must be <= 256, got: ${prefix.length}`
|
|
868
|
-
);
|
|
869
|
-
}
|
|
870
|
-
return Array.from(prefix, (ch): boolean => ch === '1');
|
|
871
|
-
}
|
|
872
|
-
|
|
873
|
-
private static async authorizeMessagesSync(
|
|
874
|
-
tenant: string,
|
|
875
|
-
messagesSync: MessagesSync,
|
|
876
|
-
messageStore: MessageStore
|
|
877
|
-
): Promise<void> {
|
|
878
|
-
if (messagesSync.author === tenant) {
|
|
879
|
-
return;
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
const permissionGrantIds = Message.getPermissionGrantIds(messagesSync.signaturePayload!);
|
|
883
|
-
if (messagesSync.author !== undefined && permissionGrantIds.length > 0) {
|
|
884
|
-
const permissionGrants = await MessagesGrantAuthorization.fetchPermissionGrants(tenant, messageStore, permissionGrantIds);
|
|
885
|
-
await MessagesGrantAuthorization.authorizeSubscribeOrSync({
|
|
886
|
-
incomingMessage : messagesSync.message,
|
|
887
|
-
expectedGrantor : tenant,
|
|
888
|
-
expectedGrantee : messagesSync.author,
|
|
889
|
-
permissionGrants,
|
|
890
|
-
messageStore
|
|
891
|
-
});
|
|
892
|
-
} else {
|
|
893
|
-
throw new DwnError(DwnErrorCode.MessagesSyncAuthorizationFailed, 'message failed authorization');
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
}
|