hypercore 11.27.1 → 11.27.3

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
@@ -117,6 +117,10 @@ class Hypercore extends EventEmitter {
117
117
  UPDATE_COMPAT(enableCompat)
118
118
  }
119
119
 
120
+ static setRecoveryPeers(peers) {
121
+ Core.setRecoveryPeers(peers)
122
+ }
123
+
120
124
  static key(manifest, { compat, version, namespace } = {}) {
121
125
  if (b4a.isBuffer(manifest)) {
122
126
  manifest = { version, signers: [{ publicKey: manifest, namespace }] }
@@ -627,10 +631,19 @@ class Hypercore extends EventEmitter {
627
631
  return this.opened === false ? null : this.core.globalCache
628
632
  }
629
633
 
634
+ get recovering() {
635
+ return this.opened === false ? 0 : this.core.header.hints.recovering
636
+ }
637
+
630
638
  ready() {
631
639
  return this.opening
632
640
  }
633
641
 
642
+ async recover() {
643
+ if (this.opened === false) await this.opening
644
+ return this.state.waitForRecovery()
645
+ }
646
+
634
647
  async setUserData(key, value) {
635
648
  if (this.opened === false) await this.opening
636
649
  const existing = await this.getUserData(key)
package/lib/core.js CHANGED
@@ -67,6 +67,10 @@ module.exports = class Core {
67
67
  this.opening.catch(noop)
68
68
  }
69
69
 
70
+ static setRecoveryPeers(peers) {
71
+ SessionState.RECOVERY_PEERS = peers
72
+ }
73
+
70
74
  ready() {
71
75
  return this.opening
72
76
  }
@@ -16,7 +16,7 @@ const Mutex = require('./mutex')
16
16
  const Bitfield = require('./bitfield')
17
17
  const { MerkleTree, MerkleTreeBatch } = require('./merkle-tree')
18
18
 
19
- module.exports = class SessionState {
19
+ class SessionState {
20
20
  constructor(core, parent, storage, treeInfo, name) {
21
21
  this.core = core
22
22
  this.index = this.core.sessionStates.push(this) - 1
@@ -54,6 +54,8 @@ module.exports = class SessionState {
54
54
  this.ref()
55
55
  }
56
56
 
57
+ static RECOVERY_PEERS = null
58
+
57
59
  isSnapshot() {
58
60
  return this.storage.snapshotted
59
61
  }
@@ -569,19 +571,21 @@ module.exports = class SessionState {
569
571
  }
570
572
 
571
573
  // simple tool to wait for other peers to catch us up
572
- async _waitForRecovery() {
574
+ async waitForRecovery() {
573
575
  const length = this.length
574
576
  const replicator = this.core.replicator
575
577
 
576
578
  while (!this.closing) {
577
- if (replicator.peers.length < 1) {
579
+ let peers = getRecoveryPeers(replicator.peers)
580
+
581
+ if (peers.length < 1) {
578
582
  await new Promise((resolve) => setTimeout(resolve, 5000))
579
583
  continue
580
584
  }
581
585
 
582
- await new Promise((resolve) => setTimeout(resolve, 5000))
586
+ await new Promise((resolve) => setTimeout(resolve, 8000))
583
587
 
584
- const peers = replicator.peers
588
+ peers = getRecoveryPeers(replicator.peers)
585
589
 
586
590
  if (peers.length < 1 || this.closing) continue
587
591
  if (peers[0].remoteLength > this.length) continue
@@ -607,7 +611,7 @@ module.exports = class SessionState {
607
611
  async append(values, { signature, keyPair, preappend, postappend, maxLength = -1 } = {}) {
608
612
  if (!keyPair && this.isDefault()) keyPair = this.core.header.keyPair
609
613
 
610
- if (this.isDefault() && this.core.header.hints.recovering) await this._waitForRecovery()
614
+ if (this.isDefault() && this.core.header.hints.recovering) await this.waitForRecovery()
611
615
 
612
616
  await this.mutex.lock()
613
617
 
@@ -965,7 +969,7 @@ module.exports = class SessionState {
965
969
  throw ASSERTION('Cannot commit while repair mode is on')
966
970
  }
967
971
 
968
- if (this.core.header.hints.recovering) await this._waitForRecovery()
972
+ if (this.core.header.hints.recovering) await this.waitForRecovery()
969
973
 
970
974
  let srcLocked = false
971
975
  await this.mutex.lock()
@@ -1189,6 +1193,8 @@ module.exports = class SessionState {
1189
1193
  }
1190
1194
  }
1191
1195
 
1196
+ module.exports = SessionState
1197
+
1192
1198
  function noop() {}
1193
1199
 
1194
1200
  function getBitfieldPage(index) {
@@ -1287,3 +1293,20 @@ function isRootIndex(index, roots) {
1287
1293
 
1288
1294
  return false
1289
1295
  }
1296
+
1297
+ function getRecoveryPeers(peers) {
1298
+ if (!SessionState.RECOVERY_PEERS) return peers
1299
+
1300
+ const actual = []
1301
+
1302
+ for (const p of peers) {
1303
+ for (const r of SessionState.RECOVERY_PEERS) {
1304
+ if (b4a.equals(p.remotePublicKey, r)) {
1305
+ actual.push(p)
1306
+ break
1307
+ }
1308
+ }
1309
+ }
1310
+
1311
+ return actual
1312
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.27.1",
3
+ "version": "11.27.3",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {