hypercore 11.37.2 → 11.38.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.
Files changed (3) hide show
  1. package/README.md +4 -0
  2. package/index.js +18 -0
  3. package/package.json +1 -1
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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.37.2",
3
+ "version": "11.38.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {