hypercore 10.22.0 → 10.22.2

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 (2) hide show
  1. package/lib/replicator.js +37 -5
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -295,6 +295,7 @@ class Peer {
295
295
  this.dataProcessing = 0
296
296
 
297
297
  this.canUpgrade = true
298
+ this.reopenMaybe = false
298
299
 
299
300
  this.needsSync = false
300
301
  this.syncsProcessing = 0
@@ -375,6 +376,7 @@ class Peer {
375
376
  }
376
377
 
377
378
  this.needsSync = false
379
+ if (this.replicator.downloading === false) this.reopenMaybe = true
378
380
 
379
381
  this.wireSync.send({
380
382
  fork: this.core.tree.fork,
@@ -382,7 +384,7 @@ class Peer {
382
384
  remoteLength: this.core.tree.fork === this.remoteFork ? this.remoteLength : 0,
383
385
  canUpgrade: this.canUpgrade,
384
386
  uploading: true,
385
- downloading: true
387
+ downloading: this.replicator.downloading
386
388
  })
387
389
  }
388
390
 
@@ -417,7 +419,12 @@ class Peer {
417
419
  }
418
420
 
419
421
  onclose (isRemote) {
420
- if (this.session) this.session.close().catch(noop)
422
+ // we might have signalled to the remote that we are done (ie not downloading) and the remote might agree on that
423
+ // if that happens, the channel might be closed by the remote. if so just renegotiate it.
424
+ const reopen = isRemote === true && this.remoteOpened === true && this.remoteDownloading === false &&
425
+ this.remoteUploading === true && this.replicator.downloading === true && this.reopenMaybe === true
426
+
427
+ if (this.session && !reopen) this.replicator._closeSession(this.session)
421
428
 
422
429
  if (this.remoteOpened === false) {
423
430
  this.replicator._ifAvailable--
@@ -427,6 +434,10 @@ class Peer {
427
434
 
428
435
  this.remoteOpened = false
429
436
  this.replicator._removePeer(this)
437
+
438
+ if (reopen) {
439
+ this.replicator._makePeer(this.protomux, this.session)
440
+ }
430
441
  }
431
442
 
432
443
  async onsync ({ fork, length, remoteLength, canUpgrade, uploading, downloading }) {
@@ -440,6 +451,12 @@ class Peer {
440
451
  this.remoteUploading = uploading
441
452
  this.remoteDownloading = downloading
442
453
 
454
+ if (this.remoteDownloading === false && this.replicator.downloading === false) {
455
+ // idling, shut it down...
456
+ this.channel.close()
457
+ return
458
+ }
459
+
443
460
  this.lengthAcked = sameFork ? remoteLength : 0
444
461
  this.syncsProcessing++
445
462
 
@@ -1016,6 +1033,8 @@ module.exports = class Replicator {
1016
1033
  this.peers = []
1017
1034
  this.findingPeers = 0 // updateable from the outside
1018
1035
  this.destroyed = false
1036
+ this.downloading = true
1037
+ this.sessions = 0
1019
1038
 
1020
1039
  this._attached = new Set()
1021
1040
  this._inflight = new InflightTracker()
@@ -1042,6 +1061,12 @@ module.exports = class Replicator {
1042
1061
  }
1043
1062
  }
1044
1063
 
1064
+ setDownloading (downloading) {
1065
+ if (this.downloading === downloading) return
1066
+ this.downloading = downloading
1067
+ for (const peer of this.peers) peer.sendSync()
1068
+ }
1069
+
1045
1070
  cork () {
1046
1071
  for (const peer of this.peers) peer.protomux.cork()
1047
1072
  }
@@ -1712,7 +1737,13 @@ module.exports = class Replicator {
1712
1737
  this._maybeResolveIfAvailableRanges()
1713
1738
  }
1714
1739
 
1740
+ _closeSession (session) {
1741
+ if (!session.closing) this.sessions--
1742
+ session.close().catch(noop)
1743
+ }
1744
+
1715
1745
  attachTo (protomux, session) {
1746
+ if (session) this.sessions++
1716
1747
  const makePeer = this._makePeer.bind(this, protomux, session)
1717
1748
 
1718
1749
  this._attached.add(protomux)
@@ -1725,7 +1756,7 @@ module.exports = class Replicator {
1725
1756
  this._ifAvailable--
1726
1757
 
1727
1758
  if (opened && !this.destroyed) makePeer()
1728
- else if (session) session.close().catch(noop)
1759
+ else if (session) this._closeSession(session)
1729
1760
  this._checkUpgradeIfAvailable()
1730
1761
  })
1731
1762
  }
@@ -1749,6 +1780,7 @@ module.exports = class Replicator {
1749
1780
  }
1750
1781
 
1751
1782
  _makePeer (protomux, session) {
1783
+ const replicator = this
1752
1784
  if (protomux.opened({ protocol: 'hypercore/alpha', id: this.discoveryKey })) return onnochannel()
1753
1785
 
1754
1786
  const channel = protomux.createChannel({
@@ -1776,7 +1808,7 @@ module.exports = class Replicator {
1776
1808
 
1777
1809
  if (channel === null) return onnochannel()
1778
1810
 
1779
- const peer = new Peer(this, protomux, channel, session)
1811
+ const peer = new Peer(replicator, protomux, channel, session)
1780
1812
  const stream = protomux.stream
1781
1813
 
1782
1814
  peer.channel.open({
@@ -1787,7 +1819,7 @@ module.exports = class Replicator {
1787
1819
  return true
1788
1820
 
1789
1821
  function onnochannel () {
1790
- if (session) session.close().catch(noop)
1822
+ if (session) replicator._closeSession(session)
1791
1823
  return false
1792
1824
  }
1793
1825
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.22.0",
3
+ "version": "10.22.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {