@dxos/echo-pipeline 0.5.0 → 0.5.1-main.140276b

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.
@@ -58,6 +58,7 @@ var import_async = require("@dxos/async");
58
58
  var import_automerge = require("@dxos/automerge/automerge");
59
59
  var import_automerge_repo = require("@dxos/automerge/automerge-repo");
60
60
  var import_context = require("@dxos/context");
61
+ var import_invariant = require("@dxos/invariant");
61
62
  var import_keys = require("@dxos/keys");
62
63
  var import_log = require("@dxos/log");
63
64
  var import_tracing = require("@dxos/tracing");
@@ -66,10 +67,10 @@ var import_context2 = require("@dxos/context");
66
67
  var import_async2 = require("@dxos/async");
67
68
  var import_automerge_repo2 = require("@dxos/automerge/automerge-repo");
68
69
  var import_codec_protobuf = require("@dxos/codec-protobuf");
69
- var import_invariant = require("@dxos/invariant");
70
+ var import_invariant2 = require("@dxos/invariant");
70
71
  var import_async3 = require("@dxos/async");
71
72
  var import_automerge_repo3 = require("@dxos/automerge/automerge-repo");
72
- var import_invariant2 = require("@dxos/invariant");
73
+ var import_invariant3 = require("@dxos/invariant");
73
74
  var import_log2 = require("@dxos/log");
74
75
  var import_teleport_extension_automerge_replicator = require("@dxos/teleport-extension-automerge-replicator");
75
76
  var import_automerge_repo_storage_indexeddb = require("@dxos/automerge/automerge-repo-storage-indexeddb");
@@ -79,7 +80,7 @@ var import_util2 = require("@dxos/util");
79
80
  var import_async4 = require("@dxos/async");
80
81
  var import_context3 = require("@dxos/context");
81
82
  var import_debug = require("@dxos/debug");
82
- var import_invariant3 = require("@dxos/invariant");
83
+ var import_invariant4 = require("@dxos/invariant");
83
84
  var import_log4 = require("@dxos/log");
84
85
  var import_tracing2 = require("@dxos/tracing");
85
86
  var import_echo_schema = require("@dxos/echo-schema");
@@ -168,7 +169,8 @@ var LevelDBStorageAdapter = class extends import_context2.Resource {
168
169
  };
169
170
  var keyEncoder = {
170
171
  encode: (key) => Buffer.from(key.map((k) => k.replaceAll("%", "%25").replaceAll("-", "%2D")).join("-")),
171
- decode: (key) => Buffer.from(key).toString().split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%"))
172
+ decode: (key) => Buffer.from(key).toString().split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%")),
173
+ format: "buffer"
172
174
  };
173
175
  var encodingOptions = {
174
176
  keyEncoding: keyEncoder,
@@ -203,7 +205,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
203
205
  }
204
206
  send(message) {
205
207
  const peer = this._peers.get(message.targetId);
206
- (0, import_invariant.invariant)(peer, "Peer not found.", {
208
+ (0, import_invariant2.invariant)(peer, "Peer not found.", {
207
209
  F: __dxlog_file,
208
210
  L: 51,
209
211
  S: this,
@@ -228,7 +230,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
228
230
  syncRepo({ id, syncMessage }) {
229
231
  const peerId = this._getPeerId(id);
230
232
  return new import_codec_protobuf.Stream(({ next, close }) => {
231
- (0, import_invariant.invariant)(!this._peers.has(peerId), "Peer already connected.", {
233
+ (0, import_invariant2.invariant)(!this._peers.has(peerId), "Peer already connected.", {
232
234
  F: __dxlog_file,
233
235
  L: 73,
234
236
  S: this,
@@ -252,7 +254,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
252
254
  });
253
255
  }
254
256
  });
255
- (0, import_invariant.invariant)(this._isConnected, void 0, {
257
+ (0, import_invariant2.invariant)(this._isConnected, void 0, {
256
258
  F: __dxlog_file,
257
259
  L: 90,
258
260
  S: this,
@@ -268,7 +270,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
268
270
  });
269
271
  }
270
272
  async sendSyncMessage({ id, syncMessage }) {
271
- (0, import_invariant.invariant)(this._isConnected, void 0, {
273
+ (0, import_invariant2.invariant)(this._isConnected, void 0, {
272
274
  F: __dxlog_file,
273
275
  L: 99,
274
276
  S: this,
@@ -281,7 +283,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
281
283
  this.emit("message", message);
282
284
  }
283
285
  async getHostInfo() {
284
- (0, import_invariant.invariant)(this._isConnected, void 0, {
286
+ (0, import_invariant2.invariant)(this._isConnected, void 0, {
285
287
  F: __dxlog_file,
286
288
  L: 105,
287
289
  S: this,
@@ -290,7 +292,7 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo2.NetworkAdapte
290
292
  ""
291
293
  ]
292
294
  });
293
- (0, import_invariant.invariant)(this.peerId, "Peer id not set.", {
295
+ (0, import_invariant2.invariant)(this.peerId, "Peer id not set.", {
294
296
  F: __dxlog_file,
295
297
  L: 106,
296
298
  S: this,
@@ -329,7 +331,7 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
329
331
  send(message) {
330
332
  const receiverId = message.targetId;
331
333
  const extension = this._extensions.get(receiverId);
332
- (0, import_invariant2.invariant)(extension, "Extension not found.", {
334
+ (0, import_invariant3.invariant)(extension, "Extension not found.", {
333
335
  F: __dxlog_file2,
334
336
  L: 38,
335
337
  S: this,
@@ -350,7 +352,7 @@ var MeshNetworkAdapter = class extends import_automerge_repo3.NetworkAdapter {
350
352
  disconnect() {
351
353
  }
352
354
  createExtension() {
353
- (0, import_invariant2.invariant)(this.peerId, "Peer id not set.", {
355
+ (0, import_invariant3.invariant)(this.peerId, "Peer id not set.", {
354
356
  F: __dxlog_file2,
355
357
  L: 47,
356
358
  S: this,
@@ -577,7 +579,7 @@ var AutomergeHost = class {
577
579
  isRequested
578
580
  }, {
579
581
  F: __dxlog_file4,
580
- L: 100,
582
+ L: 108,
581
583
  S: this,
582
584
  C: (f, a) => f(...a)
583
585
  });
@@ -591,7 +593,7 @@ var AutomergeHost = class {
591
593
  documentId
592
594
  }, {
593
595
  F: __dxlog_file4,
594
- L: 107,
596
+ L: 115,
595
597
  S: this,
596
598
  C: (f, a) => f(...a)
597
599
  });
@@ -605,7 +607,7 @@ var AutomergeHost = class {
605
607
  documentId
606
608
  }, {
607
609
  F: __dxlog_file4,
608
- L: 116,
610
+ L: 124,
609
611
  S: this,
610
612
  C: (f, a) => f(...a)
611
613
  });
@@ -622,7 +624,7 @@ var AutomergeHost = class {
622
624
  isAuthorized
623
625
  }, {
624
626
  F: __dxlog_file4,
625
- L: 122,
627
+ L: 130,
626
628
  S: this,
627
629
  C: (f, a) => f(...a)
628
630
  });
@@ -630,7 +632,7 @@ var AutomergeHost = class {
630
632
  } catch (err) {
631
633
  import_log.log.catch(err, void 0, {
632
634
  F: __dxlog_file4,
633
- L: 132,
635
+ L: 140,
634
636
  S: this,
635
637
  C: (f, a) => f(...a)
636
638
  });
@@ -678,21 +680,21 @@ var AutomergeHost = class {
678
680
  //
679
681
  // Methods for client-services.
680
682
  //
681
- async flush({ documentIds }) {
682
- await Promise.all(documentIds?.map((id) => this._repo.find(id).whenReady()) ?? []);
683
- try {
684
- await (0, import_async.asyncTimeout)(this._repo.flush(documentIds), 500);
685
- } catch (err) {
686
- import_log.log.warn("flush error", {
687
- documentIds,
688
- err
689
- }, {
683
+ async flush({ states }) {
684
+ await Promise.all(states?.map(async ({ heads, documentId }) => {
685
+ (0, import_invariant.invariant)(heads, "heads are required for flush", {
690
686
  F: __dxlog_file4,
691
- L: 196,
687
+ L: 200,
692
688
  S: this,
693
- C: (f, a) => f(...a)
689
+ A: [
690
+ "heads",
691
+ "'heads are required for flush'"
692
+ ]
694
693
  });
695
- }
694
+ const handle = this.repo.handles[documentId] ?? this._repo.find(documentId);
695
+ await waitForHeads(handle, heads);
696
+ }) ?? []);
697
+ await this._repo.flush(states?.map(({ documentId }) => documentId));
696
698
  }
697
699
  syncRepo(request) {
698
700
  return this._clientNetwork.syncRepo(request);
@@ -715,7 +717,7 @@ var AutomergeHost = class {
715
717
  deviceKey
716
718
  }, {
717
719
  F: __dxlog_file4,
718
- L: 221,
720
+ L: 230,
719
721
  S: this,
720
722
  C: (f, a) => f(...a)
721
723
  });
@@ -750,6 +752,24 @@ var getSpaceKeyFromDoc = (doc) => {
750
752
  }
751
753
  return String(rawSpaceKey);
752
754
  };
755
+ var waitForHeads = async (handle, heads) => {
756
+ await handle.whenReady();
757
+ const unavailableHeads = new Set(heads);
758
+ await import_async.Event.wrap(handle, "change").waitForCondition(() => {
759
+ for (const changeHash of unavailableHeads.values()) {
760
+ if (changeIsPresentInDoc(handle.docSync(), changeHash)) {
761
+ unavailableHeads.delete(changeHash);
762
+ }
763
+ }
764
+ if (unavailableHeads.size === 0) {
765
+ return true;
766
+ }
767
+ return false;
768
+ });
769
+ };
770
+ var changeIsPresentInDoc = (doc, changeHash) => {
771
+ return !!(0, import_automerge.getBackend)(doc).getChangeByHash(changeHash);
772
+ };
753
773
  function _ts_decorate2(decorators, target, key, desc) {
754
774
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
755
775
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -792,7 +812,7 @@ var AutomergeDocumentLoaderImpl = class {
792
812
  } else {
793
813
  const existingDocHandle = await this._initDocHandle(ctx, spaceState.rootUrl);
794
814
  const doc = existingDocHandle.docSync();
795
- (0, import_invariant3.invariant)(doc, void 0, {
815
+ (0, import_invariant4.invariant)(doc, void 0, {
796
816
  F: __dxlog_file5,
797
817
  L: 74,
798
818
  S: this,
@@ -807,45 +827,54 @@ var AutomergeDocumentLoaderImpl = class {
807
827
  this._spaceRootDocHandle = existingDocHandle;
808
828
  }
809
829
  }
810
- loadObjectDocument(objectId) {
811
- (0, import_invariant3.invariant)(this._spaceRootDocHandle, void 0, {
812
- F: __dxlog_file5,
813
- L: 83,
814
- S: this,
815
- A: [
816
- "this._spaceRootDocHandle",
817
- ""
818
- ]
819
- });
820
- if (this._objectDocumentHandles.has(objectId) || this._objectsPendingDocumentLoad.has(objectId)) {
821
- return;
822
- }
823
- const spaceRootDoc = this._spaceRootDocHandle.docSync();
824
- (0, import_invariant3.invariant)(spaceRootDoc, void 0, {
825
- F: __dxlog_file5,
826
- L: 88,
827
- S: this,
828
- A: [
829
- "spaceRootDoc",
830
- ""
831
- ]
832
- });
833
- const documentUrl = (spaceRootDoc.links ?? {})[objectId];
834
- if (documentUrl == null) {
835
- this._objectsPendingDocumentLoad.add(objectId);
836
- import_log4.log.info("loading delayed until object links are initialized", {
837
- objectId
838
- }, {
830
+ loadObjectDocument(objectIdOrMany) {
831
+ const objectIds = Array.isArray(objectIdOrMany) ? objectIdOrMany : [
832
+ objectIdOrMany
833
+ ];
834
+ let hasUrlsToLoad = false;
835
+ const urlsToLoad = {};
836
+ for (const objectId of objectIds) {
837
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
838
+ F: __dxlog_file5,
839
+ L: 87,
840
+ S: this,
841
+ A: [
842
+ "this._spaceRootDocHandle",
843
+ ""
844
+ ]
845
+ });
846
+ if (this._objectDocumentHandles.has(objectId) || this._objectsPendingDocumentLoad.has(objectId)) {
847
+ continue;
848
+ }
849
+ const spaceRootDoc = this._spaceRootDocHandle.docSync();
850
+ (0, import_invariant4.invariant)(spaceRootDoc, void 0, {
839
851
  F: __dxlog_file5,
840
852
  L: 92,
841
853
  S: this,
842
- C: (f, a) => f(...a)
854
+ A: [
855
+ "spaceRootDoc",
856
+ ""
857
+ ]
843
858
  });
844
- return;
859
+ const documentUrl = (spaceRootDoc.links ?? {})[objectId];
860
+ if (documentUrl == null) {
861
+ this._objectsPendingDocumentLoad.add(objectId);
862
+ import_log4.log.info("loading delayed until object links are initialized", {
863
+ objectId
864
+ }, {
865
+ F: __dxlog_file5,
866
+ L: 96,
867
+ S: this,
868
+ C: (f, a) => f(...a)
869
+ });
870
+ } else {
871
+ urlsToLoad[objectId] = documentUrl;
872
+ hasUrlsToLoad = true;
873
+ }
874
+ }
875
+ if (hasUrlsToLoad) {
876
+ this._loadLinkedObjects(urlsToLoad);
845
877
  }
846
- this._loadLinkedObjects({
847
- [objectId]: documentUrl
848
- });
849
878
  }
850
879
  onObjectLinksUpdated(links) {
851
880
  if (!links) {
@@ -856,9 +885,9 @@ var AutomergeDocumentLoaderImpl = class {
856
885
  linksAwaitingLoad.forEach(([objectId]) => this._objectsPendingDocumentLoad.delete(objectId));
857
886
  }
858
887
  getSpaceRootDocHandle() {
859
- (0, import_invariant3.invariant)(this._spaceRootDocHandle, void 0, {
888
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
860
889
  F: __dxlog_file5,
861
- L: 110,
890
+ L: 119,
862
891
  S: this,
863
892
  A: [
864
893
  "this._spaceRootDocHandle",
@@ -868,9 +897,9 @@ var AutomergeDocumentLoaderImpl = class {
868
897
  return this._spaceRootDocHandle;
869
898
  }
870
899
  createDocumentForObject(objectId) {
871
- (0, import_invariant3.invariant)(this._spaceRootDocHandle, void 0, {
900
+ (0, import_invariant4.invariant)(this._spaceRootDocHandle, void 0, {
872
901
  F: __dxlog_file5,
873
- L: 115,
902
+ L: 124,
874
903
  S: this,
875
904
  A: [
876
905
  "this._spaceRootDocHandle",
@@ -913,7 +942,7 @@ var AutomergeDocumentLoaderImpl = class {
913
942
  actualDocumentUrl: objectDocumentHandle.url
914
943
  }, {
915
944
  F: __dxlog_file5,
916
- L: 145,
945
+ L: 154,
917
946
  S: this,
918
947
  C: (f, a) => f(...a)
919
948
  });
@@ -922,7 +951,7 @@ var AutomergeDocumentLoaderImpl = class {
922
951
  if (objectDocumentHandle?.url === automergeUrl) {
923
952
  import_log4.log.warn("object document was already loaded", logMeta, {
924
953
  F: __dxlog_file5,
925
- L: 152,
954
+ L: 161,
926
955
  S: this,
927
956
  C: (f, a) => f(...a)
928
957
  });
@@ -931,7 +960,7 @@ var AutomergeDocumentLoaderImpl = class {
931
960
  const handle = this._repo.find(automergeUrl);
932
961
  import_log4.log.debug("document loading triggered", logMeta, {
933
962
  F: __dxlog_file5,
934
- L: 156,
963
+ L: 165,
935
964
  S: this,
936
965
  C: (f, a) => f(...a)
937
966
  });
@@ -954,7 +983,7 @@ var AutomergeDocumentLoaderImpl = class {
954
983
  state: docHandle.state
955
984
  }, {
956
985
  F: __dxlog_file5,
957
- L: 172,
986
+ L: 181,
958
987
  S: this,
959
988
  C: (f, a) => f(...a)
960
989
  });
@@ -996,7 +1025,7 @@ var AutomergeDocumentLoaderImpl = class {
996
1025
  if (this.onObjectDocumentLoaded.listenerCount() === 0) {
997
1026
  import_log4.log.info("document loaded after all listeners were removed", logMeta, {
998
1027
  F: __dxlog_file5,
999
- L: 208,
1028
+ L: 217,
1000
1029
  S: this,
1001
1030
  C: (f, a) => f(...a)
1002
1031
  });
@@ -1006,7 +1035,7 @@ var AutomergeDocumentLoaderImpl = class {
1006
1035
  if (objectDocHandle?.url !== handle.url) {
1007
1036
  import_log4.log.warn("object was rebound while a document was loading, discarding handle", logMeta, {
1008
1037
  F: __dxlog_file5,
1009
- L: 213,
1038
+ L: 222,
1010
1039
  S: this,
1011
1040
  C: (f, a) => f(...a)
1012
1041
  });
@@ -1025,7 +1054,7 @@ var AutomergeDocumentLoaderImpl = class {
1025
1054
  err
1026
1055
  }, {
1027
1056
  F: __dxlog_file5,
1028
- L: 219,
1057
+ L: 228,
1029
1058
  S: this,
1030
1059
  C: (f, a) => f(...a)
1031
1060
  });