hypercore 10.31.7 → 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/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.8",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {