@dxos/echo-pipeline 0.5.2 → 0.5.3-main.056e7da

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 (62) hide show
  1. package/dist/lib/browser/{chunk-VQQD32DM.mjs → chunk-GANAND63.mjs} +49 -30
  2. package/dist/lib/browser/chunk-GANAND63.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +431 -291
  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-M475BGBI.cjs} +50 -32
  9. package/dist/lib/node/chunk-M475BGBI.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +441 -301
  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/automerge/migrations.d.ts.map +1 -1
  27. package/dist/types/src/common/feeds.d.ts.map +1 -1
  28. package/dist/types/src/db-host/snapshot-manager.d.ts +2 -2
  29. package/dist/types/src/db-host/snapshot-manager.d.ts.map +1 -1
  30. package/dist/types/src/db-host/snapshot-store.d.ts +2 -2
  31. package/dist/types/src/db-host/snapshot-store.d.ts.map +1 -1
  32. package/dist/types/src/metadata/metadata-store.d.ts +1 -1
  33. package/dist/types/src/metadata/metadata-store.d.ts.map +1 -1
  34. package/dist/types/src/space/space-manager.d.ts +3 -2
  35. package/dist/types/src/space/space-manager.d.ts.map +1 -1
  36. package/dist/types/src/space/space-protocol.d.ts +2 -0
  37. package/dist/types/src/space/space-protocol.d.ts.map +1 -1
  38. package/dist/types/src/space/space.d.ts +4 -3
  39. package/dist/types/src/space/space.d.ts.map +1 -1
  40. package/dist/types/src/testing/test-agent-builder.d.ts.map +1 -1
  41. package/package.json +33 -33
  42. package/src/automerge/automerge-host.test.ts +22 -9
  43. package/src/automerge/automerge-host.ts +68 -90
  44. package/src/automerge/automerge-repo.test.ts +10 -1
  45. package/src/automerge/echo-network-adapter.ts +19 -0
  46. package/src/automerge/echo-replicator.ts +3 -0
  47. package/src/automerge/index.ts +1 -1
  48. package/src/automerge/leveldb-storage-adapter.ts +2 -2
  49. package/src/automerge/mesh-echo-replicator.ts +231 -0
  50. package/src/automerge/migrations.ts +2 -2
  51. package/src/db-host/snapshot-manager.ts +1 -1
  52. package/src/db-host/snapshot-store.ts +1 -1
  53. package/src/metadata/metadata-store.ts +2 -2
  54. package/src/space/space-manager.ts +4 -1
  55. package/src/space/space-protocol.ts +11 -8
  56. package/src/space/space.ts +8 -3
  57. package/src/testing/test-agent-builder.ts +1 -0
  58. package/dist/lib/browser/chunk-VQQD32DM.mjs.map +0 -7
  59. package/dist/lib/node/chunk-P7L7ICAH.cjs.map +0 -7
  60. package/dist/types/src/automerge/mesh-network-adapter.d.ts +0 -18
  61. package/dist/types/src/automerge/mesh-network-adapter.d.ts.map +0 -1
  62. 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_M475BGBI.AuthExtension,
22
+ AuthStatus: () => import_chunk_M475BGBI.AuthStatus,
23
23
  AutomergeDocumentLoaderImpl: () => AutomergeDocumentLoaderImpl,
24
24
  AutomergeHost: () => AutomergeHost,
25
25
  AutomergeStorageAdapter: () => AutomergeStorageAdapter,
26
- DataServiceImpl: () => import_chunk_P7L7ICAH.DataServiceImpl,
26
+ DataServiceImpl: () => import_chunk_M475BGBI.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_M475BGBI.MOCK_AUTH_PROVIDER,
30
+ MOCK_AUTH_VERIFIER: () => import_chunk_M475BGBI.MOCK_AUTH_VERIFIER,
31
+ MeshEchoReplicator: () => MeshEchoReplicator,
32
+ MetadataStore: () => import_chunk_M475BGBI.MetadataStore,
33
+ Pipeline: () => import_chunk_M475BGBI.Pipeline,
34
+ SnapshotManager: () => import_chunk_M475BGBI.SnapshotManager,
35
+ SnapshotStore: () => import_chunk_M475BGBI.SnapshotStore,
36
+ Space: () => import_chunk_M475BGBI.Space,
37
+ SpaceManager: () => import_chunk_M475BGBI.SpaceManager,
38
+ SpaceProtocol: () => import_chunk_M475BGBI.SpaceProtocol,
39
+ SpaceProtocolSession: () => import_chunk_M475BGBI.SpaceProtocolSession,
40
+ TimeframeClock: () => import_chunk_M475BGBI.TimeframeClock,
41
+ codec: () => import_chunk_M475BGBI.codec,
42
+ createMappedFeedWriter: () => import_chunk_M475BGBI.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_M475BGBI.hasInvitationExpired,
46
+ mapFeedIndexesToTimeframe: () => import_chunk_M475BGBI.mapFeedIndexesToTimeframe,
47
+ mapTimeframeToFeedIndexes: () => import_chunk_M475BGBI.mapTimeframeToFeedIndexes,
48
+ startAfter: () => import_chunk_M475BGBI.startAfter,
49
+ valueEncoding: () => import_chunk_M475BGBI.valueEncoding
50
50
  });
51
51
  module.exports = __toCommonJS(node_exports);
52
- var import_chunk_P7L7ICAH = require("./chunk-P7L7ICAH.cjs");
52
+ var import_chunk_M475BGBI = require("./chunk-M475BGBI.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) {
@@ -893,11 +787,13 @@ var AutomergeHost = class {
893
787
  if (!doc) {
894
788
  return;
895
789
  }
790
+ const spaceKey = getSpaceKeyFromDoc(doc) ?? void 0;
896
791
  const lastAvailableHash = (0, import_automerge.getHeads)(doc);
897
792
  const objectIds = Object.keys(doc.objects ?? {});
898
- const encodedIds = objectIds.map((objectId) => import_protocols.idCodec.encode({
793
+ const encodedIds = objectIds.map((objectId) => import_protocols.objectPointerCodec.encode({
899
794
  documentId: handle.documentId,
900
- objectId
795
+ objectId,
796
+ spaceKey
901
797
  }));
902
798
  const idToLastHash = new Map(encodedIds.map((id) => [
903
799
  id,
@@ -936,14 +832,25 @@ var AutomergeHost = class {
936
832
  _automergePeers() {
937
833
  return this._repo.peers;
938
834
  }
835
+ async _getContainingSpaceForDocument(documentId) {
836
+ const doc = this._repo.handles[documentId]?.docSync();
837
+ if (!doc) {
838
+ return null;
839
+ }
840
+ const spaceKeyHex = getSpaceKeyFromDoc(doc);
841
+ if (!spaceKeyHex) {
842
+ return null;
843
+ }
844
+ return import_keys.PublicKey.from(spaceKeyHex);
845
+ }
939
846
  //
940
847
  // Methods for client-services.
941
848
  //
942
849
  async flush({ states }) {
943
850
  await Promise.all(states?.map(async ({ heads, documentId }) => {
944
851
  (0, import_invariant.invariant)(heads, "heads are required for flush", {
945
- F: __dxlog_file5,
946
- L: 252,
852
+ F: __dxlog_file4,
853
+ L: 243,
947
854
  S: this,
948
855
  A: [
949
856
  "heads",
@@ -964,24 +871,6 @@ var AutomergeHost = class {
964
871
  async getHostInfo() {
965
872
  return this._clientNetwork.getHostInfo();
966
873
  }
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
874
  };
986
875
  _ts_decorate2([
987
876
  import_tracing.trace.info()
@@ -1039,7 +928,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1039
928
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1040
929
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1041
930
  }
1042
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
931
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
1043
932
  var AutomergeDocumentLoaderImpl = class {
1044
933
  constructor(_spaceKey, _repo) {
1045
934
  this._spaceKey = _spaceKey;
@@ -1047,7 +936,7 @@ var AutomergeDocumentLoaderImpl = class {
1047
936
  this._spaceRootDocHandle = null;
1048
937
  this._objectDocumentHandles = /* @__PURE__ */ new Map();
1049
938
  this._objectsPendingDocumentLoad = /* @__PURE__ */ new Set();
1050
- this.onObjectDocumentLoaded = new import_async5.Event();
939
+ this.onObjectDocumentLoaded = new import_async4.Event();
1051
940
  }
1052
941
  getAllHandles() {
1053
942
  return this._spaceRootDocHandle != null ? [
@@ -1060,10 +949,10 @@ var AutomergeDocumentLoaderImpl = class {
1060
949
  return;
1061
950
  }
1062
951
  if (!spaceState.rootUrl) {
1063
- import_log5.log.error("Database opened with no rootUrl", {
952
+ import_log4.log.error("Database opened with no rootUrl", {
1064
953
  spaceKey: this._spaceKey
1065
954
  }, {
1066
- F: __dxlog_file6,
955
+ F: __dxlog_file5,
1067
956
  L: 70,
1068
957
  S: this,
1069
958
  C: (f, a) => f(...a)
@@ -1072,8 +961,8 @@ var AutomergeDocumentLoaderImpl = class {
1072
961
  } else {
1073
962
  const existingDocHandle = await this._initDocHandle(ctx, spaceState.rootUrl);
1074
963
  const doc = existingDocHandle.docSync();
1075
- (0, import_invariant5.invariant)(doc, void 0, {
1076
- F: __dxlog_file6,
964
+ (0, import_invariant4.invariant)(doc, void 0, {
965
+ F: __dxlog_file5,
1077
966
  L: 75,
1078
967
  S: this,
1079
968
  A: [
@@ -1094,8 +983,8 @@ var AutomergeDocumentLoaderImpl = class {
1094
983
  let hasUrlsToLoad = false;
1095
984
  const urlsToLoad = {};
1096
985
  for (const objectId of objectIds) {
1097
- (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1098
- F: __dxlog_file6,
986
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
987
+ F: __dxlog_file5,
1099
988
  L: 88,
1100
989
  S: this,
1101
990
  A: [
@@ -1107,8 +996,8 @@ var AutomergeDocumentLoaderImpl = class {
1107
996
  continue;
1108
997
  }
1109
998
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
1110
- (0, import_invariant5.invariant)(spaceRootDoc, void 0, {
1111
- F: __dxlog_file6,
999
+ (0, import_invariant4.invariant)(spaceRootDoc, void 0, {
1000
+ F: __dxlog_file5,
1112
1001
  L: 93,
1113
1002
  S: this,
1114
1003
  A: [
@@ -1119,10 +1008,10 @@ var AutomergeDocumentLoaderImpl = class {
1119
1008
  const documentUrl = (spaceRootDoc.links ?? {})[objectId];
1120
1009
  if (documentUrl == null) {
1121
1010
  this._objectsPendingDocumentLoad.add(objectId);
1122
- import_log5.log.info("loading delayed until object links are initialized", {
1011
+ import_log4.log.info("loading delayed until object links are initialized", {
1123
1012
  objectId
1124
1013
  }, {
1125
- F: __dxlog_file6,
1014
+ F: __dxlog_file5,
1126
1015
  L: 97,
1127
1016
  S: this,
1128
1017
  C: (f, a) => f(...a)
@@ -1145,8 +1034,8 @@ var AutomergeDocumentLoaderImpl = class {
1145
1034
  linksAwaitingLoad.forEach(([objectId]) => this._objectsPendingDocumentLoad.delete(objectId));
1146
1035
  }
1147
1036
  getSpaceRootDocHandle() {
1148
- (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1149
- F: __dxlog_file6,
1037
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
1038
+ F: __dxlog_file5,
1150
1039
  L: 120,
1151
1040
  S: this,
1152
1041
  A: [
@@ -1157,8 +1046,8 @@ var AutomergeDocumentLoaderImpl = class {
1157
1046
  return this._spaceRootDocHandle;
1158
1047
  }
1159
1048
  createDocumentForObject(objectId) {
1160
- (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1161
- F: __dxlog_file6,
1049
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
1050
+ F: __dxlog_file5,
1162
1051
  L: 125,
1163
1052
  S: this,
1164
1053
  A: [
@@ -1197,11 +1086,11 @@ var AutomergeDocumentLoaderImpl = class {
1197
1086
  };
1198
1087
  const objectDocumentHandle = this._objectDocumentHandles.get(objectId);
1199
1088
  if (objectDocumentHandle != null && objectDocumentHandle.url !== automergeUrl) {
1200
- import_log5.log.warn("object already inlined in a different document, ignoring the link", {
1089
+ import_log4.log.warn("object already inlined in a different document, ignoring the link", {
1201
1090
  ...logMeta,
1202
1091
  actualDocumentUrl: objectDocumentHandle.url
1203
1092
  }, {
1204
- F: __dxlog_file6,
1093
+ F: __dxlog_file5,
1205
1094
  L: 155,
1206
1095
  S: this,
1207
1096
  C: (f, a) => f(...a)
@@ -1209,8 +1098,8 @@ var AutomergeDocumentLoaderImpl = class {
1209
1098
  continue;
1210
1099
  }
1211
1100
  if (objectDocumentHandle?.url === automergeUrl) {
1212
- import_log5.log.warn("object document was already loaded", logMeta, {
1213
- F: __dxlog_file6,
1101
+ import_log4.log.warn("object document was already loaded", logMeta, {
1102
+ F: __dxlog_file5,
1214
1103
  L: 162,
1215
1104
  S: this,
1216
1105
  C: (f, a) => f(...a)
@@ -1218,8 +1107,8 @@ var AutomergeDocumentLoaderImpl = class {
1218
1107
  continue;
1219
1108
  }
1220
1109
  const handle = this._repo.find(automergeUrl);
1221
- import_log5.log.debug("document loading triggered", logMeta, {
1222
- F: __dxlog_file6,
1110
+ import_log4.log.debug("document loading triggered", logMeta, {
1111
+ F: __dxlog_file5,
1223
1112
  L: 166,
1224
1113
  S: this,
1225
1114
  C: (f, a) => f(...a)
@@ -1238,11 +1127,11 @@ var AutomergeDocumentLoaderImpl = class {
1238
1127
  break;
1239
1128
  } catch (err) {
1240
1129
  if (`${err}`.includes("Timeout")) {
1241
- import_log5.log.info("wraparound", {
1130
+ import_log4.log.info("wraparound", {
1242
1131
  id: docHandle.documentId,
1243
1132
  state: docHandle.state
1244
1133
  }, {
1245
- F: __dxlog_file6,
1134
+ F: __dxlog_file5,
1246
1135
  L: 182,
1247
1136
  S: this,
1248
1137
  C: (f, a) => f(...a)
@@ -1283,8 +1172,8 @@ var AutomergeDocumentLoaderImpl = class {
1283
1172
  docUrl: handle.url
1284
1173
  };
1285
1174
  if (this.onObjectDocumentLoaded.listenerCount() === 0) {
1286
- import_log5.log.info("document loaded after all listeners were removed", logMeta, {
1287
- F: __dxlog_file6,
1175
+ import_log4.log.info("document loaded after all listeners were removed", logMeta, {
1176
+ F: __dxlog_file5,
1288
1177
  L: 218,
1289
1178
  S: this,
1290
1179
  C: (f, a) => f(...a)
@@ -1293,8 +1182,8 @@ var AutomergeDocumentLoaderImpl = class {
1293
1182
  }
1294
1183
  const objectDocHandle = this._objectDocumentHandles.get(objectId);
1295
1184
  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,
1185
+ import_log4.log.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1186
+ F: __dxlog_file5,
1298
1187
  L: 223,
1299
1188
  S: this,
1300
1189
  C: (f, a) => f(...a)
@@ -1307,13 +1196,13 @@ var AutomergeDocumentLoaderImpl = class {
1307
1196
  });
1308
1197
  } catch (err) {
1309
1198
  const shouldRetryLoading = this.onObjectDocumentLoaded.listenerCount() > 0;
1310
- import_log5.log.warn("failed to load a document", {
1199
+ import_log4.log.warn("failed to load a document", {
1311
1200
  objectId,
1312
1201
  automergeUrl: handle.url,
1313
1202
  retryLoading: shouldRetryLoading,
1314
1203
  err
1315
1204
  }, {
1316
- F: __dxlog_file6,
1205
+ F: __dxlog_file5,
1317
1206
  L: 229,
1318
1207
  S: this,
1319
1208
  C: (f, a) => f(...a)
@@ -1332,6 +1221,257 @@ _ts_decorate3([
1332
1221
  AutomergeDocumentLoaderImpl = _ts_decorate3([
1333
1222
  import_tracing2.trace.resource()
1334
1223
  ], AutomergeDocumentLoaderImpl);
1224
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1225
+ var MeshEchoReplicator = class {
1226
+ constructor() {
1227
+ this._connections = /* @__PURE__ */ new Set();
1228
+ this._connectionsPerPeer = /* @__PURE__ */ new Map();
1229
+ this._authorizedDevices = new import_util3.ComplexMap(import_keys2.PublicKey.hash);
1230
+ this._context = null;
1231
+ }
1232
+ async connect(context) {
1233
+ this._context = context;
1234
+ }
1235
+ async disconnect() {
1236
+ for (const connection of this._connections) {
1237
+ await connection.close();
1238
+ }
1239
+ this._connections.clear();
1240
+ this._connectionsPerPeer.clear();
1241
+ this._context = null;
1242
+ }
1243
+ createExtension() {
1244
+ (0, import_invariant5.invariant)(this._context, void 0, {
1245
+ F: __dxlog_file6,
1246
+ L: 54,
1247
+ S: this,
1248
+ A: [
1249
+ "this._context",
1250
+ ""
1251
+ ]
1252
+ });
1253
+ const connection = new MeshReplicatorConnection({
1254
+ ownPeerId: this._context.peerId,
1255
+ onRemoteConnected: async () => {
1256
+ (0, import_log5.log)("onRemoteConnected", {
1257
+ peerId: connection.peerId
1258
+ }, {
1259
+ F: __dxlog_file6,
1260
+ L: 59,
1261
+ S: this,
1262
+ C: (f, a) => f(...a)
1263
+ });
1264
+ (0, import_invariant5.invariant)(this._context, void 0, {
1265
+ F: __dxlog_file6,
1266
+ L: 60,
1267
+ S: this,
1268
+ A: [
1269
+ "this._context",
1270
+ ""
1271
+ ]
1272
+ });
1273
+ if (!this._connectionsPerPeer.has(connection.peerId)) {
1274
+ this._connectionsPerPeer.set(connection.peerId, connection);
1275
+ await connection.enable();
1276
+ this._context.onConnectionOpen(connection);
1277
+ }
1278
+ },
1279
+ onRemoteDisconnected: async () => {
1280
+ (0, import_log5.log)("onRemoteDisconnected", {
1281
+ peerId: connection.peerId
1282
+ }, {
1283
+ F: __dxlog_file6,
1284
+ L: 69,
1285
+ S: this,
1286
+ C: (f, a) => f(...a)
1287
+ });
1288
+ this._context?.onConnectionClosed(connection);
1289
+ await connection.disable();
1290
+ this._connectionsPerPeer.delete(connection.peerId);
1291
+ this._connections.delete(connection);
1292
+ },
1293
+ shouldAdvertize: async (params) => {
1294
+ (0, import_log5.log)("shouldAdvertize", {
1295
+ peerId: connection.peerId,
1296
+ documentId: params.documentId
1297
+ }, {
1298
+ F: __dxlog_file6,
1299
+ L: 76,
1300
+ S: this,
1301
+ C: (f, a) => f(...a)
1302
+ });
1303
+ (0, import_invariant5.invariant)(this._context, void 0, {
1304
+ F: __dxlog_file6,
1305
+ L: 77,
1306
+ S: this,
1307
+ A: [
1308
+ "this._context",
1309
+ ""
1310
+ ]
1311
+ });
1312
+ try {
1313
+ const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
1314
+ if (!spaceKey) {
1315
+ (0, import_log5.log)("space key not found for share policy check", {
1316
+ peerId: connection.peerId,
1317
+ documentId: params.documentId
1318
+ }, {
1319
+ F: __dxlog_file6,
1320
+ L: 81,
1321
+ S: this,
1322
+ C: (f, a) => f(...a)
1323
+ });
1324
+ return false;
1325
+ }
1326
+ const authorizedDevices = this._authorizedDevices.get(spaceKey);
1327
+ if (!connection.remoteDeviceKey) {
1328
+ (0, import_log5.log)("device key not found for share policy check", {
1329
+ peerId: connection.peerId,
1330
+ documentId: params.documentId
1331
+ }, {
1332
+ F: __dxlog_file6,
1333
+ L: 91,
1334
+ S: this,
1335
+ C: (f, a) => f(...a)
1336
+ });
1337
+ return false;
1338
+ }
1339
+ const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1340
+ (0, import_log5.log)("share policy check", {
1341
+ localPeer: this._context.peerId,
1342
+ remotePeer: connection.peerId,
1343
+ documentId: params.documentId,
1344
+ deviceKey: connection.remoteDeviceKey,
1345
+ spaceKey,
1346
+ isAuthorized
1347
+ }, {
1348
+ F: __dxlog_file6,
1349
+ L: 99,
1350
+ S: this,
1351
+ C: (f, a) => f(...a)
1352
+ });
1353
+ return isAuthorized;
1354
+ } catch (err) {
1355
+ import_log5.log.catch(err, void 0, {
1356
+ F: __dxlog_file6,
1357
+ L: 109,
1358
+ S: this,
1359
+ C: (f, a) => f(...a)
1360
+ });
1361
+ return false;
1362
+ }
1363
+ }
1364
+ });
1365
+ this._connections.add(connection);
1366
+ return connection.replicatorExtension;
1367
+ }
1368
+ authorizeDevice(spaceKey, deviceKey) {
1369
+ (0, import_log5.log)("authorizeDevice", {
1370
+ spaceKey,
1371
+ deviceKey
1372
+ }, {
1373
+ F: __dxlog_file6,
1374
+ L: 120,
1375
+ S: this,
1376
+ C: (f, a) => f(...a)
1377
+ });
1378
+ (0, import_util3.defaultMap)(this._authorizedDevices, spaceKey, () => new import_util3.ComplexSet(import_keys2.PublicKey.hash)).add(deviceKey);
1379
+ }
1380
+ };
1381
+ var MeshReplicatorConnection = class extends import_context5.Resource {
1382
+ constructor(_params) {
1383
+ super();
1384
+ this._params = _params;
1385
+ this.remoteDeviceKey = null;
1386
+ this._remotePeerId = null;
1387
+ this._isEnabled = false;
1388
+ let readableStreamController;
1389
+ this.readable = new ReadableStream({
1390
+ start: (controller) => {
1391
+ readableStreamController = controller;
1392
+ this._ctx.onDispose(() => controller.close());
1393
+ }
1394
+ });
1395
+ this.writable = new WritableStream({
1396
+ write: async (message, controller) => {
1397
+ this.replicatorExtension.sendSyncMessage({
1398
+ payload: import_automerge_repo4.cbor.encode(message)
1399
+ }).catch((err) => {
1400
+ controller.error(err);
1401
+ });
1402
+ }
1403
+ });
1404
+ this.replicatorExtension = new import_teleport_extension_automerge_replicator.AutomergeReplicator({
1405
+ peerId: this._params.ownPeerId
1406
+ }, {
1407
+ onStartReplication: async (info, remotePeerId) => {
1408
+ this.remoteDeviceKey = remotePeerId;
1409
+ this._remotePeerId = info.id;
1410
+ (0, import_log5.log)("onStartReplication", {
1411
+ id: info.id,
1412
+ thisPeerId: this.peerId,
1413
+ remotePeerId: remotePeerId.toHex()
1414
+ }, {
1415
+ F: __dxlog_file6,
1416
+ L: 185,
1417
+ S: this,
1418
+ C: (f, a) => f(...a)
1419
+ });
1420
+ await this._params.onRemoteConnected();
1421
+ },
1422
+ onSyncMessage: async ({ payload }) => {
1423
+ if (!this._isEnabled) {
1424
+ return;
1425
+ }
1426
+ const message = import_automerge_repo4.cbor.decode(payload);
1427
+ readableStreamController.enqueue(message);
1428
+ },
1429
+ onClose: async () => {
1430
+ if (!this._isEnabled) {
1431
+ return;
1432
+ }
1433
+ await this._params.onRemoteDisconnected();
1434
+ }
1435
+ });
1436
+ }
1437
+ get peerId() {
1438
+ (0, import_invariant5.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1439
+ F: __dxlog_file6,
1440
+ L: 208,
1441
+ S: this,
1442
+ A: [
1443
+ "this._remotePeerId != null",
1444
+ "'Remote peer has not connected yet.'"
1445
+ ]
1446
+ });
1447
+ return this._remotePeerId;
1448
+ }
1449
+ async shouldAdvertize(params) {
1450
+ return this._params.shouldAdvertize(params);
1451
+ }
1452
+ /**
1453
+ * Start exchanging messages with the remote peer.
1454
+ * Call after the remote peer has connected.
1455
+ */
1456
+ async enable() {
1457
+ (0, import_invariant5.invariant)(this._remotePeerId != null, "Remote peer has not connected yet.", {
1458
+ F: __dxlog_file6,
1459
+ L: 221,
1460
+ S: this,
1461
+ A: [
1462
+ "this._remotePeerId != null",
1463
+ "'Remote peer has not connected yet.'"
1464
+ ]
1465
+ });
1466
+ this._isEnabled = true;
1467
+ }
1468
+ /**
1469
+ * Stop exchanging messages with the remote peer.
1470
+ */
1471
+ async disable() {
1472
+ this._isEnabled = false;
1473
+ }
1474
+ };
1335
1475
  // Annotate the CommonJS export names for ESM import in node:
1336
1476
  0 && (module.exports = {
1337
1477
  AuthExtension,
@@ -1344,7 +1484,7 @@ AutomergeDocumentLoaderImpl = _ts_decorate3([
1344
1484
  LocalHostNetworkAdapter,
1345
1485
  MOCK_AUTH_PROVIDER,
1346
1486
  MOCK_AUTH_VERIFIER,
1347
- MeshNetworkAdapter,
1487
+ MeshEchoReplicator,
1348
1488
  MetadataStore,
1349
1489
  Pipeline,
1350
1490
  SnapshotManager,