bare-rpc 1.3.6 → 1.3.7
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 +18 -1
- package/lib/errors.d.ts +2 -0
- package/lib/errors.js +4 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const safetyCatch = require('safety-catch')
|
|
|
2
2
|
const b4a = require('b4a')
|
|
3
3
|
const c = require('compact-encoding')
|
|
4
4
|
const m = require('./lib/messages')
|
|
5
|
+
const errors = require('./lib/errors')
|
|
5
6
|
const { type: t, stream: s } = require('./lib/constants')
|
|
6
7
|
const IncomingEvent = require('./lib/incoming-event')
|
|
7
8
|
const IncomingRequest = require('./lib/incoming-request')
|
|
@@ -36,10 +37,15 @@ module.exports = exports = class RPC {
|
|
|
36
37
|
|
|
37
38
|
this._onrequest = onrequest
|
|
38
39
|
this._onerror = this._onerror.bind(this)
|
|
40
|
+
this._onclose = this._onclose.bind(this)
|
|
39
41
|
this._ondata = this._ondata.bind(this)
|
|
40
42
|
this._ondrain = this._ondrain.bind(this)
|
|
41
43
|
|
|
42
|
-
this._stream
|
|
44
|
+
this._stream
|
|
45
|
+
.on('error', this._onerror)
|
|
46
|
+
.on('close', this._onclose)
|
|
47
|
+
.on('data', this._ondata)
|
|
48
|
+
.on('drain', this._ondrain)
|
|
43
49
|
}
|
|
44
50
|
|
|
45
51
|
// Whether there are no requests or responses currently in flight. Useful for
|
|
@@ -175,6 +181,17 @@ module.exports = exports = class RPC {
|
|
|
175
181
|
}
|
|
176
182
|
|
|
177
183
|
_onerror(err) {
|
|
184
|
+
this._teardown(err)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
_onclose() {
|
|
188
|
+
// A clean close of the underlying stream leaves in-flight requests and
|
|
189
|
+
// responses with nowhere to complete, so tear them down as if the channel
|
|
190
|
+
// had errored.
|
|
191
|
+
this._teardown(errors.CHANNEL_CLOSED('Channel closed'))
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
_teardown(err) {
|
|
178
195
|
this._ondrain(err)
|
|
179
196
|
|
|
180
197
|
// Reject any request awaiting a plain reply. Requests whose response is
|
package/lib/errors.d.ts
CHANGED
package/lib/errors.js
CHANGED
|
@@ -23,4 +23,8 @@ module.exports = class RPCError extends Error {
|
|
|
23
23
|
static ALREADY_RECEIVED(msg) {
|
|
24
24
|
return new RPCError(msg, 'ALREADY_RECEIVED', RPCError.ALREADY_RECEIVED)
|
|
25
25
|
}
|
|
26
|
+
|
|
27
|
+
static CHANNEL_CLOSED(msg) {
|
|
28
|
+
return new RPCError(msg, 'CHANNEL_CLOSED', RPCError.CHANNEL_CLOSED)
|
|
29
|
+
}
|
|
26
30
|
}
|