@dxos/network-manager 0.1.58-main.f5feb2a → 0.1.58-main.f69fa2f

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 (47) hide show
  1. package/dist/lib/browser/{chunk-CNQ55TFQ.mjs → chunk-3KLH55NB.mjs} +144 -81
  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 +598 -423
  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 +931 -889
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  13. package/dist/types/src/swarm/peer.d.ts +2 -2
  14. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  15. package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
  16. package/dist/types/src/swarm/swarm.d.ts +2 -2
  17. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  18. package/dist/types/src/testing/test-builder.d.ts +1 -1
  19. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  20. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  21. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  22. package/dist/types/src/transport/index.d.ts +1 -0
  23. package/dist/types/src/transport/index.d.ts.map +1 -1
  24. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  25. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  26. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  27. package/dist/types/src/transport/tcp-transport.browser.d.ts +12 -0
  28. package/dist/types/src/transport/tcp-transport.browser.d.ts.map +1 -0
  29. package/package.json +18 -17
  30. package/src/swarm/connection.test.ts +1 -1
  31. package/src/swarm/connection.ts +4 -3
  32. package/src/swarm/peer.ts +7 -2
  33. package/src/swarm/swarm-mapper.ts +4 -7
  34. package/src/swarm/swarm.test.ts +3 -3
  35. package/src/swarm/swarm.ts +3 -3
  36. package/src/testing/test-builder.ts +1 -1
  37. package/src/tests/basic-test-suite.ts +1 -1
  38. package/src/tests/memory-transport.test.ts +2 -2
  39. package/src/tests/tcp-transport.test.ts +2 -2
  40. package/src/tests/webrtc-transport.test.ts +1 -1
  41. package/src/topology/star-topology.ts +1 -4
  42. package/src/transport/index.ts +1 -0
  43. package/src/transport/simplepeer-transport-proxy.ts +25 -24
  44. package/src/transport/simplepeer-transport-service.ts +9 -5
  45. package/src/transport/simplepeer-transport.ts +8 -5
  46. package/src/transport/tcp-transport.browser.ts +28 -0
  47. package/dist/lib/browser/chunk-CNQ55TFQ.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,
@@ -735,7 +737,7 @@ var Connection = class {
735
737
  this.closeReason = err?.message;
736
738
  }
737
739
  if (err instanceof import_protocols.ConnectionResetError) {
738
- (0, import_log.log)("aborting due to transport ConnectionResetError", void 0, {
740
+ import_log.log.warn("aborting due to transport ConnectionResetError", void 0, {
739
741
  F: __dxlog_file,
740
742
  L: 197,
741
743
  S: this,
@@ -743,7 +745,7 @@ var Connection = class {
743
745
  });
744
746
  this.abort().catch((err2) => this.errors.raise(err2));
745
747
  } else if (err instanceof import_protocols.ConnectivityError) {
746
- (0, import_log.log)("aborting due to transport ConnectivityError", void 0, {
748
+ import_log.log.warn("aborting due to transport ConnectivityError", void 0, {
747
749
  F: __dxlog_file,
748
750
  L: 200,
749
751
  S: this,
@@ -751,7 +753,7 @@ var Connection = class {
751
753
  });
752
754
  this.abort().catch((err2) => this.errors.raise(err2));
753
755
  } else if (err instanceof import_protocols.UnknownProtocolError) {
754
- (0, import_log.log)("unsure what to do for UnknownProtocolError", {
756
+ import_log.log.warn("unsure what to do with UnknownProtocolError, will keep on truckin", {
755
757
  err
756
758
  }, {
757
759
  F: __dxlog_file,
@@ -782,14 +784,14 @@ var Connection = class {
782
784
  err
783
785
  }, {
784
786
  F: __dxlog_file,
785
- L: 224,
787
+ L: 225,
786
788
  S: this,
787
789
  C: (f, a) => f(...a)
788
790
  });
789
791
  if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
790
792
  (0, import_log.log)(`abort ignored: already ${this._state}`, this.closeReason, {
791
793
  F: __dxlog_file,
792
- L: 226,
794
+ L: 227,
793
795
  S: this,
794
796
  C: (f, a) => f(...a)
795
797
  });
@@ -805,7 +807,7 @@ var Connection = class {
805
807
  proto: this._protocol
806
808
  }, {
807
809
  F: __dxlog_file,
808
- L: 239,
810
+ L: 240,
809
811
  S: this,
810
812
  C: (f, a) => f(...a)
811
813
  });
@@ -813,7 +815,7 @@ var Connection = class {
813
815
  } catch (err2) {
814
816
  import_log.log.catch(err2, void 0, {
815
817
  F: __dxlog_file,
816
- L: 242,
818
+ L: 243,
817
819
  S: this,
818
820
  C: (f, a) => f(...a)
819
821
  });
@@ -823,7 +825,7 @@ var Connection = class {
823
825
  } catch (err2) {
824
826
  import_log.log.catch(err2, void 0, {
825
827
  F: __dxlog_file,
826
- L: 249,
828
+ L: 250,
827
829
  S: this,
828
830
  C: (f, a) => f(...a)
829
831
  });
@@ -833,7 +835,7 @@ var Connection = class {
833
835
  } catch (err2) {
834
836
  import_log.log.catch(err2, void 0, {
835
837
  F: __dxlog_file,
836
- L: 255,
838
+ L: 256,
837
839
  S: this,
838
840
  C: (f, a) => f(...a)
839
841
  });
@@ -854,7 +856,7 @@ var Connection = class {
854
856
  peerId: this.ownId
855
857
  }, {
856
858
  F: __dxlog_file,
857
- L: 277,
859
+ L: 278,
858
860
  S: this,
859
861
  C: (f, a) => f(...a)
860
862
  });
@@ -864,7 +866,7 @@ var Connection = class {
864
866
  } catch (err2) {
865
867
  import_log.log.catch(err2, void 0, {
866
868
  F: __dxlog_file,
867
- L: 284,
869
+ L: 285,
868
870
  S: this,
869
871
  C: (f, a) => f(...a)
870
872
  });
@@ -874,7 +876,7 @@ var Connection = class {
874
876
  } catch (err2) {
875
877
  import_log.log.catch(err2, void 0, {
876
878
  F: __dxlog_file,
877
- L: 291,
879
+ L: 292,
878
880
  S: this,
879
881
  C: (f, a) => f(...a)
880
882
  });
@@ -884,7 +886,7 @@ var Connection = class {
884
886
  peerId: this.ownId
885
887
  }, {
886
888
  F: __dxlog_file,
887
- L: 295,
889
+ L: 296,
888
890
  S: this,
889
891
  C: (f, a) => f(...a)
890
892
  });
@@ -927,7 +929,7 @@ var Connection = class {
927
929
  err
928
930
  }, {
929
931
  F: __dxlog_file,
930
- L: 332,
932
+ L: 333,
931
933
  S: this,
932
934
  C: (f, a) => f(...a)
933
935
  });
@@ -940,7 +942,7 @@ var Connection = class {
940
942
  async signal(msg) {
941
943
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
942
944
  F: __dxlog_file,
943
- L: 341,
945
+ L: 342,
944
946
  S: this,
945
947
  A: [
946
948
  "msg.sessionId",
@@ -950,7 +952,7 @@ var Connection = class {
950
952
  if (!msg.sessionId.equals(this.sessionId)) {
951
953
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
952
954
  F: __dxlog_file,
953
- L: 343,
955
+ L: 344,
954
956
  S: this,
955
957
  C: (f, a) => f(...a)
956
958
  });
@@ -958,7 +960,7 @@ var Connection = class {
958
960
  }
959
961
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
960
962
  F: __dxlog_file,
961
- L: 346,
963
+ L: 347,
962
964
  S: this,
963
965
  A: [
964
966
  "msg.data.signal || msg.data.signalBatch",
@@ -967,7 +969,7 @@ var Connection = class {
967
969
  });
968
970
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
969
971
  F: __dxlog_file,
970
- L: 347,
972
+ L: 348,
971
973
  S: this,
972
974
  A: [
973
975
  "msg.author?.equals(this.remoteId)",
@@ -976,7 +978,7 @@ var Connection = class {
976
978
  });
977
979
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
978
980
  F: __dxlog_file,
979
- L: 348,
981
+ L: 349,
980
982
  S: this,
981
983
  A: [
982
984
  "msg.recipient?.equals(this.ownId)",
@@ -1000,7 +1002,7 @@ var Connection = class {
1000
1002
  msg: msg.data
1001
1003
  }, {
1002
1004
  F: __dxlog_file,
1003
- L: 357,
1005
+ L: 358,
1004
1006
  S: this,
1005
1007
  C: (f, a) => f(...a)
1006
1008
  });
@@ -1008,7 +1010,7 @@ var Connection = class {
1008
1010
  } else {
1009
1011
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
1010
1012
  F: __dxlog_file,
1011
- L: 360,
1013
+ L: 361,
1012
1014
  S: this,
1013
1015
  A: [
1014
1016
  "this._transport",
@@ -1021,7 +1023,7 @@ var Connection = class {
1021
1023
  msg: msg.data
1022
1024
  }, {
1023
1025
  F: __dxlog_file,
1024
- L: 361,
1026
+ L: 362,
1025
1027
  S: this,
1026
1028
  C: (f, a) => f(...a)
1027
1029
  });
@@ -1039,13 +1041,13 @@ var Connection = class {
1039
1041
  peerId: this.ownId
1040
1042
  }, {
1041
1043
  F: __dxlog_file,
1042
- L: 372,
1044
+ L: 373,
1043
1045
  S: this,
1044
1046
  C: (f, a) => f(...a)
1045
1047
  });
1046
1048
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
1047
1049
  F: __dxlog_file,
1048
- L: 373,
1050
+ L: 374,
1049
1051
  S: this,
1050
1052
  A: [
1051
1053
  "state !== this._state",
@@ -1073,245 +1075,13 @@ var import_log4 = require("@dxos/log");
1073
1075
  var import_protocols4 = require("@dxos/protocols");
1074
1076
  var import_util2 = require("@dxos/util");
1075
1077
 
1076
- // 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");
1077
1080
  var import_context2 = require("@dxos/context");
1078
1081
  var import_invariant2 = require("@dxos/invariant");
1079
1082
  var import_keys2 = require("@dxos/keys");
1080
1083
  var import_log2 = require("@dxos/log");
1081
1084
  var import_protocols2 = require("@dxos/protocols");
1082
- var import_util = require("@dxos/util");
1083
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1084
- var SwarmMessage = import_protocols2.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1085
- var SwarmMessenger = class {
1086
- constructor({ sendMessage, onSignal, onOffer, topic }) {
1087
- this._ctx = new import_context2.Context();
1088
- this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1089
- this._sendMessage = sendMessage;
1090
- this._onSignal = onSignal;
1091
- this._onOffer = onOffer;
1092
- this._topic = topic;
1093
- }
1094
- async receiveMessage({ author, recipient, payload }) {
1095
- if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1096
- return;
1097
- }
1098
- const message = SwarmMessage.decode(payload.value);
1099
- if (!this._topic.equals(message.topic)) {
1100
- return;
1101
- }
1102
- (0, import_log2.log)("received", {
1103
- from: author,
1104
- to: recipient,
1105
- msg: message
1106
- }, {
1107
- F: __dxlog_file2,
1108
- L: 69,
1109
- S: this,
1110
- C: (f, a) => f(...a)
1111
- });
1112
- if (message.data?.offer) {
1113
- await this._handleOffer({
1114
- author,
1115
- recipient,
1116
- message
1117
- });
1118
- } else if (message.data?.answer) {
1119
- await this._resolveAnswers(message);
1120
- } else if (message.data?.signal) {
1121
- await this._handleSignal({
1122
- author,
1123
- recipient,
1124
- message
1125
- });
1126
- } else if (message.data?.signalBatch) {
1127
- await this._handleSignal({
1128
- author,
1129
- recipient,
1130
- message
1131
- });
1132
- } else {
1133
- import_log2.log.warn("unknown message", {
1134
- message
1135
- }, {
1136
- F: __dxlog_file2,
1137
- L: 80,
1138
- S: this,
1139
- C: (f, a) => f(...a)
1140
- });
1141
- }
1142
- }
1143
- async signal(message) {
1144
- (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1145
- F: __dxlog_file2,
1146
- L: 85,
1147
- S: this,
1148
- A: [
1149
- "message.data?.signal || message.data?.signalBatch",
1150
- "'Invalid message'"
1151
- ]
1152
- });
1153
- await this._sendReliableMessage({
1154
- author: message.author,
1155
- recipient: message.recipient,
1156
- message
1157
- });
1158
- }
1159
- async offer(message) {
1160
- const networkMessage = {
1161
- ...message,
1162
- messageId: import_keys2.PublicKey.random()
1163
- };
1164
- return new Promise((resolve, reject) => {
1165
- this._offerRecords.set(networkMessage.messageId, {
1166
- resolve
1167
- });
1168
- this._sendReliableMessage({
1169
- author: message.author,
1170
- recipient: message.recipient,
1171
- message: networkMessage
1172
- }).catch((err) => reject(err));
1173
- });
1174
- }
1175
- async _sendReliableMessage({ author, recipient, message }) {
1176
- const networkMessage = {
1177
- ...message,
1178
- // Setting unique message_id if it not specified yet.
1179
- messageId: message.messageId ?? import_keys2.PublicKey.random()
1180
- };
1181
- (0, import_log2.log)("sending", {
1182
- from: author,
1183
- to: recipient,
1184
- msg: networkMessage
1185
- }, {
1186
- F: __dxlog_file2,
1187
- L: 123,
1188
- S: this,
1189
- C: (f, a) => f(...a)
1190
- });
1191
- await this._sendMessage({
1192
- author,
1193
- recipient,
1194
- payload: {
1195
- type_url: "dxos.mesh.swarm.SwarmMessage",
1196
- value: SwarmMessage.encode(networkMessage)
1197
- }
1198
- });
1199
- }
1200
- async _resolveAnswers(message) {
1201
- (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1202
- F: __dxlog_file2,
1203
- L: 135,
1204
- S: this,
1205
- A: [
1206
- "message.data?.answer?.offerMessageId",
1207
- "'No offerMessageId'"
1208
- ]
1209
- });
1210
- const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1211
- if (offerRecord) {
1212
- this._offerRecords.delete(message.data.answer.offerMessageId);
1213
- (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
1214
- F: __dxlog_file2,
1215
- L: 139,
1216
- S: this,
1217
- A: [
1218
- "message.data?.answer",
1219
- "'No answer'"
1220
- ]
1221
- });
1222
- (0, import_log2.log)("resolving", {
1223
- answer: message.data.answer
1224
- }, {
1225
- F: __dxlog_file2,
1226
- L: 140,
1227
- S: this,
1228
- C: (f, a) => f(...a)
1229
- });
1230
- offerRecord.resolve(message.data.answer);
1231
- }
1232
- }
1233
- async _handleOffer({ author, recipient, message }) {
1234
- (0, import_invariant2.invariant)(message.data.offer, "No offer", {
1235
- F: __dxlog_file2,
1236
- L: 154,
1237
- S: this,
1238
- A: [
1239
- "message.data.offer",
1240
- "'No offer'"
1241
- ]
1242
- });
1243
- const offerMessage = {
1244
- author,
1245
- recipient,
1246
- ...message,
1247
- data: {
1248
- offer: message.data.offer
1249
- }
1250
- };
1251
- const answer = await this._onOffer(offerMessage);
1252
- answer.offerMessageId = message.messageId;
1253
- try {
1254
- await this._sendReliableMessage({
1255
- author: recipient,
1256
- recipient: author,
1257
- message: {
1258
- topic: message.topic,
1259
- sessionId: message.sessionId,
1260
- data: {
1261
- answer
1262
- }
1263
- }
1264
- });
1265
- } catch (err) {
1266
- import_log2.log.warn("error sending answer", {
1267
- err
1268
- }, {
1269
- F: __dxlog_file2,
1270
- L: 174,
1271
- S: this,
1272
- C: (f, a) => f(...a)
1273
- });
1274
- }
1275
- }
1276
- async _handleSignal({ author, recipient, message }) {
1277
- (0, import_invariant2.invariant)(message.messageId, void 0, {
1278
- F: __dxlog_file2,
1279
- L: 187,
1280
- S: this,
1281
- A: [
1282
- "message.messageId",
1283
- ""
1284
- ]
1285
- });
1286
- (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1287
- F: __dxlog_file2,
1288
- L: 188,
1289
- S: this,
1290
- A: [
1291
- "message.data.signal || message.data.signalBatch",
1292
- "'Invalid message'"
1293
- ]
1294
- });
1295
- const signalMessage = {
1296
- author,
1297
- recipient,
1298
- ...message,
1299
- data: {
1300
- signal: message.data.signal,
1301
- signalBatch: message.data.signalBatch
1302
- }
1303
- };
1304
- await this._onSignal(signalMessage);
1305
- }
1306
- };
1307
-
1308
- // packages/core/mesh/network-manager/src/swarm/peer.ts
1309
- var import_async2 = require("@dxos/async");
1310
- var import_context3 = require("@dxos/context");
1311
- var import_invariant3 = require("@dxos/invariant");
1312
- var import_keys3 = require("@dxos/keys");
1313
- var import_log3 = require("@dxos/log");
1314
- var import_protocols3 = require("@dxos/protocols");
1315
1085
  function _ts_decorate2(decorators, target, key, desc) {
1316
1086
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1317
1087
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1322,8 +1092,8 @@ function _ts_decorate2(decorators, target, key, desc) {
1322
1092
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1323
1093
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1324
1094
  }
1325
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1326
- var ConnectionDisplacedError = class extends import_protocols3.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 {
1327
1097
  constructor() {
1328
1098
  super("Connection displaced by remote initiator.");
1329
1099
  }
@@ -1342,7 +1112,7 @@ var Peer = class {
1342
1112
  this._callbacks = _callbacks;
1343
1113
  this._availableAfter = 0;
1344
1114
  this.availableToConnect = true;
1345
- this._ctx = new import_context3.Context();
1115
+ this._ctx = new import_context2.Context();
1346
1116
  this.advertizing = false;
1347
1117
  this.initiating = false;
1348
1118
  this.connectionDisplaced = new import_async2.Event();
@@ -1352,16 +1122,30 @@ var Peer = class {
1352
1122
  */
1353
1123
  async onOffer(message) {
1354
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
+ }
1355
1139
  if (this.connection || this.initiating) {
1356
1140
  if (remoteId.toHex() < this.localPeerId.toHex()) {
1357
- (0, import_log3.log)("close local connection", {
1141
+ (0, import_log2.log)("close local connection", {
1358
1142
  localPeerId: this.id,
1359
1143
  topic: this.topic,
1360
1144
  remotePeerId: this.localPeerId,
1361
1145
  sessionId: this.connection?.sessionId
1362
1146
  }, {
1363
- F: __dxlog_file3,
1364
- L: 117,
1147
+ F: __dxlog_file2,
1148
+ L: 121,
1365
1149
  S: this,
1366
1150
  C: (f, a) => f(...a)
1367
1151
  });
@@ -1376,9 +1160,9 @@ var Peer = class {
1376
1160
  }
1377
1161
  if (await this._callbacks.onOffer(remoteId)) {
1378
1162
  if (!this.connection) {
1379
- (0, import_invariant3.invariant)(message.sessionId, void 0, {
1380
- F: __dxlog_file3,
1381
- L: 137,
1163
+ (0, import_invariant2.invariant)(message.sessionId, void 0, {
1164
+ F: __dxlog_file2,
1165
+ L: 141,
1382
1166
  S: this,
1383
1167
  A: [
1384
1168
  "message.sessionId",
@@ -1391,15 +1175,15 @@ var Peer = class {
1391
1175
  connection.initiate();
1392
1176
  await connection.openConnection();
1393
1177
  } catch (err) {
1394
- if (!(err instanceof import_protocols3.CancelledError)) {
1395
- import_log3.log.warn("connection error", {
1178
+ if (!(err instanceof import_protocols2.CancelledError)) {
1179
+ import_log2.log.warn("connection error", {
1396
1180
  topic: this.topic,
1397
1181
  peerId: this.localPeerId,
1398
1182
  remoteId: this.id,
1399
1183
  err
1400
1184
  }, {
1401
- F: __dxlog_file3,
1402
- L: 147,
1185
+ F: __dxlog_file2,
1186
+ L: 151,
1403
1187
  S: this,
1404
1188
  C: (f, a) => f(...a)
1405
1189
  });
@@ -1419,33 +1203,33 @@ var Peer = class {
1419
1203
  * Initiate a connection to the remote peer.
1420
1204
  */
1421
1205
  async initiateConnection() {
1422
- (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
1423
- F: __dxlog_file3,
1424
- L: 164,
1206
+ (0, import_invariant2.invariant)(!this.initiating, "Initiation in progress.", {
1207
+ F: __dxlog_file2,
1208
+ L: 168,
1425
1209
  S: this,
1426
1210
  A: [
1427
1211
  "!this.initiating",
1428
1212
  "'Initiation in progress.'"
1429
1213
  ]
1430
1214
  });
1431
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1432
- F: __dxlog_file3,
1433
- L: 165,
1215
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1216
+ F: __dxlog_file2,
1217
+ L: 169,
1434
1218
  S: this,
1435
1219
  A: [
1436
1220
  "!this.connection",
1437
1221
  "'Already connected.'"
1438
1222
  ]
1439
1223
  });
1440
- const sessionId = import_keys3.PublicKey.random();
1441
- (0, import_log3.log)("initiating...", {
1224
+ const sessionId = import_keys2.PublicKey.random();
1225
+ (0, import_log2.log)("initiating...", {
1442
1226
  id: this.id,
1443
1227
  topic: this.topic,
1444
1228
  peerId: this.id,
1445
1229
  sessionId
1446
1230
  }, {
1447
- F: __dxlog_file3,
1448
- L: 167,
1231
+ F: __dxlog_file2,
1232
+ L: 171,
1449
1233
  S: this,
1450
1234
  C: (f, a) => f(...a)
1451
1235
  });
@@ -1463,21 +1247,21 @@ var Peer = class {
1463
1247
  offer: {}
1464
1248
  }
1465
1249
  });
1466
- (0, import_log3.log)("received", {
1250
+ (0, import_log2.log)("received", {
1467
1251
  answer,
1468
1252
  topic: this.topic,
1469
1253
  ownId: this.localPeerId,
1470
1254
  remoteId: this.id
1471
1255
  }, {
1472
- F: __dxlog_file3,
1473
- L: 183,
1256
+ F: __dxlog_file2,
1257
+ L: 187,
1474
1258
  S: this,
1475
1259
  C: (f, a) => f(...a)
1476
1260
  });
1477
1261
  if (connection.state !== ConnectionState.INITIAL) {
1478
- (0, import_log3.log)("ignoring response", void 0, {
1479
- F: __dxlog_file3,
1480
- L: 185,
1262
+ (0, import_log2.log)("ignoring response", void 0, {
1263
+ F: __dxlog_file2,
1264
+ L: 189,
1481
1265
  S: this,
1482
1266
  C: (f, a) => f(...a)
1483
1267
  });
@@ -1490,18 +1274,24 @@ var Peer = class {
1490
1274
  await connection.openConnection();
1491
1275
  this._callbacks.onAccepted();
1492
1276
  } catch (err) {
1493
- (0, import_log3.log)("initiation error", {
1277
+ (0, import_log2.log)("initiation error", {
1494
1278
  err,
1495
1279
  topic: this.topic,
1496
1280
  peerId: this.localPeerId,
1497
1281
  remoteId: this.id
1498
1282
  }, {
1499
- F: __dxlog_file3,
1500
- L: 196,
1283
+ F: __dxlog_file2,
1284
+ L: 200,
1501
1285
  S: this,
1502
1286
  C: (f, a) => f(...a)
1503
1287
  });
1504
- if (err instanceof import_protocols3.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
+ });
1505
1295
  await connection.abort(err);
1506
1296
  }
1507
1297
  await this.closeConnection(err);
@@ -1515,21 +1305,21 @@ var Peer = class {
1515
1305
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
1516
1306
  */
1517
1307
  _createConnection(initiator, sessionId) {
1518
- (0, import_log3.log)("creating connection", {
1308
+ (0, import_log2.log)("creating connection", {
1519
1309
  topic: this.topic,
1520
1310
  peerId: this.localPeerId,
1521
1311
  remoteId: this.id,
1522
1312
  initiator,
1523
1313
  sessionId
1524
1314
  }, {
1525
- F: __dxlog_file3,
1526
- L: 213,
1315
+ F: __dxlog_file2,
1316
+ L: 218,
1527
1317
  S: this,
1528
1318
  C: (f, a) => f(...a)
1529
1319
  });
1530
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1531
- F: __dxlog_file3,
1532
- L: 220,
1320
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1321
+ F: __dxlog_file2,
1322
+ L: 225,
1533
1323
  S: this,
1534
1324
  A: [
1535
1325
  "!this.connection",
@@ -1557,50 +1347,50 @@ var Peer = class {
1557
1347
  this._lastConnectionTime = Date.now();
1558
1348
  this._callbacks.onConnected();
1559
1349
  this._connectionLimiter.doneConnecting(sessionId);
1560
- import_log3.log.trace("dxos.mesh.connection.connected", {
1350
+ import_log2.log.trace("dxos.mesh.connection.connected", {
1561
1351
  topic: this.topic,
1562
1352
  localPeerId: this.localPeerId,
1563
1353
  remotePeerId: this.id,
1564
1354
  sessionId,
1565
1355
  initiator
1566
1356
  }, {
1567
- F: __dxlog_file3,
1568
- L: 239,
1357
+ F: __dxlog_file2,
1358
+ L: 244,
1569
1359
  S: this,
1570
1360
  C: (f, a) => f(...a)
1571
1361
  });
1572
1362
  },
1573
1363
  onClosed: (err) => {
1574
- (0, import_log3.log)("connection closed", {
1364
+ (0, import_log2.log)("connection closed", {
1575
1365
  topic: this.topic,
1576
1366
  peerId: this.localPeerId,
1577
1367
  remoteId: this.id,
1578
1368
  initiator
1579
1369
  }, {
1580
- F: __dxlog_file3,
1581
- L: 248,
1370
+ F: __dxlog_file2,
1371
+ L: 253,
1582
1372
  S: this,
1583
1373
  C: (f, a) => f(...a)
1584
1374
  });
1585
1375
  this._connectionLimiter.doneConnecting(sessionId);
1586
- (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1587
- F: __dxlog_file3,
1588
- L: 253,
1376
+ (0, import_invariant2.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1377
+ F: __dxlog_file2,
1378
+ L: 258,
1589
1379
  S: this,
1590
1380
  A: [
1591
1381
  "this.connection === connection",
1592
1382
  "'Connection mismatch (race condition).'"
1593
1383
  ]
1594
1384
  });
1595
- import_log3.log.trace("dxos.mesh.connection.closed", {
1385
+ import_log2.log.trace("dxos.mesh.connection.closed", {
1596
1386
  topic: this.topic,
1597
1387
  localPeerId: this.localPeerId,
1598
1388
  remotePeerId: this.id,
1599
1389
  sessionId,
1600
1390
  initiator
1601
1391
  }, {
1602
- F: __dxlog_file3,
1603
- L: 255,
1392
+ F: __dxlog_file2,
1393
+ L: 260,
1604
1394
  S: this,
1605
1395
  C: (f, a) => f(...a)
1606
1396
  });
@@ -1622,108 +1412,340 @@ var Peer = class {
1622
1412
  this.connection = void 0;
1623
1413
  }
1624
1414
  }
1625
- );
1626
- this._callbacks.onInitiated(connection);
1627
- void this._connectionCtx?.dispose();
1628
- this._connectionCtx = this._ctx.derive();
1629
- connection.errors.handle((err) => {
1630
- import_log3.log.warn("connection error", {
1631
- topic: this.topic,
1632
- peerId: this.localPeerId,
1633
- remoteId: this.id,
1634
- initiator,
1635
- err
1636
- }, {
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", {
1637
1657
  F: __dxlog_file3,
1638
- L: 295,
1658
+ L: 139,
1639
1659
  S: this,
1640
- C: (f, a) => f(...a)
1660
+ A: [
1661
+ "message.data?.answer",
1662
+ "'No answer'"
1663
+ ]
1641
1664
  });
1642
- import_log3.log.trace("dxos.mesh.connection.error", {
1643
- topic: this.topic,
1644
- localPeerId: this.localPeerId,
1645
- remotePeerId: this.id,
1646
- sessionId,
1647
- initiator,
1648
- err
1665
+ (0, import_log3.log)("resolving", {
1666
+ answer: message.data.answer
1649
1667
  }, {
1650
1668
  F: __dxlog_file3,
1651
- L: 296,
1669
+ L: 140,
1652
1670
  S: this,
1653
1671
  C: (f, a) => f(...a)
1654
1672
  });
1655
- void this.closeConnection(err);
1656
- });
1657
- this.connection = connection;
1658
- return connection;
1659
- }
1660
- async closeConnection(err) {
1661
- if (!this.connection) {
1662
- return;
1673
+ offerRecord.resolve(message.data.answer);
1663
1674
  }
1664
- const connection = this.connection;
1665
- (0, import_log3.log)("closing...", {
1666
- peerId: this.id,
1667
- sessionId: connection.sessionId
1668
- }, {
1669
- F: __dxlog_file3,
1670
- L: 321,
1671
- S: this,
1672
- C: (f, a) => f(...a)
1673
- });
1674
- await connection.close(err);
1675
- (0, import_log3.log)("closed", {
1676
- peerId: this.id,
1677
- sessionId: connection.sessionId
1678
- }, {
1675
+ }
1676
+ async _handleOffer({ author, recipient, message }) {
1677
+ (0, import_invariant3.invariant)(message.data.offer, "No offer", {
1679
1678
  F: __dxlog_file3,
1680
- L: 327,
1679
+ L: 154,
1681
1680
  S: this,
1682
- C: (f, a) => f(...a)
1681
+ A: [
1682
+ "message.data.offer",
1683
+ "'No offer'"
1684
+ ]
1683
1685
  });
1684
- }
1685
- async onSignal(message) {
1686
- if (!this.connection) {
1687
- (0, import_log3.log)("dropping signal message for non-existent connection", {
1688
- 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
1689
1711
  }, {
1690
1712
  F: __dxlog_file3,
1691
- L: 332,
1713
+ L: 174,
1692
1714
  S: this,
1693
1715
  C: (f, a) => f(...a)
1694
1716
  });
1695
- return;
1696
1717
  }
1697
- await this.connection.signal(message);
1698
1718
  }
1699
- async destroy() {
1700
- await this._ctx.dispose();
1701
- (0, import_log3.log)("Destroying peer", {
1702
- peerId: this.id,
1703
- topic: this.topic
1704
- }, {
1719
+ async _handleSignal({ author, recipient, message }) {
1720
+ (0, import_invariant3.invariant)(message.messageId, void 0, {
1705
1721
  F: __dxlog_file3,
1706
- L: 342,
1722
+ L: 187,
1707
1723
  S: this,
1708
- C: (f, a) => f(...a)
1724
+ A: [
1725
+ "message.messageId",
1726
+ ""
1727
+ ]
1709
1728
  });
1710
- await this?.connection?.close();
1711
- }
1712
- };
1713
- _ts_decorate2([
1714
- import_async2.synchronized
1715
- ], Peer.prototype, "destroy", null);
1716
- var increaseInterval = (interval) => {
1717
- if (interval === 0) {
1718
- return 50;
1719
- } else if (interval < 500) {
1720
- return 500;
1721
- } else if (interval < 1e3) {
1722
- return 1e3;
1723
- } else if (interval < 5e3) {
1724
- 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);
1725
1748
  }
1726
- return 1e4;
1727
1749
  };
1728
1750
 
1729
1751
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
@@ -2216,7 +2238,6 @@ var SwarmMapper = class {
2216
2238
  get peers() {
2217
2239
  return Array.from(this._peers.values());
2218
2240
  }
2219
- // prettier-ignore
2220
2241
  constructor(_swarm) {
2221
2242
  this._swarm = _swarm;
2222
2243
  this._subscriptions = new import_async4.EventSubscriptions();
@@ -2239,7 +2260,7 @@ var SwarmMapper = class {
2239
2260
  _update() {
2240
2261
  (0, import_log5.log)("updating swarm", void 0, {
2241
2262
  F: __dxlog_file5,
2242
- L: 75,
2263
+ L: 72,
2243
2264
  S: this,
2244
2265
  C: (f, a) => f(...a)
2245
2266
  });
@@ -2263,7 +2284,7 @@ var SwarmMapper = class {
2263
2284
  totalPeersInSwarm: this._peers.size
2264
2285
  }, {
2265
2286
  F: __dxlog_file5,
2266
- L: 116,
2287
+ L: 113,
2267
2288
  S: this,
2268
2289
  C: (f, a) => f(...a)
2269
2290
  });
@@ -2823,7 +2844,6 @@ var import_invariant9 = require("@dxos/invariant");
2823
2844
  var import_log9 = require("@dxos/log");
2824
2845
  var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2825
2846
  var StarTopology = class {
2826
- // prettier-ignore
2827
2847
  constructor(_centralPeer) {
2828
2848
  this._centralPeer = _centralPeer;
2829
2849
  }
@@ -2833,7 +2853,7 @@ var StarTopology = class {
2833
2853
  init(controller) {
2834
2854
  (0, import_invariant9.invariant)(!this._controller, "Already initialized.", {
2835
2855
  F: __dxlog_file10,
2836
- L: 24,
2856
+ L: 21,
2837
2857
  S: this,
2838
2858
  A: [
2839
2859
  "!this._controller",
@@ -2845,7 +2865,7 @@ var StarTopology = class {
2845
2865
  update() {
2846
2866
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2847
2867
  F: __dxlog_file10,
2848
- L: 29,
2868
+ L: 26,
2849
2869
  S: this,
2850
2870
  A: [
2851
2871
  "this._controller",
@@ -2856,7 +2876,7 @@ var StarTopology = class {
2856
2876
  if (!ownPeerId.equals(this._centralPeer)) {
2857
2877
  (0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
2858
2878
  F: __dxlog_file10,
2859
- L: 32,
2879
+ L: 29,
2860
2880
  S: this,
2861
2881
  C: (f, a) => f(...a)
2862
2882
  });
@@ -2866,7 +2886,7 @@ var StarTopology = class {
2866
2886
  peer
2867
2887
  }, {
2868
2888
  F: __dxlog_file10,
2869
- L: 37,
2889
+ L: 34,
2870
2890
  S: this,
2871
2891
  C: (f, a) => f(...a)
2872
2892
  });
@@ -2880,7 +2900,7 @@ var StarTopology = class {
2880
2900
  peer
2881
2901
  }, {
2882
2902
  F: __dxlog_file10,
2883
- L: 46,
2903
+ L: 43,
2884
2904
  S: this,
2885
2905
  C: (f, a) => f(...a)
2886
2906
  });
@@ -2891,7 +2911,7 @@ var StarTopology = class {
2891
2911
  async onOffer(peer) {
2892
2912
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2893
2913
  F: __dxlog_file10,
2894
- L: 53,
2914
+ L: 50,
2895
2915
  S: this,
2896
2916
  A: [
2897
2917
  "this._controller",
@@ -2905,7 +2925,7 @@ var StarTopology = class {
2905
2925
  isSelfCentral: ownPeerId.equals(this._centralPeer)
2906
2926
  }, {
2907
2927
  F: __dxlog_file10,
2908
- L: 55,
2928
+ L: 52,
2909
2929
  S: this,
2910
2930
  C: (f, a) => f(...a)
2911
2931
  });
@@ -3197,38 +3217,52 @@ var SimplePeerTransport = class {
3197
3217
  } else {
3198
3218
  this.errors.raise(new import_protocols7.UnknownProtocolError("unknown RTCError", err));
3199
3219
  }
3200
- } else if (err.name === "InvalidStateError") {
3201
- this.errors.raise(new import_protocols7.ConnectionResetError("safari WebRTC error", err));
3202
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
+ });
3203
3227
  switch (err.code) {
3204
3228
  case "ERR_WEBRTC_SUPPORT":
3205
3229
  this.errors.raise(new import_protocols7.ProtocolError("WebRTC not supported", err));
3230
+ break;
3206
3231
  case "ERR_ICE_CONNECTION_FAILURE":
3207
3232
  case "ERR_DATA_CHANNEL":
3208
3233
  case "ERR_CONNECTION_FAILURE":
3209
3234
  case "ERR_SIGNALING":
3210
3235
  this.errors.raise(new import_protocols7.ConnectivityError("unknown communication failure", err));
3236
+ break;
3211
3237
  case "ERR_CREATE_OFFER":
3212
3238
  case "ERR_CREATE_ANSWER":
3213
3239
  case "ERR_SET_LOCAL_DESCRIPTION":
3214
3240
  case "ERR_SET_REMOTE_DESCRIPTION":
3215
3241
  case "ERR_ADD_ICE_CANDIDATE":
3216
3242
  this.errors.raise(new import_protocols7.UnknownProtocolError("unknown simple-peer library failure", err));
3243
+ break;
3217
3244
  default:
3218
3245
  this.errors.raise(new Error("unknown simple-peer error"));
3246
+ break;
3219
3247
  }
3220
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
+ });
3221
3255
  this.errors.raise(err);
3222
3256
  }
3223
3257
  try {
3224
- if (typeof this._peer?._pc.getStats === "function") {
3258
+ if (typeof this._peer?._pc?.getStats === "function") {
3225
3259
  this._peer._pc.getStats().then((stats) => {
3226
3260
  import_log11.log.warn("report after webrtc error", {
3227
3261
  config: this.params.webrtcConfig,
3228
3262
  stats
3229
3263
  }, {
3230
3264
  F: __dxlog_file12,
3231
- L: 106,
3265
+ L: 109,
3232
3266
  S: this,
3233
3267
  C: (f, a) => f(...a)
3234
3268
  });
@@ -3237,7 +3271,7 @@ var SimplePeerTransport = class {
3237
3271
  } catch (err2) {
3238
3272
  import_log11.log.catch(err2, void 0, {
3239
3273
  F: __dxlog_file12,
3240
- L: 113,
3274
+ L: 116,
3241
3275
  S: this,
3242
3276
  C: (f, a) => f(...a)
3243
3277
  });
@@ -3248,7 +3282,7 @@ var SimplePeerTransport = class {
3248
3282
  id: this._instanceId
3249
3283
  }), {
3250
3284
  F: __dxlog_file12,
3251
- L: 118,
3285
+ L: 121,
3252
3286
  S: this,
3253
3287
  C: (f, a) => f(...a)
3254
3288
  });
@@ -3256,7 +3290,7 @@ var SimplePeerTransport = class {
3256
3290
  async destroy() {
3257
3291
  (0, import_log11.log)("closing...", void 0, {
3258
3292
  F: __dxlog_file12,
3259
- L: 122,
3293
+ L: 125,
3260
3294
  S: this,
3261
3295
  C: (f, a) => f(...a)
3262
3296
  });
@@ -3266,7 +3300,7 @@ var SimplePeerTransport = class {
3266
3300
  this.closed.emit();
3267
3301
  (0, import_log11.log)("closed", void 0, {
3268
3302
  F: __dxlog_file12,
3269
- L: 127,
3303
+ L: 130,
3270
3304
  S: this,
3271
3305
  C: (f, a) => f(...a)
3272
3306
  });
@@ -3299,7 +3333,6 @@ var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
3299
3333
  var import_util8 = require("@dxos/util");
3300
3334
  var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3301
3335
  var SimplePeerTransportService = class {
3302
- // prettier-ignore
3303
3336
  constructor(_webrtcConfig) {
3304
3337
  this._webrtcConfig = _webrtcConfig;
3305
3338
  this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
@@ -3369,7 +3402,8 @@ var SimplePeerTransportService = class {
3369
3402
  const transportState = {
3370
3403
  transport,
3371
3404
  stream: duplex,
3372
- writeCallbacks: []
3405
+ writeCallbacks: [],
3406
+ state: "OPEN"
3373
3407
  };
3374
3408
  ready();
3375
3409
  this.transports.set(request.proxyId, transportState);
@@ -3379,7 +3413,7 @@ var SimplePeerTransportService = class {
3379
3413
  async sendSignal({ proxyId, signal }) {
3380
3414
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3381
3415
  F: __dxlog_file13,
3382
- L: 113,
3416
+ L: 112,
3383
3417
  S: this,
3384
3418
  A: [
3385
3419
  "this.transports.has(proxyId)",
@@ -3389,9 +3423,17 @@ var SimplePeerTransportService = class {
3389
3423
  await this.transports.get(proxyId).transport.signal(signal);
3390
3424
  }
3391
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
+ }
3392
3434
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3393
3435
  F: __dxlog_file13,
3394
- L: 118,
3436
+ L: 120,
3395
3437
  S: this,
3396
3438
  A: [
3397
3439
  "this.transports.has(proxyId)",
@@ -3409,10 +3451,12 @@ var SimplePeerTransportService = class {
3409
3451
  async close({ proxyId }) {
3410
3452
  await this.transports.get(proxyId)?.transport.destroy();
3411
3453
  await this.transports.get(proxyId)?.stream.end();
3412
- this.transports.delete(proxyId);
3454
+ if (this.transports.get(proxyId)) {
3455
+ this.transports.get(proxyId).state = "CLOSED";
3456
+ }
3413
3457
  (0, import_log12.log)("Closed.", void 0, {
3414
3458
  F: __dxlog_file13,
3415
- L: 132,
3459
+ L: 136,
3416
3460
  S: this,
3417
3461
  C: (f, a) => f(...a)
3418
3462
  });
@@ -3432,7 +3476,6 @@ var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
3432
3476
  var import_util9 = require("@dxos/util");
3433
3477
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3434
3478
  var SimplePeerTransportProxy = class {
3435
- // prettier-ignore
3436
3479
  constructor(_params) {
3437
3480
  this._params = _params;
3438
3481
  this._proxyId = import_keys12.PublicKey.random();
@@ -3449,7 +3492,7 @@ var SimplePeerTransportProxy = class {
3449
3492
  this._serviceStream.subscribe(async (event) => {
3450
3493
  (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
3451
3494
  F: __dxlog_file14,
3452
- L: 58,
3495
+ L: 55,
3453
3496
  S: this,
3454
3497
  C: (f, a) => f(...a)
3455
3498
  });
@@ -3478,7 +3521,7 @@ var SimplePeerTransportProxy = class {
3478
3521
  }));
3479
3522
  }, (error) => import_log13.log.catch(error, void 0, {
3480
3523
  F: __dxlog_file14,
3481
- L: 82,
3524
+ L: 83,
3482
3525
  S: this,
3483
3526
  C: (f, a) => f(...a)
3484
3527
  }));
@@ -3524,7 +3567,7 @@ var SimplePeerTransportProxy = class {
3524
3567
  } catch (err) {
3525
3568
  import_log13.log.catch(err, void 0, {
3526
3569
  F: __dxlog_file14,
3527
- L: 133,
3570
+ L: 134,
3528
3571
  S: this,
3529
3572
  C: (f, a) => f(...a)
3530
3573
  });
@@ -3560,7 +3603,7 @@ var SimplePeerTransportProxyFactory = class {
3560
3603
  createTransport(options) {
3561
3604
  (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3562
3605
  F: __dxlog_file14,
3563
- L: 170,
3606
+ L: 171,
3564
3607
  S: this,
3565
3608
  A: [
3566
3609
  "this._bridgeService",
@@ -3578,15 +3621,15 @@ var SimplePeerTransportProxyFactory = class {
3578
3621
  };
3579
3622
  var decodeError = (err) => {
3580
3623
  const message = typeof err === "string" ? err : err.message;
3581
- if (message.includes("ConnectionResetError")) {
3624
+ if (message.includes("CONNECTION_RESET")) {
3582
3625
  return new import_protocols8.ConnectionResetError(message);
3583
- } else if (message.includes("TimeoutError")) {
3626
+ } else if (message.includes("TIMEOUT")) {
3584
3627
  return new import_protocols8.TimeoutError(message);
3585
- } else if (message.includes("ProtocolError")) {
3628
+ } else if (message.includes("PROTOCOL_ERROR")) {
3586
3629
  return new import_protocols8.ProtocolError(message);
3587
- } else if (message.includes("ConnectivityError")) {
3630
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3588
3631
  return new import_protocols8.ConnectivityError(message);
3589
- } else if (message.includes("UnknownProtocolError")) {
3632
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3590
3633
  return new import_protocols8.UnknownProtocolError(message);
3591
3634
  } else {
3592
3635
  return typeof err === "string" ? new Error(err) : err;
@@ -3935,6 +3978,136 @@ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3935
3978
  })
3936
3979
  });
3937
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
+
3938
4111
  // packages/core/mesh/network-manager/src/wire-protocol.ts
3939
4112
  var import_teleport = require("@dxos/teleport");
3940
4113
  var createTeleportProtocolFactory = (onConnection) => {
@@ -3980,6 +4153,8 @@ var createTeleportProtocolFactory = (onConnection) => {
3980
4153
  Swarm,
3981
4154
  SwarmMapper,
3982
4155
  SwarmMessenger,
4156
+ TcpTransport,
4157
+ TcpTransportFactory,
3983
4158
  TransportKind,
3984
4159
  cleanup,
3985
4160
  createLibDataChannelTransportFactory,