@dxos/network-manager 0.8.4-main.84f28bd → 0.8.4-main.ae835ea

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 (76) hide show
  1. package/dist/lib/browser/{chunk-HQNQOWFA.mjs → chunk-E3P563GT.mjs} +329 -230
  2. package/dist/lib/browser/chunk-E3P563GT.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 +65 -46
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/browser/transport/tcp/index.mjs +3 -5
  8. package/dist/lib/browser/transport/tcp/index.mjs.map +1 -1
  9. package/dist/lib/node-esm/{chunk-JPPMRZTH.mjs → chunk-PKBROUZV.mjs} +329 -230
  10. package/dist/lib/node-esm/chunk-PKBROUZV.mjs.map +7 -0
  11. package/dist/lib/node-esm/index.mjs +1 -1
  12. package/dist/lib/node-esm/meta.json +1 -1
  13. package/dist/lib/node-esm/testing/index.mjs +65 -46
  14. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  15. package/dist/lib/node-esm/transport/tcp/index.mjs +8 -7
  16. package/dist/lib/node-esm/transport/tcp/index.mjs.map +2 -2
  17. package/dist/types/src/connection-log.d.ts.map +1 -1
  18. package/dist/types/src/index.d.ts +1 -1
  19. package/dist/types/src/index.d.ts.map +1 -1
  20. package/dist/types/src/network-manager.d.ts.map +1 -1
  21. package/dist/types/src/signal/index.d.ts +2 -2
  22. package/dist/types/src/signal/index.d.ts.map +1 -1
  23. package/dist/types/src/signal/swarm-messenger.d.ts +1 -1
  24. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  25. package/dist/types/src/swarm/peer.d.ts +2 -2
  26. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  27. package/dist/types/src/swarm/swarm.d.ts +3 -3
  28. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  29. package/dist/types/src/testing/test-builder.d.ts +1 -1
  30. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  31. package/dist/types/src/testing/test-wire-protocol.d.ts +1 -1
  32. package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
  33. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  34. package/dist/types/src/topology/index.d.ts +1 -1
  35. package/dist/types/src/topology/index.d.ts.map +1 -1
  36. package/dist/types/src/transport/webrtc/rtc-peer-connection.d.ts +2 -2
  37. package/dist/types/src/transport/webrtc/rtc-peer-connection.d.ts.map +1 -1
  38. package/dist/types/src/transport/webrtc/rtc-transport-channel.d.ts +1 -1
  39. package/dist/types/src/transport/webrtc/rtc-transport-channel.d.ts.map +1 -1
  40. package/dist/types/src/transport/webrtc/rtc-transport-factory.d.ts.map +1 -1
  41. package/dist/types/src/transport/webrtc/rtc-transport-service.d.ts +1 -1
  42. package/dist/types/src/transport/webrtc/rtc-transport-service.d.ts.map +1 -1
  43. package/dist/types/tsconfig.tsbuildinfo +1 -1
  44. package/package.json +23 -29
  45. package/src/connection-log.ts +1 -1
  46. package/src/index.ts +1 -1
  47. package/src/network-manager.ts +1 -1
  48. package/src/signal/ice.test.ts +1 -1
  49. package/src/signal/index.ts +2 -2
  50. package/src/signal/integration.node.test.ts +2 -2
  51. package/src/signal/swarm-messenger.node.test.ts +1 -1
  52. package/src/signal/swarm-messenger.ts +1 -1
  53. package/src/swarm/connection.test.ts +2 -1
  54. package/src/swarm/connection.ts +4 -4
  55. package/src/swarm/peer.ts +3 -2
  56. package/src/swarm/swarm-mapper.ts +1 -1
  57. package/src/swarm/swarm.test.ts +5 -4
  58. package/src/swarm/swarm.ts +5 -4
  59. package/src/testing/test-builder.ts +12 -4
  60. package/src/testing/test-wire-protocol.ts +2 -2
  61. package/src/tests/basic-test-suite.ts +3 -2
  62. package/src/tests/memory-transport.test.ts +4 -2
  63. package/src/tests/tcp-transport.node.test.ts +4 -2
  64. package/src/tests/webrtc-transport.test.ts +2 -1
  65. package/src/topology/index.ts +1 -1
  66. package/src/transport/tcp/tcp-transport.ts +1 -1
  67. package/src/transport/webrtc/rtc-connection-factory.ts +1 -1
  68. package/src/transport/webrtc/rtc-peer-connection.ts +4 -3
  69. package/src/transport/webrtc/rtc-transport-channel.test.ts +3 -1
  70. package/src/transport/webrtc/rtc-transport-channel.ts +2 -1
  71. package/src/transport/webrtc/rtc-transport-factory.ts +3 -2
  72. package/src/transport/webrtc/rtc-transport-proxy.test.ts +5 -3
  73. package/src/transport/webrtc/rtc-transport-service.ts +6 -5
  74. package/src/transport/webrtc/rtc-transport.test.ts +4 -3
  75. package/dist/lib/browser/chunk-HQNQOWFA.mjs.map +0 -7
  76. package/dist/lib/node-esm/chunk-JPPMRZTH.mjs.map +0 -7
@@ -1,13 +1,13 @@
1
1
  import "@dxos/node-std/globals";
2
2
 
3
3
  // src/swarm/connection.ts
4
- import { DeferredTask, Event, sleep, scheduleTask, scheduleTaskInterval, synchronized, Trigger } from "@dxos/async";
5
- import { Context, cancelWithContext, ContextDisposedError } from "@dxos/context";
4
+ import { DeferredTask, Event, Trigger, scheduleTask, scheduleTaskInterval, sleep, synchronized } from "@dxos/async";
5
+ import { Context, ContextDisposedError, cancelWithContext } from "@dxos/context";
6
6
  import { ErrorStream } from "@dxos/debug";
7
7
  import { invariant } from "@dxos/invariant";
8
8
  import { PublicKey } from "@dxos/keys";
9
9
  import { log, logInfo } from "@dxos/log";
10
- import { CancelledError, ProtocolError, ConnectionResetError, ConnectivityError, TimeoutError, trace } from "@dxos/protocols";
10
+ import { CancelledError, ConnectionResetError, ConnectivityError, ProtocolError, TimeoutError, trace } from "@dxos/protocols";
11
11
  function _ts_decorate(decorators, target, key, desc) {
12
12
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
13
13
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -19,7 +19,7 @@ var STARTING_SIGNALLING_DELAY = 10;
19
19
  var TRANSPORT_CONNECTION_TIMEOUT = 1e4;
20
20
  var TRANSPORT_STATS_INTERVAL = 5e3;
21
21
  var MAX_SIGNALLING_DELAY = 300;
22
- var ConnectionState = /* @__PURE__ */ function(ConnectionState5) {
22
+ var ConnectionState = /* @__PURE__ */ (function(ConnectionState5) {
23
23
  ConnectionState5["CREATED"] = "CREATED";
24
24
  ConnectionState5["INITIAL"] = "INITIAL";
25
25
  ConnectionState5["CONNECTING"] = "CONNECTING";
@@ -29,8 +29,40 @@ var ConnectionState = /* @__PURE__ */ function(ConnectionState5) {
29
29
  ConnectionState5["ABORTING"] = "ABORTING";
30
30
  ConnectionState5["ABORTED"] = "ABORTED";
31
31
  return ConnectionState5;
32
- }({});
32
+ })({});
33
33
  var Connection = class {
34
+ topic;
35
+ localInfo;
36
+ remoteInfo;
37
+ sessionId;
38
+ initiator;
39
+ _signalMessaging;
40
+ _protocol;
41
+ _transportFactory;
42
+ _callbacks;
43
+ _ctx = new Context(void 0, {
44
+ F: __dxlog_file,
45
+ L: 100
46
+ });
47
+ connectedTimeoutContext = new Context(void 0, {
48
+ F: __dxlog_file,
49
+ L: 101
50
+ });
51
+ _protocolClosed = new Trigger();
52
+ _transportClosed = new Trigger();
53
+ _state = "CREATED";
54
+ _transport;
55
+ closeReason;
56
+ _incomingSignalBuffer = [];
57
+ _outgoingSignalBuffer = [];
58
+ stateChanged = new Event();
59
+ errors = new ErrorStream();
60
+ _instanceId = PublicKey.random().toHex();
61
+ transportStats = new Event();
62
+ _signalSendTask = new DeferredTask(this._ctx, async () => {
63
+ await this._flushSignalBuffer();
64
+ });
65
+ _signallingDelay = STARTING_SIGNALLING_DELAY;
34
66
  constructor(topic, localInfo, remoteInfo, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
35
67
  this.topic = topic;
36
68
  this.localInfo = localInfo;
@@ -41,27 +73,6 @@ var Connection = class {
41
73
  this._protocol = _protocol;
42
74
  this._transportFactory = _transportFactory;
43
75
  this._callbacks = _callbacks;
44
- this._ctx = new Context(void 0, {
45
- F: __dxlog_file,
46
- L: 100
47
- });
48
- this.connectedTimeoutContext = new Context(void 0, {
49
- F: __dxlog_file,
50
- L: 101
51
- });
52
- this._protocolClosed = new Trigger();
53
- this._transportClosed = new Trigger();
54
- this._state = "CREATED";
55
- this._incomingSignalBuffer = [];
56
- this._outgoingSignalBuffer = [];
57
- this.stateChanged = new Event();
58
- this.errors = new ErrorStream();
59
- this._instanceId = PublicKey.random().toHex();
60
- this.transportStats = new Event();
61
- this._signalSendTask = new DeferredTask(this._ctx, async () => {
62
- await this._flushSignalBuffer();
63
- });
64
- this._signallingDelay = STARTING_SIGNALLING_DELAY;
65
76
  log.trace("dxos.mesh.connection.construct", {
66
77
  sessionId: this.sessionId,
67
78
  topic: this.topic,
@@ -641,12 +652,16 @@ import { ComplexMap } from "@dxos/util";
641
652
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
642
653
  var SwarmMessage = schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
643
654
  var SwarmMessenger = class {
655
+ _ctx = new Context2(void 0, {
656
+ F: __dxlog_file3,
657
+ L: 35
658
+ });
659
+ _sendMessage;
660
+ _onSignal;
661
+ _onOffer;
662
+ _topic;
663
+ _offerRecords = new ComplexMap((key) => key.toHex());
644
664
  constructor({ sendMessage, onSignal, onOffer, topic }) {
645
- this._ctx = new Context2(void 0, {
646
- F: __dxlog_file3,
647
- L: 35
648
- });
649
- this._offerRecords = new ComplexMap((key) => key.toHex());
650
665
  this._sendMessage = sendMessage;
651
666
  this._onSignal = onSignal;
652
667
  this._onOffer = onOffer;
@@ -909,6 +924,32 @@ var ConnectionDisplacedError = class extends SystemError {
909
924
  };
910
925
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
911
926
  var Peer = class {
927
+ remoteInfo;
928
+ topic;
929
+ localInfo;
930
+ _signalMessaging;
931
+ _protocolProvider;
932
+ _transportFactory;
933
+ _connectionLimiter;
934
+ _callbacks;
935
+ /**
936
+ * Will be available to connect after this time.
937
+ */
938
+ _availableAfter = 0;
939
+ availableToConnect = true;
940
+ _lastConnectionTime;
941
+ _ctx = new Context3(void 0, {
942
+ F: __dxlog_file4,
943
+ L: 81
944
+ });
945
+ _connectionCtx;
946
+ connection;
947
+ /**
948
+ * Whether the peer is currently advertizing itself on the signal-network.
949
+ */
950
+ advertizing = false;
951
+ initiating = false;
952
+ connectionDisplaced = new Event2();
912
953
  constructor(remoteInfo, topic, localInfo, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
913
954
  this.remoteInfo = remoteInfo;
914
955
  this.topic = topic;
@@ -918,15 +959,6 @@ var Peer = class {
918
959
  this._transportFactory = _transportFactory;
919
960
  this._connectionLimiter = _connectionLimiter;
920
961
  this._callbacks = _callbacks;
921
- this._availableAfter = 0;
922
- this.availableToConnect = true;
923
- this._ctx = new Context3(void 0, {
924
- F: __dxlog_file4,
925
- L: 80
926
- });
927
- this.advertizing = false;
928
- this.initiating = false;
929
- this.connectionDisplaced = new Event2();
930
962
  }
931
963
  /**
932
964
  * Respond to remote offer.
@@ -940,7 +972,7 @@ var Peer = class {
940
972
  ].includes(this.connection.state)) {
941
973
  log4.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
942
974
  F: __dxlog_file4,
943
- L: 115,
975
+ L: 116,
944
976
  S: this,
945
977
  C: (f, a) => f(...a)
946
978
  });
@@ -957,7 +989,7 @@ var Peer = class {
957
989
  sessionId: this.connection?.sessionId
958
990
  }, {
959
991
  F: __dxlog_file4,
960
- L: 124,
992
+ L: 125,
961
993
  S: this,
962
994
  C: (f, a) => f(...a)
963
995
  });
@@ -974,7 +1006,7 @@ var Peer = class {
974
1006
  if (!this.connection) {
975
1007
  invariant3(message.sessionId, void 0, {
976
1008
  F: __dxlog_file4,
977
- L: 144,
1009
+ L: 145,
978
1010
  S: this,
979
1011
  A: [
980
1012
  "message.sessionId",
@@ -995,7 +1027,7 @@ var Peer = class {
995
1027
  err
996
1028
  }, {
997
1029
  F: __dxlog_file4,
998
- L: 154,
1030
+ L: 155,
999
1031
  S: this,
1000
1032
  C: (f, a) => f(...a)
1001
1033
  });
@@ -1017,7 +1049,7 @@ var Peer = class {
1017
1049
  async initiateConnection() {
1018
1050
  invariant3(!this.initiating, "Initiation in progress.", {
1019
1051
  F: __dxlog_file4,
1020
- L: 171,
1052
+ L: 172,
1021
1053
  S: this,
1022
1054
  A: [
1023
1055
  "!this.initiating",
@@ -1026,7 +1058,7 @@ var Peer = class {
1026
1058
  });
1027
1059
  invariant3(!this.connection, "Already connected.", {
1028
1060
  F: __dxlog_file4,
1029
- L: 172,
1061
+ L: 173,
1030
1062
  S: this,
1031
1063
  A: [
1032
1064
  "!this.connection",
@@ -1041,7 +1073,7 @@ var Peer = class {
1041
1073
  sessionId
1042
1074
  }, {
1043
1075
  F: __dxlog_file4,
1044
- L: 174,
1076
+ L: 175,
1045
1077
  S: this,
1046
1078
  C: (f, a) => f(...a)
1047
1079
  });
@@ -1067,14 +1099,14 @@ var Peer = class {
1067
1099
  remote: this.remoteInfo
1068
1100
  }, {
1069
1101
  F: __dxlog_file4,
1070
- L: 191,
1102
+ L: 192,
1071
1103
  S: this,
1072
1104
  C: (f, a) => f(...a)
1073
1105
  });
1074
1106
  if (connection.state !== ConnectionState.INITIAL) {
1075
1107
  log4("ignoring response", void 0, {
1076
1108
  F: __dxlog_file4,
1077
- L: 193,
1109
+ L: 194,
1078
1110
  S: this,
1079
1111
  C: (f, a) => f(...a)
1080
1112
  });
@@ -1088,7 +1120,7 @@ var Peer = class {
1088
1120
  remote: this.remoteInfo
1089
1121
  }, {
1090
1122
  F: __dxlog_file4,
1091
- L: 197,
1123
+ L: 198,
1092
1124
  S: this,
1093
1125
  C: (f, a) => f(...a)
1094
1126
  });
@@ -1110,7 +1142,7 @@ var Peer = class {
1110
1142
  remote: this.remoteInfo
1111
1143
  }, {
1112
1144
  F: __dxlog_file4,
1113
- L: 210,
1145
+ L: 211,
1114
1146
  S: this,
1115
1147
  C: (f, a) => f(...a)
1116
1148
  });
@@ -1122,7 +1154,7 @@ var Peer = class {
1122
1154
  try {
1123
1155
  log4("opening connection as initiator", void 0, {
1124
1156
  F: __dxlog_file4,
1125
- L: 223,
1157
+ L: 224,
1126
1158
  S: this,
1127
1159
  C: (f, a) => f(...a)
1128
1160
  });
@@ -1136,7 +1168,7 @@ var Peer = class {
1136
1168
  remote: this.remoteInfo
1137
1169
  }, {
1138
1170
  F: __dxlog_file4,
1139
- L: 227,
1171
+ L: 228,
1140
1172
  S: this,
1141
1173
  C: (f, a) => f(...a)
1142
1174
  });
@@ -1144,7 +1176,7 @@ var Peer = class {
1144
1176
  err
1145
1177
  }, {
1146
1178
  F: __dxlog_file4,
1147
- L: 234,
1179
+ L: 235,
1148
1180
  S: this,
1149
1181
  C: (f, a) => f(...a)
1150
1182
  });
@@ -1167,13 +1199,13 @@ var Peer = class {
1167
1199
  sessionId
1168
1200
  }, {
1169
1201
  F: __dxlog_file4,
1170
- L: 248,
1202
+ L: 249,
1171
1203
  S: this,
1172
1204
  C: (f, a) => f(...a)
1173
1205
  });
1174
1206
  invariant3(!this.connection, "Already connected.", {
1175
1207
  F: __dxlog_file4,
1176
- L: 255,
1208
+ L: 256,
1177
1209
  S: this,
1178
1210
  A: [
1179
1211
  "!this.connection",
@@ -1209,7 +1241,7 @@ var Peer = class {
1209
1241
  initiator
1210
1242
  }, {
1211
1243
  F: __dxlog_file4,
1212
- L: 279,
1244
+ L: 280,
1213
1245
  S: this,
1214
1246
  C: (f, a) => f(...a)
1215
1247
  });
@@ -1223,14 +1255,14 @@ var Peer = class {
1223
1255
  };
1224
1256
  log4("connection closed", logMeta, {
1225
1257
  F: __dxlog_file4,
1226
- L: 289,
1258
+ L: 290,
1227
1259
  S: this,
1228
1260
  C: (f, a) => f(...a)
1229
1261
  });
1230
1262
  this._connectionLimiter.doneConnecting(sessionId);
1231
1263
  invariant3(this.connection === connection, "Connection mismatch (race condition).", {
1232
1264
  F: __dxlog_file4,
1233
- L: 294,
1265
+ L: 295,
1234
1266
  S: this,
1235
1267
  A: [
1236
1268
  "this.connection === connection",
@@ -1245,7 +1277,7 @@ var Peer = class {
1245
1277
  initiator
1246
1278
  }, {
1247
1279
  F: __dxlog_file4,
1248
- L: 296,
1280
+ L: 297,
1249
1281
  S: this,
1250
1282
  C: (f, a) => f(...a)
1251
1283
  });
@@ -1262,7 +1294,7 @@ var Peer = class {
1262
1294
  scheduleTask2(this._connectionCtx, () => {
1263
1295
  log4("peer became available", logMeta, {
1264
1296
  F: __dxlog_file4,
1265
- L: 320,
1297
+ L: 321,
1266
1298
  S: this,
1267
1299
  C: (f, a) => f(...a)
1268
1300
  });
@@ -1286,7 +1318,7 @@ var Peer = class {
1286
1318
  err
1287
1319
  }, {
1288
1320
  F: __dxlog_file4,
1289
- L: 338,
1321
+ L: 339,
1290
1322
  S: this,
1291
1323
  C: (f, a) => f(...a)
1292
1324
  });
@@ -1299,7 +1331,7 @@ var Peer = class {
1299
1331
  err
1300
1332
  }, {
1301
1333
  F: __dxlog_file4,
1302
- L: 345,
1334
+ L: 346,
1303
1335
  S: this,
1304
1336
  C: (f, a) => f(...a)
1305
1337
  });
@@ -1318,7 +1350,7 @@ var Peer = class {
1318
1350
  sessionId: connection.sessionId
1319
1351
  }, {
1320
1352
  F: __dxlog_file4,
1321
- L: 370,
1353
+ L: 371,
1322
1354
  S: this,
1323
1355
  C: (f, a) => f(...a)
1324
1356
  });
@@ -1330,7 +1362,7 @@ var Peer = class {
1330
1362
  sessionId: connection.sessionId
1331
1363
  }, {
1332
1364
  F: __dxlog_file4,
1333
- L: 376,
1365
+ L: 377,
1334
1366
  S: this,
1335
1367
  C: (f, a) => f(...a)
1336
1368
  });
@@ -1341,7 +1373,7 @@ var Peer = class {
1341
1373
  message
1342
1374
  }, {
1343
1375
  F: __dxlog_file4,
1344
- L: 381,
1376
+ L: 382,
1345
1377
  S: this,
1346
1378
  C: (f, a) => f(...a)
1347
1379
  });
@@ -1356,7 +1388,7 @@ var Peer = class {
1356
1388
  topic: this.topic
1357
1389
  }, {
1358
1390
  F: __dxlog_file4,
1359
- L: 391,
1391
+ L: 392,
1360
1392
  S: this,
1361
1393
  C: (f, a) => f(...a)
1362
1394
  });
@@ -1392,6 +1424,46 @@ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm
1392
1424
  var INITIATION_DELAY = 100;
1393
1425
  var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
1394
1426
  var Swarm = class {
1427
+ _topic;
1428
+ _ownPeer;
1429
+ _topology;
1430
+ _protocolProvider;
1431
+ _messenger;
1432
+ _transportFactory;
1433
+ _label;
1434
+ _connectionLimiter;
1435
+ _initiationDelay;
1436
+ _swarmMessenger;
1437
+ _ctx = new Context4(void 0, {
1438
+ F: __dxlog_file5,
1439
+ L: 39
1440
+ });
1441
+ _listeningHandle = void 0;
1442
+ /**
1443
+ * PeerInfo -> Peer.
1444
+ * @internal
1445
+ */
1446
+ _peers = new ComplexMap2(PeerInfoHash);
1447
+ /**
1448
+ * Unique id of the swarm, local to the current peer, generated when swarm is joined.
1449
+ */
1450
+ _instanceId = PublicKey4.random().toHex();
1451
+ /**
1452
+ * New connection to a peer is started.
1453
+ * @internal
1454
+ */
1455
+ connectionAdded = new Event3();
1456
+ /**
1457
+ * Connection to a peer is dropped.
1458
+ * @internal
1459
+ */
1460
+ disconnected = new Event3();
1461
+ /**
1462
+ * Connection is established to a new peer.
1463
+ * @internal
1464
+ */
1465
+ connected = new Event3();
1466
+ errors = new ErrorStream2();
1395
1467
  // TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
1396
1468
  // TODO(burdon): Pass in object.
1397
1469
  constructor(_topic, _ownPeer, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter, _initiationDelay = INITIATION_DELAY) {
@@ -1404,17 +1476,6 @@ var Swarm = class {
1404
1476
  this._label = _label;
1405
1477
  this._connectionLimiter = _connectionLimiter;
1406
1478
  this._initiationDelay = _initiationDelay;
1407
- this._ctx = new Context4(void 0, {
1408
- F: __dxlog_file5,
1409
- L: 38
1410
- });
1411
- this._listeningHandle = void 0;
1412
- this._peers = new ComplexMap2(PeerInfoHash);
1413
- this._instanceId = PublicKey4.random().toHex();
1414
- this.connectionAdded = new Event3();
1415
- this.disconnected = new Event3();
1416
- this.connected = new Event3();
1417
- this.errors = new ErrorStream2();
1418
1479
  log5.trace("dxos.mesh.swarm.constructor", trace2.begin({
1419
1480
  id: this._instanceId,
1420
1481
  data: {
@@ -1423,7 +1484,7 @@ var Swarm = class {
1423
1484
  }
1424
1485
  }), {
1425
1486
  F: __dxlog_file5,
1426
- L: 88,
1487
+ L: 89,
1427
1488
  S: this,
1428
1489
  C: (f, a) => f(...a)
1429
1490
  });
@@ -1431,7 +1492,7 @@ var Swarm = class {
1431
1492
  peerId: _ownPeer
1432
1493
  }, {
1433
1494
  F: __dxlog_file5,
1434
- L: 92,
1495
+ L: 93,
1435
1496
  S: this,
1436
1497
  C: (f, a) => f(...a)
1437
1498
  });
@@ -1446,7 +1507,7 @@ var Swarm = class {
1446
1507
  id: this._instanceId
1447
1508
  }), {
1448
1509
  F: __dxlog_file5,
1449
- L: 101,
1510
+ L: 102,
1450
1511
  S: this,
1451
1512
  C: (f, a) => f(...a)
1452
1513
  });
@@ -1472,7 +1533,7 @@ var Swarm = class {
1472
1533
  async open() {
1473
1534
  invariant4(!this._listeningHandle, void 0, {
1474
1535
  F: __dxlog_file5,
1475
- L: 132,
1536
+ L: 133,
1476
1537
  S: this,
1477
1538
  A: [
1478
1539
  "!this._listeningHandle",
@@ -1487,7 +1548,7 @@ var Swarm = class {
1487
1548
  err
1488
1549
  }, {
1489
1550
  F: __dxlog_file5,
1490
- L: 140,
1551
+ L: 141,
1491
1552
  S: this,
1492
1553
  C: (f, a) => f(...a)
1493
1554
  }));
@@ -1497,7 +1558,7 @@ var Swarm = class {
1497
1558
  async destroy() {
1498
1559
  log5("destroying...", void 0, {
1499
1560
  F: __dxlog_file5,
1500
- L: 146,
1561
+ L: 147,
1501
1562
  S: this,
1502
1563
  C: (f, a) => f(...a)
1503
1564
  });
@@ -1508,7 +1569,7 @@ var Swarm = class {
1508
1569
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key, "swarm destroyed")));
1509
1570
  log5("destroyed", void 0, {
1510
1571
  F: __dxlog_file5,
1511
- L: 153,
1572
+ L: 154,
1512
1573
  S: this,
1513
1574
  C: (f, a) => f(...a)
1514
1575
  });
@@ -1516,7 +1577,7 @@ var Swarm = class {
1516
1577
  async setTopology(topology) {
1517
1578
  invariant4(!this._ctx.disposed, "Swarm is offline", {
1518
1579
  F: __dxlog_file5,
1519
- L: 157,
1580
+ L: 158,
1520
1581
  S: this,
1521
1582
  A: [
1522
1583
  "!this._ctx.disposed",
@@ -1531,7 +1592,7 @@ var Swarm = class {
1531
1592
  topology: getClassName(topology)
1532
1593
  }, {
1533
1594
  F: __dxlog_file5,
1534
- L: 161,
1595
+ L: 162,
1535
1596
  S: this,
1536
1597
  C: (f, a) => f(...a)
1537
1598
  });
@@ -1545,14 +1606,14 @@ var Swarm = class {
1545
1606
  swarmEvent
1546
1607
  }, {
1547
1608
  F: __dxlog_file5,
1548
- L: 174,
1609
+ L: 175,
1549
1610
  S: this,
1550
1611
  C: (f, a) => f(...a)
1551
1612
  });
1552
1613
  if (this._ctx.disposed) {
1553
1614
  log5("swarm event ignored for disposed swarm", void 0, {
1554
1615
  F: __dxlog_file5,
1555
- L: 177,
1616
+ L: 178,
1556
1617
  S: this,
1557
1618
  C: (f, a) => f(...a)
1558
1619
  });
@@ -1565,7 +1626,7 @@ var Swarm = class {
1565
1626
  peerId
1566
1627
  }, {
1567
1628
  F: __dxlog_file5,
1568
- L: 184,
1629
+ L: 185,
1569
1630
  S: this,
1570
1631
  C: (f, a) => f(...a)
1571
1632
  });
@@ -1579,13 +1640,13 @@ var Swarm = class {
1579
1640
  if (this._isConnectionEstablishmentInProgress(peer)) {
1580
1641
  log5(`destroying peer, state: ${peer.connection?.state}`, void 0, {
1581
1642
  F: __dxlog_file5,
1582
- L: 195,
1643
+ L: 196,
1583
1644
  S: this,
1584
1645
  C: (f, a) => f(...a)
1585
1646
  });
1586
1647
  void this._destroyPeer(swarmEvent.peerLeft.peer, "peer left").catch((err) => log5.catch(err, void 0, {
1587
1648
  F: __dxlog_file5,
1588
- L: 196,
1649
+ L: 197,
1589
1650
  S: this,
1590
1651
  C: (f, a) => f(...a)
1591
1652
  }));
@@ -1595,7 +1656,7 @@ var Swarm = class {
1595
1656
  peer: swarmEvent.peerLeft.peer.peerKey
1596
1657
  }, {
1597
1658
  F: __dxlog_file5,
1598
- L: 199,
1659
+ L: 200,
1599
1660
  S: this,
1600
1661
  C: (f, a) => f(...a)
1601
1662
  });
@@ -1608,14 +1669,14 @@ var Swarm = class {
1608
1669
  message
1609
1670
  }, {
1610
1671
  F: __dxlog_file5,
1611
- L: 208,
1672
+ L: 209,
1612
1673
  S: this,
1613
1674
  C: (f, a) => f(...a)
1614
1675
  });
1615
1676
  if (this._ctx.disposed) {
1616
1677
  log5("ignored for disposed swarm", void 0, {
1617
1678
  F: __dxlog_file5,
1618
- L: 210,
1679
+ L: 211,
1619
1680
  S: this,
1620
1681
  C: (f, a) => f(...a)
1621
1682
  });
@@ -1625,7 +1686,7 @@ var Swarm = class {
1625
1686
  }
1626
1687
  invariant4(message.author, void 0, {
1627
1688
  F: __dxlog_file5,
1628
- L: 215,
1689
+ L: 216,
1629
1690
  S: this,
1630
1691
  A: [
1631
1692
  "message.author",
@@ -1637,7 +1698,7 @@ var Swarm = class {
1637
1698
  message
1638
1699
  }, {
1639
1700
  F: __dxlog_file5,
1640
- L: 217,
1701
+ L: 218,
1641
1702
  S: this,
1642
1703
  C: (f, a) => f(...a)
1643
1704
  });
@@ -1650,7 +1711,7 @@ var Swarm = class {
1650
1711
  message
1651
1712
  }, {
1652
1713
  F: __dxlog_file5,
1653
- L: 221,
1714
+ L: 222,
1654
1715
  S: this,
1655
1716
  C: (f, a) => f(...a)
1656
1717
  });
@@ -1678,14 +1739,14 @@ var Swarm = class {
1678
1739
  message
1679
1740
  }, {
1680
1741
  F: __dxlog_file5,
1681
- L: 246,
1742
+ L: 247,
1682
1743
  S: this,
1683
1744
  C: (f, a) => f(...a)
1684
1745
  });
1685
1746
  if (this._ctx.disposed) {
1686
1747
  log5.info("ignored for offline swarm", void 0, {
1687
1748
  F: __dxlog_file5,
1688
- L: 248,
1749
+ L: 249,
1689
1750
  S: this,
1690
1751
  C: (f, a) => f(...a)
1691
1752
  });
@@ -1693,7 +1754,7 @@ var Swarm = class {
1693
1754
  }
1694
1755
  invariant4(message.recipient.peerKey === this._ownPeer.peerKey, `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1695
1756
  F: __dxlog_file5,
1696
- L: 251,
1757
+ L: 252,
1697
1758
  S: this,
1698
1759
  A: [
1699
1760
  "message.recipient.peerKey === this._ownPeer.peerKey",
@@ -1702,7 +1763,7 @@ var Swarm = class {
1702
1763
  });
1703
1764
  invariant4(message.topic?.equals(this._topic), void 0, {
1704
1765
  F: __dxlog_file5,
1705
- L: 255,
1766
+ L: 256,
1706
1767
  S: this,
1707
1768
  A: [
1708
1769
  "message.topic?.equals(this._topic)",
@@ -1711,7 +1772,7 @@ var Swarm = class {
1711
1772
  });
1712
1773
  invariant4(message.author, void 0, {
1713
1774
  F: __dxlog_file5,
1714
- L: 256,
1775
+ L: 257,
1715
1776
  S: this,
1716
1777
  A: [
1717
1778
  "message.author",
@@ -1732,13 +1793,13 @@ var Swarm = class {
1732
1793
  async goOnline() {
1733
1794
  this._ctx = new Context4(void 0, {
1734
1795
  F: __dxlog_file5,
1735
- L: 272
1796
+ L: 273
1736
1797
  });
1737
1798
  }
1738
1799
  _getOrCreatePeer(peerInfo) {
1739
1800
  invariant4(peerInfo.peerKey, "PeerInfo.peerKey is required", {
1740
1801
  F: __dxlog_file5,
1741
- L: 276,
1802
+ L: 277,
1742
1803
  S: this,
1743
1804
  A: [
1744
1805
  "peerInfo.peerKey",
@@ -1758,7 +1819,7 @@ var Swarm = class {
1758
1819
  if (this._isUnregistered(peer)) {
1759
1820
  log5.verbose("ignored onDisconnected for unregistered peer", void 0, {
1760
1821
  F: __dxlog_file5,
1761
- L: 296,
1822
+ L: 297,
1762
1823
  S: this,
1763
1824
  C: (f, a) => f(...a)
1764
1825
  });
@@ -1776,7 +1837,7 @@ var Swarm = class {
1776
1837
  peerInfo
1777
1838
  }, {
1778
1839
  F: __dxlog_file5,
1779
- L: 310,
1840
+ L: 311,
1780
1841
  S: this,
1781
1842
  C: (f, a) => f(...a)
1782
1843
  });
@@ -1803,14 +1864,14 @@ var Swarm = class {
1803
1864
  reason
1804
1865
  }, {
1805
1866
  F: __dxlog_file5,
1806
- L: 332,
1867
+ L: 333,
1807
1868
  S: this,
1808
1869
  C: (f, a) => f(...a)
1809
1870
  });
1810
1871
  const peer = this._peers.get(peerInfo);
1811
1872
  invariant4(peer, void 0, {
1812
1873
  F: __dxlog_file5,
1813
- L: 334,
1874
+ L: 335,
1814
1875
  S: this,
1815
1876
  A: [
1816
1877
  "peer",
@@ -1840,7 +1901,7 @@ var Swarm = class {
1840
1901
  } catch (err) {
1841
1902
  log5("initiation error", err, {
1842
1903
  F: __dxlog_file5,
1843
- L: 361,
1904
+ L: 362,
1844
1905
  S: this,
1845
1906
  C: (f, a) => f(...a)
1846
1907
  });
@@ -1871,7 +1932,7 @@ var Swarm = class {
1871
1932
  remotePeer
1872
1933
  }, {
1873
1934
  F: __dxlog_file5,
1874
- L: 389,
1935
+ L: 390,
1875
1936
  S: this,
1876
1937
  C: (f, a) => f(...a)
1877
1938
  });
@@ -1890,7 +1951,7 @@ var Swarm = class {
1890
1951
  remotePeer
1891
1952
  }, {
1892
1953
  F: __dxlog_file5,
1893
- L: 405,
1954
+ L: 406,
1894
1955
  S: this,
1895
1956
  C: (f, a) => f(...a)
1896
1957
  });
@@ -1900,7 +1961,7 @@ var Swarm = class {
1900
1961
  remotePeer
1901
1962
  }, {
1902
1963
  F: __dxlog_file5,
1903
- L: 408,
1964
+ L: 409,
1904
1965
  S: this,
1905
1966
  C: (f, a) => f(...a)
1906
1967
  });
@@ -1956,15 +2017,16 @@ import { PeerInfoHash as PeerInfoHash2 } from "@dxos/messaging";
1956
2017
  import { ComplexMap as ComplexMap3 } from "@dxos/util";
1957
2018
  var __dxlog_file6 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
1958
2019
  var SwarmMapper = class {
2020
+ _swarm;
2021
+ _subscriptions = new SubscriptionList();
2022
+ _connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
2023
+ _peers = new ComplexMap3(PeerInfoHash2);
2024
+ mapUpdated = new Event4();
1959
2025
  get peers() {
1960
2026
  return Array.from(this._peers.values());
1961
2027
  }
1962
2028
  constructor(_swarm) {
1963
2029
  this._swarm = _swarm;
1964
- this._subscriptions = new SubscriptionList();
1965
- this._connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
1966
- this._peers = new ComplexMap3(PeerInfoHash2);
1967
- this.mapUpdated = new Event4();
1968
2030
  this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
1969
2031
  this._update();
1970
2032
  this._connectionSubscriptions.set(connection.remoteInfo, connection.stateChanged.on(() => {
@@ -2030,20 +2092,21 @@ import { ComplexMap as ComplexMap4 } from "@dxos/util";
2030
2092
  var __dxlog_file7 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2031
2093
  var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
2032
2094
  var ConnectionLimiter = class {
2033
- constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2034
- this._ctx = new Context5(void 0, {
2035
- F: __dxlog_file7,
2036
- L: 23
2037
- });
2038
- /**
2039
- * Queue of promises to resolve when initiating connections amount is below the limit.
2040
- */
2041
- this._waitingPromises = new ComplexMap4(PublicKey6.hash);
2042
- this.resolveWaitingPromises = new DeferredTask2(this._ctx, async () => {
2043
- Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
2044
- resolve();
2045
- });
2095
+ _ctx = new Context5(void 0, {
2096
+ F: __dxlog_file7,
2097
+ L: 23
2098
+ });
2099
+ _maxConcurrentInitConnections;
2100
+ /**
2101
+ * Queue of promises to resolve when initiating connections amount is below the limit.
2102
+ */
2103
+ _waitingPromises = new ComplexMap4(PublicKey6.hash);
2104
+ resolveWaitingPromises = new DeferredTask2(this._ctx, async () => {
2105
+ Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
2106
+ resolve();
2046
2107
  });
2108
+ });
2109
+ constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2047
2110
  this._maxConcurrentInitConnections = maxConcurrentInitConnections;
2048
2111
  }
2049
2112
  /**
@@ -2110,22 +2173,20 @@ import { raise } from "@dxos/debug";
2110
2173
  import { PublicKey as PublicKey7 } from "@dxos/keys";
2111
2174
  import { ComplexMap as ComplexMap5 } from "@dxos/util";
2112
2175
  var CONNECTION_GC_THRESHOLD = 1e3 * 60 * 15;
2113
- var EventType = /* @__PURE__ */ function(EventType2) {
2176
+ var EventType = /* @__PURE__ */ (function(EventType2) {
2114
2177
  EventType2["CONNECTION_STATE_CHANGED"] = "CONNECTION_STATE_CHANGED";
2115
2178
  EventType2["PROTOCOL_ERROR"] = "PROTOCOL_ERROR";
2116
2179
  EventType2["PROTOCOL_EXTENSIONS_INITIALIZED"] = "PROTOCOL_EXTENSIONS_INITIALIZED";
2117
2180
  EventType2["PROTOCOL_EXTENSIONS_HANDSHAKE"] = "PROTOCOL_EXTENSIONS_HANDSHAKE";
2118
2181
  EventType2["PROTOCOL_HANDSHAKE"] = "PROTOCOL_HANDSHAKE";
2119
2182
  return EventType2;
2120
- }({});
2183
+ })({});
2121
2184
  var ConnectionLog = class {
2122
- constructor() {
2123
- /**
2124
- * SwarmId => info
2125
- */
2126
- this._swarms = new ComplexMap5(PublicKey7.hash);
2127
- this.update = new Event5();
2128
- }
2185
+ /**
2186
+ * SwarmId => info
2187
+ */
2188
+ _swarms = new ComplexMap5(PublicKey7.hash);
2189
+ update = new Event5();
2129
2190
  getSwarmInfo(swarmId) {
2130
2191
  return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
2131
2192
  }
@@ -2213,17 +2274,23 @@ function _ts_decorate4(decorators, target, key, desc) {
2213
2274
  }
2214
2275
  var __dxlog_file8 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2215
2276
  var SwarmNetworkManager = class {
2277
+ /**
2278
+ * @internal
2279
+ */
2280
+ _swarms = new ComplexMap6(PublicKey8.hash);
2281
+ _mappers = new ComplexMap6(PublicKey8.hash);
2282
+ _transportFactory;
2283
+ _signalManager;
2284
+ _messenger;
2285
+ _signalConnection;
2286
+ _connectionLimiter;
2287
+ _connectionLog;
2288
+ _instanceId = PublicKey8.random().toHex();
2289
+ _peerInfo = void 0;
2290
+ _connectionState = ConnectionState2.ONLINE;
2291
+ connectionStateChanged = new Event6();
2292
+ topicsUpdated = new Event6();
2216
2293
  constructor({ transportFactory, signalManager, enableDevtoolsLogging, peerInfo }) {
2217
- /**
2218
- * @internal
2219
- */
2220
- this._swarms = new ComplexMap6(PublicKey8.hash);
2221
- this._mappers = new ComplexMap6(PublicKey8.hash);
2222
- this._instanceId = PublicKey8.random().toHex();
2223
- this._peerInfo = void 0;
2224
- this._connectionState = ConnectionState2.ONLINE;
2225
- this.connectionStateChanged = new Event6();
2226
- this.topicsUpdated = new Event6();
2227
2294
  this._transportFactory = transportFactory;
2228
2295
  this._signalManager = signalManager;
2229
2296
  this._signalManager.swarmEvent.on((event) => this._swarms.get(event.topic)?.onSwarmEvent(event));
@@ -2460,6 +2527,7 @@ _ts_decorate4([
2460
2527
  import { invariant as invariant7 } from "@dxos/invariant";
2461
2528
  var __dxlog_file9 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2462
2529
  var FullyConnectedTopology = class {
2530
+ _controller;
2463
2531
  toString() {
2464
2532
  return "FullyConnectedTopology";
2465
2533
  }
@@ -2504,9 +2572,13 @@ var __dxlog_file10 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topo
2504
2572
  var MIN_UPDATE_INTERVAL = 1e3 * 10;
2505
2573
  var MAX_CHANGES_PER_UPDATE = 1;
2506
2574
  var MMSTTopology = class {
2575
+ _originateConnections;
2576
+ _maxPeers;
2577
+ _sampleSize;
2578
+ _controller;
2579
+ _sampleCollected = false;
2580
+ _lastAction = /* @__PURE__ */ new Date(0);
2507
2581
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
2508
- this._sampleCollected = false;
2509
- this._lastAction = /* @__PURE__ */ new Date(0);
2510
2582
  this._originateConnections = originateConnections;
2511
2583
  this._maxPeers = maxPeers;
2512
2584
  this._sampleSize = sampleSize;
@@ -2708,6 +2780,8 @@ import { invariant as invariant9 } from "@dxos/invariant";
2708
2780
  import { log as log10 } from "@dxos/log";
2709
2781
  var __dxlog_file11 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2710
2782
  var StarTopology = class {
2783
+ _centralPeer;
2784
+ _controller;
2711
2785
  constructor(_centralPeer) {
2712
2786
  this._centralPeer = _centralPeer;
2713
2787
  }
@@ -2827,20 +2901,21 @@ var MemoryTransportFactory = {
2827
2901
  createTransport: (options) => new MemoryTransport(options)
2828
2902
  };
2829
2903
  var MemoryTransport = class _MemoryTransport {
2830
- static {
2831
- // TODO(burdon): Remove static properties (inject context into constructor).
2832
- this._connections = new ComplexMap7(PublicKey9.hash);
2833
- }
2904
+ _options;
2905
+ // TODO(burdon): Remove static properties (inject context into constructor).
2906
+ static _connections = new ComplexMap7(PublicKey9.hash);
2907
+ _instanceId = PublicKey9.random();
2908
+ _remote = new Trigger2();
2909
+ _outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2910
+ _incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2911
+ _closed = false;
2912
+ _remoteInstanceId;
2913
+ _remoteConnection;
2914
+ closed = new Event7();
2915
+ connected = new Event7();
2916
+ errors = new ErrorStream3();
2834
2917
  constructor(_options) {
2835
2918
  this._options = _options;
2836
- this._instanceId = PublicKey9.random();
2837
- this._remote = new Trigger2();
2838
- this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2839
- this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2840
- this._closed = false;
2841
- this.closed = new Event7();
2842
- this.connected = new Event7();
2843
- this.errors = new ErrorStream3();
2844
2919
  invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2845
2920
  F: __dxlog_file12,
2846
2921
  L: 64,
@@ -2992,13 +3067,13 @@ _ts_decorate5([
2992
3067
  var toError = (err) => err instanceof Error ? err : new Error(String(err));
2993
3068
 
2994
3069
  // src/transport/transport.ts
2995
- var TransportKind = /* @__PURE__ */ function(TransportKind2) {
3070
+ var TransportKind = /* @__PURE__ */ (function(TransportKind2) {
2996
3071
  TransportKind2["WEB_RTC"] = "WEB-RTC";
2997
3072
  TransportKind2["WEB_RTC_PROXY"] = "WEB-RTC_PROXY";
2998
3073
  TransportKind2["MEMORY"] = "MEMORY";
2999
3074
  TransportKind2["TCP"] = "TCP";
3000
3075
  return TransportKind2;
3001
- }({});
3076
+ })({});
3002
3077
 
3003
3078
  // src/transport/webrtc/rtc-connection-factory.ts
3004
3079
  import { Mutex } from "@dxos/async";
@@ -3014,14 +3089,10 @@ var BrowserRtcConnectionFactory = class {
3014
3089
  }
3015
3090
  };
3016
3091
  var NodeRtcConnectionFactory = class _NodeRtcConnectionFactory {
3017
- static {
3018
- this._createdConnections = 0;
3019
- }
3020
- static {
3021
- this._cleanupMutex = new Mutex();
3022
- }
3092
+ static _createdConnections = 0;
3093
+ static _cleanupMutex = new Mutex();
3023
3094
  // This should be inside the function to avoid triggering `eval` in the global scope.
3024
- // eslint-disable-next-line no-new-func
3095
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
3025
3096
  // TODO(burdon): Do imports here?
3026
3097
  async initialize() {
3027
3098
  }
@@ -3050,7 +3121,7 @@ var getRtcConnectionFactory = () => {
3050
3121
  };
3051
3122
 
3052
3123
  // src/transport/webrtc/rtc-peer-connection.ts
3053
- import { synchronized as synchronized5, Trigger as Trigger3, Mutex as Mutex2 } from "@dxos/async";
3124
+ import { Mutex as Mutex2, Trigger as Trigger3, synchronized as synchronized5 } from "@dxos/async";
3054
3125
  import { invariant as invariant12 } from "@dxos/invariant";
3055
3126
  import { log as log13, logInfo as logInfo4 } from "@dxos/log";
3056
3127
  import { ConnectivityError as ConnectivityError3 } from "@dxos/protocols";
@@ -3117,8 +3188,17 @@ var __dxlog_file13 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/tran
3117
3188
  var MAX_MESSAGE_SIZE = 64 * 1024;
3118
3189
  var MAX_BUFFERED_AMOUNT = 64 * 1024;
3119
3190
  var RtcTransportChannel = class extends Resource {
3191
+ _connection;
3192
+ _options;
3193
+ closed = new AsyncEvent();
3194
+ connected = new AsyncEvent();
3195
+ errors = new ErrorStream4();
3196
+ _channel;
3197
+ _stream;
3198
+ _streamDataFlushedCallback = null;
3199
+ _isChannelCreationInProgress = false;
3120
3200
  constructor(_connection, _options) {
3121
- super(), this._connection = _connection, this._options = _options, this.closed = new AsyncEvent(), this.connected = new AsyncEvent(), this.errors = new ErrorStream4(), this._streamDataFlushedCallback = null, this._isChannelCreationInProgress = false;
3201
+ super(), this._connection = _connection, this._options = _options;
3122
3202
  }
3123
3203
  get isRtcChannelCreationInProgress() {
3124
3204
  return this._isChannelCreationInProgress;
@@ -3131,7 +3211,7 @@ var RtcTransportChannel = class extends Resource {
3131
3211
  async _open() {
3132
3212
  invariant11(!this._isChannelCreationInProgress, void 0, {
3133
3213
  F: __dxlog_file13,
3134
- L: 56,
3214
+ L: 57,
3135
3215
  S: this,
3136
3216
  A: [
3137
3217
  "!this._isChannelCreationInProgress",
@@ -3155,7 +3235,7 @@ var RtcTransportChannel = class extends Resource {
3155
3235
  err
3156
3236
  }, {
3157
3237
  F: __dxlog_file13,
3158
- L: 76,
3238
+ L: 77,
3159
3239
  S: this,
3160
3240
  C: (f, a) => f(...a)
3161
3241
  });
@@ -3173,7 +3253,7 @@ var RtcTransportChannel = class extends Resource {
3173
3253
  this.closed.emit();
3174
3254
  log12("closed", void 0, {
3175
3255
  F: __dxlog_file13,
3176
- L: 92,
3256
+ L: 93,
3177
3257
  S: this,
3178
3258
  C: (f, a) => f(...a)
3179
3259
  });
@@ -3186,7 +3266,7 @@ var RtcTransportChannel = class extends Resource {
3186
3266
  topic: this._options.topic
3187
3267
  }, {
3188
3268
  F: __dxlog_file13,
3189
- L: 99,
3269
+ L: 100,
3190
3270
  S: this,
3191
3271
  C: (f, a) => f(...a)
3192
3272
  });
@@ -3195,7 +3275,7 @@ var RtcTransportChannel = class extends Resource {
3195
3275
  }
3196
3276
  log12("onopen", void 0, {
3197
3277
  F: __dxlog_file13,
3198
- L: 104,
3278
+ L: 105,
3199
3279
  S: this,
3200
3280
  C: (f, a) => f(...a)
3201
3281
  });
@@ -3213,7 +3293,7 @@ var RtcTransportChannel = class extends Resource {
3213
3293
  onclose: async () => {
3214
3294
  log12("onclose", void 0, {
3215
3295
  F: __dxlog_file13,
3216
- L: 117,
3296
+ L: 118,
3217
3297
  S: this,
3218
3298
  C: (f, a) => f(...a)
3219
3299
  });
@@ -3223,7 +3303,7 @@ var RtcTransportChannel = class extends Resource {
3223
3303
  if (!this._stream) {
3224
3304
  log12.warn("ignoring message on a closed channel", void 0, {
3225
3305
  F: __dxlog_file13,
3226
- L: 123,
3306
+ L: 124,
3227
3307
  S: this,
3228
3308
  C: (f, a) => f(...a)
3229
3309
  });
@@ -3254,7 +3334,7 @@ var RtcTransportChannel = class extends Resource {
3254
3334
  if (!this._channel) {
3255
3335
  log12.warn("writing to a channel after a connection was closed", void 0, {
3256
3336
  F: __dxlog_file13,
3257
- L: 153,
3337
+ L: 154,
3258
3338
  S: this,
3259
3339
  C: (f, a) => f(...a)
3260
3340
  });
@@ -3277,7 +3357,7 @@ var RtcTransportChannel = class extends Resource {
3277
3357
  if (this._streamDataFlushedCallback !== null) {
3278
3358
  log12.error("consumer trying to write before we are ready for more data", void 0, {
3279
3359
  F: __dxlog_file13,
3280
- L: 174,
3360
+ L: 175,
3281
3361
  S: this,
3282
3362
  C: (f, a) => f(...a)
3283
3363
  });
@@ -3293,7 +3373,7 @@ var RtcTransportChannel = class extends Resource {
3293
3373
  } catch (error) {
3294
3374
  log12.catch(error, void 0, {
3295
3375
  F: __dxlog_file13,
3296
- L: 186,
3376
+ L: 187,
3297
3377
  S: this,
3298
3378
  C: (f, a) => f(...a)
3299
3379
  });
@@ -3345,14 +3425,27 @@ function _ts_decorate6(decorators, target, key, desc) {
3345
3425
  }
3346
3426
  var __dxlog_file14 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-peer-connection.ts";
3347
3427
  var RtcPeerConnection = class {
3428
+ _factory;
3429
+ _options;
3430
+ // A peer who is not the initiator waits for another party to open a channel.
3431
+ _channelCreatedCallbacks = /* @__PURE__ */ new Map();
3432
+ // Channels indexed by topic.
3433
+ _transportChannels = /* @__PURE__ */ new Map();
3434
+ _dataChannels = /* @__PURE__ */ new Map();
3435
+ // A peer is ready to receive ICE candidates when local and remote description were set.
3436
+ _readyForCandidates = new Trigger3();
3437
+ _offerProcessingMutex = new Mutex2();
3438
+ /**
3439
+ * Can't use peer.connection.initiator, because if two connections to the same peer are created in
3440
+ * different swarms, we might be the initiator of the first one, but not of the other one.
3441
+ * Use a stable peer keypair property (key ordering) to decide who's acting as the initiator of
3442
+ * transport connection establishment and data channel creation.
3443
+ */
3444
+ _initiator;
3445
+ _connection;
3348
3446
  constructor(_factory, _options) {
3349
3447
  this._factory = _factory;
3350
3448
  this._options = _options;
3351
- this._channelCreatedCallbacks = /* @__PURE__ */ new Map();
3352
- this._transportChannels = /* @__PURE__ */ new Map();
3353
- this._dataChannels = /* @__PURE__ */ new Map();
3354
- this._readyForCandidates = new Trigger3();
3355
- this._offerProcessingMutex = new Mutex2();
3356
3449
  this._initiator = chooseInitiatorPeer(_options.ownPeerKey, _options.remotePeerKey) === _options.ownPeerKey;
3357
3450
  }
3358
3451
  get transportChannelCount() {
@@ -3380,7 +3473,7 @@ var RtcPeerConnection = class {
3380
3473
  }
3381
3474
  log13("waiting for initiator-peer to open a data channel", void 0, {
3382
3475
  F: __dxlog_file14,
3383
- L: 95,
3476
+ L: 96,
3384
3477
  S: this,
3385
3478
  C: (f, a) => f(...a)
3386
3479
  });
@@ -3411,7 +3504,7 @@ var RtcPeerConnection = class {
3411
3504
  remotePeer: this._options.remotePeerKey
3412
3505
  }), {
3413
3506
  F: __dxlog_file14,
3414
- L: 120,
3507
+ L: 121,
3415
3508
  S: this,
3416
3509
  C: (f, a) => f(...a)
3417
3510
  });
@@ -3422,7 +3515,7 @@ var RtcPeerConnection = class {
3422
3515
  onnegotiationneeded: async () => {
3423
3516
  invariant12(this._initiator, void 0, {
3424
3517
  F: __dxlog_file14,
3425
- L: 135,
3518
+ L: 136,
3426
3519
  S: this,
3427
3520
  A: [
3428
3521
  "this._initiator",
@@ -3435,7 +3528,7 @@ var RtcPeerConnection = class {
3435
3528
  }
3436
3529
  log13("onnegotiationneeded", void 0, {
3437
3530
  F: __dxlog_file14,
3438
- L: 142,
3531
+ L: 143,
3439
3532
  S: this,
3440
3533
  C: (f, a) => f(...a)
3441
3534
  });
@@ -3459,7 +3552,7 @@ var RtcPeerConnection = class {
3459
3552
  candidate: event.candidate.candidate
3460
3553
  }, {
3461
3554
  F: __dxlog_file14,
3462
- L: 161,
3555
+ L: 162,
3463
3556
  S: this,
3464
3557
  C: (f, a) => f(...a)
3465
3558
  });
@@ -3467,7 +3560,7 @@ var RtcPeerConnection = class {
3467
3560
  } else {
3468
3561
  log13("onicecandidate gathering complete", void 0, {
3469
3562
  F: __dxlog_file14,
3470
- L: 164,
3563
+ L: 165,
3471
3564
  S: this,
3472
3565
  C: (f, a) => f(...a)
3473
3566
  });
@@ -3495,7 +3588,7 @@ var RtcPeerConnection = class {
3495
3588
  state: connection.iceConnectionState
3496
3589
  }, {
3497
3590
  F: __dxlog_file14,
3498
- L: 184,
3591
+ L: 185,
3499
3592
  S: this,
3500
3593
  C: (f, a) => f(...a)
3501
3594
  });
@@ -3517,7 +3610,7 @@ var RtcPeerConnection = class {
3517
3610
  state: connection.connectionState
3518
3611
  }, {
3519
3612
  F: __dxlog_file14,
3520
- L: 201,
3613
+ L: 202,
3521
3614
  S: this,
3522
3615
  C: (f, a) => f(...a)
3523
3616
  });
@@ -3530,7 +3623,7 @@ var RtcPeerConnection = class {
3530
3623
  state: connection.signalingState
3531
3624
  }, {
3532
3625
  F: __dxlog_file14,
3533
- L: 208,
3626
+ L: 209,
3534
3627
  S: this,
3535
3628
  C: (f, a) => f(...a)
3536
3629
  });
@@ -3540,7 +3633,7 @@ var RtcPeerConnection = class {
3540
3633
  ondatachannel: (event) => {
3541
3634
  invariant12(!this._initiator, "Initiator is expected to create data channels.", {
3542
3635
  F: __dxlog_file14,
3543
- L: 214,
3636
+ L: 215,
3544
3637
  S: this,
3545
3638
  A: [
3546
3639
  "!this._initiator",
@@ -3555,7 +3648,7 @@ var RtcPeerConnection = class {
3555
3648
  label: event.channel.label
3556
3649
  }, {
3557
3650
  F: __dxlog_file14,
3558
- L: 221,
3651
+ L: 222,
3559
3652
  S: this,
3560
3653
  C: (f, a) => f(...a)
3561
3654
  });
@@ -3583,7 +3676,7 @@ var RtcPeerConnection = class {
3583
3676
  error
3584
3677
  }, {
3585
3678
  F: __dxlog_file14,
3586
- L: 246,
3679
+ L: 247,
3587
3680
  S: this,
3588
3681
  C: (f, a) => f(...a)
3589
3682
  });
@@ -3604,7 +3697,7 @@ var RtcPeerConnection = class {
3604
3697
  reason: error.message
3605
3698
  }, {
3606
3699
  F: __dxlog_file14,
3607
- L: 260,
3700
+ L: 261,
3608
3701
  S: this,
3609
3702
  C: (f, a) => f(...a)
3610
3703
  });
@@ -3612,7 +3705,7 @@ var RtcPeerConnection = class {
3612
3705
  async _lockAndCloseConnection() {
3613
3706
  invariant12(this._transportChannels.size === 0, void 0, {
3614
3707
  F: __dxlog_file14,
3615
- L: 265,
3708
+ L: 266,
3616
3709
  S: this,
3617
3710
  A: [
3618
3711
  "this._transportChannels.size === 0",
@@ -3623,7 +3716,7 @@ var RtcPeerConnection = class {
3623
3716
  this._safeCloseConnection();
3624
3717
  log13("connection closed", void 0, {
3625
3718
  F: __dxlog_file14,
3626
- L: 268,
3719
+ L: 269,
3627
3720
  S: this,
3628
3721
  C: (f, a) => f(...a)
3629
3722
  });
@@ -3636,7 +3729,7 @@ var RtcPeerConnection = class {
3636
3729
  type: signal.payload.data.type
3637
3730
  }, {
3638
3731
  F: __dxlog_file14,
3639
- L: 276,
3732
+ L: 277,
3640
3733
  S: this,
3641
3734
  C: (f, a) => f(...a)
3642
3735
  });
@@ -3703,7 +3796,7 @@ var RtcPeerConnection = class {
3703
3796
  type: data.type
3704
3797
  }, {
3705
3798
  F: __dxlog_file14,
3706
- L: 335,
3799
+ L: 336,
3707
3800
  S: this,
3708
3801
  C: (f, a) => f(...a)
3709
3802
  });
@@ -3716,7 +3809,7 @@ var RtcPeerConnection = class {
3716
3809
  candidate
3717
3810
  }, {
3718
3811
  F: __dxlog_file14,
3719
- L: 343,
3812
+ L: 344,
3720
3813
  S: this,
3721
3814
  C: (f, a) => f(...a)
3722
3815
  });
@@ -3725,7 +3818,7 @@ var RtcPeerConnection = class {
3725
3818
  } catch (err) {
3726
3819
  log13.catch(err, void 0, {
3727
3820
  F: __dxlog_file14,
3728
- L: 347,
3821
+ L: 348,
3729
3822
  S: this,
3730
3823
  C: (f, a) => f(...a)
3731
3824
  });
@@ -3735,7 +3828,7 @@ var RtcPeerConnection = class {
3735
3828
  if (connection === this._connection) {
3736
3829
  log13("ready to process ice candidates", void 0, {
3737
3830
  F: __dxlog_file14,
3738
- L: 353,
3831
+ L: 354,
3739
3832
  S: this,
3740
3833
  C: (f, a) => f(...a)
3741
3834
  });
@@ -3743,7 +3836,7 @@ var RtcPeerConnection = class {
3743
3836
  } else {
3744
3837
  log13.warn("session was negotiated after connection became inactive", void 0, {
3745
3838
  F: __dxlog_file14,
3746
- L: 356,
3839
+ L: 357,
3747
3840
  S: this,
3748
3841
  C: (f, a) => f(...a)
3749
3842
  });
@@ -3755,7 +3848,7 @@ var RtcPeerConnection = class {
3755
3848
  state: connection.connectionState
3756
3849
  }, {
3757
3850
  F: __dxlog_file14,
3758
- L: 361,
3851
+ L: 362,
3759
3852
  S: this,
3760
3853
  C: (f, a) => f(...a)
3761
3854
  });
@@ -3768,7 +3861,7 @@ var RtcPeerConnection = class {
3768
3861
  } catch (err) {
3769
3862
  log13.catch(err, void 0, {
3770
3863
  F: __dxlog_file14,
3771
- L: 373,
3864
+ L: 374,
3772
3865
  S: this,
3773
3866
  C: (f, a) => f(...a)
3774
3867
  });
@@ -3779,7 +3872,7 @@ var RtcPeerConnection = class {
3779
3872
  this._readyForCandidates.wake();
3780
3873
  void this._factory.onConnectionDestroyed().catch((err) => log13.catch(err, void 0, {
3781
3874
  F: __dxlog_file14,
3782
- L: 379,
3875
+ L: 380,
3783
3876
  S: this,
3784
3877
  C: (f, a) => f(...a)
3785
3878
  }));
@@ -3804,7 +3897,7 @@ var RtcPeerConnection = class {
3804
3897
  } catch (error) {
3805
3898
  log13.catch(error, void 0, {
3806
3899
  F: __dxlog_file14,
3807
- L: 395,
3900
+ L: 396,
3808
3901
  S: this,
3809
3902
  C: (f, a) => f(...a)
3810
3903
  });
@@ -3831,7 +3924,7 @@ var RtcPeerConnection = class {
3831
3924
  err
3832
3925
  }, {
3833
3926
  F: __dxlog_file14,
3834
- L: 416,
3927
+ L: 417,
3835
3928
  S: this,
3836
3929
  C: (f, a) => f(...a)
3837
3930
  });
@@ -3946,8 +4039,14 @@ var RPC_TIMEOUT = 1e4;
3946
4039
  var CLOSE_RPC_TIMEOUT = 3e3;
3947
4040
  var RESP_MIN_THRESHOLD = 500;
3948
4041
  var RtcTransportProxy = class extends Resource2 {
4042
+ _options;
4043
+ _proxyId = PublicKey10.random();
4044
+ closed = new Event8();
4045
+ connected = new Event8();
4046
+ errors = new ErrorStream5();
4047
+ _serviceStream;
3949
4048
  constructor(_options) {
3950
- super(), this._options = _options, this._proxyId = PublicKey10.random(), this.closed = new Event8(), this.connected = new Event8(), this.errors = new ErrorStream5();
4049
+ super(), this._options = _options;
3951
4050
  }
3952
4051
  async _open() {
3953
4052
  let stream;
@@ -4156,9 +4255,8 @@ var RtcTransportProxy = class extends Resource2 {
4156
4255
  }
4157
4256
  };
4158
4257
  var RtcTransportProxyFactory = class {
4159
- constructor() {
4160
- this._connections = /* @__PURE__ */ new Set();
4161
- }
4258
+ _bridgeService;
4259
+ _connections = /* @__PURE__ */ new Set();
4162
4260
  /**
4163
4261
  * Sets the current BridgeService to be used to open connections.
4164
4262
  * Calling this method will close any existing connections.
@@ -4214,9 +4312,10 @@ import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/
4214
4312
  import { ComplexMap as ComplexMap8 } from "@dxos/util";
4215
4313
  var __dxlog_file16 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-service.ts";
4216
4314
  var RtcTransportService = class {
4315
+ _transportFactory;
4316
+ _openTransports = new ComplexMap8(PublicKey11.hash);
4217
4317
  constructor(webrtcConfig, iceProvider, _transportFactory = createRtcTransportFactory(webrtcConfig, iceProvider)) {
4218
4318
  this._transportFactory = _transportFactory;
4219
- this._openTransports = new ComplexMap8(PublicKey11.hash);
4220
4319
  }
4221
4320
  hasOpenTransports() {
4222
4321
  return this._openTransports.size > 0;
@@ -4226,7 +4325,7 @@ var RtcTransportService = class {
4226
4325
  if (existingTransport) {
4227
4326
  log15.error("requesting a new transport bridge for an existing proxy", void 0, {
4228
4327
  F: __dxlog_file16,
4229
- L: 53,
4328
+ L: 54,
4230
4329
  S: this,
4231
4330
  C: (f, a) => f(...a)
4232
4331
  });
@@ -4292,7 +4391,7 @@ var RtcTransportService = class {
4292
4391
  ready();
4293
4392
  log15("stream ready", void 0, {
4294
4393
  F: __dxlog_file16,
4295
- L: 115,
4394
+ L: 116,
4296
4395
  S: this,
4297
4396
  C: (f, a) => f(...a)
4298
4397
  });
@@ -4303,7 +4402,7 @@ var RtcTransportService = class {
4303
4402
  const transport = this._openTransports.get(proxyId);
4304
4403
  invariant14(transport, void 0, {
4305
4404
  F: __dxlog_file16,
4306
- L: 123,
4405
+ L: 124,
4307
4406
  S: this,
4308
4407
  A: [
4309
4408
  "transport",
@@ -4316,7 +4415,7 @@ var RtcTransportService = class {
4316
4415
  const transport = this._openTransports.get(proxyId);
4317
4416
  invariant14(transport, void 0, {
4318
4417
  F: __dxlog_file16,
4319
- L: 130,
4418
+ L: 131,
4320
4419
  S: this,
4321
4420
  A: [
4322
4421
  "transport",
@@ -4331,7 +4430,7 @@ var RtcTransportService = class {
4331
4430
  const transport = this._openTransports.get(proxyId);
4332
4431
  invariant14(transport, void 0, {
4333
4432
  F: __dxlog_file16,
4334
- L: 137,
4433
+ L: 138,
4335
4434
  S: this,
4336
4435
  A: [
4337
4436
  "transport",
@@ -4346,7 +4445,7 @@ var RtcTransportService = class {
4346
4445
  const transport = this._openTransports.get(proxyId);
4347
4446
  invariant14(transport, void 0, {
4348
4447
  F: __dxlog_file16,
4349
- L: 144,
4448
+ L: 145,
4350
4449
  S: this,
4351
4450
  A: [
4352
4451
  "transport",
@@ -4380,7 +4479,7 @@ var RtcTransportService = class {
4380
4479
  message: error?.message
4381
4480
  }, {
4382
4481
  F: __dxlog_file16,
4383
- L: 174,
4482
+ L: 175,
4384
4483
  S: this,
4385
4484
  C: (f, a) => f(...a)
4386
4485
  });
@@ -4392,14 +4491,14 @@ var RtcTransportService = class {
4392
4491
  message: error?.message
4393
4492
  }, {
4394
4493
  F: __dxlog_file16,
4395
- L: 179,
4494
+ L: 180,
4396
4495
  S: this,
4397
4496
  C: (f, a) => f(...a)
4398
4497
  });
4399
4498
  }
4400
4499
  log15("closed", void 0, {
4401
4500
  F: __dxlog_file16,
4402
- L: 181,
4501
+ L: 182,
4403
4502
  S: this,
4404
4503
  C: (f, a) => f(...a)
4405
4504
  });
@@ -4466,4 +4565,4 @@ export {
4466
4565
  RtcTransportService,
4467
4566
  createTeleportProtocolFactory
4468
4567
  };
4469
- //# sourceMappingURL=chunk-HQNQOWFA.mjs.map
4568
+ //# sourceMappingURL=chunk-E3P563GT.mjs.map