@dxos/network-manager 0.1.58-main.75cbd70 → 0.1.58-main.7858df5

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 (53) hide show
  1. package/dist/lib/browser/{chunk-VP6DWC3F.mjs → chunk-HKHUCTRB.mjs} +205 -114
  2. package/dist/lib/browser/chunk-HKHUCTRB.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +146 -13
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node/index.cjs +493 -402
  8. package/dist/lib/node/index.cjs.map +4 -4
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +1085 -860
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/swarm/connection-limiter.d.ts +1 -1
  13. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
  14. package/dist/types/src/swarm/connection.d.ts +3 -1
  15. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  16. package/dist/types/src/swarm/peer.d.ts +3 -3
  17. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  18. package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
  19. package/dist/types/src/swarm/swarm.d.ts +2 -2
  20. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  21. package/dist/types/src/testing/test-builder.d.ts +1 -1
  22. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  23. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  24. package/dist/types/src/tests/tcp-transport.test.d.ts +2 -0
  25. package/dist/types/src/tests/tcp-transport.test.d.ts.map +1 -0
  26. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  27. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  28. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  29. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  30. package/dist/types/src/transport/tcp-transport.d.ts +23 -0
  31. package/dist/types/src/transport/tcp-transport.d.ts.map +1 -0
  32. package/dist/types/src/transport/transport.d.ts +2 -1
  33. package/dist/types/src/transport/transport.d.ts.map +1 -1
  34. package/package.json +17 -18
  35. package/src/swarm/connection-limiter.ts +2 -2
  36. package/src/swarm/connection.test.ts +1 -1
  37. package/src/swarm/connection.ts +53 -27
  38. package/src/swarm/peer.ts +6 -3
  39. package/src/swarm/swarm-mapper.ts +4 -7
  40. package/src/swarm/swarm.test.ts +3 -3
  41. package/src/swarm/swarm.ts +3 -3
  42. package/src/testing/test-builder.ts +6 -2
  43. package/src/tests/basic-test-suite.ts +1 -1
  44. package/src/tests/memory-transport.test.ts +2 -2
  45. package/src/tests/tcp-transport.test.ts +67 -0
  46. package/src/tests/webrtc-transport.test.ts +1 -1
  47. package/src/topology/star-topology.ts +1 -4
  48. package/src/transport/simplepeer-transport-proxy.ts +48 -21
  49. package/src/transport/simplepeer-transport-service.ts +1 -4
  50. package/src/transport/simplepeer-transport.ts +38 -4
  51. package/src/transport/tcp-transport.ts +107 -0
  52. package/src/transport/transport.ts +1 -0
  53. package/dist/lib/browser/chunk-VP6DWC3F.mjs.map +0 -7
@@ -545,7 +545,6 @@ var import_util5 = require("@dxos/util");
545
545
  var import_async = require("@dxos/async");
546
546
  var import_context = require("@dxos/context");
547
547
  var import_debug = require("@dxos/debug");
548
- var import_errors = require("@dxos/errors");
549
548
  var import_invariant = require("@dxos/invariant");
550
549
  var import_keys = require("@dxos/keys");
551
550
  var import_log = require("@dxos/log");
@@ -602,6 +601,8 @@ var ConnectionState;
602
601
  */
603
602
  "CLOSED"
604
603
  ] = "CLOSED";
604
+ ConnectionState5["ABORTING"] = "ABORTING";
605
+ ConnectionState5["ABORTED"] = "ABORTED";
605
606
  })(ConnectionState || (ConnectionState = {}));
606
607
  var Connection = class {
607
608
  constructor(topic, ownId, remoteId, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
@@ -633,7 +634,7 @@ var Connection = class {
633
634
  initiator: this.initiator
634
635
  }, {
635
636
  F: __dxlog_file,
636
- L: 114,
637
+ L: 123,
637
638
  S: this,
638
639
  C: (f, a) => f(...a)
639
640
  });
@@ -653,7 +654,7 @@ var Connection = class {
653
654
  async openConnection() {
654
655
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
655
656
  F: __dxlog_file,
656
- L: 139,
657
+ L: 148,
657
658
  S: this,
658
659
  A: [
659
660
  "this._state === ConnectionState.INITIAL",
@@ -664,7 +665,7 @@ var Connection = class {
664
665
  id: this._instanceId
665
666
  }), {
666
667
  F: __dxlog_file,
667
- L: 140,
668
+ L: 149,
668
669
  S: this,
669
670
  C: (f, a) => f(...a)
670
671
  });
@@ -676,7 +677,7 @@ var Connection = class {
676
677
  initiator: this.initiator
677
678
  }, {
678
679
  F: __dxlog_file,
679
- L: 141,
680
+ L: 150,
680
681
  S: this,
681
682
  C: (f, a) => f(...a)
682
683
  });
@@ -687,15 +688,15 @@ var Connection = class {
687
688
  this._protocol.stream.on("close", () => {
688
689
  (0, import_log.log)("protocol stream closed", void 0, {
689
690
  F: __dxlog_file,
690
- L: 158,
691
+ L: 167,
691
692
  S: this,
692
693
  C: (f, a) => f(...a)
693
694
  });
694
- this.close().catch((err) => this.errors.raise(err));
695
+ this.close(new import_protocols.ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
695
696
  });
696
697
  (0, import_invariant.invariant)(!this._transport, void 0, {
697
698
  F: __dxlog_file,
698
- L: 162,
699
+ L: 171,
699
700
  S: this,
700
701
  A: [
701
702
  "!this._transport",
@@ -715,7 +716,7 @@ var Connection = class {
715
716
  this._transport = void 0;
716
717
  (0, import_log.log)("abort triggered by transport close", void 0, {
717
718
  F: __dxlog_file,
718
- L: 176,
719
+ L: 185,
719
720
  S: this,
720
721
  C: (f, a) => f(...a)
721
722
  });
@@ -726,21 +727,38 @@ var Connection = class {
726
727
  err
727
728
  }, {
728
729
  F: __dxlog_file,
729
- L: 181,
730
+ L: 190,
730
731
  S: this,
731
732
  C: (f, a) => f(...a)
732
733
  });
733
734
  if (!this.closeReason) {
734
735
  this.closeReason = err?.message;
735
736
  }
736
- if (err?.message.includes("OperationError")) {
737
- (0, import_log.log)("aborting due to transport OperationError", void 0, {
737
+ if (err instanceof import_protocols.ConnectionResetError) {
738
+ (0, import_log.log)("aborting due to transport ConnectionResetError", void 0, {
739
+ F: __dxlog_file,
740
+ L: 197,
741
+ S: this,
742
+ C: (f, a) => f(...a)
743
+ });
744
+ this.abort().catch((err2) => this.errors.raise(err2));
745
+ } else if (err instanceof import_protocols.ConnectivityError) {
746
+ (0, import_log.log)("aborting due to transport ConnectivityError", void 0, {
738
747
  F: __dxlog_file,
739
- L: 187,
748
+ L: 200,
740
749
  S: this,
741
750
  C: (f, a) => f(...a)
742
751
  });
743
752
  this.abort().catch((err2) => this.errors.raise(err2));
753
+ } else if (err instanceof import_protocols.UnknownProtocolError) {
754
+ (0, import_log.log)("unsure what to do for UnknownProtocolError", {
755
+ err
756
+ }, {
757
+ F: __dxlog_file,
758
+ L: 203,
759
+ S: this,
760
+ C: (f, a) => f(...a)
761
+ });
744
762
  }
745
763
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
746
764
  this.errors.raise(err);
@@ -754,7 +772,7 @@ var Connection = class {
754
772
  id: this._instanceId
755
773
  }), {
756
774
  F: __dxlog_file,
757
- L: 203,
775
+ L: 218,
758
776
  S: this,
759
777
  C: (f, a) => f(...a)
760
778
  });
@@ -764,30 +782,30 @@ var Connection = class {
764
782
  err
765
783
  }, {
766
784
  F: __dxlog_file,
767
- L: 209,
785
+ L: 224,
768
786
  S: this,
769
787
  C: (f, a) => f(...a)
770
788
  });
771
- if (!this.closeReason) {
772
- this.closeReason = err?.message;
773
- }
774
- if (this._state === ConnectionState.CLOSED) {
775
- (0, import_log.log)("abort ignored: already closed", this.closeReason, {
789
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
790
+ (0, import_log.log)(`abort ignored: already ${this._state}`, this.closeReason, {
776
791
  F: __dxlog_file,
777
- L: 214,
792
+ L: 226,
778
793
  S: this,
779
794
  C: (f, a) => f(...a)
780
795
  });
781
796
  return;
782
797
  }
783
- this._changeState(ConnectionState.CLOSING);
798
+ this._changeState(ConnectionState.ABORTING);
799
+ if (!this.closeReason) {
800
+ this.closeReason = err?.message;
801
+ }
784
802
  await this._ctx.dispose();
785
803
  try {
786
804
  (0, import_log.log)("aborting protocol... ", {
787
805
  proto: this._protocol
788
806
  }, {
789
807
  F: __dxlog_file,
790
- L: 224,
808
+ L: 239,
791
809
  S: this,
792
810
  C: (f, a) => f(...a)
793
811
  });
@@ -795,7 +813,7 @@ var Connection = class {
795
813
  } catch (err2) {
796
814
  import_log.log.catch(err2, void 0, {
797
815
  F: __dxlog_file,
798
- L: 227,
816
+ L: 242,
799
817
  S: this,
800
818
  C: (f, a) => f(...a)
801
819
  });
@@ -805,56 +823,68 @@ var Connection = class {
805
823
  } catch (err2) {
806
824
  import_log.log.catch(err2, void 0, {
807
825
  F: __dxlog_file,
808
- L: 234,
826
+ L: 249,
809
827
  S: this,
810
828
  C: (f, a) => f(...a)
811
829
  });
812
830
  }
813
- this._changeState(ConnectionState.CLOSED);
814
- this._callbacks?.onClosed?.(err);
831
+ try {
832
+ this._callbacks?.onClosed?.(err);
833
+ } catch (err2) {
834
+ import_log.log.catch(err2, void 0, {
835
+ F: __dxlog_file,
836
+ L: 255,
837
+ S: this,
838
+ C: (f, a) => f(...a)
839
+ });
840
+ }
841
+ this._changeState(ConnectionState.ABORTED);
815
842
  }
816
843
  async close(err) {
817
844
  if (!this.closeReason) {
818
845
  this.closeReason = err?.message;
819
846
  }
820
- if (this._state === ConnectionState.CLOSED) {
847
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTING || this._state === ConnectionState.ABORTED) {
821
848
  return;
822
849
  }
850
+ const lastState = this._state;
823
851
  this._changeState(ConnectionState.CLOSING);
824
852
  await this._ctx.dispose();
825
853
  (0, import_log.log)("closing...", {
826
854
  peerId: this.ownId
827
855
  }, {
828
856
  F: __dxlog_file,
829
- L: 253,
857
+ L: 277,
830
858
  S: this,
831
859
  C: (f, a) => f(...a)
832
860
  });
833
- try {
834
- await this._protocol.close();
835
- } catch (err2) {
836
- import_log.log.catch(err2, void 0, {
837
- F: __dxlog_file,
838
- L: 259,
839
- S: this,
840
- C: (f, a) => f(...a)
841
- });
842
- }
843
- try {
844
- await this._transport?.destroy();
845
- } catch (err2) {
846
- import_log.log.catch(err2, void 0, {
847
- F: __dxlog_file,
848
- L: 266,
849
- S: this,
850
- C: (f, a) => f(...a)
851
- });
861
+ if (lastState === ConnectionState.CONNECTED) {
862
+ try {
863
+ await this._protocol.close();
864
+ } catch (err2) {
865
+ import_log.log.catch(err2, void 0, {
866
+ F: __dxlog_file,
867
+ L: 284,
868
+ S: this,
869
+ C: (f, a) => f(...a)
870
+ });
871
+ }
872
+ try {
873
+ await this._transport?.destroy();
874
+ } catch (err2) {
875
+ import_log.log.catch(err2, void 0, {
876
+ F: __dxlog_file,
877
+ L: 291,
878
+ S: this,
879
+ C: (f, a) => f(...a)
880
+ });
881
+ }
852
882
  }
853
883
  (0, import_log.log)("closed", {
854
884
  peerId: this.ownId
855
885
  }, {
856
886
  F: __dxlog_file,
857
- L: 269,
887
+ L: 295,
858
888
  S: this,
859
889
  C: (f, a) => f(...a)
860
890
  });
@@ -890,14 +920,14 @@ var Connection = class {
890
920
  }
891
921
  });
892
922
  } catch (err) {
893
- if (err instanceof import_errors.CancelledError) {
923
+ if (err instanceof import_protocols.CancelledError) {
894
924
  return;
895
925
  }
896
926
  import_log.log.warn("signal failed", {
897
927
  err
898
928
  }, {
899
929
  F: __dxlog_file,
900
- L: 306,
930
+ L: 332,
901
931
  S: this,
902
932
  C: (f, a) => f(...a)
903
933
  });
@@ -910,7 +940,7 @@ var Connection = class {
910
940
  async signal(msg) {
911
941
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
912
942
  F: __dxlog_file,
913
- L: 315,
943
+ L: 341,
914
944
  S: this,
915
945
  A: [
916
946
  "msg.sessionId",
@@ -920,7 +950,7 @@ var Connection = class {
920
950
  if (!msg.sessionId.equals(this.sessionId)) {
921
951
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
922
952
  F: __dxlog_file,
923
- L: 317,
953
+ L: 343,
924
954
  S: this,
925
955
  C: (f, a) => f(...a)
926
956
  });
@@ -928,7 +958,7 @@ var Connection = class {
928
958
  }
929
959
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
930
960
  F: __dxlog_file,
931
- L: 320,
961
+ L: 346,
932
962
  S: this,
933
963
  A: [
934
964
  "msg.data.signal || msg.data.signalBatch",
@@ -937,7 +967,7 @@ var Connection = class {
937
967
  });
938
968
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
939
969
  F: __dxlog_file,
940
- L: 321,
970
+ L: 347,
941
971
  S: this,
942
972
  A: [
943
973
  "msg.author?.equals(this.remoteId)",
@@ -946,7 +976,7 @@ var Connection = class {
946
976
  });
947
977
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
948
978
  F: __dxlog_file,
949
- L: 322,
979
+ L: 348,
950
980
  S: this,
951
981
  A: [
952
982
  "msg.recipient?.equals(this.ownId)",
@@ -970,7 +1000,7 @@ var Connection = class {
970
1000
  msg: msg.data
971
1001
  }, {
972
1002
  F: __dxlog_file,
973
- L: 331,
1003
+ L: 357,
974
1004
  S: this,
975
1005
  C: (f, a) => f(...a)
976
1006
  });
@@ -978,7 +1008,7 @@ var Connection = class {
978
1008
  } else {
979
1009
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
980
1010
  F: __dxlog_file,
981
- L: 334,
1011
+ L: 360,
982
1012
  S: this,
983
1013
  A: [
984
1014
  "this._transport",
@@ -991,7 +1021,7 @@ var Connection = class {
991
1021
  msg: msg.data
992
1022
  }, {
993
1023
  F: __dxlog_file,
994
- L: 335,
1024
+ L: 361,
995
1025
  S: this,
996
1026
  C: (f, a) => f(...a)
997
1027
  });
@@ -1005,17 +1035,17 @@ var Connection = class {
1005
1035
  _changeState(state) {
1006
1036
  (0, import_log.log)("stateChanged", {
1007
1037
  from: this._state,
1008
- too: state,
1038
+ to: state,
1009
1039
  peerId: this.ownId
1010
1040
  }, {
1011
1041
  F: __dxlog_file,
1012
- L: 346,
1042
+ L: 372,
1013
1043
  S: this,
1014
1044
  C: (f, a) => f(...a)
1015
1045
  });
1016
1046
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
1017
1047
  F: __dxlog_file,
1018
- L: 347,
1048
+ L: 373,
1019
1049
  S: this,
1020
1050
  A: [
1021
1051
  "state !== this._state",
@@ -1040,248 +1070,16 @@ var import_debug2 = require("@dxos/debug");
1040
1070
  var import_invariant4 = require("@dxos/invariant");
1041
1071
  var import_keys4 = require("@dxos/keys");
1042
1072
  var import_log4 = require("@dxos/log");
1043
- var import_protocols3 = require("@dxos/protocols");
1073
+ var import_protocols4 = require("@dxos/protocols");
1044
1074
  var import_util2 = require("@dxos/util");
1045
1075
 
1046
- // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1076
+ // packages/core/mesh/network-manager/src/swarm/peer.ts
1077
+ var import_async2 = require("@dxos/async");
1047
1078
  var import_context2 = require("@dxos/context");
1048
1079
  var import_invariant2 = require("@dxos/invariant");
1049
1080
  var import_keys2 = require("@dxos/keys");
1050
1081
  var import_log2 = require("@dxos/log");
1051
1082
  var import_protocols2 = require("@dxos/protocols");
1052
- var import_util = require("@dxos/util");
1053
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1054
- var SwarmMessage = import_protocols2.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1055
- var SwarmMessenger = class {
1056
- constructor({ sendMessage, onSignal, onOffer, topic }) {
1057
- this._ctx = new import_context2.Context();
1058
- this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1059
- this._sendMessage = sendMessage;
1060
- this._onSignal = onSignal;
1061
- this._onOffer = onOffer;
1062
- this._topic = topic;
1063
- }
1064
- async receiveMessage({ author, recipient, payload }) {
1065
- if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1066
- return;
1067
- }
1068
- const message = SwarmMessage.decode(payload.value);
1069
- if (!this._topic.equals(message.topic)) {
1070
- return;
1071
- }
1072
- (0, import_log2.log)("received", {
1073
- from: author,
1074
- to: recipient,
1075
- msg: message
1076
- }, {
1077
- F: __dxlog_file2,
1078
- L: 69,
1079
- S: this,
1080
- C: (f, a) => f(...a)
1081
- });
1082
- if (message.data?.offer) {
1083
- await this._handleOffer({
1084
- author,
1085
- recipient,
1086
- message
1087
- });
1088
- } else if (message.data?.answer) {
1089
- await this._resolveAnswers(message);
1090
- } else if (message.data?.signal) {
1091
- await this._handleSignal({
1092
- author,
1093
- recipient,
1094
- message
1095
- });
1096
- } else if (message.data?.signalBatch) {
1097
- await this._handleSignal({
1098
- author,
1099
- recipient,
1100
- message
1101
- });
1102
- } else {
1103
- import_log2.log.warn("unknown message", {
1104
- message
1105
- }, {
1106
- F: __dxlog_file2,
1107
- L: 80,
1108
- S: this,
1109
- C: (f, a) => f(...a)
1110
- });
1111
- }
1112
- }
1113
- async signal(message) {
1114
- (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1115
- F: __dxlog_file2,
1116
- L: 85,
1117
- S: this,
1118
- A: [
1119
- "message.data?.signal || message.data?.signalBatch",
1120
- "'Invalid message'"
1121
- ]
1122
- });
1123
- await this._sendReliableMessage({
1124
- author: message.author,
1125
- recipient: message.recipient,
1126
- message
1127
- });
1128
- }
1129
- async offer(message) {
1130
- const networkMessage = {
1131
- ...message,
1132
- messageId: import_keys2.PublicKey.random()
1133
- };
1134
- return new Promise((resolve, reject) => {
1135
- this._offerRecords.set(networkMessage.messageId, {
1136
- resolve
1137
- });
1138
- this._sendReliableMessage({
1139
- author: message.author,
1140
- recipient: message.recipient,
1141
- message: networkMessage
1142
- }).catch((err) => reject(err));
1143
- });
1144
- }
1145
- async _sendReliableMessage({ author, recipient, message }) {
1146
- const networkMessage = {
1147
- ...message,
1148
- // Setting unique message_id if it not specified yet.
1149
- messageId: message.messageId ?? import_keys2.PublicKey.random()
1150
- };
1151
- (0, import_log2.log)("sending", {
1152
- from: author,
1153
- to: recipient,
1154
- msg: networkMessage
1155
- }, {
1156
- F: __dxlog_file2,
1157
- L: 123,
1158
- S: this,
1159
- C: (f, a) => f(...a)
1160
- });
1161
- await this._sendMessage({
1162
- author,
1163
- recipient,
1164
- payload: {
1165
- type_url: "dxos.mesh.swarm.SwarmMessage",
1166
- value: SwarmMessage.encode(networkMessage)
1167
- }
1168
- });
1169
- }
1170
- async _resolveAnswers(message) {
1171
- (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1172
- F: __dxlog_file2,
1173
- L: 135,
1174
- S: this,
1175
- A: [
1176
- "message.data?.answer?.offerMessageId",
1177
- "'No offerMessageId'"
1178
- ]
1179
- });
1180
- const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1181
- if (offerRecord) {
1182
- this._offerRecords.delete(message.data.answer.offerMessageId);
1183
- (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
1184
- F: __dxlog_file2,
1185
- L: 139,
1186
- S: this,
1187
- A: [
1188
- "message.data?.answer",
1189
- "'No answer'"
1190
- ]
1191
- });
1192
- (0, import_log2.log)("resolving", {
1193
- answer: message.data.answer
1194
- }, {
1195
- F: __dxlog_file2,
1196
- L: 140,
1197
- S: this,
1198
- C: (f, a) => f(...a)
1199
- });
1200
- offerRecord.resolve(message.data.answer);
1201
- }
1202
- }
1203
- async _handleOffer({ author, recipient, message }) {
1204
- (0, import_invariant2.invariant)(message.data.offer, "No offer", {
1205
- F: __dxlog_file2,
1206
- L: 154,
1207
- S: this,
1208
- A: [
1209
- "message.data.offer",
1210
- "'No offer'"
1211
- ]
1212
- });
1213
- const offerMessage = {
1214
- author,
1215
- recipient,
1216
- ...message,
1217
- data: {
1218
- offer: message.data.offer
1219
- }
1220
- };
1221
- const answer = await this._onOffer(offerMessage);
1222
- answer.offerMessageId = message.messageId;
1223
- try {
1224
- await this._sendReliableMessage({
1225
- author: recipient,
1226
- recipient: author,
1227
- message: {
1228
- topic: message.topic,
1229
- sessionId: message.sessionId,
1230
- data: {
1231
- answer
1232
- }
1233
- }
1234
- });
1235
- } catch (err) {
1236
- import_log2.log.warn("error sending answer", {
1237
- err
1238
- }, {
1239
- F: __dxlog_file2,
1240
- L: 174,
1241
- S: this,
1242
- C: (f, a) => f(...a)
1243
- });
1244
- }
1245
- }
1246
- async _handleSignal({ author, recipient, message }) {
1247
- (0, import_invariant2.invariant)(message.messageId, void 0, {
1248
- F: __dxlog_file2,
1249
- L: 187,
1250
- S: this,
1251
- A: [
1252
- "message.messageId",
1253
- ""
1254
- ]
1255
- });
1256
- (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1257
- F: __dxlog_file2,
1258
- L: 188,
1259
- S: this,
1260
- A: [
1261
- "message.data.signal || message.data.signalBatch",
1262
- "'Invalid message'"
1263
- ]
1264
- });
1265
- const signalMessage = {
1266
- author,
1267
- recipient,
1268
- ...message,
1269
- data: {
1270
- signal: message.data.signal,
1271
- signalBatch: message.data.signalBatch
1272
- }
1273
- };
1274
- await this._onSignal(signalMessage);
1275
- }
1276
- };
1277
-
1278
- // packages/core/mesh/network-manager/src/swarm/peer.ts
1279
- var import_async2 = require("@dxos/async");
1280
- var import_context3 = require("@dxos/context");
1281
- var import_errors2 = require("@dxos/errors");
1282
- var import_invariant3 = require("@dxos/invariant");
1283
- var import_keys3 = require("@dxos/keys");
1284
- var import_log3 = require("@dxos/log");
1285
1083
  function _ts_decorate2(decorators, target, key, desc) {
1286
1084
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1287
1085
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1292,8 +1090,8 @@ function _ts_decorate2(decorators, target, key, desc) {
1292
1090
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1293
1091
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1294
1092
  }
1295
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1296
- var ConnectionDisplacedError = class extends import_errors2.SystemError {
1093
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1094
+ var ConnectionDisplacedError = class extends import_protocols2.SystemError {
1297
1095
  constructor() {
1298
1096
  super("Connection displaced by remote initiator.");
1299
1097
  }
@@ -1312,7 +1110,7 @@ var Peer = class {
1312
1110
  this._callbacks = _callbacks;
1313
1111
  this._availableAfter = 0;
1314
1112
  this.availableToConnect = true;
1315
- this._ctx = new import_context3.Context();
1113
+ this._ctx = new import_context2.Context();
1316
1114
  this.advertizing = false;
1317
1115
  this.initiating = false;
1318
1116
  this.connectionDisplaced = new import_async2.Event();
@@ -1324,13 +1122,13 @@ var Peer = class {
1324
1122
  const remoteId = message.author;
1325
1123
  if (this.connection || this.initiating) {
1326
1124
  if (remoteId.toHex() < this.localPeerId.toHex()) {
1327
- (0, import_log3.log)("close local connection", {
1125
+ (0, import_log2.log)("close local connection", {
1328
1126
  localPeerId: this.id,
1329
1127
  topic: this.topic,
1330
1128
  remotePeerId: this.localPeerId,
1331
1129
  sessionId: this.connection?.sessionId
1332
1130
  }, {
1333
- F: __dxlog_file3,
1131
+ F: __dxlog_file2,
1334
1132
  L: 117,
1335
1133
  S: this,
1336
1134
  C: (f, a) => f(...a)
@@ -1346,8 +1144,8 @@ var Peer = class {
1346
1144
  }
1347
1145
  if (await this._callbacks.onOffer(remoteId)) {
1348
1146
  if (!this.connection) {
1349
- (0, import_invariant3.invariant)(message.sessionId, void 0, {
1350
- F: __dxlog_file3,
1147
+ (0, import_invariant2.invariant)(message.sessionId, void 0, {
1148
+ F: __dxlog_file2,
1351
1149
  L: 137,
1352
1150
  S: this,
1353
1151
  A: [
@@ -1361,14 +1159,14 @@ var Peer = class {
1361
1159
  connection.initiate();
1362
1160
  await connection.openConnection();
1363
1161
  } catch (err) {
1364
- if (!(err instanceof import_errors2.CancelledError)) {
1365
- import_log3.log.warn("connection error", {
1162
+ if (!(err instanceof import_protocols2.CancelledError)) {
1163
+ import_log2.log.warn("connection error", {
1366
1164
  topic: this.topic,
1367
1165
  peerId: this.localPeerId,
1368
1166
  remoteId: this.id,
1369
1167
  err
1370
1168
  }, {
1371
- F: __dxlog_file3,
1169
+ F: __dxlog_file2,
1372
1170
  L: 147,
1373
1171
  S: this,
1374
1172
  C: (f, a) => f(...a)
@@ -1389,8 +1187,8 @@ var Peer = class {
1389
1187
  * Initiate a connection to the remote peer.
1390
1188
  */
1391
1189
  async initiateConnection() {
1392
- (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
1393
- F: __dxlog_file3,
1190
+ (0, import_invariant2.invariant)(!this.initiating, "Initiation in progress.", {
1191
+ F: __dxlog_file2,
1394
1192
  L: 164,
1395
1193
  S: this,
1396
1194
  A: [
@@ -1398,8 +1196,8 @@ var Peer = class {
1398
1196
  "'Initiation in progress.'"
1399
1197
  ]
1400
1198
  });
1401
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1402
- F: __dxlog_file3,
1199
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1200
+ F: __dxlog_file2,
1403
1201
  L: 165,
1404
1202
  S: this,
1405
1203
  A: [
@@ -1407,14 +1205,14 @@ var Peer = class {
1407
1205
  "'Already connected.'"
1408
1206
  ]
1409
1207
  });
1410
- const sessionId = import_keys3.PublicKey.random();
1411
- (0, import_log3.log)("initiating...", {
1208
+ const sessionId = import_keys2.PublicKey.random();
1209
+ (0, import_log2.log)("initiating...", {
1412
1210
  id: this.id,
1413
1211
  topic: this.topic,
1414
1212
  peerId: this.id,
1415
1213
  sessionId
1416
1214
  }, {
1417
- F: __dxlog_file3,
1215
+ F: __dxlog_file2,
1418
1216
  L: 167,
1419
1217
  S: this,
1420
1218
  C: (f, a) => f(...a)
@@ -1433,20 +1231,20 @@ var Peer = class {
1433
1231
  offer: {}
1434
1232
  }
1435
1233
  });
1436
- (0, import_log3.log)("received", {
1234
+ (0, import_log2.log)("received", {
1437
1235
  answer,
1438
1236
  topic: this.topic,
1439
1237
  ownId: this.localPeerId,
1440
1238
  remoteId: this.id
1441
1239
  }, {
1442
- F: __dxlog_file3,
1240
+ F: __dxlog_file2,
1443
1241
  L: 183,
1444
1242
  S: this,
1445
1243
  C: (f, a) => f(...a)
1446
1244
  });
1447
1245
  if (connection.state !== ConnectionState.INITIAL) {
1448
- (0, import_log3.log)("ignoring response", void 0, {
1449
- F: __dxlog_file3,
1246
+ (0, import_log2.log)("ignoring response", void 0, {
1247
+ F: __dxlog_file2,
1450
1248
  L: 185,
1451
1249
  S: this,
1452
1250
  C: (f, a) => f(...a)
@@ -1460,17 +1258,20 @@ var Peer = class {
1460
1258
  await connection.openConnection();
1461
1259
  this._callbacks.onAccepted();
1462
1260
  } catch (err) {
1463
- (0, import_log3.log)("initiation error", {
1261
+ (0, import_log2.log)("initiation error", {
1464
1262
  err,
1465
1263
  topic: this.topic,
1466
1264
  peerId: this.localPeerId,
1467
1265
  remoteId: this.id
1468
1266
  }, {
1469
- F: __dxlog_file3,
1267
+ F: __dxlog_file2,
1470
1268
  L: 196,
1471
1269
  S: this,
1472
1270
  C: (f, a) => f(...a)
1473
1271
  });
1272
+ if (err instanceof import_protocols2.TimeoutError) {
1273
+ await connection.abort(err);
1274
+ }
1474
1275
  await this.closeConnection(err);
1475
1276
  throw err;
1476
1277
  } finally {
@@ -1482,21 +1283,21 @@ var Peer = class {
1482
1283
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
1483
1284
  */
1484
1285
  _createConnection(initiator, sessionId) {
1485
- (0, import_log3.log)("creating connection", {
1286
+ (0, import_log2.log)("creating connection", {
1486
1287
  topic: this.topic,
1487
1288
  peerId: this.localPeerId,
1488
1289
  remoteId: this.id,
1489
1290
  initiator,
1490
1291
  sessionId
1491
1292
  }, {
1492
- F: __dxlog_file3,
1493
- L: 210,
1293
+ F: __dxlog_file2,
1294
+ L: 213,
1494
1295
  S: this,
1495
1296
  C: (f, a) => f(...a)
1496
1297
  });
1497
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1498
- F: __dxlog_file3,
1499
- L: 217,
1298
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1299
+ F: __dxlog_file2,
1300
+ L: 220,
1500
1301
  S: this,
1501
1302
  A: [
1502
1303
  "!this.connection",
@@ -1524,50 +1325,50 @@ var Peer = class {
1524
1325
  this._lastConnectionTime = Date.now();
1525
1326
  this._callbacks.onConnected();
1526
1327
  this._connectionLimiter.doneConnecting(sessionId);
1527
- import_log3.log.trace("dxos.mesh.connection.connected", {
1328
+ import_log2.log.trace("dxos.mesh.connection.connected", {
1528
1329
  topic: this.topic,
1529
1330
  localPeerId: this.localPeerId,
1530
1331
  remotePeerId: this.id,
1531
1332
  sessionId,
1532
1333
  initiator
1533
1334
  }, {
1534
- F: __dxlog_file3,
1535
- L: 236,
1335
+ F: __dxlog_file2,
1336
+ L: 239,
1536
1337
  S: this,
1537
1338
  C: (f, a) => f(...a)
1538
1339
  });
1539
1340
  },
1540
1341
  onClosed: (err) => {
1541
- (0, import_log3.log)("connection closed", {
1342
+ (0, import_log2.log)("connection closed", {
1542
1343
  topic: this.topic,
1543
1344
  peerId: this.localPeerId,
1544
1345
  remoteId: this.id,
1545
1346
  initiator
1546
1347
  }, {
1547
- F: __dxlog_file3,
1548
- L: 245,
1348
+ F: __dxlog_file2,
1349
+ L: 248,
1549
1350
  S: this,
1550
1351
  C: (f, a) => f(...a)
1551
1352
  });
1552
1353
  this._connectionLimiter.doneConnecting(sessionId);
1553
- (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1554
- F: __dxlog_file3,
1555
- L: 250,
1354
+ (0, import_invariant2.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1355
+ F: __dxlog_file2,
1356
+ L: 253,
1556
1357
  S: this,
1557
1358
  A: [
1558
1359
  "this.connection === connection",
1559
1360
  "'Connection mismatch (race condition).'"
1560
1361
  ]
1561
1362
  });
1562
- import_log3.log.trace("dxos.mesh.connection.closed", {
1363
+ import_log2.log.trace("dxos.mesh.connection.closed", {
1563
1364
  topic: this.topic,
1564
1365
  localPeerId: this.localPeerId,
1565
1366
  remotePeerId: this.id,
1566
1367
  sessionId,
1567
1368
  initiator
1568
1369
  }, {
1569
- F: __dxlog_file3,
1570
- L: 252,
1370
+ F: __dxlog_file2,
1371
+ L: 255,
1571
1372
  S: this,
1572
1373
  C: (f, a) => f(...a)
1573
1374
  });
@@ -1594,19 +1395,19 @@ var Peer = class {
1594
1395
  void this._connectionCtx?.dispose();
1595
1396
  this._connectionCtx = this._ctx.derive();
1596
1397
  connection.errors.handle((err) => {
1597
- import_log3.log.warn("connection error", {
1398
+ import_log2.log.warn("connection error", {
1598
1399
  topic: this.topic,
1599
1400
  peerId: this.localPeerId,
1600
1401
  remoteId: this.id,
1601
1402
  initiator,
1602
1403
  err
1603
1404
  }, {
1604
- F: __dxlog_file3,
1605
- L: 292,
1405
+ F: __dxlog_file2,
1406
+ L: 295,
1606
1407
  S: this,
1607
1408
  C: (f, a) => f(...a)
1608
1409
  });
1609
- import_log3.log.trace("dxos.mesh.connection.error", {
1410
+ import_log2.log.trace("dxos.mesh.connection.error", {
1610
1411
  topic: this.topic,
1611
1412
  localPeerId: this.localPeerId,
1612
1413
  remotePeerId: this.id,
@@ -1614,8 +1415,8 @@ var Peer = class {
1614
1415
  initiator,
1615
1416
  err
1616
1417
  }, {
1617
- F: __dxlog_file3,
1618
- L: 293,
1418
+ F: __dxlog_file2,
1419
+ L: 296,
1619
1420
  S: this,
1620
1421
  C: (f, a) => f(...a)
1621
1422
  });
@@ -1629,33 +1430,33 @@ var Peer = class {
1629
1430
  return;
1630
1431
  }
1631
1432
  const connection = this.connection;
1632
- (0, import_log3.log)("closing...", {
1433
+ (0, import_log2.log)("closing...", {
1633
1434
  peerId: this.id,
1634
1435
  sessionId: connection.sessionId
1635
1436
  }, {
1636
- F: __dxlog_file3,
1637
- L: 318,
1437
+ F: __dxlog_file2,
1438
+ L: 321,
1638
1439
  S: this,
1639
1440
  C: (f, a) => f(...a)
1640
1441
  });
1641
1442
  await connection.close(err);
1642
- (0, import_log3.log)("closed", {
1443
+ (0, import_log2.log)("closed", {
1643
1444
  peerId: this.id,
1644
1445
  sessionId: connection.sessionId
1645
1446
  }, {
1646
- F: __dxlog_file3,
1647
- L: 324,
1447
+ F: __dxlog_file2,
1448
+ L: 327,
1648
1449
  S: this,
1649
1450
  C: (f, a) => f(...a)
1650
1451
  });
1651
1452
  }
1652
1453
  async onSignal(message) {
1653
1454
  if (!this.connection) {
1654
- (0, import_log3.log)("dropping signal message for non-existent connection", {
1455
+ (0, import_log2.log)("dropping signal message for non-existent connection", {
1655
1456
  message
1656
1457
  }, {
1657
- F: __dxlog_file3,
1658
- L: 329,
1458
+ F: __dxlog_file2,
1459
+ L: 332,
1659
1460
  S: this,
1660
1461
  C: (f, a) => f(...a)
1661
1462
  });
@@ -1665,12 +1466,12 @@ var Peer = class {
1665
1466
  }
1666
1467
  async destroy() {
1667
1468
  await this._ctx.dispose();
1668
- (0, import_log3.log)("Destroying peer", {
1469
+ (0, import_log2.log)("Destroying peer", {
1669
1470
  peerId: this.id,
1670
1471
  topic: this.topic
1671
1472
  }, {
1672
- F: __dxlog_file3,
1673
- L: 339,
1473
+ F: __dxlog_file2,
1474
+ L: 342,
1674
1475
  S: this,
1675
1476
  C: (f, a) => f(...a)
1676
1477
  });
@@ -1693,6 +1494,238 @@ var increaseInterval = (interval) => {
1693
1494
  return 1e4;
1694
1495
  };
1695
1496
 
1497
+ // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1498
+ var import_context3 = require("@dxos/context");
1499
+ var import_invariant3 = require("@dxos/invariant");
1500
+ var import_keys3 = require("@dxos/keys");
1501
+ var import_log3 = require("@dxos/log");
1502
+ var import_protocols3 = require("@dxos/protocols");
1503
+ var import_util = require("@dxos/util");
1504
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1505
+ var SwarmMessage = import_protocols3.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1506
+ var SwarmMessenger = class {
1507
+ constructor({ sendMessage, onSignal, onOffer, topic }) {
1508
+ this._ctx = new import_context3.Context();
1509
+ this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1510
+ this._sendMessage = sendMessage;
1511
+ this._onSignal = onSignal;
1512
+ this._onOffer = onOffer;
1513
+ this._topic = topic;
1514
+ }
1515
+ async receiveMessage({ author, recipient, payload }) {
1516
+ if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1517
+ return;
1518
+ }
1519
+ const message = SwarmMessage.decode(payload.value);
1520
+ if (!this._topic.equals(message.topic)) {
1521
+ return;
1522
+ }
1523
+ (0, import_log3.log)("received", {
1524
+ from: author,
1525
+ to: recipient,
1526
+ msg: message
1527
+ }, {
1528
+ F: __dxlog_file3,
1529
+ L: 69,
1530
+ S: this,
1531
+ C: (f, a) => f(...a)
1532
+ });
1533
+ if (message.data?.offer) {
1534
+ await this._handleOffer({
1535
+ author,
1536
+ recipient,
1537
+ message
1538
+ });
1539
+ } else if (message.data?.answer) {
1540
+ await this._resolveAnswers(message);
1541
+ } else if (message.data?.signal) {
1542
+ await this._handleSignal({
1543
+ author,
1544
+ recipient,
1545
+ message
1546
+ });
1547
+ } else if (message.data?.signalBatch) {
1548
+ await this._handleSignal({
1549
+ author,
1550
+ recipient,
1551
+ message
1552
+ });
1553
+ } else {
1554
+ import_log3.log.warn("unknown message", {
1555
+ message
1556
+ }, {
1557
+ F: __dxlog_file3,
1558
+ L: 80,
1559
+ S: this,
1560
+ C: (f, a) => f(...a)
1561
+ });
1562
+ }
1563
+ }
1564
+ async signal(message) {
1565
+ (0, import_invariant3.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1566
+ F: __dxlog_file3,
1567
+ L: 85,
1568
+ S: this,
1569
+ A: [
1570
+ "message.data?.signal || message.data?.signalBatch",
1571
+ "'Invalid message'"
1572
+ ]
1573
+ });
1574
+ await this._sendReliableMessage({
1575
+ author: message.author,
1576
+ recipient: message.recipient,
1577
+ message
1578
+ });
1579
+ }
1580
+ async offer(message) {
1581
+ const networkMessage = {
1582
+ ...message,
1583
+ messageId: import_keys3.PublicKey.random()
1584
+ };
1585
+ return new Promise((resolve, reject) => {
1586
+ this._offerRecords.set(networkMessage.messageId, {
1587
+ resolve
1588
+ });
1589
+ this._sendReliableMessage({
1590
+ author: message.author,
1591
+ recipient: message.recipient,
1592
+ message: networkMessage
1593
+ }).catch((err) => reject(err));
1594
+ });
1595
+ }
1596
+ async _sendReliableMessage({ author, recipient, message }) {
1597
+ const networkMessage = {
1598
+ ...message,
1599
+ // Setting unique message_id if it not specified yet.
1600
+ messageId: message.messageId ?? import_keys3.PublicKey.random()
1601
+ };
1602
+ (0, import_log3.log)("sending", {
1603
+ from: author,
1604
+ to: recipient,
1605
+ msg: networkMessage
1606
+ }, {
1607
+ F: __dxlog_file3,
1608
+ L: 123,
1609
+ S: this,
1610
+ C: (f, a) => f(...a)
1611
+ });
1612
+ await this._sendMessage({
1613
+ author,
1614
+ recipient,
1615
+ payload: {
1616
+ type_url: "dxos.mesh.swarm.SwarmMessage",
1617
+ value: SwarmMessage.encode(networkMessage)
1618
+ }
1619
+ });
1620
+ }
1621
+ async _resolveAnswers(message) {
1622
+ (0, import_invariant3.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1623
+ F: __dxlog_file3,
1624
+ L: 135,
1625
+ S: this,
1626
+ A: [
1627
+ "message.data?.answer?.offerMessageId",
1628
+ "'No offerMessageId'"
1629
+ ]
1630
+ });
1631
+ const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1632
+ if (offerRecord) {
1633
+ this._offerRecords.delete(message.data.answer.offerMessageId);
1634
+ (0, import_invariant3.invariant)(message.data?.answer, "No answer", {
1635
+ F: __dxlog_file3,
1636
+ L: 139,
1637
+ S: this,
1638
+ A: [
1639
+ "message.data?.answer",
1640
+ "'No answer'"
1641
+ ]
1642
+ });
1643
+ (0, import_log3.log)("resolving", {
1644
+ answer: message.data.answer
1645
+ }, {
1646
+ F: __dxlog_file3,
1647
+ L: 140,
1648
+ S: this,
1649
+ C: (f, a) => f(...a)
1650
+ });
1651
+ offerRecord.resolve(message.data.answer);
1652
+ }
1653
+ }
1654
+ async _handleOffer({ author, recipient, message }) {
1655
+ (0, import_invariant3.invariant)(message.data.offer, "No offer", {
1656
+ F: __dxlog_file3,
1657
+ L: 154,
1658
+ S: this,
1659
+ A: [
1660
+ "message.data.offer",
1661
+ "'No offer'"
1662
+ ]
1663
+ });
1664
+ const offerMessage = {
1665
+ author,
1666
+ recipient,
1667
+ ...message,
1668
+ data: {
1669
+ offer: message.data.offer
1670
+ }
1671
+ };
1672
+ const answer = await this._onOffer(offerMessage);
1673
+ answer.offerMessageId = message.messageId;
1674
+ try {
1675
+ await this._sendReliableMessage({
1676
+ author: recipient,
1677
+ recipient: author,
1678
+ message: {
1679
+ topic: message.topic,
1680
+ sessionId: message.sessionId,
1681
+ data: {
1682
+ answer
1683
+ }
1684
+ }
1685
+ });
1686
+ } catch (err) {
1687
+ import_log3.log.warn("error sending answer", {
1688
+ err
1689
+ }, {
1690
+ F: __dxlog_file3,
1691
+ L: 174,
1692
+ S: this,
1693
+ C: (f, a) => f(...a)
1694
+ });
1695
+ }
1696
+ }
1697
+ async _handleSignal({ author, recipient, message }) {
1698
+ (0, import_invariant3.invariant)(message.messageId, void 0, {
1699
+ F: __dxlog_file3,
1700
+ L: 187,
1701
+ S: this,
1702
+ A: [
1703
+ "message.messageId",
1704
+ ""
1705
+ ]
1706
+ });
1707
+ (0, import_invariant3.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1708
+ F: __dxlog_file3,
1709
+ L: 188,
1710
+ S: this,
1711
+ A: [
1712
+ "message.data.signal || message.data.signalBatch",
1713
+ "'Invalid message'"
1714
+ ]
1715
+ });
1716
+ const signalMessage = {
1717
+ author,
1718
+ recipient,
1719
+ ...message,
1720
+ data: {
1721
+ signal: message.data.signal,
1722
+ signalBatch: message.data.signalBatch
1723
+ }
1724
+ };
1725
+ await this._onSignal(signalMessage);
1726
+ }
1727
+ };
1728
+
1696
1729
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
1697
1730
  function _ts_decorate3(decorators, target, key, desc) {
1698
1731
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -1728,7 +1761,7 @@ var Swarm = class {
1728
1761
  this.disconnected = new import_async3.Event();
1729
1762
  this.connected = new import_async3.Event();
1730
1763
  this.errors = new import_debug2.ErrorStream();
1731
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.begin({
1764
+ import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.begin({
1732
1765
  id: this._instanceId,
1733
1766
  data: {
1734
1767
  topic: this._topic.toHex(),
@@ -1755,7 +1788,7 @@ var Swarm = class {
1755
1788
  onOffer: async (msg) => await this.onOffer(msg),
1756
1789
  topic: this._topic
1757
1790
  });
1758
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.end({
1791
+ import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.end({
1759
1792
  id: this._instanceId
1760
1793
  }), {
1761
1794
  F: __dxlog_file4,
@@ -2183,7 +2216,6 @@ var SwarmMapper = class {
2183
2216
  get peers() {
2184
2217
  return Array.from(this._peers.values());
2185
2218
  }
2186
- // prettier-ignore
2187
2219
  constructor(_swarm) {
2188
2220
  this._swarm = _swarm;
2189
2221
  this._subscriptions = new import_async4.EventSubscriptions();
@@ -2206,7 +2238,7 @@ var SwarmMapper = class {
2206
2238
  _update() {
2207
2239
  (0, import_log5.log)("updating swarm", void 0, {
2208
2240
  F: __dxlog_file5,
2209
- L: 75,
2241
+ L: 72,
2210
2242
  S: this,
2211
2243
  C: (f, a) => f(...a)
2212
2244
  });
@@ -2230,7 +2262,7 @@ var SwarmMapper = class {
2230
2262
  totalPeersInSwarm: this._peers.size
2231
2263
  }, {
2232
2264
  F: __dxlog_file5,
2233
- L: 116,
2265
+ L: 113,
2234
2266
  S: this,
2235
2267
  C: (f, a) => f(...a)
2236
2268
  });
@@ -2246,13 +2278,13 @@ var SwarmMapper = class {
2246
2278
  // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
2247
2279
  var import_async5 = require("@dxos/async");
2248
2280
  var import_context5 = require("@dxos/context");
2249
- var import_errors3 = require("@dxos/errors");
2250
2281
  var import_invariant5 = require("@dxos/invariant");
2251
2282
  var import_keys6 = require("@dxos/keys");
2252
2283
  var import_log6 = require("@dxos/log");
2284
+ var import_protocols5 = require("@dxos/protocols");
2253
2285
  var import_util4 = require("@dxos/util");
2254
2286
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2255
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
2287
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
2256
2288
  var ConnectionLimiter = class {
2257
2289
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2258
2290
  this._ctx = new import_context5.Context();
@@ -2319,7 +2351,7 @@ var ConnectionLimiter = class {
2319
2351
  if (!this._waitingPromises.has(sessionId)) {
2320
2352
  return;
2321
2353
  }
2322
- this._waitingPromises.get(sessionId).reject(new import_errors3.CancelledError());
2354
+ this._waitingPromises.get(sessionId).reject(new import_protocols5.CancelledError());
2323
2355
  this._waitingPromises.delete(sessionId);
2324
2356
  this.resolveWaitingPromises.schedule();
2325
2357
  }
@@ -2397,7 +2429,7 @@ var import_invariant6 = require("@dxos/invariant");
2397
2429
  var import_keys8 = require("@dxos/keys");
2398
2430
  var import_log7 = require("@dxos/log");
2399
2431
  var import_messaging = require("@dxos/messaging");
2400
- var import_protocols4 = require("@dxos/protocols");
2432
+ var import_protocols6 = require("@dxos/protocols");
2401
2433
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
2402
2434
  var import_util6 = require("@dxos/util");
2403
2435
  var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
@@ -2445,7 +2477,7 @@ var NetworkManager = class {
2445
2477
  return this._swarms.get(topic);
2446
2478
  }
2447
2479
  async open() {
2448
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
2480
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.begin({
2449
2481
  id: this._instanceId
2450
2482
  }), {
2451
2483
  F: __dxlog_file7,
@@ -2455,7 +2487,7 @@ var NetworkManager = class {
2455
2487
  });
2456
2488
  await this._messenger.open();
2457
2489
  await this._signalManager.open();
2458
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
2490
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.end({
2459
2491
  id: this._instanceId
2460
2492
  }), {
2461
2493
  F: __dxlog_file7,
@@ -2790,7 +2822,6 @@ var import_invariant9 = require("@dxos/invariant");
2790
2822
  var import_log9 = require("@dxos/log");
2791
2823
  var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2792
2824
  var StarTopology = class {
2793
- // prettier-ignore
2794
2825
  constructor(_centralPeer) {
2795
2826
  this._centralPeer = _centralPeer;
2796
2827
  }
@@ -2800,7 +2831,7 @@ var StarTopology = class {
2800
2831
  init(controller) {
2801
2832
  (0, import_invariant9.invariant)(!this._controller, "Already initialized.", {
2802
2833
  F: __dxlog_file10,
2803
- L: 24,
2834
+ L: 21,
2804
2835
  S: this,
2805
2836
  A: [
2806
2837
  "!this._controller",
@@ -2812,7 +2843,7 @@ var StarTopology = class {
2812
2843
  update() {
2813
2844
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2814
2845
  F: __dxlog_file10,
2815
- L: 29,
2846
+ L: 26,
2816
2847
  S: this,
2817
2848
  A: [
2818
2849
  "this._controller",
@@ -2823,7 +2854,7 @@ var StarTopology = class {
2823
2854
  if (!ownPeerId.equals(this._centralPeer)) {
2824
2855
  (0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
2825
2856
  F: __dxlog_file10,
2826
- L: 32,
2857
+ L: 29,
2827
2858
  S: this,
2828
2859
  C: (f, a) => f(...a)
2829
2860
  });
@@ -2833,7 +2864,7 @@ var StarTopology = class {
2833
2864
  peer
2834
2865
  }, {
2835
2866
  F: __dxlog_file10,
2836
- L: 37,
2867
+ L: 34,
2837
2868
  S: this,
2838
2869
  C: (f, a) => f(...a)
2839
2870
  });
@@ -2847,7 +2878,7 @@ var StarTopology = class {
2847
2878
  peer
2848
2879
  }, {
2849
2880
  F: __dxlog_file10,
2850
- L: 46,
2881
+ L: 43,
2851
2882
  S: this,
2852
2883
  C: (f, a) => f(...a)
2853
2884
  });
@@ -2858,7 +2889,7 @@ var StarTopology = class {
2858
2889
  async onOffer(peer) {
2859
2890
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2860
2891
  F: __dxlog_file10,
2861
- L: 53,
2892
+ L: 50,
2862
2893
  S: this,
2863
2894
  A: [
2864
2895
  "this._controller",
@@ -2872,7 +2903,7 @@ var StarTopology = class {
2872
2903
  isSelfCentral: ownPeerId.equals(this._centralPeer)
2873
2904
  }, {
2874
2905
  F: __dxlog_file10,
2875
- L: 55,
2906
+ L: 52,
2876
2907
  S: this,
2877
2908
  C: (f, a) => f(...a)
2878
2909
  });
@@ -3075,6 +3106,7 @@ var TransportKind;
3075
3106
  TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
3076
3107
  TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
3077
3108
  TransportKind2["MEMORY"] = "MEMORY";
3109
+ TransportKind2["TCP"] = "TCP";
3078
3110
  })(TransportKind || (TransportKind = {}));
3079
3111
 
3080
3112
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
@@ -3084,7 +3116,7 @@ var import_async9 = require("@dxos/async");
3084
3116
  var import_debug5 = require("@dxos/debug");
3085
3117
  var import_keys10 = require("@dxos/keys");
3086
3118
  var import_log11 = require("@dxos/log");
3087
- var import_protocols5 = require("@dxos/protocols");
3119
+ var import_protocols7 = require("@dxos/protocols");
3088
3120
 
3089
3121
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
3090
3122
  var wrtc = null;
@@ -3103,17 +3135,17 @@ var SimplePeerTransport = class {
3103
3135
  this.connected = new import_async9.Event();
3104
3136
  this.errors = new import_debug5.ErrorStream();
3105
3137
  this._instanceId = import_keys10.PublicKey.random().toHex();
3106
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
3138
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.begin({
3107
3139
  id: this._instanceId
3108
3140
  }), {
3109
3141
  F: __dxlog_file12,
3110
- L: 39,
3142
+ L: 40,
3111
3143
  S: this,
3112
3144
  C: (f, a) => f(...a)
3113
3145
  });
3114
3146
  (0, import_log11.log)("created connection", params, {
3115
3147
  F: __dxlog_file12,
3116
- L: 40,
3148
+ L: 41,
3117
3149
  S: this,
3118
3150
  C: (f, a) => f(...a)
3119
3151
  });
@@ -3126,7 +3158,7 @@ var SimplePeerTransport = class {
3126
3158
  this._peer.on("signal", async (data) => {
3127
3159
  (0, import_log11.log)("signal", data, {
3128
3160
  F: __dxlog_file12,
3129
- L: 49,
3161
+ L: 50,
3130
3162
  S: this,
3131
3163
  C: (f, a) => f(...a)
3132
3164
  });
@@ -3139,7 +3171,7 @@ var SimplePeerTransport = class {
3139
3171
  this._peer.on("connect", () => {
3140
3172
  (0, import_log11.log)("connected", void 0, {
3141
3173
  F: __dxlog_file12,
3142
- L: 54,
3174
+ L: 55,
3143
3175
  S: this,
3144
3176
  C: (f, a) => f(...a)
3145
3177
  });
@@ -3149,7 +3181,7 @@ var SimplePeerTransport = class {
3149
3181
  this._peer.on("close", async () => {
3150
3182
  (0, import_log11.log)("closed", void 0, {
3151
3183
  F: __dxlog_file12,
3152
- L: 60,
3184
+ L: 61,
3153
3185
  S: this,
3154
3186
  C: (f, a) => f(...a)
3155
3187
  });
@@ -3157,30 +3189,74 @@ var SimplePeerTransport = class {
3157
3189
  this.closed.emit();
3158
3190
  });
3159
3191
  this._peer.on("error", async (err) => {
3160
- this.errors.raise(err);
3192
+ if (typeof RTCError !== "undefined" && err instanceof RTCError) {
3193
+ if (err.errorDetail === "sctp-failure") {
3194
+ this.errors.raise(new import_protocols7.ConnectionResetError("sctp-failure from RTCError", err));
3195
+ } else {
3196
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown RTCError", err));
3197
+ }
3198
+ } else if ("code" in err) {
3199
+ import_log11.log.info("simple-peer error", err, {
3200
+ F: __dxlog_file12,
3201
+ L: 77,
3202
+ S: this,
3203
+ C: (f, a) => f(...a)
3204
+ });
3205
+ switch (err.code) {
3206
+ case "ERR_WEBRTC_SUPPORT":
3207
+ this.errors.raise(new import_protocols7.ProtocolError("WebRTC not supported", err));
3208
+ case "ERR_ICE_CONNECTION_FAILURE":
3209
+ case "ERR_DATA_CHANNEL":
3210
+ case "ERR_CONNECTION_FAILURE":
3211
+ case "ERR_SIGNALING":
3212
+ this.errors.raise(new import_protocols7.ConnectivityError("unknown communication failure", err));
3213
+ case "ERR_CREATE_OFFER":
3214
+ case "ERR_CREATE_ANSWER":
3215
+ case "ERR_SET_LOCAL_DESCRIPTION":
3216
+ case "ERR_SET_REMOTE_DESCRIPTION":
3217
+ case "ERR_ADD_ICE_CANDIDATE":
3218
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown simple-peer library failure", err));
3219
+ default:
3220
+ this.errors.raise(new Error("unknown simple-peer error"));
3221
+ }
3222
+ } else {
3223
+ import_log11.log.info("unknown peer connection error", err, {
3224
+ F: __dxlog_file12,
3225
+ L: 96,
3226
+ S: this,
3227
+ C: (f, a) => f(...a)
3228
+ });
3229
+ this.errors.raise(err);
3230
+ }
3161
3231
  try {
3162
- if (typeof this._peer?._pc.getStats === "function") {
3232
+ if (typeof this._peer?._pc?.getStats === "function") {
3163
3233
  this._peer._pc.getStats().then((stats) => {
3164
3234
  import_log11.log.warn("report after webrtc error", {
3165
3235
  config: this.params.webrtcConfig,
3166
3236
  stats
3167
3237
  }, {
3168
3238
  F: __dxlog_file12,
3169
- L: 72,
3239
+ L: 104,
3170
3240
  S: this,
3171
3241
  C: (f, a) => f(...a)
3172
3242
  });
3173
3243
  });
3174
3244
  }
3175
- } catch {
3245
+ } catch (err2) {
3246
+ import_log11.log.catch(err2, void 0, {
3247
+ F: __dxlog_file12,
3248
+ L: 111,
3249
+ S: this,
3250
+ C: (f, a) => f(...a)
3251
+ });
3176
3252
  }
3177
3253
  await this.destroy();
3178
3254
  });
3179
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
3255
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.end({
3180
3256
  id: this._instanceId
3181
3257
  }), {
3182
3258
  F: __dxlog_file12,
3183
- L: 82,
3259
+ L: 116,
3184
3260
  S: this,
3185
3261
  C: (f, a) => f(...a)
3186
3262
  });
@@ -3188,7 +3264,7 @@ var SimplePeerTransport = class {
3188
3264
  async destroy() {
3189
3265
  (0, import_log11.log)("closing...", void 0, {
3190
3266
  F: __dxlog_file12,
3191
- L: 86,
3267
+ L: 120,
3192
3268
  S: this,
3193
3269
  C: (f, a) => f(...a)
3194
3270
  });
@@ -3198,7 +3274,7 @@ var SimplePeerTransport = class {
3198
3274
  this.closed.emit();
3199
3275
  (0, import_log11.log)("closed", void 0, {
3200
3276
  F: __dxlog_file12,
3201
- L: 91,
3277
+ L: 125,
3202
3278
  S: this,
3203
3279
  C: (f, a) => f(...a)
3204
3280
  });
@@ -3231,7 +3307,6 @@ var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
3231
3307
  var import_util8 = require("@dxos/util");
3232
3308
  var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3233
3309
  var SimplePeerTransportService = class {
3234
- // prettier-ignore
3235
3310
  constructor(_webrtcConfig) {
3236
3311
  this._webrtcConfig = _webrtcConfig;
3237
3312
  this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
@@ -3311,7 +3386,7 @@ var SimplePeerTransportService = class {
3311
3386
  async sendSignal({ proxyId, signal }) {
3312
3387
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3313
3388
  F: __dxlog_file13,
3314
- L: 113,
3389
+ L: 110,
3315
3390
  S: this,
3316
3391
  A: [
3317
3392
  "this.transports.has(proxyId)",
@@ -3323,7 +3398,7 @@ var SimplePeerTransportService = class {
3323
3398
  async sendData({ proxyId, payload }) {
3324
3399
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3325
3400
  F: __dxlog_file13,
3326
- L: 118,
3401
+ L: 115,
3327
3402
  S: this,
3328
3403
  A: [
3329
3404
  "this.transports.has(proxyId)",
@@ -3344,7 +3419,7 @@ var SimplePeerTransportService = class {
3344
3419
  this.transports.delete(proxyId);
3345
3420
  (0, import_log12.log)("Closed.", void 0, {
3346
3421
  F: __dxlog_file13,
3347
- L: 132,
3422
+ L: 129,
3348
3423
  S: this,
3349
3424
  C: (f, a) => f(...a)
3350
3425
  });
@@ -3359,11 +3434,11 @@ var import_debug6 = require("@dxos/debug");
3359
3434
  var import_invariant12 = require("@dxos/invariant");
3360
3435
  var import_keys12 = require("@dxos/keys");
3361
3436
  var import_log13 = require("@dxos/log");
3437
+ var import_protocols8 = require("@dxos/protocols");
3362
3438
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
3363
3439
  var import_util9 = require("@dxos/util");
3364
3440
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3365
3441
  var SimplePeerTransportProxy = class {
3366
- // prettier-ignore
3367
3442
  constructor(_params) {
3368
3443
  this._params = _params;
3369
3444
  this._proxyId = import_keys12.PublicKey.random();
@@ -3380,7 +3455,7 @@ var SimplePeerTransportProxy = class {
3380
3455
  this._serviceStream.subscribe(async (event) => {
3381
3456
  (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
3382
3457
  F: __dxlog_file14,
3383
- L: 51,
3458
+ L: 55,
3384
3459
  S: this,
3385
3460
  C: (f, a) => f(...a)
3386
3461
  });
@@ -3400,7 +3475,7 @@ var SimplePeerTransportProxy = class {
3400
3475
  }).then(() => callback(), (err) => {
3401
3476
  import_log13.log.catch(err, void 0, {
3402
3477
  F: __dxlog_file14,
3403
- L: 69,
3478
+ L: 76,
3404
3479
  S: this,
3405
3480
  C: (f, a) => f(...a)
3406
3481
  });
@@ -3409,14 +3484,14 @@ var SimplePeerTransportProxy = class {
3409
3484
  }));
3410
3485
  }, (error) => import_log13.log.catch(error, void 0, {
3411
3486
  F: __dxlog_file14,
3412
- L: 75,
3487
+ L: 83,
3413
3488
  S: this,
3414
3489
  C: (f, a) => f(...a)
3415
3490
  }));
3416
3491
  }
3417
3492
  async _handleConnection(connectionEvent) {
3418
3493
  if (connectionEvent.error) {
3419
- this.errors.raise(new Error(connectionEvent.error));
3494
+ this.errors.raise(decodeError(connectionEvent.error));
3420
3495
  }
3421
3496
  switch (connectionEvent.state) {
3422
3497
  case import_bridge2.ConnectionState.CONNECTED: {
@@ -3439,7 +3514,7 @@ var SimplePeerTransportProxy = class {
3439
3514
  this._params.bridgeService.sendSignal({
3440
3515
  proxyId: this._proxyId,
3441
3516
  signal
3442
- }).catch((err) => this.errors.raise(err));
3517
+ }).catch((err) => this.errors.raise(decodeError(err)));
3443
3518
  }
3444
3519
  // TODO(burdon): Move open from constructor.
3445
3520
  async destroy() {
@@ -3455,7 +3530,7 @@ var SimplePeerTransportProxy = class {
3455
3530
  } catch (err) {
3456
3531
  import_log13.log.catch(err, void 0, {
3457
3532
  F: __dxlog_file14,
3458
- L: 126,
3533
+ L: 134,
3459
3534
  S: this,
3460
3535
  C: (f, a) => f(...a)
3461
3536
  });
@@ -3491,7 +3566,7 @@ var SimplePeerTransportProxyFactory = class {
3491
3566
  createTransport(options) {
3492
3567
  (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3493
3568
  F: __dxlog_file14,
3494
- L: 163,
3569
+ L: 171,
3495
3570
  S: this,
3496
3571
  A: [
3497
3572
  "this._bridgeService",
@@ -3507,6 +3582,22 @@ var SimplePeerTransportProxyFactory = class {
3507
3582
  return transport;
3508
3583
  }
3509
3584
  };
3585
+ var decodeError = (err) => {
3586
+ const message = typeof err === "string" ? err : err.message;
3587
+ if (message.includes("CONNECTION_RESET")) {
3588
+ return new import_protocols8.ConnectionResetError(message);
3589
+ } else if (message.includes("TIMEOUT")) {
3590
+ return new import_protocols8.TimeoutError(message);
3591
+ } else if (message.includes("PROTOCOL_ERROR")) {
3592
+ return new import_protocols8.ProtocolError(message);
3593
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3594
+ return new import_protocols8.ConnectivityError(message);
3595
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3596
+ return new import_protocols8.UnknownProtocolError(message);
3597
+ } else {
3598
+ return typeof err === "string" ? new Error(err) : err;
3599
+ }
3600
+ };
3510
3601
 
3511
3602
  // packages/core/mesh/network-manager/src/transport/index.ts
3512
3603
  init_datachannel();