hypercore 11.36.1 → 11.37.1

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/messages.js CHANGED
@@ -215,7 +215,7 @@ const manifest = (exports.manifest = {
215
215
  const version = c.uint.decode(state)
216
216
 
217
217
  if (version === 0) return manifestv0.decode(state)
218
- if (version > 2) throw new Error('Unknown version: ' + version)
218
+ if (version > 3) throw new Error('Unknown version: ' + version)
219
219
 
220
220
  const flags = c.uint.decode(state)
221
221
  const hash = hashes.decode(state)
@@ -153,6 +153,7 @@ module.exports = class RemoteBitfield {
153
153
  this._pages = new BigSparseArray()
154
154
  this._segments = new BigSparseArray()
155
155
  this._maxSegments = 0
156
+ this._maxPages = 0
156
157
  }
157
158
 
158
159
  getBitfield(index) {
@@ -182,6 +183,7 @@ module.exports = class RemoteBitfield {
182
183
  const k = Math.floor(i / PAGES_PER_SEGMENT)
183
184
  const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
184
185
  if (this._maxSegments <= k) this._maxSegments = k + 1
186
+ if (this._maxPages <= i) this._maxPages = i + 1
185
187
 
186
188
  p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
187
189
  }
@@ -193,6 +195,9 @@ module.exports = class RemoteBitfield {
193
195
  let j = start & (BITS_PER_PAGE - 1)
194
196
  let i = (start - j) / BITS_PER_PAGE
195
197
 
198
+ // Cap end if val = false
199
+ if (!val) end = Math.min(end, this._maxPages * BITS_PER_PAGE)
200
+
196
201
  while (start < end) {
197
202
  let p = this._pages.get(i)
198
203
 
@@ -200,6 +205,7 @@ module.exports = class RemoteBitfield {
200
205
  const k = Math.floor(i / PAGES_PER_SEGMENT)
201
206
  const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
202
207
  if (this._maxSegments <= k) this._maxSegments = k + 1
208
+ if (this._maxPages <= i) this._maxPages = i + 1
203
209
 
204
210
  p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
205
211
  }
@@ -251,6 +257,8 @@ module.exports = class RemoteBitfield {
251
257
  let j = position & (BITS_PER_SEGMENT - 1)
252
258
  let i = (position - j) / BITS_PER_SEGMENT
253
259
 
260
+ if (i >= this._maxSegments) return -1
261
+
254
262
  while (i >= 0) {
255
263
  const s = this._segments.get(i)
256
264
 
@@ -291,6 +299,7 @@ module.exports = class RemoteBitfield {
291
299
  const k = Math.floor(i / PAGES_PER_SEGMENT)
292
300
  const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
293
301
  if (this._maxSegments <= k) this._maxSegments = k + 1
302
+ if (this._maxPages <= i) this._maxPages = i + 1
294
303
 
295
304
  p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
296
305
  }
@@ -319,20 +328,11 @@ module.exports = class RemoteBitfield {
319
328
  let i = (start - j) / BITS_PER_PAGE
320
329
 
321
330
  while (length > 0) {
322
- let p = this._pages.get(i)
323
-
324
- if (!p) {
325
- const k = Math.floor(i / PAGES_PER_SEGMENT)
326
- const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
327
- if (this._maxSegments <= k) this._maxSegments = k + 1
328
-
329
- p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
330
- }
331
-
331
+ const p = this._pages.get(i)
332
332
  const end = Math.min(j + length, BITS_PER_PAGE)
333
333
  const range = end - j
334
334
 
335
- p.clear(j, bitfield.subarray(0, range / 32))
335
+ if (p) p.clear(j, bitfield.subarray(0, range / 32))
336
336
 
337
337
  bitfield = bitfield.subarray(range / 32)
338
338
 
package/lib/replicator.js CHANGED
@@ -978,8 +978,7 @@ class Peer {
978
978
  this.remoteOpened &&
979
979
  this.protomux.drained &&
980
980
  this.receiverQueue.length > 0 &&
981
- !this.removed &&
982
- this.core.closing === null
981
+ !this.removed
983
982
  ) {
984
983
  const msg = this.receiverQueue.shift()
985
984
  await this._handleRequest(msg)
@@ -990,7 +989,7 @@ class Peer {
990
989
  }
991
990
 
992
991
  async push(index) {
993
- if (this.core.closing !== null) return
992
+ if (this.destroyed) return
994
993
  if (!this.remoteAllowPush) return
995
994
  if (!this.remoteDownloading) return
996
995
  if (this.core.state.length <= index) return
@@ -1271,7 +1270,7 @@ class Peer {
1271
1270
  return
1272
1271
  }
1273
1272
 
1274
- if ((this.core.closing !== null || this.core.closed) && !isCriticalError(err)) return
1273
+ if (this.core.closed && !isCriticalError(err)) return
1275
1274
 
1276
1275
  if (err.code !== 'INVALID_OPERATION') {
1277
1276
  // might be a fork, verify
@@ -1407,8 +1406,6 @@ class Peer {
1407
1406
  }
1408
1407
 
1409
1408
  async onrange({ drop, start, length }) {
1410
- if (this.core.closing !== null) return
1411
-
1412
1409
  const has = drop === false
1413
1410
 
1414
1411
  if (drop === true && start < this._remoteContiguousLength) {
@@ -1960,8 +1957,8 @@ class Peer {
1960
1957
  }
1961
1958
  }
1962
1959
  } catch (err) {
1963
- if (this.core.closing !== null) return
1964
- this.stream.destroy(err)
1960
+ if (!this.removed) this.stream.destroy(err)
1961
+
1965
1962
  return
1966
1963
  }
1967
1964
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.36.1",
3
+ "version": "11.37.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {