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 +1 -1
- package/lib/errors.js +25 -16
- package/lib/io.js +10 -5
- package/package.json +1 -1
package/index.js
CHANGED
package/lib/errors.js
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
|
-
exports
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
6
|
+
if (Error.captureStackTrace) {
|
|
7
|
+
Error.captureStackTrace(this, fn)
|
|
8
|
+
}
|
|
9
|
+
}
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
|
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(
|
|
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 ?
|
|
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 ||
|
|
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(
|
|
468
|
+
req.destroy(REQUEST_TIMEOUT())
|
|
464
469
|
req._io.ontimeout(req)
|
|
465
470
|
} else {
|
|
466
471
|
req.send()
|