hypercore 10.21.5 → 10.22.1

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.
package/index.js CHANGED
@@ -123,6 +123,7 @@ module.exports = class Hypercore extends EventEmitter {
123
123
  else peers = '[ ' + opts.stylize(0, 'number') + ' ]'
124
124
 
125
125
  return this.constructor.name + '(\n' +
126
+ indent + ' id: ' + opts.stylize(this.id, 'string') + '\n' +
126
127
  indent + ' key: ' + opts.stylize(toHex(this.key), 'string') + '\n' +
127
128
  indent + ' discoveryKey: ' + opts.stylize(toHex(this.discoveryKey), 'string') + '\n' +
128
129
  indent + ' opened: ' + opts.stylize(this.opened, 'boolean') + '\n' +
package/lib/replicator.js CHANGED
@@ -382,7 +382,7 @@ class Peer {
382
382
  remoteLength: this.core.tree.fork === this.remoteFork ? this.remoteLength : 0,
383
383
  canUpgrade: this.canUpgrade,
384
384
  uploading: true,
385
- downloading: true
385
+ downloading: this.replicator.downloading
386
386
  })
387
387
  }
388
388
 
@@ -417,7 +417,7 @@ class Peer {
417
417
  }
418
418
 
419
419
  onclose (isRemote) {
420
- if (this.session) this.session.close().catch(noop)
420
+ if (this.session) this.replicator._closeSession(this.session)
421
421
 
422
422
  if (this.remoteOpened === false) {
423
423
  this.replicator._ifAvailable--
@@ -440,6 +440,12 @@ class Peer {
440
440
  this.remoteUploading = uploading
441
441
  this.remoteDownloading = downloading
442
442
 
443
+ if (this.remoteDownloading === false && this.replicator.downloading === false) {
444
+ // idling, shut it down...
445
+ this.channel.close()
446
+ return
447
+ }
448
+
443
449
  this.lengthAcked = sameFork ? remoteLength : 0
444
450
  this.syncsProcessing++
445
451
 
@@ -1016,6 +1022,8 @@ module.exports = class Replicator {
1016
1022
  this.peers = []
1017
1023
  this.findingPeers = 0 // updateable from the outside
1018
1024
  this.destroyed = false
1025
+ this.downloading = true
1026
+ this.sessions = 0
1019
1027
 
1020
1028
  this._attached = new Set()
1021
1029
  this._inflight = new InflightTracker()
@@ -1042,6 +1050,12 @@ module.exports = class Replicator {
1042
1050
  }
1043
1051
  }
1044
1052
 
1053
+ setDownloading (downloading) {
1054
+ if (this.downloading === downloading) return
1055
+ this.downloading = downloading
1056
+ for (const peer of this.peers) peer.sendSync()
1057
+ }
1058
+
1045
1059
  cork () {
1046
1060
  for (const peer of this.peers) peer.protomux.cork()
1047
1061
  }
@@ -1712,7 +1726,13 @@ module.exports = class Replicator {
1712
1726
  this._maybeResolveIfAvailableRanges()
1713
1727
  }
1714
1728
 
1729
+ _closeSession (session) {
1730
+ if (!session.closing) this.sessions--
1731
+ session.close().catch(noop)
1732
+ }
1733
+
1715
1734
  attachTo (protomux, session) {
1735
+ if (session) this.sessions++
1716
1736
  const makePeer = this._makePeer.bind(this, protomux, session)
1717
1737
 
1718
1738
  this._attached.add(protomux)
@@ -1725,7 +1745,7 @@ module.exports = class Replicator {
1725
1745
  this._ifAvailable--
1726
1746
 
1727
1747
  if (opened && !this.destroyed) makePeer()
1728
- else if (session) session.close().catch(noop)
1748
+ else if (session) this._closeSession(session)
1729
1749
  this._checkUpgradeIfAvailable()
1730
1750
  })
1731
1751
  }
@@ -1749,6 +1769,7 @@ module.exports = class Replicator {
1749
1769
  }
1750
1770
 
1751
1771
  _makePeer (protomux, session) {
1772
+ const replicator = this
1752
1773
  if (protomux.opened({ protocol: 'hypercore/alpha', id: this.discoveryKey })) return onnochannel()
1753
1774
 
1754
1775
  const channel = protomux.createChannel({
@@ -1776,7 +1797,7 @@ module.exports = class Replicator {
1776
1797
 
1777
1798
  if (channel === null) return onnochannel()
1778
1799
 
1779
- const peer = new Peer(this, protomux, channel, session)
1800
+ const peer = new Peer(replicator, protomux, channel, session)
1780
1801
  const stream = protomux.stream
1781
1802
 
1782
1803
  peer.channel.open({
@@ -1787,7 +1808,7 @@ module.exports = class Replicator {
1787
1808
  return true
1788
1809
 
1789
1810
  function onnochannel () {
1790
- if (session) session.close().catch(noop)
1811
+ if (session) replicator._closeSession(session)
1791
1812
  return false
1792
1813
  }
1793
1814
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.21.5",
3
+ "version": "10.22.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {