bare-rpc 1.0.0 → 1.1.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.d.ts CHANGED
@@ -23,6 +23,16 @@ declare const constants: {
23
23
  }
24
24
  }
25
25
 
26
+ interface RPCIncomingEvent {
27
+ readonly rpc: RPC
28
+ readonly command: number
29
+ readonly data: Buffer | null
30
+ }
31
+
32
+ declare class RPCIncomingEvent {
33
+ constructor(rpc: RPC, command: number, data: Buffer)
34
+ }
35
+
26
36
  interface RPCIncomingRequest {
27
37
  readonly rpc: RPC
28
38
  readonly id: number
@@ -41,6 +51,18 @@ declare class RPCIncomingRequest {
41
51
  constructor(rpc: RPC, id: number, command: number, data: Buffer)
42
52
  }
43
53
 
54
+ interface RPCOutgoingEvent {
55
+ readonly rpc: RPC
56
+ readonly command: number
57
+ readonly sent: boolean
58
+
59
+ send(data?: Buffer | string | null, encoding?: BufferEncoding): void
60
+ }
61
+
62
+ declare class RPCOutgoingEvent {
63
+ constructor(rpc: RPC, command: number)
64
+ }
65
+
44
66
  interface RPCOutgoingRequest {
45
67
  readonly rpc: RPC
46
68
  readonly id: number
@@ -57,7 +79,7 @@ interface RPCOutgoingRequest {
57
79
  }
58
80
 
59
81
  declare class RPCOutgoingRequest {
60
- constructor(rpc: RPC, command: number)
82
+ constructor(rpc: RPC, id: number, command: number)
61
83
  }
62
84
 
63
85
  declare class RPCIncomingStream extends Readable {
@@ -79,6 +101,7 @@ declare class RPCOutgoingStream extends Writable {
79
101
  }
80
102
 
81
103
  interface RPC {
104
+ event(command: number): RPCOutgoingEvent
82
105
  request(command: number): RPCOutgoingRequest
83
106
  }
84
107
 
@@ -91,6 +114,8 @@ declare class RPC {
91
114
 
92
115
  declare namespace RPC {
93
116
  export {
117
+ type RPCIncomingEvent as IncomingEvent,
118
+ type RPCOutgoingEvent as OutgoingEvent,
94
119
  type RPCIncomingRequest as IncomingRequest,
95
120
  type RPCOutgoingRequest as OutgoingRequest,
96
121
  type RPCIncomingStream as IncomingStream,
package/index.js CHANGED
@@ -3,8 +3,10 @@ const b4a = require('b4a')
3
3
  const c = require('compact-encoding')
4
4
  const m = require('./lib/messages')
5
5
  const { type: t, stream: s } = require('./lib/constants')
6
+ const IncomingEvent = require('./lib/incoming-event')
6
7
  const IncomingRequest = require('./lib/incoming-request')
7
8
  const IncomingStream = require('./lib/incoming-stream')
9
+ const OutgoingEvent = require('./lib/outgoing-event')
8
10
  const OutgoingRequest = require('./lib/outgoing-request')
9
11
  const OutgoingStream = require('./lib/outgoing-stream')
10
12
  const CommandRouter = require('./lib/command-router')
@@ -43,6 +45,10 @@ module.exports = exports = class RPC {
43
45
  .on('drain', this._ondrain)
44
46
  }
45
47
 
48
+ event(command) {
49
+ return new OutgoingEvent(this, command)
50
+ }
51
+
46
52
  request(command) {
47
53
  return new OutgoingRequest(this, ++this._id, command)
48
54
  }
@@ -60,6 +66,16 @@ module.exports = exports = class RPC {
60
66
  }
61
67
  }
62
68
 
69
+ _sendEvent(request, data = null) {
70
+ this._sendMessage({
71
+ type: t.REQUEST,
72
+ id: 0,
73
+ command: request.command,
74
+ stream: 0,
75
+ data
76
+ })
77
+ }
78
+
63
79
  _sendRequest(request, data = null) {
64
80
  this._outgoingRequests.set(request.id, request)
65
81
 
@@ -201,19 +217,23 @@ module.exports = exports = class RPC {
201
217
 
202
218
  switch (message.type) {
203
219
  case t.REQUEST:
204
- const request = new IncomingRequest(
205
- this,
206
- message.id,
207
- message.command,
208
- message.data
209
- )
220
+ const request =
221
+ message.id === 0
222
+ ? new IncomingEvent(this, message.command, message.data)
223
+ : new IncomingRequest(
224
+ this,
225
+ message.id,
226
+ message.command,
227
+ message.data
228
+ )
210
229
 
211
230
  try {
212
231
  await this._onrequest(request)
213
232
  } catch (err) {
214
233
  safetyCatch(err)
215
234
 
216
- this._sendError(request, err)
235
+ if (message.id) this._sendError(request, err)
236
+ else this._stream.destroy(err)
217
237
  }
218
238
  break
219
239
  case t.RESPONSE:
@@ -0,0 +1,7 @@
1
+ module.exports = class RPCIncomingEvent {
2
+ constructor(rpc, command, data) {
3
+ this.rpc = rpc
4
+ this.command = command
5
+ this.data = data
6
+ }
7
+ }
@@ -0,0 +1,27 @@
1
+ const c = require('compact-encoding')
2
+ const errors = require('./errors')
3
+
4
+ module.exports = class RPCOutgoingEvent {
5
+ constructor(rpc, command) {
6
+ this.rpc = rpc
7
+ this.command = command
8
+ this.sent = false
9
+ }
10
+
11
+ send(data, encoding) {
12
+ if (this.sent) {
13
+ throw errors.ALREADY_SENT('Event has already been sent')
14
+ }
15
+
16
+ encoding =
17
+ encoding && encoding !== 'buffer'
18
+ ? c.from(encoding)
19
+ : typeof data === 'string'
20
+ ? c.raw.utf8
21
+ : null
22
+
23
+ this.sent = true
24
+
25
+ this.rpc._sendEvent(this, encoding ? c.encode(encoding, data) : data)
26
+ }
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-rpc",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "librpc ABI compatible RPC for Bare",
5
5
  "exports": {
6
6
  ".": {