blind-peer 2.9.1 → 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 +33 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -128,11 +128,28 @@ class CoreTracker {
128
128
  }
129
129
 
130
130
  class WakeupHandler {
131
- constructor(db, key, discoveryKey) {
131
+ constructor(wakeup, db, key, discoveryKey) {
132
+ this.wakeup = wakeup
132
133
  this.db = db
133
134
  this.key = key
134
135
  this.discoveryKey = discoveryKey
135
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()
136
153
  }
137
154
 
138
155
  async onpeeractive(peer, session) {
@@ -153,9 +170,16 @@ class WakeupHandler {
153
170
  }
154
171
  }
155
172
 
156
- onpeerinactive(peer, session) {
173
+ _gc() {
174
+ const sessions = this.wakeup.getSessions(this.key)
175
+ if (!sessions.length) return
176
+ const session = sessions[0]
157
177
  if (session.peers.length === 0) session.destroy()
158
178
  }
179
+
180
+ ondestroy(session) {
181
+ this.remoteAttached()
182
+ }
159
183
  }
160
184
 
161
185
  class BlindPeer extends ReadyResource {
@@ -253,8 +277,10 @@ class BlindPeer extends ReadyResource {
253
277
  const sessions = this.wakeup.getSessions(key)
254
278
  if (sessions.length) return sessions[0]
255
279
 
256
- const handler = new WakeupHandler(this.db, key, discoveryKey)
257
- return this.wakeup.session(key, handler)
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
258
284
  }
259
285
 
260
286
  async _onwakeup(discoveryKey, muxer) {
@@ -264,6 +290,8 @@ class BlindPeer extends ReadyResource {
264
290
  if (!auth) return
265
291
 
266
292
  const stream = muxer.stream
293
+ if (stream.destroying || stream.destroyed) return
294
+
267
295
  const session = this._getSession(auth.key, discoveryKey)
268
296
 
269
297
  if (session.hasStream(stream)) return
@@ -271,7 +299,7 @@ class BlindPeer extends ReadyResource {
271
299
 
272
300
  // if new peer, send back the active handler for this peer
273
301
  const peer = session.getPeer(stream)
274
- if (peer && peer.active) handler.onpeeractive(peer, session)
302
+ if (peer && peer.active) session.handlers.onpeeractive(peer, session)
275
303
  }
276
304
 
277
305
  async listen() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blind-peer",
3
- "version": "2.9.1",
3
+ "version": "2.9.2",
4
4
  "description": "Blind peers help keep hypercores available",
5
5
  "main": "index.js",
6
6
  "bin": {