hypercore 10.24.6 → 10.24.7

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
@@ -85,6 +85,7 @@ module.exports = class Hypercore extends EventEmitter {
85
85
  this._snapshot = null
86
86
  this._batch = opts._batch || null
87
87
  this._findingPeers = 0
88
+ this._active = opts.active !== false
88
89
 
89
90
  this.opening = this._openSession(key, storage, opts)
90
91
  this.opening.catch(noop)
@@ -323,6 +324,8 @@ module.exports = class Hypercore extends EventEmitter {
323
324
  // It's required so that corestore can load a name from userData before 'ready' is emitted.
324
325
  if (opts._preready) await opts._preready(this)
325
326
 
327
+ this.replicator.updateActivity(this._active ? 1 : 0, this)
328
+
326
329
  this.opened = true
327
330
  this.emit('ready')
328
331
  }
@@ -441,6 +444,7 @@ module.exports = class Hypercore extends EventEmitter {
441
444
  if (this.replicator !== null) {
442
445
  this.replicator.findingPeers -= this._findingPeers
443
446
  this.replicator.clearRequests(this.activeRequests, err)
447
+ this.replicator.updateActivity(this._active ? -1 : 0, this)
444
448
  }
445
449
 
446
450
  this._findingPeers = 0
@@ -530,7 +534,7 @@ module.exports = class Hypercore extends EventEmitter {
530
534
  _attachToMuxerOpened (mux, useSession) {
531
535
  // If the user wants to, we can make this replication run in a session
532
536
  // that way the core wont close "under them" during replication
533
- const session = useSession ? this.session() : null
537
+ const session = useSession ? this.session({ active: false }) : null
534
538
  this.replicator.attachTo(mux, session)
535
539
  }
536
540
 
package/lib/replicator.js CHANGED
@@ -1046,7 +1046,7 @@ module.exports = class Replicator {
1046
1046
  this.findingPeers = 0 // updateable from the outside
1047
1047
  this.destroyed = false
1048
1048
  this.downloading = true
1049
- this.sessions = 0
1049
+ this.activeSessions = 0
1050
1050
 
1051
1051
  this._attached = new Set()
1052
1052
  this._inflight = new InflightTracker()
@@ -1074,15 +1074,29 @@ module.exports = class Replicator {
1074
1074
  }
1075
1075
  }
1076
1076
 
1077
+ updateActivity (inc, session) {
1078
+ this.activeSessions += inc
1079
+ this.setDownloading(this.activeSessions !== 0, session)
1080
+ }
1081
+
1077
1082
  isDownloading () {
1078
1083
  return this.downloading || !this._inflight.idle
1079
1084
  }
1080
1085
 
1081
- setDownloading (downloading) {
1086
+ setDownloading (downloading, session) {
1082
1087
  if (this.downloading === downloading) return
1083
1088
  this.downloading = downloading
1084
1089
  if (!downloading && this.isDownloading()) return
1090
+
1085
1091
  for (const peer of this.peers) peer.signalUpgrade()
1092
+
1093
+ if (downloading) { // restart channel if needed...
1094
+ for (const protomux of this._attached) {
1095
+ if (!protomux.stream.handshakeHash) continue
1096
+ if (protomux.opened({ protocol: 'hypercore/alpha', id: this.discoveryKey })) continue
1097
+ this._makePeer(protomux, session && session.session())
1098
+ }
1099
+ }
1086
1100
  }
1087
1101
 
1088
1102
  cork () {
@@ -1779,12 +1793,10 @@ module.exports = class Replicator {
1779
1793
  }
1780
1794
 
1781
1795
  _closeSession (session) {
1782
- if (!session.closing) this.sessions--
1783
1796
  session.close().catch(noop)
1784
1797
  }
1785
1798
 
1786
1799
  attachTo (protomux, session) {
1787
- if (session) this.sessions++
1788
1800
  const makePeer = this._makePeer.bind(this, protomux, session)
1789
1801
 
1790
1802
  this._attached.add(protomux)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.24.6",
3
+ "version": "10.24.7",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {