@dxos/network-manager 0.8.1-main.ba2dec9 → 0.8.1-staging.31c3ee1

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 (32) hide show
  1. package/dist/lib/browser/{chunk-WXFQML2V.mjs → chunk-BHB7ZFZF.mjs} +113 -66
  2. package/dist/lib/browser/chunk-BHB7ZFZF.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 +1 -1
  6. package/dist/lib/node/{chunk-FNZNGA6E.cjs → chunk-D2HGDGMO.cjs} +115 -68
  7. package/dist/lib/node/chunk-D2HGDGMO.cjs.map +7 -0
  8. package/dist/lib/node/index.cjs +23 -23
  9. package/dist/lib/node/index.cjs.map +1 -1
  10. package/dist/lib/node/meta.json +1 -1
  11. package/dist/lib/node/testing/index.cjs +14 -14
  12. package/dist/lib/node-esm/{chunk-6IKF5FQP.mjs → chunk-NHFGO4HB.mjs} +113 -66
  13. package/dist/lib/node-esm/chunk-NHFGO4HB.mjs.map +7 -0
  14. package/dist/lib/node-esm/index.mjs +1 -1
  15. package/dist/lib/node-esm/meta.json +1 -1
  16. package/dist/lib/node-esm/testing/index.mjs +1 -1
  17. package/dist/types/src/swarm/connection.d.ts +4 -1
  18. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  19. package/dist/types/src/swarm/peer.d.ts +1 -1
  20. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  21. package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
  22. package/dist/types/src/swarm/swarm.d.ts +1 -0
  23. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  24. package/package.json +18 -18
  25. package/src/swarm/connection.ts +15 -10
  26. package/src/swarm/peer.ts +3 -3
  27. package/src/swarm/swarm-mapper.ts +4 -5
  28. package/src/swarm/swarm.test.ts +16 -0
  29. package/src/swarm/swarm.ts +16 -2
  30. package/dist/lib/browser/chunk-WXFQML2V.mjs.map +0 -7
  31. package/dist/lib/node/chunk-FNZNGA6E.cjs.map +0 -7
  32. package/dist/lib/node-esm/chunk-6IKF5FQP.mjs.map +0 -7
@@ -132,7 +132,9 @@ var Connection = class {
132
132
  C: (f, a) => f(...a)
133
133
  });
134
134
  this._protocolClosed.wake();
135
- this.close(new ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
135
+ this.close({
136
+ error: new ProtocolError("protocol stream closed")
137
+ }).catch((err) => this.errors.raise(err));
136
138
  });
137
139
  scheduleTask(this.connectedTimeoutContext, async () => {
138
140
  log.info(`timeout waiting ${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect, aborting`, void 0, {
@@ -227,7 +229,7 @@ var Connection = class {
227
229
  });
228
230
  }
229
231
  async abort(err) {
230
- log("aborting...", {
232
+ log("abort", {
231
233
  err
232
234
  }, {
233
235
  F: __dxlog_file,
@@ -250,6 +252,15 @@ var Connection = class {
250
252
  this.closeReason = err?.message;
251
253
  }
252
254
  await this._ctx.dispose();
255
+ log("aborting...", {
256
+ peerId: this.localInfo,
257
+ err
258
+ }, {
259
+ F: __dxlog_file,
260
+ L: 279,
261
+ S: this,
262
+ C: (f, a) => f(...a)
263
+ });
253
264
  try {
254
265
  await this._closeProtocol({
255
266
  abort: true
@@ -257,7 +268,7 @@ var Connection = class {
257
268
  } catch (err2) {
258
269
  log.catch(err2, void 0, {
259
270
  F: __dxlog_file,
260
- L: 283,
271
+ L: 285,
261
272
  S: this,
262
273
  C: (f, a) => f(...a)
263
274
  });
@@ -267,7 +278,7 @@ var Connection = class {
267
278
  } catch (err2) {
268
279
  log.catch(err2, void 0, {
269
280
  F: __dxlog_file,
270
- L: 290,
281
+ L: 292,
271
282
  S: this,
272
283
  C: (f, a) => f(...a)
273
284
  });
@@ -277,62 +288,81 @@ var Connection = class {
277
288
  } catch (err2) {
278
289
  log.catch(err2, void 0, {
279
290
  F: __dxlog_file,
280
- L: 296,
291
+ L: 298,
281
292
  S: this,
282
293
  C: (f, a) => f(...a)
283
294
  });
284
295
  }
285
296
  this._changeState("ABORTED");
286
297
  }
287
- async close(err) {
298
+ async close({ error, reason } = {}) {
299
+ log("close", {
300
+ error
301
+ }, {
302
+ F: __dxlog_file,
303
+ L: 305,
304
+ S: this,
305
+ C: (f, a) => f(...a)
306
+ });
288
307
  if (!this.closeReason) {
289
- this.closeReason = err?.message;
308
+ this.closeReason = reason ?? error?.message;
290
309
  } else {
291
- this.closeReason += `; ${err?.message}`;
310
+ this.closeReason += `; ${reason ?? error?.message}`;
292
311
  }
293
312
  if (this._state === "CLOSED" || this._state === "ABORTING" || this._state === "ABORTED") {
313
+ log("close ignored: already in progress", {
314
+ state: this._state,
315
+ error
316
+ }, {
317
+ F: __dxlog_file,
318
+ L: 316,
319
+ S: this,
320
+ C: (f, a) => f(...a)
321
+ });
294
322
  return;
295
323
  }
296
324
  const lastState = this._state;
297
325
  this._changeState("CLOSING");
298
326
  await this.connectedTimeoutContext.dispose();
299
327
  await this._ctx.dispose();
300
- log("closing...", {
301
- peerId: this.localInfo
302
- }, {
303
- F: __dxlog_file,
304
- L: 321,
305
- S: this,
306
- C: (f, a) => f(...a)
307
- });
308
328
  let abortProtocol = false;
309
- if (lastState !== "CONNECTED") {
329
+ if (lastState !== "CONNECTED" || error != null) {
310
330
  log(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
311
331
  F: __dxlog_file,
312
- L: 325,
332
+ L: 327,
313
333
  S: this,
314
334
  C: (f, a) => f(...a)
315
335
  });
316
336
  abortProtocol = true;
317
337
  }
338
+ log("closing...", {
339
+ peerId: this.localInfo,
340
+ abortProtocol,
341
+ error
342
+ }, {
343
+ F: __dxlog_file,
344
+ L: 331,
345
+ S: this,
346
+ C: (f, a) => f(...a)
347
+ });
318
348
  try {
319
349
  await this._closeProtocol({
320
350
  abort: abortProtocol
321
351
  });
322
- } catch (err2) {
323
- log.catch(err2, void 0, {
352
+ } catch (err) {
353
+ log.catch(err, void 0, {
324
354
  F: __dxlog_file,
325
- L: 331,
355
+ L: 336,
326
356
  S: this,
327
357
  C: (f, a) => f(...a)
328
358
  });
329
359
  }
330
360
  try {
331
361
  await this._closeTransport();
332
- } catch (err2) {
333
- log.catch(err2, void 0, {
362
+ } catch (err) {
363
+ log.catch(err, void 0, {
334
364
  F: __dxlog_file,
335
- L: 337,
365
+ L: 342,
336
366
  S: this,
337
367
  C: (f, a) => f(...a)
338
368
  });
@@ -341,17 +371,17 @@ var Connection = class {
341
371
  peerId: this.localInfo
342
372
  }, {
343
373
  F: __dxlog_file,
344
- L: 340,
374
+ L: 345,
345
375
  S: this,
346
376
  C: (f, a) => f(...a)
347
377
  });
348
378
  this._changeState("CLOSED");
349
- this._callbacks?.onClosed?.(err);
379
+ this._callbacks?.onClosed?.(error);
350
380
  }
351
381
  async _closeProtocol(options) {
352
382
  log("closing protocol", options, {
353
383
  F: __dxlog_file,
354
- L: 346,
384
+ L: 351,
355
385
  S: this,
356
386
  C: (f, a) => f(...a)
357
387
  });
@@ -361,7 +391,7 @@ var Connection = class {
361
391
  ]);
362
392
  log("protocol closed", options, {
363
393
  F: __dxlog_file,
364
- L: 348,
394
+ L: 353,
365
395
  S: this,
366
396
  C: (f, a) => f(...a)
367
397
  });
@@ -369,7 +399,7 @@ var Connection = class {
369
399
  async _closeTransport() {
370
400
  log("closing transport", void 0, {
371
401
  F: __dxlog_file,
372
- L: 352,
402
+ L: 357,
373
403
  S: this,
374
404
  C: (f, a) => f(...a)
375
405
  });
@@ -379,7 +409,7 @@ var Connection = class {
379
409
  ]);
380
410
  log("transport closed", void 0, {
381
411
  F: __dxlog_file,
382
- L: 354,
412
+ L: 359,
383
413
  S: this,
384
414
  C: (f, a) => f(...a)
385
415
  });
@@ -420,11 +450,13 @@ var Connection = class {
420
450
  err
421
451
  }, {
422
452
  F: __dxlog_file,
423
- L: 394,
453
+ L: 399,
424
454
  S: this,
425
455
  C: (f, a) => f(...a)
426
456
  });
427
- await this.close(new ConnectivityError("signal message failed to deliver", err));
457
+ await this.close({
458
+ error: new ConnectivityError("signal message failed to deliver", err)
459
+ });
428
460
  }
429
461
  }
430
462
  /**
@@ -433,7 +465,7 @@ var Connection = class {
433
465
  async signal(msg) {
434
466
  invariant(msg.sessionId, void 0, {
435
467
  F: __dxlog_file,
436
- L: 403,
468
+ L: 408,
437
469
  S: this,
438
470
  A: [
439
471
  "msg.sessionId",
@@ -443,7 +475,7 @@ var Connection = class {
443
475
  if (!msg.sessionId.equals(this.sessionId)) {
444
476
  log("dropping signal for incorrect session id", void 0, {
445
477
  F: __dxlog_file,
446
- L: 405,
478
+ L: 410,
447
479
  S: this,
448
480
  C: (f, a) => f(...a)
449
481
  });
@@ -451,7 +483,7 @@ var Connection = class {
451
483
  }
452
484
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
453
485
  F: __dxlog_file,
454
- L: 408,
486
+ L: 413,
455
487
  S: this,
456
488
  A: [
457
489
  "msg.data.signal || msg.data.signalBatch",
@@ -460,7 +492,7 @@ var Connection = class {
460
492
  });
461
493
  invariant(msg.author.peerKey === this.remoteInfo.peerKey, void 0, {
462
494
  F: __dxlog_file,
463
- L: 409,
495
+ L: 414,
464
496
  S: this,
465
497
  A: [
466
498
  "msg.author.peerKey === this.remoteInfo.peerKey",
@@ -469,7 +501,7 @@ var Connection = class {
469
501
  });
470
502
  invariant(msg.recipient.peerKey === this.localInfo.peerKey, void 0, {
471
503
  F: __dxlog_file,
472
- L: 410,
504
+ L: 415,
473
505
  S: this,
474
506
  A: [
475
507
  "msg.recipient.peerKey === this.localInfo.peerKey",
@@ -493,7 +525,7 @@ var Connection = class {
493
525
  msg: msg.data
494
526
  }, {
495
527
  F: __dxlog_file,
496
- L: 419,
528
+ L: 424,
497
529
  S: this,
498
530
  C: (f, a) => f(...a)
499
531
  });
@@ -501,7 +533,7 @@ var Connection = class {
501
533
  } else {
502
534
  invariant(this._transport, "Connection not ready to accept signals.", {
503
535
  F: __dxlog_file,
504
- L: 422,
536
+ L: 427,
505
537
  S: this,
506
538
  A: [
507
539
  "this._transport",
@@ -514,7 +546,7 @@ var Connection = class {
514
546
  msg: msg.data
515
547
  }, {
516
548
  F: __dxlog_file,
517
- L: 423,
549
+ L: 428,
518
550
  S: this,
519
551
  C: (f, a) => f(...a)
520
552
  });
@@ -532,13 +564,13 @@ var Connection = class {
532
564
  peerId: this.localInfo
533
565
  }, {
534
566
  F: __dxlog_file,
535
- L: 434,
567
+ L: 439,
536
568
  S: this,
537
569
  C: (f, a) => f(...a)
538
570
  });
539
571
  invariant(state !== this._state, "Already in this state.", {
540
572
  F: __dxlog_file,
541
- L: 435,
573
+ L: 440,
542
574
  S: this,
543
575
  A: [
544
576
  "state !== this._state",
@@ -1290,7 +1322,9 @@ var Peer = class {
1290
1322
  S: this,
1291
1323
  C: (f, a) => f(...a)
1292
1324
  });
1293
- await connection.close(err);
1325
+ await connection.close({
1326
+ error: err
1327
+ });
1294
1328
  log4("closed", {
1295
1329
  peerId: this.remoteInfo,
1296
1330
  sessionId: connection.sessionId
@@ -1326,7 +1360,9 @@ var Peer = class {
1326
1360
  S: this,
1327
1361
  C: (f, a) => f(...a)
1328
1362
  });
1329
- await this?.connection?.close(reason);
1363
+ await this?.connection?.close({
1364
+ reason
1365
+ });
1330
1366
  }
1331
1367
  };
1332
1368
  _ts_decorate2([
@@ -1622,24 +1658,34 @@ var Swarm = class {
1622
1658
  accept: false
1623
1659
  };
1624
1660
  }
1625
- const peer = this._getOrCreatePeer(message.author);
1661
+ const peer = this._getOfferSenderPeer(message.author);
1626
1662
  const answer = await peer.onOffer(message);
1627
1663
  this._topology.update();
1628
1664
  return answer;
1629
1665
  }
1666
+ _getOfferSenderPeer(senderInfo) {
1667
+ const peer = this._getOrCreatePeer(senderInfo);
1668
+ const connectionState = peer.connection?.state;
1669
+ if (connectionState === ConnectionState.CLOSING || connectionState === ConnectionState.ABORTING) {
1670
+ this._peers.delete(peer.remoteInfo);
1671
+ this.disconnected.emit(peer.remoteInfo);
1672
+ return this._getOrCreatePeer(peer.remoteInfo);
1673
+ }
1674
+ return peer;
1675
+ }
1630
1676
  async onSignal(message) {
1631
1677
  log5("signal", {
1632
1678
  message
1633
1679
  }, {
1634
1680
  F: __dxlog_file5,
1635
- L: 232,
1681
+ L: 246,
1636
1682
  S: this,
1637
1683
  C: (f, a) => f(...a)
1638
1684
  });
1639
1685
  if (this._ctx.disposed) {
1640
1686
  log5.info("ignored for offline swarm", void 0, {
1641
1687
  F: __dxlog_file5,
1642
- L: 234,
1688
+ L: 248,
1643
1689
  S: this,
1644
1690
  C: (f, a) => f(...a)
1645
1691
  });
@@ -1647,7 +1693,7 @@ var Swarm = class {
1647
1693
  }
1648
1694
  invariant4(message.recipient.peerKey === this._ownPeer.peerKey, `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1649
1695
  F: __dxlog_file5,
1650
- L: 237,
1696
+ L: 251,
1651
1697
  S: this,
1652
1698
  A: [
1653
1699
  "message.recipient.peerKey === this._ownPeer.peerKey",
@@ -1656,7 +1702,7 @@ var Swarm = class {
1656
1702
  });
1657
1703
  invariant4(message.topic?.equals(this._topic), void 0, {
1658
1704
  F: __dxlog_file5,
1659
- L: 241,
1705
+ L: 255,
1660
1706
  S: this,
1661
1707
  A: [
1662
1708
  "message.topic?.equals(this._topic)",
@@ -1665,7 +1711,7 @@ var Swarm = class {
1665
1711
  });
1666
1712
  invariant4(message.author, void 0, {
1667
1713
  F: __dxlog_file5,
1668
- L: 242,
1714
+ L: 256,
1669
1715
  S: this,
1670
1716
  A: [
1671
1717
  "message.author",
@@ -1686,13 +1732,13 @@ var Swarm = class {
1686
1732
  async goOnline() {
1687
1733
  this._ctx = new Context4(void 0, {
1688
1734
  F: __dxlog_file5,
1689
- L: 258
1735
+ L: 272
1690
1736
  });
1691
1737
  }
1692
1738
  _getOrCreatePeer(peerInfo) {
1693
1739
  invariant4(peerInfo.peerKey, "PeerInfo.peerKey is required", {
1694
1740
  F: __dxlog_file5,
1695
- L: 262,
1741
+ L: 276,
1696
1742
  S: this,
1697
1743
  A: [
1698
1744
  "peerInfo.peerKey",
@@ -1712,7 +1758,7 @@ var Swarm = class {
1712
1758
  if (this._isUnregistered(peer)) {
1713
1759
  log5.verbose("ignored onDisconnected for unregistered peer", void 0, {
1714
1760
  F: __dxlog_file5,
1715
- L: 282,
1761
+ L: 296,
1716
1762
  S: this,
1717
1763
  C: (f, a) => f(...a)
1718
1764
  });
@@ -1730,7 +1776,7 @@ var Swarm = class {
1730
1776
  peerInfo
1731
1777
  }, {
1732
1778
  F: __dxlog_file5,
1733
- L: 296,
1779
+ L: 310,
1734
1780
  S: this,
1735
1781
  C: (f, a) => f(...a)
1736
1782
  });
@@ -1757,14 +1803,14 @@ var Swarm = class {
1757
1803
  reason
1758
1804
  }, {
1759
1805
  F: __dxlog_file5,
1760
- L: 318,
1806
+ L: 332,
1761
1807
  S: this,
1762
1808
  C: (f, a) => f(...a)
1763
1809
  });
1764
1810
  const peer = this._peers.get(peerInfo);
1765
1811
  invariant4(peer, void 0, {
1766
1812
  F: __dxlog_file5,
1767
- L: 320,
1813
+ L: 334,
1768
1814
  S: this,
1769
1815
  A: [
1770
1816
  "peer",
@@ -1772,7 +1818,7 @@ var Swarm = class {
1772
1818
  ]
1773
1819
  });
1774
1820
  this._peers.delete(peerInfo);
1775
- await peer.safeDestroy(new Error(reason));
1821
+ await peer.safeDestroy(reason);
1776
1822
  }
1777
1823
  _getSwarmController() {
1778
1824
  return {
@@ -1794,7 +1840,7 @@ var Swarm = class {
1794
1840
  } catch (err) {
1795
1841
  log5("initiation error", err, {
1796
1842
  F: __dxlog_file5,
1797
- L: 347,
1843
+ L: 361,
1798
1844
  S: this,
1799
1845
  C: (f, a) => f(...a)
1800
1846
  });
@@ -1825,7 +1871,7 @@ var Swarm = class {
1825
1871
  remotePeer
1826
1872
  }, {
1827
1873
  F: __dxlog_file5,
1828
- L: 375,
1874
+ L: 389,
1829
1875
  S: this,
1830
1876
  C: (f, a) => f(...a)
1831
1877
  });
@@ -1844,7 +1890,7 @@ var Swarm = class {
1844
1890
  remotePeer
1845
1891
  }, {
1846
1892
  F: __dxlog_file5,
1847
- L: 391,
1893
+ L: 405,
1848
1894
  S: this,
1849
1895
  C: (f, a) => f(...a)
1850
1896
  });
@@ -1854,7 +1900,7 @@ var Swarm = class {
1854
1900
  remotePeer
1855
1901
  }, {
1856
1902
  F: __dxlog_file5,
1857
- L: 394,
1903
+ L: 408,
1858
1904
  S: this,
1859
1905
  C: (f, a) => f(...a)
1860
1906
  });
@@ -1903,7 +1949,7 @@ _ts_decorate3([
1903
1949
  ], Swarm.prototype, "goOnline", null);
1904
1950
 
1905
1951
  // packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts
1906
- import { Event as Event4, EventSubscriptions } from "@dxos/async";
1952
+ import { Event as Event4, SubscriptionList } from "@dxos/async";
1907
1953
  import { PublicKey as PublicKey5 } from "@dxos/keys";
1908
1954
  import { log as log6 } from "@dxos/log";
1909
1955
  import { PeerInfoHash as PeerInfoHash2 } from "@dxos/messaging";
@@ -1915,7 +1961,7 @@ var SwarmMapper = class {
1915
1961
  }
1916
1962
  constructor(_swarm) {
1917
1963
  this._swarm = _swarm;
1918
- this._subscriptions = new EventSubscriptions();
1964
+ this._subscriptions = new SubscriptionList();
1919
1965
  this._connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
1920
1966
  this._peers = new ComplexMap3(PeerInfoHash2);
1921
1967
  this.mapUpdated = new Event4();
@@ -1935,7 +1981,7 @@ var SwarmMapper = class {
1935
1981
  _update() {
1936
1982
  log6("updating swarm", void 0, {
1937
1983
  F: __dxlog_file6,
1938
- L: 73,
1984
+ L: 71,
1939
1985
  S: this,
1940
1986
  C: (f, a) => f(...a)
1941
1987
  });
@@ -1959,7 +2005,7 @@ var SwarmMapper = class {
1959
2005
  totalPeersInSwarm: this._peers.size
1960
2006
  }, {
1961
2007
  F: __dxlog_file6,
1962
- L: 114,
2008
+ L: 112,
1963
2009
  S: this,
1964
2010
  C: (f, a) => f(...a)
1965
2011
  });
@@ -1968,6 +2014,7 @@ var SwarmMapper = class {
1968
2014
  // TODO(burdon): Async open/close.
1969
2015
  destroy() {
1970
2016
  Array.from(this._connectionSubscriptions.values()).forEach((cb) => cb());
2017
+ this._connectionSubscriptions.clear();
1971
2018
  this._subscriptions.clear();
1972
2019
  }
1973
2020
  };
@@ -4431,4 +4478,4 @@ export {
4431
4478
  RtcTransportService,
4432
4479
  createTeleportProtocolFactory
4433
4480
  };
4434
- //# sourceMappingURL=chunk-WXFQML2V.mjs.map
4481
+ //# sourceMappingURL=chunk-BHB7ZFZF.mjs.map