@dxos/network-manager 0.1.58-next.3349da8 → 0.1.58-next.ffb4cc4

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 (50) hide show
  1. package/dist/lib/browser/{chunk-PSWNMALH.mjs → chunk-3KLH55NB.mjs} +155 -94
  2. package/dist/lib/browser/chunk-3KLH55NB.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 +19 -148
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node/index.cjs +631 -458
  8. package/dist/lib/node/index.cjs.map +4 -4
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +957 -917
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/swarm/connection-limiter.d.ts +1 -1
  13. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
  14. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  15. package/dist/types/src/swarm/peer.d.ts +3 -3
  16. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  17. package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
  18. package/dist/types/src/swarm/swarm.d.ts +2 -2
  19. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  20. package/dist/types/src/testing/test-builder.d.ts +1 -1
  21. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  22. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  23. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  24. package/dist/types/src/transport/index.d.ts +1 -0
  25. package/dist/types/src/transport/index.d.ts.map +1 -1
  26. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  27. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  28. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  29. package/dist/types/src/transport/tcp-transport.browser.d.ts +12 -0
  30. package/dist/types/src/transport/tcp-transport.browser.d.ts.map +1 -0
  31. package/package.json +18 -18
  32. package/src/swarm/connection-limiter.ts +2 -2
  33. package/src/swarm/connection.test.ts +1 -1
  34. package/src/swarm/connection.ts +9 -8
  35. package/src/swarm/peer.ts +8 -3
  36. package/src/swarm/swarm-mapper.ts +4 -7
  37. package/src/swarm/swarm.test.ts +3 -3
  38. package/src/swarm/swarm.ts +3 -3
  39. package/src/testing/test-builder.ts +1 -1
  40. package/src/tests/basic-test-suite.ts +1 -1
  41. package/src/tests/memory-transport.test.ts +2 -2
  42. package/src/tests/tcp-transport.test.ts +2 -2
  43. package/src/tests/webrtc-transport.test.ts +1 -1
  44. package/src/topology/star-topology.ts +1 -4
  45. package/src/transport/index.ts +1 -0
  46. package/src/transport/simplepeer-transport-proxy.ts +29 -28
  47. package/src/transport/simplepeer-transport-service.ts +9 -5
  48. package/src/transport/simplepeer-transport.ts +9 -7
  49. package/src/transport/tcp-transport.browser.ts +28 -0
  50. package/dist/lib/browser/chunk-PSWNMALH.mjs.map +0 -7
@@ -527,6 +527,8 @@ __export(src_exports, {
527
527
  Swarm: () => Swarm,
528
528
  SwarmMapper: () => SwarmMapper,
529
529
  SwarmMessenger: () => SwarmMessenger,
530
+ TcpTransport: () => TcpTransport,
531
+ TcpTransportFactory: () => TcpTransportFactory,
530
532
  TransportKind: () => TransportKind,
531
533
  cleanup: () => cleanup,
532
534
  createLibDataChannelTransportFactory: () => createLibDataChannelTransportFactory,
@@ -545,7 +547,6 @@ var import_util5 = require("@dxos/util");
545
547
  var import_async = require("@dxos/async");
546
548
  var import_context = require("@dxos/context");
547
549
  var import_debug = require("@dxos/debug");
548
- var import_errors = require("@dxos/errors");
549
550
  var import_invariant = require("@dxos/invariant");
550
551
  var import_keys = require("@dxos/keys");
551
552
  var import_log = require("@dxos/log");
@@ -693,7 +694,7 @@ var Connection = class {
693
694
  S: this,
694
695
  C: (f, a) => f(...a)
695
696
  });
696
- this.close(new import_errors.ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
697
+ this.close(new import_protocols.ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
697
698
  });
698
699
  (0, import_invariant.invariant)(!this._transport, void 0, {
699
700
  F: __dxlog_file,
@@ -735,24 +736,24 @@ var Connection = class {
735
736
  if (!this.closeReason) {
736
737
  this.closeReason = err?.message;
737
738
  }
738
- if (err instanceof import_errors.ConnectionResetError) {
739
- (0, import_log.log)("aborting due to transport ConnectionResetError", void 0, {
739
+ if (err instanceof import_protocols.ConnectionResetError) {
740
+ import_log.log.warn("aborting due to transport ConnectionResetError", void 0, {
740
741
  F: __dxlog_file,
741
742
  L: 197,
742
743
  S: this,
743
744
  C: (f, a) => f(...a)
744
745
  });
745
746
  this.abort().catch((err2) => this.errors.raise(err2));
746
- } else if (err instanceof import_errors.ConnectivityError) {
747
- (0, import_log.log)("aborting due to transport ConnectivityError", void 0, {
747
+ } else if (err instanceof import_protocols.ConnectivityError) {
748
+ import_log.log.warn("aborting due to transport ConnectivityError", void 0, {
748
749
  F: __dxlog_file,
749
750
  L: 200,
750
751
  S: this,
751
752
  C: (f, a) => f(...a)
752
753
  });
753
754
  this.abort().catch((err2) => this.errors.raise(err2));
754
- } else if (err instanceof import_errors.UnknownProtocolError) {
755
- (0, import_log.log)("unsure what to do for UnknownProtocolError", {
755
+ } else if (err instanceof import_protocols.UnknownProtocolError) {
756
+ import_log.log.warn("unsure what to do with UnknownProtocolError, will keep on truckin", {
756
757
  err
757
758
  }, {
758
759
  F: __dxlog_file,
@@ -783,14 +784,14 @@ var Connection = class {
783
784
  err
784
785
  }, {
785
786
  F: __dxlog_file,
786
- L: 224,
787
+ L: 225,
787
788
  S: this,
788
789
  C: (f, a) => f(...a)
789
790
  });
790
791
  if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
791
792
  (0, import_log.log)(`abort ignored: already ${this._state}`, this.closeReason, {
792
793
  F: __dxlog_file,
793
- L: 226,
794
+ L: 227,
794
795
  S: this,
795
796
  C: (f, a) => f(...a)
796
797
  });
@@ -806,7 +807,7 @@ var Connection = class {
806
807
  proto: this._protocol
807
808
  }, {
808
809
  F: __dxlog_file,
809
- L: 239,
810
+ L: 240,
810
811
  S: this,
811
812
  C: (f, a) => f(...a)
812
813
  });
@@ -814,7 +815,7 @@ var Connection = class {
814
815
  } catch (err2) {
815
816
  import_log.log.catch(err2, void 0, {
816
817
  F: __dxlog_file,
817
- L: 242,
818
+ L: 243,
818
819
  S: this,
819
820
  C: (f, a) => f(...a)
820
821
  });
@@ -824,7 +825,7 @@ var Connection = class {
824
825
  } catch (err2) {
825
826
  import_log.log.catch(err2, void 0, {
826
827
  F: __dxlog_file,
827
- L: 249,
828
+ L: 250,
828
829
  S: this,
829
830
  C: (f, a) => f(...a)
830
831
  });
@@ -834,7 +835,7 @@ var Connection = class {
834
835
  } catch (err2) {
835
836
  import_log.log.catch(err2, void 0, {
836
837
  F: __dxlog_file,
837
- L: 255,
838
+ L: 256,
838
839
  S: this,
839
840
  C: (f, a) => f(...a)
840
841
  });
@@ -855,7 +856,7 @@ var Connection = class {
855
856
  peerId: this.ownId
856
857
  }, {
857
858
  F: __dxlog_file,
858
- L: 277,
859
+ L: 278,
859
860
  S: this,
860
861
  C: (f, a) => f(...a)
861
862
  });
@@ -865,7 +866,7 @@ var Connection = class {
865
866
  } catch (err2) {
866
867
  import_log.log.catch(err2, void 0, {
867
868
  F: __dxlog_file,
868
- L: 284,
869
+ L: 285,
869
870
  S: this,
870
871
  C: (f, a) => f(...a)
871
872
  });
@@ -875,7 +876,7 @@ var Connection = class {
875
876
  } catch (err2) {
876
877
  import_log.log.catch(err2, void 0, {
877
878
  F: __dxlog_file,
878
- L: 291,
879
+ L: 292,
879
880
  S: this,
880
881
  C: (f, a) => f(...a)
881
882
  });
@@ -885,7 +886,7 @@ var Connection = class {
885
886
  peerId: this.ownId
886
887
  }, {
887
888
  F: __dxlog_file,
888
- L: 295,
889
+ L: 296,
889
890
  S: this,
890
891
  C: (f, a) => f(...a)
891
892
  });
@@ -921,14 +922,14 @@ var Connection = class {
921
922
  }
922
923
  });
923
924
  } catch (err) {
924
- if (err instanceof import_errors.CancelledError) {
925
+ if (err instanceof import_protocols.CancelledError) {
925
926
  return;
926
927
  }
927
928
  import_log.log.warn("signal failed", {
928
929
  err
929
930
  }, {
930
931
  F: __dxlog_file,
931
- L: 332,
932
+ L: 333,
932
933
  S: this,
933
934
  C: (f, a) => f(...a)
934
935
  });
@@ -941,7 +942,7 @@ var Connection = class {
941
942
  async signal(msg) {
942
943
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
943
944
  F: __dxlog_file,
944
- L: 341,
945
+ L: 342,
945
946
  S: this,
946
947
  A: [
947
948
  "msg.sessionId",
@@ -951,7 +952,7 @@ var Connection = class {
951
952
  if (!msg.sessionId.equals(this.sessionId)) {
952
953
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
953
954
  F: __dxlog_file,
954
- L: 343,
955
+ L: 344,
955
956
  S: this,
956
957
  C: (f, a) => f(...a)
957
958
  });
@@ -959,7 +960,7 @@ var Connection = class {
959
960
  }
960
961
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
961
962
  F: __dxlog_file,
962
- L: 346,
963
+ L: 347,
963
964
  S: this,
964
965
  A: [
965
966
  "msg.data.signal || msg.data.signalBatch",
@@ -968,7 +969,7 @@ var Connection = class {
968
969
  });
969
970
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
970
971
  F: __dxlog_file,
971
- L: 347,
972
+ L: 348,
972
973
  S: this,
973
974
  A: [
974
975
  "msg.author?.equals(this.remoteId)",
@@ -977,7 +978,7 @@ var Connection = class {
977
978
  });
978
979
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
979
980
  F: __dxlog_file,
980
- L: 348,
981
+ L: 349,
981
982
  S: this,
982
983
  A: [
983
984
  "msg.recipient?.equals(this.ownId)",
@@ -1001,7 +1002,7 @@ var Connection = class {
1001
1002
  msg: msg.data
1002
1003
  }, {
1003
1004
  F: __dxlog_file,
1004
- L: 357,
1005
+ L: 358,
1005
1006
  S: this,
1006
1007
  C: (f, a) => f(...a)
1007
1008
  });
@@ -1009,7 +1010,7 @@ var Connection = class {
1009
1010
  } else {
1010
1011
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
1011
1012
  F: __dxlog_file,
1012
- L: 360,
1013
+ L: 361,
1013
1014
  S: this,
1014
1015
  A: [
1015
1016
  "this._transport",
@@ -1022,7 +1023,7 @@ var Connection = class {
1022
1023
  msg: msg.data
1023
1024
  }, {
1024
1025
  F: __dxlog_file,
1025
- L: 361,
1026
+ L: 362,
1026
1027
  S: this,
1027
1028
  C: (f, a) => f(...a)
1028
1029
  });
@@ -1040,13 +1041,13 @@ var Connection = class {
1040
1041
  peerId: this.ownId
1041
1042
  }, {
1042
1043
  F: __dxlog_file,
1043
- L: 372,
1044
+ L: 373,
1044
1045
  S: this,
1045
1046
  C: (f, a) => f(...a)
1046
1047
  });
1047
1048
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
1048
1049
  F: __dxlog_file,
1049
- L: 373,
1050
+ L: 374,
1050
1051
  S: this,
1051
1052
  A: [
1052
1053
  "state !== this._state",
@@ -1071,248 +1072,16 @@ var import_debug2 = require("@dxos/debug");
1071
1072
  var import_invariant4 = require("@dxos/invariant");
1072
1073
  var import_keys4 = require("@dxos/keys");
1073
1074
  var import_log4 = require("@dxos/log");
1074
- var import_protocols3 = require("@dxos/protocols");
1075
+ var import_protocols4 = require("@dxos/protocols");
1075
1076
  var import_util2 = require("@dxos/util");
1076
1077
 
1077
- // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1078
+ // packages/core/mesh/network-manager/src/swarm/peer.ts
1079
+ var import_async2 = require("@dxos/async");
1078
1080
  var import_context2 = require("@dxos/context");
1079
1081
  var import_invariant2 = require("@dxos/invariant");
1080
1082
  var import_keys2 = require("@dxos/keys");
1081
1083
  var import_log2 = require("@dxos/log");
1082
1084
  var import_protocols2 = require("@dxos/protocols");
1083
- var import_util = require("@dxos/util");
1084
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1085
- var SwarmMessage = import_protocols2.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1086
- var SwarmMessenger = class {
1087
- constructor({ sendMessage, onSignal, onOffer, topic }) {
1088
- this._ctx = new import_context2.Context();
1089
- this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1090
- this._sendMessage = sendMessage;
1091
- this._onSignal = onSignal;
1092
- this._onOffer = onOffer;
1093
- this._topic = topic;
1094
- }
1095
- async receiveMessage({ author, recipient, payload }) {
1096
- if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1097
- return;
1098
- }
1099
- const message = SwarmMessage.decode(payload.value);
1100
- if (!this._topic.equals(message.topic)) {
1101
- return;
1102
- }
1103
- (0, import_log2.log)("received", {
1104
- from: author,
1105
- to: recipient,
1106
- msg: message
1107
- }, {
1108
- F: __dxlog_file2,
1109
- L: 69,
1110
- S: this,
1111
- C: (f, a) => f(...a)
1112
- });
1113
- if (message.data?.offer) {
1114
- await this._handleOffer({
1115
- author,
1116
- recipient,
1117
- message
1118
- });
1119
- } else if (message.data?.answer) {
1120
- await this._resolveAnswers(message);
1121
- } else if (message.data?.signal) {
1122
- await this._handleSignal({
1123
- author,
1124
- recipient,
1125
- message
1126
- });
1127
- } else if (message.data?.signalBatch) {
1128
- await this._handleSignal({
1129
- author,
1130
- recipient,
1131
- message
1132
- });
1133
- } else {
1134
- import_log2.log.warn("unknown message", {
1135
- message
1136
- }, {
1137
- F: __dxlog_file2,
1138
- L: 80,
1139
- S: this,
1140
- C: (f, a) => f(...a)
1141
- });
1142
- }
1143
- }
1144
- async signal(message) {
1145
- (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1146
- F: __dxlog_file2,
1147
- L: 85,
1148
- S: this,
1149
- A: [
1150
- "message.data?.signal || message.data?.signalBatch",
1151
- "'Invalid message'"
1152
- ]
1153
- });
1154
- await this._sendReliableMessage({
1155
- author: message.author,
1156
- recipient: message.recipient,
1157
- message
1158
- });
1159
- }
1160
- async offer(message) {
1161
- const networkMessage = {
1162
- ...message,
1163
- messageId: import_keys2.PublicKey.random()
1164
- };
1165
- return new Promise((resolve, reject) => {
1166
- this._offerRecords.set(networkMessage.messageId, {
1167
- resolve
1168
- });
1169
- this._sendReliableMessage({
1170
- author: message.author,
1171
- recipient: message.recipient,
1172
- message: networkMessage
1173
- }).catch((err) => reject(err));
1174
- });
1175
- }
1176
- async _sendReliableMessage({ author, recipient, message }) {
1177
- const networkMessage = {
1178
- ...message,
1179
- // Setting unique message_id if it not specified yet.
1180
- messageId: message.messageId ?? import_keys2.PublicKey.random()
1181
- };
1182
- (0, import_log2.log)("sending", {
1183
- from: author,
1184
- to: recipient,
1185
- msg: networkMessage
1186
- }, {
1187
- F: __dxlog_file2,
1188
- L: 123,
1189
- S: this,
1190
- C: (f, a) => f(...a)
1191
- });
1192
- await this._sendMessage({
1193
- author,
1194
- recipient,
1195
- payload: {
1196
- type_url: "dxos.mesh.swarm.SwarmMessage",
1197
- value: SwarmMessage.encode(networkMessage)
1198
- }
1199
- });
1200
- }
1201
- async _resolveAnswers(message) {
1202
- (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1203
- F: __dxlog_file2,
1204
- L: 135,
1205
- S: this,
1206
- A: [
1207
- "message.data?.answer?.offerMessageId",
1208
- "'No offerMessageId'"
1209
- ]
1210
- });
1211
- const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1212
- if (offerRecord) {
1213
- this._offerRecords.delete(message.data.answer.offerMessageId);
1214
- (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
1215
- F: __dxlog_file2,
1216
- L: 139,
1217
- S: this,
1218
- A: [
1219
- "message.data?.answer",
1220
- "'No answer'"
1221
- ]
1222
- });
1223
- (0, import_log2.log)("resolving", {
1224
- answer: message.data.answer
1225
- }, {
1226
- F: __dxlog_file2,
1227
- L: 140,
1228
- S: this,
1229
- C: (f, a) => f(...a)
1230
- });
1231
- offerRecord.resolve(message.data.answer);
1232
- }
1233
- }
1234
- async _handleOffer({ author, recipient, message }) {
1235
- (0, import_invariant2.invariant)(message.data.offer, "No offer", {
1236
- F: __dxlog_file2,
1237
- L: 154,
1238
- S: this,
1239
- A: [
1240
- "message.data.offer",
1241
- "'No offer'"
1242
- ]
1243
- });
1244
- const offerMessage = {
1245
- author,
1246
- recipient,
1247
- ...message,
1248
- data: {
1249
- offer: message.data.offer
1250
- }
1251
- };
1252
- const answer = await this._onOffer(offerMessage);
1253
- answer.offerMessageId = message.messageId;
1254
- try {
1255
- await this._sendReliableMessage({
1256
- author: recipient,
1257
- recipient: author,
1258
- message: {
1259
- topic: message.topic,
1260
- sessionId: message.sessionId,
1261
- data: {
1262
- answer
1263
- }
1264
- }
1265
- });
1266
- } catch (err) {
1267
- import_log2.log.warn("error sending answer", {
1268
- err
1269
- }, {
1270
- F: __dxlog_file2,
1271
- L: 174,
1272
- S: this,
1273
- C: (f, a) => f(...a)
1274
- });
1275
- }
1276
- }
1277
- async _handleSignal({ author, recipient, message }) {
1278
- (0, import_invariant2.invariant)(message.messageId, void 0, {
1279
- F: __dxlog_file2,
1280
- L: 187,
1281
- S: this,
1282
- A: [
1283
- "message.messageId",
1284
- ""
1285
- ]
1286
- });
1287
- (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1288
- F: __dxlog_file2,
1289
- L: 188,
1290
- S: this,
1291
- A: [
1292
- "message.data.signal || message.data.signalBatch",
1293
- "'Invalid message'"
1294
- ]
1295
- });
1296
- const signalMessage = {
1297
- author,
1298
- recipient,
1299
- ...message,
1300
- data: {
1301
- signal: message.data.signal,
1302
- signalBatch: message.data.signalBatch
1303
- }
1304
- };
1305
- await this._onSignal(signalMessage);
1306
- }
1307
- };
1308
-
1309
- // packages/core/mesh/network-manager/src/swarm/peer.ts
1310
- var import_async2 = require("@dxos/async");
1311
- var import_context3 = require("@dxos/context");
1312
- var import_errors2 = require("@dxos/errors");
1313
- var import_invariant3 = require("@dxos/invariant");
1314
- var import_keys3 = require("@dxos/keys");
1315
- var import_log3 = require("@dxos/log");
1316
1085
  function _ts_decorate2(decorators, target, key, desc) {
1317
1086
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1318
1087
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1323,8 +1092,8 @@ function _ts_decorate2(decorators, target, key, desc) {
1323
1092
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1324
1093
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1325
1094
  }
1326
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1327
- var ConnectionDisplacedError = class extends import_errors2.SystemError {
1095
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1096
+ var ConnectionDisplacedError = class extends import_protocols2.SystemError {
1328
1097
  constructor() {
1329
1098
  super("Connection displaced by remote initiator.");
1330
1099
  }
@@ -1343,7 +1112,7 @@ var Peer = class {
1343
1112
  this._callbacks = _callbacks;
1344
1113
  this._availableAfter = 0;
1345
1114
  this.availableToConnect = true;
1346
- this._ctx = new import_context3.Context();
1115
+ this._ctx = new import_context2.Context();
1347
1116
  this.advertizing = false;
1348
1117
  this.initiating = false;
1349
1118
  this.connectionDisplaced = new import_async2.Event();
@@ -1353,16 +1122,30 @@ var Peer = class {
1353
1122
  */
1354
1123
  async onOffer(message) {
1355
1124
  const remoteId = message.author;
1125
+ if (this.connection && ![
1126
+ ConnectionState.CREATED,
1127
+ ConnectionState.INITIAL
1128
+ ].includes(this.connection.state)) {
1129
+ import_log2.log.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
1130
+ F: __dxlog_file2,
1131
+ L: 112,
1132
+ S: this,
1133
+ C: (f, a) => f(...a)
1134
+ });
1135
+ return {
1136
+ accept: false
1137
+ };
1138
+ }
1356
1139
  if (this.connection || this.initiating) {
1357
1140
  if (remoteId.toHex() < this.localPeerId.toHex()) {
1358
- (0, import_log3.log)("close local connection", {
1141
+ (0, import_log2.log)("close local connection", {
1359
1142
  localPeerId: this.id,
1360
1143
  topic: this.topic,
1361
1144
  remotePeerId: this.localPeerId,
1362
1145
  sessionId: this.connection?.sessionId
1363
1146
  }, {
1364
- F: __dxlog_file3,
1365
- L: 117,
1147
+ F: __dxlog_file2,
1148
+ L: 121,
1366
1149
  S: this,
1367
1150
  C: (f, a) => f(...a)
1368
1151
  });
@@ -1377,9 +1160,9 @@ var Peer = class {
1377
1160
  }
1378
1161
  if (await this._callbacks.onOffer(remoteId)) {
1379
1162
  if (!this.connection) {
1380
- (0, import_invariant3.invariant)(message.sessionId, void 0, {
1381
- F: __dxlog_file3,
1382
- L: 137,
1163
+ (0, import_invariant2.invariant)(message.sessionId, void 0, {
1164
+ F: __dxlog_file2,
1165
+ L: 141,
1383
1166
  S: this,
1384
1167
  A: [
1385
1168
  "message.sessionId",
@@ -1392,15 +1175,15 @@ var Peer = class {
1392
1175
  connection.initiate();
1393
1176
  await connection.openConnection();
1394
1177
  } catch (err) {
1395
- if (!(err instanceof import_errors2.CancelledError)) {
1396
- import_log3.log.warn("connection error", {
1178
+ if (!(err instanceof import_protocols2.CancelledError)) {
1179
+ import_log2.log.warn("connection error", {
1397
1180
  topic: this.topic,
1398
1181
  peerId: this.localPeerId,
1399
1182
  remoteId: this.id,
1400
1183
  err
1401
1184
  }, {
1402
- F: __dxlog_file3,
1403
- L: 147,
1185
+ F: __dxlog_file2,
1186
+ L: 151,
1404
1187
  S: this,
1405
1188
  C: (f, a) => f(...a)
1406
1189
  });
@@ -1420,33 +1203,33 @@ var Peer = class {
1420
1203
  * Initiate a connection to the remote peer.
1421
1204
  */
1422
1205
  async initiateConnection() {
1423
- (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
1424
- F: __dxlog_file3,
1425
- L: 164,
1206
+ (0, import_invariant2.invariant)(!this.initiating, "Initiation in progress.", {
1207
+ F: __dxlog_file2,
1208
+ L: 168,
1426
1209
  S: this,
1427
1210
  A: [
1428
1211
  "!this.initiating",
1429
1212
  "'Initiation in progress.'"
1430
1213
  ]
1431
1214
  });
1432
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1433
- F: __dxlog_file3,
1434
- L: 165,
1215
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1216
+ F: __dxlog_file2,
1217
+ L: 169,
1435
1218
  S: this,
1436
1219
  A: [
1437
1220
  "!this.connection",
1438
1221
  "'Already connected.'"
1439
1222
  ]
1440
1223
  });
1441
- const sessionId = import_keys3.PublicKey.random();
1442
- (0, import_log3.log)("initiating...", {
1224
+ const sessionId = import_keys2.PublicKey.random();
1225
+ (0, import_log2.log)("initiating...", {
1443
1226
  id: this.id,
1444
1227
  topic: this.topic,
1445
1228
  peerId: this.id,
1446
1229
  sessionId
1447
1230
  }, {
1448
- F: __dxlog_file3,
1449
- L: 167,
1231
+ F: __dxlog_file2,
1232
+ L: 171,
1450
1233
  S: this,
1451
1234
  C: (f, a) => f(...a)
1452
1235
  });
@@ -1464,21 +1247,21 @@ var Peer = class {
1464
1247
  offer: {}
1465
1248
  }
1466
1249
  });
1467
- (0, import_log3.log)("received", {
1250
+ (0, import_log2.log)("received", {
1468
1251
  answer,
1469
1252
  topic: this.topic,
1470
1253
  ownId: this.localPeerId,
1471
1254
  remoteId: this.id
1472
1255
  }, {
1473
- F: __dxlog_file3,
1474
- L: 183,
1256
+ F: __dxlog_file2,
1257
+ L: 187,
1475
1258
  S: this,
1476
1259
  C: (f, a) => f(...a)
1477
1260
  });
1478
1261
  if (connection.state !== ConnectionState.INITIAL) {
1479
- (0, import_log3.log)("ignoring response", void 0, {
1480
- F: __dxlog_file3,
1481
- L: 185,
1262
+ (0, import_log2.log)("ignoring response", void 0, {
1263
+ F: __dxlog_file2,
1264
+ L: 189,
1482
1265
  S: this,
1483
1266
  C: (f, a) => f(...a)
1484
1267
  });
@@ -1491,18 +1274,24 @@ var Peer = class {
1491
1274
  await connection.openConnection();
1492
1275
  this._callbacks.onAccepted();
1493
1276
  } catch (err) {
1494
- (0, import_log3.log)("initiation error", {
1277
+ (0, import_log2.log)("initiation error", {
1495
1278
  err,
1496
1279
  topic: this.topic,
1497
1280
  peerId: this.localPeerId,
1498
1281
  remoteId: this.id
1499
1282
  }, {
1500
- F: __dxlog_file3,
1501
- L: 196,
1283
+ F: __dxlog_file2,
1284
+ L: 200,
1502
1285
  S: this,
1503
1286
  C: (f, a) => f(...a)
1504
1287
  });
1505
- if (err instanceof import_errors2.TimeoutError) {
1288
+ if (err instanceof import_protocols2.TimeoutError) {
1289
+ import_log2.log.warn("aborting connection due to signalling failure", void 0, {
1290
+ F: __dxlog_file2,
1291
+ L: 202,
1292
+ S: this,
1293
+ C: (f, a) => f(...a)
1294
+ });
1506
1295
  await connection.abort(err);
1507
1296
  }
1508
1297
  await this.closeConnection(err);
@@ -1516,21 +1305,21 @@ var Peer = class {
1516
1305
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
1517
1306
  */
1518
1307
  _createConnection(initiator, sessionId) {
1519
- (0, import_log3.log)("creating connection", {
1308
+ (0, import_log2.log)("creating connection", {
1520
1309
  topic: this.topic,
1521
1310
  peerId: this.localPeerId,
1522
1311
  remoteId: this.id,
1523
1312
  initiator,
1524
1313
  sessionId
1525
1314
  }, {
1526
- F: __dxlog_file3,
1527
- L: 213,
1315
+ F: __dxlog_file2,
1316
+ L: 218,
1528
1317
  S: this,
1529
1318
  C: (f, a) => f(...a)
1530
1319
  });
1531
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1532
- F: __dxlog_file3,
1533
- L: 220,
1320
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1321
+ F: __dxlog_file2,
1322
+ L: 225,
1534
1323
  S: this,
1535
1324
  A: [
1536
1325
  "!this.connection",
@@ -1558,50 +1347,50 @@ var Peer = class {
1558
1347
  this._lastConnectionTime = Date.now();
1559
1348
  this._callbacks.onConnected();
1560
1349
  this._connectionLimiter.doneConnecting(sessionId);
1561
- import_log3.log.trace("dxos.mesh.connection.connected", {
1350
+ import_log2.log.trace("dxos.mesh.connection.connected", {
1562
1351
  topic: this.topic,
1563
1352
  localPeerId: this.localPeerId,
1564
1353
  remotePeerId: this.id,
1565
1354
  sessionId,
1566
1355
  initiator
1567
1356
  }, {
1568
- F: __dxlog_file3,
1569
- L: 239,
1357
+ F: __dxlog_file2,
1358
+ L: 244,
1570
1359
  S: this,
1571
1360
  C: (f, a) => f(...a)
1572
1361
  });
1573
1362
  },
1574
1363
  onClosed: (err) => {
1575
- (0, import_log3.log)("connection closed", {
1364
+ (0, import_log2.log)("connection closed", {
1576
1365
  topic: this.topic,
1577
1366
  peerId: this.localPeerId,
1578
1367
  remoteId: this.id,
1579
1368
  initiator
1580
1369
  }, {
1581
- F: __dxlog_file3,
1582
- L: 248,
1370
+ F: __dxlog_file2,
1371
+ L: 253,
1583
1372
  S: this,
1584
1373
  C: (f, a) => f(...a)
1585
1374
  });
1586
1375
  this._connectionLimiter.doneConnecting(sessionId);
1587
- (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1588
- F: __dxlog_file3,
1589
- L: 253,
1376
+ (0, import_invariant2.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1377
+ F: __dxlog_file2,
1378
+ L: 258,
1590
1379
  S: this,
1591
1380
  A: [
1592
1381
  "this.connection === connection",
1593
1382
  "'Connection mismatch (race condition).'"
1594
1383
  ]
1595
1384
  });
1596
- import_log3.log.trace("dxos.mesh.connection.closed", {
1385
+ import_log2.log.trace("dxos.mesh.connection.closed", {
1597
1386
  topic: this.topic,
1598
1387
  localPeerId: this.localPeerId,
1599
1388
  remotePeerId: this.id,
1600
1389
  sessionId,
1601
1390
  initiator
1602
1391
  }, {
1603
- F: __dxlog_file3,
1604
- L: 255,
1392
+ F: __dxlog_file2,
1393
+ L: 260,
1605
1394
  S: this,
1606
1395
  C: (f, a) => f(...a)
1607
1396
  });
@@ -1623,108 +1412,340 @@ var Peer = class {
1623
1412
  this.connection = void 0;
1624
1413
  }
1625
1414
  }
1626
- );
1627
- this._callbacks.onInitiated(connection);
1628
- void this._connectionCtx?.dispose();
1629
- this._connectionCtx = this._ctx.derive();
1630
- connection.errors.handle((err) => {
1631
- import_log3.log.warn("connection error", {
1632
- topic: this.topic,
1633
- peerId: this.localPeerId,
1634
- remoteId: this.id,
1635
- initiator,
1636
- err
1637
- }, {
1415
+ );
1416
+ this._callbacks.onInitiated(connection);
1417
+ void this._connectionCtx?.dispose();
1418
+ this._connectionCtx = this._ctx.derive();
1419
+ connection.errors.handle((err) => {
1420
+ import_log2.log.warn("connection error", {
1421
+ topic: this.topic,
1422
+ peerId: this.localPeerId,
1423
+ remoteId: this.id,
1424
+ initiator,
1425
+ err
1426
+ }, {
1427
+ F: __dxlog_file2,
1428
+ L: 300,
1429
+ S: this,
1430
+ C: (f, a) => f(...a)
1431
+ });
1432
+ import_log2.log.trace("dxos.mesh.connection.error", {
1433
+ topic: this.topic,
1434
+ localPeerId: this.localPeerId,
1435
+ remotePeerId: this.id,
1436
+ sessionId,
1437
+ initiator,
1438
+ err
1439
+ }, {
1440
+ F: __dxlog_file2,
1441
+ L: 301,
1442
+ S: this,
1443
+ C: (f, a) => f(...a)
1444
+ });
1445
+ void this.closeConnection(err);
1446
+ });
1447
+ this.connection = connection;
1448
+ return connection;
1449
+ }
1450
+ async closeConnection(err) {
1451
+ if (!this.connection) {
1452
+ return;
1453
+ }
1454
+ const connection = this.connection;
1455
+ (0, import_log2.log)("closing...", {
1456
+ peerId: this.id,
1457
+ sessionId: connection.sessionId
1458
+ }, {
1459
+ F: __dxlog_file2,
1460
+ L: 326,
1461
+ S: this,
1462
+ C: (f, a) => f(...a)
1463
+ });
1464
+ await connection.close(err);
1465
+ (0, import_log2.log)("closed", {
1466
+ peerId: this.id,
1467
+ sessionId: connection.sessionId
1468
+ }, {
1469
+ F: __dxlog_file2,
1470
+ L: 332,
1471
+ S: this,
1472
+ C: (f, a) => f(...a)
1473
+ });
1474
+ }
1475
+ async onSignal(message) {
1476
+ if (!this.connection) {
1477
+ (0, import_log2.log)("dropping signal message for non-existent connection", {
1478
+ message
1479
+ }, {
1480
+ F: __dxlog_file2,
1481
+ L: 337,
1482
+ S: this,
1483
+ C: (f, a) => f(...a)
1484
+ });
1485
+ return;
1486
+ }
1487
+ await this.connection.signal(message);
1488
+ }
1489
+ async destroy() {
1490
+ await this._ctx.dispose();
1491
+ (0, import_log2.log)("Destroying peer", {
1492
+ peerId: this.id,
1493
+ topic: this.topic
1494
+ }, {
1495
+ F: __dxlog_file2,
1496
+ L: 347,
1497
+ S: this,
1498
+ C: (f, a) => f(...a)
1499
+ });
1500
+ await this?.connection?.close();
1501
+ }
1502
+ };
1503
+ _ts_decorate2([
1504
+ import_async2.synchronized
1505
+ ], Peer.prototype, "destroy", null);
1506
+ var increaseInterval = (interval) => {
1507
+ if (interval === 0) {
1508
+ return 50;
1509
+ } else if (interval < 500) {
1510
+ return 500;
1511
+ } else if (interval < 1e3) {
1512
+ return 1e3;
1513
+ } else if (interval < 5e3) {
1514
+ return 5e3;
1515
+ }
1516
+ return 1e4;
1517
+ };
1518
+
1519
+ // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1520
+ var import_context3 = require("@dxos/context");
1521
+ var import_invariant3 = require("@dxos/invariant");
1522
+ var import_keys3 = require("@dxos/keys");
1523
+ var import_log3 = require("@dxos/log");
1524
+ var import_protocols3 = require("@dxos/protocols");
1525
+ var import_util = require("@dxos/util");
1526
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1527
+ var SwarmMessage = import_protocols3.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1528
+ var SwarmMessenger = class {
1529
+ constructor({ sendMessage, onSignal, onOffer, topic }) {
1530
+ this._ctx = new import_context3.Context();
1531
+ this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1532
+ this._sendMessage = sendMessage;
1533
+ this._onSignal = onSignal;
1534
+ this._onOffer = onOffer;
1535
+ this._topic = topic;
1536
+ }
1537
+ async receiveMessage({ author, recipient, payload }) {
1538
+ if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1539
+ return;
1540
+ }
1541
+ const message = SwarmMessage.decode(payload.value);
1542
+ if (!this._topic.equals(message.topic)) {
1543
+ return;
1544
+ }
1545
+ (0, import_log3.log)("received", {
1546
+ from: author,
1547
+ to: recipient,
1548
+ msg: message
1549
+ }, {
1550
+ F: __dxlog_file3,
1551
+ L: 69,
1552
+ S: this,
1553
+ C: (f, a) => f(...a)
1554
+ });
1555
+ if (message.data?.offer) {
1556
+ await this._handleOffer({
1557
+ author,
1558
+ recipient,
1559
+ message
1560
+ });
1561
+ } else if (message.data?.answer) {
1562
+ await this._resolveAnswers(message);
1563
+ } else if (message.data?.signal) {
1564
+ await this._handleSignal({
1565
+ author,
1566
+ recipient,
1567
+ message
1568
+ });
1569
+ } else if (message.data?.signalBatch) {
1570
+ await this._handleSignal({
1571
+ author,
1572
+ recipient,
1573
+ message
1574
+ });
1575
+ } else {
1576
+ import_log3.log.warn("unknown message", {
1577
+ message
1578
+ }, {
1579
+ F: __dxlog_file3,
1580
+ L: 80,
1581
+ S: this,
1582
+ C: (f, a) => f(...a)
1583
+ });
1584
+ }
1585
+ }
1586
+ async signal(message) {
1587
+ (0, import_invariant3.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1588
+ F: __dxlog_file3,
1589
+ L: 85,
1590
+ S: this,
1591
+ A: [
1592
+ "message.data?.signal || message.data?.signalBatch",
1593
+ "'Invalid message'"
1594
+ ]
1595
+ });
1596
+ await this._sendReliableMessage({
1597
+ author: message.author,
1598
+ recipient: message.recipient,
1599
+ message
1600
+ });
1601
+ }
1602
+ async offer(message) {
1603
+ const networkMessage = {
1604
+ ...message,
1605
+ messageId: import_keys3.PublicKey.random()
1606
+ };
1607
+ return new Promise((resolve, reject) => {
1608
+ this._offerRecords.set(networkMessage.messageId, {
1609
+ resolve
1610
+ });
1611
+ this._sendReliableMessage({
1612
+ author: message.author,
1613
+ recipient: message.recipient,
1614
+ message: networkMessage
1615
+ }).catch((err) => reject(err));
1616
+ });
1617
+ }
1618
+ async _sendReliableMessage({ author, recipient, message }) {
1619
+ const networkMessage = {
1620
+ ...message,
1621
+ // Setting unique message_id if it not specified yet.
1622
+ messageId: message.messageId ?? import_keys3.PublicKey.random()
1623
+ };
1624
+ (0, import_log3.log)("sending", {
1625
+ from: author,
1626
+ to: recipient,
1627
+ msg: networkMessage
1628
+ }, {
1629
+ F: __dxlog_file3,
1630
+ L: 123,
1631
+ S: this,
1632
+ C: (f, a) => f(...a)
1633
+ });
1634
+ await this._sendMessage({
1635
+ author,
1636
+ recipient,
1637
+ payload: {
1638
+ type_url: "dxos.mesh.swarm.SwarmMessage",
1639
+ value: SwarmMessage.encode(networkMessage)
1640
+ }
1641
+ });
1642
+ }
1643
+ async _resolveAnswers(message) {
1644
+ (0, import_invariant3.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1645
+ F: __dxlog_file3,
1646
+ L: 135,
1647
+ S: this,
1648
+ A: [
1649
+ "message.data?.answer?.offerMessageId",
1650
+ "'No offerMessageId'"
1651
+ ]
1652
+ });
1653
+ const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1654
+ if (offerRecord) {
1655
+ this._offerRecords.delete(message.data.answer.offerMessageId);
1656
+ (0, import_invariant3.invariant)(message.data?.answer, "No answer", {
1638
1657
  F: __dxlog_file3,
1639
- L: 295,
1658
+ L: 139,
1640
1659
  S: this,
1641
- C: (f, a) => f(...a)
1660
+ A: [
1661
+ "message.data?.answer",
1662
+ "'No answer'"
1663
+ ]
1642
1664
  });
1643
- import_log3.log.trace("dxos.mesh.connection.error", {
1644
- topic: this.topic,
1645
- localPeerId: this.localPeerId,
1646
- remotePeerId: this.id,
1647
- sessionId,
1648
- initiator,
1649
- err
1665
+ (0, import_log3.log)("resolving", {
1666
+ answer: message.data.answer
1650
1667
  }, {
1651
1668
  F: __dxlog_file3,
1652
- L: 296,
1669
+ L: 140,
1653
1670
  S: this,
1654
1671
  C: (f, a) => f(...a)
1655
1672
  });
1656
- void this.closeConnection(err);
1657
- });
1658
- this.connection = connection;
1659
- return connection;
1660
- }
1661
- async closeConnection(err) {
1662
- if (!this.connection) {
1663
- return;
1673
+ offerRecord.resolve(message.data.answer);
1664
1674
  }
1665
- const connection = this.connection;
1666
- (0, import_log3.log)("closing...", {
1667
- peerId: this.id,
1668
- sessionId: connection.sessionId
1669
- }, {
1670
- F: __dxlog_file3,
1671
- L: 321,
1672
- S: this,
1673
- C: (f, a) => f(...a)
1674
- });
1675
- await connection.close(err);
1676
- (0, import_log3.log)("closed", {
1677
- peerId: this.id,
1678
- sessionId: connection.sessionId
1679
- }, {
1675
+ }
1676
+ async _handleOffer({ author, recipient, message }) {
1677
+ (0, import_invariant3.invariant)(message.data.offer, "No offer", {
1680
1678
  F: __dxlog_file3,
1681
- L: 327,
1679
+ L: 154,
1682
1680
  S: this,
1683
- C: (f, a) => f(...a)
1681
+ A: [
1682
+ "message.data.offer",
1683
+ "'No offer'"
1684
+ ]
1684
1685
  });
1685
- }
1686
- async onSignal(message) {
1687
- if (!this.connection) {
1688
- (0, import_log3.log)("dropping signal message for non-existent connection", {
1689
- message
1686
+ const offerMessage = {
1687
+ author,
1688
+ recipient,
1689
+ ...message,
1690
+ data: {
1691
+ offer: message.data.offer
1692
+ }
1693
+ };
1694
+ const answer = await this._onOffer(offerMessage);
1695
+ answer.offerMessageId = message.messageId;
1696
+ try {
1697
+ await this._sendReliableMessage({
1698
+ author: recipient,
1699
+ recipient: author,
1700
+ message: {
1701
+ topic: message.topic,
1702
+ sessionId: message.sessionId,
1703
+ data: {
1704
+ answer
1705
+ }
1706
+ }
1707
+ });
1708
+ } catch (err) {
1709
+ import_log3.log.warn("error sending answer", {
1710
+ err
1690
1711
  }, {
1691
1712
  F: __dxlog_file3,
1692
- L: 332,
1713
+ L: 174,
1693
1714
  S: this,
1694
1715
  C: (f, a) => f(...a)
1695
1716
  });
1696
- return;
1697
1717
  }
1698
- await this.connection.signal(message);
1699
1718
  }
1700
- async destroy() {
1701
- await this._ctx.dispose();
1702
- (0, import_log3.log)("Destroying peer", {
1703
- peerId: this.id,
1704
- topic: this.topic
1705
- }, {
1719
+ async _handleSignal({ author, recipient, message }) {
1720
+ (0, import_invariant3.invariant)(message.messageId, void 0, {
1706
1721
  F: __dxlog_file3,
1707
- L: 342,
1722
+ L: 187,
1708
1723
  S: this,
1709
- C: (f, a) => f(...a)
1724
+ A: [
1725
+ "message.messageId",
1726
+ ""
1727
+ ]
1710
1728
  });
1711
- await this?.connection?.close();
1712
- }
1713
- };
1714
- _ts_decorate2([
1715
- import_async2.synchronized
1716
- ], Peer.prototype, "destroy", null);
1717
- var increaseInterval = (interval) => {
1718
- if (interval === 0) {
1719
- return 50;
1720
- } else if (interval < 500) {
1721
- return 500;
1722
- } else if (interval < 1e3) {
1723
- return 1e3;
1724
- } else if (interval < 5e3) {
1725
- return 5e3;
1729
+ (0, import_invariant3.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1730
+ F: __dxlog_file3,
1731
+ L: 188,
1732
+ S: this,
1733
+ A: [
1734
+ "message.data.signal || message.data.signalBatch",
1735
+ "'Invalid message'"
1736
+ ]
1737
+ });
1738
+ const signalMessage = {
1739
+ author,
1740
+ recipient,
1741
+ ...message,
1742
+ data: {
1743
+ signal: message.data.signal,
1744
+ signalBatch: message.data.signalBatch
1745
+ }
1746
+ };
1747
+ await this._onSignal(signalMessage);
1726
1748
  }
1727
- return 1e4;
1728
1749
  };
1729
1750
 
1730
1751
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
@@ -1762,7 +1783,7 @@ var Swarm = class {
1762
1783
  this.disconnected = new import_async3.Event();
1763
1784
  this.connected = new import_async3.Event();
1764
1785
  this.errors = new import_debug2.ErrorStream();
1765
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.begin({
1786
+ import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.begin({
1766
1787
  id: this._instanceId,
1767
1788
  data: {
1768
1789
  topic: this._topic.toHex(),
@@ -1789,7 +1810,7 @@ var Swarm = class {
1789
1810
  onOffer: async (msg) => await this.onOffer(msg),
1790
1811
  topic: this._topic
1791
1812
  });
1792
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.end({
1813
+ import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.end({
1793
1814
  id: this._instanceId
1794
1815
  }), {
1795
1816
  F: __dxlog_file4,
@@ -2217,7 +2238,6 @@ var SwarmMapper = class {
2217
2238
  get peers() {
2218
2239
  return Array.from(this._peers.values());
2219
2240
  }
2220
- // prettier-ignore
2221
2241
  constructor(_swarm) {
2222
2242
  this._swarm = _swarm;
2223
2243
  this._subscriptions = new import_async4.EventSubscriptions();
@@ -2240,7 +2260,7 @@ var SwarmMapper = class {
2240
2260
  _update() {
2241
2261
  (0, import_log5.log)("updating swarm", void 0, {
2242
2262
  F: __dxlog_file5,
2243
- L: 75,
2263
+ L: 72,
2244
2264
  S: this,
2245
2265
  C: (f, a) => f(...a)
2246
2266
  });
@@ -2264,7 +2284,7 @@ var SwarmMapper = class {
2264
2284
  totalPeersInSwarm: this._peers.size
2265
2285
  }, {
2266
2286
  F: __dxlog_file5,
2267
- L: 116,
2287
+ L: 113,
2268
2288
  S: this,
2269
2289
  C: (f, a) => f(...a)
2270
2290
  });
@@ -2280,13 +2300,13 @@ var SwarmMapper = class {
2280
2300
  // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
2281
2301
  var import_async5 = require("@dxos/async");
2282
2302
  var import_context5 = require("@dxos/context");
2283
- var import_errors3 = require("@dxos/errors");
2284
2303
  var import_invariant5 = require("@dxos/invariant");
2285
2304
  var import_keys6 = require("@dxos/keys");
2286
2305
  var import_log6 = require("@dxos/log");
2306
+ var import_protocols5 = require("@dxos/protocols");
2287
2307
  var import_util4 = require("@dxos/util");
2288
2308
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2289
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
2309
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
2290
2310
  var ConnectionLimiter = class {
2291
2311
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2292
2312
  this._ctx = new import_context5.Context();
@@ -2353,7 +2373,7 @@ var ConnectionLimiter = class {
2353
2373
  if (!this._waitingPromises.has(sessionId)) {
2354
2374
  return;
2355
2375
  }
2356
- this._waitingPromises.get(sessionId).reject(new import_errors3.CancelledError());
2376
+ this._waitingPromises.get(sessionId).reject(new import_protocols5.CancelledError());
2357
2377
  this._waitingPromises.delete(sessionId);
2358
2378
  this.resolveWaitingPromises.schedule();
2359
2379
  }
@@ -2431,7 +2451,7 @@ var import_invariant6 = require("@dxos/invariant");
2431
2451
  var import_keys8 = require("@dxos/keys");
2432
2452
  var import_log7 = require("@dxos/log");
2433
2453
  var import_messaging = require("@dxos/messaging");
2434
- var import_protocols4 = require("@dxos/protocols");
2454
+ var import_protocols6 = require("@dxos/protocols");
2435
2455
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
2436
2456
  var import_util6 = require("@dxos/util");
2437
2457
  var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
@@ -2479,7 +2499,7 @@ var NetworkManager = class {
2479
2499
  return this._swarms.get(topic);
2480
2500
  }
2481
2501
  async open() {
2482
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
2502
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.begin({
2483
2503
  id: this._instanceId
2484
2504
  }), {
2485
2505
  F: __dxlog_file7,
@@ -2489,7 +2509,7 @@ var NetworkManager = class {
2489
2509
  });
2490
2510
  await this._messenger.open();
2491
2511
  await this._signalManager.open();
2492
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
2512
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.end({
2493
2513
  id: this._instanceId
2494
2514
  }), {
2495
2515
  F: __dxlog_file7,
@@ -2824,7 +2844,6 @@ var import_invariant9 = require("@dxos/invariant");
2824
2844
  var import_log9 = require("@dxos/log");
2825
2845
  var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2826
2846
  var StarTopology = class {
2827
- // prettier-ignore
2828
2847
  constructor(_centralPeer) {
2829
2848
  this._centralPeer = _centralPeer;
2830
2849
  }
@@ -2834,7 +2853,7 @@ var StarTopology = class {
2834
2853
  init(controller) {
2835
2854
  (0, import_invariant9.invariant)(!this._controller, "Already initialized.", {
2836
2855
  F: __dxlog_file10,
2837
- L: 24,
2856
+ L: 21,
2838
2857
  S: this,
2839
2858
  A: [
2840
2859
  "!this._controller",
@@ -2846,7 +2865,7 @@ var StarTopology = class {
2846
2865
  update() {
2847
2866
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2848
2867
  F: __dxlog_file10,
2849
- L: 29,
2868
+ L: 26,
2850
2869
  S: this,
2851
2870
  A: [
2852
2871
  "this._controller",
@@ -2857,7 +2876,7 @@ var StarTopology = class {
2857
2876
  if (!ownPeerId.equals(this._centralPeer)) {
2858
2877
  (0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
2859
2878
  F: __dxlog_file10,
2860
- L: 32,
2879
+ L: 29,
2861
2880
  S: this,
2862
2881
  C: (f, a) => f(...a)
2863
2882
  });
@@ -2867,7 +2886,7 @@ var StarTopology = class {
2867
2886
  peer
2868
2887
  }, {
2869
2888
  F: __dxlog_file10,
2870
- L: 37,
2889
+ L: 34,
2871
2890
  S: this,
2872
2891
  C: (f, a) => f(...a)
2873
2892
  });
@@ -2881,7 +2900,7 @@ var StarTopology = class {
2881
2900
  peer
2882
2901
  }, {
2883
2902
  F: __dxlog_file10,
2884
- L: 46,
2903
+ L: 43,
2885
2904
  S: this,
2886
2905
  C: (f, a) => f(...a)
2887
2906
  });
@@ -2892,7 +2911,7 @@ var StarTopology = class {
2892
2911
  async onOffer(peer) {
2893
2912
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2894
2913
  F: __dxlog_file10,
2895
- L: 53,
2914
+ L: 50,
2896
2915
  S: this,
2897
2916
  A: [
2898
2917
  "this._controller",
@@ -2906,7 +2925,7 @@ var StarTopology = class {
2906
2925
  isSelfCentral: ownPeerId.equals(this._centralPeer)
2907
2926
  }, {
2908
2927
  F: __dxlog_file10,
2909
- L: 55,
2928
+ L: 52,
2910
2929
  S: this,
2911
2930
  C: (f, a) => f(...a)
2912
2931
  });
@@ -3117,10 +3136,9 @@ var import_simple_peer = __toESM(require("simple-peer"));
3117
3136
  var import_tiny_invariant = __toESM(require("tiny-invariant"));
3118
3137
  var import_async9 = require("@dxos/async");
3119
3138
  var import_debug5 = require("@dxos/debug");
3120
- var import_errors4 = require("@dxos/errors");
3121
3139
  var import_keys10 = require("@dxos/keys");
3122
3140
  var import_log11 = require("@dxos/log");
3123
- var import_protocols5 = require("@dxos/protocols");
3141
+ var import_protocols7 = require("@dxos/protocols");
3124
3142
 
3125
3143
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
3126
3144
  var wrtc = null;
@@ -3139,17 +3157,17 @@ var SimplePeerTransport = class {
3139
3157
  this.connected = new import_async9.Event();
3140
3158
  this.errors = new import_debug5.ErrorStream();
3141
3159
  this._instanceId = import_keys10.PublicKey.random().toHex();
3142
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
3160
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.begin({
3143
3161
  id: this._instanceId
3144
3162
  }), {
3145
3163
  F: __dxlog_file12,
3146
- L: 41,
3164
+ L: 40,
3147
3165
  S: this,
3148
3166
  C: (f, a) => f(...a)
3149
3167
  });
3150
3168
  (0, import_log11.log)("created connection", params, {
3151
3169
  F: __dxlog_file12,
3152
- L: 42,
3170
+ L: 41,
3153
3171
  S: this,
3154
3172
  C: (f, a) => f(...a)
3155
3173
  });
@@ -3162,7 +3180,7 @@ var SimplePeerTransport = class {
3162
3180
  this._peer.on("signal", async (data) => {
3163
3181
  (0, import_log11.log)("signal", data, {
3164
3182
  F: __dxlog_file12,
3165
- L: 51,
3183
+ L: 50,
3166
3184
  S: this,
3167
3185
  C: (f, a) => f(...a)
3168
3186
  });
@@ -3175,7 +3193,7 @@ var SimplePeerTransport = class {
3175
3193
  this._peer.on("connect", () => {
3176
3194
  (0, import_log11.log)("connected", void 0, {
3177
3195
  F: __dxlog_file12,
3178
- L: 56,
3196
+ L: 55,
3179
3197
  S: this,
3180
3198
  C: (f, a) => f(...a)
3181
3199
  });
@@ -3185,7 +3203,7 @@ var SimplePeerTransport = class {
3185
3203
  this._peer.on("close", async () => {
3186
3204
  (0, import_log11.log)("closed", void 0, {
3187
3205
  F: __dxlog_file12,
3188
- L: 62,
3206
+ L: 61,
3189
3207
  S: this,
3190
3208
  C: (f, a) => f(...a)
3191
3209
  });
@@ -3195,42 +3213,56 @@ var SimplePeerTransport = class {
3195
3213
  this._peer.on("error", async (err) => {
3196
3214
  if (typeof RTCError !== "undefined" && err instanceof RTCError) {
3197
3215
  if (err.errorDetail === "sctp-failure") {
3198
- this.errors.raise(new import_errors4.ConnectionResetError("sctp-failure from RTCError", err));
3216
+ this.errors.raise(new import_protocols7.ConnectionResetError("sctp-failure from RTCError", err));
3199
3217
  } else {
3200
- this.errors.raise(new import_errors4.UnknownProtocolError("unknown RTCError", err));
3218
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown RTCError", err));
3201
3219
  }
3202
- } else if (err.name === "InvalidStateError") {
3203
- this.errors.raise(new import_errors4.ConnectionResetError("safari WebRTC error", err));
3204
3220
  } else if ("code" in err) {
3221
+ import_log11.log.info("simple-peer error", err, {
3222
+ F: __dxlog_file12,
3223
+ L: 77,
3224
+ S: this,
3225
+ C: (f, a) => f(...a)
3226
+ });
3205
3227
  switch (err.code) {
3206
3228
  case "ERR_WEBRTC_SUPPORT":
3207
- this.errors.raise(new import_errors4.ProtocolError("WebRTC not supported", err));
3229
+ this.errors.raise(new import_protocols7.ProtocolError("WebRTC not supported", err));
3230
+ break;
3208
3231
  case "ERR_ICE_CONNECTION_FAILURE":
3209
3232
  case "ERR_DATA_CHANNEL":
3210
3233
  case "ERR_CONNECTION_FAILURE":
3211
3234
  case "ERR_SIGNALING":
3212
- this.errors.raise(new import_errors4.ConnectivityError("unknown communication failure", err));
3235
+ this.errors.raise(new import_protocols7.ConnectivityError("unknown communication failure", err));
3236
+ break;
3213
3237
  case "ERR_CREATE_OFFER":
3214
3238
  case "ERR_CREATE_ANSWER":
3215
3239
  case "ERR_SET_LOCAL_DESCRIPTION":
3216
3240
  case "ERR_SET_REMOTE_DESCRIPTION":
3217
3241
  case "ERR_ADD_ICE_CANDIDATE":
3218
- this.errors.raise(new import_errors4.UnknownProtocolError("unknown simple-peer library failure", err));
3242
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown simple-peer library failure", err));
3243
+ break;
3219
3244
  default:
3220
3245
  this.errors.raise(new Error("unknown simple-peer error"));
3246
+ break;
3221
3247
  }
3222
3248
  } else {
3249
+ import_log11.log.info("unknown peer connection error", err, {
3250
+ F: __dxlog_file12,
3251
+ L: 101,
3252
+ S: this,
3253
+ C: (f, a) => f(...a)
3254
+ });
3223
3255
  this.errors.raise(err);
3224
3256
  }
3225
3257
  try {
3226
- if (typeof this._peer?._pc.getStats === "function") {
3258
+ if (typeof this._peer?._pc?.getStats === "function") {
3227
3259
  this._peer._pc.getStats().then((stats) => {
3228
3260
  import_log11.log.warn("report after webrtc error", {
3229
3261
  config: this.params.webrtcConfig,
3230
3262
  stats
3231
3263
  }, {
3232
3264
  F: __dxlog_file12,
3233
- L: 107,
3265
+ L: 109,
3234
3266
  S: this,
3235
3267
  C: (f, a) => f(...a)
3236
3268
  });
@@ -3239,18 +3271,18 @@ var SimplePeerTransport = class {
3239
3271
  } catch (err2) {
3240
3272
  import_log11.log.catch(err2, void 0, {
3241
3273
  F: __dxlog_file12,
3242
- L: 114,
3274
+ L: 116,
3243
3275
  S: this,
3244
3276
  C: (f, a) => f(...a)
3245
3277
  });
3246
3278
  }
3247
3279
  await this.destroy();
3248
3280
  });
3249
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
3281
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.end({
3250
3282
  id: this._instanceId
3251
3283
  }), {
3252
3284
  F: __dxlog_file12,
3253
- L: 119,
3285
+ L: 121,
3254
3286
  S: this,
3255
3287
  C: (f, a) => f(...a)
3256
3288
  });
@@ -3258,7 +3290,7 @@ var SimplePeerTransport = class {
3258
3290
  async destroy() {
3259
3291
  (0, import_log11.log)("closing...", void 0, {
3260
3292
  F: __dxlog_file12,
3261
- L: 123,
3293
+ L: 125,
3262
3294
  S: this,
3263
3295
  C: (f, a) => f(...a)
3264
3296
  });
@@ -3268,7 +3300,7 @@ var SimplePeerTransport = class {
3268
3300
  this.closed.emit();
3269
3301
  (0, import_log11.log)("closed", void 0, {
3270
3302
  F: __dxlog_file12,
3271
- L: 128,
3303
+ L: 130,
3272
3304
  S: this,
3273
3305
  C: (f, a) => f(...a)
3274
3306
  });
@@ -3301,7 +3333,6 @@ var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
3301
3333
  var import_util8 = require("@dxos/util");
3302
3334
  var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3303
3335
  var SimplePeerTransportService = class {
3304
- // prettier-ignore
3305
3336
  constructor(_webrtcConfig) {
3306
3337
  this._webrtcConfig = _webrtcConfig;
3307
3338
  this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
@@ -3371,7 +3402,8 @@ var SimplePeerTransportService = class {
3371
3402
  const transportState = {
3372
3403
  transport,
3373
3404
  stream: duplex,
3374
- writeCallbacks: []
3405
+ writeCallbacks: [],
3406
+ state: "OPEN"
3375
3407
  };
3376
3408
  ready();
3377
3409
  this.transports.set(request.proxyId, transportState);
@@ -3381,7 +3413,7 @@ var SimplePeerTransportService = class {
3381
3413
  async sendSignal({ proxyId, signal }) {
3382
3414
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3383
3415
  F: __dxlog_file13,
3384
- L: 113,
3416
+ L: 112,
3385
3417
  S: this,
3386
3418
  A: [
3387
3419
  "this.transports.has(proxyId)",
@@ -3391,9 +3423,17 @@ var SimplePeerTransportService = class {
3391
3423
  await this.transports.get(proxyId).transport.signal(signal);
3392
3424
  }
3393
3425
  async sendData({ proxyId, payload }) {
3426
+ if (this.transports.get(proxyId)?.state !== "OPEN") {
3427
+ import_log12.log.debug("transport is closed", void 0, {
3428
+ F: __dxlog_file13,
3429
+ L: 118,
3430
+ S: this,
3431
+ C: (f, a) => f(...a)
3432
+ });
3433
+ }
3394
3434
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3395
3435
  F: __dxlog_file13,
3396
- L: 118,
3436
+ L: 120,
3397
3437
  S: this,
3398
3438
  A: [
3399
3439
  "this.transports.has(proxyId)",
@@ -3411,10 +3451,12 @@ var SimplePeerTransportService = class {
3411
3451
  async close({ proxyId }) {
3412
3452
  await this.transports.get(proxyId)?.transport.destroy();
3413
3453
  await this.transports.get(proxyId)?.stream.end();
3414
- this.transports.delete(proxyId);
3454
+ if (this.transports.get(proxyId)) {
3455
+ this.transports.get(proxyId).state = "CLOSED";
3456
+ }
3415
3457
  (0, import_log12.log)("Closed.", void 0, {
3416
3458
  F: __dxlog_file13,
3417
- L: 132,
3459
+ L: 136,
3418
3460
  S: this,
3419
3461
  C: (f, a) => f(...a)
3420
3462
  });
@@ -3426,15 +3468,14 @@ var import_node_stream3 = require("node:stream");
3426
3468
  var import_async10 = require("@dxos/async");
3427
3469
  var import_context6 = require("@dxos/context");
3428
3470
  var import_debug6 = require("@dxos/debug");
3429
- var import_errors5 = require("@dxos/errors");
3430
3471
  var import_invariant12 = require("@dxos/invariant");
3431
3472
  var import_keys12 = require("@dxos/keys");
3432
3473
  var import_log13 = require("@dxos/log");
3474
+ var import_protocols8 = require("@dxos/protocols");
3433
3475
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
3434
3476
  var import_util9 = require("@dxos/util");
3435
3477
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3436
3478
  var SimplePeerTransportProxy = class {
3437
- // prettier-ignore
3438
3479
  constructor(_params) {
3439
3480
  this._params = _params;
3440
3481
  this._proxyId = import_keys12.PublicKey.random();
@@ -3451,7 +3492,7 @@ var SimplePeerTransportProxy = class {
3451
3492
  this._serviceStream.subscribe(async (event) => {
3452
3493
  (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
3453
3494
  F: __dxlog_file14,
3454
- L: 58,
3495
+ L: 55,
3455
3496
  S: this,
3456
3497
  C: (f, a) => f(...a)
3457
3498
  });
@@ -3480,7 +3521,7 @@ var SimplePeerTransportProxy = class {
3480
3521
  }));
3481
3522
  }, (error) => import_log13.log.catch(error, void 0, {
3482
3523
  F: __dxlog_file14,
3483
- L: 82,
3524
+ L: 83,
3484
3525
  S: this,
3485
3526
  C: (f, a) => f(...a)
3486
3527
  }));
@@ -3526,7 +3567,7 @@ var SimplePeerTransportProxy = class {
3526
3567
  } catch (err) {
3527
3568
  import_log13.log.catch(err, void 0, {
3528
3569
  F: __dxlog_file14,
3529
- L: 133,
3570
+ L: 134,
3530
3571
  S: this,
3531
3572
  C: (f, a) => f(...a)
3532
3573
  });
@@ -3562,7 +3603,7 @@ var SimplePeerTransportProxyFactory = class {
3562
3603
  createTransport(options) {
3563
3604
  (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3564
3605
  F: __dxlog_file14,
3565
- L: 170,
3606
+ L: 171,
3566
3607
  S: this,
3567
3608
  A: [
3568
3609
  "this._bridgeService",
@@ -3580,16 +3621,16 @@ var SimplePeerTransportProxyFactory = class {
3580
3621
  };
3581
3622
  var decodeError = (err) => {
3582
3623
  const message = typeof err === "string" ? err : err.message;
3583
- if (message.includes("ConnectionResetError")) {
3584
- return new import_errors5.ConnectionResetError(message);
3585
- } else if (message.includes("TimeoutError")) {
3586
- return new import_errors5.TimeoutError(message);
3587
- } else if (message.includes("ProtocolError")) {
3588
- return new import_errors5.ProtocolError(message);
3589
- } else if (message.includes("ConnectivityError")) {
3590
- return new import_errors5.ConnectivityError(message);
3591
- } else if (message.includes("UnknownProtocolError")) {
3592
- return new import_errors5.UnknownProtocolError(message);
3624
+ if (message.includes("CONNECTION_RESET")) {
3625
+ return new import_protocols8.ConnectionResetError(message);
3626
+ } else if (message.includes("TIMEOUT")) {
3627
+ return new import_protocols8.TimeoutError(message);
3628
+ } else if (message.includes("PROTOCOL_ERROR")) {
3629
+ return new import_protocols8.ProtocolError(message);
3630
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3631
+ return new import_protocols8.ConnectivityError(message);
3632
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3633
+ return new import_protocols8.UnknownProtocolError(message);
3593
3634
  } else {
3594
3635
  return typeof err === "string" ? new Error(err) : err;
3595
3636
  }
@@ -3937,6 +3978,136 @@ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3937
3978
  })
3938
3979
  });
3939
3980
 
3981
+ // packages/core/mesh/network-manager/src/transport/tcp-transport.ts
3982
+ var import_node_net = require("node:net");
3983
+ var import_async12 = require("@dxos/async");
3984
+ var import_debug8 = require("@dxos/debug");
3985
+ var import_log15 = require("@dxos/log");
3986
+ var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/tcp-transport.ts";
3987
+ var TcpTransportFactory = {
3988
+ createTransport: (params) => new TcpTransport(params)
3989
+ };
3990
+ var TcpTransport = class {
3991
+ constructor(options) {
3992
+ this.options = options;
3993
+ this._server = void 0;
3994
+ this._socket = void 0;
3995
+ this.closed = new import_async12.Event();
3996
+ this.connected = new import_async12.Event();
3997
+ this.errors = new import_debug8.ErrorStream();
3998
+ this._destroyed = false;
3999
+ this._connected = false;
4000
+ (0, import_log15.log)("creating", void 0, {
4001
+ F: __dxlog_file16,
4002
+ L: 34,
4003
+ S: this,
4004
+ C: (f, a) => f(...a)
4005
+ });
4006
+ if (this.options.initiator) {
4007
+ setTimeout(async () => {
4008
+ const { Server } = await import("node:net");
4009
+ this._server = new Server((socket) => {
4010
+ (0, import_log15.log)("new connection", void 0, {
4011
+ F: __dxlog_file16,
4012
+ L: 42,
4013
+ S: this,
4014
+ C: (f, a) => f(...a)
4015
+ });
4016
+ if (this._connected) {
4017
+ socket.destroy();
4018
+ }
4019
+ this._handleSocket(socket);
4020
+ });
4021
+ this._server.on("listening", () => {
4022
+ const { port } = this._server.address();
4023
+ (0, import_log15.log)("listening", {
4024
+ port
4025
+ }, {
4026
+ F: __dxlog_file16,
4027
+ L: 51,
4028
+ S: this,
4029
+ C: (f, a) => f(...a)
4030
+ });
4031
+ void this.options.sendSignal({
4032
+ payload: {
4033
+ port
4034
+ }
4035
+ }).catch((err) => {
4036
+ if (!this._destroyed) {
4037
+ this.errors.raise(err);
4038
+ }
4039
+ });
4040
+ });
4041
+ this._server.on("error", (err) => {
4042
+ this.errors.raise(err);
4043
+ });
4044
+ this._server.listen(0);
4045
+ });
4046
+ }
4047
+ }
4048
+ async destroy() {
4049
+ (0, import_log15.log)("closing", void 0, {
4050
+ F: __dxlog_file16,
4051
+ L: 72,
4052
+ S: this,
4053
+ C: (f, a) => f(...a)
4054
+ });
4055
+ this._socket?.destroy();
4056
+ this._server?.close();
4057
+ this._destroyed = true;
4058
+ }
4059
+ signal({ payload }) {
4060
+ (0, import_log15.log)("received signal", {
4061
+ payload
4062
+ }, {
4063
+ F: __dxlog_file16,
4064
+ L: 80,
4065
+ S: this,
4066
+ C: (f, a) => f(...a)
4067
+ });
4068
+ if (this.options.initiator || this._connected) {
4069
+ return;
4070
+ }
4071
+ const socket = new import_node_net.Socket();
4072
+ this._handleSocket(socket);
4073
+ socket.connect({
4074
+ port: payload.port,
4075
+ host: "localhost"
4076
+ });
4077
+ }
4078
+ _handleSocket(socket) {
4079
+ (0, import_log15.log)("handling socket", {
4080
+ remotePort: socket.remotePort,
4081
+ localPort: socket.localPort
4082
+ }, {
4083
+ F: __dxlog_file16,
4084
+ L: 91,
4085
+ S: this,
4086
+ C: (f, a) => f(...a)
4087
+ });
4088
+ this._socket = socket;
4089
+ this.connected.emit();
4090
+ this.options.stream.pipe(this._socket).pipe(this.options.stream);
4091
+ this._socket.on("connect", () => {
4092
+ (0, import_log15.log)("connected to", {
4093
+ port: this._socket?.remotePort
4094
+ }, {
4095
+ F: __dxlog_file16,
4096
+ L: 97,
4097
+ S: this,
4098
+ C: (f, a) => f(...a)
4099
+ });
4100
+ this._connected = true;
4101
+ });
4102
+ this._socket.on("error", (err) => {
4103
+ this.errors.raise(err);
4104
+ });
4105
+ this._socket.on("close", () => {
4106
+ this.closed.emit();
4107
+ });
4108
+ }
4109
+ };
4110
+
3940
4111
  // packages/core/mesh/network-manager/src/wire-protocol.ts
3941
4112
  var import_teleport = require("@dxos/teleport");
3942
4113
  var createTeleportProtocolFactory = (onConnection) => {
@@ -3982,6 +4153,8 @@ var createTeleportProtocolFactory = (onConnection) => {
3982
4153
  Swarm,
3983
4154
  SwarmMapper,
3984
4155
  SwarmMessenger,
4156
+ TcpTransport,
4157
+ TcpTransportFactory,
3985
4158
  TransportKind,
3986
4159
  cleanup,
3987
4160
  createLibDataChannelTransportFactory,