hypercore 11.39.3 → 11.39.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.
package/index.js CHANGED
@@ -960,7 +960,7 @@ class Hypercore extends EventEmitter {
960
960
  }
961
961
 
962
962
  async _get(index, opts) {
963
- const block = await readBlock(this.state.storage.read(), index)
963
+ const block = await this.state.readBlock(index)
964
964
 
965
965
  if (block !== null) return block
966
966
 
@@ -971,7 +971,7 @@ class Hypercore extends EventEmitter {
971
971
  // snapshot should check if core has block
972
972
  if (this._snapshot !== null) {
973
973
  checkSnapshot(this, index)
974
- const coreBlock = await readBlock(this.core.state.storage.read(), index)
974
+ const coreBlock = await this.core.state.readBlock(index)
975
975
 
976
976
  checkSnapshot(this, index)
977
977
  if (coreBlock !== null) return coreBlock
@@ -980,7 +980,7 @@ class Hypercore extends EventEmitter {
980
980
  // lets check the bitfield to see if we got it during the above async calls
981
981
  // this is the last resort before replication, so always safe.
982
982
  if (this.core.bitfield.get(index)) {
983
- const coreBlock = await readBlock(this.state.storage.read(), index)
983
+ const coreBlock = await this.state.readBlock(index)
984
984
  // TODO: this should not be needed, only needed atm in case we are doing a moveTo during this (we should fix)
985
985
  if (coreBlock !== null) return coreBlock
986
986
  }
@@ -1401,12 +1401,6 @@ function checkSnapshot(snapshot, index) {
1401
1401
  }
1402
1402
  }
1403
1403
 
1404
- function readBlock(rx, index) {
1405
- const promise = rx.getBlock(index)
1406
- rx.tryFlush()
1407
- return promise
1408
- }
1409
-
1410
1404
  function initOnce(session, storage, key, opts) {
1411
1405
  if (storage === null) storage = opts.storage || null
1412
1406
  if (key === null) key = opts.key || null
package/lib/replicator.js CHANGED
@@ -1272,7 +1272,7 @@ class Peer {
1272
1272
  return
1273
1273
  }
1274
1274
 
1275
- if ((this.core.closed || this.replicator.destroyed) && !isCriticalError(err)) return
1275
+ if (this.replicator.destroyed && !isCriticalError(err)) return
1276
1276
 
1277
1277
  if (err.code !== 'INVALID_OPERATION') {
1278
1278
  // might be a fork, verify
@@ -1411,6 +1411,12 @@ class Peer {
1411
1411
  const has = drop === false
1412
1412
 
1413
1413
  if (drop === true && start < this._remoteContiguousLength) {
1414
+ const end = start + length
1415
+ if (end < this._remoteContiguousLength) {
1416
+ this.remoteBitfield.setRange(end, this._remoteContiguousLength, true)
1417
+ this.missingBlocks.setRange(end, this._remoteContiguousLength, true)
1418
+ this._clearLocalRange(end, this._remoteContiguousLength - end)
1419
+ }
1414
1420
  this._remoteContiguousLength = start
1415
1421
  }
1416
1422
 
@@ -3040,7 +3046,11 @@ module.exports = class Replicator {
3040
3046
  const blks = new RandomIterator(this._queued)
3041
3047
 
3042
3048
  for (const b of blks) {
3043
- if (b.queued === false || peer._requestBlock(b) === true) {
3049
+ if (b.queued === false) {
3050
+ blks.dequeue()
3051
+ continue
3052
+ }
3053
+ if (peer._requestBlock(b) === true) {
3044
3054
  b.queued = false
3045
3055
  blks.dequeue()
3046
3056
  return true
@@ -3162,7 +3172,7 @@ module.exports = class Replicator {
3162
3172
  }
3163
3173
 
3164
3174
  attachTo(protomux) {
3165
- if (this.core.closed) return
3175
+ if (this.destroyed) return
3166
3176
 
3167
3177
  const makePeer = this._makePeer.bind(this, protomux)
3168
3178
 
@@ -31,6 +31,7 @@ class SessionState {
31
31
  this.storage = storage
32
32
  this.name = name
33
33
  this.sessions = []
34
+ this.reads = new Map()
34
35
 
35
36
  // small hack to close old storages as late as possible.
36
37
  // TODO: add a read lock so we can avoid that
@@ -64,6 +65,26 @@ class SessionState {
64
65
 
65
66
  static RECOVERY_PEERS = null
66
67
 
68
+ readBlock(index) {
69
+ const reads = this.reads
70
+
71
+ let reading = reads.get(index)
72
+ if (reading !== undefined) return reading
73
+
74
+ const rx = this.storage.read()
75
+ reading = rx.getBlock(index)
76
+ rx.tryFlush()
77
+
78
+ reads.set(index, reading)
79
+ reading.then(ondone, ondone)
80
+
81
+ return reading
82
+
83
+ function ondone() {
84
+ reads.delete(index)
85
+ }
86
+ }
87
+
67
88
  isSnapshot() {
68
89
  return this.storage.snapshotted
69
90
  }
@@ -1209,6 +1230,7 @@ class SessionState {
1209
1230
  const roots = await MerkleTree.getRootsFromStorage(storage, length)
1210
1231
 
1211
1232
  this.storage = storage
1233
+ this.reads = new Map()
1212
1234
  this.prologue = state.prologue
1213
1235
  this.fork = fork
1214
1236
  this.length = length
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.39.3",
3
+ "version": "11.39.5",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {