hypercore 10.20.0 → 10.20.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.
package/index.js CHANGED
@@ -840,6 +840,7 @@ module.exports = class Hypercore extends EventEmitter {
840
840
  const cleared = (opts && opts.diff) ? { blocks: 0 } : null
841
841
 
842
842
  if (start >= end) return cleared
843
+ if (start >= this.length) return cleared
843
844
 
844
845
  await this.core.clear(start, end, cleared)
845
846
 
package/lib/core.js CHANGED
@@ -22,6 +22,7 @@ module.exports = class Core {
22
22
  this.bitfield = bitfield
23
23
  this.defaultAuth = auth
24
24
  this.truncating = 0
25
+ this.closed = false
25
26
 
26
27
  this._maxOplogSize = 65536
27
28
  this._autoFlush = 1
@@ -335,8 +336,8 @@ module.exports = class Core {
335
336
  if (start >= end || start >= this.tree.length) return
336
337
 
337
338
  const offset = await this.tree.byteOffset(start * 2)
338
- const [byteEnd, byteEndLength] = await this.tree.byteRange((end - 1) * 2)
339
- const length = (byteEnd + byteEndLength) - offset
339
+ const endOffset = await this.tree.byteOffset(end * 2)
340
+ const length = endOffset - offset
340
341
 
341
342
  const before = cleared ? await Info.bytesUsed(this.blocks.storage) : null
342
343
 
@@ -639,6 +640,7 @@ module.exports = class Core {
639
640
  }
640
641
 
641
642
  async close () {
643
+ this.closed = true
642
644
  await this._mutex.destroy()
643
645
  await Promise.allSettled([
644
646
  this.oplog.close(),
@@ -714,6 +714,7 @@ module.exports = class MerkleTree {
714
714
  }
715
715
 
716
716
  async byteOffset (index) {
717
+ if (index === 2 * this.length) return this.byteLength
717
718
  if ((index & 1) === 1) index = flat.leftSpan(index)
718
719
 
719
720
  let head = 0
package/lib/replicator.js CHANGED
@@ -560,6 +560,7 @@ class Peer {
560
560
  proof = await this._getProof(msg)
561
561
  } catch (err) { // TODO: better error handling here, ie custom errors
562
562
  safetyCatch(err)
563
+ if (this.replicator.core.closed) throw err // just an extra safety check...
563
564
  }
564
565
  }
565
566
 
@@ -1004,6 +1005,7 @@ module.exports = class Replicator {
1004
1005
  this.onupload = onupload
1005
1006
  this.peers = []
1006
1007
  this.findingPeers = 0 // updateable from the outside
1008
+ this.destroyed = false
1007
1009
 
1008
1010
  this._attached = new Set()
1009
1011
  this._inflight = new InflightTracker()
@@ -1711,7 +1713,7 @@ module.exports = class Replicator {
1711
1713
  protomux.stream.opened.then((opened) => {
1712
1714
  this._ifAvailable--
1713
1715
 
1714
- if (opened) makePeer()
1716
+ if (opened && !this.destroyed) makePeer()
1715
1717
  else if (session) session.close().catch(noop)
1716
1718
  this._checkUpgradeIfAvailable()
1717
1719
  })
@@ -1725,6 +1727,7 @@ module.exports = class Replicator {
1725
1727
  }
1726
1728
 
1727
1729
  destroy () {
1730
+ this.destroyed = true
1728
1731
  for (const peer of this.peers) {
1729
1732
  this.detachFrom(peer.protomux)
1730
1733
  peer.channel.close()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.20.0",
3
+ "version": "10.20.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {