dht-rpc 6.10.0 → 6.11.0

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.
package/index.js CHANGED
@@ -44,6 +44,7 @@ class DHT extends EventEmitter {
44
44
  this.adaptive = typeof opts.ephemeral !== 'boolean' && opts.adaptive !== false
45
45
  this.destroyed = false
46
46
  this.suspended = false
47
+ this.online = true
47
48
 
48
49
  this._nat = new NatSampler()
49
50
  this._quickFirewall = opts.quickFirewall !== false
@@ -420,6 +421,7 @@ class DHT extends EventEmitter {
420
421
 
421
422
  _onnetworkchange (interfaces) {
422
423
  this.emit('network-change', interfaces)
424
+ this.emit('network-update')
423
425
  }
424
426
 
425
427
  _repingAndSwap (newNode, oldNode) {
@@ -688,7 +690,7 @@ class DHT extends EventEmitter {
688
690
  // if no nodes are available, including bootstrappers - bail
689
691
  if (nodes.length === 0) return true
690
692
 
691
- const hosts = []
693
+ const hosts = new Set()
692
694
  const value = b4a.allocUnsafe(2)
693
695
 
694
696
  c.uint16.encode({ start: 0, end: 2, buffer: value }, this.io.serverSocket.address().port)
@@ -697,11 +699,10 @@ class DHT extends EventEmitter {
697
699
  this.io.serverSocket.on('message', onmessage)
698
700
 
699
701
  const pongs = await requestAll(this, true, PING_NAT, value, nodes)
700
- if (!pongs.length) return true
701
702
 
702
703
  let count = 0
703
704
  for (const res of pongs) {
704
- if (hosts.indexOf(res.from.host) > -1) {
705
+ if (hosts.has(res.from.host)) {
705
706
  count++
706
707
  natSampler.add(res.to.host, res.to.port)
707
708
  }
@@ -709,7 +710,7 @@ class DHT extends EventEmitter {
709
710
 
710
711
  this.io.serverSocket.removeListener('message', onmessage)
711
712
 
712
- // if we got very few replies, consider it a fluke
713
+ // if we got no or very few replies, consider it a fluke
713
714
  if (count < (nodes.length >= 5 ? 3 : 1)) return true
714
715
 
715
716
  // check that the server socket has the same ip as the client socket
@@ -722,7 +723,7 @@ class DHT extends EventEmitter {
722
723
  return false
723
724
 
724
725
  function onmessage (_, { host }) {
725
- hosts.push(host)
726
+ hosts.add(host)
726
727
  }
727
728
  }
728
729
 
@@ -741,6 +742,20 @@ class DHT extends EventEmitter {
741
742
 
742
743
  return q
743
744
  }
745
+
746
+ // called by the query
747
+ _online () {
748
+ if (this.online) return
749
+ this.online = true
750
+ this.emit('network-update')
751
+ }
752
+
753
+ // called by the query
754
+ _offline () {
755
+ if (!this.online) return
756
+ this.online = false
757
+ this.emit('network-update')
758
+ }
744
759
  }
745
760
 
746
761
  DHT.OK = 0
package/lib/io.js CHANGED
@@ -27,6 +27,7 @@ module.exports = class IO {
27
27
  this.ephemeral = true
28
28
  this.congestion = new CongestionWindow(maxWindow)
29
29
  this.networkInterfaces = udx.watchNetworkInterfaces()
30
+ this.suspended = false
30
31
 
31
32
  this.onrequest = onrequest
32
33
  this.onresponse = onresponse
@@ -47,7 +48,7 @@ module.exports = class IO {
47
48
  }
48
49
 
49
50
  onmessage (socket, buffer, { host, port }) {
50
- if (buffer.byteLength < 2 || !(port > 0 && port < 65536)) return
51
+ if (buffer.byteLength < 2 || !(port > 0 && port < 65536) || this.suspended === true) return
51
52
 
52
53
  const from = { id: null, host, port }
53
54
  const state = { start: 1, end: buffer.byteLength, buffer }
@@ -73,6 +74,8 @@ module.exports = class IO {
73
74
  const req = this.inflight[i]
74
75
  if (req.tid !== res.tid) continue
75
76
 
77
+ res.rtt = Date.now() - req._timestamp
78
+
76
79
  if (i === this.inflight.length - 1) this.inflight.pop()
77
80
  else this.inflight[i] = this.inflight.pop()
78
81
 
@@ -151,6 +154,7 @@ module.exports = class IO {
151
154
  }
152
155
 
153
156
  suspend () {
157
+ this.suspended = true
154
158
  if (this._drainInterval) {
155
159
  clearInterval(this._drainInterval)
156
160
  this._drainInterval = null
@@ -158,6 +162,7 @@ module.exports = class IO {
158
162
  }
159
163
 
160
164
  resume () {
165
+ this.suspended = false
161
166
  const binding = this._binding
162
167
  this._binding = this._rebind(binding)
163
168
  return this._binding
@@ -281,6 +286,7 @@ class Request {
281
286
  this._buffer = null
282
287
  this._io = io
283
288
  this._timeout = null
289
+ this._timestamp = Date.now()
284
290
  }
285
291
 
286
292
  static decode (io, socket, from, state) {
@@ -485,7 +491,7 @@ function decodeReply (from, state) {
485
491
 
486
492
  if (id !== null) from.id = validateId(id, from)
487
493
 
488
- return { tid, from, to, token, closerNodes, error, value }
494
+ return { tid, rtt: 0, from, to, token, closerNodes, error, value }
489
495
  } catch {
490
496
  return null
491
497
  }
package/lib/query.js CHANGED
@@ -25,6 +25,7 @@ module.exports = class Query extends Readable {
25
25
  this.closestReplies = []
26
26
 
27
27
  this._slow = 0
28
+ this._online = false
28
29
  this._slowdown = false
29
30
  this._seen = new Map()
30
31
  this._pending = []
@@ -234,6 +235,9 @@ module.exports = class Query extends Readable {
234
235
  _onvisit (m, req) {
235
236
  this._dec(req)
236
237
 
238
+ this._online = true
239
+ if (!this.dht.online) this.dht._online()
240
+
237
241
  const addr = req.to.host + ':' + req.to.port
238
242
  this._seen.set(addr, DONE)
239
243
 
@@ -334,6 +338,7 @@ module.exports = class Query extends Readable {
334
338
  }
335
339
 
336
340
  _destroy (cb) {
341
+ if (!this._online && this.dht.online) this.dht._offline()
337
342
  if (this._autoDestroySession) this._session.destroy()
338
343
  cb(null)
339
344
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dht-rpc",
3
- "version": "6.10.0",
3
+ "version": "6.11.0",
4
4
  "description": "Make RPC calls over a Kademlia based DHT",
5
5
  "main": "index.js",
6
6
  "files": [