hypercore 11.39.0 → 11.39.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.
package/index.js CHANGED
@@ -709,9 +709,9 @@ class Hypercore extends EventEmitter {
709
709
  return this.opening
710
710
  }
711
711
 
712
- async recover() {
712
+ async recover(opts) {
713
713
  if (this.opened === false) await this.opening
714
- return this.state.waitForRecovery()
714
+ return this.state.waitForRecovery(this, false, opts)
715
715
  }
716
716
 
717
717
  async setUserData(key, value) {
@@ -6,13 +6,19 @@ const quickbit = require('quickbit-universal')
6
6
  const HypercoreStorage = require('hypercore-storage')
7
7
 
8
8
  const {
9
+ BAD_ARGUMENT,
9
10
  INVALID_OPERATION,
10
11
  INVALID_SIGNATURE,
11
12
  ASSERTION,
13
+ REQUEST_TIMEOUT,
12
14
  SESSION_CLOSED,
13
15
  SESSION_NOT_WRITABLE
14
16
  } = require('hypercore-errors')
15
17
 
18
+ const RECOVERY_RETRY = 5000 // between looks for a recovery peer
19
+ const RECOVERY_POLL = 250 // between checks of the recovery peers
20
+ const RECOVERY_SETTLE = 8000 // for which the recovery peers must have nothing more for us
21
+
16
22
  const Mutex = require('./mutex')
17
23
  const Bitfield = require('./bitfield')
18
24
  const { MerkleTree, MerkleTreeBatch } = require('./merkle-tree')
@@ -611,27 +617,41 @@ class SessionState {
611
617
  }
612
618
  }
613
619
 
614
- // simple tool to wait for other peers to catch us up
615
- async waitForRecovery() {
620
+ // waits until the recovery peers have nothing more for us
621
+ async waitForRecovery(session, writable, { timeout = 0, settle = RECOVERY_SETTLE } = {}) {
622
+ if (!writable && !this.isDefault()) {
623
+ throw BAD_ARGUMENT('Only the default session can recover', this.discoveryKey)
624
+ }
625
+ if (!writable && !SessionState.RECOVERY_PEERS) {
626
+ throw BAD_ARGUMENT('No recovery peers configured', this.discoveryKey)
627
+ }
628
+
616
629
  const length = this.length
617
630
  const replicator = this.core.replicator
631
+ const deadline = timeout > 0 ? Date.now() + timeout : 0
632
+ const polls = Math.max(1, Math.ceil(settle / RECOVERY_POLL))
633
+
634
+ while (session === null ? this.core.closing === null : session.closing === null) {
635
+ if (deadline !== 0 && Date.now() >= deadline) {
636
+ throw REQUEST_TIMEOUT('Recovery timed out', this.discoveryKey)
637
+ }
618
638
 
619
- while (!this.closing) {
620
639
  let peers = getRecoveryPeers(replicator.peers)
621
640
 
622
641
  if (peers.length < 1) {
623
- await new Promise((resolve) => setTimeout(resolve, 5000))
642
+ await sleepOrClose(bounded(RECOVERY_RETRY, deadline), session)
624
643
  continue
625
644
  }
626
645
 
627
646
  let restart = false
628
647
 
629
- for (let i = 0; i < 32; i++) {
630
- await new Promise((resolve) => setTimeout(resolve, 250))
648
+ for (let i = 0; i < polls; i++) {
649
+ await sleepOrClose(bounded(RECOVERY_POLL, deadline), session)
631
650
 
632
651
  peers = getRecoveryPeers(replicator.peers)
633
652
 
634
- if (this.closing || !validateRecoveryPeers(peers, this.length)) {
653
+ const closing = session === null ? this.core.closing !== null : session.closing !== null
654
+ if (closing || !validateRecoveryPeers(peers, this.length)) {
635
655
  restart = true
636
656
  break
637
657
  }
@@ -639,10 +659,12 @@ class SessionState {
639
659
 
640
660
  if (restart) continue
641
661
 
642
- if (this.length !== length) {
662
+ if (this.length !== length && writable) {
643
663
  throw SESSION_NOT_WRITABLE('core recovered during append')
644
664
  }
645
665
 
666
+ if (!this.core.header.hints.recovering) return
667
+
646
668
  this.core.header.hints.recovering = 0
647
669
  this.core.hintsChanged = true
648
670
 
@@ -661,7 +683,9 @@ class SessionState {
661
683
  async append(values, { signature, keyPair, preappend, postappend, maxLength = -1 } = {}) {
662
684
  if (!keyPair && this.isDefault()) keyPair = this.core.header.keyPair
663
685
 
664
- if (this.isDefault() && this.core.header.hints.recovering) await this.waitForRecovery()
686
+ if (this.isDefault() && this.core.header.hints.recovering) {
687
+ await this.waitForRecovery(null, true)
688
+ }
665
689
 
666
690
  await this.mutex.lock()
667
691
 
@@ -1050,7 +1074,9 @@ class SessionState {
1050
1074
  throw ASSERTION('Cannot commit while repair mode is on')
1051
1075
  }
1052
1076
 
1053
- if (this.core.header.hints.recovering) await this.waitForRecovery()
1077
+ if (this.core.header.hints.recovering) {
1078
+ await this.waitForRecovery(null, true)
1079
+ }
1054
1080
 
1055
1081
  let srcLocked = false
1056
1082
  await this.mutex.lock()
@@ -1375,6 +1401,24 @@ function isRootIndex(index, roots) {
1375
1401
  return false
1376
1402
  }
1377
1403
 
1404
+ // wakes early when the session closes, so recovery tears down at once
1405
+ function sleepOrClose(ms, session) {
1406
+ return new Promise((resolve) => {
1407
+ const timer = setTimeout(done, ms)
1408
+ if (session !== null) session.once('close', done)
1409
+
1410
+ function done() {
1411
+ clearTimeout(timer)
1412
+ if (session !== null) session.off('close', done)
1413
+ resolve()
1414
+ }
1415
+ })
1416
+ }
1417
+
1418
+ function bounded(ms, deadline) {
1419
+ return deadline === 0 ? ms : Math.max(0, Math.min(ms, deadline - Date.now()))
1420
+ }
1421
+
1378
1422
  function getRecoveryPeers(peers) {
1379
1423
  if (!SessionState.RECOVERY_PEERS) return peers
1380
1424
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.39.0",
3
+ "version": "11.39.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {