@dxos/echo-pipeline 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef
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-XGG76KKU.mjs → neutral/chunk-VIHYDCCC.mjs} +404 -963
- package/dist/lib/neutral/chunk-VIHYDCCC.mjs.map +7 -0
- package/dist/lib/{browser/chunk-BVR7OOZK.mjs → neutral/chunk-YBC6ONBZ.mjs} +23 -27
- package/dist/lib/neutral/chunk-YBC6ONBZ.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/filter/index.mjs +1 -2
- package/dist/lib/{browser → neutral}/index.mjs +3143 -2206
- 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 +103 -395
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/automerge/automerge-host.d.ts +43 -23
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- 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/collection-synchronizer.d.ts +5 -3
- 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 +17 -11
- package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-replicator.d.ts +20 -16
- package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
- 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/leveldb-storage-adapter.d.ts +4 -4
- package/dist/types/src/automerge/leveldb-storage-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +8 -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/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 +7 -6
- 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 +8 -7
- package/dist/types/src/db-host/documents-synchronizer.d.ts.map +1 -1
- package/dist/types/src/db-host/echo-host.d.ts +58 -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 +18 -10
- 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 +1 -0
- 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 +7 -6
- package/dist/types/src/edge/echo-edge-replicator.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 +2 -2
- package/dist/types/src/filter/filter-match.d.ts.map +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 +2 -2
- 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 +16 -20
- package/dist/types/src/query/errors.d.ts.map +1 -1
- package/dist/types/src/query/plan.d.ts +59 -8
- package/dist/types/src/query/plan.d.ts.map +1 -1
- package/dist/types/src/query/query-executor.d.ts +62 -10
- package/dist/types/src/query/query-executor.d.ts.map +1 -1
- package/dist/types/src/query/query-planner.d.ts +10 -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 +4 -3
- package/dist/types/src/space/control-pipeline.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts +7 -7
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space-protocol.d.ts +9 -8
- package/dist/types/src/space/space-protocol.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts +5 -5
- 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.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 +13 -13
- package/dist/types/src/testing/test-replicator.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +51 -54
- package/src/automerge/automerge-host.test.ts +42 -23
- package/src/automerge/automerge-host.ts +163 -88
- package/src/automerge/automerge-repo.test.ts +149 -32
- package/src/automerge/collection-synchronizer.test.ts +19 -13
- package/src/automerge/collection-synchronizer.ts +69 -28
- package/src/automerge/echo-network-adapter.test.ts +2 -1
- package/src/automerge/echo-network-adapter.ts +40 -29
- package/src/automerge/echo-replicator.ts +21 -16
- package/src/automerge/heads-store.ts +42 -3
- package/src/automerge/leveldb-storage-adapter.ts +4 -4
- package/src/automerge/mesh-echo-replicator-connection.ts +8 -8
- package/src/automerge/mesh-echo-replicator.ts +14 -11
- package/src/db-host/automerge-data-source.test.ts +299 -0
- package/src/db-host/automerge-data-source.ts +186 -0
- package/src/db-host/data-service.ts +17 -10
- package/src/db-host/documents-synchronizer.test.ts +143 -14
- package/src/db-host/documents-synchronizer.ts +54 -51
- package/src/db-host/echo-host.ts +321 -86
- 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 +110 -112
- package/src/db-host/queue-data-source.ts +159 -0
- package/src/db-host/queue-service.test.ts +145 -0
- package/src/db-host/space-state-manager.ts +5 -1
- package/src/db-host/stub.ts +33 -0
- package/src/edge/echo-edge-replicator.test.ts +9 -9
- package/src/edge/echo-edge-replicator.ts +76 -47
- package/src/filter/filter-match.test.ts +41 -25
- package/src/filter/filter-match.ts +33 -36
- package/src/metadata/metadata-store.ts +8 -5
- package/src/pipeline/pipeline-stress.test.ts +1 -3
- package/src/pipeline/pipeline.test.ts +0 -1
- package/src/pipeline/pipeline.ts +2 -3
- package/src/query/errors.ts +2 -2
- package/src/query/plan.ts +71 -8
- package/src/query/query-executor.ts +1170 -197
- package/src/query/query-planner.test.ts +854 -86
- package/src/query/query-planner.ts +408 -38
- package/src/space/admission-discovery-extension.ts +1 -1
- package/src/space/auth.ts +6 -6
- package/src/space/control-pipeline.test.ts +2 -2
- package/src/space/control-pipeline.ts +7 -7
- package/src/space/space-manager.ts +12 -17
- package/src/space/space-protocol.browser.test.ts +13 -12
- package/src/space/space-protocol.test.ts +13 -13
- package/src/space/space-protocol.ts +16 -15
- package/src/space/space.test.ts +2 -2
- package/src/space/space.ts +9 -10
- package/src/testing/index.ts +0 -2
- package/src/testing/test-agent-builder.ts +0 -1
- package/src/testing/test-network-adapter.ts +16 -3
- package/src/testing/test-replicator.ts +19 -18
- package/src/util.ts +7 -3
- package/dist/lib/browser/chunk-BVR7OOZK.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-XGG76KKU.mjs.map +0 -7
- 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-CHMJJ4DG.mjs +0 -2230
- package/dist/lib/node-esm/chunk-CHMJJ4DG.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/chunk-L3ZEKVDR.mjs +0 -251
- package/dist/lib/node-esm/chunk-L3ZEKVDR.mjs.map +0 -7
- package/dist/lib/node-esm/filter/index.mjs +0 -13
- package/dist/lib/node-esm/filter/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -5560
- 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 -805
- 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
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
type DocHandle,
|
|
20
20
|
type DocumentId,
|
|
21
21
|
type HandleState,
|
|
22
|
+
type Message,
|
|
22
23
|
type PeerId,
|
|
23
24
|
Repo,
|
|
24
25
|
type SharePolicy,
|
|
@@ -29,15 +30,16 @@ import {
|
|
|
29
30
|
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
30
31
|
|
|
31
32
|
import { asyncTimeout, sleep } from '@dxos/async';
|
|
33
|
+
import { Context } from '@dxos/context';
|
|
32
34
|
import { randomBytes } from '@dxos/crypto';
|
|
33
35
|
import { PublicKey } from '@dxos/keys';
|
|
34
36
|
import { createTestLevel } from '@dxos/kv-store/testing';
|
|
37
|
+
import { log } from '@dxos/log';
|
|
35
38
|
import { TestBuilder as TeleportBuilder, TestPeer as TeleportPeer } from '@dxos/teleport/testing';
|
|
36
39
|
import { openAndClose } from '@dxos/test-utils';
|
|
37
40
|
import { isNonNullable, range } from '@dxos/util';
|
|
38
41
|
|
|
39
42
|
import { TestAdapter, type TestConnectionStateProvider } from '../testing';
|
|
40
|
-
|
|
41
43
|
import { FIND_PARAMS } from './automerge-host';
|
|
42
44
|
import { EchoNetworkAdapter } from './echo-network-adapter';
|
|
43
45
|
import { LevelDBStorageAdapter } from './leveldb-storage-adapter';
|
|
@@ -162,32 +164,6 @@ describe('AutomergeRepo', () => {
|
|
|
162
164
|
expect((await asyncTimeout(client.find<any>(handle.url), 1000))!.doc().text).to.equal(text);
|
|
163
165
|
});
|
|
164
166
|
|
|
165
|
-
test('share policy gets enabled afterwards', async () => {
|
|
166
|
-
let sharePolicy = false;
|
|
167
|
-
|
|
168
|
-
const { repos, adapters } = await createHostClientRepoTopology({
|
|
169
|
-
sharePolicy: async () => sharePolicy,
|
|
170
|
-
});
|
|
171
|
-
const [host, client] = repos;
|
|
172
|
-
await connectAdapters(adapters);
|
|
173
|
-
|
|
174
|
-
const handle = host.create();
|
|
175
|
-
const text = 'Hello world';
|
|
176
|
-
handle.change((doc: any) => {
|
|
177
|
-
doc.text = text;
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
{
|
|
181
|
-
await client.find(handle.url, { allowableStates: ['unavailable'] });
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
sharePolicy = true;
|
|
185
|
-
|
|
186
|
-
{
|
|
187
|
-
await client.find(handle.url, { allowableStates: ['unavailable'] });
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
|
|
191
167
|
test('two documents and share policy switching', async () => {
|
|
192
168
|
const allowedDocs: DocumentId[] = [];
|
|
193
169
|
|
|
@@ -510,6 +486,139 @@ describe('AutomergeRepo', () => {
|
|
|
510
486
|
await asyncTimeout(handleB.whenReady(), 1000);
|
|
511
487
|
expect(handleB.doc()!.text).to.equal(text);
|
|
512
488
|
});
|
|
489
|
+
|
|
490
|
+
test('retry share config', async () => {
|
|
491
|
+
let announce = false;
|
|
492
|
+
|
|
493
|
+
const { repos, adapters } = await createRepoTopology({
|
|
494
|
+
peers: ['A', 'B'],
|
|
495
|
+
connections: [['A', 'B']],
|
|
496
|
+
options: {
|
|
497
|
+
shareConfig: {
|
|
498
|
+
access: async () => true,
|
|
499
|
+
announce: async () => announce,
|
|
500
|
+
},
|
|
501
|
+
},
|
|
502
|
+
onMessage: (message) => {
|
|
503
|
+
console.log(`${message.senderId} -> ${message.targetId}: ${message.type} ${message.documentId ?? ''}`);
|
|
504
|
+
},
|
|
505
|
+
});
|
|
506
|
+
const [repoA, repoB] = repos;
|
|
507
|
+
await connectAdapters(adapters);
|
|
508
|
+
console.log({ peersA: repoA.peers, peersB: repoB.peers });
|
|
509
|
+
|
|
510
|
+
const docA = repoA.create({ text: 'Hello world' });
|
|
511
|
+
|
|
512
|
+
const docB = await repoB.find(docA.url, { allowableStates: ['ready', 'unavailable'] });
|
|
513
|
+
expect(docB.state).to.equal('unavailable');
|
|
514
|
+
log.info('unavailable');
|
|
515
|
+
|
|
516
|
+
announce = true;
|
|
517
|
+
repoB.shareConfigChanged();
|
|
518
|
+
await docB.whenReady();
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
describe('create2 tests', () => {
|
|
523
|
+
test('basic document creation', async () => {
|
|
524
|
+
const repo = new Repo({ network: [] });
|
|
525
|
+
|
|
526
|
+
const handle = await repo.create2<{ text: string }>();
|
|
527
|
+
expect(handle.doc()).to.deep.equal({});
|
|
528
|
+
|
|
529
|
+
handle.change((doc: any) => {
|
|
530
|
+
doc.text = 'Hello World';
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
expect(handle.doc()?.text).to.equal('Hello World');
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
test('document mutation', async () => {
|
|
537
|
+
const repo = new Repo({ network: [] });
|
|
538
|
+
|
|
539
|
+
const handle = await repo.create2<{ counter: number }>({ counter: 0 });
|
|
540
|
+
|
|
541
|
+
for (let i = 1; i <= 10; i++) {
|
|
542
|
+
handle.change((doc: any) => {
|
|
543
|
+
doc.counter = i;
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
expect(handle.doc()?.counter).to.equal(10);
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
test('import document and mutate', async () => {
|
|
551
|
+
const repo = new Repo({ network: [] });
|
|
552
|
+
|
|
553
|
+
const original = await repo.create2<{ text: string }>({ text: 'original' });
|
|
554
|
+
const blob = A.save(original.doc()!);
|
|
555
|
+
|
|
556
|
+
const imported = repo.import<{ text: string }>(blob);
|
|
557
|
+
expect(imported.doc()?.text).to.equal('original');
|
|
558
|
+
|
|
559
|
+
imported.change((doc: any) => {
|
|
560
|
+
doc.text = 'mutated';
|
|
561
|
+
});
|
|
562
|
+
expect(imported.doc()?.text).to.equal('mutated');
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
test('reload document with flush', async () => {
|
|
566
|
+
const path = createTmpPath();
|
|
567
|
+
const text = 'Hello World!';
|
|
568
|
+
let url: AutomergeUrl;
|
|
569
|
+
|
|
570
|
+
{
|
|
571
|
+
const level = createTestLevel(path);
|
|
572
|
+
const storage = await createLevelAdapter(level);
|
|
573
|
+
const repo = new Repo({ network: [], storage });
|
|
574
|
+
const handle = await repo.create2<{ text: string }>();
|
|
575
|
+
url = handle.url;
|
|
576
|
+
handle.change((doc: any) => {
|
|
577
|
+
doc.text = text;
|
|
578
|
+
});
|
|
579
|
+
await repo.flush([handle.documentId]);
|
|
580
|
+
await level.close();
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
{
|
|
584
|
+
const level = createTestLevel(path);
|
|
585
|
+
const storage = await createLevelAdapter(level);
|
|
586
|
+
const repo = new Repo({ network: [], storage });
|
|
587
|
+
const handle = await repo.find<{ text: string }>(url);
|
|
588
|
+
await handle.whenReady();
|
|
589
|
+
expect(handle.doc()?.text).to.equal(text);
|
|
590
|
+
await level.close();
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
test('reload document without flush', async () => {
|
|
595
|
+
const path = createTmpPath();
|
|
596
|
+
const text = 'Hello World!';
|
|
597
|
+
let url: AutomergeUrl;
|
|
598
|
+
|
|
599
|
+
{
|
|
600
|
+
const level = createTestLevel(path);
|
|
601
|
+
const storage = await createLevelAdapter(level);
|
|
602
|
+
const repo = new Repo({ network: [], storage });
|
|
603
|
+
const handle = await repo.create2<{ text: string }>();
|
|
604
|
+
url = handle.url;
|
|
605
|
+
handle.change((doc: any) => {
|
|
606
|
+
doc.text = text;
|
|
607
|
+
});
|
|
608
|
+
// No explicit flush - rely on auto-save.
|
|
609
|
+
await sleep(200);
|
|
610
|
+
await level.close();
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
{
|
|
614
|
+
const level = createTestLevel(path);
|
|
615
|
+
const storage = await createLevelAdapter(level);
|
|
616
|
+
const repo = new Repo({ network: [], storage });
|
|
617
|
+
const handle = await repo.find<{ text: string }>(url);
|
|
618
|
+
await handle.whenReady();
|
|
619
|
+
expect(handle.doc()?.text).to.equal(text);
|
|
620
|
+
}
|
|
621
|
+
});
|
|
513
622
|
});
|
|
514
623
|
|
|
515
624
|
describe('teleport', () => {
|
|
@@ -730,8 +839,7 @@ describe('AutomergeRepo', () => {
|
|
|
730
839
|
});
|
|
731
840
|
});
|
|
732
841
|
|
|
733
|
-
const createLevelAdapter = async () => {
|
|
734
|
-
const level = createTestLevel();
|
|
842
|
+
const createLevelAdapter = async (level = createTestLevel()) => {
|
|
735
843
|
const storage = new LevelDBStorageAdapter({ db: level.sublevel('automerge') });
|
|
736
844
|
await openAndClose(level, storage);
|
|
737
845
|
return storage;
|
|
@@ -753,19 +861,23 @@ describe('AutomergeRepo', () => {
|
|
|
753
861
|
};
|
|
754
862
|
});
|
|
755
863
|
|
|
864
|
+
type ShareConfig = Exclude<ConstructorParameters<typeof Repo>[0], undefined>['shareConfig'];
|
|
865
|
+
|
|
756
866
|
type ConnectedRepoOptions = {
|
|
757
867
|
storages?: StorageAdapterInterface[];
|
|
758
868
|
connectionStateProvider?: TestConnectionStateProvider;
|
|
759
869
|
sharePolicy?: SharePolicy;
|
|
870
|
+
shareConfig?: ShareConfig;
|
|
760
871
|
};
|
|
761
872
|
|
|
762
873
|
const createRepoTopology = async <Peers extends string[], Peer extends string = Peers[number]>(args: {
|
|
763
874
|
peers: Peers;
|
|
764
875
|
connections: [Peer, Peer][];
|
|
765
876
|
options?: ConnectedRepoOptions;
|
|
877
|
+
onMessage?: (message: Message) => void;
|
|
766
878
|
}) => {
|
|
767
879
|
const adapters = args.connections.map(
|
|
768
|
-
() => TestAdapter.createPair(args.options?.connectionStateProvider) as [TestAdapter, TestAdapter],
|
|
880
|
+
() => TestAdapter.createPair(args.options?.connectionStateProvider, args.onMessage) as [TestAdapter, TestAdapter],
|
|
769
881
|
);
|
|
770
882
|
const repos = args.peers.map((peerId, peerIndex) => {
|
|
771
883
|
const network = adapters
|
|
@@ -781,7 +893,8 @@ const createRepoTopology = async <Peers extends string[], Peer extends string =
|
|
|
781
893
|
peerId: peerId as PeerId,
|
|
782
894
|
storage: args.options?.storages?.[peerIndex],
|
|
783
895
|
network,
|
|
784
|
-
sharePolicy: args.options?.sharePolicy ??
|
|
896
|
+
sharePolicy: (args.options?.sharePolicy ?? args.options?.shareConfig) ? undefined : async () => true,
|
|
897
|
+
shareConfig: args.options?.shareConfig,
|
|
785
898
|
});
|
|
786
899
|
});
|
|
787
900
|
return { repos, adapters };
|
|
@@ -848,7 +961,7 @@ const createTeleportTestPeer = async (
|
|
|
848
961
|
});
|
|
849
962
|
await echoAdapter.open();
|
|
850
963
|
await echoAdapter.whenConnected();
|
|
851
|
-
await echoAdapter.addReplicator(meshAdapter);
|
|
964
|
+
await echoAdapter.addReplicator(Context.default(), meshAdapter);
|
|
852
965
|
const [teleport] = teleportBuilder.createPeers({ factory: () => new TeleportPeer() });
|
|
853
966
|
return { repo, meshAdapter, teleport };
|
|
854
967
|
};
|
|
@@ -867,3 +980,7 @@ const connectPeers = async (
|
|
|
867
980
|
};
|
|
868
981
|
|
|
869
982
|
type TeleportTestPeer = { repo: Repo; meshAdapter: MeshEchoReplicator; teleport: TeleportPeer };
|
|
983
|
+
|
|
984
|
+
export const createTmpPath = (): string => {
|
|
985
|
+
return `/tmp/dxos-${PublicKey.random().toHex()}`;
|
|
986
|
+
};
|
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as A from '@automerge/automerge';
|
|
6
|
+
import type { DocumentId, PeerId } from '@automerge/automerge-repo';
|
|
6
7
|
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { sleep } from '@dxos/async';
|
|
10
|
+
import { range } from '@dxos/util';
|
|
9
11
|
|
|
10
12
|
import { type CollectionState, CollectionSynchronizer, diffCollectionState } from './collection-synchronizer';
|
|
11
13
|
|
|
@@ -53,16 +55,18 @@ describe('CollectionSynchronizer', () => {
|
|
|
53
55
|
peer1.onConnectionOpen(peerId2);
|
|
54
56
|
peer2.onConnectionOpen(peerId1);
|
|
55
57
|
|
|
58
|
+
const updated = Promise.all([
|
|
59
|
+
peer1.remoteStateUpdated.waitFor((ev) => ev.collectionId === collectionId && ev.peerId === peerId2),
|
|
60
|
+
peer2.remoteStateUpdated.waitFor((ev) => ev.collectionId === collectionId && ev.peerId === peerId1),
|
|
61
|
+
]);
|
|
62
|
+
|
|
56
63
|
peer1.setLocalCollectionState(collectionId, STATE_1);
|
|
57
64
|
peer2.setLocalCollectionState(collectionId, STATE_2);
|
|
58
65
|
|
|
59
66
|
peer1.refreshCollection(collectionId);
|
|
60
67
|
peer2.refreshCollection(collectionId);
|
|
61
68
|
|
|
62
|
-
await
|
|
63
|
-
peer1.remoteStateUpdated.waitFor((ev) => ev.collectionId === collectionId && ev.peerId === peerId2),
|
|
64
|
-
peer2.remoteStateUpdated.waitFor((ev) => ev.collectionId === collectionId && ev.peerId === peerId1),
|
|
65
|
-
]);
|
|
69
|
+
await updated;
|
|
66
70
|
|
|
67
71
|
expect(peer1.getRemoteCollectionStates(collectionId).get(peerId2)).to.deep.equal(STATE_2);
|
|
68
72
|
expect(peer2.getRemoteCollectionStates(collectionId).get(peerId1)).to.deep.equal(STATE_1);
|
|
@@ -79,18 +83,20 @@ describe('CollectionSynchronizer', () => {
|
|
|
79
83
|
});
|
|
80
84
|
});
|
|
81
85
|
|
|
86
|
+
const TEST_HEADS = range(4).map((i) => A.getHeads(A.from({ i: i.toString() })));
|
|
87
|
+
|
|
82
88
|
const STATE_1: CollectionState = {
|
|
83
89
|
documents: {
|
|
84
|
-
a: [
|
|
85
|
-
b: [
|
|
86
|
-
c: [
|
|
87
|
-
},
|
|
90
|
+
a: TEST_HEADS[0],
|
|
91
|
+
b: TEST_HEADS[1],
|
|
92
|
+
c: TEST_HEADS[2],
|
|
93
|
+
} as Record<DocumentId, A.Heads>,
|
|
88
94
|
};
|
|
89
95
|
|
|
90
96
|
const STATE_2: CollectionState = {
|
|
91
97
|
documents: {
|
|
92
|
-
a: [
|
|
93
|
-
b: [
|
|
94
|
-
d: [
|
|
95
|
-
},
|
|
98
|
+
a: TEST_HEADS[0],
|
|
99
|
+
b: TEST_HEADS[3],
|
|
100
|
+
d: TEST_HEADS[2],
|
|
101
|
+
} as Record<DocumentId, A.Heads>,
|
|
96
102
|
};
|
|
@@ -2,8 +2,10 @@
|
|
|
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
10
|
import { Event, asyncReturn, scheduleTask, scheduleTaskInterval } from '@dxos/async';
|
|
9
11
|
import { type Context, Resource } from '@dxos/context';
|
|
@@ -13,9 +15,9 @@ import { defaultMap } from '@dxos/util';
|
|
|
13
15
|
|
|
14
16
|
const MIN_QUERY_INTERVAL = 5_000;
|
|
15
17
|
|
|
16
|
-
const POLL_INTERVAL =
|
|
18
|
+
const POLL_INTERVAL = 10_000;
|
|
17
19
|
|
|
18
|
-
export type
|
|
20
|
+
export type CollectionSynchronizerProps = {
|
|
19
21
|
sendCollectionState: (collectionId: string, peerId: PeerId, state: CollectionState) => void;
|
|
20
22
|
queryCollectionState: (collectionId: string, peerId: PeerId) => void;
|
|
21
23
|
shouldSyncCollection: (collectionId: string, peerId: PeerId) => boolean;
|
|
@@ -24,11 +26,11 @@ export type CollectionSynchronizerParams = {
|
|
|
24
26
|
/**
|
|
25
27
|
* Implements collection sync protocol.
|
|
26
28
|
*/
|
|
27
|
-
@trace.resource()
|
|
29
|
+
@trace.resource({ lifecycle: true })
|
|
28
30
|
export class CollectionSynchronizer extends Resource {
|
|
29
|
-
private readonly _sendCollectionState:
|
|
30
|
-
private readonly _queryCollectionState:
|
|
31
|
-
private readonly _shouldSyncCollection:
|
|
31
|
+
private readonly _sendCollectionState: CollectionSynchronizerProps['sendCollectionState'];
|
|
32
|
+
private readonly _queryCollectionState: CollectionSynchronizerProps['queryCollectionState'];
|
|
33
|
+
private readonly _shouldSyncCollection: CollectionSynchronizerProps['shouldSyncCollection'];
|
|
32
34
|
|
|
33
35
|
/**
|
|
34
36
|
* CollectionId -> State.
|
|
@@ -40,7 +42,7 @@ export class CollectionSynchronizer extends Resource {
|
|
|
40
42
|
|
|
41
43
|
public readonly remoteStateUpdated = new Event<{ collectionId: string; peerId: PeerId; newDocsAppeared: boolean }>();
|
|
42
44
|
|
|
43
|
-
constructor(params:
|
|
45
|
+
constructor(params: CollectionSynchronizerProps) {
|
|
44
46
|
super();
|
|
45
47
|
this._sendCollectionState = params.sendCollectionState;
|
|
46
48
|
this._queryCollectionState = params.queryCollectionState;
|
|
@@ -76,6 +78,10 @@ export class CollectionSynchronizer extends Resource {
|
|
|
76
78
|
log('setLocalCollectionState', { collectionId, state });
|
|
77
79
|
this._getOrCreatePerCollectionState(collectionId).localState = state;
|
|
78
80
|
|
|
81
|
+
for (const peerId of this._connectedPeers) {
|
|
82
|
+
this._diffCollectionState(collectionId, peerId);
|
|
83
|
+
}
|
|
84
|
+
|
|
79
85
|
queueMicrotask(async () => {
|
|
80
86
|
if (!this._ctx.disposed && this._activeCollections.has(collectionId)) {
|
|
81
87
|
this._refreshInterestedPeers(collectionId);
|
|
@@ -117,10 +123,14 @@ export class CollectionSynchronizer extends Resource {
|
|
|
117
123
|
* Callback when a connection to a peer is established.
|
|
118
124
|
*/
|
|
119
125
|
onConnectionOpen(peerId: PeerId): void {
|
|
120
|
-
|
|
121
|
-
|
|
126
|
+
log('onConnectionOpen', { peerId });
|
|
127
|
+
const spanId = getSpanId(peerId);
|
|
128
|
+
// Browser-timeline-only span; the derived ctx is intentionally discarded because
|
|
129
|
+
// the downstream `_queryCollectionState` hop is user-supplied callback land with
|
|
130
|
+
// no ctx plumbing.
|
|
131
|
+
void trace.spanStart({
|
|
122
132
|
id: spanId,
|
|
123
|
-
methodName:
|
|
133
|
+
methodName: SYNC_SPAN_METHOD,
|
|
124
134
|
instance: this,
|
|
125
135
|
parentCtx: this._ctx,
|
|
126
136
|
showInBrowserTimeline: true,
|
|
@@ -146,6 +156,8 @@ export class CollectionSynchronizer extends Resource {
|
|
|
146
156
|
* Callback when a connection to a peer is closed.
|
|
147
157
|
*/
|
|
148
158
|
onConnectionClosed(peerId: PeerId): void {
|
|
159
|
+
log('onConnectionClosed', { peerId });
|
|
160
|
+
|
|
149
161
|
this._connectedPeers.delete(peerId);
|
|
150
162
|
|
|
151
163
|
for (const perCollectionState of this._perCollectionStates.values()) {
|
|
@@ -171,24 +183,48 @@ export class CollectionSynchronizer extends Resource {
|
|
|
171
183
|
log('onRemoteStateReceived', { collectionId, peerId, state });
|
|
172
184
|
validateCollectionState(state);
|
|
173
185
|
const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
186
|
+
perCollectionState.remoteStates.set(peerId, state);
|
|
187
|
+
this._diffCollectionState(collectionId, peerId);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private _diffCollectionState(collectionId: string, peerId: PeerId) {
|
|
191
|
+
const perCollectionState = this._getOrCreatePerCollectionState(collectionId);
|
|
192
|
+
const remoteState = perCollectionState.remoteStates.get(peerId);
|
|
193
|
+
if (!remoteState) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
log('diffCollectionState', { collectionId, peerId });
|
|
198
|
+
const localState = perCollectionState.localState ?? { documents: {} };
|
|
199
|
+
const diff = diffCollectionState(localState, remoteState);
|
|
200
|
+
const spanId = getSpanId(peerId);
|
|
177
201
|
if (diff.different.length === 0) {
|
|
178
202
|
trace.spanEnd(spanId);
|
|
179
203
|
} else {
|
|
180
|
-
|
|
204
|
+
// Browser-timeline-only span; see note in onConnectionOpen.
|
|
205
|
+
void trace.spanStart({
|
|
181
206
|
id: spanId,
|
|
182
|
-
methodName:
|
|
207
|
+
methodName: SYNC_SPAN_METHOD,
|
|
183
208
|
instance: this,
|
|
184
209
|
parentCtx: this._ctx,
|
|
185
210
|
showInBrowserTimeline: true,
|
|
186
211
|
attributes: { peerId },
|
|
187
212
|
});
|
|
188
213
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
214
|
+
log('diff', {
|
|
215
|
+
localState: localState.documents,
|
|
216
|
+
remoteState: remoteState.documents,
|
|
217
|
+
missingOnLocal: diff.missingOnLocal,
|
|
218
|
+
missingOnRemote: diff.missingOnRemote,
|
|
219
|
+
different: diff.different,
|
|
220
|
+
});
|
|
221
|
+
if (diff.missingOnLocal.length > 0 || diff.different.length > 0 || diff.missingOnRemote.length > 0) {
|
|
222
|
+
log('emit remote state update');
|
|
223
|
+
this.remoteStateUpdated.emit({
|
|
224
|
+
peerId,
|
|
225
|
+
collectionId,
|
|
226
|
+
newDocsAppeared: diff.missingOnLocal.length > 0,
|
|
227
|
+
});
|
|
192
228
|
}
|
|
193
229
|
}
|
|
194
230
|
|
|
@@ -223,7 +259,7 @@ export type CollectionState = {
|
|
|
223
259
|
/**
|
|
224
260
|
* DocumentId -> Heads.
|
|
225
261
|
*/
|
|
226
|
-
documents: Record<
|
|
262
|
+
documents: Record<DocumentId, Heads>;
|
|
227
263
|
};
|
|
228
264
|
|
|
229
265
|
export type CollectionStateDiff = {
|
|
@@ -233,18 +269,21 @@ export type CollectionStateDiff = {
|
|
|
233
269
|
};
|
|
234
270
|
|
|
235
271
|
export const diffCollectionState = (local: CollectionState, remote: CollectionState): CollectionStateDiff => {
|
|
236
|
-
const
|
|
272
|
+
const localDocuments = Record.filter(local.documents, (heads) => heads.length > 0);
|
|
273
|
+
const remoteDocuments = Record.filter(remote.documents, (heads) => heads.length > 0);
|
|
274
|
+
// NOTE: Using `Array.union` is slow.
|
|
275
|
+
const allDocuments = [...new Set([...Record.keys(localDocuments), ...Record.keys(remoteDocuments)])] as DocumentId[];
|
|
237
276
|
|
|
238
277
|
const missingOnRemote: DocumentId[] = [];
|
|
239
278
|
const missingOnLocal: DocumentId[] = [];
|
|
240
279
|
const different: DocumentId[] = [];
|
|
241
280
|
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
|
|
281
|
+
if (!localDocuments[documentId]) {
|
|
282
|
+
missingOnLocal.push(documentId);
|
|
283
|
+
} else if (!remoteDocuments[documentId]) {
|
|
284
|
+
missingOnRemote.push(documentId);
|
|
285
|
+
} else if (!A.equals(local.documents[documentId], remote.documents[documentId])) {
|
|
286
|
+
different.push(documentId);
|
|
248
287
|
}
|
|
249
288
|
}
|
|
250
289
|
|
|
@@ -270,6 +309,8 @@ const isValidDocumentId = (documentId: DocumentId) => {
|
|
|
270
309
|
return typeof documentId === 'string' && !documentId.includes(':');
|
|
271
310
|
};
|
|
272
311
|
|
|
273
|
-
const
|
|
312
|
+
const SYNC_SPAN_METHOD = 'syncPeer';
|
|
313
|
+
|
|
314
|
+
const getSpanId = (peerId: PeerId) => {
|
|
274
315
|
return `collection-sync-${peerId}`;
|
|
275
316
|
};
|
|
@@ -6,6 +6,7 @@ import { type PeerId, cbor } from '@automerge/automerge-repo';
|
|
|
6
6
|
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
7
7
|
|
|
8
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
|
|