hypercore 10.37.4 → 10.37.6

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)
@@ -300,9 +300,14 @@ class RoundtripQueue {
300
300
  }
301
301
 
302
302
  clear () {
303
- const q = this.queue
303
+ const ids = new Array(this.queue.length)
304
+ for (let i = 0; i < ids.length; i++) {
305
+ ids[i] = this.queue[i][1]
306
+ }
307
+
304
308
  this.queue = []
305
- return q
309
+
310
+ return ids
306
311
  }
307
312
 
308
313
  add (id) {
@@ -747,20 +752,18 @@ class Peer {
747
752
  })
748
753
  }
749
754
 
750
- _cancelRequest (id) {
751
- const req = this.replicator._inflight.get(id)
752
- if (!req) return
753
-
754
- // mark as cancelled also
755
+ _cancelRequest (req) {
756
+ if (req.priority === PRIORITY.CANCELLED) return
757
+ // mark as cancelled also and avoid re-entry
755
758
  req.priority = PRIORITY.CANCELLED
756
759
 
757
760
  this.inflight--
758
- this.replicator._removeInflight(id, false)
761
+ this.replicator._requestDone(req.id, false)
759
762
  if (isBlockRequest(req)) this.replicator._unmarkInflight(req.block.index)
760
763
 
761
764
  if (this.roundtripQueue === null) this.roundtripQueue = new RoundtripQueue()
762
- this.roundtripQueue.add(id)
763
- this.wireCancel.send({ request: id })
765
+ this.roundtripQueue.add(req.id)
766
+ this.wireCancel.send({ request: req.id })
764
767
  }
765
768
 
766
769
  _checkIfConflict () {
@@ -865,8 +868,12 @@ class Peer {
865
868
  }
866
869
 
867
870
  _onrequestroundtrip (req) {
871
+ if (req.priority === PRIORITY.CANCELLED) return
872
+ // to avoid re-entry we also just mark it as cancelled
873
+ req.priority = PRIORITY.CANCELLED
874
+
868
875
  this.inflight--
869
- this.replicator._removeInflight(req.id, true)
876
+ this.replicator._requestDone(req.id, true)
870
877
  if (this.roundtripQueue === null) return
871
878
  const flushed = this.roundtripQueue.flush(req.rt)
872
879
  if (flushed === null) return
@@ -1333,9 +1340,11 @@ class Peer {
1333
1340
  try {
1334
1341
  if (req.block !== null && req.fork === fork) {
1335
1342
  req.block.nodes = await this.core.tree.missingNodes(2 * req.block.index)
1343
+ if (req.priority === PRIORITY.CANCELLED) return
1336
1344
  }
1337
1345
  if (req.hash !== null && req.fork === fork && req.hash.nodes === 0) {
1338
1346
  req.hash.nodes = await this.core.tree.missingNodes(req.hash.index)
1347
+ if (req.priority === PRIORITY.CANCELLED) return
1339
1348
 
1340
1349
  // nodes === 0, we already have it, bail
1341
1350
  if (req.hash.nodes === 0 && (req.hash.index & 1) === 0) {
@@ -1349,9 +1358,6 @@ class Peer {
1349
1358
  return
1350
1359
  }
1351
1360
 
1352
- // this was cancelled during the above async work
1353
- if (req.priority === PRIORITY.CANCELLED) return
1354
-
1355
1361
  this.tracer.trace('send', req)
1356
1362
 
1357
1363
  this.wireRequest.send(req)
@@ -1703,7 +1709,7 @@ module.exports = class Replicator {
1703
1709
  this.onpeerupdate(true, peer)
1704
1710
  }
1705
1711
 
1706
- _removeInflight (id, roundtrip) {
1712
+ _requestDone (id, roundtrip) {
1707
1713
  this._inflight.remove(id, roundtrip)
1708
1714
  if (this.isDownloading() === true) return
1709
1715
  for (const peer of this.peers) peer.signalUpgrade()
@@ -1731,7 +1737,7 @@ module.exports = class Replicator {
1731
1737
  }
1732
1738
 
1733
1739
  _resolveHashLocally (peer, req) {
1734
- this._removeInflight(req.id, false)
1740
+ this._requestDone(req.id, false)
1735
1741
  this._resolveBlockRequest(this._hashes, req.hash.index / 2, null, req)
1736
1742
  this.updatePeer(peer)
1737
1743
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.37.4",
3
+ "version": "10.37.6",
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
  },