hypercore 10.10.0 → 10.11.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
@@ -227,7 +227,7 @@ partialStream.pipe(process.stdout)
227
227
  }
228
228
  ```
229
229
 
230
- #### `await core.clear(start, [end])`
230
+ #### `const cleared = await core.clear(start, [end], [options])`
231
231
 
232
232
  Clear stored blocks between `start` and `end`, reclaiming storage when possible.
233
233
 
@@ -238,6 +238,13 @@ await core.clear(0, 10) // clear block 0-10 from your local cache
238
238
 
239
239
  The core will also gossip to peers it is connected to, that is no longer has these blocks.
240
240
 
241
+ `options` include:
242
+ ```js
243
+ {
244
+ diff: false // Returned `cleared` bytes object is null unless you enable this
245
+ }
246
+ ```
247
+
241
248
  #### `await core.truncate(newLength, [forkId])`
242
249
 
243
250
  Truncate the core to a smaller length.
package/index.js CHANGED
@@ -745,9 +745,13 @@ module.exports = class Hypercore extends EventEmitter {
745
745
  end = start + 1
746
746
  }
747
747
 
748
- if (start >= end) return
748
+ const cleared = (opts && opts.diff) ? { blocks: 0 } : null
749
749
 
750
- await this.core.clear(start, end)
750
+ if (start >= end) return cleared
751
+
752
+ await this.core.clear(start, end, cleared)
753
+
754
+ return cleared
751
755
  }
752
756
 
753
757
  async purge () {
package/lib/core.js CHANGED
@@ -5,6 +5,7 @@ const Mutex = require('./mutex')
5
5
  const MerkleTree = require('./merkle-tree')
6
6
  const BlockStore = require('./block-store')
7
7
  const Bitfield = require('./bitfield')
8
+ const Info = require('./info')
8
9
  const { BAD_ARGUMENT, STORAGE_EMPTY, STORAGE_CONFLICT, INVALID_SIGNATURE } = require('./errors')
9
10
  const m = require('./messages')
10
11
 
@@ -238,7 +239,7 @@ module.exports = class Core {
238
239
  }
239
240
  }
240
241
 
241
- async clear (start, end) {
242
+ async clear (start, end, cleared) {
242
243
  await this._mutex.lock()
243
244
 
244
245
  try {
@@ -265,13 +266,20 @@ module.exports = class Core {
265
266
  end = this.bitfield.firstSet(end)
266
267
 
267
268
  if (end === -1) end = this.tree.length
269
+ if (start >= end || start >= this.tree.length) return
268
270
 
269
271
  const offset = await this.tree.byteOffset(start * 2)
270
272
  const [byteEnd, byteEndLength] = await this.tree.byteRange((end - 1) * 2)
271
273
  const length = (byteEnd + byteEndLength) - offset
272
274
 
275
+ const before = cleared ? await Info.bytesUsed(this.blocks.storage) : null
276
+
273
277
  await this.blocks.clear(offset, length)
274
278
 
279
+ const after = cleared ? await Info.bytesUsed(this.blocks.storage) : null
280
+
281
+ if (cleared) cleared.blocks = Math.max(before - after, 0)
282
+
275
283
  this.onupdate(0, entry.bitfield, null, null)
276
284
 
277
285
  if (this._shouldFlush()) await this._flushOplog()
package/lib/info.js CHANGED
@@ -27,27 +27,27 @@ module.exports = class Info {
27
27
  const { oplog, tree, blocks, bitfield } = session.core
28
28
  try {
29
29
  return {
30
- oplog: await bytesUsed(oplog.storage),
31
- tree: await bytesUsed(tree.storage),
32
- blocks: await bytesUsed(blocks.storage),
33
- bitfield: await bytesUsed(bitfield.storage)
30
+ oplog: await Info.bytesUsed(oplog.storage),
31
+ tree: await Info.bytesUsed(tree.storage),
32
+ blocks: await Info.bytesUsed(blocks.storage),
33
+ bitfield: await Info.bytesUsed(bitfield.storage)
34
34
  }
35
35
  } catch {
36
36
  return null
37
37
  }
38
+ }
38
39
 
39
- function bytesUsed (file) {
40
- return new Promise((resolve, reject) => {
41
- file.stat((err, st) => {
42
- if (err) {
43
- resolve(0) // prob just file not found (TODO, improve)
44
- } else if (typeof st.blocks !== 'number') {
45
- reject(new Error('cannot determine bytes used'))
46
- } else {
47
- resolve(st.blocks * 512)
48
- }
49
- })
40
+ static bytesUsed (file) {
41
+ return new Promise((resolve, reject) => {
42
+ file.stat((err, st) => {
43
+ if (err) {
44
+ resolve(0) // prob just file not found (TODO, improve)
45
+ } else if (typeof st.blocks !== 'number') {
46
+ reject(new Error('cannot determine bytes used'))
47
+ } else {
48
+ resolve(st.blocks * 512)
49
+ }
50
50
  })
51
- }
51
+ })
52
52
  }
53
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.10.0",
3
+ "version": "10.11.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {