@dxos/echo-pipeline 0.5.2 → 0.5.3-main.088a2c8

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.
Files changed (48) hide show
  1. package/dist/lib/browser/{chunk-VQQD32DM.mjs → chunk-VUJXFVSK.mjs} +49 -30
  2. package/dist/lib/browser/{chunk-VQQD32DM.mjs.map → chunk-VUJXFVSK.mjs.map} +3 -3
  3. package/dist/lib/browser/index.mjs +435 -288
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +3 -1
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/{chunk-P7L7ICAH.cjs → chunk-6733E3WM.cjs} +50 -32
  9. package/dist/lib/node/{chunk-P7L7ICAH.cjs.map → chunk-6733E3WM.cjs.map} +3 -3
  10. package/dist/lib/node/index.cjs +446 -299
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +13 -11
  14. package/dist/lib/node/testing/index.cjs.map +3 -3
  15. package/dist/types/src/automerge/automerge-host.d.ts +4 -11
  16. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
  17. package/dist/types/src/automerge/echo-network-adapter.d.ts +6 -0
  18. package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
  19. package/dist/types/src/automerge/echo-replicator.d.ts +2 -0
  20. package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
  21. package/dist/types/src/automerge/index.d.ts +1 -1
  22. package/dist/types/src/automerge/leveldb-storage-adapter.d.ts +2 -2
  23. package/dist/types/src/automerge/mesh-echo-replicator.d.ts +23 -0
  24. package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -0
  25. package/dist/types/src/automerge/migrations.d.ts +2 -2
  26. package/dist/types/src/space/space-manager.d.ts +3 -2
  27. package/dist/types/src/space/space-manager.d.ts.map +1 -1
  28. package/dist/types/src/space/space-protocol.d.ts +2 -0
  29. package/dist/types/src/space/space-protocol.d.ts.map +1 -1
  30. package/dist/types/src/space/space.d.ts +4 -3
  31. package/dist/types/src/space/space.d.ts.map +1 -1
  32. package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
  33. package/package.json +33 -33
  34. package/src/automerge/automerge-host.test.ts +22 -9
  35. package/src/automerge/automerge-host.ts +62 -88
  36. package/src/automerge/echo-network-adapter.ts +19 -0
  37. package/src/automerge/echo-replicator.ts +3 -0
  38. package/src/automerge/index.ts +1 -1
  39. package/src/automerge/leveldb-storage-adapter.ts +2 -2
  40. package/src/automerge/mesh-echo-replicator.ts +232 -0
  41. package/src/automerge/migrations.ts +2 -2
  42. package/src/space/space-manager.ts +4 -1
  43. package/src/space/space-protocol.ts +11 -8
  44. package/src/space/space.ts +8 -3
  45. package/src/testing/test-agent-builder.ts +1 -0
  46. package/dist/types/src/automerge/mesh-network-adapter.d.ts +0 -18
  47. package/dist/types/src/automerge/mesh-network-adapter.d.ts.map +0 -1
  48. package/src/automerge/mesh-network-adapter.ts +0 -107
@@ -18,38 +18,38 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var node_exports = {};
20
20
  __export(node_exports, {
21
- AuthExtension: () => import_chunk_P7L7ICAH.AuthExtension,
22
- AuthStatus: () => import_chunk_P7L7ICAH.AuthStatus,
21
+ AuthExtension: () => import_chunk_6733E3WM.AuthExtension,
22
+ AuthStatus: () => import_chunk_6733E3WM.AuthStatus,
23
23
  AutomergeDocumentLoaderImpl: () => AutomergeDocumentLoaderImpl,
24
24
  AutomergeHost: () => AutomergeHost,
25
25
  AutomergeStorageAdapter: () => AutomergeStorageAdapter,
26
- DataServiceImpl: () => import_chunk_P7L7ICAH.DataServiceImpl,
26
+ DataServiceImpl: () => import_chunk_6733E3WM.DataServiceImpl,
27
27
  LevelDBStorageAdapter: () => LevelDBStorageAdapter,
28
28
  LocalHostNetworkAdapter: () => LocalHostNetworkAdapter,
29
- MOCK_AUTH_PROVIDER: () => import_chunk_P7L7ICAH.MOCK_AUTH_PROVIDER,
30
- MOCK_AUTH_VERIFIER: () => import_chunk_P7L7ICAH.MOCK_AUTH_VERIFIER,
31
- MeshNetworkAdapter: () => MeshNetworkAdapter,
32
- MetadataStore: () => import_chunk_P7L7ICAH.MetadataStore,
33
- Pipeline: () => import_chunk_P7L7ICAH.Pipeline,
34
- SnapshotManager: () => import_chunk_P7L7ICAH.SnapshotManager,
35
- SnapshotStore: () => import_chunk_P7L7ICAH.SnapshotStore,
36
- Space: () => import_chunk_P7L7ICAH.Space,
37
- SpaceManager: () => import_chunk_P7L7ICAH.SpaceManager,
38
- SpaceProtocol: () => import_chunk_P7L7ICAH.SpaceProtocol,
39
- SpaceProtocolSession: () => import_chunk_P7L7ICAH.SpaceProtocolSession,
40
- TimeframeClock: () => import_chunk_P7L7ICAH.TimeframeClock,
41
- codec: () => import_chunk_P7L7ICAH.codec,
42
- createMappedFeedWriter: () => import_chunk_P7L7ICAH.createMappedFeedWriter,
29
+ MOCK_AUTH_PROVIDER: () => import_chunk_6733E3WM.MOCK_AUTH_PROVIDER,
30
+ MOCK_AUTH_VERIFIER: () => import_chunk_6733E3WM.MOCK_AUTH_VERIFIER,
31
+ MeshEchoReplicator: () => MeshEchoReplicator,
32
+ MetadataStore: () => import_chunk_6733E3WM.MetadataStore,
33
+ Pipeline: () => import_chunk_6733E3WM.Pipeline,
34
+ SnapshotManager: () => import_chunk_6733E3WM.SnapshotManager,
35
+ SnapshotStore: () => import_chunk_6733E3WM.SnapshotStore,
36
+ Space: () => import_chunk_6733E3WM.Space,
37
+ SpaceManager: () => import_chunk_6733E3WM.SpaceManager,
38
+ SpaceProtocol: () => import_chunk_6733E3WM.SpaceProtocol,
39
+ SpaceProtocolSession: () => import_chunk_6733E3WM.SpaceProtocolSession,
40
+ TimeframeClock: () => import_chunk_6733E3WM.TimeframeClock,
41
+ codec: () => import_chunk_6733E3WM.codec,
42
+ createMappedFeedWriter: () => import_chunk_6733E3WM.createMappedFeedWriter,
43
43
  encodingOptions: () => encodingOptions,
44
44
  getSpaceKeyFromDoc: () => getSpaceKeyFromDoc,
45
- hasInvitationExpired: () => import_chunk_P7L7ICAH.hasInvitationExpired,
46
- mapFeedIndexesToTimeframe: () => import_chunk_P7L7ICAH.mapFeedIndexesToTimeframe,
47
- mapTimeframeToFeedIndexes: () => import_chunk_P7L7ICAH.mapTimeframeToFeedIndexes,
48
- startAfter: () => import_chunk_P7L7ICAH.startAfter,
49
- valueEncoding: () => import_chunk_P7L7ICAH.valueEncoding
45
+ hasInvitationExpired: () => import_chunk_6733E3WM.hasInvitationExpired,
46
+ mapFeedIndexesToTimeframe: () => import_chunk_6733E3WM.mapFeedIndexesToTimeframe,
47
+ mapTimeframeToFeedIndexes: () => import_chunk_6733E3WM.mapTimeframeToFeedIndexes,
48
+ startAfter: () => import_chunk_6733E3WM.startAfter,
49
+ valueEncoding: () => import_chunk_6733E3WM.valueEncoding
50
50
  });
51
51
  module.exports = __toCommonJS(node_exports);
52
- var import_chunk_P7L7ICAH = require("./chunk-P7L7ICAH.cjs");
52
+ var import_chunk_6733E3WM = require("./chunk-6733E3WM.cjs");
53
53
  var import_async = require("@dxos/async");
54
54
  var import_automerge = require("@dxos/automerge/automerge");
55
55
  var import_automerge_repo = require("@dxos/automerge/automerge-repo");
@@ -70,21 +70,23 @@ var import_async3 = require("@dxos/async");
70
70
  var import_automerge_repo3 = require("@dxos/automerge/automerge-repo");
71
71
  var import_codec_protobuf = require("@dxos/codec-protobuf");
72
72
  var import_invariant3 = require("@dxos/invariant");
73
- var import_async4 = require("@dxos/async");
74
- var import_automerge_repo4 = require("@dxos/automerge/automerge-repo");
75
- var import_invariant4 = require("@dxos/invariant");
76
- var import_log3 = require("@dxos/log");
77
- var import_teleport_extension_automerge_replicator = require("@dxos/teleport-extension-automerge-replicator");
78
73
  var import_automerge_repo_storage_indexeddb = require("@dxos/automerge/automerge-repo-storage-indexeddb");
79
- var import_log4 = require("@dxos/log");
74
+ var import_log3 = require("@dxos/log");
80
75
  var import_random_access_storage = require("@dxos/random-access-storage");
81
76
  var import_util2 = require("@dxos/util");
82
- var import_async5 = require("@dxos/async");
77
+ var import_async4 = require("@dxos/async");
83
78
  var import_context4 = require("@dxos/context");
84
79
  var import_debug = require("@dxos/debug");
80
+ var import_invariant4 = require("@dxos/invariant");
81
+ var import_log4 = require("@dxos/log");
82
+ var import_tracing2 = require("@dxos/tracing");
83
+ var import_automerge_repo4 = require("@dxos/automerge/automerge-repo");
84
+ var import_context5 = require("@dxos/context");
85
85
  var import_invariant5 = require("@dxos/invariant");
86
+ var import_keys2 = require("@dxos/keys");
86
87
  var import_log5 = require("@dxos/log");
87
- var import_tracing2 = require("@dxos/tracing");
88
+ var import_teleport_extension_automerge_replicator = require("@dxos/teleport-extension-automerge-replicator");
89
+ var import_util3 = require("@dxos/util");
88
90
  function _ts_decorate(decorators, target, key, desc) {
89
91
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
90
92
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -97,8 +99,9 @@ function _ts_decorate(decorators, target, key, desc) {
97
99
  }
98
100
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
99
101
  var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
100
- constructor() {
101
- super(...arguments);
102
+ constructor(_params) {
103
+ super();
104
+ this._params = _params;
102
105
  this._replicators = /* @__PURE__ */ new Set();
103
106
  this._connections = /* @__PURE__ */ new Map();
104
107
  this._lifecycleState = import_context2.LifecycleState.CLOSED;
@@ -118,7 +121,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
118
121
  if (connectionEntry.isOpen) {
119
122
  import_log2.log.catch(err, void 0, {
120
123
  F: __dxlog_file,
121
- L: 40,
124
+ L: 49,
122
125
  S: this,
123
126
  C: (f, a) => f(...a)
124
127
  });
@@ -130,7 +133,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
130
133
  async open() {
131
134
  (0, import_invariant2.invariant)(this._lifecycleState === import_context2.LifecycleState.CLOSED, void 0, {
132
135
  F: __dxlog_file,
133
- L: 51,
136
+ L: 60,
134
137
  S: this,
135
138
  A: [
136
139
  "this._lifecycleState === LifecycleState.CLOSED",
@@ -138,6 +141,12 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
138
141
  ]
139
142
  });
140
143
  this._lifecycleState = import_context2.LifecycleState.OPEN;
144
+ (0, import_log2.log)("emit ready", void 0, {
145
+ F: __dxlog_file,
146
+ L: 63,
147
+ S: this,
148
+ C: (f, a) => f(...a)
149
+ });
141
150
  this.emit("ready", {
142
151
  network: this
143
152
  });
@@ -145,7 +154,7 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
145
154
  async close() {
146
155
  (0, import_invariant2.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
147
156
  F: __dxlog_file,
148
- L: 61,
157
+ L: 71,
149
158
  S: this,
150
159
  A: [
151
160
  "this._lifecycleState === LifecycleState.OPEN",
@@ -164,9 +173,18 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
164
173
  });
165
174
  }
166
175
  async addReplicator(replicator) {
176
+ (0, import_invariant2.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
177
+ F: __dxlog_file,
178
+ L: 87,
179
+ S: this,
180
+ A: [
181
+ "this._lifecycleState === LifecycleState.OPEN",
182
+ ""
183
+ ]
184
+ });
167
185
  (0, import_invariant2.invariant)(this.peerId, void 0, {
168
186
  F: __dxlog_file,
169
- L: 77,
187
+ L: 88,
170
188
  S: this,
171
189
  A: [
172
190
  "this.peerId",
@@ -175,30 +193,43 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
175
193
  });
176
194
  (0, import_invariant2.invariant)(!this._replicators.has(replicator), void 0, {
177
195
  F: __dxlog_file,
178
- L: 78,
196
+ L: 89,
179
197
  S: this,
180
198
  A: [
181
199
  "!this._replicators.has(replicator)",
182
200
  ""
183
201
  ]
184
202
  });
203
+ this._replicators.add(replicator);
185
204
  await replicator.connect({
186
205
  peerId: this.peerId,
187
206
  onConnectionOpen: this._onConnectionOpen.bind(this),
188
- onConnectionClosed: this._onConnectionClosed.bind(this)
207
+ onConnectionClosed: this._onConnectionClosed.bind(this),
208
+ getContainingSpaceForDocument: this._params.getContainingSpaceForDocument
189
209
  });
190
210
  }
191
211
  async removeReplicator(replicator) {
212
+ (0, import_invariant2.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
213
+ F: __dxlog_file,
214
+ L: 102,
215
+ S: this,
216
+ A: [
217
+ "this._lifecycleState === LifecycleState.OPEN",
218
+ ""
219
+ ]
220
+ });
192
221
  (0, import_invariant2.invariant)(this._replicators.has(replicator), void 0, {
193
222
  F: __dxlog_file,
194
- L: 89,
223
+ L: 103,
195
224
  S: this,
196
225
  A: [
197
226
  "this._replicators.has(replicator)",
198
227
  ""
199
228
  ]
200
229
  });
230
+ "";
201
231
  await replicator.disconnect();
232
+ this._replicators.delete(replicator);
202
233
  }
203
234
  async shouldAdvertize(peerId, params) {
204
235
  const connection = this._connections.get(peerId);
@@ -208,9 +239,17 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
208
239
  return connection.connection.shouldAdvertize(params);
209
240
  }
210
241
  _onConnectionOpen(connection) {
242
+ (0, import_log2.log)("Connection opened", {
243
+ peerId: connection.peerId
244
+ }, {
245
+ F: __dxlog_file,
246
+ L: 119,
247
+ S: this,
248
+ C: (f, a) => f(...a)
249
+ });
211
250
  (0, import_invariant2.invariant)(!this._connections.has(connection.peerId), void 0, {
212
251
  F: __dxlog_file,
213
- L: 103,
252
+ L: 120,
214
253
  S: this,
215
254
  A: [
216
255
  "!this._connections.has(connection.peerId as PeerId)",
@@ -239,13 +278,21 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
239
278
  if (connectionEntry.isOpen) {
240
279
  import_log2.log.catch(err, void 0, {
241
280
  F: __dxlog_file,
242
- L: 122,
281
+ L: 139,
243
282
  S: this,
244
283
  C: (f, a) => f(...a)
245
284
  });
246
285
  }
247
286
  }
248
287
  });
288
+ (0, import_log2.log)("emit peer-candidate", {
289
+ peerId: connection.peerId
290
+ }, {
291
+ F: __dxlog_file,
292
+ L: 144,
293
+ S: this,
294
+ C: (f, a) => f(...a)
295
+ });
249
296
  this.emit("peer-candidate", {
250
297
  peerId: connection.peerId,
251
298
  peerMetadata: {
@@ -255,10 +302,18 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
255
302
  });
256
303
  }
257
304
  _onConnectionClosed(connection) {
305
+ (0, import_log2.log)("Connection closed", {
306
+ peerId: connection.peerId
307
+ }, {
308
+ F: __dxlog_file,
309
+ L: 155,
310
+ S: this,
311
+ C: (f, a) => f(...a)
312
+ });
258
313
  const entry = this._connections.get(connection.peerId);
259
314
  (0, import_invariant2.invariant)(entry, void 0, {
260
315
  F: __dxlog_file,
261
- L: 138,
316
+ L: 157,
262
317
  S: this,
263
318
  A: [
264
319
  "entry",
@@ -271,13 +326,13 @@ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
271
326
  });
272
327
  void entry.reader.cancel().catch((err) => import_log2.log.catch(err, void 0, {
273
328
  F: __dxlog_file,
274
- L: 143,
329
+ L: 162,
275
330
  S: this,
276
331
  C: (f, a) => f(...a)
277
332
  }));
278
333
  void entry.writer.abort().catch((err) => import_log2.log.catch(err, void 0, {
279
334
  F: __dxlog_file,
280
- L: 144,
335
+ L: 163,
281
336
  S: this,
282
337
  C: (f, a) => f(...a)
283
338
  }));
@@ -521,116 +576,6 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo3.NetworkAdapte
521
576
  return id;
522
577
  }
523
578
  };
524
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-network-adapter.ts";
525
- var MeshNetworkAdapter = class extends import_automerge_repo4.NetworkAdapter {
526
- constructor() {
527
- super(...arguments);
528
- this._extensions = /* @__PURE__ */ new Map();
529
- this._connected = new import_async4.Trigger();
530
- }
531
- /**
532
- * Emits `ready` event. That signals to `Repo` that it can start using the adapter.
533
- */
534
- ready() {
535
- this.emit("ready", {
536
- network: this
537
- });
538
- }
539
- connect(peerId) {
540
- this.peerId = peerId;
541
- this._connected.wake();
542
- }
543
- send(message) {
544
- const receiverId = message.targetId;
545
- const extension = this._extensions.get(receiverId);
546
- (0, import_invariant4.invariant)(extension, "Extension not found.", {
547
- F: __dxlog_file3,
548
- L: 38,
549
- S: this,
550
- A: [
551
- "extension",
552
- "'Extension not found.'"
553
- ]
554
- });
555
- extension.sendSyncMessage({
556
- payload: import_automerge_repo4.cbor.encode(message)
557
- }).catch((err) => import_log3.log.catch(err, void 0, {
558
- F: __dxlog_file3,
559
- L: 39,
560
- S: this,
561
- C: (f, a) => f(...a)
562
- }));
563
- }
564
- disconnect() {
565
- }
566
- createExtension() {
567
- (0, import_invariant4.invariant)(this.peerId, "Peer id not set.", {
568
- F: __dxlog_file3,
569
- L: 47,
570
- S: this,
571
- A: [
572
- "this.peerId",
573
- "'Peer id not set.'"
574
- ]
575
- });
576
- let peerInfo;
577
- const extension = new import_teleport_extension_automerge_replicator.AutomergeReplicator({
578
- peerId: this.peerId
579
- }, {
580
- onStartReplication: async (info, remotePeerId) => {
581
- await this._connected.wait();
582
- (0, import_log3.log)("onStartReplication", {
583
- id: info.id,
584
- thisPeerId: this.peerId,
585
- remotePeerId: remotePeerId.toHex()
586
- }, {
587
- F: __dxlog_file3,
588
- L: 70,
589
- S: this,
590
- C: (f, a) => f(...a)
591
- });
592
- if (!this._extensions.has(info.id)) {
593
- peerInfo = info;
594
- this._extensions.set(info.id, extension);
595
- (0, import_log3.log)("peer-candidate", {
596
- id: info.id,
597
- thisPeerId: this.peerId,
598
- remotePeerId: remotePeerId.toHex()
599
- }, {
600
- F: __dxlog_file3,
601
- L: 76,
602
- S: this,
603
- C: (f, a) => f(...a)
604
- });
605
- this.emit("peer-candidate", {
606
- // TODO(mykola): Hack, stop abusing `peerMetadata` field.
607
- peerMetadata: {
608
- dxos_deviceKey: remotePeerId.toHex()
609
- },
610
- peerId: info.id
611
- });
612
- }
613
- },
614
- onSyncMessage: async ({ payload }) => {
615
- if (!peerInfo) {
616
- return;
617
- }
618
- const message = import_automerge_repo4.cbor.decode(payload);
619
- this.emit("message", message);
620
- },
621
- onClose: async () => {
622
- if (!peerInfo) {
623
- return;
624
- }
625
- this.emit("peer-disconnected", {
626
- peerId: peerInfo.id
627
- });
628
- this._extensions.delete(peerInfo.id);
629
- }
630
- });
631
- return extension;
632
- }
633
- };
634
579
  var AutomergeStorageAdapter = class {
635
580
  constructor(_directory) {
636
581
  this._directory = _directory;
@@ -704,7 +649,7 @@ var AutomergeStorageAdapter = class {
704
649
  return filename.split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%"));
705
650
  }
706
651
  };
707
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/migrations.ts";
652
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/migrations.ts";
708
653
  var levelMigration = async ({ db, directory }) => {
709
654
  const isNewLevel = !await db.iterator({
710
655
  ...encodingOptions
@@ -718,10 +663,10 @@ var levelMigration = async ({ db, directory }) => {
718
663
  return;
719
664
  }
720
665
  const batch = db.batch();
721
- import_log4.log.info("found chunks on old storage adapter", {
666
+ import_log3.log.info("found chunks on old storage adapter", {
722
667
  chunks: chunks.length
723
668
  }, {
724
- F: __dxlog_file4,
669
+ F: __dxlog_file3,
725
670
  L: 36,
726
671
  S: void 0,
727
672
  C: (f, a) => f(...a)
@@ -743,12 +688,13 @@ function _ts_decorate2(decorators, target, key, desc) {
743
688
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
744
689
  return c > 3 && r && Object.defineProperty(target, key, r), r;
745
690
  }
746
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
691
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
747
692
  var AutomergeHost = class {
748
693
  constructor({ directory, db, indexMetadataStore }) {
749
694
  this._ctx = new import_context.Context();
750
- this._echoNetworkAdapter = new EchoNetworkAdapter();
751
- this._authorizedDevices = new import_util.ComplexMap(import_keys.PublicKey.hash);
695
+ this._echoNetworkAdapter = new EchoNetworkAdapter({
696
+ getContainingSpaceForDocument: this._getContainingSpaceForDocument.bind(this)
697
+ });
752
698
  this._requestedDocs = /* @__PURE__ */ new Set();
753
699
  this._directory = directory;
754
700
  this._db = db;
@@ -768,103 +714,17 @@ var AutomergeHost = class {
768
714
  });
769
715
  await this._storage.open?.();
770
716
  this._peerId = `host-${import_keys.PublicKey.random().toHex()}`;
771
- this._meshNetwork = new MeshNetworkAdapter();
772
717
  this._clientNetwork = new LocalHostNetworkAdapter();
773
718
  this._repo = new import_automerge_repo.Repo({
774
719
  peerId: this._peerId,
775
720
  network: [
776
721
  this._clientNetwork,
777
- this._meshNetwork,
778
722
  this._echoNetworkAdapter
779
723
  ],
780
724
  storage: this._storage,
781
- // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
782
- // Hosts, running in the worker, don't share documents unless requested by other peers.
783
- sharePolicy: async (peerId, documentId) => {
784
- if (peerId.startsWith("client-")) {
785
- return false;
786
- }
787
- if (!documentId) {
788
- return false;
789
- }
790
- const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
791
- if (peerMetadata?.dxos_peerSource === "EchoNetworkAdapter") {
792
- return this._echoNetworkAdapter.shouldAdvertize(peerId, {
793
- documentId
794
- });
795
- }
796
- const doc = this._repo.handles[documentId]?.docSync();
797
- if (!doc) {
798
- const isRequested = this._requestedDocs.has(`automerge:${documentId}`);
799
- (0, import_log.log)("doc share policy check", {
800
- peerId,
801
- documentId,
802
- isRequested
803
- }, {
804
- F: __dxlog_file5,
805
- L: 124,
806
- S: this,
807
- C: (f, a) => f(...a)
808
- });
809
- return isRequested;
810
- }
811
- try {
812
- const spaceKey = getSpaceKeyFromDoc(doc);
813
- if (!spaceKey) {
814
- (0, import_log.log)("space key not found for share policy check", {
815
- peerId,
816
- documentId
817
- }, {
818
- F: __dxlog_file5,
819
- L: 131,
820
- S: this,
821
- C: (f, a) => f(...a)
822
- });
823
- return false;
824
- }
825
- const authorizedDevices = this._authorizedDevices.get(import_keys.PublicKey.from(spaceKey));
826
- const deviceKeyHex = peerMetadata?.dxos_deviceKey;
827
- if (!deviceKeyHex) {
828
- (0, import_log.log)("device key not found for share policy check", {
829
- peerId,
830
- documentId
831
- }, {
832
- F: __dxlog_file5,
833
- L: 140,
834
- S: this,
835
- C: (f, a) => f(...a)
836
- });
837
- return false;
838
- }
839
- const deviceKey = import_keys.PublicKey.from(deviceKeyHex);
840
- const isAuthorized = authorizedDevices?.has(deviceKey) ?? false;
841
- (0, import_log.log)("share policy check", {
842
- localPeer: this._peerId,
843
- remotePeer: peerId,
844
- documentId,
845
- deviceKey,
846
- spaceKey,
847
- isAuthorized
848
- }, {
849
- F: __dxlog_file5,
850
- L: 146,
851
- S: this,
852
- C: (f, a) => f(...a)
853
- });
854
- return isAuthorized;
855
- } catch (err) {
856
- import_log.log.catch(err, void 0, {
857
- F: __dxlog_file5,
858
- L: 156,
859
- S: this,
860
- C: (f, a) => f(...a)
861
- });
862
- return false;
863
- }
864
- }
725
+ sharePolicy: this._sharePolicy.bind(this)
865
726
  });
866
727
  this._clientNetwork.ready();
867
- this._meshNetwork.ready();
868
728
  await this._echoNetworkAdapter.open();
869
729
  await this._clientNetwork.whenConnected();
870
730
  await this._echoNetworkAdapter.whenConnected();
@@ -884,6 +744,40 @@ var AutomergeHost = class {
884
744
  async removeReplicator(replicator) {
885
745
  await this._echoNetworkAdapter.removeReplicator(replicator);
886
746
  }
747
+ // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
748
+ // Hosts, running in the worker, don't share documents unless requested by other peers.
749
+ // NOTE: If both peers return sharePolicy=false the replication will not happen
750
+ // https://github.com/automerge/automerge-repo/pull/292
751
+ async _sharePolicy(peerId, documentId) {
752
+ if (peerId.startsWith("client-")) {
753
+ return false;
754
+ }
755
+ if (!documentId) {
756
+ return false;
757
+ }
758
+ const doc = this._repo.handles[documentId]?.docSync();
759
+ if (!doc) {
760
+ const isRequested = this._requestedDocs.has(`automerge:${documentId}`);
761
+ (0, import_log.log)("doc share policy check", {
762
+ peerId,
763
+ documentId,
764
+ isRequested
765
+ }, {
766
+ F: __dxlog_file4,
767
+ L: 149,
768
+ S: this,
769
+ C: (f, a) => f(...a)
770
+ });
771
+ return isRequested;
772
+ }
773
+ const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
774
+ if (peerMetadata?.dxos_peerSource === "EchoNetworkAdapter") {
775
+ return this._echoNetworkAdapter.shouldAdvertize(peerId, {
776
+ documentId
777
+ });
778
+ }
779
+ return false;
780
+ }
887
781
  async _beforeSave({ path, batch }) {
888
782
  const handle = this._repo.handles[path[0]];
889
783
  if (!handle) {
@@ -936,14 +830,25 @@ var AutomergeHost = class {
936
830
  _automergePeers() {
937
831
  return this._repo.peers;
938
832
  }
833
+ async _getContainingSpaceForDocument(documentId) {
834
+ const doc = this._repo.handles[documentId]?.docSync();
835
+ if (!doc) {
836
+ return null;
837
+ }
838
+ const spaceKeyHex = getSpaceKeyFromDoc(doc);
839
+ if (!spaceKeyHex) {
840
+ return null;
841
+ }
842
+ return import_keys.PublicKey.from(spaceKeyHex);
843
+ }
939
844
  //
940
845
  // Methods for client-services.
941
846
  //
942
847
  async flush({ states }) {
943
848
  await Promise.all(states?.map(async ({ heads, documentId }) => {
944
849
  (0, import_invariant.invariant)(heads, "heads are required for flush", {
945
- F: __dxlog_file5,
946
- L: 252,
850
+ F: __dxlog_file4,
851
+ L: 239,
947
852
  S: this,
948
853
  A: [
949
854
  "heads",
@@ -964,24 +869,6 @@ var AutomergeHost = class {
964
869
  async getHostInfo() {
965
870
  return this._clientNetwork.getHostInfo();
966
871
  }
967
- //
968
- // Mesh replication.
969
- //
970
- createExtension() {
971
- return this._meshNetwork.createExtension();
972
- }
973
- authorizeDevice(spaceKey, deviceKey) {
974
- (0, import_log.log)("authorizeDevice", {
975
- spaceKey,
976
- deviceKey
977
- }, {
978
- F: __dxlog_file5,
979
- L: 282,
980
- S: this,
981
- C: (f, a) => f(...a)
982
- });
983
- (0, import_util.defaultMap)(this._authorizedDevices, spaceKey, () => new import_util.ComplexSet(import_keys.PublicKey.hash)).add(deviceKey);
984
- }
985
872
  };
986
873
  _ts_decorate2([
987
874
  import_tracing.trace.info()
@@ -1039,7 +926,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1039
926
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1040
927
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1041
928
  }
1042
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
929
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
1043
930
  var AutomergeDocumentLoaderImpl = class {
1044
931
  constructor(_spaceKey, _repo) {
1045
932
  this._spaceKey = _spaceKey;
@@ -1047,7 +934,7 @@ var AutomergeDocumentLoaderImpl = class {
1047
934
  this._spaceRootDocHandle = null;
1048
935
  this._objectDocumentHandles = /* @__PURE__ */ new Map();
1049
936
  this._objectsPendingDocumentLoad = /* @__PURE__ */ new Set();
1050
- this.onObjectDocumentLoaded = new import_async5.Event();
937
+ this.onObjectDocumentLoaded = new import_async4.Event();
1051
938
  }
1052
939
  getAllHandles() {
1053
940
  return this._spaceRootDocHandle != null ? [
@@ -1060,10 +947,10 @@ var AutomergeDocumentLoaderImpl = class {
1060
947
  return;
1061
948
  }
1062
949
  if (!spaceState.rootUrl) {
1063
- import_log5.log.error("Database opened with no rootUrl", {
950
+ import_log4.log.error("Database opened with no rootUrl", {
1064
951
  spaceKey: this._spaceKey
1065
952
  }, {
1066
- F: __dxlog_file6,
953
+ F: __dxlog_file5,
1067
954
  L: 70,
1068
955
  S: this,
1069
956
  C: (f, a) => f(...a)
@@ -1072,8 +959,8 @@ var AutomergeDocumentLoaderImpl = class {
1072
959
  } else {
1073
960
  const existingDocHandle = await this._initDocHandle(ctx, spaceState.rootUrl);
1074
961
  const doc = existingDocHandle.docSync();
1075
- (0, import_invariant5.invariant)(doc, void 0, {
1076
- F: __dxlog_file6,
962
+ (0, import_invariant4.invariant)(doc, void 0, {
963
+ F: __dxlog_file5,
1077
964
  L: 75,
1078
965
  S: this,
1079
966
  A: [
@@ -1094,8 +981,8 @@ var AutomergeDocumentLoaderImpl = class {
1094
981
  let hasUrlsToLoad = false;
1095
982
  const urlsToLoad = {};
1096
983
  for (const objectId of objectIds) {
1097
- (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1098
- F: __dxlog_file6,
984
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
985
+ F: __dxlog_file5,
1099
986
  L: 88,
1100
987
  S: this,
1101
988
  A: [
@@ -1107,8 +994,8 @@ var AutomergeDocumentLoaderImpl = class {
1107
994
  continue;
1108
995
  }
1109
996
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
1110
- (0, import_invariant5.invariant)(spaceRootDoc, void 0, {
1111
- F: __dxlog_file6,
997
+ (0, import_invariant4.invariant)(spaceRootDoc, void 0, {
998
+ F: __dxlog_file5,
1112
999
  L: 93,
1113
1000
  S: this,
1114
1001
  A: [
@@ -1119,10 +1006,10 @@ var AutomergeDocumentLoaderImpl = class {
1119
1006
  const documentUrl = (spaceRootDoc.links ?? {})[objectId];
1120
1007
  if (documentUrl == null) {
1121
1008
  this._objectsPendingDocumentLoad.add(objectId);
1122
- import_log5.log.info("loading delayed until object links are initialized", {
1009
+ import_log4.log.info("loading delayed until object links are initialized", {
1123
1010
  objectId
1124
1011
  }, {
1125
- F: __dxlog_file6,
1012
+ F: __dxlog_file5,
1126
1013
  L: 97,
1127
1014
  S: this,
1128
1015
  C: (f, a) => f(...a)
@@ -1145,8 +1032,8 @@ var AutomergeDocumentLoaderImpl = class {
1145
1032
  linksAwaitingLoad.forEach(([objectId]) => this._objectsPendingDocumentLoad.delete(objectId));
1146
1033
  }
1147
1034
  getSpaceRootDocHandle() {
1148
- (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1149
- F: __dxlog_file6,
1035
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
1036
+ F: __dxlog_file5,
1150
1037
  L: 120,
1151
1038
  S: this,
1152
1039
  A: [
@@ -1157,8 +1044,8 @@ var AutomergeDocumentLoaderImpl = class {
1157
1044
  return this._spaceRootDocHandle;
1158
1045
  }
1159
1046
  createDocumentForObject(objectId) {
1160
- (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1161
- F: __dxlog_file6,
1047
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
1048
+ F: __dxlog_file5,
1162
1049
  L: 125,
1163
1050
  S: this,
1164
1051
  A: [
@@ -1197,11 +1084,11 @@ var AutomergeDocumentLoaderImpl = class {
1197
1084
  };
1198
1085
  const objectDocumentHandle = this._objectDocumentHandles.get(objectId);
1199
1086
  if (objectDocumentHandle != null && objectDocumentHandle.url !== automergeUrl) {
1200
- import_log5.log.warn("object already inlined in a different document, ignoring the link", {
1087
+ import_log4.log.warn("object already inlined in a different document, ignoring the link", {
1201
1088
  ...logMeta,
1202
1089
  actualDocumentUrl: objectDocumentHandle.url
1203
1090
  }, {
1204
- F: __dxlog_file6,
1091
+ F: __dxlog_file5,
1205
1092
  L: 155,
1206
1093
  S: this,
1207
1094
  C: (f, a) => f(...a)
@@ -1209,8 +1096,8 @@ var AutomergeDocumentLoaderImpl = class {
1209
1096
  continue;
1210
1097
  }
1211
1098
  if (objectDocumentHandle?.url === automergeUrl) {
1212
- import_log5.log.warn("object document was already loaded", logMeta, {
1213
- F: __dxlog_file6,
1099
+ import_log4.log.warn("object document was already loaded", logMeta, {
1100
+ F: __dxlog_file5,
1214
1101
  L: 162,
1215
1102
  S: this,
1216
1103
  C: (f, a) => f(...a)
@@ -1218,8 +1105,8 @@ var AutomergeDocumentLoaderImpl = class {
1218
1105
  continue;
1219
1106
  }
1220
1107
  const handle = this._repo.find(automergeUrl);
1221
- import_log5.log.debug("document loading triggered", logMeta, {
1222
- F: __dxlog_file6,
1108
+ import_log4.log.debug("document loading triggered", logMeta, {
1109
+ F: __dxlog_file5,
1223
1110
  L: 166,
1224
1111
  S: this,
1225
1112
  C: (f, a) => f(...a)
@@ -1238,11 +1125,11 @@ var AutomergeDocumentLoaderImpl = class {
1238
1125
  break;
1239
1126
  } catch (err) {
1240
1127
  if (`${err}`.includes("Timeout")) {
1241
- import_log5.log.info("wraparound", {
1128
+ import_log4.log.info("wraparound", {
1242
1129
  id: docHandle.documentId,
1243
1130
  state: docHandle.state
1244
1131
  }, {
1245
- F: __dxlog_file6,
1132
+ F: __dxlog_file5,
1246
1133
  L: 182,
1247
1134
  S: this,
1248
1135
  C: (f, a) => f(...a)
@@ -1283,8 +1170,8 @@ var AutomergeDocumentLoaderImpl = class {
1283
1170
  docUrl: handle.url
1284
1171
  };
1285
1172
  if (this.onObjectDocumentLoaded.listenerCount() === 0) {
1286
- import_log5.log.info("document loaded after all listeners were removed", logMeta, {
1287
- F: __dxlog_file6,
1173
+ import_log4.log.info("document loaded after all listeners were removed", logMeta, {
1174
+ F: __dxlog_file5,
1288
1175
  L: 218,
1289
1176
  S: this,
1290
1177
  C: (f, a) => f(...a)
@@ -1293,8 +1180,8 @@ var AutomergeDocumentLoaderImpl = class {
1293
1180
  }
1294
1181
  const objectDocHandle = this._objectDocumentHandles.get(objectId);
1295
1182
  if (objectDocHandle?.url !== handle.url) {
1296
- import_log5.log.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1297
- F: __dxlog_file6,
1183
+ import_log4.log.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1184
+ F: __dxlog_file5,
1298
1185
  L: 223,
1299
1186
  S: this,
1300
1187
  C: (f, a) => f(...a)
@@ -1307,13 +1194,13 @@ var AutomergeDocumentLoaderImpl = class {
1307
1194
  });
1308
1195
  } catch (err) {
1309
1196
  const shouldRetryLoading = this.onObjectDocumentLoaded.listenerCount() > 0;
1310
- import_log5.log.warn("failed to load a document", {
1197
+ import_log4.log.warn("failed to load a document", {
1311
1198
  objectId,
1312
1199
  automergeUrl: handle.url,
1313
1200
  retryLoading: shouldRetryLoading,
1314
1201
  err
1315
1202
  }, {
1316
- F: __dxlog_file6,
1203
+ F: __dxlog_file5,
1317
1204
  L: 229,
1318
1205
  S: this,
1319
1206
  C: (f, a) => f(...a)
@@ -1332,6 +1219,266 @@ _ts_decorate3([
1332
1219
  AutomergeDocumentLoaderImpl = _ts_decorate3([
1333
1220
  import_tracing2.trace.resource()
1334
1221
  ], AutomergeDocumentLoaderImpl);
1222
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1223
+ var MeshEchoReplicator = class {
1224
+ constructor() {
1225
+ this._connections = /* @__PURE__ */ new Set();
1226
+ this._connectionsPerPeer = /* @__PURE__ */ new Map();
1227
+ this._authorizedDevices = new import_util3.ComplexMap(import_keys2.PublicKey.hash);
1228
+ this._context = null;
1229
+ }
1230
+ async connect(context) {
1231
+ this._context = context;
1232
+ }
1233
+ async disconnect() {
1234
+ for (const connection of this._connections) {
1235
+ await connection.close();
1236
+ }
1237
+ this._connections.clear();
1238
+ this._connectionsPerPeer.clear();
1239
+ this._context = null;
1240
+ }
1241
+ createExtension() {
1242
+ (0, import_invariant5.invariant)(this._context, void 0, {
1243
+ F: __dxlog_file6,
1244
+ L: 54,
1245
+ S: this,
1246
+ A: [
1247
+ "this._context",
1248
+ ""
1249
+ ]
1250
+ });
1251
+ const connection = new MeshReplicatorConnection({
1252
+ ownPeerId: this._context.peerId,
1253
+ onRemoteConnected: async () => {
1254
+ (0, import_log5.log)("onRemoteConnected", {
1255
+ peerId: connection.peerId
1256
+ }, {
1257
+ F: __dxlog_file6,
1258
+ L: 59,
1259
+ S: this,
1260
+ C: (f, a) => f(...a)
1261
+ });
1262
+ (0, import_invariant5.invariant)(this._context, void 0, {
1263
+ F: __dxlog_file6,
1264
+ L: 60,
1265
+ S: this,
1266
+ A: [
1267
+ "this._context",
1268
+ ""
1269
+ ]
1270
+ });
1271
+ if (!this._connectionsPerPeer.has(connection.peerId)) {
1272
+ this._connectionsPerPeer.set(connection.peerId, connection);
1273
+ await connection.enable();
1274
+ this._context.onConnectionOpen(connection);
1275
+ }
1276
+ },
1277
+ onRemoteDisconnected: async () => {
1278
+ (0, import_log5.log)("onRemoteDisconnected", {
1279
+ peerId: connection.peerId
1280
+ }, {
1281
+ F: __dxlog_file6,
1282
+ L: 69,
1283
+ S: this,
1284
+ C: (f, a) => f(...a)
1285
+ });
1286
+ (0, import_invariant5.invariant)(this._context, void 0, {
1287
+ F: __dxlog_file6,
1288
+ L: 70,
1289
+ S: this,
1290
+ A: [
1291
+ "this._context",
1292
+ ""
1293
+ ]
1294
+ });
1295
+ this._context.onConnectionClosed(connection);
1296
+ await connection.disable();
1297
+ this._connectionsPerPeer.delete(connection.peerId);
1298
+ this._connections.delete(connection);
1299
+ },
1300
+ shouldAdvertize: async (params) => {
1301
+ (0, import_log5.log)("shouldAdvertize", {
1302
+ peerId: connection.peerId,
1303
+ documentId: params.documentId
1304
+ }, {
1305
+ F: __dxlog_file6,
1306
+ L: 77,
1307
+ S: this,
1308
+ C: (f, a) => f(...a)
1309
+ });
1310
+ (0, import_invariant5.invariant)(this._context, void 0, {
1311
+ F: __dxlog_file6,
1312
+ L: 78,
1313
+ S: this,
1314
+ A: [
1315
+ "this._context",
1316
+ ""
1317
+ ]
1318
+ });
1319
+ try {
1320
+ const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
1321
+ if (!spaceKey) {
1322
+ (0, import_log5.log)("space key not found for share policy check", {
1323
+ peerId: connection.peerId,
1324
+ documentId: params.documentId
1325
+ }, {
1326
+ F: __dxlog_file6,
1327
+ L: 82,
1328
+ S: this,
1329
+ C: (f, a) => f(...a)
1330
+ });
1331
+ return false;
1332
+ }
1333
+ const authorizedDevices = this._authorizedDevices.get(spaceKey);
1334
+ if (!connection.remoteDeviceKey) {
1335
+ (0, import_log5.log)("device key not found for share policy check", {
1336
+ peerId: connection.peerId,
1337
+ documentId: params.documentId
1338
+ }, {
1339
+ F: __dxlog_file6,
1340
+ L: 92,
1341
+ S: this,
1342
+ C: (f, a) => f(...a)
1343
+ });
1344
+ return false;
1345
+ }
1346
+ const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1347
+ (0, import_log5.log)("share policy check", {
1348
+ localPeer: this._context.peerId,
1349
+ remotePeer: connection.peerId,
1350
+ documentId: params.documentId,
1351
+ deviceKey: connection.remoteDeviceKey,
1352
+ spaceKey,
1353
+ isAuthorized
1354
+ }, {
1355
+ F: __dxlog_file6,
1356
+ L: 100,
1357
+ S: this,
1358
+ C: (f, a) => f(...a)
1359
+ });
1360
+ return isAuthorized;
1361
+ } catch (err) {
1362
+ import_log5.log.catch(err, void 0, {
1363
+ F: __dxlog_file6,
1364
+ L: 110,
1365
+ S: this,
1366
+ C: (f, a) => f(...a)
1367
+ });
1368
+ return false;
1369
+ }
1370
+ }
1371
+ });
1372
+ this._connections.add(connection);
1373
+ return connection.replicatorExtension;
1374
+ }
1375
+ authorizeDevice(spaceKey, deviceKey) {
1376
+ (0, import_log5.log)("authorizeDevice", {
1377
+ spaceKey,
1378
+ deviceKey
1379
+ }, {
1380
+ F: __dxlog_file6,
1381
+ L: 121,
1382
+ S: this,
1383
+ C: (f, a) => f(...a)
1384
+ });
1385
+ (0, import_util3.defaultMap)(this._authorizedDevices, spaceKey, () => new import_util3.ComplexSet(import_keys2.PublicKey.hash)).add(deviceKey);
1386
+ }
1387
+ };
1388
+ var MeshReplicatorConnection = class extends import_context5.Resource {
1389
+ constructor(_params) {
1390
+ super();
1391
+ this._params = _params;
1392
+ this.remoteDeviceKey = null;
1393
+ this._remotePeerId = null;
1394
+ this._isEnabled = false;
1395
+ let readableStreamController;
1396
+ this.readable = new ReadableStream({
1397
+ start: (controller) => {
1398
+ readableStreamController = controller;
1399
+ this._ctx.onDispose(() => controller.close());
1400
+ }
1401
+ });
1402
+ this.writable = new WritableStream({
1403
+ write: async (message, controller) => {
1404
+ this.replicatorExtension.sendSyncMessage({
1405
+ payload: import_automerge_repo4.cbor.encode(message)
1406
+ }).catch((err) => {
1407
+ controller.error(err);
1408
+ });
1409
+ }
1410
+ });
1411
+ this.replicatorExtension = new import_teleport_extension_automerge_replicator.AutomergeReplicator({
1412
+ peerId: this._params.ownPeerId
1413
+ }, {
1414
+ onStartReplication: async (info, remotePeerId) => {
1415
+ this.remoteDeviceKey = remotePeerId;
1416
+ this._remotePeerId = info.id;
1417
+ (0, import_log5.log)("onStartReplication", {
1418
+ id: info.id,
1419
+ thisPeerId: this.peerId,
1420
+ remotePeerId: remotePeerId.toHex()
1421
+ }, {
1422
+ F: __dxlog_file6,
1423
+ L: 186,
1424
+ S: this,
1425
+ C: (f, a) => f(...a)
1426
+ });
1427
+ await this._params.onRemoteConnected();
1428
+ },
1429
+ onSyncMessage: async ({ payload }) => {
1430
+ if (!this._isEnabled) {
1431
+ return;
1432
+ }
1433
+ const message = import_automerge_repo4.cbor.decode(payload);
1434
+ readableStreamController.enqueue(message);
1435
+ },
1436
+ onClose: async () => {
1437
+ if (!this._isEnabled) {
1438
+ return;
1439
+ }
1440
+ await this._params.onRemoteDisconnected();
1441
+ }
1442
+ });
1443
+ }
1444
+ get peerId() {
1445
+ (0, import_invariant5.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1446
+ F: __dxlog_file6,
1447
+ L: 209,
1448
+ S: this,
1449
+ A: [
1450
+ "this._remotePeerId != null",
1451
+ "'Remote peer has not connected yet.'"
1452
+ ]
1453
+ });
1454
+ return this._remotePeerId;
1455
+ }
1456
+ async shouldAdvertize(params) {
1457
+ return this._params.shouldAdvertize(params);
1458
+ }
1459
+ /**
1460
+ * Start exchanging messages with the remote peer.
1461
+ * Call after the remote peer has connected.
1462
+ */
1463
+ async enable() {
1464
+ (0, import_invariant5.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1465
+ F: __dxlog_file6,
1466
+ L: 222,
1467
+ S: this,
1468
+ A: [
1469
+ "this._remotePeerId != null",
1470
+ "'Remote peer has not connected yet.'"
1471
+ ]
1472
+ });
1473
+ this._isEnabled = true;
1474
+ }
1475
+ /**
1476
+ * Stop exchanging messages with the remote peer.
1477
+ */
1478
+ async disable() {
1479
+ this._isEnabled = false;
1480
+ }
1481
+ };
1335
1482
  // Annotate the CommonJS export names for ESM import in node:
1336
1483
  0 && (module.exports = {
1337
1484
  AuthExtension,
@@ -1344,7 +1491,7 @@ AutomergeDocumentLoaderImpl = _ts_decorate3([
1344
1491
  LocalHostNetworkAdapter,
1345
1492
  MOCK_AUTH_PROVIDER,
1346
1493
  MOCK_AUTH_VERIFIER,
1347
- MeshNetworkAdapter,
1494
+ MeshEchoReplicator,
1348
1495
  MetadataStore,
1349
1496
  Pipeline,
1350
1497
  SnapshotManager,