hypercore 10.9.0 → 10.9.2

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/bitfield.js CHANGED
@@ -80,7 +80,7 @@ class BitfieldSegment {
80
80
  constructor (index, bitfield) {
81
81
  this.index = index
82
82
  this.offset = index * BYTES_PER_SEGMENT
83
- this.tree = quickbit.Index.from(bitfield)
83
+ this.tree = quickbit.Index.from(bitfield, BYTES_PER_SEGMENT)
84
84
  this.pages = new Array(PAGES_PER_SEGMENT)
85
85
  }
86
86
 
@@ -107,7 +107,7 @@ class BitfieldSegment {
107
107
  const bitfield = new Uint32Array(target)
108
108
  bitfield.set(this.bitfield)
109
109
 
110
- this.tree = quickbit.Index.from(bitfield)
110
+ this.tree = quickbit.Index.from(bitfield, BYTES_PER_SEGMENT)
111
111
 
112
112
  for (let i = 0; i < this.pages.length; i++) {
113
113
  const page = this.pages[i]
@@ -123,19 +123,23 @@ class BitfieldSegment {
123
123
  findFirst (val, position) {
124
124
  position = this.tree.skipFirst(!val, position)
125
125
 
126
- const j = position & (BITS_PER_PAGE - 1)
127
- const i = (position - j) / BITS_PER_PAGE
126
+ let j = position & (BITS_PER_PAGE - 1)
127
+ let i = (position - j) / BITS_PER_PAGE
128
128
 
129
129
  if (i >= PAGES_PER_SEGMENT) return -1
130
130
 
131
- const p = this.pages[i]
131
+ while (i < this.pages.length) {
132
+ const p = this.pages[i]
132
133
 
133
- if (p) {
134
- const index = p.findFirst(val, j)
134
+ let index = -1
135
135
 
136
- if (index !== -1) {
137
- return i * BITS_PER_PAGE + index
138
- }
136
+ if (p) index = p.findFirst(val, j)
137
+ else if (!val) index = j
138
+
139
+ if (index !== -1) return i * BITS_PER_PAGE + index
140
+
141
+ j = 0
142
+ i++
139
143
  }
140
144
 
141
145
  return -1
@@ -144,19 +148,23 @@ class BitfieldSegment {
144
148
  findLast (val, position) {
145
149
  position = this.tree.skipLast(!val, position)
146
150
 
147
- const j = position & (BITS_PER_PAGE - 1)
148
- const i = (position - j) / BITS_PER_PAGE
151
+ let j = position & (BITS_PER_PAGE - 1)
152
+ let i = (position - j) / BITS_PER_PAGE
149
153
 
150
154
  if (i >= PAGES_PER_SEGMENT) return -1
151
155
 
152
- const p = this.pages[i]
156
+ while (i >= 0) {
157
+ const p = this.pages[i]
153
158
 
154
- if (p) {
155
- const index = p.findLast(val, j)
159
+ let index = -1
156
160
 
157
- if (index !== -1) {
158
- return i * BITS_PER_PAGE + index
159
- }
161
+ if (p) index = p.findLast(val, j)
162
+ else if (!val) index = j
163
+
164
+ if (index !== -1) return i * BITS_PER_PAGE + index
165
+
166
+ j = BITS_PER_PAGE - 1
167
+ i--
160
168
  }
161
169
 
162
170
  return -1
@@ -276,19 +284,18 @@ module.exports = class Bitfield {
276
284
  while (i < this._segments.maxLength) {
277
285
  const s = this._segments.get(i)
278
286
 
279
- if (s) {
280
- const index = s.findFirst(val, j)
287
+ let index = -1
281
288
 
282
- if (index !== -1) {
283
- return i * BITS_PER_SEGMENT + index
284
- }
285
- }
289
+ if (s) index = s.findFirst(val, j)
290
+ else if (!val) index = j
291
+
292
+ if (index !== -1) return i * BITS_PER_SEGMENT + index
286
293
 
287
294
  j = 0
288
295
  i++
289
296
  }
290
297
 
291
- return -1
298
+ return val ? -1 : position
292
299
  }
293
300
 
294
301
  firstSet (position) {
@@ -306,13 +313,12 @@ module.exports = class Bitfield {
306
313
  while (i >= 0) {
307
314
  const s = this._segments.get(i)
308
315
 
309
- if (s) {
310
- const index = s.findLast(val, j)
316
+ let index = -1
311
317
 
312
- if (index !== -1) {
313
- return i * BITS_PER_SEGMENT + index
314
- }
315
- }
318
+ if (s) index = s.findLast(val, j)
319
+ else if (!val) index = j
320
+
321
+ if (index !== -1) return i * BITS_PER_SEGMENT + index
316
322
 
317
323
  j = BITS_PER_SEGMENT - 1
318
324
  i--
@@ -58,7 +58,7 @@ class RemoteBitfieldSegment {
58
58
  constructor (index) {
59
59
  this.index = index
60
60
  this.offset = index * BYTES_PER_SEGMENT
61
- this.tree = quickbit.Index.from([])
61
+ this.tree = quickbit.Index.from([], BYTES_PER_SEGMENT)
62
62
  this.pages = new Array(PAGES_PER_SEGMENT)
63
63
  }
64
64
 
@@ -84,19 +84,23 @@ class RemoteBitfieldSegment {
84
84
  findFirst (val, position) {
85
85
  position = this.tree.skipFirst(!val, position)
86
86
 
87
- const j = position & (BITS_PER_PAGE - 1)
88
- const i = (position - j) / BITS_PER_PAGE
87
+ let j = position & (BITS_PER_PAGE - 1)
88
+ let i = (position - j) / BITS_PER_PAGE
89
89
 
90
90
  if (i >= PAGES_PER_SEGMENT) return -1
91
91
 
92
- const p = this.pages[i]
92
+ while (i < this.pages.length) {
93
+ const p = this.pages[i]
93
94
 
94
- if (p) {
95
- const index = p.findFirst(val, j)
95
+ let index = -1
96
96
 
97
- if (index !== -1) {
98
- return i * BITS_PER_PAGE + index
99
- }
97
+ if (p) index = p.findFirst(val, j)
98
+ else if (!val) index = j
99
+
100
+ if (index !== -1) return i * BITS_PER_PAGE + index
101
+
102
+ j = 0
103
+ i++
100
104
  }
101
105
 
102
106
  return -1
@@ -105,19 +109,23 @@ class RemoteBitfieldSegment {
105
109
  findLast (val, position) {
106
110
  position = this.tree.skipLast(!val, position)
107
111
 
108
- const j = position & (BITS_PER_PAGE - 1)
109
- const i = (position - j) / BITS_PER_PAGE
112
+ let j = position & (BITS_PER_PAGE - 1)
113
+ let i = (position - j) / BITS_PER_PAGE
110
114
 
111
115
  if (i >= PAGES_PER_SEGMENT) return -1
112
116
 
113
- const p = this.pages[i]
117
+ while (i >= 0) {
118
+ const p = this.pages[i]
119
+
120
+ let index = -1
114
121
 
115
- if (p) {
116
- const index = p.findLast(val, j)
122
+ if (p) index = p.findLast(val, j)
123
+ else if (!val) index = j
117
124
 
118
- if (index !== -1) {
119
- return i * BITS_PER_PAGE + index
120
- }
125
+ if (index !== -1) return i * BITS_PER_PAGE + index
126
+
127
+ j = BITS_PER_PAGE - 1
128
+ i--
121
129
  }
122
130
 
123
131
  return -1
@@ -187,19 +195,18 @@ module.exports = class RemoteBitfield {
187
195
  while (i < this._segments.maxLength) {
188
196
  const s = this._segments.get(i)
189
197
 
190
- if (s) {
191
- const index = s.findFirst(val, j)
198
+ let index = -1
192
199
 
193
- if (index !== -1) {
194
- return i * BITS_PER_SEGMENT + index
195
- }
196
- }
200
+ if (s) index = s.findFirst(val, j)
201
+ else if (!val) index = j
202
+
203
+ if (index !== -1) return i * BITS_PER_SEGMENT + index
197
204
 
198
205
  j = 0
199
206
  i++
200
207
  }
201
208
 
202
- return -1
209
+ return val ? -1 : position
203
210
  }
204
211
 
205
212
  firstSet (position) {
@@ -217,13 +224,12 @@ module.exports = class RemoteBitfield {
217
224
  while (i >= 0) {
218
225
  const s = this._segments.get(i)
219
226
 
220
- if (s) {
221
- const index = s.findLast(val, j)
227
+ let index = -1
222
228
 
223
- if (index !== -1) {
224
- return i * BITS_PER_SEGMENT + index
225
- }
226
- }
229
+ if (s) index = s.findLast(val, j)
230
+ else if (!val) index = j
231
+
232
+ if (index !== -1) return i * BITS_PER_SEGMENT + index
227
233
 
228
234
  j = BITS_PER_SEGMENT - 1
229
235
  i--
package/lib/replicator.js CHANGED
@@ -1673,17 +1673,15 @@ function clampRange (core, r) {
1673
1673
  if (r.blocks === null) {
1674
1674
  const start = core.bitfield.firstUnset(r.start)
1675
1675
 
1676
- if (r.end === -1) {
1677
- r.start = start === -1 ? core.tree.length : start
1678
- } else {
1679
- const end = core.bitfield.lastUnset(r.end - 1) + 1
1680
-
1681
- if (start === -1) {
1682
- r.start = r.end
1683
- } else {
1684
- r.start = start
1685
- r.end = end
1686
- }
1676
+ if (r.end === -1) r.start = start === -1 ? core.tree.length : start
1677
+ else if (start === -1 || start >= r.end) r.start = r.end
1678
+ else {
1679
+ r.start = start
1680
+
1681
+ const end = core.bitfield.lastUnset(r.end - 1)
1682
+
1683
+ if (end === -1 || start >= end + 1) r.end = r.start
1684
+ else r.end = end + 1
1687
1685
  }
1688
1686
  } else {
1689
1687
  while (r.start < r.end && core.bitfield.get(r.blocks[r.start])) r.start++
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.9.0",
3
+ "version": "10.9.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -43,7 +43,7 @@
43
43
  "hypercore-crypto": "^3.2.1",
44
44
  "is-options": "^1.0.1",
45
45
  "protomux": "^3.4.0",
46
- "quickbit-universal": "^2.0.3",
46
+ "quickbit-universal": "^2.1.1",
47
47
  "random-access-file": "^4.0.0",
48
48
  "random-array-iterator": "^1.0.0",
49
49
  "safety-catch": "^1.0.1",