@dxos/echo-pipeline 0.8.4-main.84f28bd → 0.8.4-main.ae835ea
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/dist/lib/browser/{chunk-GBFX5J5B.mjs → chunk-FR6GJ4S4.mjs} +239 -162
- package/dist/lib/browser/chunk-FR6GJ4S4.mjs.map +7 -0
- package/dist/lib/browser/chunk-WJJXJTNS.mjs +240 -0
- package/dist/lib/browser/chunk-WJJXJTNS.mjs.map +7 -0
- package/dist/lib/browser/filter/index.mjs +3 -1
- package/dist/lib/browser/index.mjs +1094 -345
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +36 -14
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/{chunk-FQFKWA3X.mjs → chunk-JQCCVDLT.mjs} +239 -162
- package/dist/lib/node-esm/chunk-JQCCVDLT.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-LEQ77KAT.mjs +240 -0
- package/dist/lib/node-esm/chunk-LEQ77KAT.mjs.map +7 -0
- package/dist/lib/node-esm/filter/index.mjs +3 -1
- package/dist/lib/node-esm/index.mjs +1094 -345
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +36 -14
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/automerge/automerge-host.d.ts +15 -28
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/collection-synchronizer.d.ts +3 -2
- package/dist/types/src/automerge/collection-synchronizer.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts +8 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-replicator.d.ts +21 -2
- package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/index.d.ts +1 -1
- package/dist/types/src/automerge/index.d.ts.map +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +1 -1
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +1 -0
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
- 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/db-host/data-service.d.ts +2 -2
- package/dist/types/src/db-host/data-service.d.ts.map +1 -1
- package/dist/types/src/db-host/database-root.d.ts.map +1 -1
- package/dist/types/src/db-host/documents-synchronizer.d.ts +2 -2
- package/dist/types/src/db-host/documents-synchronizer.d.ts.map +1 -1
- package/dist/types/src/db-host/echo-host.d.ts +2 -2
- package/dist/types/src/db-host/echo-host.d.ts.map +1 -1
- package/dist/types/src/db-host/query-service.d.ts +1 -1
- package/dist/types/src/db-host/query-service.d.ts.map +1 -1
- package/dist/types/src/db-host/space-state-manager.d.ts +1 -1
- package/dist/types/src/db-host/space-state-manager.d.ts.map +1 -1
- package/dist/types/src/edge/echo-edge-replicator.d.ts +4 -2
- package/dist/types/src/edge/echo-edge-replicator.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/pipeline.d.ts +1 -1
- package/dist/types/src/pipeline/pipeline.d.ts.map +1 -1
- package/dist/types/src/query/errors.d.ts +24 -8
- package/dist/types/src/query/errors.d.ts.map +1 -1
- package/dist/types/src/query/plan.d.ts +8 -1
- package/dist/types/src/query/plan.d.ts.map +1 -1
- package/dist/types/src/query/query-executor.d.ts +13 -1
- package/dist/types/src/query/query-executor.d.ts.map +1 -1
- package/dist/types/src/query/query-planner.d.ts +2 -0
- 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/control-pipeline.d.ts +1 -1
- package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts +1 -1
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space-protocol.d.ts +1 -1
- package/dist/types/src/space/space-protocol.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts +1 -1
- package/dist/types/src/space/space.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-data.d.ts.map +1 -1
- package/dist/types/src/testing/test-replicator.d.ts +1 -0
- package/dist/types/src/testing/test-replicator.d.ts.map +1 -1
- package/dist/types/src/testing/test-schema.d.ts +1 -1
- package/dist/types/src/testing/test-schema.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 +40 -38
- package/src/automerge/automerge-host.test.ts +18 -8
- package/src/automerge/automerge-host.ts +273 -74
- package/src/automerge/automerge-repo.test.ts +67 -16
- package/src/automerge/collection-synchronizer.test.ts +6 -5
- package/src/automerge/collection-synchronizer.ts +16 -11
- package/src/automerge/echo-data-monitor.ts +1 -1
- package/src/automerge/echo-network-adapter.test.ts +3 -3
- package/src/automerge/echo-network-adapter.ts +38 -6
- package/src/automerge/echo-replicator.ts +23 -2
- package/src/automerge/index.ts +1 -1
- package/src/automerge/leveldb-storage-adapter.ts +7 -7
- package/src/automerge/mesh-echo-replicator-connection.ts +4 -0
- package/src/automerge/mesh-echo-replicator.ts +2 -1
- package/src/automerge/storage-adapter.test.ts +1 -1
- package/src/common/space-id.ts +1 -1
- package/src/db-host/data-service.ts +9 -17
- package/src/db-host/database-root.ts +2 -2
- package/src/db-host/documents-synchronizer.test.ts +1 -1
- package/src/db-host/documents-synchronizer.ts +39 -26
- package/src/db-host/echo-host.ts +14 -15
- package/src/db-host/query-service.ts +10 -2
- package/src/db-host/space-state-manager.ts +2 -2
- package/src/edge/echo-edge-replicator.test.ts +5 -3
- package/src/edge/echo-edge-replicator.ts +105 -27
- package/src/filter/filter-match.test.ts +33 -13
- package/src/filter/filter-match.ts +164 -10
- package/src/metadata/metadata-store.ts +3 -3
- package/src/pipeline/pipeline-stress.test.ts +4 -2
- package/src/pipeline/pipeline.test.ts +3 -2
- package/src/pipeline/pipeline.ts +8 -5
- package/src/query/errors.ts +2 -0
- package/src/query/plan.ts +12 -1
- package/src/query/query-executor.ts +154 -14
- package/src/query/query-planner.test.ts +146 -2
- package/src/query/query-planner.ts +81 -11
- package/src/space/admission-discovery-extension.ts +2 -2
- package/src/space/control-pipeline.test.ts +4 -3
- package/src/space/control-pipeline.ts +9 -6
- package/src/space/space-manager.browser.test.ts +1 -1
- package/src/space/space-manager.ts +5 -4
- package/src/space/space-protocol.browser.test.ts +2 -2
- package/src/space/space-protocol.test.ts +3 -2
- package/src/space/space-protocol.ts +6 -3
- package/src/space/space.test.ts +1 -1
- package/src/space/space.ts +3 -2
- package/src/testing/test-agent-builder.ts +4 -3
- package/src/testing/test-data.ts +1 -1
- package/src/testing/test-replicator.ts +4 -0
- package/src/testing/test-schema.ts +1 -1
- package/src/util.ts +1 -1
- package/dist/lib/browser/chunk-ANZAS5CC.mjs +0 -126
- package/dist/lib/browser/chunk-ANZAS5CC.mjs.map +0 -7
- package/dist/lib/browser/chunk-GBFX5J5B.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.map +0 -7
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
+
next as A,
|
|
6
7
|
type Heads,
|
|
7
8
|
change,
|
|
8
9
|
clone,
|
|
@@ -10,7 +11,6 @@ import {
|
|
|
10
11
|
from,
|
|
11
12
|
getBackend,
|
|
12
13
|
getHeads,
|
|
13
|
-
next as A,
|
|
14
14
|
save,
|
|
15
15
|
saveSince,
|
|
16
16
|
} from '@automerge/automerge';
|
|
@@ -19,14 +19,14 @@ import {
|
|
|
19
19
|
type DocHandle,
|
|
20
20
|
type DocumentId,
|
|
21
21
|
type HandleState,
|
|
22
|
-
type StorageAdapterInterface,
|
|
23
22
|
type PeerId,
|
|
24
23
|
Repo,
|
|
25
24
|
type SharePolicy,
|
|
25
|
+
type StorageAdapterInterface,
|
|
26
26
|
generateAutomergeUrl,
|
|
27
27
|
parseAutomergeUrl,
|
|
28
28
|
} from '@automerge/automerge-repo';
|
|
29
|
-
import {
|
|
29
|
+
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
30
30
|
|
|
31
31
|
import { asyncTimeout, sleep } from '@dxos/async';
|
|
32
32
|
import { randomBytes } from '@dxos/crypto';
|
|
@@ -36,11 +36,12 @@ import { TestBuilder as TeleportBuilder, TestPeer as TeleportPeer } from '@dxos/
|
|
|
36
36
|
import { openAndClose } from '@dxos/test-utils';
|
|
37
37
|
import { isNonNullable, range } from '@dxos/util';
|
|
38
38
|
|
|
39
|
+
import { TestAdapter, type TestConnectionStateProvider } from '../testing';
|
|
40
|
+
|
|
39
41
|
import { FIND_PARAMS } from './automerge-host';
|
|
40
42
|
import { EchoNetworkAdapter } from './echo-network-adapter';
|
|
41
43
|
import { LevelDBStorageAdapter } from './leveldb-storage-adapter';
|
|
42
44
|
import { MeshEchoReplicator } from './mesh-echo-replicator';
|
|
43
|
-
import { TestAdapter, type TestConnectionStateProvider } from '../testing';
|
|
44
45
|
|
|
45
46
|
const HOST_AND_CLIENT: [string, string] = ['host', 'client'];
|
|
46
47
|
|
|
@@ -420,13 +421,7 @@ describe('AutomergeRepo', () => {
|
|
|
420
421
|
|
|
421
422
|
test('client creates doc and syncs with a Repo', async () => {
|
|
422
423
|
const repo = new Repo({ network: [] });
|
|
423
|
-
const receiveByServer =
|
|
424
|
-
const serverHandle = await repo.find(docId, FIND_PARAMS);
|
|
425
|
-
serverHandle.update((doc) => {
|
|
426
|
-
return A.loadIncremental(doc, blob);
|
|
427
|
-
});
|
|
428
|
-
};
|
|
429
|
-
|
|
424
|
+
const receiveByServer = (blob: Uint8Array, docId: DocumentId) => repo.import<any>(blob, { docId });
|
|
430
425
|
let clientDoc = A.from<{ field?: string }>({});
|
|
431
426
|
const { documentId } = parseAutomergeUrl(generateAutomergeUrl());
|
|
432
427
|
// Sync handshake.
|
|
@@ -434,7 +429,7 @@ describe('AutomergeRepo', () => {
|
|
|
434
429
|
|
|
435
430
|
// Sync protocol.
|
|
436
431
|
const sendDoc = async (doc: A.Doc<any>) => {
|
|
437
|
-
|
|
432
|
+
receiveByServer(saveSince(doc, sentHeads), documentId);
|
|
438
433
|
sentHeads = getHeads(doc);
|
|
439
434
|
};
|
|
440
435
|
|
|
@@ -456,10 +451,9 @@ describe('AutomergeRepo', () => {
|
|
|
456
451
|
|
|
457
452
|
const repo = new Repo({ network: [], storage });
|
|
458
453
|
const receiveByServer = async (blob: Uint8Array, docId: DocumentId) => {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
});
|
|
454
|
+
repo.import<any>(blob, { docId });
|
|
455
|
+
// TODO(mykola): This should not be required. Document is not persisted without it.
|
|
456
|
+
await repo.flush([docId]);
|
|
463
457
|
};
|
|
464
458
|
|
|
465
459
|
let clientDoc = A.from<{ field?: string }>({ field: 'foo' });
|
|
@@ -677,6 +671,63 @@ describe('AutomergeRepo', () => {
|
|
|
677
671
|
await doc.whenReady();
|
|
678
672
|
expect(doc.doc()).to.deep.eq(document.doc());
|
|
679
673
|
});
|
|
674
|
+
|
|
675
|
+
test('document is passively replicated to connected peers', async () => {
|
|
676
|
+
const [spaceKey] = PublicKey.randomSequence();
|
|
677
|
+
|
|
678
|
+
const teleportBuilder = new TeleportBuilder();
|
|
679
|
+
onTestFinished(() => teleportBuilder.destroy());
|
|
680
|
+
|
|
681
|
+
const peer1 = await createTeleportTestPeer(teleportBuilder, spaceKey);
|
|
682
|
+
const peer2 = await createTeleportTestPeer(teleportBuilder, spaceKey);
|
|
683
|
+
|
|
684
|
+
const handle = peer1.repo.create();
|
|
685
|
+
handle.change((doc: any) => (doc.text = 'hello'));
|
|
686
|
+
await connectPeers(spaceKey, teleportBuilder, peer1, peer2);
|
|
687
|
+
|
|
688
|
+
await expect
|
|
689
|
+
.poll(async () => {
|
|
690
|
+
const doc = peer2.repo.handles[handle.documentId];
|
|
691
|
+
await doc.whenReady();
|
|
692
|
+
return doc.doc()!.text;
|
|
693
|
+
})
|
|
694
|
+
.toEqual('hello');
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
test('imported document is passively replicated to connected peers', async () => {
|
|
698
|
+
let blob: Uint8Array;
|
|
699
|
+
let documentId: DocumentId;
|
|
700
|
+
{
|
|
701
|
+
const repo = new Repo();
|
|
702
|
+
const handle = repo.create();
|
|
703
|
+
handle.change((doc: any) => (doc.text = 'hello'));
|
|
704
|
+
blob = A.save(handle.doc()!);
|
|
705
|
+
documentId = handle.documentId;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
const [spaceKey] = PublicKey.randomSequence();
|
|
709
|
+
|
|
710
|
+
const teleportBuilder = new TeleportBuilder();
|
|
711
|
+
onTestFinished(() => teleportBuilder.destroy());
|
|
712
|
+
|
|
713
|
+
const peer1 = await createTeleportTestPeer(teleportBuilder, spaceKey);
|
|
714
|
+
const peer2 = await createTeleportTestPeer(teleportBuilder, spaceKey);
|
|
715
|
+
await connectPeers(spaceKey, teleportBuilder, peer1, peer2);
|
|
716
|
+
|
|
717
|
+
const handle = peer1.repo.import(blob, { docId: documentId });
|
|
718
|
+
await handle.whenReady();
|
|
719
|
+
|
|
720
|
+
await expect
|
|
721
|
+
.poll(
|
|
722
|
+
async () => {
|
|
723
|
+
const doc = peer2.repo.handles[handle.documentId];
|
|
724
|
+
await doc.whenReady();
|
|
725
|
+
return doc.doc()!.text;
|
|
726
|
+
},
|
|
727
|
+
{ timeout: 1_000 },
|
|
728
|
+
)
|
|
729
|
+
.toEqual('hello');
|
|
730
|
+
});
|
|
680
731
|
});
|
|
681
732
|
|
|
682
733
|
const createLevelAdapter = async () => {
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type
|
|
6
|
-
import {
|
|
5
|
+
import { type Heads } from '@automerge/automerge';
|
|
6
|
+
import type { DocumentId, PeerId } from '@automerge/automerge-repo';
|
|
7
|
+
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { sleep } from '@dxos/async';
|
|
9
10
|
|
|
10
|
-
import { CollectionSynchronizer, diffCollectionState
|
|
11
|
+
import { type CollectionState, CollectionSynchronizer, diffCollectionState } from './collection-synchronizer';
|
|
11
12
|
|
|
12
13
|
describe('CollectionSynchronizer', () => {
|
|
13
14
|
test('sync two peers', async () => {
|
|
@@ -84,7 +85,7 @@ const STATE_1: CollectionState = {
|
|
|
84
85
|
a: ['1'],
|
|
85
86
|
b: ['2'],
|
|
86
87
|
c: ['3'],
|
|
87
|
-
},
|
|
88
|
+
} as Record<DocumentId, Heads>,
|
|
88
89
|
};
|
|
89
90
|
|
|
90
91
|
const STATE_2: CollectionState = {
|
|
@@ -92,5 +93,5 @@ const STATE_2: CollectionState = {
|
|
|
92
93
|
a: ['1'],
|
|
93
94
|
b: ['4'],
|
|
94
95
|
d: ['3'],
|
|
95
|
-
},
|
|
96
|
+
} as Record<DocumentId, Heads>,
|
|
96
97
|
};
|
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { next as
|
|
5
|
+
import { next as A, 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';
|
|
10
12
|
import { log } from '@dxos/log';
|
|
11
13
|
import { trace } from '@dxos/tracing';
|
|
12
14
|
import { defaultMap } from '@dxos/util';
|
|
@@ -223,7 +225,7 @@ export type CollectionState = {
|
|
|
223
225
|
/**
|
|
224
226
|
* DocumentId -> Heads.
|
|
225
227
|
*/
|
|
226
|
-
documents: Record<
|
|
228
|
+
documents: Record<DocumentId, Heads>;
|
|
227
229
|
};
|
|
228
230
|
|
|
229
231
|
export type CollectionStateDiff = {
|
|
@@ -233,18 +235,21 @@ export type CollectionStateDiff = {
|
|
|
233
235
|
};
|
|
234
236
|
|
|
235
237
|
export const diffCollectionState = (local: CollectionState, remote: CollectionState): CollectionStateDiff => {
|
|
236
|
-
const
|
|
238
|
+
const localDocuments = Record.filter(local.documents, (heads) => heads.length > 0);
|
|
239
|
+
const remoteDocuments = Record.filter(remote.documents, (heads) => heads.length > 0);
|
|
240
|
+
// NOTE: Using `Array.union` is slow.
|
|
241
|
+
const allDocuments = [...new Set([...Record.keys(localDocuments), ...Record.keys(remoteDocuments)])] as DocumentId[];
|
|
237
242
|
|
|
238
243
|
const missingOnRemote: DocumentId[] = [];
|
|
239
244
|
const missingOnLocal: DocumentId[] = [];
|
|
240
245
|
const different: DocumentId[] = [];
|
|
241
246
|
for (const documentId of allDocuments) {
|
|
242
|
-
if (!
|
|
243
|
-
missingOnLocal.push(documentId
|
|
244
|
-
} else if (!
|
|
245
|
-
missingOnRemote.push(documentId
|
|
246
|
-
} else if (!
|
|
247
|
-
different.push(documentId
|
|
247
|
+
if (!localDocuments[documentId]) {
|
|
248
|
+
missingOnLocal.push(documentId);
|
|
249
|
+
} else if (!remoteDocuments[documentId]) {
|
|
250
|
+
missingOnRemote.push(documentId);
|
|
251
|
+
} else if (!A.equals(local.documents[documentId], remote.documents[documentId])) {
|
|
252
|
+
different.push(documentId);
|
|
248
253
|
}
|
|
249
254
|
}
|
|
250
255
|
|
|
@@ -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,10 @@
|
|
|
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
9
|
import { invariant } from '@dxos/invariant';
|
|
10
10
|
import { PublicKey } from '@dxos/keys';
|
|
11
11
|
import { type SyncMessage } from '@dxos/protocols/proto/dxos/mesh/teleport/automerge';
|
|
@@ -2,9 +2,16 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
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 { Trigger, synchronized } from '@dxos/async';
|
|
8
15
|
import { LifecycleState } from '@dxos/context';
|
|
9
16
|
import { invariant } from '@dxos/invariant';
|
|
10
17
|
import { type PublicKey } from '@dxos/keys';
|
|
@@ -12,6 +19,8 @@ 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';
|
|
23
|
+
|
|
15
24
|
import {
|
|
16
25
|
type EchoReplicator,
|
|
17
26
|
type RemoteDocumentExistenceCheckParams,
|
|
@@ -20,12 +29,11 @@ import {
|
|
|
20
29
|
type ShouldSyncCollectionParams,
|
|
21
30
|
} from './echo-replicator';
|
|
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;
|
|
@@ -203,6 +211,30 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
203
211
|
.filter(isNonNullable);
|
|
204
212
|
}
|
|
205
213
|
|
|
214
|
+
bundleSyncEnabledForPeer(peerId: PeerId): boolean {
|
|
215
|
+
const connection = this._connections.get(peerId);
|
|
216
|
+
if (!connection) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
return connection.connection.bundleSyncEnabled;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async pushBundle(peerId: PeerId, bundle: { documentId: DocumentId; data: Uint8Array; heads: Heads }[]) {
|
|
223
|
+
const connection = this._connections.get(peerId);
|
|
224
|
+
if (!connection) {
|
|
225
|
+
throw new Error('Connection not found.');
|
|
226
|
+
}
|
|
227
|
+
return connection.connection.pushBundle!(bundle);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async pullBundle(peerId: PeerId, docHeads: Record<DocumentId, Heads>) {
|
|
231
|
+
const connection = this._connections.get(peerId);
|
|
232
|
+
if (!connection) {
|
|
233
|
+
throw new Error('Connection not found.');
|
|
234
|
+
}
|
|
235
|
+
return connection.connection.pullBundle!(docHeads);
|
|
236
|
+
}
|
|
237
|
+
|
|
206
238
|
private _send(message: Message): void {
|
|
207
239
|
const connectionEntry = this._connections.get(message.targetId);
|
|
208
240
|
if (!connectionEntry) {
|
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { type Heads } from '@automerge/automerge';
|
|
6
|
+
import { type DocumentId } from '@automerge/automerge-repo';
|
|
7
|
+
|
|
5
8
|
import { type PublicKey, type SpaceId } from '@dxos/keys';
|
|
6
|
-
import type
|
|
9
|
+
import { type AutomergeProtocolMessage } from '@dxos/protocols';
|
|
7
10
|
|
|
11
|
+
// TODO(burdon): Rename AutomergeReplicator?
|
|
8
12
|
export interface EchoReplicator {
|
|
9
13
|
/**
|
|
10
14
|
* Called on when replicator is added to EchoHost.
|
|
@@ -37,7 +41,7 @@ export interface EchoReplicatorContext {
|
|
|
37
41
|
|
|
38
42
|
export interface ReplicatorConnection {
|
|
39
43
|
/**
|
|
40
|
-
*
|
|
44
|
+
* Remote peer id.
|
|
41
45
|
*/
|
|
42
46
|
get peerId(): string;
|
|
43
47
|
|
|
@@ -61,6 +65,23 @@ export interface ReplicatorConnection {
|
|
|
61
65
|
* @returns true if the collection should be synced to this peer.
|
|
62
66
|
*/
|
|
63
67
|
shouldSyncCollection(params: ShouldSyncCollectionParams): boolean;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Batch syncing considered enabled if ReplicatorConnection implements `pushBatch` and `pullBatch` methods.
|
|
71
|
+
* @returns true if the batch syncing is enabled.
|
|
72
|
+
*/
|
|
73
|
+
get bundleSyncEnabled(): boolean;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Pushes the batch of documents to the remote peer.
|
|
77
|
+
*/
|
|
78
|
+
pushBundle?(bundle: { documentId: DocumentId; data: Uint8Array; heads: Heads }[]): Promise<void>;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Pulls the batch of documents from the remote peer.
|
|
82
|
+
*/
|
|
83
|
+
// TODO(mykola): Use automerge-repo-bundles Bundle type here.
|
|
84
|
+
pullBundle?(docHeads: Record<DocumentId, Heads>): Promise<Record<DocumentId, Uint8Array>>;
|
|
64
85
|
}
|
|
65
86
|
|
|
66
87
|
export type ShouldAdvertiseParams = {
|
package/src/automerge/index.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
export * from './automerge-host';
|
|
6
6
|
export * from './leveldb-storage-adapter';
|
|
7
7
|
export * from './mesh-echo-replicator';
|
|
8
|
-
export * from './echo-replicator';
|
|
8
|
+
export type * from './echo-replicator';
|
|
9
9
|
export { diffCollectionState } from './collection-synchronizer';
|
|
10
10
|
export * from './space-collection';
|
|
11
11
|
export * from './echo-data-monitor';
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
// s
|
|
4
4
|
|
|
5
|
-
import { type
|
|
5
|
+
import { type Chunk, type StorageAdapterInterface, type StorageKey } from '@automerge/automerge-repo';
|
|
6
6
|
import { type MixedEncoding } from 'level-transcoder';
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { Resource } from '@dxos/context';
|
|
9
9
|
import { type BatchLevel, type SublevelDB } from '@dxos/kv-store';
|
|
10
10
|
import { type MaybePromise } from '@dxos/util';
|
|
11
11
|
|
|
@@ -36,7 +36,7 @@ export class LevelDBStorageAdapter extends Resource implements StorageAdapterInt
|
|
|
36
36
|
|
|
37
37
|
async load(keyArray: StorageKey): Promise<Uint8Array | undefined> {
|
|
38
38
|
try {
|
|
39
|
-
if (this.
|
|
39
|
+
if (!this.isOpen) {
|
|
40
40
|
// TODO(mykola): this should be an error.
|
|
41
41
|
return undefined;
|
|
42
42
|
}
|
|
@@ -54,7 +54,7 @@ export class LevelDBStorageAdapter extends Resource implements StorageAdapterInt
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
async save(keyArray: StorageKey, binary: Uint8Array): Promise<void> {
|
|
57
|
-
if (this.
|
|
57
|
+
if (!this.isOpen) {
|
|
58
58
|
return undefined;
|
|
59
59
|
}
|
|
60
60
|
const startMs = Date.now();
|
|
@@ -72,14 +72,14 @@ export class LevelDBStorageAdapter extends Resource implements StorageAdapterInt
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
async remove(keyArray: StorageKey): Promise<void> {
|
|
75
|
-
if (this.
|
|
75
|
+
if (!this.isOpen) {
|
|
76
76
|
return undefined;
|
|
77
77
|
}
|
|
78
78
|
await this._params.db.del<StorageKey>(keyArray, { ...encodingOptions });
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
async loadRange(keyPrefix: StorageKey): Promise<Chunk[]> {
|
|
82
|
-
if (this.
|
|
82
|
+
if (!this.isOpen) {
|
|
83
83
|
return [];
|
|
84
84
|
}
|
|
85
85
|
const startMs = Date.now();
|
|
@@ -100,7 +100,7 @@ export class LevelDBStorageAdapter extends Resource implements StorageAdapterInt
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
async removeRange(keyPrefix: StorageKey): Promise<void> {
|
|
103
|
-
if (this.
|
|
103
|
+
if (!this.isOpen) {
|
|
104
104
|
return undefined;
|
|
105
105
|
}
|
|
106
106
|
const batch = this._params.db.batch();
|
|
@@ -111,6 +111,10 @@ export class MeshReplicatorConnection extends Resource implements ReplicatorConn
|
|
|
111
111
|
return this._isEnabled;
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
get bundleSyncEnabled(): boolean {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
114
118
|
async shouldAdvertise(params: ShouldAdvertiseParams): Promise<boolean> {
|
|
115
119
|
return this._params.shouldAdvertise(params);
|
|
116
120
|
}
|
|
@@ -12,10 +12,11 @@ import {
|
|
|
12
12
|
} from '@dxos/teleport-extension-automerge-replicator';
|
|
13
13
|
import { ComplexSet, defaultMap } from '@dxos/util';
|
|
14
14
|
|
|
15
|
+
import { createIdFromSpaceKey } from '../common/space-id';
|
|
16
|
+
|
|
15
17
|
import { type EchoReplicator, type EchoReplicatorContext, type ShouldAdvertiseParams } from './echo-replicator';
|
|
16
18
|
import { MeshReplicatorConnection } from './mesh-echo-replicator-connection';
|
|
17
19
|
import { getSpaceIdFromCollectionId } from './space-collection';
|
|
18
|
-
import { createIdFromSpaceKey } from '../common/space-id';
|
|
19
20
|
|
|
20
21
|
// TODO(dmaretskyi): Move out of @dxos/echo-pipeline.
|
|
21
22
|
|
package/src/common/space-id.ts
CHANGED
|
@@ -18,7 +18,7 @@ export const createIdFromSpaceKey = async (spaceKey: PublicKey): Promise<SpaceId
|
|
|
18
18
|
return cachedValue;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
const digest = await subtleCrypto.digest('SHA-256', spaceKey.asUint8Array());
|
|
21
|
+
const digest = await subtleCrypto.digest('SHA-256', spaceKey.asUint8Array() as Uint8Array<ArrayBuffer>);
|
|
22
22
|
|
|
23
23
|
const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);
|
|
24
24
|
const spaceId = SpaceId.encode(bytes);
|
|
@@ -11,23 +11,24 @@ import { invariant } from '@dxos/invariant';
|
|
|
11
11
|
import { SpaceId } from '@dxos/keys';
|
|
12
12
|
import { log } from '@dxos/log';
|
|
13
13
|
import {
|
|
14
|
+
type BatchedDocumentUpdates,
|
|
14
15
|
type DataService,
|
|
15
16
|
type FlushRequest,
|
|
16
|
-
type SubscribeRequest,
|
|
17
|
-
type BatchedDocumentUpdates,
|
|
18
|
-
type UpdateSubscriptionRequest,
|
|
19
17
|
type GetDocumentHeadsRequest,
|
|
20
18
|
type GetDocumentHeadsResponse,
|
|
21
|
-
type ReIndexHeadsRequest,
|
|
22
|
-
type WaitUntilHeadsReplicatedRequest,
|
|
23
|
-
type UpdateRequest,
|
|
24
19
|
type GetSpaceSyncStateRequest,
|
|
20
|
+
type ReIndexHeadsRequest,
|
|
25
21
|
type SpaceSyncState,
|
|
22
|
+
type SubscribeRequest,
|
|
23
|
+
type UpdateRequest,
|
|
24
|
+
type UpdateSubscriptionRequest,
|
|
25
|
+
type WaitUntilHeadsReplicatedRequest,
|
|
26
26
|
} from '@dxos/protocols/proto/dxos/echo/service';
|
|
27
27
|
|
|
28
|
+
import { type AutomergeHost, deriveCollectionIdFromSpaceId } from '../automerge';
|
|
29
|
+
|
|
28
30
|
import { DocumentsSynchronizer } from './documents-synchronizer';
|
|
29
31
|
import { type SpaceStateManager } from './space-state-manager';
|
|
30
|
-
import { deriveCollectionIdFromSpaceId, type AutomergeHost } from '../automerge';
|
|
31
32
|
|
|
32
33
|
export type DataServiceParams = {
|
|
33
34
|
automergeHost: AutomergeHost;
|
|
@@ -145,16 +146,7 @@ export class DataServiceImpl implements DataService {
|
|
|
145
146
|
const scheduler = new UpdateScheduler(ctx, async () => {
|
|
146
147
|
const state = collectionId ? await this._automergeHost.getCollectionSyncState(collectionId) : { peers: [] };
|
|
147
148
|
|
|
148
|
-
next({
|
|
149
|
-
peers: state.peers.map((peer) => ({
|
|
150
|
-
peerId: peer.peerId,
|
|
151
|
-
missingOnRemote: peer.missingOnRemote,
|
|
152
|
-
missingOnLocal: peer.missingOnLocal,
|
|
153
|
-
differentDocuments: peer.differentDocuments,
|
|
154
|
-
localDocumentCount: peer.localDocumentCount,
|
|
155
|
-
remoteDocumentCount: peer.remoteDocumentCount,
|
|
156
|
-
})),
|
|
157
|
-
});
|
|
149
|
+
next({ peers: state.peers });
|
|
158
150
|
});
|
|
159
151
|
|
|
160
152
|
this._automergeHost.collectionStateUpdated.on(ctx, (e) => {
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import type * as A from '@automerge/automerge';
|
|
6
|
-
import {
|
|
6
|
+
import { type AutomergeUrl, type DocHandle, type DocumentId, interpretAsDocumentId } from '@automerge/automerge-repo';
|
|
7
7
|
|
|
8
8
|
import { DatabaseDirectory, SpaceDocVersion } from '@dxos/echo-protocol';
|
|
9
9
|
import { invariant } from '@dxos/invariant';
|
|
10
10
|
|
|
11
|
-
import {
|
|
11
|
+
import { type DocMetrics, measureDocMetrics } from './automerge-metrics';
|
|
12
12
|
|
|
13
13
|
export class DatabaseRoot {
|
|
14
14
|
static mapLinks(doc: DocHandle<DatabaseDirectory>, mapping: Record<DocumentId, DocumentId>): void {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { next as A } from '@automerge/automerge';
|
|
6
|
-
import { generateAutomergeUrl, parseAutomergeUrl
|
|
6
|
+
import { Repo, generateAutomergeUrl, parseAutomergeUrl } from '@automerge/automerge-repo';
|
|
7
7
|
import { describe, expect, test } from 'vitest';
|
|
8
8
|
|
|
9
9
|
import { sleep } from '@dxos/async';
|