hypercore-storage 1.1.4 → 1.1.6

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
@@ -86,29 +86,42 @@ class HypercoreStorage {
86
86
  }
87
87
 
88
88
  // TODO: this might have to be async if the dependents have changed, but prop ok for now
89
- updateDependencyLength (length) {
89
+ updateDependencyLength (length, truncated) {
90
90
  const deps = this.core.dependencies
91
91
 
92
- for (let i = deps.length - 1; i >= 0; i--) {
93
- if (deps[i].length > length) continue
92
+ const i = this.findDependencyIndex(length, truncated)
93
+ if (i === -1) throw new Error('Dependency not found')
94
94
 
95
- this.core = {
96
- corePointer: this.core.corePointer,
97
- dataPointer: this.core.dataPointer,
98
- dependencies: deps.slice(0, i + 1)
95
+ this.core = {
96
+ corePointer: this.core.corePointer,
97
+ dataPointer: this.core.dataPointer,
98
+ dependencies: deps.slice(0, i + 1)
99
+ }
100
+
101
+ if (this.core.dependencies[i].length !== length) {
102
+ this.core.dependencies[i] = {
103
+ dataPointer: deps[i].dataPointer,
104
+ length
99
105
  }
106
+ }
107
+ }
100
108
 
101
- if (this.core.dependencies[i].length !== length) {
102
- this.core.dependencies[i] = {
103
- dataPointer: deps[i].dataPointer,
104
- length
105
- }
109
+ findDependencyIndex (length, truncated) {
110
+ const deps = this.core.dependencies
111
+
112
+ if (truncated) {
113
+ for (let i = 0; i < deps.length; i++) {
114
+ if (deps[i].length >= length) return i
106
115
  }
107
116
 
108
- return
117
+ return -1
118
+ }
119
+
120
+ for (let i = deps.length - 1; i >= 0; i--) {
121
+ if (deps[i].length <= length) return i
109
122
  }
110
123
 
111
- throw new Error('Dependency not found')
124
+ return -1
112
125
  }
113
126
 
114
127
  get snapshotted () {
@@ -0,0 +1,14 @@
1
+ const { Readable } = require('streamx')
2
+
3
+ // used for returned a stream that just errors (during read during teardown)
4
+
5
+ module.exports = class CloseErrorStream extends Readable {
6
+ constructor (err) {
7
+ super()
8
+ this.error = err
9
+ }
10
+
11
+ _open (cb) {
12
+ cb(this.error)
13
+ }
14
+ }
package/lib/view.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { Readable, getStreamError } = require('streamx')
2
+ const CloseErrorStream = require('./close-error-stream.js')
2
3
  const b4a = require('b4a')
3
4
 
4
5
  class OverlayStream extends Readable {
@@ -212,6 +213,8 @@ class View {
212
213
  }
213
214
 
214
215
  iterator (db, start, end, reverse) {
216
+ if (dbClosing(db)) return new CloseErrorStream(new Error('RocksDB session is closed'))
217
+
215
218
  const stream = db.iterator({ gte: start, lt: end, reverse })
216
219
  if (this.changes === null) return stream
217
220
 
@@ -344,3 +347,8 @@ function reverseArray (list) {
344
347
  for (let i = 0; i < list.length; i++) r[r.length - 1 - i] = list[i]
345
348
  return r
346
349
  }
350
+
351
+ // TODO: expose from rocks instead
352
+ function dbClosing (db) {
353
+ return db._state.closing || db._index === -1
354
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",