@dxos/network-manager 0.1.58-main.4cbb52d → 0.1.58-main.4e2a73a

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 (39) hide show
  1. package/dist/lib/browser/{chunk-CNQ55TFQ.mjs → chunk-HKHUCTRB.mjs} +40 -34
  2. package/dist/lib/browser/chunk-HKHUCTRB.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +109 -109
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node/index.cjs +321 -315
  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 +836 -829
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/swarm/peer.d.ts +2 -2
  13. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  14. package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
  15. package/dist/types/src/swarm/swarm.d.ts +2 -2
  16. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  17. package/dist/types/src/testing/test-builder.d.ts +1 -1
  18. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  19. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  20. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  21. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  22. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  23. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  24. package/package.json +17 -17
  25. package/src/swarm/connection.test.ts +1 -1
  26. package/src/swarm/peer.ts +2 -2
  27. package/src/swarm/swarm-mapper.ts +4 -7
  28. package/src/swarm/swarm.test.ts +3 -3
  29. package/src/swarm/swarm.ts +3 -3
  30. package/src/testing/test-builder.ts +1 -1
  31. package/src/tests/basic-test-suite.ts +1 -1
  32. package/src/tests/memory-transport.test.ts +2 -2
  33. package/src/tests/tcp-transport.test.ts +2 -2
  34. package/src/tests/webrtc-transport.test.ts +1 -1
  35. package/src/topology/star-topology.ts +1 -4
  36. package/src/transport/simplepeer-transport-proxy.ts +25 -24
  37. package/src/transport/simplepeer-transport-service.ts +1 -4
  38. package/src/transport/simplepeer-transport.ts +3 -5
  39. package/dist/lib/browser/chunk-CNQ55TFQ.mjs.map +0 -7
@@ -1073,245 +1073,13 @@ var import_log4 = require("@dxos/log");
1073
1073
  var import_protocols4 = require("@dxos/protocols");
1074
1074
  var import_util2 = require("@dxos/util");
1075
1075
 
1076
- // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1076
+ // packages/core/mesh/network-manager/src/swarm/peer.ts
1077
+ var import_async2 = require("@dxos/async");
1077
1078
  var import_context2 = require("@dxos/context");
1078
1079
  var import_invariant2 = require("@dxos/invariant");
1079
1080
  var import_keys2 = require("@dxos/keys");
1080
1081
  var import_log2 = require("@dxos/log");
1081
1082
  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
1083
  function _ts_decorate2(decorators, target, key, desc) {
1316
1084
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1317
1085
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1322,8 +1090,8 @@ function _ts_decorate2(decorators, target, key, desc) {
1322
1090
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1323
1091
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1324
1092
  }
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 {
1093
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1094
+ var ConnectionDisplacedError = class extends import_protocols2.SystemError {
1327
1095
  constructor() {
1328
1096
  super("Connection displaced by remote initiator.");
1329
1097
  }
@@ -1342,7 +1110,7 @@ var Peer = class {
1342
1110
  this._callbacks = _callbacks;
1343
1111
  this._availableAfter = 0;
1344
1112
  this.availableToConnect = true;
1345
- this._ctx = new import_context3.Context();
1113
+ this._ctx = new import_context2.Context();
1346
1114
  this.advertizing = false;
1347
1115
  this.initiating = false;
1348
1116
  this.connectionDisplaced = new import_async2.Event();
@@ -1354,13 +1122,13 @@ var Peer = class {
1354
1122
  const remoteId = message.author;
1355
1123
  if (this.connection || this.initiating) {
1356
1124
  if (remoteId.toHex() < this.localPeerId.toHex()) {
1357
- (0, import_log3.log)("close local connection", {
1125
+ (0, import_log2.log)("close local connection", {
1358
1126
  localPeerId: this.id,
1359
1127
  topic: this.topic,
1360
1128
  remotePeerId: this.localPeerId,
1361
1129
  sessionId: this.connection?.sessionId
1362
1130
  }, {
1363
- F: __dxlog_file3,
1131
+ F: __dxlog_file2,
1364
1132
  L: 117,
1365
1133
  S: this,
1366
1134
  C: (f, a) => f(...a)
@@ -1376,8 +1144,8 @@ var Peer = class {
1376
1144
  }
1377
1145
  if (await this._callbacks.onOffer(remoteId)) {
1378
1146
  if (!this.connection) {
1379
- (0, import_invariant3.invariant)(message.sessionId, void 0, {
1380
- F: __dxlog_file3,
1147
+ (0, import_invariant2.invariant)(message.sessionId, void 0, {
1148
+ F: __dxlog_file2,
1381
1149
  L: 137,
1382
1150
  S: this,
1383
1151
  A: [
@@ -1391,14 +1159,14 @@ var Peer = class {
1391
1159
  connection.initiate();
1392
1160
  await connection.openConnection();
1393
1161
  } catch (err) {
1394
- if (!(err instanceof import_protocols3.CancelledError)) {
1395
- import_log3.log.warn("connection error", {
1162
+ if (!(err instanceof import_protocols2.CancelledError)) {
1163
+ import_log2.log.warn("connection error", {
1396
1164
  topic: this.topic,
1397
1165
  peerId: this.localPeerId,
1398
1166
  remoteId: this.id,
1399
1167
  err
1400
1168
  }, {
1401
- F: __dxlog_file3,
1169
+ F: __dxlog_file2,
1402
1170
  L: 147,
1403
1171
  S: this,
1404
1172
  C: (f, a) => f(...a)
@@ -1419,8 +1187,8 @@ var Peer = class {
1419
1187
  * Initiate a connection to the remote peer.
1420
1188
  */
1421
1189
  async initiateConnection() {
1422
- (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
1423
- F: __dxlog_file3,
1190
+ (0, import_invariant2.invariant)(!this.initiating, "Initiation in progress.", {
1191
+ F: __dxlog_file2,
1424
1192
  L: 164,
1425
1193
  S: this,
1426
1194
  A: [
@@ -1428,8 +1196,8 @@ var Peer = class {
1428
1196
  "'Initiation in progress.'"
1429
1197
  ]
1430
1198
  });
1431
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1432
- F: __dxlog_file3,
1199
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1200
+ F: __dxlog_file2,
1433
1201
  L: 165,
1434
1202
  S: this,
1435
1203
  A: [
@@ -1437,14 +1205,14 @@ var Peer = class {
1437
1205
  "'Already connected.'"
1438
1206
  ]
1439
1207
  });
1440
- const sessionId = import_keys3.PublicKey.random();
1441
- (0, import_log3.log)("initiating...", {
1208
+ const sessionId = import_keys2.PublicKey.random();
1209
+ (0, import_log2.log)("initiating...", {
1442
1210
  id: this.id,
1443
1211
  topic: this.topic,
1444
1212
  peerId: this.id,
1445
1213
  sessionId
1446
1214
  }, {
1447
- F: __dxlog_file3,
1215
+ F: __dxlog_file2,
1448
1216
  L: 167,
1449
1217
  S: this,
1450
1218
  C: (f, a) => f(...a)
@@ -1463,20 +1231,20 @@ var Peer = class {
1463
1231
  offer: {}
1464
1232
  }
1465
1233
  });
1466
- (0, import_log3.log)("received", {
1234
+ (0, import_log2.log)("received", {
1467
1235
  answer,
1468
1236
  topic: this.topic,
1469
1237
  ownId: this.localPeerId,
1470
1238
  remoteId: this.id
1471
1239
  }, {
1472
- F: __dxlog_file3,
1240
+ F: __dxlog_file2,
1473
1241
  L: 183,
1474
1242
  S: this,
1475
1243
  C: (f, a) => f(...a)
1476
1244
  });
1477
1245
  if (connection.state !== ConnectionState.INITIAL) {
1478
- (0, import_log3.log)("ignoring response", void 0, {
1479
- F: __dxlog_file3,
1246
+ (0, import_log2.log)("ignoring response", void 0, {
1247
+ F: __dxlog_file2,
1480
1248
  L: 185,
1481
1249
  S: this,
1482
1250
  C: (f, a) => f(...a)
@@ -1490,18 +1258,18 @@ var Peer = class {
1490
1258
  await connection.openConnection();
1491
1259
  this._callbacks.onAccepted();
1492
1260
  } catch (err) {
1493
- (0, import_log3.log)("initiation error", {
1261
+ (0, import_log2.log)("initiation error", {
1494
1262
  err,
1495
1263
  topic: this.topic,
1496
1264
  peerId: this.localPeerId,
1497
1265
  remoteId: this.id
1498
1266
  }, {
1499
- F: __dxlog_file3,
1267
+ F: __dxlog_file2,
1500
1268
  L: 196,
1501
1269
  S: this,
1502
1270
  C: (f, a) => f(...a)
1503
1271
  });
1504
- if (err instanceof import_protocols3.TimeoutError) {
1272
+ if (err instanceof import_protocols2.TimeoutError) {
1505
1273
  await connection.abort(err);
1506
1274
  }
1507
1275
  await this.closeConnection(err);
@@ -1515,20 +1283,20 @@ var Peer = class {
1515
1283
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
1516
1284
  */
1517
1285
  _createConnection(initiator, sessionId) {
1518
- (0, import_log3.log)("creating connection", {
1286
+ (0, import_log2.log)("creating connection", {
1519
1287
  topic: this.topic,
1520
1288
  peerId: this.localPeerId,
1521
1289
  remoteId: this.id,
1522
1290
  initiator,
1523
1291
  sessionId
1524
1292
  }, {
1525
- F: __dxlog_file3,
1293
+ F: __dxlog_file2,
1526
1294
  L: 213,
1527
1295
  S: this,
1528
1296
  C: (f, a) => f(...a)
1529
1297
  });
1530
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1531
- F: __dxlog_file3,
1298
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1299
+ F: __dxlog_file2,
1532
1300
  L: 220,
1533
1301
  S: this,
1534
1302
  A: [
@@ -1557,34 +1325,34 @@ var Peer = class {
1557
1325
  this._lastConnectionTime = Date.now();
1558
1326
  this._callbacks.onConnected();
1559
1327
  this._connectionLimiter.doneConnecting(sessionId);
1560
- import_log3.log.trace("dxos.mesh.connection.connected", {
1328
+ import_log2.log.trace("dxos.mesh.connection.connected", {
1561
1329
  topic: this.topic,
1562
1330
  localPeerId: this.localPeerId,
1563
1331
  remotePeerId: this.id,
1564
1332
  sessionId,
1565
1333
  initiator
1566
1334
  }, {
1567
- F: __dxlog_file3,
1335
+ F: __dxlog_file2,
1568
1336
  L: 239,
1569
1337
  S: this,
1570
1338
  C: (f, a) => f(...a)
1571
1339
  });
1572
1340
  },
1573
1341
  onClosed: (err) => {
1574
- (0, import_log3.log)("connection closed", {
1342
+ (0, import_log2.log)("connection closed", {
1575
1343
  topic: this.topic,
1576
1344
  peerId: this.localPeerId,
1577
1345
  remoteId: this.id,
1578
1346
  initiator
1579
1347
  }, {
1580
- F: __dxlog_file3,
1348
+ F: __dxlog_file2,
1581
1349
  L: 248,
1582
1350
  S: this,
1583
1351
  C: (f, a) => f(...a)
1584
1352
  });
1585
1353
  this._connectionLimiter.doneConnecting(sessionId);
1586
- (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1587
- F: __dxlog_file3,
1354
+ (0, import_invariant2.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1355
+ F: __dxlog_file2,
1588
1356
  L: 253,
1589
1357
  S: this,
1590
1358
  A: [
@@ -1592,14 +1360,14 @@ var Peer = class {
1592
1360
  "'Connection mismatch (race condition).'"
1593
1361
  ]
1594
1362
  });
1595
- import_log3.log.trace("dxos.mesh.connection.closed", {
1363
+ import_log2.log.trace("dxos.mesh.connection.closed", {
1596
1364
  topic: this.topic,
1597
1365
  localPeerId: this.localPeerId,
1598
1366
  remotePeerId: this.id,
1599
1367
  sessionId,
1600
1368
  initiator
1601
1369
  }, {
1602
- F: __dxlog_file3,
1370
+ F: __dxlog_file2,
1603
1371
  L: 255,
1604
1372
  S: this,
1605
1373
  C: (f, a) => f(...a)
@@ -1627,19 +1395,19 @@ var Peer = class {
1627
1395
  void this._connectionCtx?.dispose();
1628
1396
  this._connectionCtx = this._ctx.derive();
1629
1397
  connection.errors.handle((err) => {
1630
- import_log3.log.warn("connection error", {
1398
+ import_log2.log.warn("connection error", {
1631
1399
  topic: this.topic,
1632
1400
  peerId: this.localPeerId,
1633
1401
  remoteId: this.id,
1634
1402
  initiator,
1635
1403
  err
1636
1404
  }, {
1637
- F: __dxlog_file3,
1405
+ F: __dxlog_file2,
1638
1406
  L: 295,
1639
1407
  S: this,
1640
1408
  C: (f, a) => f(...a)
1641
1409
  });
1642
- import_log3.log.trace("dxos.mesh.connection.error", {
1410
+ import_log2.log.trace("dxos.mesh.connection.error", {
1643
1411
  topic: this.topic,
1644
1412
  localPeerId: this.localPeerId,
1645
1413
  remotePeerId: this.id,
@@ -1647,7 +1415,7 @@ var Peer = class {
1647
1415
  initiator,
1648
1416
  err
1649
1417
  }, {
1650
- F: __dxlog_file3,
1418
+ F: __dxlog_file2,
1651
1419
  L: 296,
1652
1420
  S: this,
1653
1421
  C: (f, a) => f(...a)
@@ -1662,21 +1430,21 @@ var Peer = class {
1662
1430
  return;
1663
1431
  }
1664
1432
  const connection = this.connection;
1665
- (0, import_log3.log)("closing...", {
1433
+ (0, import_log2.log)("closing...", {
1666
1434
  peerId: this.id,
1667
1435
  sessionId: connection.sessionId
1668
1436
  }, {
1669
- F: __dxlog_file3,
1437
+ F: __dxlog_file2,
1670
1438
  L: 321,
1671
1439
  S: this,
1672
1440
  C: (f, a) => f(...a)
1673
1441
  });
1674
1442
  await connection.close(err);
1675
- (0, import_log3.log)("closed", {
1443
+ (0, import_log2.log)("closed", {
1676
1444
  peerId: this.id,
1677
1445
  sessionId: connection.sessionId
1678
1446
  }, {
1679
- F: __dxlog_file3,
1447
+ F: __dxlog_file2,
1680
1448
  L: 327,
1681
1449
  S: this,
1682
1450
  C: (f, a) => f(...a)
@@ -1684,10 +1452,10 @@ var Peer = class {
1684
1452
  }
1685
1453
  async onSignal(message) {
1686
1454
  if (!this.connection) {
1687
- (0, import_log3.log)("dropping signal message for non-existent connection", {
1455
+ (0, import_log2.log)("dropping signal message for non-existent connection", {
1688
1456
  message
1689
1457
  }, {
1690
- F: __dxlog_file3,
1458
+ F: __dxlog_file2,
1691
1459
  L: 332,
1692
1460
  S: this,
1693
1461
  C: (f, a) => f(...a)
@@ -1698,11 +1466,11 @@ var Peer = class {
1698
1466
  }
1699
1467
  async destroy() {
1700
1468
  await this._ctx.dispose();
1701
- (0, import_log3.log)("Destroying peer", {
1469
+ (0, import_log2.log)("Destroying peer", {
1702
1470
  peerId: this.id,
1703
1471
  topic: this.topic
1704
1472
  }, {
1705
- F: __dxlog_file3,
1473
+ F: __dxlog_file2,
1706
1474
  L: 342,
1707
1475
  S: this,
1708
1476
  C: (f, a) => f(...a)
@@ -1726,6 +1494,238 @@ var increaseInterval = (interval) => {
1726
1494
  return 1e4;
1727
1495
  };
1728
1496
 
1497
+ // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1498
+ var import_context3 = require("@dxos/context");
1499
+ var import_invariant3 = require("@dxos/invariant");
1500
+ var import_keys3 = require("@dxos/keys");
1501
+ var import_log3 = require("@dxos/log");
1502
+ var import_protocols3 = require("@dxos/protocols");
1503
+ var import_util = require("@dxos/util");
1504
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1505
+ var SwarmMessage = import_protocols3.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1506
+ var SwarmMessenger = class {
1507
+ constructor({ sendMessage, onSignal, onOffer, topic }) {
1508
+ this._ctx = new import_context3.Context();
1509
+ this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1510
+ this._sendMessage = sendMessage;
1511
+ this._onSignal = onSignal;
1512
+ this._onOffer = onOffer;
1513
+ this._topic = topic;
1514
+ }
1515
+ async receiveMessage({ author, recipient, payload }) {
1516
+ if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1517
+ return;
1518
+ }
1519
+ const message = SwarmMessage.decode(payload.value);
1520
+ if (!this._topic.equals(message.topic)) {
1521
+ return;
1522
+ }
1523
+ (0, import_log3.log)("received", {
1524
+ from: author,
1525
+ to: recipient,
1526
+ msg: message
1527
+ }, {
1528
+ F: __dxlog_file3,
1529
+ L: 69,
1530
+ S: this,
1531
+ C: (f, a) => f(...a)
1532
+ });
1533
+ if (message.data?.offer) {
1534
+ await this._handleOffer({
1535
+ author,
1536
+ recipient,
1537
+ message
1538
+ });
1539
+ } else if (message.data?.answer) {
1540
+ await this._resolveAnswers(message);
1541
+ } else if (message.data?.signal) {
1542
+ await this._handleSignal({
1543
+ author,
1544
+ recipient,
1545
+ message
1546
+ });
1547
+ } else if (message.data?.signalBatch) {
1548
+ await this._handleSignal({
1549
+ author,
1550
+ recipient,
1551
+ message
1552
+ });
1553
+ } else {
1554
+ import_log3.log.warn("unknown message", {
1555
+ message
1556
+ }, {
1557
+ F: __dxlog_file3,
1558
+ L: 80,
1559
+ S: this,
1560
+ C: (f, a) => f(...a)
1561
+ });
1562
+ }
1563
+ }
1564
+ async signal(message) {
1565
+ (0, import_invariant3.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1566
+ F: __dxlog_file3,
1567
+ L: 85,
1568
+ S: this,
1569
+ A: [
1570
+ "message.data?.signal || message.data?.signalBatch",
1571
+ "'Invalid message'"
1572
+ ]
1573
+ });
1574
+ await this._sendReliableMessage({
1575
+ author: message.author,
1576
+ recipient: message.recipient,
1577
+ message
1578
+ });
1579
+ }
1580
+ async offer(message) {
1581
+ const networkMessage = {
1582
+ ...message,
1583
+ messageId: import_keys3.PublicKey.random()
1584
+ };
1585
+ return new Promise((resolve, reject) => {
1586
+ this._offerRecords.set(networkMessage.messageId, {
1587
+ resolve
1588
+ });
1589
+ this._sendReliableMessage({
1590
+ author: message.author,
1591
+ recipient: message.recipient,
1592
+ message: networkMessage
1593
+ }).catch((err) => reject(err));
1594
+ });
1595
+ }
1596
+ async _sendReliableMessage({ author, recipient, message }) {
1597
+ const networkMessage = {
1598
+ ...message,
1599
+ // Setting unique message_id if it not specified yet.
1600
+ messageId: message.messageId ?? import_keys3.PublicKey.random()
1601
+ };
1602
+ (0, import_log3.log)("sending", {
1603
+ from: author,
1604
+ to: recipient,
1605
+ msg: networkMessage
1606
+ }, {
1607
+ F: __dxlog_file3,
1608
+ L: 123,
1609
+ S: this,
1610
+ C: (f, a) => f(...a)
1611
+ });
1612
+ await this._sendMessage({
1613
+ author,
1614
+ recipient,
1615
+ payload: {
1616
+ type_url: "dxos.mesh.swarm.SwarmMessage",
1617
+ value: SwarmMessage.encode(networkMessage)
1618
+ }
1619
+ });
1620
+ }
1621
+ async _resolveAnswers(message) {
1622
+ (0, import_invariant3.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1623
+ F: __dxlog_file3,
1624
+ L: 135,
1625
+ S: this,
1626
+ A: [
1627
+ "message.data?.answer?.offerMessageId",
1628
+ "'No offerMessageId'"
1629
+ ]
1630
+ });
1631
+ const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1632
+ if (offerRecord) {
1633
+ this._offerRecords.delete(message.data.answer.offerMessageId);
1634
+ (0, import_invariant3.invariant)(message.data?.answer, "No answer", {
1635
+ F: __dxlog_file3,
1636
+ L: 139,
1637
+ S: this,
1638
+ A: [
1639
+ "message.data?.answer",
1640
+ "'No answer'"
1641
+ ]
1642
+ });
1643
+ (0, import_log3.log)("resolving", {
1644
+ answer: message.data.answer
1645
+ }, {
1646
+ F: __dxlog_file3,
1647
+ L: 140,
1648
+ S: this,
1649
+ C: (f, a) => f(...a)
1650
+ });
1651
+ offerRecord.resolve(message.data.answer);
1652
+ }
1653
+ }
1654
+ async _handleOffer({ author, recipient, message }) {
1655
+ (0, import_invariant3.invariant)(message.data.offer, "No offer", {
1656
+ F: __dxlog_file3,
1657
+ L: 154,
1658
+ S: this,
1659
+ A: [
1660
+ "message.data.offer",
1661
+ "'No offer'"
1662
+ ]
1663
+ });
1664
+ const offerMessage = {
1665
+ author,
1666
+ recipient,
1667
+ ...message,
1668
+ data: {
1669
+ offer: message.data.offer
1670
+ }
1671
+ };
1672
+ const answer = await this._onOffer(offerMessage);
1673
+ answer.offerMessageId = message.messageId;
1674
+ try {
1675
+ await this._sendReliableMessage({
1676
+ author: recipient,
1677
+ recipient: author,
1678
+ message: {
1679
+ topic: message.topic,
1680
+ sessionId: message.sessionId,
1681
+ data: {
1682
+ answer
1683
+ }
1684
+ }
1685
+ });
1686
+ } catch (err) {
1687
+ import_log3.log.warn("error sending answer", {
1688
+ err
1689
+ }, {
1690
+ F: __dxlog_file3,
1691
+ L: 174,
1692
+ S: this,
1693
+ C: (f, a) => f(...a)
1694
+ });
1695
+ }
1696
+ }
1697
+ async _handleSignal({ author, recipient, message }) {
1698
+ (0, import_invariant3.invariant)(message.messageId, void 0, {
1699
+ F: __dxlog_file3,
1700
+ L: 187,
1701
+ S: this,
1702
+ A: [
1703
+ "message.messageId",
1704
+ ""
1705
+ ]
1706
+ });
1707
+ (0, import_invariant3.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1708
+ F: __dxlog_file3,
1709
+ L: 188,
1710
+ S: this,
1711
+ A: [
1712
+ "message.data.signal || message.data.signalBatch",
1713
+ "'Invalid message'"
1714
+ ]
1715
+ });
1716
+ const signalMessage = {
1717
+ author,
1718
+ recipient,
1719
+ ...message,
1720
+ data: {
1721
+ signal: message.data.signal,
1722
+ signalBatch: message.data.signalBatch
1723
+ }
1724
+ };
1725
+ await this._onSignal(signalMessage);
1726
+ }
1727
+ };
1728
+
1729
1729
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
1730
1730
  function _ts_decorate3(decorators, target, key, desc) {
1731
1731
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -2216,7 +2216,6 @@ var SwarmMapper = class {
2216
2216
  get peers() {
2217
2217
  return Array.from(this._peers.values());
2218
2218
  }
2219
- // prettier-ignore
2220
2219
  constructor(_swarm) {
2221
2220
  this._swarm = _swarm;
2222
2221
  this._subscriptions = new import_async4.EventSubscriptions();
@@ -2239,7 +2238,7 @@ var SwarmMapper = class {
2239
2238
  _update() {
2240
2239
  (0, import_log5.log)("updating swarm", void 0, {
2241
2240
  F: __dxlog_file5,
2242
- L: 75,
2241
+ L: 72,
2243
2242
  S: this,
2244
2243
  C: (f, a) => f(...a)
2245
2244
  });
@@ -2263,7 +2262,7 @@ var SwarmMapper = class {
2263
2262
  totalPeersInSwarm: this._peers.size
2264
2263
  }, {
2265
2264
  F: __dxlog_file5,
2266
- L: 116,
2265
+ L: 113,
2267
2266
  S: this,
2268
2267
  C: (f, a) => f(...a)
2269
2268
  });
@@ -2823,7 +2822,6 @@ var import_invariant9 = require("@dxos/invariant");
2823
2822
  var import_log9 = require("@dxos/log");
2824
2823
  var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2825
2824
  var StarTopology = class {
2826
- // prettier-ignore
2827
2825
  constructor(_centralPeer) {
2828
2826
  this._centralPeer = _centralPeer;
2829
2827
  }
@@ -2833,7 +2831,7 @@ var StarTopology = class {
2833
2831
  init(controller) {
2834
2832
  (0, import_invariant9.invariant)(!this._controller, "Already initialized.", {
2835
2833
  F: __dxlog_file10,
2836
- L: 24,
2834
+ L: 21,
2837
2835
  S: this,
2838
2836
  A: [
2839
2837
  "!this._controller",
@@ -2845,7 +2843,7 @@ var StarTopology = class {
2845
2843
  update() {
2846
2844
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2847
2845
  F: __dxlog_file10,
2848
- L: 29,
2846
+ L: 26,
2849
2847
  S: this,
2850
2848
  A: [
2851
2849
  "this._controller",
@@ -2856,7 +2854,7 @@ var StarTopology = class {
2856
2854
  if (!ownPeerId.equals(this._centralPeer)) {
2857
2855
  (0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
2858
2856
  F: __dxlog_file10,
2859
- L: 32,
2857
+ L: 29,
2860
2858
  S: this,
2861
2859
  C: (f, a) => f(...a)
2862
2860
  });
@@ -2866,7 +2864,7 @@ var StarTopology = class {
2866
2864
  peer
2867
2865
  }, {
2868
2866
  F: __dxlog_file10,
2869
- L: 37,
2867
+ L: 34,
2870
2868
  S: this,
2871
2869
  C: (f, a) => f(...a)
2872
2870
  });
@@ -2880,7 +2878,7 @@ var StarTopology = class {
2880
2878
  peer
2881
2879
  }, {
2882
2880
  F: __dxlog_file10,
2883
- L: 46,
2881
+ L: 43,
2884
2882
  S: this,
2885
2883
  C: (f, a) => f(...a)
2886
2884
  });
@@ -2891,7 +2889,7 @@ var StarTopology = class {
2891
2889
  async onOffer(peer) {
2892
2890
  (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2893
2891
  F: __dxlog_file10,
2894
- L: 53,
2892
+ L: 50,
2895
2893
  S: this,
2896
2894
  A: [
2897
2895
  "this._controller",
@@ -2905,7 +2903,7 @@ var StarTopology = class {
2905
2903
  isSelfCentral: ownPeerId.equals(this._centralPeer)
2906
2904
  }, {
2907
2905
  F: __dxlog_file10,
2908
- L: 55,
2906
+ L: 52,
2909
2907
  S: this,
2910
2908
  C: (f, a) => f(...a)
2911
2909
  });
@@ -3197,9 +3195,13 @@ var SimplePeerTransport = class {
3197
3195
  } else {
3198
3196
  this.errors.raise(new import_protocols7.UnknownProtocolError("unknown RTCError", err));
3199
3197
  }
3200
- } else if (err.name === "InvalidStateError") {
3201
- this.errors.raise(new import_protocols7.ConnectionResetError("safari WebRTC error", err));
3202
3198
  } else if ("code" in err) {
3199
+ import_log11.log.info("simple-peer error", err, {
3200
+ F: __dxlog_file12,
3201
+ L: 77,
3202
+ S: this,
3203
+ C: (f, a) => f(...a)
3204
+ });
3203
3205
  switch (err.code) {
3204
3206
  case "ERR_WEBRTC_SUPPORT":
3205
3207
  this.errors.raise(new import_protocols7.ProtocolError("WebRTC not supported", err));
@@ -3218,17 +3220,23 @@ var SimplePeerTransport = class {
3218
3220
  this.errors.raise(new Error("unknown simple-peer error"));
3219
3221
  }
3220
3222
  } else {
3223
+ import_log11.log.info("unknown peer connection error", err, {
3224
+ F: __dxlog_file12,
3225
+ L: 96,
3226
+ S: this,
3227
+ C: (f, a) => f(...a)
3228
+ });
3221
3229
  this.errors.raise(err);
3222
3230
  }
3223
3231
  try {
3224
- if (typeof this._peer?._pc.getStats === "function") {
3232
+ if (typeof this._peer?._pc?.getStats === "function") {
3225
3233
  this._peer._pc.getStats().then((stats) => {
3226
3234
  import_log11.log.warn("report after webrtc error", {
3227
3235
  config: this.params.webrtcConfig,
3228
3236
  stats
3229
3237
  }, {
3230
3238
  F: __dxlog_file12,
3231
- L: 106,
3239
+ L: 104,
3232
3240
  S: this,
3233
3241
  C: (f, a) => f(...a)
3234
3242
  });
@@ -3237,7 +3245,7 @@ var SimplePeerTransport = class {
3237
3245
  } catch (err2) {
3238
3246
  import_log11.log.catch(err2, void 0, {
3239
3247
  F: __dxlog_file12,
3240
- L: 113,
3248
+ L: 111,
3241
3249
  S: this,
3242
3250
  C: (f, a) => f(...a)
3243
3251
  });
@@ -3248,7 +3256,7 @@ var SimplePeerTransport = class {
3248
3256
  id: this._instanceId
3249
3257
  }), {
3250
3258
  F: __dxlog_file12,
3251
- L: 118,
3259
+ L: 116,
3252
3260
  S: this,
3253
3261
  C: (f, a) => f(...a)
3254
3262
  });
@@ -3256,7 +3264,7 @@ var SimplePeerTransport = class {
3256
3264
  async destroy() {
3257
3265
  (0, import_log11.log)("closing...", void 0, {
3258
3266
  F: __dxlog_file12,
3259
- L: 122,
3267
+ L: 120,
3260
3268
  S: this,
3261
3269
  C: (f, a) => f(...a)
3262
3270
  });
@@ -3266,7 +3274,7 @@ var SimplePeerTransport = class {
3266
3274
  this.closed.emit();
3267
3275
  (0, import_log11.log)("closed", void 0, {
3268
3276
  F: __dxlog_file12,
3269
- L: 127,
3277
+ L: 125,
3270
3278
  S: this,
3271
3279
  C: (f, a) => f(...a)
3272
3280
  });
@@ -3299,7 +3307,6 @@ var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
3299
3307
  var import_util8 = require("@dxos/util");
3300
3308
  var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3301
3309
  var SimplePeerTransportService = class {
3302
- // prettier-ignore
3303
3310
  constructor(_webrtcConfig) {
3304
3311
  this._webrtcConfig = _webrtcConfig;
3305
3312
  this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
@@ -3379,7 +3386,7 @@ var SimplePeerTransportService = class {
3379
3386
  async sendSignal({ proxyId, signal }) {
3380
3387
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3381
3388
  F: __dxlog_file13,
3382
- L: 113,
3389
+ L: 110,
3383
3390
  S: this,
3384
3391
  A: [
3385
3392
  "this.transports.has(proxyId)",
@@ -3391,7 +3398,7 @@ var SimplePeerTransportService = class {
3391
3398
  async sendData({ proxyId, payload }) {
3392
3399
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3393
3400
  F: __dxlog_file13,
3394
- L: 118,
3401
+ L: 115,
3395
3402
  S: this,
3396
3403
  A: [
3397
3404
  "this.transports.has(proxyId)",
@@ -3412,7 +3419,7 @@ var SimplePeerTransportService = class {
3412
3419
  this.transports.delete(proxyId);
3413
3420
  (0, import_log12.log)("Closed.", void 0, {
3414
3421
  F: __dxlog_file13,
3415
- L: 132,
3422
+ L: 129,
3416
3423
  S: this,
3417
3424
  C: (f, a) => f(...a)
3418
3425
  });
@@ -3432,7 +3439,6 @@ var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
3432
3439
  var import_util9 = require("@dxos/util");
3433
3440
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3434
3441
  var SimplePeerTransportProxy = class {
3435
- // prettier-ignore
3436
3442
  constructor(_params) {
3437
3443
  this._params = _params;
3438
3444
  this._proxyId = import_keys12.PublicKey.random();
@@ -3449,7 +3455,7 @@ var SimplePeerTransportProxy = class {
3449
3455
  this._serviceStream.subscribe(async (event) => {
3450
3456
  (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
3451
3457
  F: __dxlog_file14,
3452
- L: 58,
3458
+ L: 55,
3453
3459
  S: this,
3454
3460
  C: (f, a) => f(...a)
3455
3461
  });
@@ -3478,7 +3484,7 @@ var SimplePeerTransportProxy = class {
3478
3484
  }));
3479
3485
  }, (error) => import_log13.log.catch(error, void 0, {
3480
3486
  F: __dxlog_file14,
3481
- L: 82,
3487
+ L: 83,
3482
3488
  S: this,
3483
3489
  C: (f, a) => f(...a)
3484
3490
  }));
@@ -3524,7 +3530,7 @@ var SimplePeerTransportProxy = class {
3524
3530
  } catch (err) {
3525
3531
  import_log13.log.catch(err, void 0, {
3526
3532
  F: __dxlog_file14,
3527
- L: 133,
3533
+ L: 134,
3528
3534
  S: this,
3529
3535
  C: (f, a) => f(...a)
3530
3536
  });
@@ -3560,7 +3566,7 @@ var SimplePeerTransportProxyFactory = class {
3560
3566
  createTransport(options) {
3561
3567
  (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3562
3568
  F: __dxlog_file14,
3563
- L: 170,
3569
+ L: 171,
3564
3570
  S: this,
3565
3571
  A: [
3566
3572
  "this._bridgeService",
@@ -3578,15 +3584,15 @@ var SimplePeerTransportProxyFactory = class {
3578
3584
  };
3579
3585
  var decodeError = (err) => {
3580
3586
  const message = typeof err === "string" ? err : err.message;
3581
- if (message.includes("ConnectionResetError")) {
3587
+ if (message.includes("CONNECTION_RESET")) {
3582
3588
  return new import_protocols8.ConnectionResetError(message);
3583
- } else if (message.includes("TimeoutError")) {
3589
+ } else if (message.includes("TIMEOUT")) {
3584
3590
  return new import_protocols8.TimeoutError(message);
3585
- } else if (message.includes("ProtocolError")) {
3591
+ } else if (message.includes("PROTOCOL_ERROR")) {
3586
3592
  return new import_protocols8.ProtocolError(message);
3587
- } else if (message.includes("ConnectivityError")) {
3593
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3588
3594
  return new import_protocols8.ConnectivityError(message);
3589
- } else if (message.includes("UnknownProtocolError")) {
3595
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3590
3596
  return new import_protocols8.UnknownProtocolError(message);
3591
3597
  } else {
3592
3598
  return typeof err === "string" ? new Error(err) : err;