hypercore-storage 0.0.29 → 0.0.31

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
@@ -471,6 +471,18 @@ class HypercoreStorage {
471
471
  }
472
472
  }
473
473
 
474
+ async registerOverlay (head) {
475
+ const storage = new MemoryOverlay(this)
476
+ const batch = storage.createWriteBatch()
477
+
478
+ batch.setDataDependency({ data: this.dataPointer, length: head.length })
479
+ if (head.rootHash) batch.setCoreHead(head) // if no root hash its the empty core - no head yet
480
+
481
+ await batch.flush()
482
+
483
+ return storage
484
+ }
485
+
474
486
  createMemoryOverlay () {
475
487
  return new MemoryOverlay(this)
476
488
  }
@@ -501,7 +513,7 @@ class HypercoreStorage {
501
513
  createUserDataStream (opts = {}) {
502
514
  assert(this.destroyed === false)
503
515
 
504
- const r = encodeIndexRange(this.dataPointer, DATA.USER_DATA, this.dbSnapshot, opts)
516
+ const r = encodeUserDataRange(this.dataPointer, DATA.USER_DATA, this.dbSnapshot, opts)
505
517
  const s = this.db.iterator(r)
506
518
  s._readableState.map = mapStreamUserData
507
519
  return s
@@ -572,6 +584,8 @@ function mapStreamUserData (data) {
572
584
 
573
585
  const key = c.string.decode(state)
574
586
 
587
+ if (data.value.byteLength === 0) return null
588
+
575
589
  return { key, value: data.value }
576
590
  }
577
591
 
@@ -643,6 +657,20 @@ function encodeIndexRange (pointer, type, snapshot, opts) {
643
657
  return bounded
644
658
  }
645
659
 
660
+ function encodeUserDataRange (pointer, type, snapshot, opts) {
661
+ const bounded = { snapshot, gt: null, gte: null, lte: null, lt: null, reverse: !!opts.reverse, limit: toLimit(opts.limit) }
662
+
663
+ if (opts.gt || opts.gt === 0) bounded.gt = encodeUserDataIndex(pointer, type, opts.gt)
664
+ else if (opts.gte) bounded.gte = encodeUserDataIndex(pointer, type, opts.gte)
665
+ else bounded.gte = encodeDataIndex(pointer, type, 0)
666
+
667
+ if (opts.lt || opts.lt === 0) bounded.lt = encodeUserDataIndex(pointer, type, opts.lt)
668
+ else if (opts.lte) bounded.lte = encodeUserDataIndex(pointer, type, opts.lte)
669
+ else bounded.lte = encodeDataIndex(pointer, type, Infinity)
670
+
671
+ return bounded
672
+ }
673
+
646
674
  function toLimit (n) {
647
675
  return n === 0 ? 0 : (n || Infinity)
648
676
  }
@@ -196,6 +196,7 @@ class MemoryOverlay {
196
196
  }
197
197
 
198
198
  createBitfieldPageStream (opts) {
199
+ if (this.bitfields === null) return createBitfieldPageStream(this.storage)
199
200
  return new OverlayStream(this.bitfields, this.storage, createBitfieldPageStream, opts)
200
201
  }
201
202
 
package/lib/tip-list.js CHANGED
@@ -78,7 +78,7 @@ module.exports = class TipList {
78
78
  }
79
79
 
80
80
  merge (tip) {
81
- const invalidDeletion = tip.removed && tip.end() !== -1 && tip.end() < this.end()
81
+ const invalidDeletion = tip.removed > 0 && tip.end() !== -1 && tip.end() < this.end()
82
82
  const invalidTip = (tip.removed !== -1 && tip.end() < this.offset) || this.end() < tip.offset
83
83
 
84
84
  if (invalidTip || invalidDeletion) throw ASSERTION('Cannot merge tip list')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",