hypercore 11.37.2 → 11.39.0

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/README.md CHANGED
@@ -168,6 +168,10 @@ const blockLocal = await core.get(44, { wait: false })
168
168
 
169
169
  Check if the core has all blocks between `start` and `end`.
170
170
 
171
+ #### `const count = await core.count(start, end)`
172
+
173
+ Count how many blocks between `start` and `end` the core has stored locally.
174
+
171
175
  #### `const updated = await core.update([options])`
172
176
 
173
177
  Waits for initial proof of the new core length until all `findingPeers` calls have finished.
package/index.js CHANGED
@@ -880,6 +880,24 @@ class Hypercore extends EventEmitter {
880
880
  return count === end - start
881
881
  }
882
882
 
883
+ async count(start, end) {
884
+ if (this.opened === false) await this.opening
885
+ if (!isValidIndex(start) || !isValidIndex(end)) {
886
+ throw ASSERTION('count range is invalid', this.discoveryKey)
887
+ }
888
+
889
+ if (this.state.isDefault()) return this.core.bitfield.countSet(start, end - start)
890
+
891
+ let count = 0
892
+
893
+ const stream = this.state.storage.createBlockStream({ gte: start, lt: end })
894
+ for await (const block of stream) {
895
+ if (block !== null) count++
896
+ }
897
+
898
+ return count
899
+ }
900
+
883
901
  async get(index, opts) {
884
902
  if (this.opened === false) await this.opening
885
903
  if (!isValidIndex(index)) throw ASSERTION('block index is invalid', this.discoveryKey)
@@ -937,7 +955,7 @@ class Hypercore extends EventEmitter {
937
955
  }
938
956
 
939
957
  async purge() {
940
- await this._closeAllSessions(null)
958
+ if (this.opened === false) await this.opening
941
959
  await this.core.purge()
942
960
  }
943
961
 
package/lib/core.js CHANGED
@@ -939,6 +939,31 @@ module.exports = class Core {
939
939
  await Promise.allSettled(all)
940
940
  }
941
941
 
942
+ // closes every session and deletes the core from storage
943
+ async purge() {
944
+ if (this.closing !== null) throw new Error('Cannot purge a closed core')
945
+
946
+ const db = this.db
947
+ const ptr = this.storage.core
948
+ const autoClose = this.autoClose
949
+
950
+ // closing the last session would close the storage along with the core
951
+ this.autoClose = false
952
+
953
+ try {
954
+ await this.closeAllSessions(null)
955
+ if (this.hasSession() === true) throw new Error('Cannot purge while sessions are open')
956
+ await this.close()
957
+ } catch (err) {
958
+ this.autoClose = autoClose
959
+ throw err
960
+ }
961
+
962
+ await db.deleteCore(ptr)
963
+
964
+ if (autoClose) await db.close()
965
+ }
966
+
942
967
  async destroy() {
943
968
  if (this.destroyed === true) return
944
969
  this.destroyed = true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.37.2",
3
+ "version": "11.39.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -54,7 +54,7 @@
54
54
  "hypercore-crypto": "^3.2.1",
55
55
  "hypercore-errors": "^1.5.0",
56
56
  "hypercore-id-encoding": "^1.2.0",
57
- "hypercore-storage": "^3.2.0",
57
+ "hypercore-storage": "^3.3.2",
58
58
  "is-options": "^1.0.1",
59
59
  "nanoassert": "^2.0.0",
60
60
  "protomux": "^3.5.0",