hypercore 11.21.6 → 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.
package/README.md CHANGED
@@ -66,7 +66,7 @@ Alternatively you can pass a [Hypercore Storage](https://github.com/holepunchto/
66
66
  }
67
67
  ```
68
68
 
69
- You can also set `valueEncoding` to any [compact-encoding](https://github.com/compact-encoding) instance.
69
+ You can also set `valueEncoding` to any [compact-encoding](https://github.com/holepunchto/compact-encoding) instance.
70
70
 
71
71
  `valueEncoding`s will be applied to individual blocks, even if you append batches. If you want to control encoding at the batch-level, you can use the `encodeBatch` option, which is a function that takes a batch and returns a binary-encoded batch. If you provide a custom `valueEncoding`, it will not be applied prior to `encodeBatch`.
72
72
 
@@ -169,7 +169,7 @@ Check if the core has all blocks between `start` and `end`.
169
169
 
170
170
  #### `const updated = await core.update([options])`
171
171
 
172
- Waits for initial proof of the new core length until all `findingPeers` calls has finished.
172
+ Waits for initial proof of the new core length until all `findingPeers` calls have finished.
173
173
 
174
174
  ```js
175
175
  const updated = await core.update()
@@ -414,7 +414,7 @@ Register a custom protocol extension. This is a legacy implementation and is no
414
414
  ```
415
415
  {
416
416
  encoding: 'json' | 'utf-8' | 'binary', // Compact encoding to use for messages. Defaults to buffer
417
- onmessage: (message, peer) => { ... } // Callback for when a message for the extension is receive
417
+ onmessage: (message, peer) => { ... } // Callback for when a message for the extension is received
418
418
  }
419
419
  ```
420
420
 
@@ -438,7 +438,7 @@ You must close any session you make.
438
438
 
439
439
  Options are inherited from the parent instance, unless they are re-set.
440
440
 
441
- `options` are the same as in the constructor with the follow additions:
441
+ `options` are the same as in the constructor with the following additions:
442
442
 
443
443
  ```
444
444
  {
@@ -588,7 +588,7 @@ Populated after `ready` has been emitted. Will be `0` before the event.
588
588
 
589
589
  #### `core.signedLength`
590
590
 
591
- How many blocks of data are available on this core that have been signed by a quorum. This is equal to `core.length` for Hypercores's with a single signer.
591
+ How many blocks of data are available on this core that have been signed by a quorum. This is equal to `core.length` for Hypercores with a single signer.
592
592
 
593
593
  Populated after `ready` has been emitted. Will be `0` before the event.
594
594
 
@@ -730,9 +730,9 @@ Returns the key for a given manifest.
730
730
 
731
731
  ```
732
732
  {
733
- compat: false, // Whether the manifest has a single singer whose public key is the key
734
- version, // Manifest version if the manifest argument is the public key of a single singer
735
- namespace // The signer namespace if the manifest argument is the public key of a single singer
733
+ compat: false, // Whether the manifest has a single signer whose public key is the key
734
+ version, // Manifest version if the manifest argument is the public key of a single signer
735
+ namespace // The signer namespace if the manifest argument is the public key of a single signer
736
736
  }
737
737
  ```
738
738
 
package/lib/replicator.js CHANGED
@@ -401,7 +401,7 @@ class Peer {
401
401
  this.remoteSupportsSeeks = false
402
402
  this.inflightRange = inflightRange
403
403
  this.remoteSegmentsWanted = new Set()
404
- this.fullyDownloadedSignaled = false
404
+ this.fullyDownloadedSignaled = 0
405
405
 
406
406
  this.paused = false
407
407
  this.removed = false
@@ -511,9 +511,6 @@ class Peer {
511
511
  }
512
512
 
513
513
  signalUpgrade() {
514
- if (this.fullyDownloadedSignaled && !this.replicator.fullyDownloaded()) {
515
- this.fullyDownloadedSignaled = false
516
- }
517
514
  if (this._shouldUpdateCanUpgrade() === true) this._updateCanUpgradeAndSync()
518
515
  else this.sendSync()
519
516
  }
@@ -546,8 +543,8 @@ class Peer {
546
543
  length = this.core.state.length
547
544
 
548
545
  // Always send the broadcast when we switch from sparse to fully contiguous, so remote knows too
549
- if (!this.fullyDownloadedSignaled) {
550
- this.fullyDownloadedSignaled = true
546
+ if (this.fullyDownloadedSignaled < length) {
547
+ this.fullyDownloadedSignaled = length
551
548
  force = true
552
549
  }
553
550
  }
@@ -608,7 +605,7 @@ class Peer {
608
605
  uploading: true,
609
606
  downloading: this.replicator.isDownloading(),
610
607
  hasManifest: !!this.core.header.manifest && this.core.compat === false,
611
- allowPush: this.replicator.allowPush
608
+ allowPush: this.replicator.isAllowingPush()
612
609
  })
613
610
  incrementTx(this.stats.wireSync, this.replicator.stats.wireSync)
614
611
  }
@@ -1790,7 +1787,7 @@ module.exports = class Replicator {
1790
1787
  this._applyingReorg = null
1791
1788
  this._manifestPeer = null
1792
1789
  this._notDownloadingLinger = notDownloadingLinger
1793
- this._downloadingTimer = null
1790
+ this._notDownloadingTimer = null
1794
1791
 
1795
1792
  const self = this
1796
1793
  this._onstreamclose = onstreamclose
@@ -1823,29 +1820,45 @@ module.exports = class Replicator {
1823
1820
  for (const peer of this.peers) peer.sendSync()
1824
1821
  }
1825
1822
 
1823
+ isAllowingPush() {
1824
+ if (!this.allowPush) return false
1825
+ if (this._notDownloadingTimer) return false
1826
+ return this.downloading
1827
+ }
1828
+
1826
1829
  setDownloading(downloading) {
1827
- 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
+ }
1828
1837
 
1829
1838
  if (this.destroyed) return
1830
- if (downloading || this._notDownloadingLinger === 0) {
1831
- this.setDownloadingNow(downloading)
1839
+
1840
+ if (downloading || linger === 0) {
1841
+ if (!this._setDownloadingNow(downloading)) this.signalAllowPush(allowPush)
1832
1842
  return
1833
1843
  }
1834
1844
 
1835
- this._downloadingTimer = setTimeout(
1836
- setDownloadingLater,
1837
- this._notDownloadingLinger,
1838
- this,
1839
- downloading
1840
- )
1841
- 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)
1848
+ }
1849
+
1850
+ signalAllowPush(allowPush) {
1851
+ if (allowPush === this.isAllowingPush()) return
1852
+ for (const peer of this.peers) peer.signalUpgrade()
1842
1853
  }
1843
1854
 
1844
- setDownloadingNow(downloading) {
1845
- this._downloadingTimer = null
1846
- if (this.downloading === downloading) return
1855
+ _setDownloadingNow(downloading) {
1856
+ this._notDownloadingTimer = null
1857
+
1858
+ if (this.downloading === downloading) return false
1847
1859
  this.downloading = downloading
1848
- if (!downloading && this.isDownloading()) return
1860
+
1861
+ if (!downloading && this.isDownloading()) return false
1849
1862
 
1850
1863
  for (const peer of this.peers) peer.signalUpgrade()
1851
1864
 
@@ -1861,6 +1874,7 @@ module.exports = class Replicator {
1861
1874
  }
1862
1875
 
1863
1876
  if (this.ondownloading !== null && downloading) this.ondownloading()
1877
+ return true
1864
1878
  }
1865
1879
 
1866
1880
  cork() {
@@ -1892,7 +1906,10 @@ module.exports = class Replicator {
1892
1906
  }
1893
1907
  }
1894
1908
 
1895
- for (const peer of this.peers) peer._unclearLocalRange(newLength, truncated)
1909
+ for (const peer of this.peers) {
1910
+ peer.fullyDownloadedSignaled = Math.min(newLength, peer.fullyDownloadedSignaled)
1911
+ peer._unclearLocalRange(newLength, truncated)
1912
+ }
1896
1913
  }
1897
1914
 
1898
1915
  // Called externally when a upgrade has been processed
@@ -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.6",
3
+ "version": "11.22.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {