hypercore 10.37.4 → 10.37.5

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.
@@ -3,6 +3,7 @@ const crypto = require('hypercore-crypto')
3
3
  const c = require('compact-encoding')
4
4
  const Xache = require('xache')
5
5
  const b4a = require('b4a')
6
+ const unslab = require('unslab')
6
7
  const caps = require('./caps')
7
8
  const { INVALID_PROOF, INVALID_CHECKSUM, INVALID_OPERATION, BAD_ARGUMENT, ASSERTION } = require('hypercore-errors')
8
9
 
@@ -1224,7 +1225,13 @@ function getStoredNode (storage, index, cache, error) {
1224
1225
  }
1225
1226
 
1226
1227
  const node = { index, size, hash }
1227
- if (cache !== null) cache.set(index, node)
1228
+
1229
+ if (cache !== null) {
1230
+ // Copy hash to a new buffer to avoid blocking gc of its original slab
1231
+ node.hash = unslab(hash)
1232
+ cache.set(index, node)
1233
+ }
1234
+
1228
1235
  resolve(node)
1229
1236
  })
1230
1237
  })
package/lib/replicator.js CHANGED
@@ -144,7 +144,7 @@ class BlockRequest extends Attachable {
144
144
  this.queued = false
145
145
 
146
146
  for (const req of this.inflight) {
147
- req.peer._cancelRequest(req.id)
147
+ req.peer._cancelRequest(req)
148
148
  }
149
149
 
150
150
  this.tracker.remove(this.index)
@@ -747,20 +747,18 @@ class Peer {
747
747
  })
748
748
  }
749
749
 
750
- _cancelRequest (id) {
751
- const req = this.replicator._inflight.get(id)
752
- if (!req) return
753
-
754
- // mark as cancelled also
750
+ _cancelRequest (req) {
751
+ if (req.priority === PRIORITY.CANCELLED) return
752
+ // mark as cancelled also and avoid re-entry
755
753
  req.priority = PRIORITY.CANCELLED
756
754
 
757
755
  this.inflight--
758
- this.replicator._removeInflight(id, false)
756
+ this.replicator._requestDone(req.id, false)
759
757
  if (isBlockRequest(req)) this.replicator._unmarkInflight(req.block.index)
760
758
 
761
759
  if (this.roundtripQueue === null) this.roundtripQueue = new RoundtripQueue()
762
- this.roundtripQueue.add(id)
763
- this.wireCancel.send({ request: id })
760
+ this.roundtripQueue.add(req.id)
761
+ this.wireCancel.send({ request: req.id })
764
762
  }
765
763
 
766
764
  _checkIfConflict () {
@@ -865,8 +863,12 @@ class Peer {
865
863
  }
866
864
 
867
865
  _onrequestroundtrip (req) {
866
+ if (req.priority === PRIORITY.CANCELLED) return
867
+ // to avoid re-entry we also just mark it as cancelled
868
+ req.priority = PRIORITY.CANCELLED
869
+
868
870
  this.inflight--
869
- this.replicator._removeInflight(req.id, true)
871
+ this.replicator._requestDone(req.id, true)
870
872
  if (this.roundtripQueue === null) return
871
873
  const flushed = this.roundtripQueue.flush(req.rt)
872
874
  if (flushed === null) return
@@ -1333,9 +1335,11 @@ class Peer {
1333
1335
  try {
1334
1336
  if (req.block !== null && req.fork === fork) {
1335
1337
  req.block.nodes = await this.core.tree.missingNodes(2 * req.block.index)
1338
+ if (req.priority === PRIORITY.CANCELLED) return
1336
1339
  }
1337
1340
  if (req.hash !== null && req.fork === fork && req.hash.nodes === 0) {
1338
1341
  req.hash.nodes = await this.core.tree.missingNodes(req.hash.index)
1342
+ if (req.priority === PRIORITY.CANCELLED) return
1339
1343
 
1340
1344
  // nodes === 0, we already have it, bail
1341
1345
  if (req.hash.nodes === 0 && (req.hash.index & 1) === 0) {
@@ -1349,9 +1353,6 @@ class Peer {
1349
1353
  return
1350
1354
  }
1351
1355
 
1352
- // this was cancelled during the above async work
1353
- if (req.priority === PRIORITY.CANCELLED) return
1354
-
1355
1356
  this.tracer.trace('send', req)
1356
1357
 
1357
1358
  this.wireRequest.send(req)
@@ -1703,7 +1704,7 @@ module.exports = class Replicator {
1703
1704
  this.onpeerupdate(true, peer)
1704
1705
  }
1705
1706
 
1706
- _removeInflight (id, roundtrip) {
1707
+ _requestDone (id, roundtrip) {
1707
1708
  this._inflight.remove(id, roundtrip)
1708
1709
  if (this.isDownloading() === true) return
1709
1710
  for (const peer of this.peers) peer.signalUpgrade()
@@ -1731,7 +1732,7 @@ module.exports = class Replicator {
1731
1732
  }
1732
1733
 
1733
1734
  _resolveHashLocally (peer, req) {
1734
- this._removeInflight(req.id, false)
1735
+ this._requestDone(req.id, false)
1735
1736
  this._resolveBlockRequest(this._hashes, req.hash.index / 2, null, req)
1736
1737
  this.updatePeer(peer)
1737
1738
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.37.4",
3
+ "version": "10.37.5",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -61,6 +61,7 @@
61
61
  "safety-catch": "^1.0.1",
62
62
  "sodium-universal": "^4.0.0",
63
63
  "streamx": "^2.12.4",
64
+ "unslab": "^1.0.0",
64
65
  "xache": "^1.1.0",
65
66
  "z32": "^1.0.0"
66
67
  },