@dxos/echo-pipeline 0.8.4-main.84f28bd → 0.8.4-main.8baae0fced
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 +102 -5
- package/README.md +1 -1
- package/dist/lib/neutral/chunk-46DCRDA7.mjs +271 -0
- package/dist/lib/neutral/chunk-46DCRDA7.mjs.map +7 -0
- package/dist/lib/{browser/chunk-GBFX5J5B.mjs → neutral/chunk-LR7UFVQX.mjs} +294 -690
- package/dist/lib/neutral/chunk-LR7UFVQX.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/filter/index.mjs +3 -2
- package/dist/lib/neutral/index.mjs +7508 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/{browser → neutral}/testing/index.mjs +97 -308
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/automerge/automerge-host-subduction.test.d.ts +2 -0
- package/dist/types/src/automerge/automerge-host-subduction.test.d.ts.map +1 -0
- package/dist/types/src/automerge/automerge-host.d.ts +184 -55
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-repo-subduction-policy.test.d.ts +2 -0
- package/dist/types/src/automerge/automerge-repo-subduction-policy.test.d.ts.map +1 -0
- package/dist/types/src/automerge/automerge-repo-subduction.test.d.ts +2 -0
- package/dist/types/src/automerge/automerge-repo-subduction.test.d.ts.map +1 -0
- package/dist/types/src/automerge/automerge-repo.test.d.ts +1 -1
- package/dist/types/src/automerge/automerge-repo.test.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-subduction.test.d.ts +2 -0
- package/dist/types/src/automerge/automerge-subduction.test.d.ts.map +1 -0
- package/dist/types/src/automerge/collection-synchronizer.d.ts +59 -5
- package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-data-monitor.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts +23 -10
- package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-replicator.d.ts +46 -15
- package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/errors.d.ts +35 -0
- package/dist/types/src/automerge/errors.d.ts.map +1 -0
- package/dist/types/src/automerge/handle-state.d.ts +52 -0
- package/dist/types/src/automerge/handle-state.d.ts.map +1 -0
- package/dist/types/src/automerge/heads-store.d.ts +9 -2
- package/dist/types/src/automerge/heads-store.d.ts.map +1 -1
- package/dist/types/src/automerge/index.d.ts +3 -1
- package/dist/types/src/automerge/index.d.ts.map +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +11 -5
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +9 -8
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts +6 -5
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/network-protocol.d.ts.map +1 -1
- package/dist/types/src/automerge/space-collection.d.ts.map +1 -1
- package/dist/types/src/automerge/subduction-test-utils.d.ts +132 -0
- package/dist/types/src/automerge/subduction-test-utils.d.ts.map +1 -0
- package/dist/types/src/common/codec.d.ts +1 -1
- package/dist/types/src/common/codec.d.ts.map +1 -1
- package/dist/types/src/common/feeds.d.ts.map +1 -1
- package/dist/types/src/common/space-id.d.ts.map +1 -1
- package/dist/types/src/db-host/automerge-data-source.d.ts +35 -0
- package/dist/types/src/db-host/automerge-data-source.d.ts.map +1 -0
- package/dist/types/src/db-host/automerge-data-source.test.d.ts +2 -0
- package/dist/types/src/db-host/automerge-data-source.test.d.ts.map +1 -0
- package/dist/types/src/db-host/automerge-metrics.d.ts.map +1 -1
- package/dist/types/src/db-host/data-service.d.ts +13 -7
- package/dist/types/src/db-host/data-service.d.ts.map +1 -1
- package/dist/types/src/db-host/database-root.d.ts +9 -3
- package/dist/types/src/db-host/database-root.d.ts.map +1 -1
- package/dist/types/src/db-host/documents-synchronizer.d.ts +30 -8
- package/dist/types/src/db-host/documents-synchronizer.d.ts.map +1 -1
- package/dist/types/src/db-host/echo-host.d.ts +74 -28
- package/dist/types/src/db-host/echo-host.d.ts.map +1 -1
- package/dist/types/src/db-host/index.d.ts +2 -0
- package/dist/types/src/db-host/index.d.ts.map +1 -1
- package/dist/types/src/db-host/invalidation-hint.d.ts +19 -0
- package/dist/types/src/db-host/invalidation-hint.d.ts.map +1 -0
- package/dist/types/src/db-host/local-queue-service.d.ts +19 -0
- package/dist/types/src/db-host/local-queue-service.d.ts.map +1 -0
- package/dist/types/src/db-host/query-invalidation.test.d.ts +2 -0
- package/dist/types/src/db-host/query-invalidation.test.d.ts.map +1 -0
- package/dist/types/src/db-host/query-service.d.ts +19 -11
- package/dist/types/src/db-host/query-service.d.ts.map +1 -1
- package/dist/types/src/db-host/queue-data-source.d.ts +38 -0
- package/dist/types/src/db-host/queue-data-source.d.ts.map +1 -0
- package/dist/types/src/db-host/queue-service.test.d.ts +2 -0
- package/dist/types/src/db-host/queue-service.test.d.ts.map +1 -0
- package/dist/types/src/db-host/space-state-manager.d.ts +4 -3
- package/dist/types/src/db-host/space-state-manager.d.ts.map +1 -1
- package/dist/types/src/db-host/stub.d.ts +11 -0
- package/dist/types/src/db-host/stub.d.ts.map +1 -0
- package/dist/types/src/edge/echo-edge-replicator.d.ts +10 -7
- package/dist/types/src/edge/echo-edge-replicator.d.ts.map +1 -1
- package/dist/types/src/edge/echo-edge-subduction-replicator.d.ts +39 -0
- package/dist/types/src/edge/echo-edge-subduction-replicator.d.ts.map +1 -0
- package/dist/types/src/edge/echo-edge-subduction-replicator.test.d.ts +2 -0
- package/dist/types/src/edge/echo-edge-subduction-replicator.test.d.ts.map +1 -0
- package/dist/types/src/edge/index.d.ts +1 -0
- package/dist/types/src/edge/index.d.ts.map +1 -1
- package/dist/types/src/edge/inflight-request-limiter.d.ts.map +1 -1
- package/dist/types/src/filter/filter-match.d.ts +4 -1
- package/dist/types/src/filter/filter-match.d.ts.map +1 -1
- package/dist/types/src/metadata/metadata-store.d.ts +1 -1
- package/dist/types/src/metadata/metadata-store.d.ts.map +1 -1
- package/dist/types/src/pipeline/message-selector.d.ts.map +1 -1
- package/dist/types/src/pipeline/pipeline.d.ts +3 -3
- package/dist/types/src/pipeline/pipeline.d.ts.map +1 -1
- package/dist/types/src/pipeline/timeframe-clock.d.ts.map +1 -1
- package/dist/types/src/query/errors.d.ts +22 -10
- package/dist/types/src/query/errors.d.ts.map +1 -1
- package/dist/types/src/query/plan.d.ts +66 -8
- package/dist/types/src/query/plan.d.ts.map +1 -1
- package/dist/types/src/query/query-executor.d.ts +66 -11
- package/dist/types/src/query/query-executor.d.ts.map +1 -1
- package/dist/types/src/query/query-planner.d.ts +12 -1
- package/dist/types/src/query/query-planner.d.ts.map +1 -1
- package/dist/types/src/space/admission-discovery-extension.d.ts.map +1 -1
- package/dist/types/src/space/auth.d.ts +3 -3
- package/dist/types/src/space/auth.d.ts.map +1 -1
- package/dist/types/src/space/control-pipeline.d.ts +5 -4
- package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts +8 -8
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space-protocol.d.ts +10 -9
- package/dist/types/src/space/space-protocol.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts +6 -6
- package/dist/types/src/space/space.d.ts.map +1 -1
- package/dist/types/src/testing/change-metadata.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +0 -2
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-agent-builder.d.ts +2 -2
- package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-feed-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-network-adapter.d.ts +1 -1
- package/dist/types/src/testing/test-network-adapter.d.ts.map +1 -1
- package/dist/types/src/testing/test-replicator.d.ts +30 -15
- package/dist/types/src/testing/test-replicator.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +1 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +59 -55
- package/src/automerge/automerge-host-subduction.test.ts +833 -0
- package/src/automerge/automerge-host.test.ts +162 -39
- package/src/automerge/automerge-host.ts +885 -213
- package/src/automerge/automerge-repo-subduction-policy.test.ts +1105 -0
- package/src/automerge/automerge-repo-subduction.test.ts +690 -0
- package/src/automerge/automerge-repo.test.ts +346 -90
- package/src/automerge/automerge-subduction.test.ts +330 -0
- package/src/automerge/collection-synchronizer.test.ts +208 -17
- package/src/automerge/collection-synchronizer.ts +229 -39
- package/src/automerge/echo-data-monitor.ts +1 -1
- package/src/automerge/echo-network-adapter.test.ts +5 -4
- package/src/automerge/echo-network-adapter.ts +74 -30
- package/src/automerge/echo-replicator.ts +50 -15
- package/src/automerge/errors.ts +10 -0
- package/src/automerge/handle-state.ts +62 -0
- package/src/automerge/heads-store.ts +42 -3
- package/src/automerge/index.ts +3 -1
- package/src/automerge/leveldb-storage-adapter.ts +77 -11
- package/src/automerge/mesh-echo-replicator-connection.ts +12 -8
- package/src/automerge/mesh-echo-replicator.ts +15 -11
- package/src/automerge/storage-adapter.test.ts +1 -1
- package/src/automerge/subduction-test-utils.ts +417 -0
- package/src/common/space-id.ts +1 -1
- package/src/db-host/automerge-data-source.test.ts +299 -0
- package/src/db-host/automerge-data-source.ts +187 -0
- package/src/db-host/data-service.ts +41 -26
- package/src/db-host/database-root.ts +20 -8
- package/src/db-host/documents-synchronizer.test.ts +145 -14
- package/src/db-host/documents-synchronizer.ts +163 -51
- package/src/db-host/echo-host.ts +384 -99
- package/src/db-host/index.ts +2 -0
- package/src/db-host/invalidation-hint.ts +50 -0
- package/src/db-host/local-queue-service.ts +127 -0
- package/src/db-host/query-invalidation.test.ts +270 -0
- package/src/db-host/query-service.ts +111 -106
- package/src/db-host/queue-data-source.ts +160 -0
- package/src/db-host/queue-service.test.ts +145 -0
- package/src/db-host/space-state-manager.ts +12 -10
- package/src/db-host/stub.ts +33 -0
- package/src/edge/echo-edge-replicator.test.ts +12 -10
- package/src/edge/echo-edge-replicator.ts +144 -58
- package/src/edge/echo-edge-subduction-replicator.test.ts +158 -0
- package/src/edge/echo-edge-subduction-replicator.ts +377 -0
- package/src/edge/index.ts +1 -0
- package/src/filter/filter-match.test.ts +68 -32
- package/src/filter/filter-match.ts +230 -21
- package/src/metadata/metadata-store.ts +11 -8
- package/src/pipeline/pipeline-stress.test.ts +1 -1
- package/src/pipeline/pipeline.test.ts +2 -2
- package/src/pipeline/pipeline.ts +9 -7
- package/src/query/errors.ts +3 -1
- package/src/query/plan.ts +84 -9
- package/src/query/query-executor.ts +1197 -165
- package/src/query/query-planner.test.ts +993 -81
- package/src/query/query-planner.ts +503 -27
- package/src/space/admission-discovery-extension.ts +3 -3
- package/src/space/auth.ts +6 -6
- package/src/space/control-pipeline.test.ts +5 -4
- package/src/space/control-pipeline.ts +16 -13
- package/src/space/space-manager.browser.test.ts +1 -1
- package/src/space/space-manager.ts +16 -20
- package/src/space/space-protocol.browser.test.ts +15 -14
- package/src/space/space-protocol.test.ts +15 -14
- package/src/space/space-protocol.ts +22 -18
- package/src/space/space.test.ts +3 -3
- package/src/space/space.ts +11 -11
- package/src/testing/index.ts +0 -2
- package/src/testing/test-agent-builder.ts +3 -3
- package/src/testing/test-network-adapter.ts +24 -3
- package/src/testing/test-replicator.ts +62 -26
- package/src/util.ts +7 -3
- package/dist/lib/browser/chunk-ANZAS5CC.mjs +0 -126
- package/dist/lib/browser/chunk-ANZAS5CC.mjs.map +0 -7
- package/dist/lib/browser/chunk-CGS2ULMK.mjs +0 -11
- package/dist/lib/browser/chunk-CGS2ULMK.mjs.map +0 -7
- package/dist/lib/browser/chunk-GBFX5J5B.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -4743
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-2SAZ7CCF.mjs +0 -126
- package/dist/lib/node-esm/chunk-2SAZ7CCF.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FQFKWA3X.mjs +0 -2067
- package/dist/lib/node-esm/chunk-FQFKWA3X.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/filter/index.mjs +0 -11
- package/dist/lib/node-esm/filter/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -4743
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -742
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/db-host/documents-iterator.d.ts +0 -7
- package/dist/types/src/db-host/documents-iterator.d.ts.map +0 -1
- package/dist/types/src/testing/test-data.d.ts +0 -18
- package/dist/types/src/testing/test-data.d.ts.map +0 -1
- package/dist/types/src/testing/test-schema.d.ts +0 -37
- package/dist/types/src/testing/test-schema.d.ts.map +0 -1
- package/src/db-host/documents-iterator.ts +0 -74
- package/src/testing/test-data.ts +0 -127
- package/src/testing/test-schema.ts +0 -53
- /package/dist/lib/{browser → neutral}/filter/index.mjs.map +0 -0
|
@@ -2,20 +2,25 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { type Heads } from '@automerge/automerge';
|
|
6
6
|
import type { DocumentId, PeerId } from '@automerge/automerge-repo';
|
|
7
|
+
import * as Array from 'effect/Array';
|
|
8
|
+
import * as Record from 'effect/Record';
|
|
7
9
|
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
+
import { Event, asyncReturn, scheduleTask, scheduleTaskInterval } from '@dxos/async';
|
|
11
|
+
import { type Context, Resource } from '@dxos/context';
|
|
12
|
+
import { isEdgePeerId } from '@dxos/echo-protocol';
|
|
10
13
|
import { log } from '@dxos/log';
|
|
11
14
|
import { trace } from '@dxos/tracing';
|
|
12
15
|
import { defaultMap } from '@dxos/util';
|
|
13
16
|
|
|
17
|
+
import { PeerNotFoundError } from './errors';
|
|
18
|
+
|
|
14
19
|
const MIN_QUERY_INTERVAL = 5_000;
|
|
15
20
|
|
|
16
|
-
const POLL_INTERVAL =
|
|
21
|
+
const POLL_INTERVAL = 10_000;
|
|
17
22
|
|
|
18
|
-
export type
|
|
23
|
+
export type CollectionSynchronizerProps = {
|
|
19
24
|
sendCollectionState: (collectionId: string, peerId: PeerId, state: CollectionState) => void;
|
|
20
25
|
queryCollectionState: (collectionId: string, peerId: PeerId) => void;
|
|
21
26
|
shouldSyncCollection: (collectionId: string, peerId: PeerId) => boolean;
|
|
@@ -24,11 +29,11 @@ export type CollectionSynchronizerParams = {
|
|
|
24
29
|
/**
|
|
25
30
|
* Implements collection sync protocol.
|
|
26
31
|
*/
|
|
27
|
-
@trace.resource()
|
|
32
|
+
@trace.resource({ lifecycle: true })
|
|
28
33
|
export class CollectionSynchronizer extends Resource {
|
|
29
|
-
private readonly _sendCollectionState:
|
|
30
|
-
private readonly _queryCollectionState:
|
|
31
|
-
private readonly _shouldSyncCollection:
|
|
34
|
+
private readonly _sendCollectionState: CollectionSynchronizerProps['sendCollectionState'];
|
|
35
|
+
private readonly _queryCollectionState: CollectionSynchronizerProps['queryCollectionState'];
|
|
36
|
+
private readonly _shouldSyncCollection: CollectionSynchronizerProps['shouldSyncCollection'];
|
|
32
37
|
|
|
33
38
|
/**
|
|
34
39
|
* CollectionId -> State.
|
|
@@ -38,9 +43,13 @@ export class CollectionSynchronizer extends Resource {
|
|
|
38
43
|
|
|
39
44
|
private readonly _connectedPeers = new Set<PeerId>();
|
|
40
45
|
|
|
41
|
-
public readonly
|
|
46
|
+
public readonly peerCollectionStateUpdated = new Event<{
|
|
47
|
+
collectionId: string;
|
|
48
|
+
peerId: PeerId;
|
|
49
|
+
newDocsAppeared: boolean;
|
|
50
|
+
}>();
|
|
42
51
|
|
|
43
|
-
constructor(params:
|
|
52
|
+
constructor(params: CollectionSynchronizerProps) {
|
|
44
53
|
super();
|
|
45
54
|
this._sendCollectionState = params.sendCollectionState;
|
|
46
55
|
this._queryCollectionState = params.queryCollectionState;
|
|
@@ -74,13 +83,29 @@ export class CollectionSynchronizer extends Resource {
|
|
|
74
83
|
this._activeCollections.add(collectionId);
|
|
75
84
|
|
|
76
85
|
log('setLocalCollectionState', { collectionId, state });
|
|
77
|
-
this._getOrCreatePerCollectionState(collectionId)
|
|
86
|
+
const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
|
|
87
|
+
perCollectionState.localState = state;
|
|
78
88
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
89
|
+
for (const peerId of this._connectedPeers) {
|
|
90
|
+
this._diffCollectionState(collectionId, peerId);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
this._scheduleBroadcast(collectionId);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Coalesce bursts of `setLocalCollectionState` (e.g. from `_onHeadsChanged` during an
|
|
98
|
+
* import) into one broadcast microtask per collection. If another call arrives while
|
|
99
|
+
* the microtask is pending, we just flag the collection dirty — the in-flight run
|
|
100
|
+
* already reads the latest `localState`. If it arrives during the run itself, we
|
|
101
|
+
* schedule exactly one follow-up.
|
|
102
|
+
*/
|
|
103
|
+
private _scheduleBroadcast(collectionId: string): void {
|
|
104
|
+
const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
|
|
105
|
+
queueMicrotask(() => {
|
|
106
|
+
this._refreshInterestedPeers(collectionId);
|
|
107
|
+
this._broadcastLocalState(collectionId);
|
|
108
|
+
this.refreshCollection(collectionId);
|
|
84
109
|
});
|
|
85
110
|
}
|
|
86
111
|
|
|
@@ -117,10 +142,14 @@ export class CollectionSynchronizer extends Resource {
|
|
|
117
142
|
* Callback when a connection to a peer is established.
|
|
118
143
|
*/
|
|
119
144
|
onConnectionOpen(peerId: PeerId): void {
|
|
120
|
-
|
|
121
|
-
|
|
145
|
+
log('onConnectionOpen', { peerId });
|
|
146
|
+
const spanId = getSpanId(peerId);
|
|
147
|
+
// Browser-timeline-only span; the derived ctx is intentionally discarded because
|
|
148
|
+
// the downstream `_queryCollectionState` hop is user-supplied callback land with
|
|
149
|
+
// no ctx plumbing.
|
|
150
|
+
void trace.spanStart({
|
|
122
151
|
id: spanId,
|
|
123
|
-
methodName:
|
|
152
|
+
methodName: SYNC_SPAN_METHOD,
|
|
124
153
|
instance: this,
|
|
125
154
|
parentCtx: this._ctx,
|
|
126
155
|
showInBrowserTimeline: true,
|
|
@@ -146,21 +175,37 @@ export class CollectionSynchronizer extends Resource {
|
|
|
146
175
|
* Callback when a connection to a peer is closed.
|
|
147
176
|
*/
|
|
148
177
|
onConnectionClosed(peerId: PeerId): void {
|
|
178
|
+
log('onConnectionClosed', { peerId });
|
|
179
|
+
|
|
149
180
|
this._connectedPeers.delete(peerId);
|
|
150
181
|
|
|
151
182
|
for (const perCollectionState of this._perCollectionStates.values()) {
|
|
152
183
|
perCollectionState.remoteStates.delete(peerId);
|
|
184
|
+
perCollectionState.interestedPeers.delete(peerId);
|
|
185
|
+
perCollectionState.lastQueried.delete(peerId);
|
|
186
|
+
perCollectionState.lastBroadcast.delete(peerId);
|
|
153
187
|
}
|
|
154
188
|
}
|
|
155
189
|
|
|
156
190
|
/**
|
|
157
191
|
* Callback when a peer queries the state of a collection.
|
|
192
|
+
*
|
|
193
|
+
* If we have no local state yet we silently drop the query; the peer will receive our
|
|
194
|
+
* state via {@link _broadcastLocalState} once `setLocalCollectionState` is called.
|
|
158
195
|
*/
|
|
159
196
|
onCollectionStateQueried(collectionId: string, peerId: PeerId): void {
|
|
160
197
|
const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
|
|
161
198
|
|
|
162
199
|
if (perCollectionState.localState) {
|
|
163
|
-
|
|
200
|
+
try {
|
|
201
|
+
this._sendCollectionState(collectionId, peerId, withoutEmptyHeads(perCollectionState.localState));
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (PeerNotFoundError.is(error)) {
|
|
204
|
+
log('peer not found when sending collection state callback', { error });
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
164
209
|
}
|
|
165
210
|
}
|
|
166
211
|
|
|
@@ -171,25 +216,51 @@ export class CollectionSynchronizer extends Resource {
|
|
|
171
216
|
log('onRemoteStateReceived', { collectionId, peerId, state });
|
|
172
217
|
validateCollectionState(state);
|
|
173
218
|
const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
219
|
+
const previousRemoteState = perCollectionState.remoteStates.get(peerId);
|
|
220
|
+
if (previousRemoteState && isCollectionStateEqual(previousRemoteState, state)) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
perCollectionState.remoteStates.set(peerId, state);
|
|
224
|
+
this._diffCollectionState(collectionId, peerId);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private _diffCollectionState(collectionId: string, peerId: PeerId) {
|
|
228
|
+
const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
|
|
229
|
+
const remoteState = perCollectionState.remoteStates.get(peerId);
|
|
230
|
+
if (!remoteState) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
log('diffCollectionState', { collectionId, peerId });
|
|
235
|
+
const localState = perCollectionState.localState ?? { documents: {} };
|
|
236
|
+
const diff = diffCollectionStateForPeer(localState, remoteState, { isEdgePeer: isEdgePeerId(peerId) });
|
|
237
|
+
const spanId = getSpanId(peerId);
|
|
177
238
|
if (diff.different.length === 0) {
|
|
178
239
|
trace.spanEnd(spanId);
|
|
179
240
|
} else {
|
|
180
|
-
|
|
241
|
+
// Browser-timeline-only span; see note in onConnectionOpen.
|
|
242
|
+
void trace.spanStart({
|
|
181
243
|
id: spanId,
|
|
182
|
-
methodName:
|
|
244
|
+
methodName: SYNC_SPAN_METHOD,
|
|
183
245
|
instance: this,
|
|
184
246
|
parentCtx: this._ctx,
|
|
185
247
|
showInBrowserTimeline: true,
|
|
186
248
|
attributes: { peerId },
|
|
187
249
|
});
|
|
188
250
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
251
|
+
log('diff', {
|
|
252
|
+
localState: localState.documents,
|
|
253
|
+
remoteState: remoteState.documents,
|
|
254
|
+
missingOnLocal: diff.missingOnLocal,
|
|
255
|
+
missingOnRemote: diff.missingOnRemote,
|
|
256
|
+
different: diff.different,
|
|
257
|
+
});
|
|
258
|
+
log('emit peer collection state update');
|
|
259
|
+
this.peerCollectionStateUpdated.emit({
|
|
260
|
+
peerId,
|
|
261
|
+
collectionId,
|
|
262
|
+
newDocsAppeared: diff.missingOnLocal.length > 0,
|
|
263
|
+
});
|
|
193
264
|
}
|
|
194
265
|
|
|
195
266
|
private _getOrCreatePerCollectionState(collectionId: string): PerCollectionState {
|
|
@@ -198,9 +269,49 @@ export class CollectionSynchronizer extends Resource {
|
|
|
198
269
|
remoteStates: new Map(),
|
|
199
270
|
interestedPeers: new Set(),
|
|
200
271
|
lastQueried: new Map(),
|
|
272
|
+
lastBroadcast: new Map(),
|
|
201
273
|
}));
|
|
202
274
|
}
|
|
203
275
|
|
|
276
|
+
/**
|
|
277
|
+
* Push local state to interested peers whose last-known remote state differs from local
|
|
278
|
+
* (or is unknown), then pull from peers via {@link refreshCollection}.
|
|
279
|
+
*
|
|
280
|
+
* Diff-gating avoids spamming peers that are already in sync; this matters because
|
|
281
|
+
* {@link setLocalCollectionState} is called on every local heads change.
|
|
282
|
+
*/
|
|
283
|
+
private _broadcastLocalState(collectionId: string): void {
|
|
284
|
+
if (this._ctx.disposed || !this._activeCollections.has(collectionId)) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
|
|
288
|
+
const localState = perCollectionState.localState;
|
|
289
|
+
if (!localState) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
// Edge replicators are pull-only from our perspective: the edge is the source of truth and
|
|
293
|
+
// already holds every doc in the space, so proactively pushing our heads on every local
|
|
294
|
+
// change is wasted work. The edge can still pull our state via `onCollectionStateQueried`.
|
|
295
|
+
for (const peerId of perCollectionState.interestedPeers) {
|
|
296
|
+
if (isEdgePeerId(peerId)) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const lastBroadcast = perCollectionState.lastBroadcast.get(peerId) ?? 0;
|
|
300
|
+
if (Date.now() - lastBroadcast > MIN_QUERY_INTERVAL) {
|
|
301
|
+
perCollectionState.lastBroadcast.set(peerId, Date.now());
|
|
302
|
+
try {
|
|
303
|
+
this._sendCollectionState(collectionId, peerId, withoutEmptyHeads(localState));
|
|
304
|
+
} catch (error) {
|
|
305
|
+
if (PeerNotFoundError.is(error)) {
|
|
306
|
+
log('peer not found when broadcasting collection state', { error });
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
204
315
|
private _refreshInterestedPeers(collectionId: string): void {
|
|
205
316
|
for (const peerId of this._connectedPeers) {
|
|
206
317
|
if (this._shouldSyncCollection(collectionId, peerId)) {
|
|
@@ -217,13 +328,14 @@ type PerCollectionState = {
|
|
|
217
328
|
remoteStates: Map<PeerId, CollectionState>;
|
|
218
329
|
interestedPeers: Set<PeerId>;
|
|
219
330
|
lastQueried: Map<PeerId, number>;
|
|
331
|
+
lastBroadcast: Map<PeerId, number>;
|
|
220
332
|
};
|
|
221
333
|
|
|
222
334
|
export type CollectionState = {
|
|
223
335
|
/**
|
|
224
336
|
* DocumentId -> Heads.
|
|
225
337
|
*/
|
|
226
|
-
documents: Record<
|
|
338
|
+
documents: Record<DocumentId, Heads>;
|
|
227
339
|
};
|
|
228
340
|
|
|
229
341
|
export type CollectionStateDiff = {
|
|
@@ -232,19 +344,77 @@ export type CollectionStateDiff = {
|
|
|
232
344
|
different: DocumentId[];
|
|
233
345
|
};
|
|
234
346
|
|
|
347
|
+
export const isCollectionStateEqual = (local: CollectionState, remote: CollectionState): boolean => {
|
|
348
|
+
const diff = diffCollectionState(local, remote);
|
|
349
|
+
return diff.different.length === 0 && diff.missingOnLocal.length === 0 && diff.missingOnRemote.length === 0;
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Strip entries whose heads array is empty before sending a CollectionState
|
|
354
|
+
* over the wire. Empty-heads entries are inert (they don't participate in the
|
|
355
|
+
* diff thanks to `Record.filter` in {@link diffCollectionState}) but they
|
|
356
|
+
* inflate the on-wire payload AND end up advertised as "I know about this
|
|
357
|
+
* document with no commits" to the receiver — leaking sedimentree ids that
|
|
358
|
+
* leaked into the local store from other spaces via the subduction
|
|
359
|
+
* fingerprint exchange. Filtering at the send site is the cheapest place to
|
|
360
|
+
* keep collection-state semantics aligned with `diffCollectionState`.
|
|
361
|
+
*/
|
|
362
|
+
export const withoutEmptyHeads = (state: CollectionState): CollectionState => ({
|
|
363
|
+
documents: Record.filter(state.documents, (heads) => heads.length > 0),
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Restrict a remote {@link CollectionState} to the document keys present in `local`.
|
|
368
|
+
*
|
|
369
|
+
* Used when diffing against an edge peer: edge subduction storage retains every
|
|
370
|
+
* sedimentree id it has ever seen (old roots, partial creations, cross-space
|
|
371
|
+
* fingerprint leaks), so a raw symmetric diff would surface those as
|
|
372
|
+
* `missingOnLocal` forever. The local root's `links` are authoritative for
|
|
373
|
+
* "what docs are in this space"; intersecting the remote view with the local
|
|
374
|
+
* key set drops the noise without changing wire format.
|
|
375
|
+
*/
|
|
376
|
+
export const subsetRemoteToLocal = (local: CollectionState, remote: CollectionState): CollectionState => ({
|
|
377
|
+
documents: Record.filter(remote.documents, (_heads, documentId) => documentId in local.documents),
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Peer-aware wrapper around {@link diffCollectionState}.
|
|
382
|
+
*
|
|
383
|
+
* For mesh peers the diff is symmetric (every doc on either side participates).
|
|
384
|
+
* For edge peers we first intersect the remote state with the local key set —
|
|
385
|
+
* see {@link subsetRemoteToLocal} for why.
|
|
386
|
+
*/
|
|
387
|
+
export const diffCollectionStateForPeer = (
|
|
388
|
+
local: CollectionState,
|
|
389
|
+
remote: CollectionState,
|
|
390
|
+
{ isEdgePeer }: { isEdgePeer: boolean },
|
|
391
|
+
): CollectionStateDiff => {
|
|
392
|
+
const effectiveRemote = isEdgePeer ? subsetRemoteToLocal(local, remote) : remote;
|
|
393
|
+
return diffCollectionState(local, effectiveRemote);
|
|
394
|
+
};
|
|
395
|
+
|
|
235
396
|
export const diffCollectionState = (local: CollectionState, remote: CollectionState): CollectionStateDiff => {
|
|
236
|
-
const
|
|
397
|
+
const localDocuments = Record.filter(local.documents, (heads) => heads.length > 0);
|
|
398
|
+
const remoteDocuments = Record.filter(remote.documents, (heads) => heads.length > 0);
|
|
399
|
+
// NOTE: Using `Array.union` is slow.
|
|
400
|
+
const allDocuments = [...new Set([...Record.keys(localDocuments), ...Record.keys(remoteDocuments)])] as DocumentId[];
|
|
237
401
|
|
|
238
402
|
const missingOnRemote: DocumentId[] = [];
|
|
239
403
|
const missingOnLocal: DocumentId[] = [];
|
|
240
404
|
const different: DocumentId[] = [];
|
|
241
405
|
for (const documentId of allDocuments) {
|
|
242
|
-
if (!
|
|
243
|
-
missingOnLocal.push(documentId
|
|
244
|
-
} else if (!
|
|
245
|
-
missingOnRemote.push(documentId
|
|
246
|
-
} else if (!
|
|
247
|
-
|
|
406
|
+
if (!localDocuments[documentId]) {
|
|
407
|
+
missingOnLocal.push(documentId);
|
|
408
|
+
} else if (!remoteDocuments[documentId]) {
|
|
409
|
+
missingOnRemote.push(documentId);
|
|
410
|
+
} else if (!headsOverlap(local.documents[documentId], remote.documents[documentId])) {
|
|
411
|
+
// Subduction's `getAllHeads()` on the edge mixes raw `LooseCommit` tips with
|
|
412
|
+
// fragment heads (commit IDs promoted to depth >= 1 by leading-zero count of the
|
|
413
|
+
// hash). The host's `automerge.getHeads(doc)` only ever sees raw change tips —
|
|
414
|
+
// it has no notion of fragments — so the two views can disagree on a doc's
|
|
415
|
+
// head set even when every change byte is replicated. We treat the doc as in
|
|
416
|
+
// sync as long as both sides agree on at least one head.
|
|
417
|
+
different.push(documentId);
|
|
248
418
|
}
|
|
249
419
|
}
|
|
250
420
|
|
|
@@ -255,6 +425,24 @@ export const diffCollectionState = (local: CollectionState, remote: CollectionSt
|
|
|
255
425
|
};
|
|
256
426
|
};
|
|
257
427
|
|
|
428
|
+
/**
|
|
429
|
+
* Returns true when two head-sets share at least one element. Used by the collection-state
|
|
430
|
+
* diff so transient fragment-vs-commit-ID mismatches between subduction peers don't get
|
|
431
|
+
* reported as `differentDocuments` while the underlying change bytes are fully replicated.
|
|
432
|
+
*/
|
|
433
|
+
const headsOverlap = (a: readonly string[], b: readonly string[]): boolean => {
|
|
434
|
+
if (a.length === 0 || b.length === 0) {
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
const aset = new Set(a);
|
|
438
|
+
for (const h of b) {
|
|
439
|
+
if (aset.has(h)) {
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return false;
|
|
444
|
+
};
|
|
445
|
+
|
|
258
446
|
const validateCollectionState = (state: CollectionState) => {
|
|
259
447
|
Object.entries(state.documents).forEach(([documentId, heads]) => {
|
|
260
448
|
if (!isValidDocumentId(documentId as DocumentId)) {
|
|
@@ -270,6 +458,8 @@ const isValidDocumentId = (documentId: DocumentId) => {
|
|
|
270
458
|
return typeof documentId === 'string' && !documentId.includes(':');
|
|
271
459
|
};
|
|
272
460
|
|
|
273
|
-
const
|
|
461
|
+
const SYNC_SPAN_METHOD = 'syncPeer';
|
|
462
|
+
|
|
463
|
+
const getSpanId = (peerId: PeerId) => {
|
|
274
464
|
return `collection-sync-${peerId}`;
|
|
275
465
|
};
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { type Message } from '@automerge/automerge-repo';
|
|
6
6
|
|
|
7
7
|
import { type TimeAware, trace } from '@dxos/tracing';
|
|
8
|
-
import { CircularBuffer,
|
|
8
|
+
import { CircularBuffer, SlidingWindowSummary, type SlidingWindowSummaryConfig, mapValues } from '@dxos/util';
|
|
9
9
|
|
|
10
10
|
import { type NetworkDataMonitor } from './echo-network-adapter';
|
|
11
11
|
import { type StorageAdapterDataMonitor } from './leveldb-storage-adapter';
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { type PeerId, cbor } from '@automerge/automerge-repo';
|
|
6
|
+
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { Trigger, sleep, waitForCondition } from '@dxos/async';
|
|
9
|
+
import { Context } from '@dxos/context';
|
|
9
10
|
import { invariant } from '@dxos/invariant';
|
|
10
11
|
import { PublicKey } from '@dxos/keys';
|
|
11
12
|
import { type SyncMessage } from '@dxos/protocols/proto/dxos/mesh/teleport/automerge';
|
|
@@ -113,7 +114,7 @@ describe('EchoNetworkAdapter', () => {
|
|
|
113
114
|
onTestFinished(async () => {
|
|
114
115
|
await adapter.close();
|
|
115
116
|
});
|
|
116
|
-
await adapter.addReplicator(replicator);
|
|
117
|
+
await adapter.addReplicator(Context.default(), replicator);
|
|
117
118
|
return adapter;
|
|
118
119
|
};
|
|
119
120
|
|
|
@@ -2,30 +2,38 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
import {
|
|
6
|
+
type DocumentId,
|
|
7
|
+
type Heads,
|
|
8
|
+
type Message,
|
|
9
|
+
NetworkAdapter,
|
|
10
|
+
type PeerId,
|
|
11
|
+
type PeerMetadata,
|
|
12
|
+
} from '@automerge/automerge-repo';
|
|
13
|
+
|
|
14
|
+
import { Event, Trigger, synchronized } from '@dxos/async';
|
|
15
|
+
import { type Context, LifecycleState } from '@dxos/context';
|
|
9
16
|
import { invariant } from '@dxos/invariant';
|
|
10
17
|
import { type PublicKey } from '@dxos/keys';
|
|
11
18
|
import { log } from '@dxos/log';
|
|
12
19
|
import type { AutomergeProtocolMessage } from '@dxos/protocols';
|
|
13
20
|
import { isNonNullable } from '@dxos/util';
|
|
14
21
|
|
|
22
|
+
import { createIdFromSpaceKey } from '../common/space-id';
|
|
15
23
|
import {
|
|
16
|
-
type
|
|
17
|
-
type
|
|
18
|
-
type
|
|
19
|
-
type
|
|
20
|
-
type
|
|
24
|
+
type AutomergeReplicator,
|
|
25
|
+
type AutomergeReplicatorConnection,
|
|
26
|
+
type RemoteDocumentExistenceCheckProps,
|
|
27
|
+
type ShouldAdvertiseProps,
|
|
28
|
+
type ShouldSyncCollectionProps,
|
|
21
29
|
} from './echo-replicator';
|
|
30
|
+
import { PeerNotFoundError } from './errors';
|
|
22
31
|
import {
|
|
23
|
-
isCollectionQueryMessage,
|
|
24
|
-
isCollectionStateMessage,
|
|
25
32
|
type CollectionQueryMessage,
|
|
26
33
|
type CollectionStateMessage,
|
|
34
|
+
isCollectionQueryMessage,
|
|
35
|
+
isCollectionStateMessage,
|
|
27
36
|
} from './network-protocol';
|
|
28
|
-
import { createIdFromSpaceKey } from '../common/space-id';
|
|
29
37
|
|
|
30
38
|
export interface NetworkDataMonitor {
|
|
31
39
|
recordPeerConnected(peerId: string): void;
|
|
@@ -35,9 +43,9 @@ export interface NetworkDataMonitor {
|
|
|
35
43
|
recordMessageSendingFailed(message: Message): void;
|
|
36
44
|
}
|
|
37
45
|
|
|
38
|
-
export type
|
|
46
|
+
export type EchoNetworkAdapterProps = {
|
|
39
47
|
getContainingSpaceForDocument: (documentId: string) => Promise<PublicKey | null>;
|
|
40
|
-
isDocumentInRemoteCollection: (params:
|
|
48
|
+
isDocumentInRemoteCollection: (params: RemoteDocumentExistenceCheckProps) => Promise<boolean>;
|
|
41
49
|
onCollectionStateQueried: (collectionId: string, peerId: PeerId) => void;
|
|
42
50
|
onCollectionStateReceived: (collectionId: string, peerId: PeerId, state: unknown) => void;
|
|
43
51
|
monitor?: NetworkDataMonitor;
|
|
@@ -45,16 +53,17 @@ export type EchoNetworkAdapterParams = {
|
|
|
45
53
|
|
|
46
54
|
type ConnectionEntry = {
|
|
47
55
|
isOpen: boolean;
|
|
48
|
-
connection:
|
|
56
|
+
connection: AutomergeReplicatorConnection;
|
|
49
57
|
reader: ReadableStreamDefaultReader<AutomergeProtocolMessage>;
|
|
50
58
|
writer: WritableStreamDefaultWriter<AutomergeProtocolMessage>;
|
|
59
|
+
requestedDocuments: Set<DocumentId>;
|
|
51
60
|
};
|
|
52
61
|
|
|
53
62
|
/**
|
|
54
|
-
* Manages a set of {@link
|
|
63
|
+
* Manages a set of {@link AutomergeReplicator} instances.
|
|
55
64
|
*/
|
|
56
65
|
export class EchoNetworkAdapter extends NetworkAdapter {
|
|
57
|
-
private readonly _replicators = new Set<
|
|
66
|
+
private readonly _replicators = new Set<AutomergeReplicator>();
|
|
58
67
|
/**
|
|
59
68
|
* Remote peer id -> connection.
|
|
60
69
|
*/
|
|
@@ -63,7 +72,9 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
63
72
|
private readonly _connected = new Trigger();
|
|
64
73
|
private readonly _ready = new Trigger();
|
|
65
74
|
|
|
66
|
-
|
|
75
|
+
public readonly documentRequested = new Event<{ documentId: DocumentId; peerId: PeerId }>();
|
|
76
|
+
|
|
77
|
+
constructor(private readonly _params: EchoNetworkAdapterProps) {
|
|
67
78
|
super();
|
|
68
79
|
}
|
|
69
80
|
|
|
@@ -125,13 +136,13 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
125
136
|
}
|
|
126
137
|
|
|
127
138
|
@synchronized
|
|
128
|
-
async addReplicator(replicator:
|
|
139
|
+
async addReplicator(ctx: Context, replicator: AutomergeReplicator): Promise<void> {
|
|
129
140
|
invariant(this._lifecycleState === LifecycleState.OPEN);
|
|
130
141
|
invariant(this.peerId);
|
|
131
142
|
invariant(!this._replicators.has(replicator));
|
|
132
143
|
|
|
133
144
|
this._replicators.add(replicator);
|
|
134
|
-
await replicator.connect({
|
|
145
|
+
await replicator.connect(ctx, {
|
|
135
146
|
peerId: this.peerId,
|
|
136
147
|
onConnectionOpen: this._onConnectionOpen.bind(this),
|
|
137
148
|
onConnectionClosed: this._onConnectionClosed.bind(this),
|
|
@@ -146,14 +157,14 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
146
157
|
}
|
|
147
158
|
|
|
148
159
|
@synchronized
|
|
149
|
-
async removeReplicator(replicator:
|
|
160
|
+
async removeReplicator(replicator: AutomergeReplicator): Promise<void> {
|
|
150
161
|
invariant(this._lifecycleState === LifecycleState.OPEN);
|
|
151
162
|
invariant(this._replicators.has(replicator));
|
|
152
163
|
await replicator.disconnect();
|
|
153
164
|
this._replicators.delete(replicator);
|
|
154
165
|
}
|
|
155
166
|
|
|
156
|
-
async shouldAdvertise(peerId: PeerId, params:
|
|
167
|
+
async shouldAdvertise(peerId: PeerId, params: ShouldAdvertiseProps): Promise<boolean> {
|
|
157
168
|
const connection = this._connections.get(peerId);
|
|
158
169
|
if (!connection) {
|
|
159
170
|
return false;
|
|
@@ -162,7 +173,7 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
162
173
|
return connection.connection.shouldAdvertise(params);
|
|
163
174
|
}
|
|
164
175
|
|
|
165
|
-
shouldSyncCollection(peerId: PeerId, params:
|
|
176
|
+
shouldSyncCollection(peerId: PeerId, params: ShouldSyncCollectionProps): boolean {
|
|
166
177
|
const connection = this._connections.get(peerId);
|
|
167
178
|
if (!connection) {
|
|
168
179
|
return false;
|
|
@@ -203,10 +214,34 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
203
214
|
.filter(isNonNullable);
|
|
204
215
|
}
|
|
205
216
|
|
|
217
|
+
bundleSyncEnabledForPeer(peerId: PeerId): boolean {
|
|
218
|
+
const connection = this._connections.get(peerId);
|
|
219
|
+
if (!connection) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
return connection.connection.bundleSyncEnabled;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async pushBundle(ctx: Context, peerId: PeerId, bundle: { documentId: DocumentId; data: Uint8Array; heads: Heads }[]) {
|
|
226
|
+
const connection = this._connections.get(peerId);
|
|
227
|
+
if (!connection) {
|
|
228
|
+
throw new PeerNotFoundError({ context: { peerId } });
|
|
229
|
+
}
|
|
230
|
+
return connection.connection.pushBundle!(ctx, bundle);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async pullBundle(ctx: Context, peerId: PeerId, docHeads: Record<DocumentId, Heads>) {
|
|
234
|
+
const connection = this._connections.get(peerId);
|
|
235
|
+
if (!connection) {
|
|
236
|
+
throw new PeerNotFoundError({ context: { peerId } });
|
|
237
|
+
}
|
|
238
|
+
return connection.connection.pullBundle!(ctx, docHeads);
|
|
239
|
+
}
|
|
240
|
+
|
|
206
241
|
private _send(message: Message): void {
|
|
207
242
|
const connectionEntry = this._connections.get(message.targetId);
|
|
208
243
|
if (!connectionEntry) {
|
|
209
|
-
throw new
|
|
244
|
+
throw new PeerNotFoundError({ context: { peerId: message.targetId } });
|
|
210
245
|
}
|
|
211
246
|
|
|
212
247
|
// TODO(dmaretskyi): Find a way to enforce backpressure on AM-repo.
|
|
@@ -225,7 +260,7 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
225
260
|
});
|
|
226
261
|
}
|
|
227
262
|
|
|
228
|
-
private _onConnectionOpen(connection:
|
|
263
|
+
private _onConnectionOpen(connection: AutomergeReplicatorConnection): void {
|
|
229
264
|
log('connection opened', { peerId: connection.peerId });
|
|
230
265
|
invariant(!this._connections.has(connection.peerId as PeerId));
|
|
231
266
|
const connectionEntry: ConnectionEntry = {
|
|
@@ -233,6 +268,7 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
233
268
|
connection,
|
|
234
269
|
reader: connection.readable.getReader(),
|
|
235
270
|
writer: connection.writable.getWriter(),
|
|
271
|
+
requestedDocuments: new Set(),
|
|
236
272
|
};
|
|
237
273
|
|
|
238
274
|
this._connections.set(connection.peerId as PeerId, connectionEntry);
|
|
@@ -247,7 +283,7 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
247
283
|
break;
|
|
248
284
|
}
|
|
249
285
|
|
|
250
|
-
this._onMessage(value as Message);
|
|
286
|
+
this._onMessage(connectionEntry, value as Message);
|
|
251
287
|
}
|
|
252
288
|
} catch (err) {
|
|
253
289
|
if (connectionEntry.isOpen) {
|
|
@@ -261,7 +297,15 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
261
297
|
this._params.monitor?.recordPeerConnected(connection.peerId);
|
|
262
298
|
}
|
|
263
299
|
|
|
264
|
-
private _onMessage(message: Message): void {
|
|
300
|
+
private _onMessage(connectionEntry: ConnectionEntry, message: Message): void {
|
|
301
|
+
const amMessage = message as AutomergeProtocolMessage;
|
|
302
|
+
if (amMessage.type === 'request') {
|
|
303
|
+
this.documentRequested.emit({
|
|
304
|
+
documentId: amMessage.documentId as DocumentId,
|
|
305
|
+
peerId: connectionEntry.connection.peerId as PeerId,
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
265
309
|
if (isCollectionQueryMessage(message)) {
|
|
266
310
|
this._params.onCollectionStateQueried(message.collectionId, message.senderId);
|
|
267
311
|
} else if (isCollectionStateMessage(message)) {
|
|
@@ -272,7 +316,7 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
272
316
|
this._params.monitor?.recordMessageReceived(message);
|
|
273
317
|
}
|
|
274
318
|
|
|
275
|
-
private _onConnectionClosed(connection:
|
|
319
|
+
private _onConnectionClosed(connection: AutomergeReplicatorConnection): void {
|
|
276
320
|
log('connection closed', { peerId: connection.peerId });
|
|
277
321
|
const entry = this._connections.get(connection.peerId as PeerId);
|
|
278
322
|
invariant(entry);
|
|
@@ -291,7 +335,7 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
291
335
|
* Trigger doc-synchronizer shared documents set recalculation. Happens on peer-candidate.
|
|
292
336
|
* TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
|
|
293
337
|
*/
|
|
294
|
-
private _onConnectionAuthScopeChanged(connection:
|
|
338
|
+
private _onConnectionAuthScopeChanged(connection: AutomergeReplicatorConnection): void {
|
|
295
339
|
log('Connection auth scope changed', { peerId: connection.peerId });
|
|
296
340
|
const entry = this._connections.get(connection.peerId as PeerId);
|
|
297
341
|
invariant(entry);
|
|
@@ -299,7 +343,7 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
299
343
|
this._emitPeerCandidate(connection);
|
|
300
344
|
}
|
|
301
345
|
|
|
302
|
-
private _emitPeerCandidate(connection:
|
|
346
|
+
private _emitPeerCandidate(connection: AutomergeReplicatorConnection): void {
|
|
303
347
|
this.emit('peer-candidate', {
|
|
304
348
|
peerId: connection.peerId as PeerId,
|
|
305
349
|
peerMetadata: createEchoPeerMetadata(),
|