hypercore 10.18.1 → 10.18.3

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
@@ -258,7 +258,7 @@ module.exports = class Hypercore extends EventEmitter {
258
258
  }
259
259
 
260
260
  async _openFromExisting (from, opts) {
261
- await from.opening
261
+ if (!from.opened) await from.opening
262
262
 
263
263
  // includes ourself as well, so the loop below also updates us
264
264
  const sessions = this.sessions
@@ -279,7 +279,7 @@ module.exports = class Hypercore extends EventEmitter {
279
279
  const isFirst = !opts._opening
280
280
 
281
281
  if (!isFirst) await opts._opening
282
- if (opts.preload) opts = { ...opts, ...(await opts.preload()) }
282
+ if (opts.preload) opts = { ...opts, ...(await this._retryPreload(opts.preload)) }
283
283
 
284
284
  const keyPair = (key && opts.keyPair)
285
285
  ? { ...opts.keyPair, publicKey: key }
@@ -329,6 +329,18 @@ module.exports = class Hypercore extends EventEmitter {
329
329
  this.emit('ready')
330
330
  }
331
331
 
332
+ async _retryPreload (preload) {
333
+ while (true) { // TODO: better long term fix is allowing lib/core.js creation from the outside...
334
+ const result = await preload()
335
+ const from = result && result.from
336
+ if (from) {
337
+ if (!from.opened) await from.ready()
338
+ if (from.closing) continue
339
+ }
340
+ return result
341
+ }
342
+ }
343
+
332
344
  async _openCapabilities (keyPair, storage, opts) {
333
345
  if (opts.from) return this._openFromExisting(opts.from, opts)
334
346
 
package/lib/replicator.js CHANGED
@@ -305,6 +305,7 @@ class Peer {
305
305
 
306
306
  this.remoteOpened = false
307
307
  this.remoteBitfield = new RemoteBitfield()
308
+ this.skipList = new RemoteBitfield()
308
309
 
309
310
  this.remoteFork = 0
310
311
  this.remoteLength = 0
@@ -678,6 +679,7 @@ class Peer {
678
679
 
679
680
  onbitfield ({ start, bitfield }) {
680
681
  if (this.remoteBitfield.insert(start, bitfield)) {
682
+ this.skipList.setRange(start, bitfield.byteLength * 8, false)
681
683
  this._update()
682
684
  }
683
685
  }
@@ -687,12 +689,14 @@ class Peer {
687
689
 
688
690
  if (length === 1) {
689
691
  this.remoteBitfield.setRange(start, length, has)
692
+ this.skipList.set(start, false)
690
693
  } else {
691
694
  const rangeStart = this.remoteBitfield.findFirst(!has, start)
692
695
  const rangeLength = length - (rangeStart - start)
693
696
 
694
697
  if (rangeLength > 0) {
695
698
  this.remoteBitfield.setRange(rangeStart, rangeLength, has)
699
+ this.skipList.setRange(rangeStart, rangeLength, false)
696
700
  }
697
701
  }
698
702
 
@@ -850,17 +854,36 @@ class Peer {
850
854
  // as they are "cheaper" and will trigger an auto upgrade if possible
851
855
  // If no blocks < .length is avaible then try the "needs upgrade" range
852
856
 
853
- for (let i = 0; i < len; i++) {
857
+ let wrapped = 0
858
+
859
+ for (let i = 0; i < len && wrapped < 2; i++) {
854
860
  let index = off + i
855
861
  if (index >= end) index -= len
856
862
 
857
- if (r.blocks !== null) index = r.blocks[index]
863
+ if (r.blocks !== null) {
864
+ index = r.blocks[index]
865
+ } else { // TODO: make this loop better (something like a for loop that skips with the skip list)
866
+ index = this.skipList.findFirst(false, index)
867
+ if (index === -1 || index >= end) {
868
+ wrapped++
869
+ index = this.skipList.findFirst(false, r.start)
870
+ if (index === -1 || index >= end) {
871
+ return false
872
+ }
873
+ }
874
+ }
858
875
 
859
- if (this.remoteBitfield.get(index) === false) continue
860
- if (this.core.bitfield.get(index) === true) continue
876
+ if (this.remoteBitfield.get(index) === false || this.core.bitfield.get(index) === true) {
877
+ this.skipList.set(index, true)
878
+ continue
879
+ }
861
880
 
862
881
  const b = this.replicator._blocks.add(index, PRIORITY.NORMAL)
863
- if (b.inflight.length > 0) continue
882
+
883
+ if (b.inflight.length > 0) {
884
+ this.skipList.set(index, true)
885
+ continue
886
+ }
864
887
 
865
888
  const req = this._makeRequest(index >= length, b.priority)
866
889
 
@@ -870,6 +893,8 @@ class Peer {
870
893
  return false
871
894
  }
872
895
 
896
+ this.skipList.set(index, true)
897
+
873
898
  req.block = { index, nodes: 0 }
874
899
 
875
900
  b.inflight.push(req)
@@ -1332,6 +1357,10 @@ module.exports = class Replicator {
1332
1357
 
1333
1358
  if (b === null || removeInflight(b.inflight, req) === false) return
1334
1359
 
1360
+ if (isBlock && this.core.bitfield.get(index) === false) {
1361
+ for (const peer of this.peers) peer.skipList.set(index, false)
1362
+ }
1363
+
1335
1364
  if (b.refs.length > 0 && isBlock === true) {
1336
1365
  this._queueBlock(b)
1337
1366
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.18.1",
3
+ "version": "10.18.3",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {