hypercore-storage 0.0.25 → 0.0.27

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
@@ -358,7 +358,7 @@ module.exports = class CoreStorage {
358
358
  return new HypercoreStorage(this, discoveryKey, core, data, null)
359
359
  }
360
360
 
361
- async create ({ key, manifest, keyPair, encryptionKey, discoveryKey }) {
361
+ async create ({ key, manifest, keyPair, encryptionKey, discoveryKey, userData }) {
362
362
  await this.mutex.write.lock()
363
363
 
364
364
  try {
@@ -390,7 +390,7 @@ module.exports = class CoreStorage {
390
390
  const batch = new WriteBatch(storage, write)
391
391
 
392
392
  initialiseCoreInfo(batch, { key, manifest, keyPair, encryptionKey })
393
- initialiseCoreData(batch)
393
+ initialiseCoreData(batch, { userData })
394
394
 
395
395
  await batch.flush()
396
396
  return storage
@@ -745,6 +745,11 @@ function initialiseCoreInfo (db, { key, manifest, keyPair, encryptionKey }) {
745
745
  if (encryptionKey) db.setEncryptionKey(encryptionKey)
746
746
  }
747
747
 
748
- function initialiseCoreData (db) {
748
+ function initialiseCoreData (db, { userData } = {}) {
749
749
  db.setDataInfo({ version: 0 })
750
+ if (userData) {
751
+ for (const { key, value } of userData) {
752
+ db.setUserData(key, value)
753
+ }
754
+ }
750
755
  }
@@ -1,7 +1,98 @@
1
1
  const { ASSERTION } = require('hypercore-errors')
2
+ const { Readable, isEnded, getStreamError } = require('streamx')
2
3
 
3
4
  const TipList = require('./tip-list')
4
5
 
6
+ class OverlayStream extends Readable {
7
+ constructor (tip, storage, createStream, opts = {}) {
8
+ super()
9
+
10
+ this.tip = tip
11
+ this.storage = storage
12
+ this.opts = opts
13
+
14
+ this._reverse = !!opts.reverse
15
+ this._active = null
16
+ this._position = this._reverse ? tip.length() - 1 : tip.offset
17
+
18
+ this._createStream = createStream
19
+ this._pendingDestroy = null
20
+
21
+ if (!opts.reverse) {
22
+ this._active = createStream(storage, opts)
23
+ this._active.on('close', this._onclose.bind(this))
24
+ this._active.on('data', this._ondata.bind(this))
25
+ }
26
+ }
27
+
28
+ _predestroy () {
29
+ if (this._active) this._active.destroy()
30
+ }
31
+
32
+ _read (cb) {
33
+ if (this._active === null) {
34
+ if (this._yield()) return cb(null)
35
+ }
36
+
37
+ if (this._active) {
38
+ this._active.resume()
39
+ }
40
+
41
+ cb(null)
42
+ }
43
+
44
+ _yield () {
45
+ while (this._memoryRemaining()) {
46
+ const { valid, value } = this.tip.get(this._position)
47
+ const index = this._nextIndex()
48
+
49
+ if (!valid) continue
50
+ if (!this.push({ index, page: value })) break
51
+ }
52
+
53
+ return this._onmemorydrain()
54
+ }
55
+
56
+ _nextIndex () {
57
+ if (this._reverse) return this._position--
58
+ return this._position++
59
+ }
60
+
61
+ _memoryRemaining () {
62
+ if (this._reverse) return this._position >= this.tip.offset
63
+ return this._position < this.tip.length()
64
+ }
65
+
66
+ _onmemorydrain () {
67
+ if (!this._reverse) {
68
+ this.push(null)
69
+ return true
70
+ }
71
+
72
+ this._active = this._createStream(this.storage, this.opts)
73
+ this._active.on('close', this._onclose.bind(this))
74
+ this._active.on('data', this._ondata.bind(this))
75
+
76
+ return false
77
+ }
78
+
79
+ _ondata (data) {
80
+ if (!this.push(data)) this._active.pause()
81
+ }
82
+
83
+ _onclose () {
84
+ if (!isEnded(this._active)) {
85
+ const error = getStreamError(this._active)
86
+ this.destroy(error)
87
+ return
88
+ }
89
+
90
+ if (this._reverse) return this.push(null)
91
+
92
+ this._yield()
93
+ }
94
+ }
95
+
5
96
  class MemoryOverlay {
6
97
  constructor (storage) {
7
98
  this.storage = storage
@@ -24,7 +115,7 @@ class MemoryOverlay {
24
115
  }
25
116
 
26
117
  snapshot () {
27
- todo()
118
+ return new MemoryOverlay(this.storage.snapshot())
28
119
  }
29
120
 
30
121
  get dependencies () {
@@ -56,8 +147,8 @@ class MemoryOverlay {
56
147
  todo()
57
148
  }
58
149
 
59
- createBitfieldPageStream () {
60
- todo()
150
+ createBitfieldPageStream (opts) {
151
+ return new OverlayStream(this.bitfields, this.storage, createBitfieldPageStream, opts)
61
152
  }
62
153
 
63
154
  async peekLastTreeNode () {
@@ -266,6 +357,10 @@ class MemoryOverlayWriteBatch {
266
357
  }
267
358
  }
268
359
 
360
+ function createBitfieldPageStream (storage, opts) {
361
+ return storage.createBitfieldPageStream(opts)
362
+ }
363
+
269
364
  function mergeMap (a, b) {
270
365
  if (a === null) return b
271
366
  for (const [key, value] of b) a.set(key, value)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "0.0.25",
3
+ "version": "0.0.27",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",