@dxos/echo-pipeline 0.5.8 → 0.5.9-main.0a0e87d
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-GANAND63.mjs → chunk-I2J5TTHJ.mjs} +48 -23
- package/dist/lib/browser/{chunk-GANAND63.mjs.map → chunk-I2J5TTHJ.mjs.map} +3 -3
- package/dist/lib/browser/index.mjs +89 -51
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +99 -4
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/{chunk-M475BGBI.cjs → chunk-QPCNQ4ZK.cjs} +51 -25
- package/dist/lib/node/chunk-QPCNQ4ZK.cjs.map +7 -0
- package/dist/lib/node/index.cjs +110 -72
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +107 -13
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/types/src/automerge/automerge-doc-loader.d.ts +7 -3
- package/dist/types/src/automerge/automerge-doc-loader.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-network-adapter.d.ts +6 -0
- package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
- package/dist/types/src/automerge/echo-replicator.d.ts +1 -0
- package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
- package/dist/types/src/automerge/migrations.d.ts.map +1 -1
- package/dist/types/src/space/space-manager.d.ts +2 -2
- package/dist/types/src/space/space-manager.d.ts.map +1 -1
- package/dist/types/src/space/space-protocol.d.ts +2 -2
- package/dist/types/src/space/space-protocol.d.ts.map +1 -1
- package/dist/types/src/space/space.d.ts +9 -1
- package/dist/types/src/space/space.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +1 -0
- 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-network-adapter.d.ts +18 -0
- package/dist/types/src/testing/test-network-adapter.d.ts.map +1 -0
- package/package.json +33 -33
- package/src/automerge/automerge-doc-loader.test.ts +5 -2
- package/src/automerge/automerge-doc-loader.ts +6 -4
- package/src/automerge/automerge-host.test.ts +1 -553
- package/src/automerge/automerge-host.ts +12 -5
- package/src/automerge/automerge-repo.test.ts +450 -2
- package/src/automerge/echo-network-adapter.ts +24 -8
- package/src/automerge/echo-replicator.ts +2 -0
- package/src/automerge/mesh-echo-replicator.ts +3 -1
- package/src/automerge/migrations.ts +2 -1
- package/src/automerge/storage-adapter.test.ts +81 -15
- package/src/space/space-manager.ts +6 -4
- package/src/space/space-protocol.test.ts +5 -3
- package/src/space/space-protocol.ts +3 -3
- package/src/space/space.ts +32 -2
- package/src/testing/index.ts +1 -0
- package/src/testing/test-agent-builder.ts +4 -4
- package/src/testing/test-network-adapter.ts +62 -0
- package/dist/lib/node/chunk-M475BGBI.cjs.map +0 -7
|
@@ -3,16 +3,40 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { expect } from 'chai';
|
|
6
|
+
import waitForExpect from 'wait-for-expect';
|
|
6
7
|
|
|
8
|
+
import { asyncTimeout, sleep } from '@dxos/async';
|
|
7
9
|
import { type Heads, change, clone, equals, from, getBackend, getHeads } from '@dxos/automerge/automerge';
|
|
8
|
-
import { Repo } from '@dxos/automerge/automerge-repo';
|
|
10
|
+
import { type Message, Repo, type PeerId, type DocumentId, type HandleState } from '@dxos/automerge/automerge-repo';
|
|
9
11
|
import { randomBytes } from '@dxos/crypto';
|
|
12
|
+
import { PublicKey } from '@dxos/keys';
|
|
10
13
|
import { createTestLevel } from '@dxos/kv-store/testing';
|
|
11
|
-
import {
|
|
14
|
+
import { TestBuilder as TeleportBuilder, TestPeer as TeleportPeer } from '@dxos/teleport/testing';
|
|
15
|
+
import { afterTest, describe, openAndClose, test } from '@dxos/test';
|
|
12
16
|
|
|
17
|
+
import { EchoNetworkAdapter } from './echo-network-adapter';
|
|
13
18
|
import { LevelDBStorageAdapter } from './leveldb-storage-adapter';
|
|
19
|
+
import { MeshEchoReplicator } from './mesh-echo-replicator';
|
|
20
|
+
import { TestAdapter } from '../testing';
|
|
14
21
|
|
|
15
22
|
describe('AutomergeRepo', () => {
|
|
23
|
+
test('change events', () => {
|
|
24
|
+
const repo = new Repo({ network: [] });
|
|
25
|
+
const handle = repo.create<{ field?: string }>();
|
|
26
|
+
|
|
27
|
+
let valueDuringChange: string | undefined;
|
|
28
|
+
|
|
29
|
+
handle.addListener('change', (doc) => {
|
|
30
|
+
valueDuringChange = handle.docSync().field;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
handle.change((doc: any) => {
|
|
34
|
+
doc.field = 'value';
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(valueDuringChange).to.eq('value');
|
|
38
|
+
});
|
|
39
|
+
|
|
16
40
|
test('flush', async () => {
|
|
17
41
|
const level = createTestLevel();
|
|
18
42
|
const storage = new LevelDBStorageAdapter({ db: level.sublevel('automerge') });
|
|
@@ -67,4 +91,428 @@ describe('AutomergeRepo', () => {
|
|
|
67
91
|
expect(equals(a, b)).to.be.false;
|
|
68
92
|
expect(equals(a, c)).to.be.true;
|
|
69
93
|
});
|
|
94
|
+
|
|
95
|
+
describe('network', () => {
|
|
96
|
+
test('basic networking', async () => {
|
|
97
|
+
const hostAdapter: TestAdapter = new TestAdapter({
|
|
98
|
+
send: (message: Message) => clientAdapter.receive(message),
|
|
99
|
+
});
|
|
100
|
+
const clientAdapter: TestAdapter = new TestAdapter({
|
|
101
|
+
send: (message: Message) => hostAdapter.receive(message),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const host = new Repo({
|
|
105
|
+
network: [hostAdapter],
|
|
106
|
+
});
|
|
107
|
+
const client = new Repo({
|
|
108
|
+
network: [clientAdapter],
|
|
109
|
+
});
|
|
110
|
+
hostAdapter.ready();
|
|
111
|
+
clientAdapter.ready();
|
|
112
|
+
await hostAdapter.onConnect.wait();
|
|
113
|
+
await clientAdapter.onConnect.wait();
|
|
114
|
+
hostAdapter.peerCandidate(clientAdapter.peerId!);
|
|
115
|
+
clientAdapter.peerCandidate(hostAdapter.peerId!);
|
|
116
|
+
|
|
117
|
+
const handle = host.create();
|
|
118
|
+
const text = 'Hello world';
|
|
119
|
+
handle.change((doc: any) => {
|
|
120
|
+
doc.text = text;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const docOnClient = client.find(handle.url);
|
|
124
|
+
expect((await asyncTimeout(docOnClient.doc(), 1000)).text).to.equal(text);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('share policy gets enabled afterwards', async () => {
|
|
128
|
+
const [hostAdapter, clientAdapter] = TestAdapter.createPair();
|
|
129
|
+
let sharePolicy = false;
|
|
130
|
+
|
|
131
|
+
const host = new Repo({
|
|
132
|
+
network: [hostAdapter],
|
|
133
|
+
peerId: 'host' as PeerId,
|
|
134
|
+
sharePolicy: async () => sharePolicy,
|
|
135
|
+
});
|
|
136
|
+
const client = new Repo({
|
|
137
|
+
network: [clientAdapter],
|
|
138
|
+
peerId: 'client' as PeerId,
|
|
139
|
+
sharePolicy: async () => sharePolicy,
|
|
140
|
+
});
|
|
141
|
+
hostAdapter.ready();
|
|
142
|
+
clientAdapter.ready();
|
|
143
|
+
await hostAdapter.onConnect.wait();
|
|
144
|
+
await clientAdapter.onConnect.wait();
|
|
145
|
+
hostAdapter.peerCandidate(clientAdapter.peerId!);
|
|
146
|
+
clientAdapter.peerCandidate(hostAdapter.peerId!);
|
|
147
|
+
|
|
148
|
+
const handle = host.create();
|
|
149
|
+
const text = 'Hello world';
|
|
150
|
+
handle.change((doc: any) => {
|
|
151
|
+
doc.text = text;
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
{
|
|
155
|
+
const docOnClient = client.find(handle.url);
|
|
156
|
+
await asyncTimeout(docOnClient.whenReady(['unavailable']), 1000);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
sharePolicy = true;
|
|
160
|
+
|
|
161
|
+
{
|
|
162
|
+
const docOnClient = client.find(handle.url);
|
|
163
|
+
// TODO(mykola): We expect the document to be available here, but it's not.
|
|
164
|
+
await asyncTimeout(docOnClient.whenReady(['unavailable']), 1000);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('two documents and share policy switching', async () => {
|
|
169
|
+
const [hostAdapter, clientAdapter] = TestAdapter.createPair();
|
|
170
|
+
const allowedDocs: DocumentId[] = [];
|
|
171
|
+
|
|
172
|
+
const host: Repo = new Repo({
|
|
173
|
+
network: [hostAdapter],
|
|
174
|
+
peerId: 'host' as PeerId,
|
|
175
|
+
sharePolicy: async (_, docId) => (docId ? allowedDocs.includes(docId) && !!host.handles[docId] : false),
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const client: Repo = new Repo({
|
|
179
|
+
network: [clientAdapter],
|
|
180
|
+
peerId: 'client' as PeerId,
|
|
181
|
+
sharePolicy: async (_, docId) => (docId ? allowedDocs.includes(docId) && !!client.handles[docId] : false),
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const firstHandle = host.create();
|
|
185
|
+
firstHandle.change((doc: any) => (doc.text = 'Hello world'));
|
|
186
|
+
await host.find(firstHandle.url).whenReady();
|
|
187
|
+
allowedDocs.push(firstHandle.documentId);
|
|
188
|
+
|
|
189
|
+
{
|
|
190
|
+
// Initiate connection.
|
|
191
|
+
hostAdapter.ready();
|
|
192
|
+
clientAdapter.ready();
|
|
193
|
+
await hostAdapter.onConnect.wait();
|
|
194
|
+
await clientAdapter.onConnect.wait();
|
|
195
|
+
hostAdapter.peerCandidate(clientAdapter.peerId!);
|
|
196
|
+
clientAdapter.peerCandidate(hostAdapter.peerId!);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
{
|
|
200
|
+
const firstDocOnClient = client.find(firstHandle.url);
|
|
201
|
+
await asyncTimeout(firstDocOnClient.whenReady(), 1000);
|
|
202
|
+
expect(firstDocOnClient.docSync().text).to.equal('Hello world');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const secondHandle = host.create();
|
|
206
|
+
secondHandle.change((doc: any) => (doc.text = 'Hello world'));
|
|
207
|
+
await host.find(secondHandle.url).whenReady();
|
|
208
|
+
allowedDocs.push(secondHandle.documentId);
|
|
209
|
+
|
|
210
|
+
{
|
|
211
|
+
const secondDocOnClient = client.find(secondHandle.url);
|
|
212
|
+
await asyncTimeout(secondDocOnClient.whenReady(), 1000);
|
|
213
|
+
expect(secondDocOnClient.docSync().text).to.equal('Hello world');
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test('recovering from a lost connection', async () => {
|
|
218
|
+
let connectionState: 'on' | 'off' = 'on';
|
|
219
|
+
|
|
220
|
+
const hostAdapter: TestAdapter = new TestAdapter({
|
|
221
|
+
send: (message: Message) => connectionState === 'on' && sleep(10).then(() => clientAdapter.receive(message)),
|
|
222
|
+
});
|
|
223
|
+
const clientAdapter: TestAdapter = new TestAdapter({
|
|
224
|
+
send: (message: Message) => connectionState === 'on' && sleep(10).then(() => hostAdapter.receive(message)),
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const host = new Repo({
|
|
228
|
+
network: [hostAdapter],
|
|
229
|
+
});
|
|
230
|
+
const client = new Repo({
|
|
231
|
+
network: [clientAdapter],
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// Establish connection.
|
|
235
|
+
hostAdapter.ready();
|
|
236
|
+
clientAdapter.ready();
|
|
237
|
+
await hostAdapter.onConnect.wait();
|
|
238
|
+
await clientAdapter.onConnect.wait();
|
|
239
|
+
hostAdapter.peerCandidate(clientAdapter.peerId!);
|
|
240
|
+
clientAdapter.peerCandidate(hostAdapter.peerId!);
|
|
241
|
+
|
|
242
|
+
const handle = host.create();
|
|
243
|
+
const docOnClient = client.find(handle.url);
|
|
244
|
+
{
|
|
245
|
+
const sanityText = 'Hello world';
|
|
246
|
+
handle.change((doc: any) => {
|
|
247
|
+
doc.sanityText = sanityText;
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
expect((await asyncTimeout(docOnClient.doc(), 1000)).sanityText).to.equal(sanityText);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Disrupt connection.
|
|
254
|
+
const offlineText = 'This has been written while the connection was off';
|
|
255
|
+
{
|
|
256
|
+
connectionState = 'off';
|
|
257
|
+
|
|
258
|
+
handle.change((doc: any) => {
|
|
259
|
+
doc.offlineText = offlineText;
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
await sleep(100);
|
|
263
|
+
expect((await asyncTimeout(docOnClient.doc(), 1000)).offlineText).to.be.undefined;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Re-establish connection.
|
|
267
|
+
const onlineText = 'This has been written after the connection was re-established';
|
|
268
|
+
{
|
|
269
|
+
connectionState = 'on';
|
|
270
|
+
hostAdapter.peerDisconnected(clientAdapter.peerId!);
|
|
271
|
+
clientAdapter.peerDisconnected(hostAdapter.peerId!);
|
|
272
|
+
hostAdapter.peerCandidate(clientAdapter.peerId!);
|
|
273
|
+
clientAdapter.peerCandidate(hostAdapter.peerId!);
|
|
274
|
+
|
|
275
|
+
handle.change((doc: any) => {
|
|
276
|
+
doc.onlineText = onlineText;
|
|
277
|
+
});
|
|
278
|
+
await sleep(100);
|
|
279
|
+
expect((await asyncTimeout(docOnClient.doc(), 1000)).onlineText).to.equal(onlineText);
|
|
280
|
+
expect((await asyncTimeout(docOnClient.doc(), 1000)).offlineText).to.equal(offlineText);
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
test('integration test with teleport', async () => {
|
|
285
|
+
const [spaceKey] = PublicKey.randomSequence();
|
|
286
|
+
|
|
287
|
+
const createAutomergeRepo = async () => {
|
|
288
|
+
const meshAdapter = new MeshEchoReplicator();
|
|
289
|
+
const echoAdapter = new EchoNetworkAdapter({
|
|
290
|
+
getContainingSpaceForDocument: async () => spaceKey,
|
|
291
|
+
});
|
|
292
|
+
const repo = new Repo({
|
|
293
|
+
network: [echoAdapter],
|
|
294
|
+
});
|
|
295
|
+
await echoAdapter.open();
|
|
296
|
+
await echoAdapter.whenConnected();
|
|
297
|
+
await echoAdapter.addReplicator(meshAdapter);
|
|
298
|
+
return { repo, meshAdapter };
|
|
299
|
+
};
|
|
300
|
+
const peer1 = await createAutomergeRepo();
|
|
301
|
+
const peer2 = await createAutomergeRepo();
|
|
302
|
+
|
|
303
|
+
const handle = peer1.repo.create();
|
|
304
|
+
|
|
305
|
+
const teleportBuilder = new TeleportBuilder();
|
|
306
|
+
afterTest(() => teleportBuilder.destroy());
|
|
307
|
+
|
|
308
|
+
const [teleportPeer1, teleportPeer2] = teleportBuilder.createPeers({ factory: () => new TeleportPeer() });
|
|
309
|
+
{
|
|
310
|
+
// Initiate connection.
|
|
311
|
+
const [connection1, connection2] = await teleportBuilder.connect(teleportPeer1, teleportPeer2);
|
|
312
|
+
connection1.teleport.addExtension('automerge', peer1.meshAdapter.createExtension());
|
|
313
|
+
connection2.teleport.addExtension('automerge', peer2.meshAdapter.createExtension());
|
|
314
|
+
|
|
315
|
+
// Test connection.
|
|
316
|
+
const text = 'Hello world';
|
|
317
|
+
handle.change((doc: any) => {
|
|
318
|
+
doc.text = text;
|
|
319
|
+
});
|
|
320
|
+
await waitForExpect(async () => {
|
|
321
|
+
const docOnPeer2 = peer2.repo.find(handle.url);
|
|
322
|
+
const doc = await asyncTimeout(docOnPeer2.doc(), 1000);
|
|
323
|
+
expect(doc.text).to.eq(text);
|
|
324
|
+
}, 1000);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const offlineText = 'This has been written while the connection was off';
|
|
328
|
+
{
|
|
329
|
+
// Disconnect peers.
|
|
330
|
+
await teleportBuilder.disconnect(teleportPeer1, teleportPeer2);
|
|
331
|
+
|
|
332
|
+
// Make offline changes.
|
|
333
|
+
const offlineText = 'This has been written while the connection was off';
|
|
334
|
+
handle.change((doc: any) => {
|
|
335
|
+
doc.offlineText = offlineText;
|
|
336
|
+
});
|
|
337
|
+
const docOnPeer2 = peer2.repo.find(handle.url);
|
|
338
|
+
await sleep(100);
|
|
339
|
+
expect((await asyncTimeout(docOnPeer2.doc(), 1000)).offlineText).to.be.undefined;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
{
|
|
343
|
+
const docOnPeer2 = peer2.repo.find(handle.url);
|
|
344
|
+
const receivedUpdate = new Promise((resolve, reject) => docOnPeer2.once('heads-changed', resolve));
|
|
345
|
+
|
|
346
|
+
// Reconnect peers.
|
|
347
|
+
const [connection1, connection2] = await teleportBuilder.connect(teleportPeer1, teleportPeer2);
|
|
348
|
+
connection1.teleport.addExtension('automerge', peer1.meshAdapter.createExtension());
|
|
349
|
+
connection2.teleport.addExtension('automerge', peer2.meshAdapter.createExtension());
|
|
350
|
+
|
|
351
|
+
// Wait for offline changes to be synced.
|
|
352
|
+
await receivedUpdate;
|
|
353
|
+
await docOnPeer2.whenReady();
|
|
354
|
+
expect((await asyncTimeout(docOnPeer2.doc(), 1000)).offlineText).to.eq(offlineText);
|
|
355
|
+
|
|
356
|
+
// Test connection.
|
|
357
|
+
const onlineText = 'This has been written after the connection was re-established';
|
|
358
|
+
const receivedOnlineUpdate = new Promise((resolve, reject) => docOnPeer2.once('heads-changed', resolve));
|
|
359
|
+
handle.change((doc: any) => {
|
|
360
|
+
doc.onlineText = onlineText;
|
|
361
|
+
});
|
|
362
|
+
await receivedOnlineUpdate;
|
|
363
|
+
await docOnPeer2.whenReady();
|
|
364
|
+
expect((await asyncTimeout(docOnPeer2.doc(), 1000)).onlineText).to.eq(onlineText);
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
test('replication though a 4 peer chain', async () => {
|
|
369
|
+
const pairAB = TestAdapter.createPair();
|
|
370
|
+
const pairBC = TestAdapter.createPair();
|
|
371
|
+
const pairCD = TestAdapter.createPair();
|
|
372
|
+
|
|
373
|
+
const repoA = new Repo({
|
|
374
|
+
peerId: 'A' as any,
|
|
375
|
+
network: [pairAB[0]],
|
|
376
|
+
sharePolicy: async () => true,
|
|
377
|
+
});
|
|
378
|
+
const _repoB = new Repo({
|
|
379
|
+
peerId: 'B' as any,
|
|
380
|
+
network: [pairAB[1], pairBC[0]],
|
|
381
|
+
sharePolicy: async () => true,
|
|
382
|
+
});
|
|
383
|
+
const _repoC = new Repo({
|
|
384
|
+
peerId: 'C' as any,
|
|
385
|
+
network: [pairBC[1], pairCD[0]],
|
|
386
|
+
sharePolicy: async () => true,
|
|
387
|
+
});
|
|
388
|
+
const repoD = new Repo({
|
|
389
|
+
peerId: 'D' as any,
|
|
390
|
+
network: [pairCD[1]],
|
|
391
|
+
sharePolicy: async () => true,
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
for (const pair of [pairAB, pairBC, pairCD]) {
|
|
395
|
+
pair[0].ready();
|
|
396
|
+
pair[1].ready();
|
|
397
|
+
await pair[0].onConnect.wait();
|
|
398
|
+
await pair[1].onConnect.wait();
|
|
399
|
+
pair[0].peerCandidate(pair[1].peerId!);
|
|
400
|
+
pair[1].peerCandidate(pair[0].peerId!);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const docA = repoA.create();
|
|
404
|
+
// NOTE: Doesn't work if the doc is empty.
|
|
405
|
+
docA.change((doc: any) => {
|
|
406
|
+
doc.text = 'Hello world';
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
// If we wait here for replication to finish naturally, the test will pass.
|
|
410
|
+
|
|
411
|
+
const docD = repoD.find(docA.url);
|
|
412
|
+
|
|
413
|
+
await docD.whenReady();
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
test('replication though a 3 peer chain', async () => {
|
|
417
|
+
const pairAB = TestAdapter.createPair();
|
|
418
|
+
const pairBC = TestAdapter.createPair();
|
|
419
|
+
|
|
420
|
+
const repoA = new Repo({
|
|
421
|
+
peerId: 'A' as any,
|
|
422
|
+
network: [pairAB[0]],
|
|
423
|
+
sharePolicy: async () => true,
|
|
424
|
+
});
|
|
425
|
+
const repoB = new Repo({
|
|
426
|
+
peerId: 'B' as any,
|
|
427
|
+
network: [pairAB[1], pairBC[0]],
|
|
428
|
+
sharePolicy: async () => true,
|
|
429
|
+
});
|
|
430
|
+
const repoC = new Repo({
|
|
431
|
+
peerId: 'C' as any,
|
|
432
|
+
network: [pairBC[1]],
|
|
433
|
+
sharePolicy: async () => true,
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
for (const pair of [pairAB, pairBC]) {
|
|
437
|
+
pair[0].ready();
|
|
438
|
+
pair[1].ready();
|
|
439
|
+
await pair[0].onConnect.wait();
|
|
440
|
+
await pair[1].onConnect.wait();
|
|
441
|
+
pair[0].peerCandidate(pair[1].peerId!);
|
|
442
|
+
pair[1].peerCandidate(pair[0].peerId!);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const docA = repoA.create();
|
|
446
|
+
// NOTE: Doesn't work if the doc is empty.
|
|
447
|
+
docA.change((doc: any) => {
|
|
448
|
+
doc.text = 'Hello world';
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
const _docB = repoB.find(docA.url);
|
|
452
|
+
const docC = repoC.find(docA.url);
|
|
453
|
+
|
|
454
|
+
await docC.whenReady();
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test('replicate document after request', async () => {
|
|
458
|
+
const [adapter1, adapter2] = TestAdapter.createPair();
|
|
459
|
+
const repoA = new Repo({
|
|
460
|
+
peerId: 'A' as any,
|
|
461
|
+
network: [adapter1],
|
|
462
|
+
sharePolicy: async () => true,
|
|
463
|
+
});
|
|
464
|
+
const repoB = new Repo({
|
|
465
|
+
peerId: 'B' as any,
|
|
466
|
+
network: [adapter2],
|
|
467
|
+
sharePolicy: async () => true,
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
const unavailable: HandleState = 'unavailable';
|
|
471
|
+
|
|
472
|
+
{
|
|
473
|
+
// Connect repos.
|
|
474
|
+
adapter1.ready();
|
|
475
|
+
adapter2.ready();
|
|
476
|
+
await adapter1.onConnect.wait();
|
|
477
|
+
await adapter2.onConnect.wait();
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const docA = repoA.create();
|
|
481
|
+
// NOTE: Doesn't work if the doc is empty.
|
|
482
|
+
docA.change((doc: any) => {
|
|
483
|
+
doc.text = 'Hello world';
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
const docB = repoB.find(docA.url);
|
|
487
|
+
{
|
|
488
|
+
// Request document from repoB.
|
|
489
|
+
await asyncTimeout(docB.whenReady([unavailable]), 1_000);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
{
|
|
493
|
+
// Failing to find a document.
|
|
494
|
+
// await (docB.whenReady([unavailable]), 1_000);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
{
|
|
498
|
+
adapter1.peerCandidate(adapter2.peerId!);
|
|
499
|
+
await sleep(100);
|
|
500
|
+
adapter2.peerCandidate(adapter1.peerId!);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
{
|
|
504
|
+
await asyncTimeout(docB.whenReady([unavailable]), 1_000);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
{
|
|
508
|
+
// Note: Retry hack.
|
|
509
|
+
adapter1.peerDisconnected(adapter2.peerId!);
|
|
510
|
+
adapter2.peerDisconnected(adapter1.peerId!);
|
|
511
|
+
adapter1.peerCandidate(adapter2.peerId!);
|
|
512
|
+
adapter2.peerCandidate(adapter1.peerId!);
|
|
513
|
+
|
|
514
|
+
await asyncTimeout(docB.whenReady(), 1_000);
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
});
|
|
70
518
|
});
|
|
@@ -93,6 +93,7 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
93
93
|
peerId: this.peerId,
|
|
94
94
|
onConnectionOpen: this._onConnectionOpen.bind(this),
|
|
95
95
|
onConnectionClosed: this._onConnectionClosed.bind(this),
|
|
96
|
+
onConnectionAuthScopeChanged: this._onConnectionAuthScopeChanged.bind(this),
|
|
96
97
|
getContainingSpaceForDocument: this._params.getContainingSpaceForDocument,
|
|
97
98
|
});
|
|
98
99
|
}
|
|
@@ -101,7 +102,6 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
101
102
|
async removeReplicator(replicator: EchoReplicator) {
|
|
102
103
|
invariant(this._lifecycleState === LifecycleState.OPEN);
|
|
103
104
|
invariant(this._replicators.has(replicator));
|
|
104
|
-
('');
|
|
105
105
|
await replicator.disconnect();
|
|
106
106
|
this._replicators.delete(replicator);
|
|
107
107
|
}
|
|
@@ -142,13 +142,19 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
142
142
|
});
|
|
143
143
|
|
|
144
144
|
log('emit peer-candidate', { peerId: connection.peerId });
|
|
145
|
-
this.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
145
|
+
this._emitPeerCandidate(connection);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Trigger doc-synchronizer shared documents set recalculation. Happens on peer-candidate.
|
|
150
|
+
* TODO(y): replace with a proper API call when sharePolicy update becomes supported by automerge-repo
|
|
151
|
+
*/
|
|
152
|
+
private _onConnectionAuthScopeChanged(connection: ReplicatorConnection) {
|
|
153
|
+
log('Connection auth scope changed', { peerId: connection.peerId });
|
|
154
|
+
const entry = this._connections.get(connection.peerId as PeerId);
|
|
155
|
+
invariant(entry);
|
|
156
|
+
this.emit('peer-disconnected', { peerId: connection.peerId as PeerId });
|
|
157
|
+
this._emitPeerCandidate(connection);
|
|
152
158
|
}
|
|
153
159
|
|
|
154
160
|
private _onConnectionClosed(connection: ReplicatorConnection) {
|
|
@@ -164,6 +170,16 @@ export class EchoNetworkAdapter extends NetworkAdapter {
|
|
|
164
170
|
|
|
165
171
|
this._connections.delete(connection.peerId as PeerId);
|
|
166
172
|
}
|
|
173
|
+
|
|
174
|
+
private _emitPeerCandidate(connection: ReplicatorConnection) {
|
|
175
|
+
this.emit('peer-candidate', {
|
|
176
|
+
peerId: connection.peerId as PeerId,
|
|
177
|
+
peerMetadata: {
|
|
178
|
+
// TODO(dmaretskyi): Refactor this.
|
|
179
|
+
dxos_peerSource: 'EchoNetworkAdapter',
|
|
180
|
+
} as any,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
167
183
|
}
|
|
168
184
|
|
|
169
185
|
type ConnectionEntry = {
|
|
@@ -27,6 +27,8 @@ export interface EchoReplicatorContext {
|
|
|
27
27
|
|
|
28
28
|
onConnectionClosed(connection: ReplicatorConnection): void;
|
|
29
29
|
|
|
30
|
+
onConnectionAuthScopeChanged(connection: ReplicatorConnection): void;
|
|
31
|
+
|
|
30
32
|
getContainingSpaceForDocument(documentId: string): Promise<PublicKey | null>;
|
|
31
33
|
}
|
|
32
34
|
|
|
@@ -59,7 +59,9 @@ export class MeshEchoReplicator implements EchoReplicator {
|
|
|
59
59
|
log('onRemoteConnected', { peerId: connection.peerId });
|
|
60
60
|
invariant(this._context);
|
|
61
61
|
|
|
62
|
-
if (
|
|
62
|
+
if (this._connectionsPerPeer.has(connection.peerId)) {
|
|
63
|
+
this._context.onConnectionAuthScopeChanged(connection);
|
|
64
|
+
} else {
|
|
63
65
|
this._connectionsPerPeer.set(connection.peerId, connection);
|
|
64
66
|
await connection.enable();
|
|
65
67
|
this._context.onConnectionOpen(connection);
|
|
@@ -12,7 +12,7 @@ import { AutomergeStorageAdapter } from './automerge-storage-adapter';
|
|
|
12
12
|
import { encodingOptions } from './leveldb-storage-adapter';
|
|
13
13
|
|
|
14
14
|
export const levelMigration = async ({ db, directory }: { db: SublevelDB; directory: Directory }) => {
|
|
15
|
-
// Note: Make
|
|
15
|
+
// Note: Make auto-migration from previous storage to leveldb here.
|
|
16
16
|
const isNewLevel = !(await db
|
|
17
17
|
.iterator<StorageKey, Uint8Array>({
|
|
18
18
|
...encodingOptions,
|
|
@@ -32,6 +32,7 @@ export const levelMigration = async ({ db, directory }: { db: SublevelDB; direct
|
|
|
32
32
|
if (chunks.length === 0) {
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
|
+
|
|
35
36
|
const batch = db.batch();
|
|
36
37
|
log.info('found chunks on old storage adapter', { chunks: chunks.length });
|
|
37
38
|
for (const { key, data } of await oldStorageAdapter.loadRange([])) {
|