@dxos/network-manager 0.6.1-main.e985410 → 0.6.1-main.faf01b2

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.
@@ -275,7 +275,9 @@ var Connection = class {
275
275
  }
276
276
  await this._ctx.dispose();
277
277
  try {
278
- await this._closeProtocol();
278
+ await this._closeProtocol({
279
+ abort: true
280
+ });
279
281
  } catch (err2) {
280
282
  log.catch(err2, void 0, {
281
283
  F: __dxlog_file,
@@ -327,80 +329,63 @@ var Connection = class {
327
329
  S: this,
328
330
  C: (f, a) => f(...a)
329
331
  });
330
- if (lastState === "CONNECTED") {
331
- try {
332
- await this._closeProtocol();
333
- } catch (err2) {
334
- log.catch(err2, void 0, {
335
- F: __dxlog_file,
336
- L: 327,
337
- S: this,
338
- C: (f, a) => f(...a)
339
- });
340
- }
341
- try {
342
- await this._closeTransport();
343
- } catch (err2) {
344
- log.catch(err2, void 0, {
345
- F: __dxlog_file,
346
- L: 334,
347
- S: this,
348
- C: (f, a) => f(...a)
349
- });
350
- }
351
- } else {
332
+ let abortProtocol = false;
333
+ if (lastState !== "CONNECTED") {
352
334
  log(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
353
335
  F: __dxlog_file,
354
- L: 337,
336
+ L: 324,
337
+ S: this,
338
+ C: (f, a) => f(...a)
339
+ });
340
+ abortProtocol = true;
341
+ }
342
+ try {
343
+ await this._closeProtocol({
344
+ abort: abortProtocol
345
+ });
346
+ } catch (err2) {
347
+ log.catch(err2, void 0, {
348
+ F: __dxlog_file,
349
+ L: 330,
350
+ S: this,
351
+ C: (f, a) => f(...a)
352
+ });
353
+ }
354
+ try {
355
+ await this._closeTransport();
356
+ } catch (err2) {
357
+ log.catch(err2, void 0, {
358
+ F: __dxlog_file,
359
+ L: 336,
355
360
  S: this,
356
361
  C: (f, a) => f(...a)
357
362
  });
358
- try {
359
- await this._closeProtocol();
360
- } catch (err2) {
361
- log.catch(err2, void 0, {
362
- F: __dxlog_file,
363
- L: 341,
364
- S: this,
365
- C: (f, a) => f(...a)
366
- });
367
- }
368
- try {
369
- await this._closeTransport();
370
- } catch (err2) {
371
- log.catch(err2, void 0, {
372
- F: __dxlog_file,
373
- L: 346,
374
- S: this,
375
- C: (f, a) => f(...a)
376
- });
377
- }
378
363
  }
379
364
  log("closed", {
380
365
  peerId: this.ownId
381
366
  }, {
382
367
  F: __dxlog_file,
383
- L: 350,
368
+ L: 339,
384
369
  S: this,
385
370
  C: (f, a) => f(...a)
386
371
  });
387
372
  this._changeState("CLOSED");
388
373
  this._callbacks?.onClosed?.(err);
389
374
  }
390
- async _closeProtocol() {
391
- log("closing protocol", void 0, {
375
+ async _closeProtocol(options) {
376
+ log("closing protocol", options, {
392
377
  F: __dxlog_file,
393
- L: 356,
378
+ L: 345,
394
379
  S: this,
395
380
  C: (f, a) => f(...a)
396
381
  });
397
382
  await Promise.race([
398
- this._protocol.close(),
383
+ options?.abort ? this._protocol.abort() : this._protocol.close(),
399
384
  this._protocolClosed.wait()
400
385
  ]);
401
- log("protocol closed", void 0, {
386
+ log("protocol closed", options, {
402
387
  F: __dxlog_file,
403
- L: 358,
388
+ L: 347,
404
389
  S: this,
405
390
  C: (f, a) => f(...a)
406
391
  });
@@ -408,7 +393,7 @@ var Connection = class {
408
393
  async _closeTransport() {
409
394
  log("closing transport", void 0, {
410
395
  F: __dxlog_file,
411
- L: 362,
396
+ L: 351,
412
397
  S: this,
413
398
  C: (f, a) => f(...a)
414
399
  });
@@ -418,7 +403,7 @@ var Connection = class {
418
403
  ]);
419
404
  log("transport closed", void 0, {
420
405
  F: __dxlog_file,
421
- L: 364,
406
+ L: 353,
422
407
  S: this,
423
408
  C: (f, a) => f(...a)
424
409
  });
@@ -459,7 +444,7 @@ var Connection = class {
459
444
  err
460
445
  }, {
461
446
  F: __dxlog_file,
462
- L: 400,
447
+ L: 389,
463
448
  S: this,
464
449
  C: (f, a) => f(...a)
465
450
  });
@@ -472,7 +457,7 @@ var Connection = class {
472
457
  async signal(msg) {
473
458
  invariant(msg.sessionId, void 0, {
474
459
  F: __dxlog_file,
475
- L: 409,
460
+ L: 398,
476
461
  S: this,
477
462
  A: [
478
463
  "msg.sessionId",
@@ -482,7 +467,7 @@ var Connection = class {
482
467
  if (!msg.sessionId.equals(this.sessionId)) {
483
468
  log("dropping signal for incorrect session id", void 0, {
484
469
  F: __dxlog_file,
485
- L: 411,
470
+ L: 400,
486
471
  S: this,
487
472
  C: (f, a) => f(...a)
488
473
  });
@@ -490,7 +475,7 @@ var Connection = class {
490
475
  }
491
476
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
492
477
  F: __dxlog_file,
493
- L: 414,
478
+ L: 403,
494
479
  S: this,
495
480
  A: [
496
481
  "msg.data.signal || msg.data.signalBatch",
@@ -499,7 +484,7 @@ var Connection = class {
499
484
  });
500
485
  invariant(msg.author?.equals(this.remoteId), void 0, {
501
486
  F: __dxlog_file,
502
- L: 415,
487
+ L: 404,
503
488
  S: this,
504
489
  A: [
505
490
  "msg.author?.equals(this.remoteId)",
@@ -508,7 +493,7 @@ var Connection = class {
508
493
  });
509
494
  invariant(msg.recipient?.equals(this.ownId), void 0, {
510
495
  F: __dxlog_file,
511
- L: 416,
496
+ L: 405,
512
497
  S: this,
513
498
  A: [
514
499
  "msg.recipient?.equals(this.ownId)",
@@ -532,7 +517,7 @@ var Connection = class {
532
517
  msg: msg.data
533
518
  }, {
534
519
  F: __dxlog_file,
535
- L: 425,
520
+ L: 414,
536
521
  S: this,
537
522
  C: (f, a) => f(...a)
538
523
  });
@@ -540,7 +525,7 @@ var Connection = class {
540
525
  } else {
541
526
  invariant(this._transport, "Connection not ready to accept signals.", {
542
527
  F: __dxlog_file,
543
- L: 428,
528
+ L: 417,
544
529
  S: this,
545
530
  A: [
546
531
  "this._transport",
@@ -553,7 +538,7 @@ var Connection = class {
553
538
  msg: msg.data
554
539
  }, {
555
540
  F: __dxlog_file,
556
- L: 429,
541
+ L: 418,
557
542
  S: this,
558
543
  C: (f, a) => f(...a)
559
544
  });
@@ -571,13 +556,13 @@ var Connection = class {
571
556
  peerId: this.ownId
572
557
  }, {
573
558
  F: __dxlog_file,
574
- L: 440,
559
+ L: 429,
575
560
  S: this,
576
561
  C: (f, a) => f(...a)
577
562
  });
578
563
  invariant(state !== this._state, "Already in this state.", {
579
564
  F: __dxlog_file,
580
- L: 441,
565
+ L: 430,
581
566
  S: this,
582
567
  A: [
583
568
  "state !== this._state",
@@ -1316,7 +1301,7 @@ var Peer = class {
1316
1301
  }
1317
1302
  await this.connection.signal(message);
1318
1303
  }
1319
- async destroy(reason) {
1304
+ async safeDestroy(reason) {
1320
1305
  await this._ctx.dispose();
1321
1306
  log3("Destroying peer", {
1322
1307
  peerId: this.id,
@@ -1332,7 +1317,7 @@ var Peer = class {
1332
1317
  };
1333
1318
  _ts_decorate2([
1334
1319
  synchronized2
1335
- ], Peer.prototype, "destroy", null);
1320
+ ], Peer.prototype, "safeDestroy", null);
1336
1321
  var increaseInterval = (interval) => {
1337
1322
  if (interval === 0) {
1338
1323
  return 50;
@@ -1526,15 +1511,15 @@ var Swarm = class {
1526
1511
  }
1527
1512
  if (swarmEvent.peerAvailable) {
1528
1513
  const peerId = PublicKey4.from(swarmEvent.peerAvailable.peer);
1529
- log4("new peer", {
1530
- peerId
1531
- }, {
1532
- F: __dxlog_file4,
1533
- L: 180,
1534
- S: this,
1535
- C: (f, a) => f(...a)
1536
- });
1537
1514
  if (!peerId.equals(this._ownPeerId)) {
1515
+ log4("new peer", {
1516
+ peerId
1517
+ }, {
1518
+ F: __dxlog_file4,
1519
+ L: 181,
1520
+ S: this,
1521
+ C: (f, a) => f(...a)
1522
+ });
1538
1523
  const peer = this._getOrCreatePeer(peerId);
1539
1524
  peer.advertizing = true;
1540
1525
  }
@@ -1696,6 +1681,9 @@ var Swarm = class {
1696
1681
  this.connected.emit(peerId);
1697
1682
  },
1698
1683
  onDisconnected: async () => {
1684
+ if (this._isUnregistered(peer)) {
1685
+ return;
1686
+ }
1699
1687
  if (!peer.advertizing) {
1700
1688
  await this._destroyPeer(peer.id, "peer disconnected");
1701
1689
  }
@@ -1703,12 +1691,12 @@ var Swarm = class {
1703
1691
  this._topology.update();
1704
1692
  },
1705
1693
  onRejected: () => {
1706
- if (this._peers.has(peerId)) {
1694
+ if (!this._isUnregistered(peer)) {
1707
1695
  log4("peer rejected connection", {
1708
1696
  peerId
1709
1697
  }, {
1710
1698
  F: __dxlog_file4,
1711
- L: 286,
1699
+ L: 289,
1712
1700
  S: this,
1713
1701
  C: (f, a) => f(...a)
1714
1702
  });
@@ -1730,17 +1718,18 @@ var Swarm = class {
1730
1718
  return peer;
1731
1719
  }
1732
1720
  async _destroyPeer(peerId, reason) {
1733
- invariant4(this._peers.has(peerId), void 0, {
1721
+ const peer = this._peers.get(peerId);
1722
+ invariant4(peer, void 0, {
1734
1723
  F: __dxlog_file4,
1735
- L: 308,
1724
+ L: 312,
1736
1725
  S: this,
1737
1726
  A: [
1738
- "this._peers.has(peerId)",
1727
+ "peer",
1739
1728
  ""
1740
1729
  ]
1741
1730
  });
1742
- await this._peers.get(peerId).destroy(new Error(reason));
1743
1731
  this._peers.delete(peerId);
1732
+ await peer.safeDestroy(new Error(reason));
1744
1733
  }
1745
1734
  _getSwarmController() {
1746
1735
  return {
@@ -1760,7 +1749,7 @@ var Swarm = class {
1760
1749
  } catch (err) {
1761
1750
  log4("initiation error", err, {
1762
1751
  F: __dxlog_file4,
1763
- L: 335,
1752
+ L: 339,
1764
1753
  S: this,
1765
1754
  C: (f, a) => f(...a)
1766
1755
  });
@@ -1789,7 +1778,7 @@ var Swarm = class {
1789
1778
  remoteId
1790
1779
  }, {
1791
1780
  F: __dxlog_file4,
1792
- L: 363,
1781
+ L: 367,
1793
1782
  S: this,
1794
1783
  C: (f, a) => f(...a)
1795
1784
  });
@@ -1798,7 +1787,7 @@ var Swarm = class {
1798
1787
  if (ctx.disposed) {
1799
1788
  return;
1800
1789
  }
1801
- if (this._peers.get(remoteId) == null) {
1790
+ if (this._isUnregistered(peer)) {
1802
1791
  throw new Error("Peer left during initiation delay");
1803
1792
  }
1804
1793
  if (peer.connection) {
@@ -1808,7 +1797,7 @@ var Swarm = class {
1808
1797
  remoteId
1809
1798
  }, {
1810
1799
  F: __dxlog_file4,
1811
- L: 379,
1800
+ L: 383,
1812
1801
  S: this,
1813
1802
  C: (f, a) => f(...a)
1814
1803
  });
@@ -1818,7 +1807,7 @@ var Swarm = class {
1818
1807
  remoteId
1819
1808
  }, {
1820
1809
  F: __dxlog_file4,
1821
- L: 382,
1810
+ L: 386,
1822
1811
  S: this,
1823
1812
  C: (f, a) => f(...a)
1824
1813
  });
@@ -1830,6 +1819,9 @@ var Swarm = class {
1830
1819
  }
1831
1820
  await peer.closeConnection();
1832
1821
  }
1822
+ _isUnregistered(peer) {
1823
+ return !peer || this._peers.get(peer.id) !== peer;
1824
+ }
1833
1825
  };
1834
1826
  _ts_decorate3([
1835
1827
  logInfo2
@@ -2313,7 +2305,7 @@ var SwarmNetworkManager = class {
2313
2305
  this._connectionLog?.leftSwarm(swarm);
2314
2306
  await swarm.destroy();
2315
2307
  this._swarms.delete(topic);
2316
- await this.topicsUpdated.emit();
2308
+ this.topicsUpdated.emit();
2317
2309
  log7("left", {
2318
2310
  topic: PublicKey8.from(topic),
2319
2311
  count: this._swarms.size
@@ -4087,4 +4079,4 @@ export {
4087
4079
  TcpTransport,
4088
4080
  createTeleportProtocolFactory
4089
4081
  };
4090
- //# sourceMappingURL=chunk-VCGCTQ7O.mjs.map
4082
+ //# sourceMappingURL=chunk-3UBXH53L.mjs.map