hypercore 10.5.4 → 10.6.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 (3) hide show
  1. package/index.js +10 -11
  2. package/lib/replicator.js +25 -16
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -385,7 +385,7 @@ module.exports = class Hypercore extends EventEmitter {
385
385
  }
386
386
 
387
387
  async _close (err) {
388
- await this.opening
388
+ if (this.opened === false) await this.opening
389
389
 
390
390
  const i = this.sessions.indexOf(this)
391
391
  if (i === -1) return
@@ -646,20 +646,19 @@ module.exports = class Hypercore extends EventEmitter {
646
646
  return this._updateSnapshot()
647
647
  }
648
648
 
649
- const activeRequests = (opts && opts.activeRequests) || this.activeRequests
650
- const req = this.replicator.addUpgrade(activeRequests)
649
+ const remoteWait = typeof (opts && opts.wait) === 'boolean' ? opts.wait : this.replicator.findingPeers > 0
651
650
 
652
- let upgraded = await req.promise
651
+ let upgraded = false
653
652
 
654
- if (!this.sparse) {
655
- // Download all available blocks in non-sparse mode
656
- const start = this.length
657
- const end = this.core.tree.length
658
- const contig = this.contiguousLength
653
+ if (await this.replicator.applyPendingReorg()) {
654
+ upgraded = true
655
+ }
659
656
 
660
- await this.download({ start, end, ifAvailable: true }).done()
657
+ if (!upgraded && remoteWait) {
658
+ const activeRequests = (opts && opts.activeRequests) || this.activeRequests
659
+ const req = this.replicator.addUpgrade(activeRequests)
661
660
 
662
- if (!upgraded) upgraded = this.contiguousLength !== contig
661
+ upgraded = await req.promise
663
662
  }
664
663
 
665
664
  if (!upgraded) return false
package/lib/replicator.js CHANGED
@@ -909,7 +909,7 @@ module.exports = class Replicator {
909
909
  this._hadPeers = false
910
910
  this._ifAvailable = 0
911
911
  this._updatesPending = 0
912
- this._applyingReorg = false
912
+ this._applyingReorg = null
913
913
  }
914
914
 
915
915
  cork () {
@@ -959,6 +959,23 @@ module.exports = class Replicator {
959
959
  await Promise.allSettled(all)
960
960
  }
961
961
 
962
+ async applyPendingReorg () {
963
+ if (this._applyingReorg !== null) {
964
+ await this._applyingReorg
965
+ return true
966
+ }
967
+
968
+ for (let i = this._reorgs.length - 1; i >= 0; i--) {
969
+ const f = this._reorgs[i]
970
+ if (f.batch !== null && f.batch.finished) {
971
+ await this._applyReorg(f)
972
+ return true
973
+ }
974
+ }
975
+
976
+ return false
977
+ }
978
+
962
979
  addUpgrade (session) {
963
980
  if (this._upgrade !== null) {
964
981
  const ref = this._upgrade.attach(session)
@@ -968,14 +985,6 @@ module.exports = class Replicator {
968
985
 
969
986
  const ref = this._addUpgrade().attach(session)
970
987
 
971
- for (let i = this._reorgs.length - 1; i >= 0 && this._applyingReorg === false; i--) {
972
- const f = this._reorgs[i]
973
- if (f.batch !== null && f.batch.finished) {
974
- this._applyReorg(f)
975
- break
976
- }
977
- }
978
-
979
988
  this.updateAll()
980
989
 
981
990
  return ref
@@ -1070,7 +1079,7 @@ module.exports = class Replicator {
1070
1079
 
1071
1080
  // check if reorgs in progress...
1072
1081
 
1073
- if (this._applyingReorg === true) return
1082
+ if (this._applyingReorg !== null) return
1074
1083
 
1075
1084
  // TODO: we prob should NOT wait for inflight reorgs here, seems better to just resolve the upgrade
1076
1085
  // and then apply the reorg on the next call in case it's slow - needs some testing in practice
@@ -1429,17 +1438,17 @@ module.exports = class Replicator {
1429
1438
 
1430
1439
  const u = this._upgrade
1431
1440
 
1432
- this._applyingReorg = true
1433
1441
  this._reorgs = [] // clear all as the nodes are against the old tree - easier
1442
+ this._applyingReorg = this.core.reorg(f.batch, null) // TODO: null should be the first/last peer?
1434
1443
 
1435
1444
  try {
1436
- await this.core.reorg(f.batch, null) // TODO: null should be the first/last peer?
1445
+ await this._applyingReorg
1437
1446
  } catch (err) {
1438
1447
  this._upgrade = null
1439
1448
  u.reject(err)
1440
1449
  }
1441
1450
 
1442
- this._applyingReorg = false
1451
+ this._applyingReorg = null
1443
1452
 
1444
1453
  if (this._upgrade !== null) {
1445
1454
  this._resolveUpgradeRequest(null)
@@ -1461,7 +1470,7 @@ module.exports = class Replicator {
1461
1470
  }
1462
1471
 
1463
1472
  _updateFork (peer) {
1464
- if (this._applyingReorg === true || this.allowFork === false || peer.remoteFork <= this.core.tree.fork) {
1473
+ if (this._applyingReorg !== null || this.allowFork === false || peer.remoteFork <= this.core.tree.fork) {
1465
1474
  return false
1466
1475
  }
1467
1476
 
@@ -1531,7 +1540,7 @@ module.exports = class Replicator {
1531
1540
 
1532
1541
  updatePeer (peer) {
1533
1542
  // Quick shortcut to wait for flushing reorgs - not needed but less waisted requests
1534
- if (this._applyingReorg === true) return
1543
+ if (this._applyingReorg !== null) return
1535
1544
 
1536
1545
  while (this._updatePeer(peer) === true);
1537
1546
  while (this._updatePeerNonPrimary(peer) === true);
@@ -1542,7 +1551,7 @@ module.exports = class Replicator {
1542
1551
 
1543
1552
  updateAll () {
1544
1553
  // Quick shortcut to wait for flushing reorgs - not needed but less waisted requests
1545
- if (this._applyingReorg === true) return
1554
+ if (this._applyingReorg !== null) return
1546
1555
 
1547
1556
  const peers = new RandomIterator(this.peers)
1548
1557
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.5.4",
3
+ "version": "10.6.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -54,7 +54,7 @@
54
54
  },
55
55
  "devDependencies": {
56
56
  "brittle": "^3.0.0",
57
- "hyperswarm": "^4.3.0",
57
+ "hyperswarm": "^4.3.6",
58
58
  "random-access-memory": "^6.1.0",
59
59
  "random-access-memory-overlay": "^3.0.0",
60
60
  "standard": "^17.0.0",