@dxos/echo-pipeline 0.5.1-next.260c093 → 0.5.1-next.26de2f3

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.
@@ -60,34 +60,250 @@ var import_log = require("@dxos/log");
60
60
  var import_protocols = require("@dxos/protocols");
61
61
  var import_tracing = require("@dxos/tracing");
62
62
  var import_util = require("@dxos/util");
63
- var import_context2 = require("@dxos/context");
64
63
  var import_async2 = require("@dxos/async");
65
64
  var import_automerge_repo2 = require("@dxos/automerge/automerge-repo");
66
- var import_codec_protobuf = require("@dxos/codec-protobuf");
65
+ var import_context2 = require("@dxos/context");
67
66
  var import_invariant2 = require("@dxos/invariant");
67
+ var import_log2 = require("@dxos/log");
68
+ var import_context3 = require("@dxos/context");
68
69
  var import_async3 = require("@dxos/async");
69
70
  var import_automerge_repo3 = require("@dxos/automerge/automerge-repo");
71
+ var import_codec_protobuf = require("@dxos/codec-protobuf");
70
72
  var import_invariant3 = require("@dxos/invariant");
71
- var import_log2 = require("@dxos/log");
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");
72
77
  var import_teleport_extension_automerge_replicator = require("@dxos/teleport-extension-automerge-replicator");
73
78
  var import_automerge_repo_storage_indexeddb = require("@dxos/automerge/automerge-repo-storage-indexeddb");
74
- var import_log3 = require("@dxos/log");
79
+ var import_log4 = require("@dxos/log");
75
80
  var import_random_access_storage = require("@dxos/random-access-storage");
76
81
  var import_util2 = require("@dxos/util");
77
- var import_async4 = require("@dxos/async");
78
- var import_context3 = require("@dxos/context");
82
+ var import_async5 = require("@dxos/async");
83
+ var import_context4 = require("@dxos/context");
79
84
  var import_debug = require("@dxos/debug");
80
- var import_invariant4 = require("@dxos/invariant");
81
- var import_log4 = require("@dxos/log");
85
+ var import_invariant5 = require("@dxos/invariant");
86
+ var import_log5 = require("@dxos/log");
82
87
  var import_tracing2 = require("@dxos/tracing");
83
- var LevelDBStorageAdapter = class extends import_context2.Resource {
88
+ function _ts_decorate(decorators, target, key, desc) {
89
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
90
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
91
+ r = Reflect.decorate(decorators, target, key, desc);
92
+ else
93
+ for (var i = decorators.length - 1; i >= 0; i--)
94
+ if (d = decorators[i])
95
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
96
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
97
+ }
98
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts";
99
+ var EchoNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
100
+ constructor() {
101
+ super(...arguments);
102
+ this._replicators = /* @__PURE__ */ new Set();
103
+ this._connections = /* @__PURE__ */ new Map();
104
+ this._lifecycleState = import_context2.LifecycleState.CLOSED;
105
+ this._connected = new import_async2.Trigger();
106
+ }
107
+ connect(peerId, peerMetadata) {
108
+ this.peerId = peerId;
109
+ this.peerMetadata = peerMetadata;
110
+ this._connected.wake();
111
+ }
112
+ send(message) {
113
+ const connectionEntry = this._connections.get(message.targetId);
114
+ if (!connectionEntry) {
115
+ throw new Error("Connection not found.");
116
+ }
117
+ connectionEntry.writer.write(message).catch((err) => {
118
+ if (connectionEntry.isOpen) {
119
+ import_log2.log.catch(err, void 0, {
120
+ F: __dxlog_file,
121
+ L: 40,
122
+ S: this,
123
+ C: (f, a) => f(...a)
124
+ });
125
+ }
126
+ });
127
+ }
128
+ disconnect() {
129
+ }
130
+ async open() {
131
+ (0, import_invariant2.invariant)(this._lifecycleState === import_context2.LifecycleState.CLOSED, void 0, {
132
+ F: __dxlog_file,
133
+ L: 51,
134
+ S: this,
135
+ A: [
136
+ "this._lifecycleState === LifecycleState.CLOSED",
137
+ ""
138
+ ]
139
+ });
140
+ this._lifecycleState = import_context2.LifecycleState.OPEN;
141
+ this.emit("ready", {
142
+ network: this
143
+ });
144
+ }
145
+ async close() {
146
+ (0, import_invariant2.invariant)(this._lifecycleState === import_context2.LifecycleState.OPEN, void 0, {
147
+ F: __dxlog_file,
148
+ L: 61,
149
+ S: this,
150
+ A: [
151
+ "this._lifecycleState === LifecycleState.OPEN",
152
+ ""
153
+ ]
154
+ });
155
+ for (const replicator of this._replicators) {
156
+ await replicator.disconnect();
157
+ }
158
+ this._replicators.clear();
159
+ this._lifecycleState = import_context2.LifecycleState.CLOSED;
160
+ }
161
+ async whenConnected() {
162
+ await this._connected.wait({
163
+ timeout: 1e4
164
+ });
165
+ }
166
+ async addReplicator(replicator) {
167
+ (0, import_invariant2.invariant)(this.peerId, void 0, {
168
+ F: __dxlog_file,
169
+ L: 77,
170
+ S: this,
171
+ A: [
172
+ "this.peerId",
173
+ ""
174
+ ]
175
+ });
176
+ (0, import_invariant2.invariant)(!this._replicators.has(replicator), void 0, {
177
+ F: __dxlog_file,
178
+ L: 78,
179
+ S: this,
180
+ A: [
181
+ "!this._replicators.has(replicator)",
182
+ ""
183
+ ]
184
+ });
185
+ await replicator.connect({
186
+ peerId: this.peerId,
187
+ onConnectionOpen: this._onConnectionOpen.bind(this),
188
+ onConnectionClosed: this._onConnectionClosed.bind(this)
189
+ });
190
+ }
191
+ async removeReplicator(replicator) {
192
+ (0, import_invariant2.invariant)(this._replicators.has(replicator), void 0, {
193
+ F: __dxlog_file,
194
+ L: 89,
195
+ S: this,
196
+ A: [
197
+ "this._replicators.has(replicator)",
198
+ ""
199
+ ]
200
+ });
201
+ await replicator.disconnect();
202
+ }
203
+ async shouldAdvertize(peerId, params) {
204
+ const connection = this._connections.get(peerId);
205
+ if (!connection) {
206
+ return false;
207
+ }
208
+ return connection.connection.shouldAdvertize(params);
209
+ }
210
+ _onConnectionOpen(connection) {
211
+ (0, import_invariant2.invariant)(!this._connections.has(connection.peerId), void 0, {
212
+ F: __dxlog_file,
213
+ L: 103,
214
+ S: this,
215
+ A: [
216
+ "!this._connections.has(connection.peerId as PeerId)",
217
+ ""
218
+ ]
219
+ });
220
+ const reader = connection.readable.getReader();
221
+ const writer = connection.writable.getWriter();
222
+ const connectionEntry = {
223
+ connection,
224
+ reader,
225
+ writer,
226
+ isOpen: true
227
+ };
228
+ this._connections.set(connection.peerId, connectionEntry);
229
+ queueMicrotask(async () => {
230
+ try {
231
+ while (true) {
232
+ const { done, value } = await reader.read();
233
+ if (done) {
234
+ break;
235
+ }
236
+ this.emit("message", value);
237
+ }
238
+ } catch (err) {
239
+ if (connectionEntry.isOpen) {
240
+ import_log2.log.catch(err, void 0, {
241
+ F: __dxlog_file,
242
+ L: 122,
243
+ S: this,
244
+ C: (f, a) => f(...a)
245
+ });
246
+ }
247
+ }
248
+ });
249
+ this.emit("peer-candidate", {
250
+ peerId: connection.peerId,
251
+ peerMetadata: {
252
+ // TODO(dmaretskyi): Refactor this.
253
+ dxos_peerSource: "EchoNetworkAdapter"
254
+ }
255
+ });
256
+ }
257
+ _onConnectionClosed(connection) {
258
+ const entry = this._connections.get(connection.peerId);
259
+ (0, import_invariant2.invariant)(entry, void 0, {
260
+ F: __dxlog_file,
261
+ L: 138,
262
+ S: this,
263
+ A: [
264
+ "entry",
265
+ ""
266
+ ]
267
+ });
268
+ entry.isOpen = false;
269
+ this.emit("peer-disconnected", {
270
+ peerId: connection.peerId
271
+ });
272
+ void entry.reader.cancel().catch((err) => import_log2.log.catch(err, void 0, {
273
+ F: __dxlog_file,
274
+ L: 143,
275
+ S: this,
276
+ C: (f, a) => f(...a)
277
+ }));
278
+ void entry.writer.abort().catch((err) => import_log2.log.catch(err, void 0, {
279
+ F: __dxlog_file,
280
+ L: 144,
281
+ S: this,
282
+ C: (f, a) => f(...a)
283
+ }));
284
+ this._connections.delete(connection.peerId);
285
+ }
286
+ };
287
+ _ts_decorate([
288
+ import_async2.synchronized
289
+ ], EchoNetworkAdapter.prototype, "open", null);
290
+ _ts_decorate([
291
+ import_async2.synchronized
292
+ ], EchoNetworkAdapter.prototype, "close", null);
293
+ _ts_decorate([
294
+ import_async2.synchronized
295
+ ], EchoNetworkAdapter.prototype, "addReplicator", null);
296
+ _ts_decorate([
297
+ import_async2.synchronized
298
+ ], EchoNetworkAdapter.prototype, "removeReplicator", null);
299
+ var LevelDBStorageAdapter = class extends import_context3.Resource {
84
300
  constructor(_params) {
85
301
  super();
86
302
  this._params = _params;
87
303
  }
88
304
  async load(keyArray) {
89
305
  try {
90
- if (this._lifecycleState !== import_context2.LifecycleState.OPEN) {
306
+ if (this._lifecycleState !== import_context3.LifecycleState.OPEN) {
91
307
  return void 0;
92
308
  }
93
309
  return await this._params.db.get(keyArray, {
@@ -101,7 +317,7 @@ var LevelDBStorageAdapter = class extends import_context2.Resource {
101
317
  }
102
318
  }
103
319
  async save(keyArray, binary) {
104
- if (this._lifecycleState !== import_context2.LifecycleState.OPEN) {
320
+ if (this._lifecycleState !== import_context3.LifecycleState.OPEN) {
105
321
  return void 0;
106
322
  }
107
323
  const batch = this._params.db.batch();
@@ -116,7 +332,7 @@ var LevelDBStorageAdapter = class extends import_context2.Resource {
116
332
  await this._params.callbacks?.afterSave?.(keyArray);
117
333
  }
118
334
  async remove(keyArray) {
119
- if (this._lifecycleState !== import_context2.LifecycleState.OPEN) {
335
+ if (this._lifecycleState !== import_context3.LifecycleState.OPEN) {
120
336
  return void 0;
121
337
  }
122
338
  await this._params.db.del(keyArray, {
@@ -124,7 +340,7 @@ var LevelDBStorageAdapter = class extends import_context2.Resource {
124
340
  });
125
341
  }
126
342
  async loadRange(keyPrefix) {
127
- if (this._lifecycleState !== import_context2.LifecycleState.OPEN) {
343
+ if (this._lifecycleState !== import_context3.LifecycleState.OPEN) {
128
344
  return [];
129
345
  }
130
346
  const result = [];
@@ -144,7 +360,7 @@ var LevelDBStorageAdapter = class extends import_context2.Resource {
144
360
  return result;
145
361
  }
146
362
  async removeRange(keyPrefix) {
147
- if (this._lifecycleState !== import_context2.LifecycleState.OPEN) {
363
+ if (this._lifecycleState !== import_context3.LifecycleState.OPEN) {
148
364
  return void 0;
149
365
  }
150
366
  const batch = this._params.db.batch();
@@ -173,12 +389,12 @@ var encodingOptions = {
173
389
  valueEncoding: "buffer"
174
390
  };
175
391
  var isLevelDbNotFoundError = (err) => err.code === "LEVEL_NOT_FOUND";
176
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/local-host-network-adapter.ts";
177
- var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapter {
392
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/local-host-network-adapter.ts";
393
+ var LocalHostNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
178
394
  constructor() {
179
395
  super(...arguments);
180
396
  this._peers = /* @__PURE__ */ new Map();
181
- this._connected = new import_async2.Trigger();
397
+ this._connected = new import_async3.Trigger();
182
398
  this._isConnected = false;
183
399
  }
184
400
  /**
@@ -201,8 +417,8 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
201
417
  }
202
418
  send(message) {
203
419
  const peer = this._peers.get(message.targetId);
204
- (0, import_invariant2.invariant)(peer, "Peer not found.", {
205
- F: __dxlog_file,
420
+ (0, import_invariant3.invariant)(peer, "Peer not found.", {
421
+ F: __dxlog_file2,
206
422
  L: 51,
207
423
  S: this,
208
424
  A: [
@@ -226,8 +442,8 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
226
442
  syncRepo({ id, syncMessage }) {
227
443
  const peerId = this._getPeerId(id);
228
444
  return new import_codec_protobuf.Stream(({ next, close }) => {
229
- (0, import_invariant2.invariant)(!this._peers.has(peerId), "Peer already connected.", {
230
- F: __dxlog_file,
445
+ (0, import_invariant3.invariant)(!this._peers.has(peerId), "Peer already connected.", {
446
+ F: __dxlog_file2,
231
447
  L: 73,
232
448
  S: this,
233
449
  A: [
@@ -239,7 +455,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
239
455
  connected: true,
240
456
  send: (message) => {
241
457
  next({
242
- syncMessage: import_automerge_repo2.cbor.encode(message)
458
+ syncMessage: import_automerge_repo3.cbor.encode(message)
243
459
  });
244
460
  },
245
461
  disconnect: () => {
@@ -250,8 +466,8 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
250
466
  });
251
467
  }
252
468
  });
253
- (0, import_invariant2.invariant)(this._isConnected, void 0, {
254
- F: __dxlog_file,
469
+ (0, import_invariant3.invariant)(this._isConnected, void 0, {
470
+ F: __dxlog_file2,
255
471
  L: 90,
256
472
  S: this,
257
473
  A: [
@@ -266,8 +482,8 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
266
482
  });
267
483
  }
268
484
  async sendSyncMessage({ id, syncMessage }) {
269
- (0, import_invariant2.invariant)(this._isConnected, void 0, {
270
- F: __dxlog_file,
485
+ (0, import_invariant3.invariant)(this._isConnected, void 0, {
486
+ F: __dxlog_file2,
271
487
  L: 99,
272
488
  S: this,
273
489
  A: [
@@ -275,12 +491,12 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
275
491
  ""
276
492
  ]
277
493
  });
278
- const message = import_automerge_repo2.cbor.decode(syncMessage);
494
+ const message = import_automerge_repo3.cbor.decode(syncMessage);
279
495
  this.emit("message", message);
280
496
  }
281
497
  async getHostInfo() {
282
- (0, import_invariant2.invariant)(this._isConnected, void 0, {
283
- F: __dxlog_file,
498
+ (0, import_invariant3.invariant)(this._isConnected, void 0, {
499
+ F: __dxlog_file2,
284
500
  L: 105,
285
501
  S: this,
286
502
  A: [
@@ -288,8 +504,8 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
288
504
  ""
289
505
  ]
290
506
  });
291
- (0, import_invariant2.invariant)(this.peerId, "Peer id not set.", {
292
- F: __dxlog_file,
507
+ (0, import_invariant3.invariant)(this.peerId, "Peer id not set.", {
508
+ F: __dxlog_file2,
293
509
  L: 106,
294
510
  S: this,
295
511
  A: [
@@ -305,12 +521,12 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
305
521
  return id;
306
522
  }
307
523
  };
308
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-network-adapter.ts";
309
- var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
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 {
310
526
  constructor() {
311
527
  super(...arguments);
312
528
  this._extensions = /* @__PURE__ */ new Map();
313
- this._connected = new import_async3.Trigger();
529
+ this._connected = new import_async4.Trigger();
314
530
  }
315
531
  /**
316
532
  * Emits `ready` event. That signals to `Repo` that it can start using the adapter.
@@ -327,8 +543,8 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
327
543
  send(message) {
328
544
  const receiverId = message.targetId;
329
545
  const extension = this._extensions.get(receiverId);
330
- (0, import_invariant3.invariant)(extension, "Extension not found.", {
331
- F: __dxlog_file2,
546
+ (0, import_invariant4.invariant)(extension, "Extension not found.", {
547
+ F: __dxlog_file3,
332
548
  L: 38,
333
549
  S: this,
334
550
  A: [
@@ -337,9 +553,9 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
337
553
  ]
338
554
  });
339
555
  extension.sendSyncMessage({
340
- payload: import_automerge_repo3.cbor.encode(message)
341
- }).catch((err) => import_log2.log.catch(err, void 0, {
342
- F: __dxlog_file2,
556
+ payload: import_automerge_repo4.cbor.encode(message)
557
+ }).catch((err) => import_log3.log.catch(err, void 0, {
558
+ F: __dxlog_file3,
343
559
  L: 39,
344
560
  S: this,
345
561
  C: (f, a) => f(...a)
@@ -348,8 +564,8 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
348
564
  disconnect() {
349
565
  }
350
566
  createExtension() {
351
- (0, import_invariant3.invariant)(this.peerId, "Peer id not set.", {
352
- F: __dxlog_file2,
567
+ (0, import_invariant4.invariant)(this.peerId, "Peer id not set.", {
568
+ F: __dxlog_file3,
353
569
  L: 47,
354
570
  S: this,
355
571
  A: [
@@ -363,12 +579,12 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
363
579
  }, {
364
580
  onStartReplication: async (info, remotePeerId) => {
365
581
  await this._connected.wait();
366
- (0, import_log2.log)("onStartReplication", {
582
+ (0, import_log3.log)("onStartReplication", {
367
583
  id: info.id,
368
584
  thisPeerId: this.peerId,
369
585
  remotePeerId: remotePeerId.toHex()
370
586
  }, {
371
- F: __dxlog_file2,
587
+ F: __dxlog_file3,
372
588
  L: 70,
373
589
  S: this,
374
590
  C: (f, a) => f(...a)
@@ -376,12 +592,12 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
376
592
  if (!this._extensions.has(info.id)) {
377
593
  peerInfo = info;
378
594
  this._extensions.set(info.id, extension);
379
- (0, import_log2.log)("peer-candidate", {
595
+ (0, import_log3.log)("peer-candidate", {
380
596
  id: info.id,
381
597
  thisPeerId: this.peerId,
382
598
  remotePeerId: remotePeerId.toHex()
383
599
  }, {
384
- F: __dxlog_file2,
600
+ F: __dxlog_file3,
385
601
  L: 76,
386
602
  S: this,
387
603
  C: (f, a) => f(...a)
@@ -399,7 +615,7 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
399
615
  if (!peerInfo) {
400
616
  return;
401
617
  }
402
- const message = import_automerge_repo3.cbor.decode(payload);
618
+ const message = import_automerge_repo4.cbor.decode(payload);
403
619
  this.emit("message", message);
404
620
  },
405
621
  onClose: async () => {
@@ -488,7 +704,7 @@ var AutomergeStorageAdapter = class {
488
704
  return filename.split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%"));
489
705
  }
490
706
  };
491
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/migrations.ts";
707
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/migrations.ts";
492
708
  var levelMigration = async ({ db, directory }) => {
493
709
  const isNewLevel = !await db.iterator({
494
710
  ...encodingOptions
@@ -502,10 +718,10 @@ var levelMigration = async ({ db, directory }) => {
502
718
  return;
503
719
  }
504
720
  const batch = db.batch();
505
- import_log3.log.info("found chunks on old storage adapter", {
721
+ import_log4.log.info("found chunks on old storage adapter", {
506
722
  chunks: chunks.length
507
723
  }, {
508
- F: __dxlog_file3,
724
+ F: __dxlog_file4,
509
725
  L: 36,
510
726
  S: void 0,
511
727
  C: (f, a) => f(...a)
@@ -517,7 +733,7 @@ var levelMigration = async ({ db, directory }) => {
517
733
  }
518
734
  await batch.write();
519
735
  };
520
- function _ts_decorate(decorators, target, key, desc) {
736
+ function _ts_decorate2(decorators, target, key, desc) {
521
737
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
522
738
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
523
739
  r = Reflect.decorate(decorators, target, key, desc);
@@ -527,10 +743,11 @@ function _ts_decorate(decorators, target, key, desc) {
527
743
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
528
744
  return c > 3 && r && Object.defineProperty(target, key, r), r;
529
745
  }
530
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
746
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
531
747
  var AutomergeHost = class {
532
748
  constructor({ directory, db, indexMetadataStore }) {
533
749
  this._ctx = new import_context.Context();
750
+ this._echoNetworkAdapter = new EchoNetworkAdapter();
534
751
  this._authorizedDevices = new import_util.ComplexMap(import_keys.PublicKey.hash);
535
752
  this._requestedDocs = /* @__PURE__ */ new Set();
536
753
  this._directory = directory;
@@ -557,7 +774,8 @@ var AutomergeHost = class {
557
774
  peerId: this._peerId,
558
775
  network: [
559
776
  this._clientNetwork,
560
- this._meshNetwork
777
+ this._meshNetwork,
778
+ this._echoNetworkAdapter
561
779
  ],
562
780
  storage: this._storage,
563
781
  // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
@@ -569,6 +787,12 @@ var AutomergeHost = class {
569
787
  if (!documentId) {
570
788
  return false;
571
789
  }
790
+ const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
791
+ if (peerMetadata?.dxos_peerSource === "EchoNetworkAdapter") {
792
+ return this._echoNetworkAdapter.shouldAdvertize(peerId, {
793
+ documentId
794
+ });
795
+ }
572
796
  const doc = this._repo.handles[documentId]?.docSync();
573
797
  if (!doc) {
574
798
  const isRequested = this._requestedDocs.has(`automerge:${documentId}`);
@@ -577,8 +801,8 @@ var AutomergeHost = class {
577
801
  documentId,
578
802
  isRequested
579
803
  }, {
580
- F: __dxlog_file4,
581
- L: 116,
804
+ F: __dxlog_file5,
805
+ L: 124,
582
806
  S: this,
583
807
  C: (f, a) => f(...a)
584
808
  });
@@ -591,22 +815,22 @@ var AutomergeHost = class {
591
815
  peerId,
592
816
  documentId
593
817
  }, {
594
- F: __dxlog_file4,
595
- L: 123,
818
+ F: __dxlog_file5,
819
+ L: 131,
596
820
  S: this,
597
821
  C: (f, a) => f(...a)
598
822
  });
599
823
  return false;
600
824
  }
601
825
  const authorizedDevices = this._authorizedDevices.get(import_keys.PublicKey.from(spaceKey));
602
- const deviceKeyHex = this.repo.peerMetadataByPeerId[peerId]?.dxos_deviceKey;
826
+ const deviceKeyHex = peerMetadata?.dxos_deviceKey;
603
827
  if (!deviceKeyHex) {
604
828
  (0, import_log.log)("device key not found for share policy check", {
605
829
  peerId,
606
830
  documentId
607
831
  }, {
608
- F: __dxlog_file4,
609
- L: 132,
832
+ F: __dxlog_file5,
833
+ L: 140,
610
834
  S: this,
611
835
  C: (f, a) => f(...a)
612
836
  });
@@ -622,16 +846,16 @@ var AutomergeHost = class {
622
846
  spaceKey,
623
847
  isAuthorized
624
848
  }, {
625
- F: __dxlog_file4,
626
- L: 138,
849
+ F: __dxlog_file5,
850
+ L: 146,
627
851
  S: this,
628
852
  C: (f, a) => f(...a)
629
853
  });
630
854
  return isAuthorized;
631
855
  } catch (err) {
632
856
  import_log.log.catch(err, void 0, {
633
- F: __dxlog_file4,
634
- L: 148,
857
+ F: __dxlog_file5,
858
+ L: 156,
635
859
  S: this,
636
860
  C: (f, a) => f(...a)
637
861
  });
@@ -641,16 +865,25 @@ var AutomergeHost = class {
641
865
  });
642
866
  this._clientNetwork.ready();
643
867
  this._meshNetwork.ready();
868
+ await this._echoNetworkAdapter.open();
644
869
  await this._clientNetwork.whenConnected();
870
+ await this._echoNetworkAdapter.whenConnected();
645
871
  }
646
872
  async close() {
647
873
  await this._storage.close?.();
648
874
  await this._clientNetwork.close();
875
+ await this._echoNetworkAdapter.close();
649
876
  await this._ctx.dispose();
650
877
  }
651
878
  get repo() {
652
879
  return this._repo;
653
880
  }
881
+ async addReplicator(replicator) {
882
+ await this._echoNetworkAdapter.addReplicator(replicator);
883
+ }
884
+ async removeReplicator(replicator) {
885
+ await this._echoNetworkAdapter.removeReplicator(replicator);
886
+ }
654
887
  async _beforeSave({ path, batch }) {
655
888
  const handle = this._repo.handles[path[0]];
656
889
  if (!handle) {
@@ -709,8 +942,8 @@ var AutomergeHost = class {
709
942
  async flush({ states }) {
710
943
  await Promise.all(states?.map(async ({ heads, documentId }) => {
711
944
  (0, import_invariant.invariant)(heads, "heads are required for flush", {
712
- F: __dxlog_file4,
713
- L: 233,
945
+ F: __dxlog_file5,
946
+ L: 252,
714
947
  S: this,
715
948
  A: [
716
949
  "heads",
@@ -742,33 +975,33 @@ var AutomergeHost = class {
742
975
  spaceKey,
743
976
  deviceKey
744
977
  }, {
745
- F: __dxlog_file4,
746
- L: 263,
978
+ F: __dxlog_file5,
979
+ L: 282,
747
980
  S: this,
748
981
  C: (f, a) => f(...a)
749
982
  });
750
983
  (0, import_util.defaultMap)(this._authorizedDevices, spaceKey, () => new import_util.ComplexSet(import_keys.PublicKey.hash)).add(deviceKey);
751
984
  }
752
985
  };
753
- _ts_decorate([
986
+ _ts_decorate2([
754
987
  import_tracing.trace.info()
755
988
  ], AutomergeHost.prototype, "_peerId", void 0);
756
- _ts_decorate([
989
+ _ts_decorate2([
757
990
  import_tracing.trace.info({
758
991
  depth: null
759
992
  })
760
993
  ], AutomergeHost.prototype, "_automergeDocs", null);
761
- _ts_decorate([
994
+ _ts_decorate2([
762
995
  import_tracing.trace.info({
763
996
  depth: null
764
997
  })
765
998
  ], AutomergeHost.prototype, "_automergePeers", null);
766
- _ts_decorate([
999
+ _ts_decorate2([
767
1000
  import_tracing.trace.span({
768
1001
  showInBrowserTimeline: true
769
1002
  })
770
1003
  ], AutomergeHost.prototype, "flush", null);
771
- AutomergeHost = _ts_decorate([
1004
+ AutomergeHost = _ts_decorate2([
772
1005
  import_tracing.trace.resource()
773
1006
  ], AutomergeHost);
774
1007
  var getSpaceKeyFromDoc = (doc) => {
@@ -796,7 +1029,7 @@ var waitForHeads = async (handle, heads) => {
796
1029
  var changeIsPresentInDoc = (doc, changeHash) => {
797
1030
  return !!(0, import_automerge.getBackend)(doc).getChangeByHash(changeHash);
798
1031
  };
799
- function _ts_decorate2(decorators, target, key, desc) {
1032
+ function _ts_decorate3(decorators, target, key, desc) {
800
1033
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
801
1034
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
802
1035
  r = Reflect.decorate(decorators, target, key, desc);
@@ -806,7 +1039,7 @@ function _ts_decorate2(decorators, target, key, desc) {
806
1039
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
807
1040
  return c > 3 && r && Object.defineProperty(target, key, r), r;
808
1041
  }
809
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
1042
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-doc-loader.ts";
810
1043
  var AutomergeDocumentLoaderImpl = class {
811
1044
  constructor(_spaceKey, _repo) {
812
1045
  this._spaceKey = _spaceKey;
@@ -814,23 +1047,24 @@ var AutomergeDocumentLoaderImpl = class {
814
1047
  this._spaceRootDocHandle = null;
815
1048
  this._objectDocumentHandles = /* @__PURE__ */ new Map();
816
1049
  this._objectsPendingDocumentLoad = /* @__PURE__ */ new Set();
817
- this.onObjectDocumentLoaded = new import_async4.Event();
1050
+ this.onObjectDocumentLoaded = new import_async5.Event();
818
1051
  }
819
1052
  getAllHandles() {
820
- return [
1053
+ return this._spaceRootDocHandle != null ? [
1054
+ this._spaceRootDocHandle,
821
1055
  ...new Set(this._objectDocumentHandles.values())
822
- ];
1056
+ ] : [];
823
1057
  }
824
1058
  async loadSpaceRootDocHandle(ctx, spaceState) {
825
1059
  if (this._spaceRootDocHandle != null) {
826
1060
  return;
827
1061
  }
828
1062
  if (!spaceState.rootUrl) {
829
- import_log4.log.error("Database opened with no rootUrl", {
1063
+ import_log5.log.error("Database opened with no rootUrl", {
830
1064
  spaceKey: this._spaceKey
831
1065
  }, {
832
- F: __dxlog_file5,
833
- L: 68,
1066
+ F: __dxlog_file6,
1067
+ L: 70,
834
1068
  S: this,
835
1069
  C: (f, a) => f(...a)
836
1070
  });
@@ -838,9 +1072,9 @@ var AutomergeDocumentLoaderImpl = class {
838
1072
  } else {
839
1073
  const existingDocHandle = await this._initDocHandle(ctx, spaceState.rootUrl);
840
1074
  const doc = existingDocHandle.docSync();
841
- (0, import_invariant4.invariant)(doc, void 0, {
842
- F: __dxlog_file5,
843
- L: 73,
1075
+ (0, import_invariant5.invariant)(doc, void 0, {
1076
+ F: __dxlog_file6,
1077
+ L: 75,
844
1078
  S: this,
845
1079
  A: [
846
1080
  "doc",
@@ -860,9 +1094,9 @@ var AutomergeDocumentLoaderImpl = class {
860
1094
  let hasUrlsToLoad = false;
861
1095
  const urlsToLoad = {};
862
1096
  for (const objectId of objectIds) {
863
- (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
864
- F: __dxlog_file5,
865
- L: 86,
1097
+ (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1098
+ F: __dxlog_file6,
1099
+ L: 88,
866
1100
  S: this,
867
1101
  A: [
868
1102
  "this._spaceRootDocHandle",
@@ -873,9 +1107,9 @@ var AutomergeDocumentLoaderImpl = class {
873
1107
  continue;
874
1108
  }
875
1109
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
876
- (0, import_invariant4.invariant)(spaceRootDoc, void 0, {
877
- F: __dxlog_file5,
878
- L: 91,
1110
+ (0, import_invariant5.invariant)(spaceRootDoc, void 0, {
1111
+ F: __dxlog_file6,
1112
+ L: 93,
879
1113
  S: this,
880
1114
  A: [
881
1115
  "spaceRootDoc",
@@ -885,11 +1119,11 @@ var AutomergeDocumentLoaderImpl = class {
885
1119
  const documentUrl = (spaceRootDoc.links ?? {})[objectId];
886
1120
  if (documentUrl == null) {
887
1121
  this._objectsPendingDocumentLoad.add(objectId);
888
- import_log4.log.info("loading delayed until object links are initialized", {
1122
+ import_log5.log.info("loading delayed until object links are initialized", {
889
1123
  objectId
890
1124
  }, {
891
- F: __dxlog_file5,
892
- L: 95,
1125
+ F: __dxlog_file6,
1126
+ L: 97,
893
1127
  S: this,
894
1128
  C: (f, a) => f(...a)
895
1129
  });
@@ -911,9 +1145,9 @@ var AutomergeDocumentLoaderImpl = class {
911
1145
  linksAwaitingLoad.forEach(([objectId]) => this._objectsPendingDocumentLoad.delete(objectId));
912
1146
  }
913
1147
  getSpaceRootDocHandle() {
914
- (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
915
- F: __dxlog_file5,
916
- L: 118,
1148
+ (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1149
+ F: __dxlog_file6,
1150
+ L: 120,
917
1151
  S: this,
918
1152
  A: [
919
1153
  "this._spaceRootDocHandle",
@@ -923,9 +1157,9 @@ var AutomergeDocumentLoaderImpl = class {
923
1157
  return this._spaceRootDocHandle;
924
1158
  }
925
1159
  createDocumentForObject(objectId) {
926
- (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
927
- F: __dxlog_file5,
928
- L: 123,
1160
+ (0, import_invariant5.invariant)(this._spaceRootDocHandle, void 0, {
1161
+ F: __dxlog_file6,
1162
+ L: 125,
929
1163
  S: this,
930
1164
  A: [
931
1165
  "this._spaceRootDocHandle",
@@ -963,30 +1197,30 @@ var AutomergeDocumentLoaderImpl = class {
963
1197
  };
964
1198
  const objectDocumentHandle = this._objectDocumentHandles.get(objectId);
965
1199
  if (objectDocumentHandle != null && objectDocumentHandle.url !== automergeUrl) {
966
- import_log4.log.warn("object already inlined in a different document, ignoring the link", {
1200
+ import_log5.log.warn("object already inlined in a different document, ignoring the link", {
967
1201
  ...logMeta,
968
1202
  actualDocumentUrl: objectDocumentHandle.url
969
1203
  }, {
970
- F: __dxlog_file5,
971
- L: 153,
1204
+ F: __dxlog_file6,
1205
+ L: 155,
972
1206
  S: this,
973
1207
  C: (f, a) => f(...a)
974
1208
  });
975
1209
  continue;
976
1210
  }
977
1211
  if (objectDocumentHandle?.url === automergeUrl) {
978
- import_log4.log.warn("object document was already loaded", logMeta, {
979
- F: __dxlog_file5,
980
- L: 160,
1212
+ import_log5.log.warn("object document was already loaded", logMeta, {
1213
+ F: __dxlog_file6,
1214
+ L: 162,
981
1215
  S: this,
982
1216
  C: (f, a) => f(...a)
983
1217
  });
984
1218
  continue;
985
1219
  }
986
1220
  const handle = this._repo.find(automergeUrl);
987
- import_log4.log.debug("document loading triggered", logMeta, {
988
- F: __dxlog_file5,
989
- L: 164,
1221
+ import_log5.log.debug("document loading triggered", logMeta, {
1222
+ F: __dxlog_file6,
1223
+ L: 166,
990
1224
  S: this,
991
1225
  C: (f, a) => f(...a)
992
1226
  });
@@ -999,17 +1233,17 @@ var AutomergeDocumentLoaderImpl = class {
999
1233
  while (true) {
1000
1234
  try {
1001
1235
  await (0, import_debug.warnAfterTimeout)(5e3, "Automerge root doc load timeout (AutomergeDb)", async () => {
1002
- await (0, import_context3.cancelWithContext)(ctx, docHandle.whenReady());
1236
+ await (0, import_context4.cancelWithContext)(ctx, docHandle.whenReady());
1003
1237
  });
1004
1238
  break;
1005
1239
  } catch (err) {
1006
1240
  if (`${err}`.includes("Timeout")) {
1007
- import_log4.log.info("wraparound", {
1241
+ import_log5.log.info("wraparound", {
1008
1242
  id: docHandle.documentId,
1009
1243
  state: docHandle.state
1010
1244
  }, {
1011
- F: __dxlog_file5,
1012
- L: 180,
1245
+ F: __dxlog_file6,
1246
+ L: 182,
1013
1247
  S: this,
1014
1248
  C: (f, a) => f(...a)
1015
1249
  });
@@ -1049,9 +1283,9 @@ var AutomergeDocumentLoaderImpl = class {
1049
1283
  docUrl: handle.url
1050
1284
  };
1051
1285
  if (this.onObjectDocumentLoaded.listenerCount() === 0) {
1052
- import_log4.log.info("document loaded after all listeners were removed", logMeta, {
1053
- F: __dxlog_file5,
1054
- L: 216,
1286
+ import_log5.log.info("document loaded after all listeners were removed", logMeta, {
1287
+ F: __dxlog_file6,
1288
+ L: 218,
1055
1289
  S: this,
1056
1290
  C: (f, a) => f(...a)
1057
1291
  });
@@ -1059,9 +1293,9 @@ var AutomergeDocumentLoaderImpl = class {
1059
1293
  }
1060
1294
  const objectDocHandle = this._objectDocumentHandles.get(objectId);
1061
1295
  if (objectDocHandle?.url !== handle.url) {
1062
- import_log4.log.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1063
- F: __dxlog_file5,
1064
- L: 221,
1296
+ import_log5.log.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1297
+ F: __dxlog_file6,
1298
+ L: 223,
1065
1299
  S: this,
1066
1300
  C: (f, a) => f(...a)
1067
1301
  });
@@ -1073,14 +1307,14 @@ var AutomergeDocumentLoaderImpl = class {
1073
1307
  });
1074
1308
  } catch (err) {
1075
1309
  const shouldRetryLoading = this.onObjectDocumentLoaded.listenerCount() > 0;
1076
- import_log4.log.warn("failed to load a document", {
1310
+ import_log5.log.warn("failed to load a document", {
1077
1311
  objectId,
1078
1312
  automergeUrl: handle.url,
1079
1313
  retryLoading: shouldRetryLoading,
1080
1314
  err
1081
1315
  }, {
1082
- F: __dxlog_file5,
1083
- L: 227,
1316
+ F: __dxlog_file6,
1317
+ L: 229,
1084
1318
  S: this,
1085
1319
  C: (f, a) => f(...a)
1086
1320
  });
@@ -1090,12 +1324,12 @@ var AutomergeDocumentLoaderImpl = class {
1090
1324
  }
1091
1325
  }
1092
1326
  };
1093
- _ts_decorate2([
1327
+ _ts_decorate3([
1094
1328
  import_tracing2.trace.span({
1095
1329
  showInBrowserTimeline: true
1096
1330
  })
1097
1331
  ], AutomergeDocumentLoaderImpl.prototype, "loadSpaceRootDocHandle", null);
1098
- AutomergeDocumentLoaderImpl = _ts_decorate2([
1332
+ AutomergeDocumentLoaderImpl = _ts_decorate3([
1099
1333
  import_tracing2.trace.resource()
1100
1334
  ], AutomergeDocumentLoaderImpl);
1101
1335
  // Annotate the CommonJS export names for ESM import in node: