hypercore 11.21.7 → 11.22.0

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 +39 -27
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -605,7 +605,7 @@ class Peer {
605
605
  uploading: true,
606
606
  downloading: this.replicator.isDownloading(),
607
607
  hasManifest: !!this.core.header.manifest && this.core.compat === false,
608
- allowPush: this.replicator.allowPush
608
+ allowPush: this.replicator.isAllowingPush()
609
609
  })
610
610
  incrementTx(this.stats.wireSync, this.replicator.stats.wireSync)
611
611
  }
@@ -1787,7 +1787,7 @@ module.exports = class Replicator {
1787
1787
  this._applyingReorg = null
1788
1788
  this._manifestPeer = null
1789
1789
  this._notDownloadingLinger = notDownloadingLinger
1790
- this._downloadingTimer = null
1790
+ this._notDownloadingTimer = null
1791
1791
 
1792
1792
  const self = this
1793
1793
  this._onstreamclose = onstreamclose
@@ -1820,29 +1820,45 @@ module.exports = class Replicator {
1820
1820
  for (const peer of this.peers) peer.sendSync()
1821
1821
  }
1822
1822
 
1823
+ isAllowingPush() {
1824
+ if (!this.allowPush) return false
1825
+ if (this._notDownloadingTimer) return false
1826
+ return this.downloading
1827
+ }
1828
+
1823
1829
  setDownloading(downloading) {
1824
- clearTimeout(this._downloadingTimer)
1830
+ const allowPush = this.isAllowingPush()
1831
+ const linger = this._notDownloadingLinger
1832
+
1833
+ if (this._notDownloadingTimer) {
1834
+ clearTimeout(this._notDownloadingTimer)
1835
+ this._notDownloadingTimer = null
1836
+ }
1825
1837
 
1826
1838
  if (this.destroyed) return
1827
- if (downloading || this._notDownloadingLinger === 0) {
1828
- this.setDownloadingNow(downloading)
1839
+
1840
+ if (downloading || linger === 0) {
1841
+ if (!this._setDownloadingNow(downloading)) this.signalAllowPush(allowPush)
1829
1842
  return
1830
1843
  }
1831
1844
 
1832
- this._downloadingTimer = setTimeout(
1833
- setDownloadingLater,
1834
- this._notDownloadingLinger,
1835
- this,
1836
- downloading
1837
- )
1838
- if (this._downloadingTimer.unref) this._downloadingTimer.unref()
1845
+ this._notDownloadingTimer = setTimeout(setNotDownloadingLater, linger, this)
1846
+ if (this._notDownloadingTimer.unref) this._notDownloadingTimer.unref()
1847
+ this.signalAllowPush(allowPush)
1839
1848
  }
1840
1849
 
1841
- setDownloadingNow(downloading) {
1842
- this._downloadingTimer = null
1843
- if (this.downloading === downloading) return
1850
+ signalAllowPush(allowPush) {
1851
+ if (allowPush === this.isAllowingPush()) return
1852
+ for (const peer of this.peers) peer.signalUpgrade()
1853
+ }
1854
+
1855
+ _setDownloadingNow(downloading) {
1856
+ this._notDownloadingTimer = null
1857
+
1858
+ if (this.downloading === downloading) return false
1844
1859
  this.downloading = downloading
1845
- if (!downloading && this.isDownloading()) return
1860
+
1861
+ if (!downloading && this.isDownloading()) return false
1846
1862
 
1847
1863
  for (const peer of this.peers) peer.signalUpgrade()
1848
1864
 
@@ -1858,6 +1874,7 @@ module.exports = class Replicator {
1858
1874
  }
1859
1875
 
1860
1876
  if (this.ondownloading !== null && downloading) this.ondownloading()
1877
+ return true
1861
1878
  }
1862
1879
 
1863
1880
  cork() {
@@ -1890,7 +1907,7 @@ module.exports = class Replicator {
1890
1907
  }
1891
1908
 
1892
1909
  for (const peer of this.peers) {
1893
- peer.fullyDownloadedSignaled = 0
1910
+ peer.fullyDownloadedSignaled = Math.min(newLength, peer.fullyDownloadedSignaled)
1894
1911
  peer._unclearLocalRange(newLength, truncated)
1895
1912
  }
1896
1913
  }
@@ -2490,11 +2507,6 @@ module.exports = class Replicator {
2490
2507
  for (const peer of this.peers) peer._resetMissingBlock(index)
2491
2508
  }
2492
2509
 
2493
- fullyDownloaded() {
2494
- if (!this.core.state.length) return false
2495
- return this.core.state.length === this.core.header.hints.contiguousLength
2496
- }
2497
-
2498
2510
  _ondata(peer, req, data) {
2499
2511
  if (req) {
2500
2512
  req.elapsed = Date.now() - req.timestamp
@@ -2830,9 +2842,9 @@ module.exports = class Replicator {
2830
2842
  if (this.destroyed) return
2831
2843
  this.destroyed = true
2832
2844
 
2833
- if (this._downloadingTimer) {
2834
- clearTimeout(this._downloadingTimer)
2835
- this._downloadingTimer = null
2845
+ if (this._notDownloadingTimer) {
2846
+ clearTimeout(this._notDownloadingTimer)
2847
+ this._notDownloadingTimer = null
2836
2848
  }
2837
2849
 
2838
2850
  while (this.peers.length) {
@@ -3065,8 +3077,8 @@ function onwireextension(m, c) {
3065
3077
  return c.userData.onextension(m)
3066
3078
  }
3067
3079
 
3068
- function setDownloadingLater(repl, downloading, session) {
3069
- repl.setDownloadingNow(downloading, session)
3080
+ function setNotDownloadingLater(repl, session) {
3081
+ repl._setDownloadingNow(false, session)
3070
3082
  }
3071
3083
 
3072
3084
  function isBlockRequest(req) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.21.7",
3
+ "version": "11.22.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {