hypercore-storage 0.0.27 → 0.0.28
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/lib/memory-overlay.js +41 -12
- package/package.json +1 -1
package/lib/memory-overlay.js
CHANGED
|
@@ -9,7 +9,7 @@ class OverlayStream extends Readable {
|
|
|
9
9
|
|
|
10
10
|
this.tip = tip
|
|
11
11
|
this.storage = storage
|
|
12
|
-
this.
|
|
12
|
+
this.options = opts
|
|
13
13
|
|
|
14
14
|
this._reverse = !!opts.reverse
|
|
15
15
|
this._active = null
|
|
@@ -18,11 +18,7 @@ class OverlayStream extends Readable {
|
|
|
18
18
|
this._createStream = createStream
|
|
19
19
|
this._pendingDestroy = null
|
|
20
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
|
-
}
|
|
21
|
+
if (!opts.reverse) this._makeActiveStream()
|
|
26
22
|
}
|
|
27
23
|
|
|
28
24
|
_predestroy () {
|
|
@@ -31,7 +27,10 @@ class OverlayStream extends Readable {
|
|
|
31
27
|
|
|
32
28
|
_read (cb) {
|
|
33
29
|
if (this._active === null) {
|
|
34
|
-
if (this._yield())
|
|
30
|
+
if (this._yield()) {
|
|
31
|
+
cb(null)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
35
34
|
}
|
|
36
35
|
|
|
37
36
|
if (this._active) {
|
|
@@ -69,13 +68,18 @@ class OverlayStream extends Readable {
|
|
|
69
68
|
return true
|
|
70
69
|
}
|
|
71
70
|
|
|
72
|
-
this.
|
|
73
|
-
this._active.on('close', this._onclose.bind(this))
|
|
74
|
-
this._active.on('data', this._ondata.bind(this))
|
|
71
|
+
this._makeActiveStream()
|
|
75
72
|
|
|
76
73
|
return false
|
|
77
74
|
}
|
|
78
75
|
|
|
76
|
+
_makeActiveStream () {
|
|
77
|
+
this._active = this._createStream(this.storage, this.options)
|
|
78
|
+
this._active.on('error', noop) // handled in onclose
|
|
79
|
+
this._active.on('close', this._onclose.bind(this))
|
|
80
|
+
this._active.on('data', this._ondata.bind(this))
|
|
81
|
+
}
|
|
82
|
+
|
|
79
83
|
_ondata (data) {
|
|
80
84
|
if (!this.push(data)) this._active.pause()
|
|
81
85
|
}
|
|
@@ -87,7 +91,10 @@ class OverlayStream extends Readable {
|
|
|
87
91
|
return
|
|
88
92
|
}
|
|
89
93
|
|
|
90
|
-
if (this._reverse)
|
|
94
|
+
if (this._reverse) {
|
|
95
|
+
this.push(null)
|
|
96
|
+
return
|
|
97
|
+
}
|
|
91
98
|
|
|
92
99
|
this._yield()
|
|
93
100
|
}
|
|
@@ -115,7 +122,27 @@ class MemoryOverlay {
|
|
|
115
122
|
}
|
|
116
123
|
|
|
117
124
|
snapshot () {
|
|
118
|
-
|
|
125
|
+
const snap = new MemoryOverlay(this.storage.snapshot())
|
|
126
|
+
snap.merge(this)
|
|
127
|
+
|
|
128
|
+
if (this.userData !== null) snap.userData = new Map()
|
|
129
|
+
if (this.blocks !== null) snap.blocks = new TipList()
|
|
130
|
+
if (this.treeNodes !== null) snap.treeNodes = new Map()
|
|
131
|
+
if (this.bitfields !== null) snap.bitfields = new TipList()
|
|
132
|
+
|
|
133
|
+
// clone state
|
|
134
|
+
if (this.head !== null) snap.head = Object.assign(this.head)
|
|
135
|
+
if (this.auth !== null) snap.auth = Object.assign(this.auth)
|
|
136
|
+
if (this.localKeyPair !== null) snap.localKeyPair = Object.assign(this.localKeyPair)
|
|
137
|
+
if (this.encryptionKey !== null) snap.encryptionKey = Object.assign(this.encryptionKey)
|
|
138
|
+
if (this.dataDependency !== null) snap.dataDependency = Object.assign(this.dataDependency)
|
|
139
|
+
if (this.dataInfo !== null) snap.dataInfo = Object.assign(this.dataInfo)
|
|
140
|
+
if (this.userData !== null) snap.userData = mergeMap(snap.userData, this.userData)
|
|
141
|
+
if (this.blocks !== null) snap.blocks = mergeTip(snap.blocks, this.blocks)
|
|
142
|
+
if (this.treeNodes !== null) snap.treeNodes = mergeMap(snap.treeNodes, this.treeNodes)
|
|
143
|
+
if (this.bitfields !== null) snap.bitfields = mergeTip(snap.bitfields, this.bitfields)
|
|
144
|
+
|
|
145
|
+
return snap
|
|
119
146
|
}
|
|
120
147
|
|
|
121
148
|
get dependencies () {
|
|
@@ -373,6 +400,8 @@ function mergeTip (a, b) {
|
|
|
373
400
|
return a
|
|
374
401
|
}
|
|
375
402
|
|
|
403
|
+
function noop () {}
|
|
404
|
+
|
|
376
405
|
function todo () {
|
|
377
406
|
throw ASSERTION('Not supported yet, but will be')
|
|
378
407
|
}
|