@dxos/network-manager 0.1.57 → 0.1.58-main.0009f6a

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 (56) hide show
  1. package/dist/lib/browser/{chunk-UKMTAO4L.mjs → chunk-26KK6XOP.mjs} +266 -140
  2. package/dist/lib/browser/chunk-26KK6XOP.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 +10 -7
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/node/index.cjs +837 -577
  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 +1125 -865
  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/index.d.ts +1 -0
  28. package/dist/types/src/transport/index.d.ts.map +1 -1
  29. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  30. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  31. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  32. package/dist/types/src/transport/tcp-transport.d.ts +23 -0
  33. package/dist/types/src/transport/tcp-transport.d.ts.map +1 -0
  34. package/dist/types/src/transport/transport.d.ts +2 -1
  35. package/dist/types/src/transport/transport.d.ts.map +1 -1
  36. package/package.json +18 -18
  37. package/src/swarm/connection-limiter.ts +2 -2
  38. package/src/swarm/connection.test.ts +1 -1
  39. package/src/swarm/connection.ts +54 -27
  40. package/src/swarm/peer.ts +11 -3
  41. package/src/swarm/swarm-mapper.ts +4 -7
  42. package/src/swarm/swarm.test.ts +3 -3
  43. package/src/swarm/swarm.ts +3 -3
  44. package/src/testing/test-builder.ts +6 -2
  45. package/src/tests/basic-test-suite.ts +1 -1
  46. package/src/tests/memory-transport.test.ts +2 -2
  47. package/src/tests/tcp-transport.test.ts +67 -0
  48. package/src/tests/webrtc-transport.test.ts +1 -1
  49. package/src/topology/star-topology.ts +1 -4
  50. package/src/transport/index.ts +1 -0
  51. package/src/transport/simplepeer-transport-proxy.ts +48 -21
  52. package/src/transport/simplepeer-transport-service.ts +9 -5
  53. package/src/transport/simplepeer-transport.ts +43 -4
  54. package/src/transport/tcp-transport.ts +107 -0
  55. package/src/transport/transport.ts +1 -0
  56. package/dist/lib/browser/chunk-UKMTAO4L.mjs.map +0 -7
@@ -527,6 +527,8 @@ __export(src_exports, {
527
527
  Swarm: () => Swarm,
528
528
  SwarmMapper: () => SwarmMapper,
529
529
  SwarmMessenger: () => SwarmMessenger,
530
+ TcpTransport: () => TcpTransport,
531
+ TcpTransportFactory: () => TcpTransportFactory,
530
532
  TransportKind: () => TransportKind,
531
533
  cleanup: () => cleanup,
532
534
  createLibDataChannelTransportFactory: () => createLibDataChannelTransportFactory,
@@ -545,7 +547,6 @@ var import_util5 = require("@dxos/util");
545
547
  var import_async = require("@dxos/async");
546
548
  var import_context = require("@dxos/context");
547
549
  var import_debug = require("@dxos/debug");
548
- var import_errors = require("@dxos/errors");
549
550
  var import_invariant = require("@dxos/invariant");
550
551
  var import_keys = require("@dxos/keys");
551
552
  var import_log = require("@dxos/log");
@@ -560,7 +561,7 @@ function _ts_decorate(decorators, target, key, desc) {
560
561
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
561
562
  return c > 3 && r && Object.defineProperty(target, key, r), r;
562
563
  }
563
- var __dxlog_file = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/connection.ts";
564
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
564
565
  var STARTING_SIGNALLING_DELAY = 10;
565
566
  var MAX_SIGNALLING_DELAY = 300;
566
567
  var ConnectionState;
@@ -602,6 +603,8 @@ var ConnectionState;
602
603
  */
603
604
  "CLOSED"
604
605
  ] = "CLOSED";
606
+ ConnectionState5["ABORTING"] = "ABORTING";
607
+ ConnectionState5["ABORTED"] = "ABORTED";
605
608
  })(ConnectionState || (ConnectionState = {}));
606
609
  var Connection = class {
607
610
  constructor(topic, ownId, remoteId, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
@@ -633,7 +636,7 @@ var Connection = class {
633
636
  initiator: this.initiator
634
637
  }, {
635
638
  F: __dxlog_file,
636
- L: 114,
639
+ L: 123,
637
640
  S: this,
638
641
  C: (f, a) => f(...a)
639
642
  });
@@ -653,7 +656,7 @@ var Connection = class {
653
656
  async openConnection() {
654
657
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
655
658
  F: __dxlog_file,
656
- L: 139,
659
+ L: 148,
657
660
  S: this,
658
661
  A: [
659
662
  "this._state === ConnectionState.INITIAL",
@@ -664,7 +667,7 @@ var Connection = class {
664
667
  id: this._instanceId
665
668
  }), {
666
669
  F: __dxlog_file,
667
- L: 140,
670
+ L: 149,
668
671
  S: this,
669
672
  C: (f, a) => f(...a)
670
673
  });
@@ -676,7 +679,7 @@ var Connection = class {
676
679
  initiator: this.initiator
677
680
  }, {
678
681
  F: __dxlog_file,
679
- L: 141,
682
+ L: 150,
680
683
  S: this,
681
684
  C: (f, a) => f(...a)
682
685
  });
@@ -687,15 +690,15 @@ var Connection = class {
687
690
  this._protocol.stream.on("close", () => {
688
691
  (0, import_log.log)("protocol stream closed", void 0, {
689
692
  F: __dxlog_file,
690
- L: 158,
693
+ L: 167,
691
694
  S: this,
692
695
  C: (f, a) => f(...a)
693
696
  });
694
- this.close().catch((err) => this.errors.raise(err));
697
+ this.close(new import_protocols.ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
695
698
  });
696
699
  (0, import_invariant.invariant)(!this._transport, void 0, {
697
700
  F: __dxlog_file,
698
- L: 162,
701
+ L: 171,
699
702
  S: this,
700
703
  A: [
701
704
  "!this._transport",
@@ -715,7 +718,7 @@ var Connection = class {
715
718
  this._transport = void 0;
716
719
  (0, import_log.log)("abort triggered by transport close", void 0, {
717
720
  F: __dxlog_file,
718
- L: 176,
721
+ L: 185,
719
722
  S: this,
720
723
  C: (f, a) => f(...a)
721
724
  });
@@ -726,21 +729,38 @@ var Connection = class {
726
729
  err
727
730
  }, {
728
731
  F: __dxlog_file,
729
- L: 181,
732
+ L: 190,
730
733
  S: this,
731
734
  C: (f, a) => f(...a)
732
735
  });
733
736
  if (!this.closeReason) {
734
737
  this.closeReason = err?.message;
735
738
  }
736
- if (err?.message.includes("OperationError")) {
737
- (0, import_log.log)("aborting due to transport OperationError", void 0, {
739
+ if (err instanceof import_protocols.ConnectionResetError) {
740
+ import_log.log.warn("aborting due to transport ConnectionResetError", void 0, {
741
+ F: __dxlog_file,
742
+ L: 197,
743
+ S: this,
744
+ C: (f, a) => f(...a)
745
+ });
746
+ this.abort().catch((err2) => this.errors.raise(err2));
747
+ } else if (err instanceof import_protocols.ConnectivityError) {
748
+ import_log.log.warn("aborting due to transport ConnectivityError", void 0, {
738
749
  F: __dxlog_file,
739
- L: 187,
750
+ L: 200,
740
751
  S: this,
741
752
  C: (f, a) => f(...a)
742
753
  });
743
754
  this.abort().catch((err2) => this.errors.raise(err2));
755
+ } else if (err instanceof import_protocols.UnknownProtocolError) {
756
+ import_log.log.warn("unsure what to do with UnknownProtocolError, will keep on truckin", {
757
+ err
758
+ }, {
759
+ F: __dxlog_file,
760
+ L: 203,
761
+ S: this,
762
+ C: (f, a) => f(...a)
763
+ });
744
764
  }
745
765
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
746
766
  this.errors.raise(err);
@@ -754,7 +774,7 @@ var Connection = class {
754
774
  id: this._instanceId
755
775
  }), {
756
776
  F: __dxlog_file,
757
- L: 203,
777
+ L: 218,
758
778
  S: this,
759
779
  C: (f, a) => f(...a)
760
780
  });
@@ -764,30 +784,30 @@ var Connection = class {
764
784
  err
765
785
  }, {
766
786
  F: __dxlog_file,
767
- L: 209,
787
+ L: 225,
768
788
  S: this,
769
789
  C: (f, a) => f(...a)
770
790
  });
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, {
791
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
792
+ (0, import_log.log)(`abort ignored: already ${this._state}`, this.closeReason, {
776
793
  F: __dxlog_file,
777
- L: 214,
794
+ L: 227,
778
795
  S: this,
779
796
  C: (f, a) => f(...a)
780
797
  });
781
798
  return;
782
799
  }
783
- this._changeState(ConnectionState.CLOSING);
800
+ this._changeState(ConnectionState.ABORTING);
801
+ if (!this.closeReason) {
802
+ this.closeReason = err?.message;
803
+ }
784
804
  await this._ctx.dispose();
785
805
  try {
786
806
  (0, import_log.log)("aborting protocol... ", {
787
807
  proto: this._protocol
788
808
  }, {
789
809
  F: __dxlog_file,
790
- L: 224,
810
+ L: 240,
791
811
  S: this,
792
812
  C: (f, a) => f(...a)
793
813
  });
@@ -795,7 +815,7 @@ var Connection = class {
795
815
  } catch (err2) {
796
816
  import_log.log.catch(err2, void 0, {
797
817
  F: __dxlog_file,
798
- L: 227,
818
+ L: 243,
799
819
  S: this,
800
820
  C: (f, a) => f(...a)
801
821
  });
@@ -805,56 +825,68 @@ var Connection = class {
805
825
  } catch (err2) {
806
826
  import_log.log.catch(err2, void 0, {
807
827
  F: __dxlog_file,
808
- L: 234,
828
+ L: 250,
809
829
  S: this,
810
830
  C: (f, a) => f(...a)
811
831
  });
812
832
  }
813
- this._changeState(ConnectionState.CLOSED);
814
- this._callbacks?.onClosed?.(err);
833
+ try {
834
+ this._callbacks?.onClosed?.(err);
835
+ } catch (err2) {
836
+ import_log.log.catch(err2, void 0, {
837
+ F: __dxlog_file,
838
+ L: 256,
839
+ S: this,
840
+ C: (f, a) => f(...a)
841
+ });
842
+ }
843
+ this._changeState(ConnectionState.ABORTED);
815
844
  }
816
845
  async close(err) {
817
846
  if (!this.closeReason) {
818
847
  this.closeReason = err?.message;
819
848
  }
820
- if (this._state === ConnectionState.CLOSED) {
849
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTING || this._state === ConnectionState.ABORTED) {
821
850
  return;
822
851
  }
852
+ const lastState = this._state;
823
853
  this._changeState(ConnectionState.CLOSING);
824
854
  await this._ctx.dispose();
825
855
  (0, import_log.log)("closing...", {
826
856
  peerId: this.ownId
827
857
  }, {
828
858
  F: __dxlog_file,
829
- L: 253,
859
+ L: 278,
830
860
  S: this,
831
861
  C: (f, a) => f(...a)
832
862
  });
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
- });
863
+ if (lastState === ConnectionState.CONNECTED) {
864
+ try {
865
+ await this._protocol.close();
866
+ } catch (err2) {
867
+ import_log.log.catch(err2, void 0, {
868
+ F: __dxlog_file,
869
+ L: 285,
870
+ S: this,
871
+ C: (f, a) => f(...a)
872
+ });
873
+ }
874
+ try {
875
+ await this._transport?.destroy();
876
+ } catch (err2) {
877
+ import_log.log.catch(err2, void 0, {
878
+ F: __dxlog_file,
879
+ L: 292,
880
+ S: this,
881
+ C: (f, a) => f(...a)
882
+ });
883
+ }
852
884
  }
853
885
  (0, import_log.log)("closed", {
854
886
  peerId: this.ownId
855
887
  }, {
856
888
  F: __dxlog_file,
857
- L: 269,
889
+ L: 296,
858
890
  S: this,
859
891
  C: (f, a) => f(...a)
860
892
  });
@@ -890,14 +922,14 @@ var Connection = class {
890
922
  }
891
923
  });
892
924
  } catch (err) {
893
- if (err instanceof import_errors.CancelledError) {
925
+ if (err instanceof import_protocols.CancelledError) {
894
926
  return;
895
927
  }
896
928
  import_log.log.warn("signal failed", {
897
929
  err
898
930
  }, {
899
931
  F: __dxlog_file,
900
- L: 306,
932
+ L: 333,
901
933
  S: this,
902
934
  C: (f, a) => f(...a)
903
935
  });
@@ -910,7 +942,7 @@ var Connection = class {
910
942
  async signal(msg) {
911
943
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
912
944
  F: __dxlog_file,
913
- L: 315,
945
+ L: 342,
914
946
  S: this,
915
947
  A: [
916
948
  "msg.sessionId",
@@ -920,7 +952,7 @@ var Connection = class {
920
952
  if (!msg.sessionId.equals(this.sessionId)) {
921
953
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
922
954
  F: __dxlog_file,
923
- L: 317,
955
+ L: 344,
924
956
  S: this,
925
957
  C: (f, a) => f(...a)
926
958
  });
@@ -928,7 +960,7 @@ var Connection = class {
928
960
  }
929
961
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
930
962
  F: __dxlog_file,
931
- L: 320,
963
+ L: 347,
932
964
  S: this,
933
965
  A: [
934
966
  "msg.data.signal || msg.data.signalBatch",
@@ -937,7 +969,7 @@ var Connection = class {
937
969
  });
938
970
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
939
971
  F: __dxlog_file,
940
- L: 321,
972
+ L: 348,
941
973
  S: this,
942
974
  A: [
943
975
  "msg.author?.equals(this.remoteId)",
@@ -946,7 +978,7 @@ var Connection = class {
946
978
  });
947
979
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
948
980
  F: __dxlog_file,
949
- L: 322,
981
+ L: 349,
950
982
  S: this,
951
983
  A: [
952
984
  "msg.recipient?.equals(this.ownId)",
@@ -970,7 +1002,7 @@ var Connection = class {
970
1002
  msg: msg.data
971
1003
  }, {
972
1004
  F: __dxlog_file,
973
- L: 331,
1005
+ L: 358,
974
1006
  S: this,
975
1007
  C: (f, a) => f(...a)
976
1008
  });
@@ -978,7 +1010,7 @@ var Connection = class {
978
1010
  } else {
979
1011
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
980
1012
  F: __dxlog_file,
981
- L: 334,
1013
+ L: 361,
982
1014
  S: this,
983
1015
  A: [
984
1016
  "this._transport",
@@ -991,7 +1023,7 @@ var Connection = class {
991
1023
  msg: msg.data
992
1024
  }, {
993
1025
  F: __dxlog_file,
994
- L: 335,
1026
+ L: 362,
995
1027
  S: this,
996
1028
  C: (f, a) => f(...a)
997
1029
  });
@@ -1005,17 +1037,17 @@ var Connection = class {
1005
1037
  _changeState(state) {
1006
1038
  (0, import_log.log)("stateChanged", {
1007
1039
  from: this._state,
1008
- too: state,
1040
+ to: state,
1009
1041
  peerId: this.ownId
1010
1042
  }, {
1011
1043
  F: __dxlog_file,
1012
- L: 346,
1044
+ L: 373,
1013
1045
  S: this,
1014
1046
  C: (f, a) => f(...a)
1015
1047
  });
1016
1048
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
1017
1049
  F: __dxlog_file,
1018
- L: 347,
1050
+ L: 374,
1019
1051
  S: this,
1020
1052
  A: [
1021
1053
  "state !== this._state",
@@ -1040,382 +1072,164 @@ var import_debug2 = require("@dxos/debug");
1040
1072
  var import_invariant4 = require("@dxos/invariant");
1041
1073
  var import_keys4 = require("@dxos/keys");
1042
1074
  var import_log4 = require("@dxos/log");
1043
- var import_protocols3 = require("@dxos/protocols");
1075
+ var import_protocols4 = require("@dxos/protocols");
1044
1076
  var import_util2 = require("@dxos/util");
1045
1077
 
1046
- // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1078
+ // packages/core/mesh/network-manager/src/swarm/peer.ts
1079
+ var import_async2 = require("@dxos/async");
1047
1080
  var import_context2 = require("@dxos/context");
1048
1081
  var import_invariant2 = require("@dxos/invariant");
1049
1082
  var import_keys2 = require("@dxos/keys");
1050
1083
  var import_log2 = require("@dxos/log");
1051
1084
  var import_protocols2 = require("@dxos/protocols");
1052
- var import_util = require("@dxos/util");
1053
- var __dxlog_file2 = "/mnt/ramdisk/work/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 }) {
1085
+ function _ts_decorate2(decorators, target, key, desc) {
1086
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1087
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1088
+ r = Reflect.decorate(decorators, target, key, desc);
1089
+ else
1090
+ for (var i = decorators.length - 1; i >= 0; i--)
1091
+ if (d = decorators[i])
1092
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1093
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1094
+ }
1095
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1096
+ var ConnectionDisplacedError = class extends import_protocols2.SystemError {
1097
+ constructor() {
1098
+ super("Connection displaced by remote initiator.");
1099
+ }
1100
+ };
1101
+ var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
1102
+ var Peer = class {
1103
+ // TODO(burdon): Convert to map?
1104
+ constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
1105
+ this.id = id;
1106
+ this.topic = topic;
1107
+ this.localPeerId = localPeerId;
1108
+ this._signalMessaging = _signalMessaging;
1109
+ this._protocolProvider = _protocolProvider;
1110
+ this._transportFactory = _transportFactory;
1111
+ this._connectionLimiter = _connectionLimiter;
1112
+ this._callbacks = _callbacks;
1113
+ this._availableAfter = 0;
1114
+ this.availableToConnect = true;
1057
1115
  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;
1116
+ this.advertizing = false;
1117
+ this.initiating = false;
1118
+ this.connectionDisplaced = new import_async2.Event();
1063
1119
  }
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
- }, {
1120
+ /**
1121
+ * Respond to remote offer.
1122
+ */
1123
+ async onOffer(message) {
1124
+ const remoteId = message.author;
1125
+ if (this.connection && ![
1126
+ ConnectionState.CREATED,
1127
+ ConnectionState.INITIAL
1128
+ ].includes(this.connection.state)) {
1129
+ import_log2.log.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
1106
1130
  F: __dxlog_file2,
1107
- L: 80,
1131
+ L: 112,
1108
1132
  S: this,
1109
1133
  C: (f, a) => f(...a)
1110
1134
  });
1135
+ return {
1136
+ accept: false
1137
+ };
1138
+ }
1139
+ if (this.connection || this.initiating) {
1140
+ if (remoteId.toHex() < this.localPeerId.toHex()) {
1141
+ (0, import_log2.log)("close local connection", {
1142
+ localPeerId: this.id,
1143
+ topic: this.topic,
1144
+ remotePeerId: this.localPeerId,
1145
+ sessionId: this.connection?.sessionId
1146
+ }, {
1147
+ F: __dxlog_file2,
1148
+ L: 121,
1149
+ S: this,
1150
+ C: (f, a) => f(...a)
1151
+ });
1152
+ if (this.connection) {
1153
+ await this.closeConnection(new ConnectionDisplacedError());
1154
+ }
1155
+ } else {
1156
+ return {
1157
+ accept: false
1158
+ };
1159
+ }
1160
+ }
1161
+ if (await this._callbacks.onOffer(remoteId)) {
1162
+ if (!this.connection) {
1163
+ (0, import_invariant2.invariant)(message.sessionId, void 0, {
1164
+ F: __dxlog_file2,
1165
+ L: 141,
1166
+ S: this,
1167
+ A: [
1168
+ "message.sessionId",
1169
+ ""
1170
+ ]
1171
+ });
1172
+ const connection = this._createConnection(false, message.sessionId);
1173
+ try {
1174
+ await this._connectionLimiter.connecting(message.sessionId);
1175
+ connection.initiate();
1176
+ await connection.openConnection();
1177
+ } catch (err) {
1178
+ if (!(err instanceof import_protocols2.CancelledError)) {
1179
+ import_log2.log.warn("connection error", {
1180
+ topic: this.topic,
1181
+ peerId: this.localPeerId,
1182
+ remoteId: this.id,
1183
+ err
1184
+ }, {
1185
+ F: __dxlog_file2,
1186
+ L: 151,
1187
+ S: this,
1188
+ C: (f, a) => f(...a)
1189
+ });
1190
+ }
1191
+ await this.closeConnection(err);
1192
+ }
1193
+ return {
1194
+ accept: true
1195
+ };
1196
+ }
1111
1197
  }
1198
+ return {
1199
+ accept: false
1200
+ };
1112
1201
  }
1113
- async signal(message) {
1114
- (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1202
+ /**
1203
+ * Initiate a connection to the remote peer.
1204
+ */
1205
+ async initiateConnection() {
1206
+ (0, import_invariant2.invariant)(!this.initiating, "Initiation in progress.", {
1115
1207
  F: __dxlog_file2,
1116
- L: 85,
1208
+ L: 168,
1117
1209
  S: this,
1118
1210
  A: [
1119
- "message.data?.signal || message.data?.signalBatch",
1120
- "'Invalid message'"
1211
+ "!this.initiating",
1212
+ "'Initiation in progress.'"
1121
1213
  ]
1122
1214
  });
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
- }, {
1215
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1156
1216
  F: __dxlog_file2,
1157
- L: 123,
1217
+ L: 169,
1158
1218
  S: this,
1159
- C: (f, a) => f(...a)
1219
+ A: [
1220
+ "!this.connection",
1221
+ "'Already connected.'"
1222
+ ]
1160
1223
  });
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
- function _ts_decorate2(decorators, target, key, desc) {
1286
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1287
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
1288
- r = Reflect.decorate(decorators, target, key, desc);
1289
- else
1290
- for (var i = decorators.length - 1; i >= 0; i--)
1291
- if (d = decorators[i])
1292
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1293
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1294
- }
1295
- var __dxlog_file3 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/peer.ts";
1296
- var ConnectionDisplacedError = class extends import_errors2.SystemError {
1297
- constructor() {
1298
- super("Connection displaced by remote initiator.");
1299
- }
1300
- };
1301
- var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
1302
- var Peer = class {
1303
- // TODO(burdon): Convert to map?
1304
- constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
1305
- this.id = id;
1306
- this.topic = topic;
1307
- this.localPeerId = localPeerId;
1308
- this._signalMessaging = _signalMessaging;
1309
- this._protocolProvider = _protocolProvider;
1310
- this._transportFactory = _transportFactory;
1311
- this._connectionLimiter = _connectionLimiter;
1312
- this._callbacks = _callbacks;
1313
- this._availableAfter = 0;
1314
- this.availableToConnect = true;
1315
- this._ctx = new import_context3.Context();
1316
- this.advertizing = false;
1317
- this.initiating = false;
1318
- this.connectionDisplaced = new import_async2.Event();
1319
- }
1320
- /**
1321
- * Respond to remote offer.
1322
- */
1323
- async onOffer(message) {
1324
- const remoteId = message.author;
1325
- if (this.connection || this.initiating) {
1326
- if (remoteId.toHex() < this.localPeerId.toHex()) {
1327
- (0, import_log3.log)("close local connection", {
1328
- localPeerId: this.id,
1329
- topic: this.topic,
1330
- remotePeerId: this.localPeerId,
1331
- sessionId: this.connection?.sessionId
1332
- }, {
1333
- F: __dxlog_file3,
1334
- L: 117,
1335
- S: this,
1336
- C: (f, a) => f(...a)
1337
- });
1338
- if (this.connection) {
1339
- await this.closeConnection(new ConnectionDisplacedError());
1340
- }
1341
- } else {
1342
- return {
1343
- accept: false
1344
- };
1345
- }
1346
- }
1347
- if (await this._callbacks.onOffer(remoteId)) {
1348
- if (!this.connection) {
1349
- (0, import_invariant3.invariant)(message.sessionId, void 0, {
1350
- F: __dxlog_file3,
1351
- L: 137,
1352
- S: this,
1353
- A: [
1354
- "message.sessionId",
1355
- ""
1356
- ]
1357
- });
1358
- const connection = this._createConnection(false, message.sessionId);
1359
- try {
1360
- await this._connectionLimiter.connecting(message.sessionId);
1361
- connection.initiate();
1362
- await connection.openConnection();
1363
- } catch (err) {
1364
- if (!(err instanceof import_errors2.CancelledError)) {
1365
- import_log3.log.warn("connection error", {
1366
- topic: this.topic,
1367
- peerId: this.localPeerId,
1368
- remoteId: this.id,
1369
- err
1370
- }, {
1371
- F: __dxlog_file3,
1372
- L: 147,
1373
- S: this,
1374
- C: (f, a) => f(...a)
1375
- });
1376
- }
1377
- await this.closeConnection(err);
1378
- }
1379
- return {
1380
- accept: true
1381
- };
1382
- }
1383
- }
1384
- return {
1385
- accept: false
1386
- };
1387
- }
1388
- /**
1389
- * Initiate a connection to the remote peer.
1390
- */
1391
- async initiateConnection() {
1392
- (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
1393
- F: __dxlog_file3,
1394
- L: 164,
1395
- S: this,
1396
- A: [
1397
- "!this.initiating",
1398
- "'Initiation in progress.'"
1399
- ]
1400
- });
1401
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1402
- F: __dxlog_file3,
1403
- L: 165,
1404
- S: this,
1405
- A: [
1406
- "!this.connection",
1407
- "'Already connected.'"
1408
- ]
1409
- });
1410
- const sessionId = import_keys3.PublicKey.random();
1411
- (0, import_log3.log)("initiating...", {
1224
+ const sessionId = import_keys2.PublicKey.random();
1225
+ (0, import_log2.log)("initiating...", {
1412
1226
  id: this.id,
1413
1227
  topic: this.topic,
1414
1228
  peerId: this.id,
1415
1229
  sessionId
1416
1230
  }, {
1417
- F: __dxlog_file3,
1418
- L: 167,
1231
+ F: __dxlog_file2,
1232
+ L: 171,
1419
1233
  S: this,
1420
1234
  C: (f, a) => f(...a)
1421
1235
  });
@@ -1433,21 +1247,21 @@ var Peer = class {
1433
1247
  offer: {}
1434
1248
  }
1435
1249
  });
1436
- (0, import_log3.log)("received", {
1250
+ (0, import_log2.log)("received", {
1437
1251
  answer,
1438
1252
  topic: this.topic,
1439
1253
  ownId: this.localPeerId,
1440
1254
  remoteId: this.id
1441
1255
  }, {
1442
- F: __dxlog_file3,
1443
- L: 183,
1256
+ F: __dxlog_file2,
1257
+ L: 187,
1444
1258
  S: this,
1445
1259
  C: (f, a) => f(...a)
1446
1260
  });
1447
1261
  if (connection.state !== ConnectionState.INITIAL) {
1448
- (0, import_log3.log)("ignoring response", void 0, {
1449
- F: __dxlog_file3,
1450
- L: 185,
1262
+ (0, import_log2.log)("ignoring response", void 0, {
1263
+ F: __dxlog_file2,
1264
+ L: 189,
1451
1265
  S: this,
1452
1266
  C: (f, a) => f(...a)
1453
1267
  });
@@ -1460,17 +1274,26 @@ var Peer = class {
1460
1274
  await connection.openConnection();
1461
1275
  this._callbacks.onAccepted();
1462
1276
  } catch (err) {
1463
- (0, import_log3.log)("initiation error", {
1277
+ (0, import_log2.log)("initiation error", {
1464
1278
  err,
1465
1279
  topic: this.topic,
1466
1280
  peerId: this.localPeerId,
1467
1281
  remoteId: this.id
1468
1282
  }, {
1469
- F: __dxlog_file3,
1470
- L: 196,
1283
+ F: __dxlog_file2,
1284
+ L: 200,
1471
1285
  S: this,
1472
1286
  C: (f, a) => f(...a)
1473
1287
  });
1288
+ if (err instanceof import_protocols2.TimeoutError) {
1289
+ import_log2.log.warn("aborting connection due to signalling failure", void 0, {
1290
+ F: __dxlog_file2,
1291
+ L: 202,
1292
+ S: this,
1293
+ C: (f, a) => f(...a)
1294
+ });
1295
+ await connection.abort(err);
1296
+ }
1474
1297
  await this.closeConnection(err);
1475
1298
  throw err;
1476
1299
  } finally {
@@ -1482,21 +1305,21 @@ var Peer = class {
1482
1305
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
1483
1306
  */
1484
1307
  _createConnection(initiator, sessionId) {
1485
- (0, import_log3.log)("creating connection", {
1308
+ (0, import_log2.log)("creating connection", {
1486
1309
  topic: this.topic,
1487
1310
  peerId: this.localPeerId,
1488
1311
  remoteId: this.id,
1489
1312
  initiator,
1490
1313
  sessionId
1491
1314
  }, {
1492
- F: __dxlog_file3,
1493
- L: 210,
1315
+ F: __dxlog_file2,
1316
+ L: 218,
1494
1317
  S: this,
1495
1318
  C: (f, a) => f(...a)
1496
1319
  });
1497
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1498
- F: __dxlog_file3,
1499
- L: 217,
1320
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1321
+ F: __dxlog_file2,
1322
+ L: 225,
1500
1323
  S: this,
1501
1324
  A: [
1502
1325
  "!this.connection",
@@ -1524,50 +1347,50 @@ var Peer = class {
1524
1347
  this._lastConnectionTime = Date.now();
1525
1348
  this._callbacks.onConnected();
1526
1349
  this._connectionLimiter.doneConnecting(sessionId);
1527
- import_log3.log.trace("dxos.mesh.connection.connected", {
1350
+ import_log2.log.trace("dxos.mesh.connection.connected", {
1528
1351
  topic: this.topic,
1529
1352
  localPeerId: this.localPeerId,
1530
1353
  remotePeerId: this.id,
1531
1354
  sessionId,
1532
1355
  initiator
1533
1356
  }, {
1534
- F: __dxlog_file3,
1535
- L: 236,
1357
+ F: __dxlog_file2,
1358
+ L: 244,
1536
1359
  S: this,
1537
1360
  C: (f, a) => f(...a)
1538
1361
  });
1539
1362
  },
1540
1363
  onClosed: (err) => {
1541
- (0, import_log3.log)("connection closed", {
1364
+ (0, import_log2.log)("connection closed", {
1542
1365
  topic: this.topic,
1543
1366
  peerId: this.localPeerId,
1544
1367
  remoteId: this.id,
1545
1368
  initiator
1546
1369
  }, {
1547
- F: __dxlog_file3,
1548
- L: 245,
1370
+ F: __dxlog_file2,
1371
+ L: 253,
1549
1372
  S: this,
1550
1373
  C: (f, a) => f(...a)
1551
1374
  });
1552
1375
  this._connectionLimiter.doneConnecting(sessionId);
1553
- (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1554
- F: __dxlog_file3,
1555
- L: 250,
1376
+ (0, import_invariant2.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1377
+ F: __dxlog_file2,
1378
+ L: 258,
1556
1379
  S: this,
1557
1380
  A: [
1558
1381
  "this.connection === connection",
1559
1382
  "'Connection mismatch (race condition).'"
1560
1383
  ]
1561
1384
  });
1562
- import_log3.log.trace("dxos.mesh.connection.closed", {
1385
+ import_log2.log.trace("dxos.mesh.connection.closed", {
1563
1386
  topic: this.topic,
1564
1387
  localPeerId: this.localPeerId,
1565
1388
  remotePeerId: this.id,
1566
1389
  sessionId,
1567
1390
  initiator
1568
1391
  }, {
1569
- F: __dxlog_file3,
1570
- L: 252,
1392
+ F: __dxlog_file2,
1393
+ L: 260,
1571
1394
  S: this,
1572
1395
  C: (f, a) => f(...a)
1573
1396
  });
@@ -1589,108 +1412,340 @@ var Peer = class {
1589
1412
  this.connection = void 0;
1590
1413
  }
1591
1414
  }
1592
- );
1593
- this._callbacks.onInitiated(connection);
1594
- void this._connectionCtx?.dispose();
1595
- this._connectionCtx = this._ctx.derive();
1596
- connection.errors.handle((err) => {
1597
- import_log3.log.warn("connection error", {
1598
- topic: this.topic,
1599
- peerId: this.localPeerId,
1600
- remoteId: this.id,
1601
- initiator,
1602
- err
1603
- }, {
1415
+ );
1416
+ this._callbacks.onInitiated(connection);
1417
+ void this._connectionCtx?.dispose();
1418
+ this._connectionCtx = this._ctx.derive();
1419
+ connection.errors.handle((err) => {
1420
+ import_log2.log.warn("connection error", {
1421
+ topic: this.topic,
1422
+ peerId: this.localPeerId,
1423
+ remoteId: this.id,
1424
+ initiator,
1425
+ err
1426
+ }, {
1427
+ F: __dxlog_file2,
1428
+ L: 300,
1429
+ S: this,
1430
+ C: (f, a) => f(...a)
1431
+ });
1432
+ import_log2.log.trace("dxos.mesh.connection.error", {
1433
+ topic: this.topic,
1434
+ localPeerId: this.localPeerId,
1435
+ remotePeerId: this.id,
1436
+ sessionId,
1437
+ initiator,
1438
+ err
1439
+ }, {
1440
+ F: __dxlog_file2,
1441
+ L: 301,
1442
+ S: this,
1443
+ C: (f, a) => f(...a)
1444
+ });
1445
+ void this.closeConnection(err);
1446
+ });
1447
+ this.connection = connection;
1448
+ return connection;
1449
+ }
1450
+ async closeConnection(err) {
1451
+ if (!this.connection) {
1452
+ return;
1453
+ }
1454
+ const connection = this.connection;
1455
+ (0, import_log2.log)("closing...", {
1456
+ peerId: this.id,
1457
+ sessionId: connection.sessionId
1458
+ }, {
1459
+ F: __dxlog_file2,
1460
+ L: 326,
1461
+ S: this,
1462
+ C: (f, a) => f(...a)
1463
+ });
1464
+ await connection.close(err);
1465
+ (0, import_log2.log)("closed", {
1466
+ peerId: this.id,
1467
+ sessionId: connection.sessionId
1468
+ }, {
1469
+ F: __dxlog_file2,
1470
+ L: 332,
1471
+ S: this,
1472
+ C: (f, a) => f(...a)
1473
+ });
1474
+ }
1475
+ async onSignal(message) {
1476
+ if (!this.connection) {
1477
+ (0, import_log2.log)("dropping signal message for non-existent connection", {
1478
+ message
1479
+ }, {
1480
+ F: __dxlog_file2,
1481
+ L: 337,
1482
+ S: this,
1483
+ C: (f, a) => f(...a)
1484
+ });
1485
+ return;
1486
+ }
1487
+ await this.connection.signal(message);
1488
+ }
1489
+ async destroy() {
1490
+ await this._ctx.dispose();
1491
+ (0, import_log2.log)("Destroying peer", {
1492
+ peerId: this.id,
1493
+ topic: this.topic
1494
+ }, {
1495
+ F: __dxlog_file2,
1496
+ L: 347,
1497
+ S: this,
1498
+ C: (f, a) => f(...a)
1499
+ });
1500
+ await this?.connection?.close();
1501
+ }
1502
+ };
1503
+ _ts_decorate2([
1504
+ import_async2.synchronized
1505
+ ], Peer.prototype, "destroy", null);
1506
+ var increaseInterval = (interval) => {
1507
+ if (interval === 0) {
1508
+ return 50;
1509
+ } else if (interval < 500) {
1510
+ return 500;
1511
+ } else if (interval < 1e3) {
1512
+ return 1e3;
1513
+ } else if (interval < 5e3) {
1514
+ return 5e3;
1515
+ }
1516
+ return 1e4;
1517
+ };
1518
+
1519
+ // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1520
+ var import_context3 = require("@dxos/context");
1521
+ var import_invariant3 = require("@dxos/invariant");
1522
+ var import_keys3 = require("@dxos/keys");
1523
+ var import_log3 = require("@dxos/log");
1524
+ var import_protocols3 = require("@dxos/protocols");
1525
+ var import_util = require("@dxos/util");
1526
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1527
+ var SwarmMessage = import_protocols3.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1528
+ var SwarmMessenger = class {
1529
+ constructor({ sendMessage, onSignal, onOffer, topic }) {
1530
+ this._ctx = new import_context3.Context();
1531
+ this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1532
+ this._sendMessage = sendMessage;
1533
+ this._onSignal = onSignal;
1534
+ this._onOffer = onOffer;
1535
+ this._topic = topic;
1536
+ }
1537
+ async receiveMessage({ author, recipient, payload }) {
1538
+ if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1539
+ return;
1540
+ }
1541
+ const message = SwarmMessage.decode(payload.value);
1542
+ if (!this._topic.equals(message.topic)) {
1543
+ return;
1544
+ }
1545
+ (0, import_log3.log)("received", {
1546
+ from: author,
1547
+ to: recipient,
1548
+ msg: message
1549
+ }, {
1550
+ F: __dxlog_file3,
1551
+ L: 69,
1552
+ S: this,
1553
+ C: (f, a) => f(...a)
1554
+ });
1555
+ if (message.data?.offer) {
1556
+ await this._handleOffer({
1557
+ author,
1558
+ recipient,
1559
+ message
1560
+ });
1561
+ } else if (message.data?.answer) {
1562
+ await this._resolveAnswers(message);
1563
+ } else if (message.data?.signal) {
1564
+ await this._handleSignal({
1565
+ author,
1566
+ recipient,
1567
+ message
1568
+ });
1569
+ } else if (message.data?.signalBatch) {
1570
+ await this._handleSignal({
1571
+ author,
1572
+ recipient,
1573
+ message
1574
+ });
1575
+ } else {
1576
+ import_log3.log.warn("unknown message", {
1577
+ message
1578
+ }, {
1579
+ F: __dxlog_file3,
1580
+ L: 80,
1581
+ S: this,
1582
+ C: (f, a) => f(...a)
1583
+ });
1584
+ }
1585
+ }
1586
+ async signal(message) {
1587
+ (0, import_invariant3.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1588
+ F: __dxlog_file3,
1589
+ L: 85,
1590
+ S: this,
1591
+ A: [
1592
+ "message.data?.signal || message.data?.signalBatch",
1593
+ "'Invalid message'"
1594
+ ]
1595
+ });
1596
+ await this._sendReliableMessage({
1597
+ author: message.author,
1598
+ recipient: message.recipient,
1599
+ message
1600
+ });
1601
+ }
1602
+ async offer(message) {
1603
+ const networkMessage = {
1604
+ ...message,
1605
+ messageId: import_keys3.PublicKey.random()
1606
+ };
1607
+ return new Promise((resolve, reject) => {
1608
+ this._offerRecords.set(networkMessage.messageId, {
1609
+ resolve
1610
+ });
1611
+ this._sendReliableMessage({
1612
+ author: message.author,
1613
+ recipient: message.recipient,
1614
+ message: networkMessage
1615
+ }).catch((err) => reject(err));
1616
+ });
1617
+ }
1618
+ async _sendReliableMessage({ author, recipient, message }) {
1619
+ const networkMessage = {
1620
+ ...message,
1621
+ // Setting unique message_id if it not specified yet.
1622
+ messageId: message.messageId ?? import_keys3.PublicKey.random()
1623
+ };
1624
+ (0, import_log3.log)("sending", {
1625
+ from: author,
1626
+ to: recipient,
1627
+ msg: networkMessage
1628
+ }, {
1629
+ F: __dxlog_file3,
1630
+ L: 123,
1631
+ S: this,
1632
+ C: (f, a) => f(...a)
1633
+ });
1634
+ await this._sendMessage({
1635
+ author,
1636
+ recipient,
1637
+ payload: {
1638
+ type_url: "dxos.mesh.swarm.SwarmMessage",
1639
+ value: SwarmMessage.encode(networkMessage)
1640
+ }
1641
+ });
1642
+ }
1643
+ async _resolveAnswers(message) {
1644
+ (0, import_invariant3.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1645
+ F: __dxlog_file3,
1646
+ L: 135,
1647
+ S: this,
1648
+ A: [
1649
+ "message.data?.answer?.offerMessageId",
1650
+ "'No offerMessageId'"
1651
+ ]
1652
+ });
1653
+ const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1654
+ if (offerRecord) {
1655
+ this._offerRecords.delete(message.data.answer.offerMessageId);
1656
+ (0, import_invariant3.invariant)(message.data?.answer, "No answer", {
1604
1657
  F: __dxlog_file3,
1605
- L: 292,
1658
+ L: 139,
1606
1659
  S: this,
1607
- C: (f, a) => f(...a)
1660
+ A: [
1661
+ "message.data?.answer",
1662
+ "'No answer'"
1663
+ ]
1608
1664
  });
1609
- import_log3.log.trace("dxos.mesh.connection.error", {
1610
- topic: this.topic,
1611
- localPeerId: this.localPeerId,
1612
- remotePeerId: this.id,
1613
- sessionId,
1614
- initiator,
1615
- err
1665
+ (0, import_log3.log)("resolving", {
1666
+ answer: message.data.answer
1616
1667
  }, {
1617
1668
  F: __dxlog_file3,
1618
- L: 293,
1669
+ L: 140,
1619
1670
  S: this,
1620
1671
  C: (f, a) => f(...a)
1621
1672
  });
1622
- void this.closeConnection(err);
1623
- });
1624
- this.connection = connection;
1625
- return connection;
1626
- }
1627
- async closeConnection(err) {
1628
- if (!this.connection) {
1629
- return;
1673
+ offerRecord.resolve(message.data.answer);
1630
1674
  }
1631
- const connection = this.connection;
1632
- (0, import_log3.log)("closing...", {
1633
- peerId: this.id,
1634
- sessionId: connection.sessionId
1635
- }, {
1636
- F: __dxlog_file3,
1637
- L: 318,
1638
- S: this,
1639
- C: (f, a) => f(...a)
1640
- });
1641
- await connection.close(err);
1642
- (0, import_log3.log)("closed", {
1643
- peerId: this.id,
1644
- sessionId: connection.sessionId
1645
- }, {
1675
+ }
1676
+ async _handleOffer({ author, recipient, message }) {
1677
+ (0, import_invariant3.invariant)(message.data.offer, "No offer", {
1646
1678
  F: __dxlog_file3,
1647
- L: 324,
1679
+ L: 154,
1648
1680
  S: this,
1649
- C: (f, a) => f(...a)
1681
+ A: [
1682
+ "message.data.offer",
1683
+ "'No offer'"
1684
+ ]
1650
1685
  });
1651
- }
1652
- async onSignal(message) {
1653
- if (!this.connection) {
1654
- (0, import_log3.log)("dropping signal message for non-existent connection", {
1655
- message
1686
+ const offerMessage = {
1687
+ author,
1688
+ recipient,
1689
+ ...message,
1690
+ data: {
1691
+ offer: message.data.offer
1692
+ }
1693
+ };
1694
+ const answer = await this._onOffer(offerMessage);
1695
+ answer.offerMessageId = message.messageId;
1696
+ try {
1697
+ await this._sendReliableMessage({
1698
+ author: recipient,
1699
+ recipient: author,
1700
+ message: {
1701
+ topic: message.topic,
1702
+ sessionId: message.sessionId,
1703
+ data: {
1704
+ answer
1705
+ }
1706
+ }
1707
+ });
1708
+ } catch (err) {
1709
+ import_log3.log.warn("error sending answer", {
1710
+ err
1656
1711
  }, {
1657
1712
  F: __dxlog_file3,
1658
- L: 329,
1713
+ L: 174,
1659
1714
  S: this,
1660
1715
  C: (f, a) => f(...a)
1661
1716
  });
1662
- return;
1663
1717
  }
1664
- await this.connection.signal(message);
1665
1718
  }
1666
- async destroy() {
1667
- await this._ctx.dispose();
1668
- (0, import_log3.log)("Destroying peer", {
1669
- peerId: this.id,
1670
- topic: this.topic
1671
- }, {
1719
+ async _handleSignal({ author, recipient, message }) {
1720
+ (0, import_invariant3.invariant)(message.messageId, void 0, {
1672
1721
  F: __dxlog_file3,
1673
- L: 339,
1722
+ L: 187,
1674
1723
  S: this,
1675
- C: (f, a) => f(...a)
1724
+ A: [
1725
+ "message.messageId",
1726
+ ""
1727
+ ]
1676
1728
  });
1677
- await this?.connection?.close();
1678
- }
1679
- };
1680
- _ts_decorate2([
1681
- import_async2.synchronized
1682
- ], Peer.prototype, "destroy", null);
1683
- var increaseInterval = (interval) => {
1684
- if (interval === 0) {
1685
- return 50;
1686
- } else if (interval < 500) {
1687
- return 500;
1688
- } else if (interval < 1e3) {
1689
- return 1e3;
1690
- } else if (interval < 5e3) {
1691
- return 5e3;
1729
+ (0, import_invariant3.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1730
+ F: __dxlog_file3,
1731
+ L: 188,
1732
+ S: this,
1733
+ A: [
1734
+ "message.data.signal || message.data.signalBatch",
1735
+ "'Invalid message'"
1736
+ ]
1737
+ });
1738
+ const signalMessage = {
1739
+ author,
1740
+ recipient,
1741
+ ...message,
1742
+ data: {
1743
+ signal: message.data.signal,
1744
+ signalBatch: message.data.signalBatch
1745
+ }
1746
+ };
1747
+ await this._onSignal(signalMessage);
1692
1748
  }
1693
- return 1e4;
1694
1749
  };
1695
1750
 
1696
1751
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
@@ -1704,7 +1759,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1704
1759
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1705
1760
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1706
1761
  }
1707
- var __dxlog_file4 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1762
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1708
1763
  var INITIATION_DELAY = 100;
1709
1764
  var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
1710
1765
  var Swarm = class {
@@ -1728,7 +1783,7 @@ var Swarm = class {
1728
1783
  this.disconnected = new import_async3.Event();
1729
1784
  this.connected = new import_async3.Event();
1730
1785
  this.errors = new import_debug2.ErrorStream();
1731
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.begin({
1786
+ import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.begin({
1732
1787
  id: this._instanceId,
1733
1788
  data: {
1734
1789
  topic: this._topic.toHex(),
@@ -1755,7 +1810,7 @@ var Swarm = class {
1755
1810
  onOffer: async (msg) => await this.onOffer(msg),
1756
1811
  topic: this._topic
1757
1812
  });
1758
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.end({
1813
+ import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.end({
1759
1814
  id: this._instanceId
1760
1815
  }), {
1761
1816
  F: __dxlog_file4,
@@ -2178,12 +2233,11 @@ var import_async4 = require("@dxos/async");
2178
2233
  var import_keys5 = require("@dxos/keys");
2179
2234
  var import_log5 = require("@dxos/log");
2180
2235
  var import_util3 = require("@dxos/util");
2181
- var __dxlog_file5 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2236
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2182
2237
  var SwarmMapper = class {
2183
2238
  get peers() {
2184
2239
  return Array.from(this._peers.values());
2185
2240
  }
2186
- // prettier-ignore
2187
2241
  constructor(_swarm) {
2188
2242
  this._swarm = _swarm;
2189
2243
  this._subscriptions = new import_async4.EventSubscriptions();
@@ -2206,7 +2260,7 @@ var SwarmMapper = class {
2206
2260
  _update() {
2207
2261
  (0, import_log5.log)("updating swarm", void 0, {
2208
2262
  F: __dxlog_file5,
2209
- L: 75,
2263
+ L: 72,
2210
2264
  S: this,
2211
2265
  C: (f, a) => f(...a)
2212
2266
  });
@@ -2230,7 +2284,7 @@ var SwarmMapper = class {
2230
2284
  totalPeersInSwarm: this._peers.size
2231
2285
  }, {
2232
2286
  F: __dxlog_file5,
2233
- L: 116,
2287
+ L: 113,
2234
2288
  S: this,
2235
2289
  C: (f, a) => f(...a)
2236
2290
  });
@@ -2246,13 +2300,13 @@ var SwarmMapper = class {
2246
2300
  // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
2247
2301
  var import_async5 = require("@dxos/async");
2248
2302
  var import_context5 = require("@dxos/context");
2249
- var import_errors3 = require("@dxos/errors");
2250
2303
  var import_invariant5 = require("@dxos/invariant");
2251
2304
  var import_keys6 = require("@dxos/keys");
2252
2305
  var import_log6 = require("@dxos/log");
2306
+ var import_protocols5 = require("@dxos/protocols");
2253
2307
  var import_util4 = require("@dxos/util");
2254
- var __dxlog_file6 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2255
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
2308
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2309
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
2256
2310
  var ConnectionLimiter = class {
2257
2311
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2258
2312
  this._ctx = new import_context5.Context();
@@ -2319,7 +2373,7 @@ var ConnectionLimiter = class {
2319
2373
  if (!this._waitingPromises.has(sessionId)) {
2320
2374
  return;
2321
2375
  }
2322
- this._waitingPromises.get(sessionId).reject(new import_errors3.CancelledError());
2376
+ this._waitingPromises.get(sessionId).reject(new import_protocols5.CancelledError());
2323
2377
  this._waitingPromises.delete(sessionId);
2324
2378
  this.resolveWaitingPromises.schedule();
2325
2379
  }
@@ -2397,10 +2451,10 @@ var import_invariant6 = require("@dxos/invariant");
2397
2451
  var import_keys8 = require("@dxos/keys");
2398
2452
  var import_log7 = require("@dxos/log");
2399
2453
  var import_messaging = require("@dxos/messaging");
2400
- var import_protocols4 = require("@dxos/protocols");
2454
+ var import_protocols6 = require("@dxos/protocols");
2401
2455
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
2402
2456
  var import_util6 = require("@dxos/util");
2403
- var __dxlog_file7 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/network-manager.ts";
2457
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2404
2458
  var NetworkManager = class {
2405
2459
  constructor({ transportFactory, signalManager, log: log1 }) {
2406
2460
  /**
@@ -2445,7 +2499,7 @@ var NetworkManager = class {
2445
2499
  return this._swarms.get(topic);
2446
2500
  }
2447
2501
  async open() {
2448
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
2502
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.begin({
2449
2503
  id: this._instanceId
2450
2504
  }), {
2451
2505
  F: __dxlog_file7,
@@ -2455,7 +2509,7 @@ var NetworkManager = class {
2455
2509
  });
2456
2510
  await this._messenger.open();
2457
2511
  await this._signalManager.open();
2458
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
2512
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.end({
2459
2513
  id: this._instanceId
2460
2514
  }), {
2461
2515
  F: __dxlog_file7,
@@ -2636,7 +2690,7 @@ var NetworkManager = class {
2636
2690
 
2637
2691
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
2638
2692
  var import_invariant7 = require("@dxos/invariant");
2639
- var __dxlog_file8 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2693
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2640
2694
  var FullyConnectedTopology = class {
2641
2695
  toString() {
2642
2696
  return "FullyConnectedTopology";
@@ -2679,7 +2733,7 @@ var FullyConnectedTopology = class {
2679
2733
  var import_xor_distance = __toESM(require("xor-distance"));
2680
2734
  var import_invariant8 = require("@dxos/invariant");
2681
2735
  var import_log8 = require("@dxos/log");
2682
- var __dxlog_file9 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2736
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2683
2737
  var MMSTTopology = class {
2684
2738
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
2685
2739
  this._sampleCollected = false;
@@ -2788,9 +2842,8 @@ var sortByXorDistance = (keys, reference) => keys.sort((a, b) => import_xor_dist
2788
2842
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
2789
2843
  var import_invariant9 = require("@dxos/invariant");
2790
2844
  var import_log9 = require("@dxos/log");
2791
- var __dxlog_file10 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2845
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2792
2846
  var StarTopology = class {
2793
- // prettier-ignore
2794
2847
  constructor(_centralPeer) {
2795
2848
  this._centralPeer = _centralPeer;
2796
2849
  }
@@ -2800,7 +2853,7 @@ var StarTopology = class {
2800
2853
  init(controller) {
2801
2854
  (0, import_invariant9.invariant)(!this._controller, "Already initialized.", {
2802
2855
  F: __dxlog_file10,
2803
- L: 24,
2856
+ L: 21,
2804
2857
  S: this,
2805
2858
  A: [
2806
2859
  "!this._controller",
@@ -2812,7 +2865,7 @@ var StarTopology = class {
2812
2865
  update() {
2813
2866
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2814
2867
  F: __dxlog_file10,
2815
- L: 29,
2868
+ L: 26,
2816
2869
  S: this,
2817
2870
  A: [
2818
2871
  "this._controller",
@@ -2823,7 +2876,7 @@ var StarTopology = class {
2823
2876
  if (!ownPeerId.equals(this._centralPeer)) {
2824
2877
  (0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
2825
2878
  F: __dxlog_file10,
2826
- L: 32,
2879
+ L: 29,
2827
2880
  S: this,
2828
2881
  C: (f, a) => f(...a)
2829
2882
  });
@@ -2833,7 +2886,7 @@ var StarTopology = class {
2833
2886
  peer
2834
2887
  }, {
2835
2888
  F: __dxlog_file10,
2836
- L: 37,
2889
+ L: 34,
2837
2890
  S: this,
2838
2891
  C: (f, a) => f(...a)
2839
2892
  });
@@ -2847,7 +2900,7 @@ var StarTopology = class {
2847
2900
  peer
2848
2901
  }, {
2849
2902
  F: __dxlog_file10,
2850
- L: 46,
2903
+ L: 43,
2851
2904
  S: this,
2852
2905
  C: (f, a) => f(...a)
2853
2906
  });
@@ -2858,7 +2911,7 @@ var StarTopology = class {
2858
2911
  async onOffer(peer) {
2859
2912
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2860
2913
  F: __dxlog_file10,
2861
- L: 53,
2914
+ L: 50,
2862
2915
  S: this,
2863
2916
  A: [
2864
2917
  "this._controller",
@@ -2872,7 +2925,7 @@ var StarTopology = class {
2872
2925
  isSelfCentral: ownPeerId.equals(this._centralPeer)
2873
2926
  }, {
2874
2927
  F: __dxlog_file10,
2875
- L: 55,
2928
+ L: 52,
2876
2929
  S: this,
2877
2930
  C: (f, a) => f(...a)
2878
2931
  });
@@ -2900,7 +2953,7 @@ function _ts_decorate4(decorators, target, key, desc) {
2900
2953
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2901
2954
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2902
2955
  }
2903
- var __dxlog_file11 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2956
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2904
2957
  var MEMORY_TRANSPORT_DELAY = 1;
2905
2958
  var createStreamDelay = (delay) => {
2906
2959
  return new import_node_stream.Transform({
@@ -3075,6 +3128,7 @@ var TransportKind;
3075
3128
  TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
3076
3129
  TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
3077
3130
  TransportKind2["MEMORY"] = "MEMORY";
3131
+ TransportKind2["TCP"] = "TCP";
3078
3132
  })(TransportKind || (TransportKind = {}));
3079
3133
 
3080
3134
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
@@ -3084,7 +3138,7 @@ var import_async9 = require("@dxos/async");
3084
3138
  var import_debug5 = require("@dxos/debug");
3085
3139
  var import_keys10 = require("@dxos/keys");
3086
3140
  var import_log11 = require("@dxos/log");
3087
- var import_protocols5 = require("@dxos/protocols");
3141
+ var import_protocols7 = require("@dxos/protocols");
3088
3142
 
3089
3143
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
3090
3144
  var wrtc = null;
@@ -3094,7 +3148,7 @@ try {
3094
3148
  }
3095
3149
 
3096
3150
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
3097
- var __dxlog_file12 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
3151
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
3098
3152
  var SimplePeerTransport = class {
3099
3153
  constructor(params) {
3100
3154
  this.params = params;
@@ -3103,17 +3157,17 @@ var SimplePeerTransport = class {
3103
3157
  this.connected = new import_async9.Event();
3104
3158
  this.errors = new import_debug5.ErrorStream();
3105
3159
  this._instanceId = import_keys10.PublicKey.random().toHex();
3106
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
3160
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.begin({
3107
3161
  id: this._instanceId
3108
3162
  }), {
3109
3163
  F: __dxlog_file12,
3110
- L: 39,
3164
+ L: 40,
3111
3165
  S: this,
3112
3166
  C: (f, a) => f(...a)
3113
3167
  });
3114
3168
  (0, import_log11.log)("created connection", params, {
3115
3169
  F: __dxlog_file12,
3116
- L: 40,
3170
+ L: 41,
3117
3171
  S: this,
3118
3172
  C: (f, a) => f(...a)
3119
3173
  });
@@ -3126,7 +3180,7 @@ var SimplePeerTransport = class {
3126
3180
  this._peer.on("signal", async (data) => {
3127
3181
  (0, import_log11.log)("signal", data, {
3128
3182
  F: __dxlog_file12,
3129
- L: 49,
3183
+ L: 50,
3130
3184
  S: this,
3131
3185
  C: (f, a) => f(...a)
3132
3186
  });
@@ -3139,7 +3193,7 @@ var SimplePeerTransport = class {
3139
3193
  this._peer.on("connect", () => {
3140
3194
  (0, import_log11.log)("connected", void 0, {
3141
3195
  F: __dxlog_file12,
3142
- L: 54,
3196
+ L: 55,
3143
3197
  S: this,
3144
3198
  C: (f, a) => f(...a)
3145
3199
  });
@@ -3149,7 +3203,7 @@ var SimplePeerTransport = class {
3149
3203
  this._peer.on("close", async () => {
3150
3204
  (0, import_log11.log)("closed", void 0, {
3151
3205
  F: __dxlog_file12,
3152
- L: 60,
3206
+ L: 61,
3153
3207
  S: this,
3154
3208
  C: (f, a) => f(...a)
3155
3209
  });
@@ -3157,30 +3211,78 @@ var SimplePeerTransport = class {
3157
3211
  this.closed.emit();
3158
3212
  });
3159
3213
  this._peer.on("error", async (err) => {
3160
- this.errors.raise(err);
3214
+ if (typeof RTCError !== "undefined" && err instanceof RTCError) {
3215
+ if (err.errorDetail === "sctp-failure") {
3216
+ this.errors.raise(new import_protocols7.ConnectionResetError("sctp-failure from RTCError", err));
3217
+ } else {
3218
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown RTCError", err));
3219
+ }
3220
+ } else if ("code" in err) {
3221
+ import_log11.log.info("simple-peer error", err, {
3222
+ F: __dxlog_file12,
3223
+ L: 77,
3224
+ S: this,
3225
+ C: (f, a) => f(...a)
3226
+ });
3227
+ switch (err.code) {
3228
+ case "ERR_WEBRTC_SUPPORT":
3229
+ this.errors.raise(new import_protocols7.ProtocolError("WebRTC not supported", err));
3230
+ break;
3231
+ case "ERR_ICE_CONNECTION_FAILURE":
3232
+ case "ERR_DATA_CHANNEL":
3233
+ case "ERR_CONNECTION_FAILURE":
3234
+ case "ERR_SIGNALING":
3235
+ this.errors.raise(new import_protocols7.ConnectivityError("unknown communication failure", err));
3236
+ break;
3237
+ case "ERR_CREATE_OFFER":
3238
+ case "ERR_CREATE_ANSWER":
3239
+ case "ERR_SET_LOCAL_DESCRIPTION":
3240
+ case "ERR_SET_REMOTE_DESCRIPTION":
3241
+ case "ERR_ADD_ICE_CANDIDATE":
3242
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown simple-peer library failure", err));
3243
+ break;
3244
+ default:
3245
+ this.errors.raise(new Error("unknown simple-peer error"));
3246
+ break;
3247
+ }
3248
+ } else {
3249
+ import_log11.log.info("unknown peer connection error", err, {
3250
+ F: __dxlog_file12,
3251
+ L: 101,
3252
+ S: this,
3253
+ C: (f, a) => f(...a)
3254
+ });
3255
+ this.errors.raise(err);
3256
+ }
3161
3257
  try {
3162
- if (typeof this._peer?._pc.getStats === "function") {
3258
+ if (typeof this._peer?._pc?.getStats === "function") {
3163
3259
  this._peer._pc.getStats().then((stats) => {
3164
3260
  import_log11.log.warn("report after webrtc error", {
3165
3261
  config: this.params.webrtcConfig,
3166
3262
  stats
3167
3263
  }, {
3168
3264
  F: __dxlog_file12,
3169
- L: 72,
3265
+ L: 109,
3170
3266
  S: this,
3171
3267
  C: (f, a) => f(...a)
3172
3268
  });
3173
3269
  });
3174
3270
  }
3175
- } catch {
3271
+ } catch (err2) {
3272
+ import_log11.log.catch(err2, void 0, {
3273
+ F: __dxlog_file12,
3274
+ L: 116,
3275
+ S: this,
3276
+ C: (f, a) => f(...a)
3277
+ });
3176
3278
  }
3177
3279
  await this.destroy();
3178
3280
  });
3179
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
3281
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.end({
3180
3282
  id: this._instanceId
3181
3283
  }), {
3182
3284
  F: __dxlog_file12,
3183
- L: 82,
3285
+ L: 121,
3184
3286
  S: this,
3185
3287
  C: (f, a) => f(...a)
3186
3288
  });
@@ -3188,7 +3290,7 @@ var SimplePeerTransport = class {
3188
3290
  async destroy() {
3189
3291
  (0, import_log11.log)("closing...", void 0, {
3190
3292
  F: __dxlog_file12,
3191
- L: 86,
3293
+ L: 125,
3192
3294
  S: this,
3193
3295
  C: (f, a) => f(...a)
3194
3296
  });
@@ -3198,7 +3300,7 @@ var SimplePeerTransport = class {
3198
3300
  this.closed.emit();
3199
3301
  (0, import_log11.log)("closed", void 0, {
3200
3302
  F: __dxlog_file12,
3201
- L: 91,
3303
+ L: 130,
3202
3304
  S: this,
3203
3305
  C: (f, a) => f(...a)
3204
3306
  });
@@ -3229,9 +3331,8 @@ var import_keys11 = require("@dxos/keys");
3229
3331
  var import_log12 = require("@dxos/log");
3230
3332
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
3231
3333
  var import_util8 = require("@dxos/util");
3232
- var __dxlog_file13 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3334
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3233
3335
  var SimplePeerTransportService = class {
3234
- // prettier-ignore
3235
3336
  constructor(_webrtcConfig) {
3236
3337
  this._webrtcConfig = _webrtcConfig;
3237
3338
  this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
@@ -3301,7 +3402,8 @@ var SimplePeerTransportService = class {
3301
3402
  const transportState = {
3302
3403
  transport,
3303
3404
  stream: duplex,
3304
- writeCallbacks: []
3405
+ writeCallbacks: [],
3406
+ state: "OPEN"
3305
3407
  };
3306
3408
  ready();
3307
3409
  this.transports.set(request.proxyId, transportState);
@@ -3311,7 +3413,7 @@ var SimplePeerTransportService = class {
3311
3413
  async sendSignal({ proxyId, signal }) {
3312
3414
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3313
3415
  F: __dxlog_file13,
3314
- L: 113,
3416
+ L: 112,
3315
3417
  S: this,
3316
3418
  A: [
3317
3419
  "this.transports.has(proxyId)",
@@ -3321,9 +3423,17 @@ var SimplePeerTransportService = class {
3321
3423
  await this.transports.get(proxyId).transport.signal(signal);
3322
3424
  }
3323
3425
  async sendData({ proxyId, payload }) {
3426
+ if (this.transports.get(proxyId)?.state !== "OPEN") {
3427
+ import_log12.log.debug("transport is closed", void 0, {
3428
+ F: __dxlog_file13,
3429
+ L: 118,
3430
+ S: this,
3431
+ C: (f, a) => f(...a)
3432
+ });
3433
+ }
3324
3434
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3325
3435
  F: __dxlog_file13,
3326
- L: 118,
3436
+ L: 120,
3327
3437
  S: this,
3328
3438
  A: [
3329
3439
  "this.transports.has(proxyId)",
@@ -3341,10 +3451,12 @@ var SimplePeerTransportService = class {
3341
3451
  async close({ proxyId }) {
3342
3452
  await this.transports.get(proxyId)?.transport.destroy();
3343
3453
  await this.transports.get(proxyId)?.stream.end();
3344
- this.transports.delete(proxyId);
3454
+ if (this.transports.get(proxyId)) {
3455
+ this.transports.get(proxyId).state = "CLOSED";
3456
+ }
3345
3457
  (0, import_log12.log)("Closed.", void 0, {
3346
3458
  F: __dxlog_file13,
3347
- L: 132,
3459
+ L: 136,
3348
3460
  S: this,
3349
3461
  C: (f, a) => f(...a)
3350
3462
  });
@@ -3359,11 +3471,11 @@ var import_debug6 = require("@dxos/debug");
3359
3471
  var import_invariant12 = require("@dxos/invariant");
3360
3472
  var import_keys12 = require("@dxos/keys");
3361
3473
  var import_log13 = require("@dxos/log");
3474
+ var import_protocols8 = require("@dxos/protocols");
3362
3475
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
3363
3476
  var import_util9 = require("@dxos/util");
3364
- var __dxlog_file14 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3477
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3365
3478
  var SimplePeerTransportProxy = class {
3366
- // prettier-ignore
3367
3479
  constructor(_params) {
3368
3480
  this._params = _params;
3369
3481
  this._proxyId = import_keys12.PublicKey.random();
@@ -3380,7 +3492,7 @@ var SimplePeerTransportProxy = class {
3380
3492
  this._serviceStream.subscribe(async (event) => {
3381
3493
  (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
3382
3494
  F: __dxlog_file14,
3383
- L: 51,
3495
+ L: 55,
3384
3496
  S: this,
3385
3497
  C: (f, a) => f(...a)
3386
3498
  });
@@ -3400,7 +3512,7 @@ var SimplePeerTransportProxy = class {
3400
3512
  }).then(() => callback(), (err) => {
3401
3513
  import_log13.log.catch(err, void 0, {
3402
3514
  F: __dxlog_file14,
3403
- L: 69,
3515
+ L: 76,
3404
3516
  S: this,
3405
3517
  C: (f, a) => f(...a)
3406
3518
  });
@@ -3409,14 +3521,14 @@ var SimplePeerTransportProxy = class {
3409
3521
  }));
3410
3522
  }, (error) => import_log13.log.catch(error, void 0, {
3411
3523
  F: __dxlog_file14,
3412
- L: 75,
3524
+ L: 83,
3413
3525
  S: this,
3414
3526
  C: (f, a) => f(...a)
3415
3527
  }));
3416
3528
  }
3417
3529
  async _handleConnection(connectionEvent) {
3418
3530
  if (connectionEvent.error) {
3419
- this.errors.raise(new Error(connectionEvent.error));
3531
+ this.errors.raise(decodeError(connectionEvent.error));
3420
3532
  }
3421
3533
  switch (connectionEvent.state) {
3422
3534
  case import_bridge2.ConnectionState.CONNECTED: {
@@ -3439,7 +3551,7 @@ var SimplePeerTransportProxy = class {
3439
3551
  this._params.bridgeService.sendSignal({
3440
3552
  proxyId: this._proxyId,
3441
3553
  signal
3442
- }).catch((err) => this.errors.raise(err));
3554
+ }).catch((err) => this.errors.raise(decodeError(err)));
3443
3555
  }
3444
3556
  // TODO(burdon): Move open from constructor.
3445
3557
  async destroy() {
@@ -3455,7 +3567,7 @@ var SimplePeerTransportProxy = class {
3455
3567
  } catch (err) {
3456
3568
  import_log13.log.catch(err, void 0, {
3457
3569
  F: __dxlog_file14,
3458
- L: 126,
3570
+ L: 134,
3459
3571
  S: this,
3460
3572
  C: (f, a) => f(...a)
3461
3573
  });
@@ -3491,7 +3603,7 @@ var SimplePeerTransportProxyFactory = class {
3491
3603
  createTransport(options) {
3492
3604
  (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3493
3605
  F: __dxlog_file14,
3494
- L: 163,
3606
+ L: 171,
3495
3607
  S: this,
3496
3608
  A: [
3497
3609
  "this._bridgeService",
@@ -3507,6 +3619,22 @@ var SimplePeerTransportProxyFactory = class {
3507
3619
  return transport;
3508
3620
  }
3509
3621
  };
3622
+ var decodeError = (err) => {
3623
+ const message = typeof err === "string" ? err : err.message;
3624
+ if (message.includes("CONNECTION_RESET")) {
3625
+ return new import_protocols8.ConnectionResetError(message);
3626
+ } else if (message.includes("TIMEOUT")) {
3627
+ return new import_protocols8.TimeoutError(message);
3628
+ } else if (message.includes("PROTOCOL_ERROR")) {
3629
+ return new import_protocols8.ProtocolError(message);
3630
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3631
+ return new import_protocols8.ConnectivityError(message);
3632
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3633
+ return new import_protocols8.UnknownProtocolError(message);
3634
+ } else {
3635
+ return typeof err === "string" ? new Error(err) : err;
3636
+ }
3637
+ };
3510
3638
 
3511
3639
  // packages/core/mesh/network-manager/src/transport/index.ts
3512
3640
  init_datachannel();
@@ -3526,7 +3654,7 @@ function _ts_decorate5(decorators, target, key, desc) {
3526
3654
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3527
3655
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3528
3656
  }
3529
- var __dxlog_file15 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
3657
+ var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
3530
3658
  var DATACHANNEL_LABEL = "dxos.mesh.transport";
3531
3659
  var MAX_BUFFERED_AMOUNT = 64 * 1024;
3532
3660
  var MAX_MESSAGE_SIZE = 64 * 1024;
@@ -3850,6 +3978,136 @@ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3850
3978
  })
3851
3979
  });
3852
3980
 
3981
+ // packages/core/mesh/network-manager/src/transport/tcp-transport.ts
3982
+ var import_node_net = require("node:net");
3983
+ var import_async12 = require("@dxos/async");
3984
+ var import_debug8 = require("@dxos/debug");
3985
+ var import_log15 = require("@dxos/log");
3986
+ var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/tcp-transport.ts";
3987
+ var TcpTransportFactory = {
3988
+ createTransport: (params) => new TcpTransport(params)
3989
+ };
3990
+ var TcpTransport = class {
3991
+ constructor(options) {
3992
+ this.options = options;
3993
+ this._server = void 0;
3994
+ this._socket = void 0;
3995
+ this.closed = new import_async12.Event();
3996
+ this.connected = new import_async12.Event();
3997
+ this.errors = new import_debug8.ErrorStream();
3998
+ this._destroyed = false;
3999
+ this._connected = false;
4000
+ (0, import_log15.log)("creating", void 0, {
4001
+ F: __dxlog_file16,
4002
+ L: 34,
4003
+ S: this,
4004
+ C: (f, a) => f(...a)
4005
+ });
4006
+ if (this.options.initiator) {
4007
+ setTimeout(async () => {
4008
+ const { Server } = await import("node:net");
4009
+ this._server = new Server((socket) => {
4010
+ (0, import_log15.log)("new connection", void 0, {
4011
+ F: __dxlog_file16,
4012
+ L: 42,
4013
+ S: this,
4014
+ C: (f, a) => f(...a)
4015
+ });
4016
+ if (this._connected) {
4017
+ socket.destroy();
4018
+ }
4019
+ this._handleSocket(socket);
4020
+ });
4021
+ this._server.on("listening", () => {
4022
+ const { port } = this._server.address();
4023
+ (0, import_log15.log)("listening", {
4024
+ port
4025
+ }, {
4026
+ F: __dxlog_file16,
4027
+ L: 51,
4028
+ S: this,
4029
+ C: (f, a) => f(...a)
4030
+ });
4031
+ void this.options.sendSignal({
4032
+ payload: {
4033
+ port
4034
+ }
4035
+ }).catch((err) => {
4036
+ if (!this._destroyed) {
4037
+ this.errors.raise(err);
4038
+ }
4039
+ });
4040
+ });
4041
+ this._server.on("error", (err) => {
4042
+ this.errors.raise(err);
4043
+ });
4044
+ this._server.listen(0);
4045
+ });
4046
+ }
4047
+ }
4048
+ async destroy() {
4049
+ (0, import_log15.log)("closing", void 0, {
4050
+ F: __dxlog_file16,
4051
+ L: 72,
4052
+ S: this,
4053
+ C: (f, a) => f(...a)
4054
+ });
4055
+ this._socket?.destroy();
4056
+ this._server?.close();
4057
+ this._destroyed = true;
4058
+ }
4059
+ signal({ payload }) {
4060
+ (0, import_log15.log)("received signal", {
4061
+ payload
4062
+ }, {
4063
+ F: __dxlog_file16,
4064
+ L: 80,
4065
+ S: this,
4066
+ C: (f, a) => f(...a)
4067
+ });
4068
+ if (this.options.initiator || this._connected) {
4069
+ return;
4070
+ }
4071
+ const socket = new import_node_net.Socket();
4072
+ this._handleSocket(socket);
4073
+ socket.connect({
4074
+ port: payload.port,
4075
+ host: "localhost"
4076
+ });
4077
+ }
4078
+ _handleSocket(socket) {
4079
+ (0, import_log15.log)("handling socket", {
4080
+ remotePort: socket.remotePort,
4081
+ localPort: socket.localPort
4082
+ }, {
4083
+ F: __dxlog_file16,
4084
+ L: 91,
4085
+ S: this,
4086
+ C: (f, a) => f(...a)
4087
+ });
4088
+ this._socket = socket;
4089
+ this.connected.emit();
4090
+ this.options.stream.pipe(this._socket).pipe(this.options.stream);
4091
+ this._socket.on("connect", () => {
4092
+ (0, import_log15.log)("connected to", {
4093
+ port: this._socket?.remotePort
4094
+ }, {
4095
+ F: __dxlog_file16,
4096
+ L: 97,
4097
+ S: this,
4098
+ C: (f, a) => f(...a)
4099
+ });
4100
+ this._connected = true;
4101
+ });
4102
+ this._socket.on("error", (err) => {
4103
+ this.errors.raise(err);
4104
+ });
4105
+ this._socket.on("close", () => {
4106
+ this.closed.emit();
4107
+ });
4108
+ }
4109
+ };
4110
+
3853
4111
  // packages/core/mesh/network-manager/src/wire-protocol.ts
3854
4112
  var import_teleport = require("@dxos/teleport");
3855
4113
  var createTeleportProtocolFactory = (onConnection) => {
@@ -3895,6 +4153,8 @@ var createTeleportProtocolFactory = (onConnection) => {
3895
4153
  Swarm,
3896
4154
  SwarmMapper,
3897
4155
  SwarmMessenger,
4156
+ TcpTransport,
4157
+ TcpTransportFactory,
3898
4158
  TransportKind,
3899
4159
  cleanup,
3900
4160
  createLibDataChannelTransportFactory,