dht-rpc 6.8.0 → 6.8.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.
package/index.js CHANGED
@@ -109,7 +109,7 @@ class DHT extends EventEmitter {
109
109
  async suspend () {
110
110
  await this.io.bind()
111
111
  if (this.suspended || this.destroyed) return
112
- this.suspended = false
112
+ this.suspended = true
113
113
  this.io.suspend()
114
114
  this.emit('suspend')
115
115
  }
package/lib/errors.js CHANGED
@@ -1,20 +1,29 @@
1
- exports.UNKNOWN_COMMAND = 1
2
- exports.INVALID_TOKEN = 2
1
+ module.exports = class DHTError extends Error {
2
+ constructor (msg, code, fn = DHTError) {
3
+ super(`${code}: ${msg}`)
4
+ this.code = code
3
5
 
4
- exports.createTimeoutError = () => {
5
- const timeoutErr = new Error('Request timed out')
6
- timeoutErr.code = 'ETIMEDOUT'
7
- return timeoutErr
8
- }
6
+ if (Error.captureStackTrace) {
7
+ Error.captureStackTrace(this, fn)
8
+ }
9
+ }
9
10
 
10
- exports.createSuspendError = () => {
11
- const suspendErr = new Error('Suspended')
12
- suspendErr.code = 'ESUSPENDED'
13
- return suspendErr
14
- }
11
+ get name () {
12
+ return 'DHTError'
13
+ }
14
+
15
+ static UNKNOWN_COMMAND = 1
16
+ static INVALID_TOKEN = 2
17
+
18
+ static REQUEST_TIMEOUT (msg = 'Request timed out') {
19
+ return new DHTError(msg, 'REQUEST_TIMEOUT', DHTError.REQUEST_TIMEOUT)
20
+ }
21
+
22
+ static REQUEST_DESTROYED (msg = 'Request destroyed') {
23
+ return new DHTError(msg, 'REQUEST_DESTROYED', DHTError.REQUEST_DESTROYED)
24
+ }
15
25
 
16
- exports.createDestroyedError = () => {
17
- const destroyErr = new Error('Request destroyed')
18
- destroyErr.code = 'EDESTROYED'
19
- return destroyErr
26
+ static IO_SUSPENDED (msg = 'I/O suspended') {
27
+ return new DHTError(msg, 'IO_SUSPENDED', DHTError.IO_SUSPENDED)
28
+ }
20
29
  }
package/lib/io.js CHANGED
@@ -3,7 +3,12 @@ const sodium = require('sodium-universal')
3
3
  const c = require('compact-encoding')
4
4
  const b4a = require('b4a')
5
5
  const peer = require('./peer')
6
- const errors = require('./errors')
6
+ const {
7
+ INVALID_TOKEN,
8
+ REQUEST_TIMEOUT,
9
+ REQUEST_DESTROYED,
10
+ IO_SUSPENDED
11
+ } = require('./errors')
7
12
 
8
13
  const VERSION = 0b11
9
14
  const RESPONSE_ID = (0b0001 << 4) | VERSION
@@ -53,7 +58,7 @@ module.exports = class IO {
53
58
  const req = Request.decode(this, socket, from, state)
54
59
  if (req === null) return
55
60
  if (req.token !== null && !b4a.equals(req.token, this.token(req.from, 1)) && !b4a.equals(req.token, this.token(req.from, 0))) {
56
- req.error(errors.INVALID_TOKEN, { token: true })
61
+ req.error(INVALID_TOKEN, { token: true })
57
62
  return
58
63
  }
59
64
  this.onrequest(req, external)
@@ -127,7 +132,7 @@ module.exports = class IO {
127
132
 
128
133
  if (req.session) req.session._detach(req)
129
134
 
130
- req.onerror(suspended ? errors.createSuspendError() : errors.createDestroyedError(), req)
135
+ req.onerror(suspended ? IO_SUSPENDED() : REQUEST_DESTROYED(), req)
131
136
  }
132
137
 
133
138
  await Promise.allSettled([
@@ -360,7 +365,7 @@ class Request {
360
365
 
361
366
  if (this.session) this.session._detach(this)
362
367
 
363
- this.onerror(err || errors.createDestroyedError(), this)
368
+ this.onerror(err || REQUEST_DESTROYED(), this)
364
369
  }
365
370
 
366
371
  _sendReply (error, value, token, hasCloserNodes, from, socket) {
@@ -460,7 +465,7 @@ function oncycle (req) {
460
465
  req._timeout = null
461
466
  req.oncycle(req)
462
467
  if (req.sent >= req.retries) {
463
- req.destroy(errors.createTimeoutError())
468
+ req.destroy(REQUEST_TIMEOUT())
464
469
  req._io.ontimeout(req)
465
470
  } else {
466
471
  req.send()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dht-rpc",
3
- "version": "6.8.0",
3
+ "version": "6.8.2",
4
4
  "description": "Make RPC calls over a Kademlia based DHT",
5
5
  "main": "index.js",
6
6
  "files": [