blind-peer 2.8.3 → 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 (3) hide show
  1. package/bin.js +16 -0
  2. package/index.js +24 -15
  3. package/package.json +2 -2
package/bin.js CHANGED
@@ -60,6 +60,10 @@ const cmd = command(
60
60
  '--repl [repl]',
61
61
  'Expose a repl-swarm at the passed-in seed (32 bytes in hex or z32 notation). Use for debugging only.'
62
62
  ),
63
+ flag(
64
+ '--auto-shutdown-minutes [auto-shutdown-minutes]',
65
+ '(Temporary, Advanced) Automatically shut the process down after X minutes, with a variation of 20%'
66
+ ),
63
67
  async function ({ flags }) {
64
68
  const debug = flags.debug
65
69
  const logger = pino({
@@ -290,6 +294,18 @@ const cmd = command(
290
294
 
291
295
  logger.info(`Listening at ${idEnc.normalize(blindPeer.publicKey)}`)
292
296
  logger.info(`Encryption public key is ${idEnc.normalize(blindPeer.encryptionPublicKey)}`)
297
+
298
+ if (flags.autoShutdownMinutes) {
299
+ const delay = flags.autoShutdownMinutes * (1 + Math.random() / 5)
300
+ logger.warn(`Automatically shutting down the process in ${delay} minutes`)
301
+ setTimeout(
302
+ () => {
303
+ logger.warn('Auto-shutdown triggered. Shutting down...')
304
+ goodbye.exit()
305
+ },
306
+ delay * 60 * 1000
307
+ )
308
+ }
293
309
  }
294
310
  )
295
311
 
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)
267
+ const session = this._getSession(auth.key, discoveryKey)
254
268
 
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)
269
+ if (session.hasStream(stream)) return
270
+ session.addStream(stream)
261
271
 
262
- for (const peer of w.peers) {
263
- if (peer.active) handler.onpeeractive(peer, w)
264
- }
265
-
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() {
@@ -527,6 +534,8 @@ class BlindPeer extends ReadyResource {
527
534
  }
528
535
 
529
536
  registerMetrics(promClient) {
537
+ this.wakeup.registerMetrics(promClient)
538
+
530
539
  const self = this
531
540
  new promClient.Gauge({
532
541
  // eslint-disable-line no-new
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blind-peer",
3
- "version": "2.8.3",
3
+ "version": "2.9.1",
4
4
  "description": "Blind peers help keep hypercores available",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -24,7 +24,7 @@
24
24
  "paparam": "^1.8.0",
25
25
  "pino": "^9.6.0",
26
26
  "protomux-rpc": "^1.7.1",
27
- "protomux-wakeup": "^2.6.0",
27
+ "protomux-wakeup": "^2.7.0",
28
28
  "ready-resource": "^1.1.2",
29
29
  "repl-swarm": "^2.3.0",
30
30
  "rocksdb-native": "^3.1.6",