hypercore 10.31.6 → 10.31.8

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
@@ -389,6 +389,7 @@ module.exports = class Hypercore extends EventEmitter {
389
389
 
390
390
  this.replicator = new Replicator(this.core, this.key, {
391
391
  eagerUpgrade: true,
392
+ notDownloadingLinger: opts.notDownloadingLinger,
392
393
  allowFork: opts.allowFork !== false,
393
394
  onpeerupdate: this._onpeerupdate.bind(this),
394
395
  onupload: this._onupload.bind(this),
package/lib/bitfield.js CHANGED
@@ -43,7 +43,7 @@ class BitfieldPage {
43
43
  let i = Math.floor(start / 128)
44
44
  const n = i + Math.ceil(length / 128)
45
45
 
46
- while (i < n) this.tree.update(this.offset * 8 + i++ * 128)
46
+ while (i <= n) this.tree.update(this.offset * 8 + i++ * 128)
47
47
  }
48
48
 
49
49
  findFirst (val, position) {
@@ -38,7 +38,7 @@ class RemoteBitfieldPage {
38
38
  let i = Math.floor(start / 128)
39
39
  const n = i + Math.ceil(length / 128)
40
40
 
41
- while (i < n) this.tree.update(this.offset * 8 + i++ * 128)
41
+ while (i <= n) this.tree.update(this.offset * 8 + i++ * 128)
42
42
  }
43
43
 
44
44
  findFirst (val, position) {
@@ -65,6 +65,7 @@ class RemoteBitfieldSegment {
65
65
  this.offset = index * BYTES_PER_SEGMENT
66
66
  this.tree = quickbit.Index.from([], BYTES_PER_SEGMENT)
67
67
  this.pages = new Array(PAGES_PER_SEGMENT)
68
+ this.pagesLength = 0
68
69
  }
69
70
 
70
71
  get chunks () {
@@ -76,7 +77,10 @@ class RemoteBitfieldSegment {
76
77
  }
77
78
 
78
79
  add (page) {
79
- this.pages[page.index - this.index * PAGES_PER_SEGMENT] = page
80
+ const pageIndex = page.index - this.index * PAGES_PER_SEGMENT
81
+ if (pageIndex >= this.pagesLength) this.pagesLength = pageIndex + 1
82
+
83
+ this.pages[pageIndex] = page
80
84
 
81
85
  const chunk = { field: page.bitfield, offset: page.offset }
82
86
 
@@ -98,7 +102,7 @@ class RemoteBitfieldSegment {
98
102
 
99
103
  if (i >= PAGES_PER_SEGMENT) return -1
100
104
 
101
- while (i < this.pages.length) {
105
+ while (i < this.pagesLength) {
102
106
  const p = this.pages[i]
103
107
 
104
108
  let index = -1
package/lib/replicator.js CHANGED
@@ -11,6 +11,7 @@ const caps = require('./caps')
11
11
  const DEFAULT_MAX_INFLIGHT = [32, 512]
12
12
  const SCALE_LATENCY = 50
13
13
  const DEFAULT_SEGMENT_SIZE = 256 * 1024 * 8 // 256 KiB in bits
14
+ const NOT_DOWNLOADING_SLACK = 4000 + (Math.random() * 4000) | 0
14
15
 
15
16
  const PRIORITY = {
16
17
  NORMAL: 0,
@@ -743,20 +744,23 @@ class Peer {
743
744
 
744
745
  _clearLocalRange (start, length) {
745
746
  if (length === 1) {
746
- this.missingBlocks.set(start, false)
747
+ this.missingBlocks.set(start, this.remoteBitfield.get(start) && !this.core.bitfield.get(start))
747
748
  return
748
749
  }
749
750
 
750
751
  const contig = Math.min(this.core.tree.length, this.core.header.hints.contiguousLength)
751
752
 
752
- if (start < contig) {
753
+ if (start + length < contig) {
753
754
  const delta = contig - start
754
- this.missingBlocks.setRange(start, delta)
755
- start = contig
756
- length -= delta
755
+ this.missingBlocks.setRange(start, delta, false)
756
+ return
757
757
  }
758
758
 
759
- if ((start & 31) > 0) start -= (start & 31)
759
+ const rem = start & 32767
760
+ if (rem > 0) {
761
+ start -= rem
762
+ length += rem
763
+ }
760
764
 
761
765
  const end = start + Math.min(length, this.core.tree.length)
762
766
  while (start < end) {
@@ -772,11 +776,17 @@ class Peer {
772
776
 
773
777
  _unclearLocalRange (start, length) {
774
778
  if (length === 1) {
775
- this.missingBlocks.set(start, this.remoteBitfield.get(start))
779
+ this.missingBlocks.set(start, this.remoteBitfield.get(start) && !this.core.bitfield.get(start))
776
780
  return
777
781
  }
778
782
 
779
- if ((start & 31) > 0) start -= (start & 31)
783
+ const rem = start & 2097151
784
+ if (rem > 0) {
785
+ start -= rem
786
+ length += rem
787
+ }
788
+
789
+ const fixedStart = start
780
790
 
781
791
  const end = start + Math.min(length, this.remoteLength)
782
792
  while (start < end) {
@@ -788,14 +798,14 @@ class Peer {
788
798
  start += 2097152
789
799
  }
790
800
 
791
- this._clearLocalRange(start, length)
801
+ this._clearLocalRange(fixedStart, length)
792
802
  }
793
803
 
794
804
  onrange ({ drop, start, length }) {
795
805
  const has = drop === false
796
806
 
797
807
  if (length === 1) {
798
- this.remoteBitfield.setRange(start, length, has)
808
+ this.remoteBitfield.set(start, has)
799
809
  this.missingBlocks.set(start, has && !this.core.bitfield.get(start))
800
810
  } else {
801
811
  const rangeStart = this.remoteBitfield.findFirst(!has, start)
@@ -1027,12 +1037,14 @@ class Peer {
1027
1037
  if (r.blocks) {
1028
1038
  let min = -1
1029
1039
  let max = -1
1040
+
1030
1041
  for (let i = r.start; i < r.end; i++) {
1031
1042
  const index = r.blocks[i]
1032
1043
  if (min === -1 || index < min) min = index
1033
1044
  if (max === -1 || index > max) max = index
1034
1045
  if (this.missingBlocks.get(index) === true && this._requestRangeBlock(index, length)) return true
1035
1046
  }
1047
+
1036
1048
  if (min > -1) this._maybeWant(min, max - min)
1037
1049
  return false
1038
1050
  }
@@ -1047,7 +1059,6 @@ class Peer {
1047
1059
 
1048
1060
  while (true) {
1049
1061
  i = this.missingBlocks.findFirst(true, i)
1050
-
1051
1062
  if (i === -1 || i >= end) break
1052
1063
 
1053
1064
  if (this._requestRangeBlock(i, length)) return true
@@ -1061,7 +1072,7 @@ class Peer {
1061
1072
 
1062
1073
  if (i === -1 || i >= off) break
1063
1074
 
1064
- if (this.core.bitfield.get(i) === false && this._hasTreeParent(i) && this._requestRangeBlock(i, length)) return true
1075
+ if (this._requestRangeBlock(i, length)) return true
1065
1076
  i++
1066
1077
  }
1067
1078
 
@@ -1165,7 +1176,14 @@ class Peer {
1165
1176
  }
1166
1177
 
1167
1178
  module.exports = class Replicator {
1168
- constructor (core, key, { eagerUpgrade = true, allowFork = true, onpeerupdate = noop, onupload = noop, oninvalid = noop } = {}) {
1179
+ constructor (core, key, {
1180
+ notDownloadingLinger = NOT_DOWNLOADING_SLACK,
1181
+ eagerUpgrade = true,
1182
+ allowFork = true,
1183
+ onpeerupdate = noop,
1184
+ onupload = noop,
1185
+ oninvalid = noop
1186
+ } = {}) {
1169
1187
  this.key = key
1170
1188
  this.discoveryKey = core.crypto.discoveryKey(key)
1171
1189
  this.core = core
@@ -1198,6 +1216,8 @@ module.exports = class Replicator {
1198
1216
  this._updatesPending = 0
1199
1217
  this._applyingReorg = null
1200
1218
  this._manifestPeer = null
1219
+ this._notDownloadingLinger = notDownloadingLinger
1220
+ this._downloadingTimer = null
1201
1221
 
1202
1222
  const self = this
1203
1223
  this._onstreamclose = onstreamclose
@@ -1217,6 +1237,19 @@ module.exports = class Replicator {
1217
1237
  }
1218
1238
 
1219
1239
  setDownloading (downloading, session) {
1240
+ clearTimeout(this._downloadingTimer)
1241
+
1242
+ if (this.destroyed) return
1243
+ if (downloading || this._notDownloadingLinger === 0) {
1244
+ this.setDownloadingNow(downloading, session)
1245
+ return
1246
+ }
1247
+
1248
+ this._downloadingTimer = setTimeout(setDownloadingLater, this._notDownloadingLinger, this, downloading, session)
1249
+ }
1250
+
1251
+ setDownloadingNow (downloading, session) {
1252
+ this._downloadingTimer = null
1220
1253
  if (this.downloading === downloading) return
1221
1254
  this.downloading = downloading
1222
1255
  if (!downloading && this.isDownloading()) return
@@ -1973,6 +2006,10 @@ module.exports = class Replicator {
1973
2006
 
1974
2007
  destroy () {
1975
2008
  this.destroyed = true
2009
+ if (this._downloadingTimer) {
2010
+ clearTimeout(this._downloadingTimer)
2011
+ this._downloadingTimer = null
2012
+ }
1976
2013
  for (const peer of this.peers) {
1977
2014
  this.detachFrom(peer.protomux)
1978
2015
  peer.channel.close()
@@ -2137,3 +2174,7 @@ function onwirerange (m, c) {
2137
2174
  function onwireextension (m, c) {
2138
2175
  return c.userData.onextension(m)
2139
2176
  }
2177
+
2178
+ function setDownloadingLater (repl, downloading, session) {
2179
+ repl.setDownloadingNow(downloading, session)
2180
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.31.6",
3
+ "version": "10.31.8",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {