hypercore 10.0.0-alpha.24 → 10.0.0-alpha.25

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
@@ -101,6 +101,10 @@ Truncate the core to a smaller length.
101
101
  Per default this will update the fork id of the core to `+ 1`, but you can set the fork id you prefer with the option.
102
102
  Note that the fork id should be monotonely incrementing.
103
103
 
104
+ #### `const hash = await core.treeHash([length])`
105
+
106
+ Get the Merkle Tree hash of the core at a given length, defaulting to the current length of the core.
107
+
104
108
  #### `const stream = core.createReadStream([options])`
105
109
 
106
110
  Make a read stream. Options include:
package/index.js CHANGED
@@ -73,6 +73,7 @@ module.exports = class Hypercore extends EventEmitter {
73
73
  this.opening.catch(noop)
74
74
 
75
75
  this._preappend = preappend.bind(this)
76
+ this._snapshot = opts.snapshot || null
76
77
  }
77
78
 
78
79
  [inspect] (depth, opts) {
@@ -129,6 +130,10 @@ module.exports = class Hypercore extends EventEmitter {
129
130
  }
130
131
  }
131
132
 
133
+ snapshot () {
134
+ return this.session({ snapshot: { length: this.length, byteLength: this.byteLength, fork: this.fork } })
135
+ }
136
+
132
137
  session (opts = {}) {
133
138
  if (this.closing) {
134
139
  // This makes the closing logic alot easier. If this turns out to be a problem
@@ -277,6 +282,7 @@ module.exports = class Hypercore extends EventEmitter {
277
282
  this.readable = false
278
283
  this.writable = false
279
284
  this.closed = true
285
+ this.opened = false
280
286
 
281
287
  if (this.sessions.length) {
282
288
  // if this is the last session and we are auto closing, trigger that first to enforce error handling
@@ -306,15 +312,21 @@ module.exports = class Hypercore extends EventEmitter {
306
312
  }
307
313
 
308
314
  get length () {
309
- return this.core === null ? 0 : this.core.tree.length
315
+ return this._snapshot
316
+ ? this._snapshot.length
317
+ : (this.core === null ? 0 : this.core.tree.length)
310
318
  }
311
319
 
312
320
  get byteLength () {
313
- return this.core === null ? 0 : this.core.tree.byteLength - (this.core.tree.length * this.padding)
321
+ return this._snapshot
322
+ ? this._snapshot.byteLength
323
+ : (this.core === null ? 0 : this.core.tree.byteLength - (this.core.tree.length * this.padding))
314
324
  }
315
325
 
316
326
  get fork () {
317
- return this.core === null ? 0 : this.core.tree.fork
327
+ return this._snapshot
328
+ ? this._snapshot.fork
329
+ : (this.core === null ? 0 : this.core.tree.fork)
318
330
  }
319
331
 
320
332
  get peers () {
@@ -518,6 +530,16 @@ module.exports = class Hypercore extends EventEmitter {
518
530
  return await this.core.append(buffers, this.sign, { preappend })
519
531
  }
520
532
 
533
+ async treeHash (length) {
534
+ if (length === undefined) {
535
+ await this.ready()
536
+ length = this.core.length
537
+ }
538
+
539
+ const roots = await this.core.tree.getRoots(length)
540
+ return this.crypto.tree(roots)
541
+ }
542
+
521
543
  registerExtension (name, handlers) {
522
544
  return this.extensions.register(name, handlers)
523
545
  }
@@ -392,6 +392,21 @@ module.exports = class MerkleTree {
392
392
  return Promise.all(roots)
393
393
  }
394
394
 
395
+ async upgradeable (length) {
396
+ const indexes = flat.fullRoots(2 * length)
397
+ const roots = new Array(indexes.length)
398
+
399
+ for (let i = 0; i < indexes.length; i++) {
400
+ roots[i] = this.get(indexes[i], false)
401
+ }
402
+
403
+ for (const node of await Promise.all(roots)) {
404
+ if (node === null) return false
405
+ }
406
+
407
+ return true
408
+ }
409
+
395
410
  get (index, error = true) {
396
411
  let node = this.unflushed.get(index)
397
412
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.24",
3
+ "version": "10.0.0-alpha.25",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {