blind-peer 2.9.0 → 2.9.2

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 +50 -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
@@ -127,11 +128,28 @@ class CoreTracker {
127
128
  }
128
129
 
129
130
  class WakeupHandler {
130
- constructor(db, key, discoveryKey) {
131
+ constructor(wakeup, db, key, discoveryKey) {
132
+ this.wakeup = wakeup
131
133
  this.db = db
132
134
  this.key = key
133
135
  this.discoveryKey = discoveryKey
134
136
  this.active = false
137
+ this.timeout = setTimeout(this._gc.bind(this), 15_000) // sanity
138
+ }
139
+
140
+ remoteAttached() {
141
+ if (!this.timeout) return
142
+ clearTimeout(this.timeout)
143
+ this.timeout = null
144
+ }
145
+
146
+ onpeeradd(peer, session) {
147
+ this.remoteAttached()
148
+ }
149
+
150
+ onpeerremove(peer, session) {
151
+ this.remoteAttached()
152
+ if (session.peers.length === 0) session.destroy()
135
153
  }
136
154
 
137
155
  async onpeeractive(peer, session) {
@@ -151,6 +169,17 @@ class WakeupHandler {
151
169
  // do nothing
152
170
  }
153
171
  }
172
+
173
+ _gc() {
174
+ const sessions = this.wakeup.getSessions(this.key)
175
+ if (!sessions.length) return
176
+ const session = sessions[0]
177
+ if (session.peers.length === 0) session.destroy()
178
+ }
179
+
180
+ ondestroy(session) {
181
+ this.remoteAttached()
182
+ }
154
183
  }
155
184
 
156
185
  class BlindPeer extends ReadyResource {
@@ -228,8 +257,9 @@ class BlindPeer extends ReadyResource {
228
257
 
229
258
  if (this.swarm === null) {
230
259
  const swarmOpts = { keyPair: this.db.swarmingKeyPair }
231
- if (this._port)
260
+ if (this._port) {
232
261
  swarmOpts.port = typeof this._port === 'number' ? [this._port, this._port + 64] : this._port
262
+ }
233
263
  this.swarm = new Hyperswarm(swarmOpts)
234
264
  }
235
265
  this.swarm.on('connection', this._onconnection.bind(this))
@@ -243,6 +273,16 @@ class BlindPeer extends ReadyResource {
243
273
  this.flushInterval = setInterval(this.flush.bind(this), 10_000)
244
274
  }
245
275
 
276
+ _getSession(key, discoveryKey) {
277
+ const sessions = this.wakeup.getSessions(key)
278
+ if (sessions.length) return sessions[0]
279
+
280
+ const handler = new WakeupHandler(this.wakeup, this.db, key, discoveryKey)
281
+ const session = this.wakeup.session(key, handler)
282
+ if (session.peers.length) handler.remoteAttached()
283
+ return session
284
+ }
285
+
246
286
  async _onwakeup(discoveryKey, muxer) {
247
287
  this.stats.wakeups++
248
288
 
@@ -250,21 +290,16 @@ class BlindPeer extends ReadyResource {
250
290
  if (!auth) return
251
291
 
252
292
  const stream = muxer.stream
253
- const handler = new WakeupHandler(this.db, auth.key, discoveryKey)
293
+ if (stream.destroying || stream.destroyed) return
254
294
 
255
- if (this.wakeup.hasStream(stream, auth.key, handler)) {
256
- return
257
- }
295
+ const session = this._getSession(auth.key, discoveryKey)
258
296
 
259
- const w = this.wakeup.session(auth.key, handler)
260
- w.addStream(stream)
261
-
262
- for (const peer of w.peers) {
263
- if (peer.active) handler.onpeeractive(peer, w)
264
- }
297
+ if (session.hasStream(stream)) return
298
+ session.addStream(stream)
265
299
 
266
- stream.setMaxListeners(0)
267
- stream.once('close', () => w.destroy())
300
+ // if new peer, send back the active handler for this peer
301
+ const peer = session.getPeer(stream)
302
+ if (peer && peer.active) session.handlers.onpeeractive(peer, session)
268
303
  }
269
304
 
270
305
  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.2",
4
4
  "description": "Blind peers help keep hypercores available",
5
5
  "main": "index.js",
6
6
  "bin": {