hypercore 10.37.21 → 10.37.22

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.
Files changed (2) hide show
  1. package/lib/replicator.js +37 -9
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -537,9 +537,11 @@ class Peer {
537
537
  const reopen = isRemote === true && this.remoteOpened === true && this.remoteDownloading === false &&
538
538
  this.remoteUploading === true && this.replicator.downloading === true
539
539
 
540
- if (this.useSession && !reopen) this.replicator._closeSession()
541
-
542
540
  if (this.remoteOpened === false) {
541
+ if (this.useSession) {
542
+ this.replicator._peerSessions--
543
+ this.replicator._closeSessionMaybe()
544
+ }
543
545
  this.replicator._ifAvailable--
544
546
  this.replicator.updateAll()
545
547
  return
@@ -559,6 +561,11 @@ class Peer {
559
561
  if (reopen) {
560
562
  this.replicator._makePeer(this.protomux, this.useSession)
561
563
  }
564
+
565
+ if (this.useSession) {
566
+ this.replicator._peerSessions--
567
+ this.replicator._closeSessionMaybe()
568
+ }
562
569
  }
563
570
 
564
571
  closeIfIdle () {
@@ -1456,6 +1463,8 @@ module.exports = class Replicator {
1456
1463
  this._updatesPending = 0
1457
1464
  this._applyingReorg = null
1458
1465
  this._manifestPeer = null
1466
+ this._hasSession = false
1467
+ this._peerSessions = 0
1459
1468
  this._notDownloadingLinger = notDownloadingLinger
1460
1469
  this._downloadingTimer = null
1461
1470
 
@@ -1771,6 +1780,8 @@ module.exports = class Replicator {
1771
1780
  this._clearRequest(peer, req)
1772
1781
  }
1773
1782
 
1783
+ if (peer.useSession) this._closeSessionMaybe()
1784
+
1774
1785
  this.onpeerupdate(false, peer)
1775
1786
  this.updateAll()
1776
1787
  }
@@ -2243,8 +2254,11 @@ module.exports = class Replicator {
2243
2254
  this._maybeResolveIfAvailableRanges()
2244
2255
  }
2245
2256
 
2246
- _closeSession () {
2247
- this.core.active--
2257
+ _closeSessionMaybe () {
2258
+ if (this._hasSession && this._peerSessions === 0) {
2259
+ this._hasSession = false
2260
+ this.core.active--
2261
+ }
2248
2262
 
2249
2263
  // we were the last active ref, so lets shut things down
2250
2264
  if (this.core.active === 0 && this.core.sessions.length === 0) {
@@ -2263,10 +2277,15 @@ module.exports = class Replicator {
2263
2277
  return this._attached.has(protomux)
2264
2278
  }
2265
2279
 
2280
+ ensureSession () {
2281
+ if (this._hasSession) return
2282
+ this._hasSession = true
2283
+ this.core.active++
2284
+ }
2285
+
2266
2286
  attachTo (protomux, useSession) {
2267
- if (useSession) {
2268
- this.core.active++
2269
- }
2287
+ if (this.core.closed) return
2288
+ if (useSession) this.ensureSession()
2270
2289
 
2271
2290
  const makePeer = this._makePeer.bind(this, protomux, useSession)
2272
2291
 
@@ -2275,12 +2294,15 @@ module.exports = class Replicator {
2275
2294
  protomux.stream.setMaxListeners(0)
2276
2295
  protomux.stream.on('close', this._onstreamclose)
2277
2296
 
2297
+ if (useSession) this._peerSessions++
2278
2298
  this._ifAvailable++
2299
+
2279
2300
  protomux.stream.opened.then((opened) => {
2301
+ if (useSession) this._peerSessions--
2280
2302
  this._ifAvailable--
2281
2303
 
2282
2304
  if (opened && !this.destroyed) makePeer()
2283
- else if (useSession) this._closeSession()
2305
+ else if (useSession) this._closeSessionMaybe()
2284
2306
  this._checkUpgradeIfAvailable()
2285
2307
  })
2286
2308
  }
@@ -2339,6 +2361,12 @@ module.exports = class Replicator {
2339
2361
  const peer = new Peer(replicator, protomux, channel, useSession, this.inflightRange)
2340
2362
  const stream = protomux.stream
2341
2363
 
2364
+ if (useSession) {
2365
+ // session may have been unref'd underneath us
2366
+ replicator.ensureSession()
2367
+ replicator._peerSessions++
2368
+ }
2369
+
2342
2370
  peer.channel.open({
2343
2371
  seeks: true,
2344
2372
  capability: caps.replicate(stream.isInitiator, this.key, stream.handshakeHash)
@@ -2347,7 +2375,7 @@ module.exports = class Replicator {
2347
2375
  return true
2348
2376
 
2349
2377
  function onnochannel () {
2350
- if (useSession) replicator._closeSession()
2378
+ if (useSession) replicator._closeSessionMaybe()
2351
2379
  return false
2352
2380
  }
2353
2381
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.37.21",
3
+ "version": "10.37.22",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {