@dxos/network-manager 0.1.56-main.c787da3 → 0.1.56-main.e47dfd1

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 (37) hide show
  1. package/dist/lib/browser/{chunk-MIYEP22D.mjs → chunk-5R7TDXUU.mjs} +323 -229
  2. package/dist/lib/browser/chunk-5R7TDXUU.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +5 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +1 -1
  6. package/dist/lib/node/index.cjs +318 -222
  7. package/dist/lib/node/index.cjs.map +4 -4
  8. package/dist/lib/node/meta.json +1 -1
  9. package/dist/lib/node/testing/index.cjs +314 -222
  10. package/dist/lib/node/testing/index.cjs.map +4 -4
  11. package/dist/types/src/network-manager.d.ts +1 -1
  12. package/dist/types/src/network-manager.d.ts.map +1 -1
  13. package/dist/types/src/swarm/connection-limiter.d.ts +28 -0
  14. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -0
  15. package/dist/types/src/swarm/connection-limiter.test.d.ts +2 -0
  16. package/dist/types/src/swarm/connection-limiter.test.d.ts.map +1 -0
  17. package/dist/types/src/swarm/connection.d.ts +1 -1
  18. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  19. package/dist/types/src/swarm/connection.test.d.ts +2 -0
  20. package/dist/types/src/swarm/connection.test.d.ts.map +1 -0
  21. package/dist/types/src/swarm/index.d.ts +1 -0
  22. package/dist/types/src/swarm/index.d.ts.map +1 -1
  23. package/dist/types/src/swarm/peer.d.ts +3 -1
  24. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  25. package/dist/types/src/swarm/swarm.d.ts +3 -1
  26. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  27. package/package.json +17 -17
  28. package/src/network-manager.ts +18 -4
  29. package/src/swarm/connection-limiter.test.ts +44 -0
  30. package/src/swarm/connection-limiter.ts +71 -0
  31. package/src/swarm/connection.test.ts +72 -0
  32. package/src/swarm/connection.ts +1 -2
  33. package/src/swarm/index.ts +1 -0
  34. package/src/swarm/peer.ts +22 -7
  35. package/src/swarm/swarm.test.ts +140 -90
  36. package/src/swarm/swarm.ts +3 -1
  37. package/dist/lib/browser/chunk-MIYEP22D.mjs.map +0 -7
@@ -31,10 +31,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
33
  Connection: () => Connection,
34
+ ConnectionLimiter: () => ConnectionLimiter,
34
35
  ConnectionLog: () => ConnectionLog,
35
36
  ConnectionState: () => ConnectionState,
36
37
  EventType: () => EventType,
37
38
  FullyConnectedTopology: () => FullyConnectedTopology,
39
+ MAX_CONCURRENT_INITIATING_CONNECTIONS: () => MAX_CONCURRENT_INITIATING_CONNECTIONS,
38
40
  MMSTTopology: () => MMSTTopology,
39
41
  MemoryTransport: () => MemoryTransport,
40
42
  MemoryTransportFactory: () => MemoryTransportFactory,
@@ -53,10 +55,10 @@ __export(src_exports, {
53
55
  module.exports = __toCommonJS(src_exports);
54
56
 
55
57
  // packages/core/mesh/network-manager/src/connection-log.ts
56
- var import_async5 = require("@dxos/async");
58
+ var import_async6 = require("@dxos/async");
57
59
  var import_debug3 = require("@dxos/debug");
58
- var import_keys6 = require("@dxos/keys");
59
- var import_util4 = require("@dxos/util");
60
+ var import_keys7 = require("@dxos/keys");
61
+ var import_util5 = require("@dxos/util");
60
62
 
61
63
  // packages/core/mesh/network-manager/src/swarm/connection.ts
62
64
  var import_tiny_invariant = __toESM(require("tiny-invariant"));
@@ -148,14 +150,13 @@ var Connection = class {
148
150
  /**
149
151
  * Create an underlying transport and prepares it for the connection.
150
152
  */
151
- // TODO(burdon): Make async?
152
- openConnection() {
153
+ async openConnection() {
153
154
  (0, import_tiny_invariant.default)(this._state === ConnectionState.INITIAL, "Invalid state.");
154
155
  import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
155
156
  id: this._instanceId
156
157
  }), {
157
158
  F: __dxlog_file,
158
- L: 115,
159
+ L: 114,
159
160
  S: this,
160
161
  C: (f, a) => f(...a)
161
162
  });
@@ -166,7 +167,7 @@ var Connection = class {
166
167
  this._protocol.stream.on("close", () => {
167
168
  (0, import_log.log)("protocol stream closed", void 0, {
168
169
  F: __dxlog_file,
169
- L: 126,
170
+ L: 125,
170
171
  S: this,
171
172
  C: (f, a) => f(...a)
172
173
  });
@@ -198,7 +199,7 @@ var Connection = class {
198
199
  id: this._instanceId
199
200
  }), {
200
201
  F: __dxlog_file,
201
- L: 159,
202
+ L: 158,
202
203
  S: this,
203
204
  C: (f, a) => f(...a)
204
205
  });
@@ -214,7 +215,7 @@ var Connection = class {
214
215
  peerId: this.ownId
215
216
  }, {
216
217
  F: __dxlog_file,
217
- L: 171,
218
+ L: 170,
218
219
  S: this,
219
220
  C: (f, a) => f(...a)
220
221
  });
@@ -223,7 +224,7 @@ var Connection = class {
223
224
  } catch (err) {
224
225
  import_log.log.catch(err, void 0, {
225
226
  F: __dxlog_file,
226
- L: 177,
227
+ L: 176,
227
228
  S: this,
228
229
  C: (f, a) => f(...a)
229
230
  });
@@ -233,7 +234,7 @@ var Connection = class {
233
234
  } catch (err) {
234
235
  import_log.log.catch(err, void 0, {
235
236
  F: __dxlog_file,
236
- L: 184,
237
+ L: 183,
237
238
  S: this,
238
239
  C: (f, a) => f(...a)
239
240
  });
@@ -242,7 +243,7 @@ var Connection = class {
242
243
  peerId: this.ownId
243
244
  }, {
244
245
  F: __dxlog_file,
245
- L: 187,
246
+ L: 186,
246
247
  S: this,
247
248
  C: (f, a) => f(...a)
248
249
  });
@@ -282,7 +283,7 @@ var Connection = class {
282
283
  err
283
284
  }, {
284
285
  F: __dxlog_file,
285
- L: 221,
286
+ L: 220,
286
287
  S: this,
287
288
  C: (f, a) => f(...a)
288
289
  });
@@ -298,7 +299,7 @@ var Connection = class {
298
299
  if (!msg.sessionId.equals(this.sessionId)) {
299
300
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
300
301
  F: __dxlog_file,
301
- L: 232,
302
+ L: 231,
302
303
  S: this,
303
304
  C: (f, a) => f(...a)
304
305
  });
@@ -321,7 +322,7 @@ var Connection = class {
321
322
  msg: msg.data
322
323
  }, {
323
324
  F: __dxlog_file,
324
- L: 246,
325
+ L: 245,
325
326
  S: this,
326
327
  C: (f, a) => f(...a)
327
328
  });
@@ -334,7 +335,7 @@ var Connection = class {
334
335
  msg: msg.data
335
336
  }, {
336
337
  F: __dxlog_file,
337
- L: 250,
338
+ L: 249,
338
339
  S: this,
339
340
  C: (f, a) => f(...a)
340
341
  });
@@ -349,7 +350,7 @@ var Connection = class {
349
350
  peerId: this.ownId
350
351
  }, {
351
352
  F: __dxlog_file,
352
- L: 257,
353
+ L: 256,
353
354
  S: this,
354
355
  C: (f, a) => f(...a)
355
356
  });
@@ -564,6 +565,7 @@ var SwarmMessenger = class {
564
565
  var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
565
566
  var import_async2 = require("@dxos/async");
566
567
  var import_context3 = require("@dxos/context");
568
+ var import_errors2 = require("@dxos/errors");
567
569
  var import_keys3 = require("@dxos/keys");
568
570
  var import_log3 = require("@dxos/log");
569
571
  function _ts_decorate2(decorators, target, key, desc) {
@@ -579,13 +581,14 @@ function _ts_decorate2(decorators, target, key, desc) {
579
581
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
580
582
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
581
583
  var Peer = class {
582
- constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _callbacks) {
584
+ constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
583
585
  this.id = id;
584
586
  this.topic = topic;
585
587
  this.localPeerId = localPeerId;
586
588
  this._signalMessaging = _signalMessaging;
587
589
  this._protocolProvider = _protocolProvider;
588
590
  this._transportFactory = _transportFactory;
591
+ this._connectionLimiter = _connectionLimiter;
589
592
  this._callbacks = _callbacks;
590
593
  this._availableAfter = 0;
591
594
  this.availableToConnect = true;
@@ -597,16 +600,18 @@ var Peer = class {
597
600
  * Respond to remote offer.
598
601
  */
599
602
  async onOffer(message) {
603
+ var _a;
600
604
  const remoteId = message.author;
601
605
  if (this.connection || this.initiating) {
602
606
  if (remoteId.toHex() < this.localPeerId.toHex()) {
603
- (0, import_log3.log)("closing local connection and accepting remote peer's offer", {
604
- id: this.id,
607
+ (0, import_log3.log)("close local connection", {
608
+ localPeerId: this.id,
605
609
  topic: this.topic,
606
- peerId: this.localPeerId
610
+ remotePeerId: this.localPeerId,
611
+ sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
607
612
  }, {
608
613
  F: __dxlog_file3,
609
- L: 105,
614
+ L: 107,
610
615
  S: this,
611
616
  C: (f, a) => f(...a)
612
617
  });
@@ -624,19 +629,22 @@ var Peer = class {
624
629
  (0, import_tiny_invariant3.default)(message.sessionId);
625
630
  const connection = this._createConnection(false, message.sessionId);
626
631
  try {
627
- connection.openConnection();
632
+ await this._connectionLimiter.connecting(message.sessionId);
633
+ await connection.openConnection();
628
634
  } catch (err) {
629
- import_log3.log.warn("connection error", {
630
- topic: this.topic,
631
- peerId: this.localPeerId,
632
- remoteId: this.id,
633
- err
634
- }, {
635
- F: __dxlog_file3,
636
- L: 129,
637
- S: this,
638
- C: (f, a) => f(...a)
639
- });
635
+ if (!(err instanceof import_errors2.CancelledError)) {
636
+ import_log3.log.warn("connection error", {
637
+ topic: this.topic,
638
+ peerId: this.localPeerId,
639
+ remoteId: this.id,
640
+ err
641
+ }, {
642
+ F: __dxlog_file3,
643
+ L: 135,
644
+ S: this,
645
+ C: (f, a) => f(...a)
646
+ });
647
+ }
640
648
  await this.closeConnection();
641
649
  }
642
650
  return {
@@ -662,13 +670,14 @@ var Peer = class {
662
670
  sessionId
663
671
  }, {
664
672
  F: __dxlog_file3,
665
- L: 147,
673
+ L: 155,
666
674
  S: this,
667
675
  C: (f, a) => f(...a)
668
676
  });
669
677
  const connection = this._createConnection(true, sessionId);
670
678
  this.initiating = true;
671
679
  try {
680
+ await this._connectionLimiter.connecting(sessionId);
672
681
  const answer = await this._signalMessaging.offer({
673
682
  author: this.localPeerId,
674
683
  recipient: this.id,
@@ -685,14 +694,14 @@ var Peer = class {
685
694
  remoteId: this.id
686
695
  }, {
687
696
  F: __dxlog_file3,
688
- L: 159,
697
+ L: 170,
689
698
  S: this,
690
699
  C: (f, a) => f(...a)
691
700
  });
692
701
  if (connection.state !== ConnectionState.INITIAL) {
693
702
  (0, import_log3.log)("ignoring response", void 0, {
694
703
  F: __dxlog_file3,
695
- L: 161,
704
+ L: 172,
696
705
  S: this,
697
706
  C: (f, a) => f(...a)
698
707
  });
@@ -702,7 +711,7 @@ var Peer = class {
702
711
  this._callbacks.onRejected();
703
712
  return;
704
713
  }
705
- connection.openConnection();
714
+ await connection.openConnection();
706
715
  this._callbacks.onAccepted();
707
716
  } catch (err) {
708
717
  (0, import_log3.log)("initiation error", {
@@ -712,7 +721,7 @@ var Peer = class {
712
721
  remoteId: this.id
713
722
  }, {
714
723
  F: __dxlog_file3,
715
- L: 172,
724
+ L: 183,
716
725
  S: this,
717
726
  C: (f, a) => f(...a)
718
727
  });
@@ -736,7 +745,7 @@ var Peer = class {
736
745
  sessionId
737
746
  }, {
738
747
  F: __dxlog_file3,
739
- L: 186,
748
+ L: 197,
740
749
  S: this,
741
750
  C: (f, a) => f(...a)
742
751
  });
@@ -766,6 +775,7 @@ var Peer = class {
766
775
  this.availableToConnect = true;
767
776
  this._lastConnectionTime = Date.now();
768
777
  this._callbacks.onConnected();
778
+ this._connectionLimiter.doneConnecting(sessionId);
769
779
  break;
770
780
  }
771
781
  case ConnectionState.CLOSED: {
@@ -776,7 +786,7 @@ var Peer = class {
776
786
  initiator
777
787
  }, {
778
788
  F: __dxlog_file3,
779
- L: 221,
789
+ L: 234,
780
790
  S: this,
781
791
  C: (f, a) => f(...a)
782
792
  });
@@ -789,6 +799,7 @@ var Peer = class {
789
799
  }
790
800
  this.connection = void 0;
791
801
  this._callbacks.onDisconnected();
802
+ this._connectionLimiter.doneConnecting(sessionId);
792
803
  (0, import_async2.scheduleTask)(this._connectionCtx, () => {
793
804
  this.availableToConnect = true;
794
805
  this._callbacks.onPeerAvailable();
@@ -806,7 +817,7 @@ var Peer = class {
806
817
  err
807
818
  }, {
808
819
  F: __dxlog_file3,
809
- L: 249,
820
+ L: 263,
810
821
  S: this,
811
822
  C: (f, a) => f(...a)
812
823
  });
@@ -825,7 +836,7 @@ var Peer = class {
825
836
  sessionId: connection.sessionId
826
837
  }, {
827
838
  F: __dxlog_file3,
828
- L: 265,
839
+ L: 279,
829
840
  S: this,
830
841
  C: (f, a) => f(...a)
831
842
  });
@@ -835,7 +846,7 @@ var Peer = class {
835
846
  sessionId: connection.sessionId
836
847
  }, {
837
848
  F: __dxlog_file3,
838
- L: 271,
849
+ L: 285,
839
850
  S: this,
840
851
  C: (f, a) => f(...a)
841
852
  });
@@ -846,7 +857,7 @@ var Peer = class {
846
857
  message
847
858
  }, {
848
859
  F: __dxlog_file3,
849
- L: 276,
860
+ L: 290,
850
861
  S: this,
851
862
  C: (f, a) => f(...a)
852
863
  });
@@ -862,7 +873,7 @@ var Peer = class {
862
873
  topic: this.topic
863
874
  }, {
864
875
  F: __dxlog_file3,
865
- L: 285,
876
+ L: 300,
866
877
  S: this,
867
878
  C: (f, a) => f(...a)
868
879
  });
@@ -902,7 +913,7 @@ var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
902
913
  var Swarm = class {
903
914
  // TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
904
915
  // TODO(burdon): Split up properties.
905
- constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label) {
916
+ constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter) {
906
917
  this._topic = _topic;
907
918
  this._ownPeerId = _ownPeerId;
908
919
  this._topology = _topology;
@@ -910,6 +921,7 @@ var Swarm = class {
910
921
  this._messenger = _messenger;
911
922
  this._transportFactory = _transportFactory;
912
923
  this._label = _label;
924
+ this._connectionLimiter = _connectionLimiter;
913
925
  this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
914
926
  this._ctx = new import_context4.Context();
915
927
  this._listeningHandle = void 0;
@@ -926,7 +938,7 @@ var Swarm = class {
926
938
  }
927
939
  }), {
928
940
  F: __dxlog_file4,
929
- L: 86,
941
+ L: 88,
930
942
  S: this,
931
943
  C: (f, a) => f(...a)
932
944
  });
@@ -934,7 +946,7 @@ var Swarm = class {
934
946
  peerId: _ownPeerId
935
947
  }, {
936
948
  F: __dxlog_file4,
937
- L: 90,
949
+ L: 92,
938
950
  S: this,
939
951
  C: (f, a) => f(...a)
940
952
  });
@@ -949,7 +961,7 @@ var Swarm = class {
949
961
  id: this._instanceId
950
962
  }), {
951
963
  F: __dxlog_file4,
952
- L: 99,
964
+ L: 101,
953
965
  S: this,
954
966
  C: (f, a) => f(...a)
955
967
  });
@@ -979,7 +991,7 @@ var Swarm = class {
979
991
  err
980
992
  }, {
981
993
  F: __dxlog_file4,
982
- L: 133,
994
+ L: 135,
983
995
  S: this,
984
996
  C: (f, a) => f(...a)
985
997
  }));
@@ -990,7 +1002,7 @@ var Swarm = class {
990
1002
  var _a;
991
1003
  (0, import_log4.log)("destroying...", void 0, {
992
1004
  F: __dxlog_file4,
993
- L: 139,
1005
+ L: 141,
994
1006
  S: this,
995
1007
  C: (f, a) => f(...a)
996
1008
  });
@@ -1001,7 +1013,7 @@ var Swarm = class {
1001
1013
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
1002
1014
  (0, import_log4.log)("destroyed", void 0, {
1003
1015
  F: __dxlog_file4,
1004
- L: 146,
1016
+ L: 148,
1005
1017
  S: this,
1006
1018
  C: (f, a) => f(...a)
1007
1019
  });
@@ -1016,7 +1028,7 @@ var Swarm = class {
1016
1028
  topology: getClassName(topology)
1017
1029
  }, {
1018
1030
  F: __dxlog_file4,
1019
- L: 154,
1031
+ L: 156,
1020
1032
  S: this,
1021
1033
  C: (f, a) => f(...a)
1022
1034
  });
@@ -1031,14 +1043,14 @@ var Swarm = class {
1031
1043
  swarmEvent
1032
1044
  }, {
1033
1045
  F: __dxlog_file4,
1034
- L: 167,
1046
+ L: 169,
1035
1047
  S: this,
1036
1048
  C: (f, a) => f(...a)
1037
1049
  });
1038
1050
  if (this._ctx.disposed) {
1039
1051
  (0, import_log4.log)("swarm event ignored for disposed swarm", void 0, {
1040
1052
  F: __dxlog_file4,
1041
- L: 170,
1053
+ L: 172,
1042
1054
  S: this,
1043
1055
  C: (f, a) => f(...a)
1044
1056
  });
@@ -1050,7 +1062,7 @@ var Swarm = class {
1050
1062
  peerId
1051
1063
  }, {
1052
1064
  F: __dxlog_file4,
1053
- L: 176,
1065
+ L: 178,
1054
1066
  S: this,
1055
1067
  C: (f, a) => f(...a)
1056
1068
  });
@@ -1065,7 +1077,7 @@ var Swarm = class {
1065
1077
  if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1066
1078
  void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1067
1079
  F: __dxlog_file4,
1068
- L: 187,
1080
+ L: 189,
1069
1081
  S: this,
1070
1082
  C: (f, a) => f(...a)
1071
1083
  }));
@@ -1080,14 +1092,14 @@ var Swarm = class {
1080
1092
  message
1081
1093
  }, {
1082
1094
  F: __dxlog_file4,
1083
- L: 197,
1095
+ L: 199,
1084
1096
  S: this,
1085
1097
  C: (f, a) => f(...a)
1086
1098
  });
1087
1099
  if (this._ctx.disposed) {
1088
1100
  (0, import_log4.log)("ignored for disposed swarm", void 0, {
1089
1101
  F: __dxlog_file4,
1090
- L: 199,
1102
+ L: 201,
1091
1103
  S: this,
1092
1104
  C: (f, a) => f(...a)
1093
1105
  });
@@ -1101,7 +1113,7 @@ var Swarm = class {
1101
1113
  message
1102
1114
  }, {
1103
1115
  F: __dxlog_file4,
1104
- L: 206,
1116
+ L: 208,
1105
1117
  S: this,
1106
1118
  C: (f, a) => f(...a)
1107
1119
  });
@@ -1114,7 +1126,7 @@ var Swarm = class {
1114
1126
  message
1115
1127
  }, {
1116
1128
  F: __dxlog_file4,
1117
- L: 210,
1129
+ L: 212,
1118
1130
  S: this,
1119
1131
  C: (f, a) => f(...a)
1120
1132
  });
@@ -1133,14 +1145,14 @@ var Swarm = class {
1133
1145
  message
1134
1146
  }, {
1135
1147
  F: __dxlog_file4,
1136
- L: 222,
1148
+ L: 223,
1137
1149
  S: this,
1138
1150
  C: (f, a) => f(...a)
1139
1151
  });
1140
1152
  if (this._ctx.disposed) {
1141
1153
  import_log4.log.info("ignored for offline swarm", void 0, {
1142
1154
  F: __dxlog_file4,
1143
- L: 224,
1155
+ L: 225,
1144
1156
  S: this,
1145
1157
  C: (f, a) => f(...a)
1146
1158
  });
@@ -1166,7 +1178,7 @@ var Swarm = class {
1166
1178
  _getOrCreatePeer(peerId) {
1167
1179
  let peer = this._peers.get(peerId);
1168
1180
  if (!peer) {
1169
- peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, {
1181
+ peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, this._connectionLimiter, {
1170
1182
  onInitiated: (connection) => {
1171
1183
  this.connectionAdded.emit(connection);
1172
1184
  },
@@ -1221,7 +1233,7 @@ var Swarm = class {
1221
1233
  } catch (err) {
1222
1234
  (0, import_log4.log)("initiation error", err, {
1223
1235
  F: __dxlog_file4,
1224
- L: 327,
1236
+ L: 329,
1225
1237
  S: this,
1226
1238
  C: (f, a) => f(...a)
1227
1239
  });
@@ -1249,7 +1261,7 @@ var Swarm = class {
1249
1261
  remoteId
1250
1262
  }, {
1251
1263
  F: __dxlog_file4,
1252
- L: 354,
1264
+ L: 356,
1253
1265
  S: this,
1254
1266
  C: (f, a) => f(...a)
1255
1267
  });
@@ -1266,7 +1278,7 @@ var Swarm = class {
1266
1278
  remoteId
1267
1279
  }, {
1268
1280
  F: __dxlog_file4,
1269
- L: 368,
1281
+ L: 370,
1270
1282
  S: this,
1271
1283
  C: (f, a) => f(...a)
1272
1284
  });
@@ -1276,7 +1288,7 @@ var Swarm = class {
1276
1288
  remoteId
1277
1289
  }, {
1278
1290
  F: __dxlog_file4,
1279
- L: 371,
1291
+ L: 373,
1280
1292
  S: this,
1281
1293
  C: (f, a) => f(...a)
1282
1294
  });
@@ -1304,9 +1316,6 @@ _ts_decorate3([
1304
1316
  _ts_decorate3([
1305
1317
  import_async3.synchronized
1306
1318
  ], Swarm.prototype, "onOffer", null);
1307
- _ts_decorate3([
1308
- import_async3.synchronized
1309
- ], Swarm.prototype, "onSignal", null);
1310
1319
  _ts_decorate3([
1311
1320
  import_async3.synchronized
1312
1321
  ], Swarm.prototype, "goOffline", null);
@@ -1385,6 +1394,87 @@ var SwarmMapper = class {
1385
1394
  }
1386
1395
  };
1387
1396
 
1397
+ // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
1398
+ var import_async5 = require("@dxos/async");
1399
+ var import_context5 = require("@dxos/context");
1400
+ var import_errors3 = require("@dxos/errors");
1401
+ var import_keys6 = require("@dxos/keys");
1402
+ var import_log6 = require("@dxos/log");
1403
+ var import_util4 = require("@dxos/util");
1404
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1405
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
1406
+ var ConnectionLimiter = class {
1407
+ constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1408
+ this._ctx = new import_context5.Context();
1409
+ /**
1410
+ * Queue of promises to resolve when initiating connections amount is below the limit.
1411
+ */
1412
+ this._waitingPromises = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
1413
+ this.resolveWaitingPromises = new import_async5.DeferredTask(this._ctx, async () => {
1414
+ Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
1415
+ resolve();
1416
+ });
1417
+ });
1418
+ this._maxConcurrentInitConnections = maxConcurrentInitConnections;
1419
+ }
1420
+ /**
1421
+ * @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
1422
+ */
1423
+ async connecting(sessionId) {
1424
+ (0, import_log6.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1425
+ F: __dxlog_file6,
1426
+ L: 47,
1427
+ S: this,
1428
+ A: [
1429
+ "!this._waitingPromises.has(sessionId)",
1430
+ "'Peer is already waiting for connection'"
1431
+ ]
1432
+ });
1433
+ (0, import_log6.log)("waiting", {
1434
+ sessionId
1435
+ }, {
1436
+ F: __dxlog_file6,
1437
+ L: 48,
1438
+ S: this,
1439
+ C: (f, a) => f(...a)
1440
+ });
1441
+ await new Promise((resolve, reject) => {
1442
+ this._waitingPromises.set(sessionId, {
1443
+ resolve,
1444
+ reject
1445
+ });
1446
+ this.resolveWaitingPromises.schedule();
1447
+ });
1448
+ (0, import_log6.log)("allow", {
1449
+ sessionId
1450
+ }, {
1451
+ F: __dxlog_file6,
1452
+ L: 56,
1453
+ S: this,
1454
+ C: (f, a) => f(...a)
1455
+ });
1456
+ }
1457
+ /**
1458
+ * Rejects promise returned by `connecting` method.
1459
+ */
1460
+ doneConnecting(sessionId) {
1461
+ (0, import_log6.log)("done", {
1462
+ sessionId
1463
+ }, {
1464
+ F: __dxlog_file6,
1465
+ L: 63,
1466
+ S: this,
1467
+ C: (f, a) => f(...a)
1468
+ });
1469
+ if (!this._waitingPromises.has(sessionId)) {
1470
+ return;
1471
+ }
1472
+ this._waitingPromises.get(sessionId).reject(new import_errors3.CancelledError());
1473
+ this._waitingPromises.delete(sessionId);
1474
+ this.resolveWaitingPromises.schedule();
1475
+ }
1476
+ };
1477
+
1388
1478
  // packages/core/mesh/network-manager/src/connection-log.ts
1389
1479
  var EventType;
1390
1480
  (function(EventType2) {
@@ -1399,8 +1489,8 @@ var ConnectionLog = class {
1399
1489
  /**
1400
1490
  * SwarmId => info
1401
1491
  */
1402
- this._swarms = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
1403
- this.update = new import_async5.Event();
1492
+ this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1493
+ this.update = new import_async6.Event();
1404
1494
  }
1405
1495
  getSwarmInfo(swarmId) {
1406
1496
  var _a;
@@ -1411,13 +1501,13 @@ var ConnectionLog = class {
1411
1501
  }
1412
1502
  joinedSwarm(swarm) {
1413
1503
  const info = {
1414
- id: import_keys6.PublicKey.from(swarm._instanceId),
1504
+ id: import_keys7.PublicKey.from(swarm._instanceId),
1415
1505
  topic: swarm.topic,
1416
1506
  isActive: true,
1417
1507
  label: swarm.label,
1418
1508
  connections: []
1419
1509
  };
1420
- this._swarms.set(import_keys6.PublicKey.from(swarm._instanceId), info);
1510
+ this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
1421
1511
  this.update.emit();
1422
1512
  swarm.connectionAdded.on((connection) => {
1423
1513
  var _a, _b;
@@ -1446,29 +1536,32 @@ var ConnectionLog = class {
1446
1536
  });
1447
1537
  }
1448
1538
  leftSwarm(swarm) {
1449
- this.getSwarmInfo(import_keys6.PublicKey.from(swarm._instanceId)).isActive = false;
1539
+ this.getSwarmInfo(import_keys7.PublicKey.from(swarm._instanceId)).isActive = false;
1450
1540
  this.update.emit();
1451
1541
  }
1452
1542
  };
1453
1543
 
1454
1544
  // packages/core/mesh/network-manager/src/network-manager.ts
1455
1545
  var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
1456
- var import_async6 = require("@dxos/async");
1457
- var import_keys7 = require("@dxos/keys");
1458
- var import_log6 = require("@dxos/log");
1546
+ var import_async7 = require("@dxos/async");
1547
+ var import_keys8 = require("@dxos/keys");
1548
+ var import_log7 = require("@dxos/log");
1459
1549
  var import_messaging = require("@dxos/messaging");
1460
1550
  var import_protocols4 = require("@dxos/protocols");
1461
1551
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
1462
- var import_util5 = require("@dxos/util");
1463
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
1552
+ var import_util6 = require("@dxos/util");
1553
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
1464
1554
  var NetworkManager = class {
1465
1555
  constructor({ transportFactory, signalManager, log: log1 }) {
1466
- this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1467
- this._mappers = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1556
+ /**
1557
+ * @internal
1558
+ */
1559
+ this._swarms = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
1560
+ this._mappers = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
1468
1561
  this._connectionState = import_services.ConnectionState.ONLINE;
1469
- this.connectionStateChanged = new import_async6.Event();
1470
- this.topicsUpdated = new import_async6.Event();
1471
- this._instanceId = import_keys7.PublicKey.random().toHex();
1562
+ this.connectionStateChanged = new import_async7.Event();
1563
+ this.topicsUpdated = new import_async7.Event();
1564
+ this._instanceId = import_keys8.PublicKey.random().toHex();
1472
1565
  this._transportFactory = transportFactory;
1473
1566
  this._signalManager = signalManager;
1474
1567
  this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
@@ -1482,6 +1575,7 @@ var NetworkManager = class {
1482
1575
  join: (opts) => this._signalManager.join(opts),
1483
1576
  leave: (opts) => this._signalManager.leave(opts)
1484
1577
  };
1578
+ this._connectionLimiter = new ConnectionLimiter();
1485
1579
  if (log1) {
1486
1580
  this._connectionLog = new ConnectionLog();
1487
1581
  }
@@ -1504,21 +1598,21 @@ var NetworkManager = class {
1504
1598
  return this._swarms.get(topic);
1505
1599
  }
1506
1600
  async open() {
1507
- import_log6.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
1601
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
1508
1602
  id: this._instanceId
1509
1603
  }), {
1510
- F: __dxlog_file6,
1511
- L: 127,
1604
+ F: __dxlog_file7,
1605
+ L: 131,
1512
1606
  S: this,
1513
1607
  C: (f, a) => f(...a)
1514
1608
  });
1515
1609
  await this._messenger.open();
1516
1610
  await this._signalManager.open();
1517
- import_log6.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
1611
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
1518
1612
  id: this._instanceId
1519
1613
  }), {
1520
- F: __dxlog_file6,
1521
- L: 130,
1614
+ F: __dxlog_file7,
1615
+ L: 134,
1522
1616
  S: this,
1523
1617
  C: (f, a) => f(...a)
1524
1618
  });
@@ -1526,9 +1620,9 @@ var NetworkManager = class {
1526
1620
  async close() {
1527
1621
  for (const topic of this._swarms.keys()) {
1528
1622
  await this.leaveSwarm(topic).catch((err) => {
1529
- (0, import_log6.log)(err, void 0, {
1530
- F: __dxlog_file6,
1531
- L: 136,
1623
+ (0, import_log7.log)(err, void 0, {
1624
+ F: __dxlog_file7,
1625
+ L: 140,
1532
1626
  S: this,
1533
1627
  C: (f, a) => f(...a)
1534
1628
  });
@@ -1542,30 +1636,30 @@ var NetworkManager = class {
1542
1636
  */
1543
1637
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1544
1638
  var _a;
1545
- (0, import_tiny_invariant5.default)(import_keys7.PublicKey.isPublicKey(topic));
1546
- (0, import_tiny_invariant5.default)(import_keys7.PublicKey.isPublicKey(peerId));
1639
+ (0, import_tiny_invariant5.default)(import_keys8.PublicKey.isPublicKey(topic));
1640
+ (0, import_tiny_invariant5.default)(import_keys8.PublicKey.isPublicKey(peerId));
1547
1641
  (0, import_tiny_invariant5.default)(topology);
1548
1642
  (0, import_tiny_invariant5.default)(typeof protocol === "function");
1549
1643
  if (this._swarms.has(topic)) {
1550
- throw new Error(`Already connected to swarm: ${import_keys7.PublicKey.from(topic)}`);
1644
+ throw new Error(`Already connected to swarm: ${import_keys8.PublicKey.from(topic)}`);
1551
1645
  }
1552
- (0, import_log6.log)("joining", {
1553
- topic: import_keys7.PublicKey.from(topic),
1646
+ (0, import_log7.log)("joining", {
1647
+ topic: import_keys8.PublicKey.from(topic),
1554
1648
  peerId,
1555
1649
  topology: topology.toString()
1556
1650
  }, {
1557
- F: __dxlog_file6,
1558
- L: 162,
1651
+ F: __dxlog_file7,
1652
+ L: 166,
1559
1653
  S: this,
1560
1654
  C: (f, a) => f(...a)
1561
1655
  });
1562
- const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label);
1656
+ const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
1563
1657
  swarm.errors.handle((error) => {
1564
- (0, import_log6.log)("swarm error", {
1658
+ (0, import_log7.log)("swarm error", {
1565
1659
  error
1566
1660
  }, {
1567
- F: __dxlog_file6,
1568
- L: 165,
1661
+ F: __dxlog_file7,
1662
+ L: 179,
1569
1663
  S: this,
1570
1664
  C: (f, a) => f(...a)
1571
1665
  });
@@ -1576,20 +1670,20 @@ var NetworkManager = class {
1576
1670
  this._signalConnection.join({
1577
1671
  topic,
1578
1672
  peerId
1579
- }).catch((error) => import_log6.log.catch(error, void 0, {
1580
- F: __dxlog_file6,
1581
- L: 174,
1673
+ }).catch((error) => import_log7.log.catch(error, void 0, {
1674
+ F: __dxlog_file7,
1675
+ L: 188,
1582
1676
  S: this,
1583
1677
  C: (f, a) => f(...a)
1584
1678
  }));
1585
1679
  this.topicsUpdated.emit();
1586
1680
  (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1587
- (0, import_log6.log)("joined", {
1588
- topic: import_keys7.PublicKey.from(topic),
1681
+ (0, import_log7.log)("joined", {
1682
+ topic: import_keys8.PublicKey.from(topic),
1589
1683
  count: this._swarms.size
1590
1684
  }, {
1591
- F: __dxlog_file6,
1592
- L: 178,
1685
+ F: __dxlog_file7,
1686
+ L: 192,
1593
1687
  S: this,
1594
1688
  C: (f, a) => f(...a)
1595
1689
  });
@@ -1605,11 +1699,11 @@ var NetworkManager = class {
1605
1699
  if (!this._swarms.has(topic)) {
1606
1700
  return;
1607
1701
  }
1608
- (0, import_log6.log)("leaving", {
1609
- topic: import_keys7.PublicKey.from(topic)
1702
+ (0, import_log7.log)("leaving", {
1703
+ topic: import_keys8.PublicKey.from(topic)
1610
1704
  }, {
1611
- F: __dxlog_file6,
1612
- L: 194,
1705
+ F: __dxlog_file7,
1706
+ L: 208,
1613
1707
  S: this,
1614
1708
  C: (f, a) => f(...a)
1615
1709
  });
@@ -1625,12 +1719,12 @@ var NetworkManager = class {
1625
1719
  await swarm.destroy();
1626
1720
  this._swarms.delete(topic);
1627
1721
  await this.topicsUpdated.emit();
1628
- (0, import_log6.log)("left", {
1629
- topic: import_keys7.PublicKey.from(topic),
1722
+ (0, import_log7.log)("left", {
1723
+ topic: import_keys8.PublicKey.from(topic),
1630
1724
  count: this._swarms.size
1631
1725
  }, {
1632
- F: __dxlog_file6,
1633
- L: 208,
1726
+ F: __dxlog_file7,
1727
+ L: 222,
1634
1728
  S: this,
1635
1729
  C: (f, a) => f(...a)
1636
1730
  });
@@ -1690,8 +1784,8 @@ var FullyConnectedTopology = class {
1690
1784
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
1691
1785
  var import_tiny_invariant7 = __toESM(require("tiny-invariant"));
1692
1786
  var import_xor_distance = __toESM(require("xor-distance"));
1693
- var import_log7 = require("@dxos/log");
1694
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
1787
+ var import_log8 = require("@dxos/log");
1788
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
1695
1789
  var MMSTTopology = class {
1696
1790
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
1697
1791
  this._sampleCollected = false;
@@ -1707,8 +1801,8 @@ var MMSTTopology = class {
1707
1801
  (0, import_tiny_invariant7.default)(this._controller, "Not initialized");
1708
1802
  const { connected, candidates } = this._controller.getState();
1709
1803
  if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
1710
- (0, import_log7.log)("Running the algorithm.", void 0, {
1711
- F: __dxlog_file7,
1804
+ (0, import_log8.log)("Running the algorithm.", void 0, {
1805
+ F: __dxlog_file8,
1712
1806
  L: 55,
1713
1807
  S: this,
1714
1808
  C: (f, a) => f(...a)
@@ -1721,8 +1815,8 @@ var MMSTTopology = class {
1721
1815
  (0, import_tiny_invariant7.default)(this._controller, "Not initialized");
1722
1816
  const { connected } = this._controller.getState();
1723
1817
  const accept = connected.length < this._maxPeers;
1724
- (0, import_log7.log)(`Offer ${peer} accept=${accept}`, void 0, {
1725
- F: __dxlog_file7,
1818
+ (0, import_log8.log)(`Offer ${peer} accept=${accept}`, void 0, {
1819
+ F: __dxlog_file8,
1726
1820
  L: 65,
1727
1821
  S: this,
1728
1822
  C: (f, a) => f(...a)
@@ -1737,8 +1831,8 @@ var MMSTTopology = class {
1737
1831
  if (connected.length > this._maxPeers) {
1738
1832
  const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
1739
1833
  for (const peer of sorted) {
1740
- (0, import_log7.log)(`Disconnect ${peer}.`, void 0, {
1741
- F: __dxlog_file7,
1834
+ (0, import_log8.log)(`Disconnect ${peer}.`, void 0, {
1835
+ F: __dxlog_file8,
1742
1836
  L: 83,
1743
1837
  S: this,
1744
1838
  C: (f, a) => f(...a)
@@ -1749,8 +1843,8 @@ var MMSTTopology = class {
1749
1843
  const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
1750
1844
  const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
1751
1845
  for (const peer of sorted) {
1752
- (0, import_log7.log)(`Connect ${peer}.`, void 0, {
1753
- F: __dxlog_file7,
1846
+ (0, import_log8.log)(`Connect ${peer}.`, void 0, {
1847
+ F: __dxlog_file8,
1754
1848
  L: 91,
1755
1849
  S: this,
1756
1850
  C: (f, a) => f(...a)
@@ -1767,8 +1861,8 @@ var sortByXorDistance = (keys, reference) => keys.sort((a, b) => import_xor_dist
1767
1861
 
1768
1862
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
1769
1863
  var import_tiny_invariant8 = __toESM(require("tiny-invariant"));
1770
- var import_log8 = require("@dxos/log");
1771
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
1864
+ var import_log9 = require("@dxos/log");
1865
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
1772
1866
  var StarTopology = class {
1773
1867
  // prettier-ignore
1774
1868
  constructor(_centralPeer) {
@@ -1785,18 +1879,18 @@ var StarTopology = class {
1785
1879
  (0, import_tiny_invariant8.default)(this._controller, "Not initialized.");
1786
1880
  const { candidates, connected, ownPeerId } = this._controller.getState();
1787
1881
  if (!ownPeerId.equals(this._centralPeer)) {
1788
- (0, import_log8.log)("leaf peer dropping all connections apart from central peer.", void 0, {
1789
- F: __dxlog_file8,
1882
+ (0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
1883
+ F: __dxlog_file9,
1790
1884
  L: 33,
1791
1885
  S: this,
1792
1886
  C: (f, a) => f(...a)
1793
1887
  });
1794
1888
  for (const peer of connected) {
1795
1889
  if (!peer.equals(this._centralPeer)) {
1796
- (0, import_log8.log)("dropping connection", {
1890
+ (0, import_log9.log)("dropping connection", {
1797
1891
  peer
1798
1892
  }, {
1799
- F: __dxlog_file8,
1893
+ F: __dxlog_file9,
1800
1894
  L: 38,
1801
1895
  S: this,
1802
1896
  C: (f, a) => f(...a)
@@ -1807,10 +1901,10 @@ var StarTopology = class {
1807
1901
  }
1808
1902
  for (const peer of candidates) {
1809
1903
  if (peer.equals(this._centralPeer) || ownPeerId.equals(this._centralPeer)) {
1810
- (0, import_log8.log)("connecting to peer", {
1904
+ (0, import_log9.log)("connecting to peer", {
1811
1905
  peer
1812
1906
  }, {
1813
- F: __dxlog_file8,
1907
+ F: __dxlog_file9,
1814
1908
  L: 47,
1815
1909
  S: this,
1816
1910
  C: (f, a) => f(...a)
@@ -1822,12 +1916,12 @@ var StarTopology = class {
1822
1916
  async onOffer(peer) {
1823
1917
  (0, import_tiny_invariant8.default)(this._controller, "Not initialized.");
1824
1918
  const { ownPeerId } = this._controller.getState();
1825
- (0, import_log8.log)("offer", {
1919
+ (0, import_log9.log)("offer", {
1826
1920
  peer,
1827
1921
  isCentral: peer.equals(this._centralPeer),
1828
1922
  isSelfCentral: ownPeerId.equals(this._centralPeer)
1829
1923
  }, {
1830
- F: __dxlog_file8,
1924
+ F: __dxlog_file9,
1831
1925
  L: 56,
1832
1926
  S: this,
1833
1927
  C: (f, a) => f(...a)
@@ -1841,11 +1935,11 @@ var StarTopology = class {
1841
1935
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
1842
1936
  var import_node_stream = require("node:stream");
1843
1937
  var import_tiny_invariant9 = __toESM(require("tiny-invariant"));
1844
- var import_async7 = require("@dxos/async");
1938
+ var import_async8 = require("@dxos/async");
1845
1939
  var import_debug4 = require("@dxos/debug");
1846
- var import_keys8 = require("@dxos/keys");
1847
- var import_log9 = require("@dxos/log");
1848
- var import_util6 = require("@dxos/util");
1940
+ var import_keys9 = require("@dxos/keys");
1941
+ var import_log10 = require("@dxos/log");
1942
+ var import_util7 = require("@dxos/util");
1849
1943
  function _ts_decorate4(decorators, target, key, desc) {
1850
1944
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1851
1945
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1856,7 +1950,7 @@ function _ts_decorate4(decorators, target, key, desc) {
1856
1950
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1857
1951
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1858
1952
  }
1859
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1953
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1860
1954
  var MEMORY_TRANSPORT_DELAY = 1;
1861
1955
  var createStreamDelay = (delay) => {
1862
1956
  return new import_node_stream.Transform({
@@ -1873,16 +1967,16 @@ var _MemoryTransport = class {
1873
1967
  constructor(options) {
1874
1968
  var _a;
1875
1969
  this.options = options;
1876
- this.closed = new import_async7.Event();
1877
- this.connected = new import_async7.Event();
1970
+ this.closed = new import_async8.Event();
1971
+ this.connected = new import_async8.Event();
1878
1972
  this.errors = new import_debug4.ErrorStream();
1879
- this._instanceId = import_keys8.PublicKey.random();
1880
- this._remote = new import_async7.Trigger();
1973
+ this._instanceId = import_keys9.PublicKey.random();
1974
+ this._remote = new import_async8.Trigger();
1881
1975
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1882
1976
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1883
1977
  this._destroyed = false;
1884
- (0, import_log9.log)("creating", void 0, {
1885
- F: __dxlog_file9,
1978
+ (0, import_log10.log)("creating", void 0, {
1979
+ F: __dxlog_file10,
1886
1980
  L: 64,
1887
1981
  S: this,
1888
1982
  C: (f, a) => f(...a)
@@ -1891,8 +1985,8 @@ var _MemoryTransport = class {
1891
1985
  _MemoryTransport._connections.set(this._instanceId, this);
1892
1986
  if (this.options.initiator) {
1893
1987
  setTimeout(async () => {
1894
- (0, import_log9.log)("sending signal", void 0, {
1895
- F: __dxlog_file9,
1988
+ (0, import_log10.log)("sending signal", void 0, {
1989
+ F: __dxlog_file10,
1896
1990
  L: 73,
1897
1991
  S: this,
1898
1992
  C: (f, a) => f(...a)
@@ -1924,8 +2018,8 @@ var _MemoryTransport = class {
1924
2018
  (0, import_tiny_invariant9.default)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
1925
2019
  this._remoteConnection._remoteConnection = this;
1926
2020
  this._remoteConnection._remoteInstanceId = this._instanceId;
1927
- (0, import_log9.log)("connected", void 0, {
1928
- F: __dxlog_file9,
2021
+ (0, import_log10.log)("connected", void 0, {
2022
+ F: __dxlog_file10,
1929
2023
  L: 103,
1930
2024
  S: this,
1931
2025
  C: (f, a) => f(...a)
@@ -1942,8 +2036,8 @@ var _MemoryTransport = class {
1942
2036
  }
1943
2037
  }
1944
2038
  async destroy() {
1945
- (0, import_log9.log)("closing", void 0, {
1946
- F: __dxlog_file9,
2039
+ (0, import_log10.log)("closing", void 0, {
2040
+ F: __dxlog_file10,
1947
2041
  L: 124,
1948
2042
  S: this,
1949
2043
  C: (f, a) => f(...a)
@@ -1951,8 +2045,8 @@ var _MemoryTransport = class {
1951
2045
  this._destroyed = true;
1952
2046
  _MemoryTransport._connections.delete(this._instanceId);
1953
2047
  if (this._remoteConnection) {
1954
- (0, import_log9.log)("closing", void 0, {
1955
- F: __dxlog_file9,
2048
+ (0, import_log10.log)("closing", void 0, {
2049
+ F: __dxlog_file10,
1956
2050
  L: 129,
1957
2051
  S: this,
1958
2052
  C: (f, a) => f(...a)
@@ -1964,26 +2058,26 @@ var _MemoryTransport = class {
1964
2058
  this._remoteConnection.closed.emit();
1965
2059
  this._remoteConnection._remoteConnection = void 0;
1966
2060
  this._remoteConnection = void 0;
1967
- (0, import_log9.log)("closed", void 0, {
1968
- F: __dxlog_file9,
2061
+ (0, import_log10.log)("closed", void 0, {
2062
+ F: __dxlog_file10,
1969
2063
  L: 147,
1970
2064
  S: this,
1971
2065
  C: (f, a) => f(...a)
1972
2066
  });
1973
2067
  }
1974
2068
  this.closed.emit();
1975
- (0, import_log9.log)("closed", void 0, {
1976
- F: __dxlog_file9,
2069
+ (0, import_log10.log)("closed", void 0, {
2070
+ F: __dxlog_file10,
1977
2071
  L: 151,
1978
2072
  S: this,
1979
2073
  C: (f, a) => f(...a)
1980
2074
  });
1981
2075
  }
1982
2076
  signal({ payload }) {
1983
- (0, import_log9.log)("received signal", {
2077
+ (0, import_log10.log)("received signal", {
1984
2078
  payload
1985
2079
  }, {
1986
- F: __dxlog_file9,
2080
+ F: __dxlog_file10,
1987
2081
  L: 155,
1988
2082
  S: this,
1989
2083
  C: (f, a) => f(...a)
@@ -1993,28 +2087,28 @@ var _MemoryTransport = class {
1993
2087
  }
1994
2088
  const transportId = payload.transportId;
1995
2089
  if (transportId) {
1996
- const remoteId = import_keys8.PublicKey.fromHex(transportId);
2090
+ const remoteId = import_keys9.PublicKey.fromHex(transportId);
1997
2091
  this._remote.wake(remoteId);
1998
2092
  }
1999
2093
  }
2000
2094
  };
2001
2095
  var MemoryTransport = _MemoryTransport;
2002
2096
  // TODO(burdon): Remove static properties (inject context into constructor).
2003
- MemoryTransport._connections = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
2097
+ MemoryTransport._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2004
2098
  _ts_decorate4([
2005
- import_log9.logInfo
2099
+ import_log10.logInfo
2006
2100
  ], MemoryTransport.prototype, "_instanceId", void 0);
2007
2101
  _ts_decorate4([
2008
- import_log9.logInfo
2102
+ import_log10.logInfo
2009
2103
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2010
2104
 
2011
2105
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2012
2106
  var import_simple_peer = __toESM(require("simple-peer"));
2013
2107
  var import_tiny_invariant10 = __toESM(require("tiny-invariant"));
2014
- var import_async8 = require("@dxos/async");
2108
+ var import_async9 = require("@dxos/async");
2015
2109
  var import_debug5 = require("@dxos/debug");
2016
- var import_keys9 = require("@dxos/keys");
2017
- var import_log10 = require("@dxos/log");
2110
+ var import_keys10 = require("@dxos/keys");
2111
+ var import_log11 = require("@dxos/log");
2018
2112
  var import_protocols5 = require("@dxos/protocols");
2019
2113
 
2020
2114
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
@@ -2025,26 +2119,26 @@ try {
2025
2119
  }
2026
2120
 
2027
2121
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2028
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2122
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2029
2123
  var WebRTCTransport = class {
2030
2124
  constructor(params) {
2031
2125
  var _a;
2032
2126
  this.params = params;
2033
2127
  this._closed = false;
2034
- this.closed = new import_async8.Event();
2035
- this.connected = new import_async8.Event();
2128
+ this.closed = new import_async9.Event();
2129
+ this.connected = new import_async9.Event();
2036
2130
  this.errors = new import_debug5.ErrorStream();
2037
- this._instanceId = import_keys9.PublicKey.random().toHex();
2038
- import_log10.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2131
+ this._instanceId = import_keys10.PublicKey.random().toHex();
2132
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2039
2133
  id: this._instanceId
2040
2134
  }), {
2041
- F: __dxlog_file10,
2135
+ F: __dxlog_file11,
2042
2136
  L: 39,
2043
2137
  S: this,
2044
2138
  C: (f, a) => f(...a)
2045
2139
  });
2046
- (0, import_log10.log)("created connection", params, {
2047
- F: __dxlog_file10,
2140
+ (0, import_log11.log)("created connection", params, {
2141
+ F: __dxlog_file11,
2048
2142
  L: 40,
2049
2143
  S: this,
2050
2144
  C: (f, a) => f(...a)
@@ -2055,8 +2149,8 @@ var WebRTCTransport = class {
2055
2149
  config: this.params.webrtcConfig
2056
2150
  });
2057
2151
  this._peer.on("signal", async (data) => {
2058
- (0, import_log10.log)("signal", data, {
2059
- F: __dxlog_file10,
2152
+ (0, import_log11.log)("signal", data, {
2153
+ F: __dxlog_file11,
2060
2154
  L: 48,
2061
2155
  S: this,
2062
2156
  C: (f, a) => f(...a)
@@ -2068,8 +2162,8 @@ var WebRTCTransport = class {
2068
2162
  });
2069
2163
  });
2070
2164
  this._peer.on("connect", () => {
2071
- (0, import_log10.log)("connected", void 0, {
2072
- F: __dxlog_file10,
2165
+ (0, import_log11.log)("connected", void 0, {
2166
+ F: __dxlog_file11,
2073
2167
  L: 53,
2074
2168
  S: this,
2075
2169
  C: (f, a) => f(...a)
@@ -2078,8 +2172,8 @@ var WebRTCTransport = class {
2078
2172
  this.connected.emit();
2079
2173
  });
2080
2174
  this._peer.on("close", async () => {
2081
- (0, import_log10.log)("closed", void 0, {
2082
- F: __dxlog_file10,
2175
+ (0, import_log11.log)("closed", void 0, {
2176
+ F: __dxlog_file11,
2083
2177
  L: 59,
2084
2178
  S: this,
2085
2179
  C: (f, a) => f(...a)
@@ -2093,11 +2187,11 @@ var WebRTCTransport = class {
2093
2187
  try {
2094
2188
  if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2095
2189
  this._peer._pc.getStats().then((stats) => {
2096
- import_log10.log.warn("report after webrtc error", {
2190
+ import_log11.log.warn("report after webrtc error", {
2097
2191
  config: this.params.webrtcConfig,
2098
2192
  stats
2099
2193
  }, {
2100
- F: __dxlog_file10,
2194
+ F: __dxlog_file11,
2101
2195
  L: 71,
2102
2196
  S: this,
2103
2197
  C: (f, a) => f(...a)
@@ -2108,18 +2202,18 @@ var WebRTCTransport = class {
2108
2202
  }
2109
2203
  await this.destroy();
2110
2204
  });
2111
- import_log10.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
2205
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
2112
2206
  id: this._instanceId
2113
2207
  }), {
2114
- F: __dxlog_file10,
2208
+ F: __dxlog_file11,
2115
2209
  L: 81,
2116
2210
  S: this,
2117
2211
  C: (f, a) => f(...a)
2118
2212
  });
2119
2213
  }
2120
2214
  async destroy() {
2121
- (0, import_log10.log)("closing...", void 0, {
2122
- F: __dxlog_file10,
2215
+ (0, import_log11.log)("closing...", void 0, {
2216
+ F: __dxlog_file11,
2123
2217
  L: 85,
2124
2218
  S: this,
2125
2219
  C: (f, a) => f(...a)
@@ -2128,8 +2222,8 @@ var WebRTCTransport = class {
2128
2222
  await this._disconnectStreams();
2129
2223
  this._peer.destroy();
2130
2224
  this.closed.emit();
2131
- (0, import_log10.log)("closed", void 0, {
2132
- F: __dxlog_file10,
2225
+ (0, import_log11.log)("closed", void 0, {
2226
+ F: __dxlog_file11,
2133
2227
  L: 90,
2134
2228
  S: this,
2135
2229
  C: (f, a) => f(...a)
@@ -2158,16 +2252,16 @@ var createWebRTCTransportFactory = (webrtcConfig) => ({
2158
2252
  var import_node_stream2 = require("node:stream");
2159
2253
  var import_tiny_invariant11 = __toESM(require("tiny-invariant"));
2160
2254
  var import_codec_protobuf = require("@dxos/codec-protobuf");
2161
- var import_keys10 = require("@dxos/keys");
2162
- var import_log11 = require("@dxos/log");
2255
+ var import_keys11 = require("@dxos/keys");
2256
+ var import_log12 = require("@dxos/log");
2163
2257
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
2164
- var import_util7 = require("@dxos/util");
2165
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2258
+ var import_util8 = require("@dxos/util");
2259
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2166
2260
  var WebRTCTransportService = class {
2167
2261
  // prettier-ignore
2168
2262
  constructor(_webrtcConfig) {
2169
2263
  this._webrtcConfig = _webrtcConfig;
2170
- this.transports = new import_util7.ComplexMap(import_keys10.PublicKey.hash);
2264
+ this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
2171
2265
  }
2172
2266
  open(request) {
2173
2267
  const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
@@ -2245,8 +2339,8 @@ var WebRTCTransportService = class {
2245
2339
  await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2246
2340
  await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2247
2341
  this.transports.delete(proxyId);
2248
- (0, import_log11.log)("Closed.", void 0, {
2249
- F: __dxlog_file11,
2342
+ (0, import_log12.log)("Closed.", void 0, {
2343
+ F: __dxlog_file12,
2250
2344
  L: 109,
2251
2345
  S: this,
2252
2346
  C: (f, a) => f(...a)
@@ -2256,23 +2350,23 @@ var WebRTCTransportService = class {
2256
2350
 
2257
2351
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2258
2352
  var import_tiny_invariant12 = __toESM(require("tiny-invariant"));
2259
- var import_async9 = require("@dxos/async");
2260
- var import_context5 = require("@dxos/context");
2353
+ var import_async10 = require("@dxos/async");
2354
+ var import_context6 = require("@dxos/context");
2261
2355
  var import_debug6 = require("@dxos/debug");
2262
- var import_keys11 = require("@dxos/keys");
2263
- var import_log12 = require("@dxos/log");
2356
+ var import_keys12 = require("@dxos/keys");
2357
+ var import_log13 = require("@dxos/log");
2264
2358
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
2265
- var import_util8 = require("@dxos/util");
2266
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2359
+ var import_util9 = require("@dxos/util");
2360
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2267
2361
  var WebRTCTransportProxy = class {
2268
2362
  // prettier-ignore
2269
2363
  constructor(_params) {
2270
2364
  this._params = _params;
2271
- this._proxyId = import_keys11.PublicKey.random();
2272
- this._ctx = new import_context5.Context();
2273
- this.closed = new import_async9.Event();
2365
+ this._proxyId = import_keys12.PublicKey.random();
2366
+ this._ctx = new import_context6.Context();
2367
+ this.closed = new import_async10.Event();
2274
2368
  this._closed = false;
2275
- this.connected = new import_async9.Event();
2369
+ this.connected = new import_async10.Event();
2276
2370
  this.errors = new import_debug6.ErrorStream();
2277
2371
  this._serviceStream = this._params.bridgeService.open({
2278
2372
  proxyId: this._proxyId,
@@ -2280,8 +2374,8 @@ var WebRTCTransportProxy = class {
2280
2374
  });
2281
2375
  this._serviceStream.waitUntilReady().then(() => {
2282
2376
  this._serviceStream.subscribe(async (event) => {
2283
- (0, import_log12.log)("WebRTCTransportProxy: event", event, {
2284
- F: __dxlog_file12,
2377
+ (0, import_log13.log)("WebRTCTransportProxy: event", event, {
2378
+ F: __dxlog_file13,
2285
2379
  L: 50,
2286
2380
  S: this,
2287
2381
  C: (f, a) => f(...a)
@@ -2301,8 +2395,8 @@ var WebRTCTransportProxy = class {
2301
2395
  payload: data
2302
2396
  });
2303
2397
  } catch (err) {
2304
- import_log12.log.catch(err, void 0, {
2305
- F: __dxlog_file12,
2398
+ import_log13.log.catch(err, void 0, {
2399
+ F: __dxlog_file13,
2306
2400
  L: 67,
2307
2401
  S: this,
2308
2402
  C: (f, a) => f(...a)
@@ -2313,8 +2407,8 @@ var WebRTCTransportProxy = class {
2313
2407
  this._ctx.onDispose(() => {
2314
2408
  this._params.stream.off("data", dataListener);
2315
2409
  });
2316
- }, (error) => import_log12.log.catch(error, void 0, {
2317
- F: __dxlog_file12,
2410
+ }, (error) => import_log13.log.catch(error, void 0, {
2411
+ F: __dxlog_file13,
2318
2412
  L: 73,
2319
2413
  S: this,
2320
2414
  C: (f, a) => f(...a)
@@ -2336,7 +2430,7 @@ var WebRTCTransportProxy = class {
2336
2430
  }
2337
2431
  }
2338
2432
  _handleData(dataEvent) {
2339
- this._params.stream.write((0, import_util8.arrayToBuffer)(dataEvent.payload));
2433
+ this._params.stream.write((0, import_util9.arrayToBuffer)(dataEvent.payload));
2340
2434
  }
2341
2435
  async _handleSignal(signalEvent) {
2342
2436
  await this._params.sendSignal(signalEvent.payload);
@@ -2359,8 +2453,8 @@ var WebRTCTransportProxy = class {
2359
2453
  proxyId: this._proxyId
2360
2454
  });
2361
2455
  } catch (err) {
2362
- import_log12.log.catch(err, void 0, {
2363
- F: __dxlog_file12,
2456
+ import_log13.log.catch(err, void 0, {
2457
+ F: __dxlog_file13,
2364
2458
  L: 124,
2365
2459
  S: this,
2366
2460
  C: (f, a) => f(...a)
@@ -2426,10 +2520,12 @@ var createTeleportProtocolFactory = (onConnection) => {
2426
2520
  // Annotate the CommonJS export names for ESM import in node:
2427
2521
  0 && (module.exports = {
2428
2522
  Connection,
2523
+ ConnectionLimiter,
2429
2524
  ConnectionLog,
2430
2525
  ConnectionState,
2431
2526
  EventType,
2432
2527
  FullyConnectedTopology,
2528
+ MAX_CONCURRENT_INITIATING_CONNECTIONS,
2433
2529
  MMSTTopology,
2434
2530
  MemoryTransport,
2435
2531
  MemoryTransportFactory,