hypercore 11.21.7 → 11.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.
Files changed (2) hide show
  1. package/lib/replicator.js +43 -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
@@ -1797,6 +1797,10 @@ module.exports = class Replicator {
1797
1797
  }
1798
1798
  }
1799
1799
 
1800
+ setInflightRange(min, max) {
1801
+ this.inflightRange = [min, max]
1802
+ }
1803
+
1800
1804
  updateActivity(inc, session) {
1801
1805
  this.activeSessions += inc
1802
1806
  this.setDownloading(this.activeSessions !== 0, session)
@@ -1820,29 +1824,45 @@ module.exports = class Replicator {
1820
1824
  for (const peer of this.peers) peer.sendSync()
1821
1825
  }
1822
1826
 
1827
+ isAllowingPush() {
1828
+ if (!this.allowPush) return false
1829
+ if (this._notDownloadingTimer) return false
1830
+ return this.downloading
1831
+ }
1832
+
1823
1833
  setDownloading(downloading) {
1824
- clearTimeout(this._downloadingTimer)
1834
+ const allowPush = this.isAllowingPush()
1835
+ const linger = this._notDownloadingLinger
1836
+
1837
+ if (this._notDownloadingTimer) {
1838
+ clearTimeout(this._notDownloadingTimer)
1839
+ this._notDownloadingTimer = null
1840
+ }
1825
1841
 
1826
1842
  if (this.destroyed) return
1827
- if (downloading || this._notDownloadingLinger === 0) {
1828
- this.setDownloadingNow(downloading)
1843
+
1844
+ if (downloading || linger === 0) {
1845
+ if (!this._setDownloadingNow(downloading)) this.signalAllowPush(allowPush)
1829
1846
  return
1830
1847
  }
1831
1848
 
1832
- this._downloadingTimer = setTimeout(
1833
- setDownloadingLater,
1834
- this._notDownloadingLinger,
1835
- this,
1836
- downloading
1837
- )
1838
- if (this._downloadingTimer.unref) this._downloadingTimer.unref()
1849
+ this._notDownloadingTimer = setTimeout(setNotDownloadingLater, linger, this)
1850
+ if (this._notDownloadingTimer.unref) this._notDownloadingTimer.unref()
1851
+ this.signalAllowPush(allowPush)
1839
1852
  }
1840
1853
 
1841
- setDownloadingNow(downloading) {
1842
- this._downloadingTimer = null
1843
- if (this.downloading === downloading) return
1854
+ signalAllowPush(allowPush) {
1855
+ if (allowPush === this.isAllowingPush()) return
1856
+ for (const peer of this.peers) peer.signalUpgrade()
1857
+ }
1858
+
1859
+ _setDownloadingNow(downloading) {
1860
+ this._notDownloadingTimer = null
1861
+
1862
+ if (this.downloading === downloading) return false
1844
1863
  this.downloading = downloading
1845
- if (!downloading && this.isDownloading()) return
1864
+
1865
+ if (!downloading && this.isDownloading()) return false
1846
1866
 
1847
1867
  for (const peer of this.peers) peer.signalUpgrade()
1848
1868
 
@@ -1858,6 +1878,7 @@ module.exports = class Replicator {
1858
1878
  }
1859
1879
 
1860
1880
  if (this.ondownloading !== null && downloading) this.ondownloading()
1881
+ return true
1861
1882
  }
1862
1883
 
1863
1884
  cork() {
@@ -1890,7 +1911,7 @@ module.exports = class Replicator {
1890
1911
  }
1891
1912
 
1892
1913
  for (const peer of this.peers) {
1893
- peer.fullyDownloadedSignaled = 0
1914
+ peer.fullyDownloadedSignaled = Math.min(newLength, peer.fullyDownloadedSignaled)
1894
1915
  peer._unclearLocalRange(newLength, truncated)
1895
1916
  }
1896
1917
  }
@@ -2490,11 +2511,6 @@ module.exports = class Replicator {
2490
2511
  for (const peer of this.peers) peer._resetMissingBlock(index)
2491
2512
  }
2492
2513
 
2493
- fullyDownloaded() {
2494
- if (!this.core.state.length) return false
2495
- return this.core.state.length === this.core.header.hints.contiguousLength
2496
- }
2497
-
2498
2514
  _ondata(peer, req, data) {
2499
2515
  if (req) {
2500
2516
  req.elapsed = Date.now() - req.timestamp
@@ -2830,9 +2846,9 @@ module.exports = class Replicator {
2830
2846
  if (this.destroyed) return
2831
2847
  this.destroyed = true
2832
2848
 
2833
- if (this._downloadingTimer) {
2834
- clearTimeout(this._downloadingTimer)
2835
- this._downloadingTimer = null
2849
+ if (this._notDownloadingTimer) {
2850
+ clearTimeout(this._notDownloadingTimer)
2851
+ this._notDownloadingTimer = null
2836
2852
  }
2837
2853
 
2838
2854
  while (this.peers.length) {
@@ -3065,8 +3081,8 @@ function onwireextension(m, c) {
3065
3081
  return c.userData.onextension(m)
3066
3082
  }
3067
3083
 
3068
- function setDownloadingLater(repl, downloading, session) {
3069
- repl.setDownloadingNow(downloading, session)
3084
+ function setNotDownloadingLater(repl, session) {
3085
+ repl._setDownloadingNow(false, session)
3070
3086
  }
3071
3087
 
3072
3088
  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.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {