hypercore 10.31.7 → 10.31.9

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),
@@ -149,6 +149,7 @@ module.exports = class RemoteBitfield {
149
149
  constructor () {
150
150
  this._pages = new BigSparseArray()
151
151
  this._segments = new BigSparseArray()
152
+ this._maxSegments = 0
152
153
  }
153
154
 
154
155
  getBitfield (index) {
@@ -177,6 +178,7 @@ module.exports = class RemoteBitfield {
177
178
  if (!p && val) {
178
179
  const k = Math.floor(i / PAGES_PER_SEGMENT)
179
180
  const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
181
+ if (this._maxSegments <= k) this._maxSegments = k + 1
180
182
 
181
183
  p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
182
184
  }
@@ -194,6 +196,7 @@ module.exports = class RemoteBitfield {
194
196
  if (!p && val) {
195
197
  const k = Math.floor(i / PAGES_PER_SEGMENT)
196
198
  const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
199
+ if (this._maxSegments <= k) this._maxSegments = k + 1
197
200
 
198
201
  p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
199
202
  }
@@ -213,7 +216,7 @@ module.exports = class RemoteBitfield {
213
216
  let j = position & (BITS_PER_SEGMENT - 1)
214
217
  let i = (position - j) / BITS_PER_SEGMENT
215
218
 
216
- while (i < this._segments.maxLength) {
219
+ while (i < this._maxSegments) {
217
220
  const s = this._segments.get(i)
218
221
 
219
222
  let index = -1
@@ -281,6 +284,7 @@ module.exports = class RemoteBitfield {
281
284
  if (!p) {
282
285
  const k = Math.floor(i / PAGES_PER_SEGMENT)
283
286
  const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
287
+ if (this._maxSegments <= k) this._maxSegments = k + 1
284
288
 
285
289
  p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
286
290
  }
@@ -314,6 +318,7 @@ module.exports = class RemoteBitfield {
314
318
  if (!p) {
315
319
  const k = Math.floor(i / PAGES_PER_SEGMENT)
316
320
  const s = this._segments.get(k) || this._segments.set(k, new RemoteBitfieldSegment(k))
321
+ if (this._maxSegments <= k) this._maxSegments = k + 1
317
322
 
318
323
  p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
319
324
  }
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,
@@ -1175,7 +1176,14 @@ class Peer {
1175
1176
  }
1176
1177
 
1177
1178
  module.exports = class Replicator {
1178
- 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
+ } = {}) {
1179
1187
  this.key = key
1180
1188
  this.discoveryKey = core.crypto.discoveryKey(key)
1181
1189
  this.core = core
@@ -1208,6 +1216,8 @@ module.exports = class Replicator {
1208
1216
  this._updatesPending = 0
1209
1217
  this._applyingReorg = null
1210
1218
  this._manifestPeer = null
1219
+ this._notDownloadingLinger = notDownloadingLinger
1220
+ this._downloadingTimer = null
1211
1221
 
1212
1222
  const self = this
1213
1223
  this._onstreamclose = onstreamclose
@@ -1227,6 +1237,19 @@ module.exports = class Replicator {
1227
1237
  }
1228
1238
 
1229
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
1230
1253
  if (this.downloading === downloading) return
1231
1254
  this.downloading = downloading
1232
1255
  if (!downloading && this.isDownloading()) return
@@ -1983,6 +2006,10 @@ module.exports = class Replicator {
1983
2006
 
1984
2007
  destroy () {
1985
2008
  this.destroyed = true
2009
+ if (this._downloadingTimer) {
2010
+ clearTimeout(this._downloadingTimer)
2011
+ this._downloadingTimer = null
2012
+ }
1986
2013
  for (const peer of this.peers) {
1987
2014
  this.detachFrom(peer.protomux)
1988
2015
  peer.channel.close()
@@ -2147,3 +2174,7 @@ function onwirerange (m, c) {
2147
2174
  function onwireextension (m, c) {
2148
2175
  return c.userData.onextension(m)
2149
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.7",
3
+ "version": "10.31.9",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {