blind-peer 2.9.0 → 2.9.1

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/index.js +22 -15
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -96,8 +96,9 @@ class CoreTracker {
96
96
 
97
97
  announceToReferrer() {
98
98
  if (!this.record || !this.record.referrer) return
99
- if (!this.referrerDiscoveryKey)
99
+ if (!this.referrerDiscoveryKey) {
100
100
  this.referrerDiscoveryKey = crypto.discoveryKey(this.record.referrer)
101
+ }
101
102
 
102
103
  const sessions = this.blindPeer.wakeup.getSessions(null, {
103
104
  discoveryKey: this.referrerDiscoveryKey
@@ -151,6 +152,10 @@ class WakeupHandler {
151
152
  // do nothing
152
153
  }
153
154
  }
155
+
156
+ onpeerinactive(peer, session) {
157
+ if (session.peers.length === 0) session.destroy()
158
+ }
154
159
  }
155
160
 
156
161
  class BlindPeer extends ReadyResource {
@@ -228,8 +233,9 @@ class BlindPeer extends ReadyResource {
228
233
 
229
234
  if (this.swarm === null) {
230
235
  const swarmOpts = { keyPair: this.db.swarmingKeyPair }
231
- if (this._port)
236
+ if (this._port) {
232
237
  swarmOpts.port = typeof this._port === 'number' ? [this._port, this._port + 64] : this._port
238
+ }
233
239
  this.swarm = new Hyperswarm(swarmOpts)
234
240
  }
235
241
  this.swarm.on('connection', this._onconnection.bind(this))
@@ -243,6 +249,14 @@ class BlindPeer extends ReadyResource {
243
249
  this.flushInterval = setInterval(this.flush.bind(this), 10_000)
244
250
  }
245
251
 
252
+ _getSession(key, discoveryKey) {
253
+ const sessions = this.wakeup.getSessions(key)
254
+ if (sessions.length) return sessions[0]
255
+
256
+ const handler = new WakeupHandler(this.db, key, discoveryKey)
257
+ return this.wakeup.session(key, handler)
258
+ }
259
+
246
260
  async _onwakeup(discoveryKey, muxer) {
247
261
  this.stats.wakeups++
248
262
 
@@ -250,21 +264,14 @@ class BlindPeer extends ReadyResource {
250
264
  if (!auth) return
251
265
 
252
266
  const stream = muxer.stream
253
- const handler = new WakeupHandler(this.db, auth.key, discoveryKey)
254
-
255
- if (this.wakeup.hasStream(stream, auth.key, handler)) {
256
- return
257
- }
258
-
259
- const w = this.wakeup.session(auth.key, handler)
260
- w.addStream(stream)
267
+ const session = this._getSession(auth.key, discoveryKey)
261
268
 
262
- for (const peer of w.peers) {
263
- if (peer.active) handler.onpeeractive(peer, w)
264
- }
269
+ if (session.hasStream(stream)) return
270
+ session.addStream(stream)
265
271
 
266
- stream.setMaxListeners(0)
267
- stream.once('close', () => w.destroy())
272
+ // if new peer, send back the active handler for this peer
273
+ const peer = session.getPeer(stream)
274
+ if (peer && peer.active) handler.onpeeractive(peer, session)
268
275
  }
269
276
 
270
277
  async listen() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blind-peer",
3
- "version": "2.9.0",
3
+ "version": "2.9.1",
4
4
  "description": "Blind peers help keep hypercores available",
5
5
  "main": "index.js",
6
6
  "bin": {