@dxos/echo-pipeline 0.6.1-main.2c1a092 → 0.6.1-main.358d1ca

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 (42) hide show
  1. package/dist/lib/browser/{chunk-HS77A4I4.mjs → chunk-DMUP426Q.mjs} +27 -3
  2. package/dist/lib/browser/chunk-DMUP426Q.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +343 -217
  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 +1 -1
  7. package/dist/lib/node/{chunk-Y5U7UXEL.cjs → chunk-NH5WJKOW.cjs} +30 -6
  8. package/dist/lib/node/chunk-NH5WJKOW.cjs.map +7 -0
  9. package/dist/lib/node/index.cjs +383 -263
  10. package/dist/lib/node/index.cjs.map +4 -4
  11. package/dist/lib/node/meta.json +1 -1
  12. package/dist/lib/node/testing/index.cjs +11 -11
  13. package/dist/types/src/automerge/automerge-host.d.ts +15 -10
  14. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -1
  15. package/dist/types/src/automerge/echo-network-adapter.d.ts +2 -2
  16. package/dist/types/src/automerge/echo-network-adapter.d.ts.map +1 -1
  17. package/dist/types/src/automerge/echo-network-adapter.test.d.ts +2 -0
  18. package/dist/types/src/automerge/echo-network-adapter.test.d.ts.map +1 -0
  19. package/dist/types/src/automerge/echo-replicator.d.ts +5 -6
  20. package/dist/types/src/automerge/echo-replicator.d.ts.map +1 -1
  21. package/dist/types/src/automerge/heads-store.d.ts +13 -0
  22. package/dist/types/src/automerge/heads-store.d.ts.map +1 -0
  23. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts +35 -0
  24. package/dist/types/src/automerge/mesh-echo-replicator-connection.d.ts.map +1 -0
  25. package/dist/types/src/automerge/mesh-echo-replicator.d.ts +2 -2
  26. package/dist/types/src/automerge/mesh-echo-replicator.d.ts.map +1 -1
  27. package/dist/types/src/db-host/data-service.d.ts +12 -3
  28. package/dist/types/src/db-host/data-service.d.ts.map +1 -1
  29. package/package.json +33 -33
  30. package/src/automerge/automerge-doc-loader.ts +1 -1
  31. package/src/automerge/automerge-host.test.ts +34 -17
  32. package/src/automerge/automerge-host.ts +86 -22
  33. package/src/automerge/automerge-repo.test.ts +117 -1
  34. package/src/automerge/echo-network-adapter.test.ts +131 -0
  35. package/src/automerge/echo-network-adapter.ts +10 -6
  36. package/src/automerge/echo-replicator.ts +6 -9
  37. package/src/automerge/heads-store.ts +39 -0
  38. package/src/automerge/mesh-echo-replicator-connection.ts +130 -0
  39. package/src/automerge/mesh-echo-replicator.ts +15 -123
  40. package/src/db-host/data-service.ts +51 -3
  41. package/dist/lib/browser/chunk-HS77A4I4.mjs.map +0 -7
  42. package/dist/lib/node/chunk-Y5U7UXEL.cjs.map +0 -7
@@ -25,20 +25,22 @@ import {
25
25
  mapTimeframeToFeedIndexes,
26
26
  startAfter,
27
27
  valueEncoding
28
- } from "./chunk-HS77A4I4.mjs";
28
+ } from "./chunk-DMUP426Q.mjs";
29
29
 
30
30
  // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
31
31
  import { Event, asyncTimeout } from "@dxos/async";
32
- import { next as automerge, getBackend, getHeads, isAutomerge, save } from "@dxos/automerge/automerge";
32
+ import { next as automerge, getBackend, getHeads, isAutomerge, save, equals as headsEquals } from "@dxos/automerge/automerge";
33
33
  import { Repo } from "@dxos/automerge/automerge-repo";
34
- import { Context, cancelWithContext } from "@dxos/context";
34
+ import { Context, Resource as Resource2, cancelWithContext } from "@dxos/context";
35
+ import { invariant as invariant3 } from "@dxos/invariant";
35
36
  import { PublicKey } from "@dxos/keys";
37
+ import { log as log2 } from "@dxos/log";
36
38
  import { objectPointerCodec } from "@dxos/protocols";
37
39
  import { trace } from "@dxos/tracing";
38
40
  import { mapValues } from "@dxos/util";
39
41
 
40
42
  // packages/core/echo/echo-pipeline/src/automerge/echo-network-adapter.ts
41
- import { Trigger, synchronized } from "@dxos/async";
43
+ import { synchronized, Trigger } from "@dxos/async";
42
44
  import { NetworkAdapter } from "@dxos/automerge/automerge-repo";
43
45
  import { LifecycleState } from "@dxos/context";
44
46
  import { invariant } from "@dxos/invariant";
@@ -87,19 +89,13 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
87
89
  disconnect() {
88
90
  }
89
91
  async open() {
90
- invariant(this._lifecycleState === LifecycleState.CLOSED, void 0, {
91
- F: __dxlog_file,
92
- L: 60,
93
- S: this,
94
- A: [
95
- "this._lifecycleState === LifecycleState.CLOSED",
96
- ""
97
- ]
98
- });
92
+ if (this._lifecycleState === LifecycleState.OPEN) {
93
+ return;
94
+ }
99
95
  this._lifecycleState = LifecycleState.OPEN;
100
96
  log("emit ready", void 0, {
101
97
  F: __dxlog_file,
102
- L: 63,
98
+ L: 65,
103
99
  S: this,
104
100
  C: (f, a) => f(...a)
105
101
  });
@@ -108,15 +104,9 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
108
104
  });
109
105
  }
110
106
  async close() {
111
- invariant(this._lifecycleState === LifecycleState.OPEN, void 0, {
112
- F: __dxlog_file,
113
- L: 71,
114
- S: this,
115
- A: [
116
- "this._lifecycleState === LifecycleState.OPEN",
117
- ""
118
- ]
119
- });
107
+ if (this._lifecycleState === LifecycleState.CLOSED) {
108
+ return;
109
+ }
120
110
  for (const replicator of this._replicators) {
121
111
  await replicator.disconnect();
122
112
  }
@@ -131,7 +121,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
131
121
  async addReplicator(replicator) {
132
122
  invariant(this._lifecycleState === LifecycleState.OPEN, void 0, {
133
123
  F: __dxlog_file,
134
- L: 87,
124
+ L: 91,
135
125
  S: this,
136
126
  A: [
137
127
  "this._lifecycleState === LifecycleState.OPEN",
@@ -140,7 +130,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
140
130
  });
141
131
  invariant(this.peerId, void 0, {
142
132
  F: __dxlog_file,
143
- L: 88,
133
+ L: 92,
144
134
  S: this,
145
135
  A: [
146
136
  "this.peerId",
@@ -149,7 +139,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
149
139
  });
150
140
  invariant(!this._replicators.has(replicator), void 0, {
151
141
  F: __dxlog_file,
152
- L: 89,
142
+ L: 93,
153
143
  S: this,
154
144
  A: [
155
145
  "!this._replicators.has(replicator)",
@@ -168,7 +158,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
168
158
  async removeReplicator(replicator) {
169
159
  invariant(this._lifecycleState === LifecycleState.OPEN, void 0, {
170
160
  F: __dxlog_file,
171
- L: 103,
161
+ L: 107,
172
162
  S: this,
173
163
  A: [
174
164
  "this._lifecycleState === LifecycleState.OPEN",
@@ -177,7 +167,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
177
167
  });
178
168
  invariant(this._replicators.has(replicator), void 0, {
179
169
  F: __dxlog_file,
180
- L: 104,
170
+ L: 108,
181
171
  S: this,
182
172
  A: [
183
173
  "this._replicators.has(replicator)",
@@ -187,25 +177,25 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
187
177
  await replicator.disconnect();
188
178
  this._replicators.delete(replicator);
189
179
  }
190
- async shouldAdvertize(peerId, params) {
180
+ async shouldAdvertise(peerId, params) {
191
181
  const connection = this._connections.get(peerId);
192
182
  if (!connection) {
193
183
  return false;
194
184
  }
195
- return connection.connection.shouldAdvertize(params);
185
+ return connection.connection.shouldAdvertise(params);
196
186
  }
197
187
  _onConnectionOpen(connection) {
198
188
  log("Connection opened", {
199
189
  peerId: connection.peerId
200
190
  }, {
201
191
  F: __dxlog_file,
202
- L: 119,
192
+ L: 123,
203
193
  S: this,
204
194
  C: (f, a) => f(...a)
205
195
  });
206
196
  invariant(!this._connections.has(connection.peerId), void 0, {
207
197
  F: __dxlog_file,
208
- L: 120,
198
+ L: 124,
209
199
  S: this,
210
200
  A: [
211
201
  "!this._connections.has(connection.peerId as PeerId)",
@@ -234,7 +224,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
234
224
  if (connectionEntry.isOpen) {
235
225
  log.catch(err, void 0, {
236
226
  F: __dxlog_file,
237
- L: 139,
227
+ L: 143,
238
228
  S: this,
239
229
  C: (f, a) => f(...a)
240
230
  });
@@ -245,7 +235,7 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
245
235
  peerId: connection.peerId
246
236
  }, {
247
237
  F: __dxlog_file,
248
- L: 144,
238
+ L: 148,
249
239
  S: this,
250
240
  C: (f, a) => f(...a)
251
241
  });
@@ -260,14 +250,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
260
250
  peerId: connection.peerId
261
251
  }, {
262
252
  F: __dxlog_file,
263
- L: 153,
253
+ L: 157,
264
254
  S: this,
265
255
  C: (f, a) => f(...a)
266
256
  });
267
257
  const entry = this._connections.get(connection.peerId);
268
258
  invariant(entry, void 0, {
269
259
  F: __dxlog_file,
270
- L: 155,
260
+ L: 159,
271
261
  S: this,
272
262
  A: [
273
263
  "entry",
@@ -284,14 +274,14 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
284
274
  peerId: connection.peerId
285
275
  }, {
286
276
  F: __dxlog_file,
287
- L: 161,
277
+ L: 165,
288
278
  S: this,
289
279
  C: (f, a) => f(...a)
290
280
  });
291
281
  const entry = this._connections.get(connection.peerId);
292
282
  invariant(entry, void 0, {
293
283
  F: __dxlog_file,
294
- L: 163,
284
+ L: 167,
295
285
  S: this,
296
286
  A: [
297
287
  "entry",
@@ -304,13 +294,13 @@ var EchoNetworkAdapter = class extends NetworkAdapter {
304
294
  });
305
295
  void entry.reader.cancel().catch((err) => log.catch(err, void 0, {
306
296
  F: __dxlog_file,
307
- L: 168,
297
+ L: 172,
308
298
  S: this,
309
299
  C: (f, a) => f(...a)
310
300
  }));
311
301
  void entry.writer.abort().catch((err) => log.catch(err, void 0, {
312
302
  F: __dxlog_file,
313
- L: 169,
303
+ L: 173,
314
304
  S: this,
315
305
  C: (f, a) => f(...a)
316
306
  }));
@@ -341,6 +331,34 @@ var createEchoPeerMetadata = () => ({
341
331
  });
342
332
  var isEchoPeerMetadata = (metadata) => metadata?.dxos_peerSource === "EchoNetworkAdapter";
343
333
 
334
+ // packages/core/echo/echo-pipeline/src/automerge/heads-store.ts
335
+ import { headsEncoding } from "@dxos/indexing";
336
+ var HeadsStore = class {
337
+ constructor({ db }) {
338
+ this._db = db;
339
+ }
340
+ setHeads(documentId, heads, batch) {
341
+ batch.put(documentId, heads, {
342
+ sublevel: this._db,
343
+ keyEncoding: "utf8",
344
+ valueEncoding: headsEncoding
345
+ });
346
+ }
347
+ async getHeads(documentId) {
348
+ try {
349
+ return await this._db.get(documentId, {
350
+ keyEncoding: "utf8",
351
+ valueEncoding: headsEncoding
352
+ });
353
+ } catch (err) {
354
+ if (err.notFound) {
355
+ return void 0;
356
+ }
357
+ throw err;
358
+ }
359
+ }
360
+ };
361
+
344
362
  // packages/core/echo/echo-pipeline/src/automerge/leveldb-storage-adapter.ts
345
363
  import { LifecycleState as LifecycleState2, Resource } from "@dxos/context";
346
364
  var LevelDBStorageAdapter = class extends Resource {
@@ -587,25 +605,26 @@ function _ts_decorate2(decorators, target, key, desc) {
587
605
  return c > 3 && r && Object.defineProperty(target, key, r), r;
588
606
  }
589
607
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
590
- var AutomergeHost = class {
608
+ var AutomergeHost = class extends Resource2 {
591
609
  constructor({ db, indexMetadataStore }) {
592
- this._ctx = new Context(void 0, {
593
- F: __dxlog_file3,
594
- L: 71
595
- });
610
+ super();
596
611
  this._echoNetworkAdapter = new EchoNetworkAdapter({
597
612
  getContainingSpaceForDocument: this._getContainingSpaceForDocument.bind(this)
598
613
  });
614
+ this._db = db;
599
615
  this._storage = new LevelDBStorageAdapter({
600
- db,
616
+ db: db.sublevel("automerge"),
601
617
  callbacks: {
602
618
  beforeSave: async (params) => this._beforeSave(params),
603
619
  afterSave: async () => this._afterSave()
604
620
  }
605
621
  });
622
+ this._headsStore = new HeadsStore({
623
+ db: db.sublevel("heads")
624
+ });
606
625
  this._indexMetadataStore = indexMetadataStore;
607
626
  }
608
- async open() {
627
+ async _open() {
609
628
  this._peerId = `host-${PublicKey.random().toHex()}`;
610
629
  await this._storage.open?.();
611
630
  this._clientNetwork = new LocalHostNetworkAdapter();
@@ -625,7 +644,7 @@ var AutomergeHost = class {
625
644
  await this._clientNetwork.whenConnected();
626
645
  await this._echoNetworkAdapter.whenConnected();
627
646
  }
628
- async close() {
647
+ async _close() {
629
648
  await this._storage.close?.();
630
649
  await this._clientNetwork.close();
631
650
  await this._echoNetworkAdapter.close();
@@ -637,6 +656,9 @@ var AutomergeHost = class {
637
656
  get repo() {
638
657
  return this._repo;
639
658
  }
659
+ get loadedDocsCount() {
660
+ return Object.keys(this._repo.handles).length;
661
+ }
640
662
  async addReplicator(replicator) {
641
663
  await this._echoNetworkAdapter.addReplicator(replicator);
642
664
  }
@@ -676,6 +698,73 @@ var AutomergeHost = class {
676
698
  return this._repo.create(initialValue);
677
699
  }
678
700
  }
701
+ async waitUntilHeadsReplicated(heads) {
702
+ await Promise.all(heads.entries?.map(async ({ documentId, heads: heads2 }) => {
703
+ if (!heads2 || heads2.length === 0) {
704
+ return;
705
+ }
706
+ const currentHeads = this.getHeads(documentId);
707
+ if (currentHeads !== null && headsEquals(currentHeads, heads2)) {
708
+ return;
709
+ }
710
+ const handle = await this.loadDoc(Context.default(void 0, {
711
+ F: __dxlog_file3,
712
+ L: 207
713
+ }), documentId);
714
+ await waitForHeads(handle, heads2);
715
+ }) ?? []);
716
+ await this._repo.flush(heads.entries?.map((entry) => entry.documentId) ?? []);
717
+ }
718
+ async reIndexHeads(documentIds) {
719
+ for (const documentId of documentIds) {
720
+ log2.info("re-indexing heads for document", {
721
+ documentId
722
+ }, {
723
+ F: __dxlog_file3,
724
+ L: 218,
725
+ S: this,
726
+ C: (f, a) => f(...a)
727
+ });
728
+ const handle = this._repo.find(documentId);
729
+ await handle.whenReady([
730
+ "ready",
731
+ "requesting"
732
+ ]);
733
+ if (handle.inState([
734
+ "requesting"
735
+ ])) {
736
+ log2.warn("document is not available locally, skipping", {
737
+ documentId
738
+ }, {
739
+ F: __dxlog_file3,
740
+ L: 222,
741
+ S: this,
742
+ C: (f, a) => f(...a)
743
+ });
744
+ continue;
745
+ }
746
+ const doc = handle.docSync();
747
+ invariant3(doc, void 0, {
748
+ F: __dxlog_file3,
749
+ L: 227,
750
+ S: this,
751
+ A: [
752
+ "doc",
753
+ ""
754
+ ]
755
+ });
756
+ const heads = getHeads(doc);
757
+ const batch = this._db.batch();
758
+ this._headsStore.setHeads(documentId, heads, batch);
759
+ await batch.write();
760
+ }
761
+ log2.info("done re-indexing heads", void 0, {
762
+ F: __dxlog_file3,
763
+ L: 234,
764
+ S: this,
765
+ C: (f, a) => f(...a)
766
+ });
767
+ }
679
768
  // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
680
769
  // Hosts, running in the worker, don't share documents unless requested by other peers.
681
770
  // NOTE: If both peers return sharePolicy=false the replication will not happen
@@ -689,7 +778,7 @@ var AutomergeHost = class {
689
778
  }
690
779
  const peerMetadata = this.repo.peerMetadataByPeerId[peerId];
691
780
  if (isEchoPeerMetadata(peerMetadata)) {
692
- return this._echoNetworkAdapter.shouldAdvertize(peerId, {
781
+ return this._echoNetworkAdapter.shouldAdvertise(peerId, {
693
782
  documentId
694
783
  });
695
784
  }
@@ -705,7 +794,8 @@ var AutomergeHost = class {
705
794
  return;
706
795
  }
707
796
  const spaceKey = getSpaceKeyFromDoc(doc) ?? void 0;
708
- const lastAvailableHash = getHeads(doc);
797
+ const heads = getHeads(doc);
798
+ this._headsStore.setHeads(handle.documentId, heads, batch);
709
799
  const objectIds = Object.keys(doc.objects ?? {});
710
800
  const encodedIds = objectIds.map((objectId) => objectPointerCodec.encode({
711
801
  documentId: handle.documentId,
@@ -714,7 +804,7 @@ var AutomergeHost = class {
714
804
  }));
715
805
  const idToLastHash = new Map(encodedIds.map((id) => [
716
806
  id,
717
- lastAvailableHash
807
+ heads
718
808
  ]));
719
809
  this._indexMetadataStore.markDirty(idToLastHash, batch);
720
810
  }
@@ -763,18 +853,30 @@ var AutomergeHost = class {
763
853
  /**
764
854
  * Flush documents to disk.
765
855
  */
766
- async flush({ states }) {
856
+ async flush({ states } = {}) {
767
857
  if (states) {
768
858
  await Promise.all(states.map(async ({ heads, documentId }) => {
769
859
  if (!heads) {
770
860
  return;
771
861
  }
772
- const handle = this.repo.handles[documentId] ?? this._repo.find(documentId);
862
+ const handle = this._repo.handles[documentId] ?? this._repo.find(documentId);
773
863
  await waitForHeads(handle, heads);
774
864
  }) ?? []);
775
865
  }
776
866
  await this._repo.flush(states?.map(({ documentId }) => documentId));
777
867
  }
868
+ async getHeads(documentId) {
869
+ const handle = this._repo.handles[documentId];
870
+ if (handle) {
871
+ const doc = handle.docSync();
872
+ if (!doc) {
873
+ return void 0;
874
+ }
875
+ return getHeads(doc);
876
+ } else {
877
+ return this._headsStore.getHeads(documentId);
878
+ }
879
+ }
778
880
  /**
779
881
  * Host <-> Client sync.
780
882
  */
@@ -823,18 +925,15 @@ var getSpaceKeyFromDoc = (doc) => {
823
925
  return String(rawSpaceKey);
824
926
  };
825
927
  var waitForHeads = async (handle, heads) => {
826
- await handle.whenReady();
827
928
  const unavailableHeads = new Set(heads);
929
+ await handle.whenReady();
828
930
  await Event.wrap(handle, "change").waitForCondition(() => {
829
931
  for (const changeHash of unavailableHeads.values()) {
830
932
  if (changeIsPresentInDoc(handle.docSync(), changeHash)) {
831
933
  unavailableHeads.delete(changeHash);
832
934
  }
833
935
  }
834
- if (unavailableHeads.size === 0) {
835
- return true;
836
- }
837
- return false;
936
+ return unavailableHeads.size === 0;
838
937
  });
839
938
  };
840
939
  var changeIsPresentInDoc = (doc, changeHash) => {
@@ -847,8 +946,8 @@ import { interpretAsDocumentId } from "@dxos/automerge/automerge-repo";
847
946
  import { cancelWithContext as cancelWithContext2 } from "@dxos/context";
848
947
  import { warnAfterTimeout } from "@dxos/debug";
849
948
  import { SpaceDocVersion } from "@dxos/echo-protocol";
850
- import { invariant as invariant3 } from "@dxos/invariant";
851
- import { log as log2 } from "@dxos/log";
949
+ import { invariant as invariant4 } from "@dxos/invariant";
950
+ import { log as log3 } from "@dxos/log";
852
951
  import { trace as trace2 } from "@dxos/tracing";
853
952
  function _ts_decorate3(decorators, target, key, desc) {
854
953
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -886,7 +985,7 @@ var AutomergeDocumentLoaderImpl = class {
886
985
  }
887
986
  const existingDocHandle = await this._initDocHandle(ctx, spaceState.rootUrl);
888
987
  const doc = existingDocHandle.docSync();
889
- invariant3(doc, void 0, {
988
+ invariant4(doc, void 0, {
890
989
  F: __dxlog_file4,
891
990
  L: 84,
892
991
  S: this,
@@ -895,7 +994,7 @@ var AutomergeDocumentLoaderImpl = class {
895
994
  ""
896
995
  ]
897
996
  });
898
- invariant3(doc.version === SpaceDocVersion.CURRENT, void 0, {
997
+ invariant4(doc.version === SpaceDocVersion.CURRENT, void 0, {
899
998
  F: __dxlog_file4,
900
999
  L: 85,
901
1000
  S: this,
@@ -916,7 +1015,7 @@ var AutomergeDocumentLoaderImpl = class {
916
1015
  let hasUrlsToLoad = false;
917
1016
  const urlsToLoad = {};
918
1017
  for (const objectId of objectIds) {
919
- invariant3(this._spaceRootDocHandle, void 0, {
1018
+ invariant4(this._spaceRootDocHandle, void 0, {
920
1019
  F: __dxlog_file4,
921
1020
  L: 97,
922
1021
  S: this,
@@ -929,7 +1028,7 @@ var AutomergeDocumentLoaderImpl = class {
929
1028
  continue;
930
1029
  }
931
1030
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
932
- invariant3(spaceRootDoc, void 0, {
1031
+ invariant4(spaceRootDoc, void 0, {
933
1032
  F: __dxlog_file4,
934
1033
  L: 102,
935
1034
  S: this,
@@ -941,7 +1040,7 @@ var AutomergeDocumentLoaderImpl = class {
941
1040
  const documentUrl = (spaceRootDoc.links ?? {})[objectId];
942
1041
  if (documentUrl == null) {
943
1042
  this._objectsPendingDocumentLoad.add(objectId);
944
- log2.info("loading delayed until object links are initialized", {
1043
+ log3.info("loading delayed until object links are initialized", {
945
1044
  objectId
946
1045
  }, {
947
1046
  F: __dxlog_file4,
@@ -959,7 +1058,7 @@ var AutomergeDocumentLoaderImpl = class {
959
1058
  }
960
1059
  }
961
1060
  getObjectDocumentId(objectId) {
962
- invariant3(this._spaceRootDocHandle, void 0, {
1061
+ invariant4(this._spaceRootDocHandle, void 0, {
963
1062
  F: __dxlog_file4,
964
1063
  L: 118,
965
1064
  S: this,
@@ -969,7 +1068,7 @@ var AutomergeDocumentLoaderImpl = class {
969
1068
  ]
970
1069
  });
971
1070
  const spaceRootDoc = this._spaceRootDocHandle.docSync();
972
- invariant3(spaceRootDoc, void 0, {
1071
+ invariant4(spaceRootDoc, void 0, {
973
1072
  F: __dxlog_file4,
974
1073
  L: 120,
975
1074
  S: this,
@@ -993,7 +1092,7 @@ var AutomergeDocumentLoaderImpl = class {
993
1092
  linksAwaitingLoad.forEach(([objectId]) => this._objectsPendingDocumentLoad.delete(objectId));
994
1093
  }
995
1094
  getSpaceRootDocHandle() {
996
- invariant3(this._spaceRootDocHandle, void 0, {
1095
+ invariant4(this._spaceRootDocHandle, void 0, {
997
1096
  F: __dxlog_file4,
998
1097
  L: 140,
999
1098
  S: this,
@@ -1005,7 +1104,7 @@ var AutomergeDocumentLoaderImpl = class {
1005
1104
  return this._spaceRootDocHandle;
1006
1105
  }
1007
1106
  createDocumentForObject(objectId) {
1008
- invariant3(this._spaceRootDocHandle, void 0, {
1107
+ invariant4(this._spaceRootDocHandle, void 0, {
1009
1108
  F: __dxlog_file4,
1010
1109
  L: 145,
1011
1110
  S: this,
@@ -1047,7 +1146,7 @@ var AutomergeDocumentLoaderImpl = class {
1047
1146
  };
1048
1147
  const objectDocumentHandle = this._objectDocumentHandles.get(objectId);
1049
1148
  if (objectDocumentHandle != null && objectDocumentHandle.url !== automergeUrl) {
1050
- log2.warn("object already inlined in a different document, ignoring the link", {
1149
+ log3.warn("object already inlined in a different document, ignoring the link", {
1051
1150
  ...logMeta,
1052
1151
  actualDocumentUrl: objectDocumentHandle.url
1053
1152
  }, {
@@ -1059,7 +1158,7 @@ var AutomergeDocumentLoaderImpl = class {
1059
1158
  continue;
1060
1159
  }
1061
1160
  if (objectDocumentHandle?.url === automergeUrl) {
1062
- log2.warn("object document was already loaded", logMeta, {
1161
+ log3.warn("object document was already loaded", logMeta, {
1063
1162
  F: __dxlog_file4,
1064
1163
  L: 184,
1065
1164
  S: this,
@@ -1068,7 +1167,7 @@ var AutomergeDocumentLoaderImpl = class {
1068
1167
  continue;
1069
1168
  }
1070
1169
  const handle = this._repo.find(automergeUrl);
1071
- log2.debug("document loading triggered", logMeta, {
1170
+ log3.debug("document loading triggered", logMeta, {
1072
1171
  F: __dxlog_file4,
1073
1172
  L: 188,
1074
1173
  S: this,
@@ -1088,7 +1187,7 @@ var AutomergeDocumentLoaderImpl = class {
1088
1187
  break;
1089
1188
  } catch (err) {
1090
1189
  if (`${err}`.includes("Timeout")) {
1091
- log2.info("wraparound", {
1190
+ log3.info("wraparound", {
1092
1191
  id: docHandle.documentId,
1093
1192
  state: docHandle.state
1094
1193
  }, {
@@ -1117,15 +1216,13 @@ var AutomergeDocumentLoaderImpl = class {
1117
1216
  }
1118
1217
  async _createObjectOnDocumentLoad(handle, objectId) {
1119
1218
  try {
1120
- await handle.doc([
1121
- "ready"
1122
- ]);
1219
+ await handle.whenReady();
1123
1220
  const logMeta = {
1124
1221
  objectId,
1125
1222
  docUrl: handle.url
1126
1223
  };
1127
1224
  if (this.onObjectDocumentLoaded.listenerCount() === 0) {
1128
- log2.info("document loaded after all listeners were removed", logMeta, {
1225
+ log3.info("document loaded after all listeners were removed", logMeta, {
1129
1226
  F: __dxlog_file4,
1130
1227
  L: 231,
1131
1228
  S: this,
@@ -1135,7 +1232,7 @@ var AutomergeDocumentLoaderImpl = class {
1135
1232
  }
1136
1233
  const objectDocHandle = this._objectDocumentHandles.get(objectId);
1137
1234
  if (objectDocHandle?.url !== handle.url) {
1138
- log2.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1235
+ log3.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1139
1236
  F: __dxlog_file4,
1140
1237
  L: 236,
1141
1238
  S: this,
@@ -1149,7 +1246,7 @@ var AutomergeDocumentLoaderImpl = class {
1149
1246
  });
1150
1247
  } catch (err) {
1151
1248
  const shouldRetryLoading = this.onObjectDocumentLoaded.listenerCount() > 0;
1152
- log2.warn("failed to load a document", {
1249
+ log3.warn("failed to load a document", {
1153
1250
  objectId,
1154
1251
  automergeUrl: handle.url,
1155
1252
  retryLoading: shouldRetryLoading,
@@ -1176,14 +1273,134 @@ AutomergeDocumentLoaderImpl = _ts_decorate3([
1176
1273
  ], AutomergeDocumentLoaderImpl);
1177
1274
 
1178
1275
  // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1179
- import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
1180
- import { Resource as Resource2 } from "@dxos/context";
1181
- import { invariant as invariant4 } from "@dxos/invariant";
1276
+ import { invariant as invariant6 } from "@dxos/invariant";
1182
1277
  import { PublicKey as PublicKey2 } from "@dxos/keys";
1183
- import { log as log3 } from "@dxos/log";
1184
- import { AutomergeReplicator } from "@dxos/teleport-extension-automerge-replicator";
1278
+ import { log as log5 } from "@dxos/log";
1185
1279
  import { ComplexMap, ComplexSet, defaultMap } from "@dxos/util";
1186
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1280
+
1281
+ // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts
1282
+ import { cbor as cbor2 } from "@dxos/automerge/automerge-repo";
1283
+ import { Resource as Resource3 } from "@dxos/context";
1284
+ import { invariant as invariant5 } from "@dxos/invariant";
1285
+ import { log as log4 } from "@dxos/log";
1286
+ import { AutomergeReplicator } from "@dxos/teleport-extension-automerge-replicator";
1287
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator-connection.ts";
1288
+ var DEFAULT_FACTORY = (params) => new AutomergeReplicator(...params);
1289
+ var MeshReplicatorConnection = class extends Resource3 {
1290
+ constructor(_params) {
1291
+ super();
1292
+ this._params = _params;
1293
+ this.remoteDeviceKey = null;
1294
+ this._remotePeerId = null;
1295
+ this._isEnabled = false;
1296
+ let readableStreamController;
1297
+ this.readable = new ReadableStream({
1298
+ start: (controller) => {
1299
+ readableStreamController = controller;
1300
+ this._ctx.onDispose(() => controller.close());
1301
+ }
1302
+ });
1303
+ this.writable = new WritableStream({
1304
+ write: async (message, controller) => {
1305
+ invariant5(this._isEnabled, "Writing to a disabled connection", {
1306
+ F: __dxlog_file5,
1307
+ L: 47,
1308
+ S: this,
1309
+ A: [
1310
+ "this._isEnabled",
1311
+ "'Writing to a disabled connection'"
1312
+ ]
1313
+ });
1314
+ try {
1315
+ await this.replicatorExtension.sendSyncMessage({
1316
+ payload: cbor2.encode(message)
1317
+ });
1318
+ } catch (err) {
1319
+ controller.error(err);
1320
+ this._disconnectIfEnabled();
1321
+ }
1322
+ }
1323
+ });
1324
+ const createAutomergeReplicator = this._params.replicatorFactory ?? DEFAULT_FACTORY;
1325
+ this.replicatorExtension = createAutomergeReplicator([
1326
+ {
1327
+ peerId: this._params.ownPeerId
1328
+ },
1329
+ {
1330
+ onStartReplication: async (info, remotePeerId) => {
1331
+ this.remoteDeviceKey = remotePeerId;
1332
+ this._remotePeerId = info.id;
1333
+ log4("onStartReplication", {
1334
+ id: info.id,
1335
+ thisPeerId: this.peerId,
1336
+ remotePeerId: remotePeerId.toHex()
1337
+ }, {
1338
+ F: __dxlog_file5,
1339
+ L: 81,
1340
+ S: this,
1341
+ C: (f, a) => f(...a)
1342
+ });
1343
+ this._params.onRemoteConnected();
1344
+ },
1345
+ onSyncMessage: async ({ payload }) => {
1346
+ if (!this._isEnabled) {
1347
+ return;
1348
+ }
1349
+ const message = cbor2.decode(payload);
1350
+ readableStreamController.enqueue(message);
1351
+ },
1352
+ onClose: async () => {
1353
+ this._disconnectIfEnabled();
1354
+ }
1355
+ }
1356
+ ]);
1357
+ }
1358
+ _disconnectIfEnabled() {
1359
+ if (this._isEnabled) {
1360
+ this._params.onRemoteDisconnected();
1361
+ }
1362
+ }
1363
+ get peerId() {
1364
+ invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1365
+ F: __dxlog_file5,
1366
+ L: 107,
1367
+ S: this,
1368
+ A: [
1369
+ "this._remotePeerId != null",
1370
+ "'Remote peer has not connected yet.'"
1371
+ ]
1372
+ });
1373
+ return this._remotePeerId;
1374
+ }
1375
+ async shouldAdvertise(params) {
1376
+ return this._params.shouldAdvertise(params);
1377
+ }
1378
+ /**
1379
+ * Start exchanging messages with the remote peer.
1380
+ * Call after the remote peer has connected.
1381
+ */
1382
+ enable() {
1383
+ invariant5(this._remotePeerId != null, "Remote peer has not connected yet.", {
1384
+ F: __dxlog_file5,
1385
+ L: 120,
1386
+ S: this,
1387
+ A: [
1388
+ "this._remotePeerId != null",
1389
+ "'Remote peer has not connected yet.'"
1390
+ ]
1391
+ });
1392
+ this._isEnabled = true;
1393
+ }
1394
+ /**
1395
+ * Stop exchanging messages with the remote peer.
1396
+ */
1397
+ disable() {
1398
+ this._isEnabled = false;
1399
+ }
1400
+ };
1401
+
1402
+ // packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts
1403
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/mesh-echo-replicator.ts";
1187
1404
  var MeshEchoReplicator = class {
1188
1405
  constructor() {
1189
1406
  this._connections = /* @__PURE__ */ new Set();
@@ -1208,10 +1425,10 @@ var MeshEchoReplicator = class {
1208
1425
  this._connectionsPerPeer.clear();
1209
1426
  this._context = null;
1210
1427
  }
1211
- createExtension() {
1212
- invariant4(this._context, void 0, {
1213
- F: __dxlog_file5,
1214
- L: 54,
1428
+ createExtension(extensionFactory) {
1429
+ invariant6(this._context, void 0, {
1430
+ F: __dxlog_file6,
1431
+ L: 51,
1215
1432
  S: this,
1216
1433
  A: [
1217
1434
  "this._context",
@@ -1220,18 +1437,19 @@ var MeshEchoReplicator = class {
1220
1437
  });
1221
1438
  const connection = new MeshReplicatorConnection({
1222
1439
  ownPeerId: this._context.peerId,
1440
+ replicatorFactory: extensionFactory,
1223
1441
  onRemoteConnected: async () => {
1224
- log3("onRemoteConnected", {
1442
+ log5("onRemoteConnected", {
1225
1443
  peerId: connection.peerId
1226
1444
  }, {
1227
- F: __dxlog_file5,
1228
- L: 59,
1445
+ F: __dxlog_file6,
1446
+ L: 57,
1229
1447
  S: this,
1230
1448
  C: (f, a) => f(...a)
1231
1449
  });
1232
- invariant4(this._context, void 0, {
1233
- F: __dxlog_file5,
1234
- L: 60,
1450
+ invariant6(this._context, void 0, {
1451
+ F: __dxlog_file6,
1452
+ L: 58,
1235
1453
  S: this,
1236
1454
  A: [
1237
1455
  "this._context",
@@ -1243,36 +1461,36 @@ var MeshEchoReplicator = class {
1243
1461
  } else {
1244
1462
  this._connectionsPerPeer.set(connection.peerId, connection);
1245
1463
  this._context.onConnectionOpen(connection);
1246
- await connection.enable();
1464
+ connection.enable();
1247
1465
  }
1248
1466
  },
1249
1467
  onRemoteDisconnected: async () => {
1250
- log3("onRemoteDisconnected", {
1468
+ log5("onRemoteDisconnected", {
1251
1469
  peerId: connection.peerId
1252
1470
  }, {
1253
- F: __dxlog_file5,
1254
- L: 71,
1471
+ F: __dxlog_file6,
1472
+ L: 69,
1255
1473
  S: this,
1256
1474
  C: (f, a) => f(...a)
1257
1475
  });
1258
1476
  this._context?.onConnectionClosed(connection);
1259
1477
  this._connectionsPerPeer.delete(connection.peerId);
1260
- await connection.disable();
1478
+ connection.disable();
1261
1479
  this._connections.delete(connection);
1262
1480
  },
1263
- shouldAdvertize: async (params) => {
1264
- log3("shouldAdvertize", {
1481
+ shouldAdvertise: async (params) => {
1482
+ log5("shouldAdvertise", {
1265
1483
  peerId: connection.peerId,
1266
1484
  documentId: params.documentId
1267
1485
  }, {
1268
- F: __dxlog_file5,
1269
- L: 78,
1486
+ F: __dxlog_file6,
1487
+ L: 76,
1270
1488
  S: this,
1271
1489
  C: (f, a) => f(...a)
1272
1490
  });
1273
- invariant4(this._context, void 0, {
1274
- F: __dxlog_file5,
1275
- L: 79,
1491
+ invariant6(this._context, void 0, {
1492
+ F: __dxlog_file6,
1493
+ L: 77,
1276
1494
  S: this,
1277
1495
  A: [
1278
1496
  "this._context",
@@ -1282,12 +1500,12 @@ var MeshEchoReplicator = class {
1282
1500
  try {
1283
1501
  const spaceKey = await this._context.getContainingSpaceForDocument(params.documentId);
1284
1502
  if (!spaceKey) {
1285
- log3("space key not found for share policy check", {
1503
+ log5("space key not found for share policy check", {
1286
1504
  peerId: connection.peerId,
1287
1505
  documentId: params.documentId
1288
1506
  }, {
1289
- F: __dxlog_file5,
1290
- L: 83,
1507
+ F: __dxlog_file6,
1508
+ L: 81,
1291
1509
  S: this,
1292
1510
  C: (f, a) => f(...a)
1293
1511
  });
@@ -1295,19 +1513,19 @@ var MeshEchoReplicator = class {
1295
1513
  }
1296
1514
  const authorizedDevices = this._authorizedDevices.get(spaceKey);
1297
1515
  if (!connection.remoteDeviceKey) {
1298
- log3("device key not found for share policy check", {
1516
+ log5("device key not found for share policy check", {
1299
1517
  peerId: connection.peerId,
1300
1518
  documentId: params.documentId
1301
1519
  }, {
1302
- F: __dxlog_file5,
1303
- L: 93,
1520
+ F: __dxlog_file6,
1521
+ L: 91,
1304
1522
  S: this,
1305
1523
  C: (f, a) => f(...a)
1306
1524
  });
1307
1525
  return false;
1308
1526
  }
1309
1527
  const isAuthorized = authorizedDevices?.has(connection.remoteDeviceKey) ?? false;
1310
- log3("share policy check", {
1528
+ log5("share policy check", {
1311
1529
  localPeer: this._context.peerId,
1312
1530
  remotePeer: connection.peerId,
1313
1531
  documentId: params.documentId,
@@ -1315,16 +1533,16 @@ var MeshEchoReplicator = class {
1315
1533
  spaceKey,
1316
1534
  isAuthorized
1317
1535
  }, {
1318
- F: __dxlog_file5,
1319
- L: 101,
1536
+ F: __dxlog_file6,
1537
+ L: 99,
1320
1538
  S: this,
1321
1539
  C: (f, a) => f(...a)
1322
1540
  });
1323
1541
  return isAuthorized;
1324
1542
  } catch (err) {
1325
- log3.catch(err, void 0, {
1326
- F: __dxlog_file5,
1327
- L: 111,
1543
+ log5.catch(err, void 0, {
1544
+ F: __dxlog_file6,
1545
+ L: 109,
1328
1546
  S: this,
1329
1547
  C: (f, a) => f(...a)
1330
1548
  });
@@ -1336,115 +1554,23 @@ var MeshEchoReplicator = class {
1336
1554
  return connection.replicatorExtension;
1337
1555
  }
1338
1556
  authorizeDevice(spaceKey, deviceKey) {
1339
- log3("authorizeDevice", {
1557
+ log5("authorizeDevice", {
1340
1558
  spaceKey,
1341
1559
  deviceKey
1342
1560
  }, {
1343
- F: __dxlog_file5,
1344
- L: 122,
1561
+ F: __dxlog_file6,
1562
+ L: 120,
1345
1563
  S: this,
1346
1564
  C: (f, a) => f(...a)
1347
1565
  });
1348
1566
  defaultMap(this._authorizedDevices, spaceKey, () => new ComplexSet(PublicKey2.hash)).add(deviceKey);
1349
1567
  for (const connection of this._connections) {
1350
1568
  if (connection.remoteDeviceKey && connection.remoteDeviceKey.equals(deviceKey)) {
1351
- this._context?.onConnectionAuthScopeChanged(connection);
1352
- }
1353
- }
1354
- }
1355
- };
1356
- var MeshReplicatorConnection = class extends Resource2 {
1357
- constructor(_params) {
1358
- super();
1359
- this._params = _params;
1360
- this.remoteDeviceKey = null;
1361
- this._remotePeerId = null;
1362
- this._isEnabled = false;
1363
- let readableStreamController;
1364
- this.readable = new ReadableStream({
1365
- start: (controller) => {
1366
- readableStreamController = controller;
1367
- this._ctx.onDispose(() => controller.close());
1368
- }
1369
- });
1370
- this.writable = new WritableStream({
1371
- write: async (message, controller) => {
1372
- this.replicatorExtension.sendSyncMessage({
1373
- payload: cbor2.encode(message)
1374
- }).catch((err) => {
1375
- controller.error(err);
1376
- });
1377
- }
1378
- });
1379
- this.replicatorExtension = new AutomergeReplicator({
1380
- peerId: this._params.ownPeerId
1381
- }, {
1382
- onStartReplication: async (info, remotePeerId) => {
1383
- this.remoteDeviceKey = remotePeerId;
1384
- this._remotePeerId = info.id;
1385
- log3("onStartReplication", {
1386
- id: info.id,
1387
- thisPeerId: this.peerId,
1388
- remotePeerId: remotePeerId.toHex()
1389
- }, {
1390
- F: __dxlog_file5,
1391
- L: 192,
1392
- S: this,
1393
- C: (f, a) => f(...a)
1394
- });
1395
- await this._params.onRemoteConnected();
1396
- },
1397
- onSyncMessage: async ({ payload }) => {
1398
- if (!this._isEnabled) {
1399
- return;
1400
- }
1401
- const message = cbor2.decode(payload);
1402
- readableStreamController.enqueue(message);
1403
- },
1404
- onClose: async () => {
1405
- if (!this._isEnabled) {
1406
- return;
1569
+ if (this._connectionsPerPeer.has(connection.peerId)) {
1570
+ this._context?.onConnectionAuthScopeChanged(connection);
1407
1571
  }
1408
- await this._params.onRemoteDisconnected();
1409
1572
  }
1410
- });
1411
- }
1412
- get peerId() {
1413
- invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1414
- F: __dxlog_file5,
1415
- L: 215,
1416
- S: this,
1417
- A: [
1418
- "this._remotePeerId != null",
1419
- "'Remote peer has not connected yet.'"
1420
- ]
1421
- });
1422
- return this._remotePeerId;
1423
- }
1424
- async shouldAdvertize(params) {
1425
- return this._params.shouldAdvertize(params);
1426
- }
1427
- /**
1428
- * Start exchanging messages with the remote peer.
1429
- * Call after the remote peer has connected.
1430
- */
1431
- async enable() {
1432
- invariant4(this._remotePeerId != null, "Remote peer has not connected yet.", {
1433
- F: __dxlog_file5,
1434
- L: 228,
1435
- S: this,
1436
- A: [
1437
- "this._remotePeerId != null",
1438
- "'Remote peer has not connected yet.'"
1439
- ]
1440
- });
1441
- this._isEnabled = true;
1442
- }
1443
- /**
1444
- * Stop exchanging messages with the remote peer.
1445
- */
1446
- async disable() {
1447
- this._isEnabled = false;
1573
+ }
1448
1574
  }
1449
1575
  };
1450
1576
  export {