hypercore 11.39.2 → 11.39.4

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
@@ -1009,6 +1009,8 @@ class Peer {
1009
1009
 
1010
1010
  await this.replicator._txLock.lock()
1011
1011
  try {
1012
+ if (this.replicator.destroyed) return
1013
+
1012
1014
  const remoteLength = Math.max(this.remoteLength, this.pushedLength)
1013
1015
 
1014
1016
  msg.block = {
@@ -1270,7 +1272,7 @@ class Peer {
1270
1272
  return
1271
1273
  }
1272
1274
 
1273
- if (this.core.closed && !isCriticalError(err)) return
1275
+ if (this.replicator.destroyed && !isCriticalError(err)) return
1274
1276
 
1275
1277
  if (err.code !== 'INVALID_OPERATION') {
1276
1278
  // might be a fork, verify
@@ -2203,6 +2205,8 @@ module.exports = class Replicator {
2203
2205
  }
2204
2206
 
2205
2207
  async applyPendingReorg() {
2208
+ if (this.destroyed) return false
2209
+
2206
2210
  if (this._applyingReorg !== null) {
2207
2211
  await this._applyingReorg
2208
2212
  return true
@@ -2498,6 +2502,8 @@ module.exports = class Replicator {
2498
2502
 
2499
2503
  // Runs in the background - not allowed to throw
2500
2504
  async _resolveBlocksLocally() {
2505
+ if (this.destroyed) return
2506
+
2501
2507
  // TODO: check if fork compat etc. Requires that we pass down truncation info
2502
2508
 
2503
2509
  const clear = []
@@ -2946,11 +2952,11 @@ module.exports = class Replicator {
2946
2952
  await this._applyingReorg
2947
2953
  } catch (err) {
2948
2954
  this._upgrade = null
2949
- u.reject(err)
2955
+ if (u !== null) u.reject(err)
2956
+ } finally {
2957
+ this._applyingReorg = null
2950
2958
  }
2951
2959
 
2952
- this._applyingReorg = null
2953
-
2954
2960
  if (this._upgrade !== null) {
2955
2961
  this._resolveUpgradeRequest(null)
2956
2962
  }
@@ -3156,7 +3162,7 @@ module.exports = class Replicator {
3156
3162
  }
3157
3163
 
3158
3164
  attachTo(protomux) {
3159
- if (this.core.closed) return
3165
+ if (this.destroyed) return
3160
3166
 
3161
3167
  const makePeer = this._makePeer.bind(this, protomux)
3162
3168
 
@@ -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.2",
3
+ "version": "11.39.4",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {