bare-rpc 0.0.1 → 0.1.1
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 +223 -32
- package/lib/constants.js +14 -1
- package/lib/incoming-request.js +23 -1
- package/lib/incoming-stream.js +59 -0
- package/lib/messages.js +61 -33
- package/lib/outgoing-request.js +23 -10
- package/lib/outgoing-stream.js +94 -0
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
const safetyCatch = require('safety-catch')
|
|
2
|
+
const b4a = require('b4a')
|
|
2
3
|
const c = require('compact-encoding')
|
|
3
4
|
const m = require('./lib/messages')
|
|
4
|
-
const { type } = require('./lib/constants')
|
|
5
|
+
const { type: t, stream: s } = require('./lib/constants')
|
|
5
6
|
const IncomingRequest = require('./lib/incoming-request')
|
|
7
|
+
const IncomingStream = require('./lib/incoming-stream')
|
|
6
8
|
const OutgoingRequest = require('./lib/outgoing-request')
|
|
9
|
+
const OutgoingStream = require('./lib/outgoing-stream')
|
|
7
10
|
|
|
8
11
|
module.exports = class RPC {
|
|
9
12
|
constructor (stream, onrequest) {
|
|
10
13
|
this._stream = stream
|
|
11
14
|
|
|
12
15
|
this._id = 0
|
|
13
|
-
this._responding = 0
|
|
14
16
|
this._requests = new Map()
|
|
17
|
+
this._responses = new Map()
|
|
18
|
+
this._incoming = new Map()
|
|
15
19
|
|
|
16
20
|
this._buffer = null
|
|
17
21
|
|
|
@@ -26,42 +30,77 @@ module.exports = class RPC {
|
|
|
26
30
|
return new OutgoingRequest(this, command)
|
|
27
31
|
}
|
|
28
32
|
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
_sendMessage (message) {
|
|
34
|
+
this._stream.write(c.encode(m.message, message))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
_sendRequest (request, data = null) {
|
|
38
|
+
const id = request.id = ++this._id
|
|
31
39
|
|
|
32
40
|
this._requests.set(id, request)
|
|
33
41
|
|
|
34
|
-
this.
|
|
35
|
-
type:
|
|
42
|
+
this._sendMessage({
|
|
43
|
+
type: t.REQUEST,
|
|
36
44
|
id,
|
|
37
45
|
command: request.command,
|
|
46
|
+
stream: 0,
|
|
38
47
|
data
|
|
39
|
-
})
|
|
48
|
+
})
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
_createRequestStream (request, isInitiator, opts) {
|
|
52
|
+
if (isInitiator) {
|
|
53
|
+
const id = request.id = ++this._id
|
|
54
|
+
|
|
55
|
+
this._requests.set(id, request)
|
|
56
|
+
|
|
57
|
+
request._requestStream = new OutgoingStream(this, request, t.REQUEST, opts)
|
|
58
|
+
} else {
|
|
59
|
+
this._incoming.set(request.id, request)
|
|
60
|
+
|
|
61
|
+
const stream = request._requestStream = new IncomingStream(this, request, t.REQUEST, opts)
|
|
62
|
+
|
|
63
|
+
stream.on('close', () => this._incoming.delete(request.id))
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_sendResponse (request, data) {
|
|
68
|
+
this._sendMessage({
|
|
69
|
+
type: t.RESPONSE,
|
|
45
70
|
id: request.id,
|
|
46
|
-
|
|
71
|
+
stream: 0,
|
|
72
|
+
error: null,
|
|
47
73
|
data
|
|
48
|
-
})
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_createResponseStream (request, isInitiator, opts) {
|
|
78
|
+
if (isInitiator) {
|
|
79
|
+
this._responses.set(request.id, request)
|
|
80
|
+
|
|
81
|
+
request._responseStream = new OutgoingStream(this, request, t.RESPONSE, opts)
|
|
82
|
+
} else {
|
|
83
|
+
this._incoming.set(request.id, request)
|
|
84
|
+
|
|
85
|
+
const stream = request._responseStream = new IncomingStream(this, request, t.RESPONSE, opts)
|
|
86
|
+
|
|
87
|
+
stream.on('close', () => this._incoming.delete(request.id))
|
|
88
|
+
}
|
|
49
89
|
}
|
|
50
90
|
|
|
51
|
-
|
|
52
|
-
this.
|
|
53
|
-
type:
|
|
91
|
+
_sendError (request, err) {
|
|
92
|
+
this._sendMessage({
|
|
93
|
+
type: t.RESPONSE,
|
|
54
94
|
id: request.id,
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}))
|
|
95
|
+
stream: 0,
|
|
96
|
+
error: err,
|
|
97
|
+
data: null
|
|
98
|
+
})
|
|
60
99
|
}
|
|
61
100
|
|
|
62
101
|
_ondata (data) {
|
|
63
102
|
if (this._buffer === null) this._buffer = data
|
|
64
|
-
else this._buffer =
|
|
103
|
+
else this._buffer = b4a.concat([this._buffer, data])
|
|
65
104
|
|
|
66
105
|
while (this._buffer !== null) {
|
|
67
106
|
const state = { start: 0, end: this._buffer.length, buffer: this._buffer }
|
|
@@ -78,7 +117,7 @@ module.exports = class RPC {
|
|
|
78
117
|
if (message === null) return
|
|
79
118
|
|
|
80
119
|
switch (message.type) {
|
|
81
|
-
case
|
|
120
|
+
case t.REQUEST: {
|
|
82
121
|
const request = new IncomingRequest(this, message.id, message.command, message.data)
|
|
83
122
|
|
|
84
123
|
try {
|
|
@@ -86,18 +125,23 @@ module.exports = class RPC {
|
|
|
86
125
|
} catch (err) {
|
|
87
126
|
safetyCatch(err)
|
|
88
127
|
|
|
89
|
-
this.
|
|
128
|
+
this._sendError(request, err)
|
|
90
129
|
}
|
|
91
|
-
|
|
92
130
|
break
|
|
93
131
|
}
|
|
94
|
-
|
|
95
|
-
case type.RESPONSE:
|
|
132
|
+
case t.RESPONSE:
|
|
96
133
|
try {
|
|
97
134
|
this._onresponse(message)
|
|
98
135
|
} catch (err) {
|
|
99
136
|
safetyCatch(err)
|
|
100
137
|
}
|
|
138
|
+
break
|
|
139
|
+
case t.STREAM:
|
|
140
|
+
try {
|
|
141
|
+
this._onstream(message)
|
|
142
|
+
} catch (err) {
|
|
143
|
+
safetyCatch(err)
|
|
144
|
+
}
|
|
101
145
|
}
|
|
102
146
|
|
|
103
147
|
this._buffer = state.start === state.end ? null : this._buffer.subarray(state.start)
|
|
@@ -108,17 +152,164 @@ module.exports = class RPC {
|
|
|
108
152
|
if (message.id === 0) return
|
|
109
153
|
|
|
110
154
|
const request = this._requests.get(message.id)
|
|
111
|
-
|
|
112
155
|
if (request === undefined) return
|
|
113
156
|
|
|
114
157
|
if (message.error) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
158
|
+
request._reject(message.error)
|
|
159
|
+
} else if (message.stream === 0) {
|
|
160
|
+
request._resolve(message.data)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
_onstream (message) {
|
|
165
|
+
if (message.id === 0) return
|
|
166
|
+
|
|
167
|
+
if (message.stream & s.OPEN) this._onstreamopen(message)
|
|
168
|
+
else if (message.stream & s.CLOSE) this._onstreamclose(message)
|
|
169
|
+
else if (message.stream & s.PAUSE) this._onstreampause(message)
|
|
170
|
+
else if (message.stream & s.RESUME) this._onstreamresume(message)
|
|
171
|
+
else if (message.stream & s.DATA) this._onstreamdata(message)
|
|
172
|
+
else if (message.stream & s.END) this._onstreamend(message)
|
|
173
|
+
else if (message.stream & s.DESTROY) this._onstreamdestroy(message)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
_onstreamopen (message) {
|
|
177
|
+
let stream
|
|
118
178
|
|
|
119
|
-
|
|
179
|
+
if (message.stream & s.REQUEST) {
|
|
180
|
+
const request = this._requests.get(message.id)
|
|
181
|
+
if (request === undefined) return
|
|
182
|
+
|
|
183
|
+
stream = request._requestStream
|
|
184
|
+
} else if (message.stream & s.RESPONSE) {
|
|
185
|
+
const request = this._responses.get(message.id)
|
|
186
|
+
if (request === undefined) return
|
|
187
|
+
|
|
188
|
+
stream = request._responseStream
|
|
120
189
|
} else {
|
|
121
|
-
|
|
190
|
+
return
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
stream._continueOpen()
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
_onstreamclose (message) {
|
|
197
|
+
const request = this._incoming.get(message.id)
|
|
198
|
+
if (request === undefined) return
|
|
199
|
+
|
|
200
|
+
let stream
|
|
201
|
+
|
|
202
|
+
if (message.stream & s.REQUEST) {
|
|
203
|
+
stream = request._requestStream
|
|
204
|
+
} else if (message.stream & s.RESPONSE) {
|
|
205
|
+
stream = request._responseStream
|
|
206
|
+
} else {
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (message.error) stream.destroy(message.error)
|
|
211
|
+
else stream.push(null)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
_onstreampause (message) {
|
|
215
|
+
let stream
|
|
216
|
+
|
|
217
|
+
if (message.stream & s.REQUEST) {
|
|
218
|
+
const request = this._requests.get(message.id)
|
|
219
|
+
if (request === undefined) return
|
|
220
|
+
|
|
221
|
+
stream = request._requestStream
|
|
222
|
+
} else if (message.stream & s.RESPONSE) {
|
|
223
|
+
const request = this._responses.get(message.id)
|
|
224
|
+
if (request === undefined) return
|
|
225
|
+
|
|
226
|
+
stream = request._responseStream
|
|
227
|
+
} else {
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
stream.cork()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
_onstreamresume (message) {
|
|
235
|
+
let stream
|
|
236
|
+
|
|
237
|
+
if (message.stream & s.REQUEST) {
|
|
238
|
+
const request = this._requests.get(message.id)
|
|
239
|
+
if (request === undefined) return
|
|
240
|
+
|
|
241
|
+
stream = request._requestStream
|
|
242
|
+
} else if (message.stream & s.RESPONSE) {
|
|
243
|
+
const request = this._responses.get(message.id)
|
|
244
|
+
if (request === undefined) return
|
|
245
|
+
|
|
246
|
+
stream = request._responseStream
|
|
247
|
+
} else {
|
|
248
|
+
return
|
|
122
249
|
}
|
|
250
|
+
|
|
251
|
+
stream.uncork()
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
_onstreamdata (message) {
|
|
255
|
+
const request = this._incoming.get(message.id)
|
|
256
|
+
if (request === undefined) return
|
|
257
|
+
|
|
258
|
+
let stream
|
|
259
|
+
|
|
260
|
+
if (message.stream & s.REQUEST) {
|
|
261
|
+
stream = request._requestStream
|
|
262
|
+
} else if (message.stream & s.RESPONSE) {
|
|
263
|
+
stream = request._responseStream
|
|
264
|
+
} else {
|
|
265
|
+
return
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (stream.push(message.data) === false) {
|
|
269
|
+
this._sendMessage({
|
|
270
|
+
type: t.STREAM,
|
|
271
|
+
id: stream._request.id,
|
|
272
|
+
stream: stream._mask | s.PAUSE,
|
|
273
|
+
error: null,
|
|
274
|
+
data: null
|
|
275
|
+
})
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
_onstreamend (message) {
|
|
280
|
+
const request = this._incoming.get(message.id)
|
|
281
|
+
if (request === undefined) return
|
|
282
|
+
|
|
283
|
+
let stream
|
|
284
|
+
|
|
285
|
+
if (message.stream & s.REQUEST) {
|
|
286
|
+
stream = request._requestStream
|
|
287
|
+
} else if (message.stream & s.RESPONSE) {
|
|
288
|
+
stream = request._responseStream
|
|
289
|
+
} else {
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
stream.push(null)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
_onstreamdestroy (message) {
|
|
297
|
+
let stream
|
|
298
|
+
|
|
299
|
+
if (message.stream & s.REQUEST) {
|
|
300
|
+
const request = this._requests.get(message.id)
|
|
301
|
+
if (request === undefined) return
|
|
302
|
+
|
|
303
|
+
stream = request._requestStream
|
|
304
|
+
} else if (message.stream & s.RESPONSE) {
|
|
305
|
+
const request = this._responses.get(message.id)
|
|
306
|
+
if (request === undefined) return
|
|
307
|
+
|
|
308
|
+
stream = request._responseStream
|
|
309
|
+
} else {
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
stream.destroy(message.error)
|
|
123
314
|
}
|
|
124
315
|
}
|
package/lib/constants.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
module.exports = {
|
|
2
2
|
type: {
|
|
3
3
|
REQUEST: 1,
|
|
4
|
-
RESPONSE: 2
|
|
4
|
+
RESPONSE: 2,
|
|
5
|
+
STREAM: 3
|
|
6
|
+
},
|
|
7
|
+
stream: {
|
|
8
|
+
OPEN: 0x1,
|
|
9
|
+
CLOSE: 0x2,
|
|
10
|
+
PAUSE: 0x4,
|
|
11
|
+
RESUME: 0x8,
|
|
12
|
+
DATA: 0x10,
|
|
13
|
+
END: 0x20,
|
|
14
|
+
DESTROY: 0x40,
|
|
15
|
+
ERROR: 0x80,
|
|
16
|
+
REQUEST: 0x100,
|
|
17
|
+
RESPONSE: 0x200
|
|
5
18
|
}
|
|
6
19
|
}
|
package/lib/incoming-request.js
CHANGED
|
@@ -1,12 +1,34 @@
|
|
|
1
|
+
const b4a = require('b4a')
|
|
2
|
+
const errors = require('./errors')
|
|
3
|
+
|
|
1
4
|
module.exports = class RPCIncomingRequest {
|
|
2
5
|
constructor (rpc, id, command, data) {
|
|
3
6
|
this.rpc = rpc
|
|
4
7
|
this.id = id
|
|
5
8
|
this.command = command
|
|
6
9
|
this.data = data
|
|
10
|
+
this.sent = false
|
|
11
|
+
|
|
12
|
+
this._requestStream = null
|
|
13
|
+
this._responseStream = null
|
|
7
14
|
}
|
|
8
15
|
|
|
9
16
|
reply (data) {
|
|
10
|
-
|
|
17
|
+
if (this.sent) throw errors.ALREADY_SENT('Response has already been sent')
|
|
18
|
+
this.sent = true
|
|
19
|
+
|
|
20
|
+
this.rpc._sendResponse(this, typeof data === 'string' ? b4a.from(data) : data)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
createResponseStream (opts = {}) {
|
|
24
|
+
if (this._responseStream === null) this.rpc._createResponseStream(this, true, opts)
|
|
25
|
+
|
|
26
|
+
return this._responseStream
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
createRequestStream (opts = {}) {
|
|
30
|
+
if (this._requestStream === null) this.rpc._createRequestStream(this, false, opts)
|
|
31
|
+
|
|
32
|
+
return this._requestStream
|
|
11
33
|
}
|
|
12
34
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const { Readable } = require('bare-stream')
|
|
2
|
+
const { type: t, stream: s } = require('./constants')
|
|
3
|
+
|
|
4
|
+
module.exports = class RPCIncomingStream extends Readable {
|
|
5
|
+
constructor (rpc, request, type, opts) {
|
|
6
|
+
super({ ...opts, eagerOpen: true })
|
|
7
|
+
|
|
8
|
+
this._rpc = rpc
|
|
9
|
+
this._request = request
|
|
10
|
+
this._type = type
|
|
11
|
+
this._mask = type === t.REQUEST ? s.REQUEST : s.RESPONSE
|
|
12
|
+
|
|
13
|
+
this._pendingOpen = null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
_open (cb) {
|
|
17
|
+
this._rpc._sendMessage({
|
|
18
|
+
type: t.STREAM,
|
|
19
|
+
id: this._request.id,
|
|
20
|
+
stream: this._mask | s.OPEN,
|
|
21
|
+
error: null,
|
|
22
|
+
data: null
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
cb(null)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_read () {
|
|
29
|
+
this._rpc._sendMessage({
|
|
30
|
+
type: t.STREAM,
|
|
31
|
+
id: this._request.id,
|
|
32
|
+
stream: this._mask | s.RESUME,
|
|
33
|
+
error: null,
|
|
34
|
+
data: null
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_destroy (err, cb) {
|
|
39
|
+
if (err) {
|
|
40
|
+
this._rpc._sendMessage({
|
|
41
|
+
type: t.STREAM,
|
|
42
|
+
id: this._request.id,
|
|
43
|
+
stream: this._mask | s.DESTROY | s.ERROR,
|
|
44
|
+
error: err,
|
|
45
|
+
data: null
|
|
46
|
+
})
|
|
47
|
+
} else {
|
|
48
|
+
this._rpc._sendMessage({
|
|
49
|
+
type: t.STREAM,
|
|
50
|
+
id: this._request.id,
|
|
51
|
+
stream: this._mask | s.DESTROY,
|
|
52
|
+
error: null,
|
|
53
|
+
data: null
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
cb(null)
|
|
58
|
+
}
|
|
59
|
+
}
|
package/lib/messages.js
CHANGED
|
@@ -1,74 +1,92 @@
|
|
|
1
1
|
const c = require('compact-encoding')
|
|
2
|
-
const { type: t } = require('./constants')
|
|
2
|
+
const { type: t, stream: s } = require('./constants')
|
|
3
3
|
const errors = require('./errors')
|
|
4
4
|
|
|
5
5
|
const request = {
|
|
6
6
|
preencode (state, m) {
|
|
7
7
|
c.utf8.preencode(state, m.command)
|
|
8
|
-
c.
|
|
9
|
-
c.
|
|
8
|
+
c.uint.preencode(state, m.stream)
|
|
9
|
+
if (m.stream === 0) c.buffer.preencode(state, m.data)
|
|
10
10
|
},
|
|
11
11
|
encode (state, m) {
|
|
12
12
|
c.utf8.encode(state, m.command)
|
|
13
|
-
c.
|
|
14
|
-
c.
|
|
13
|
+
c.uint.encode(state, m.stream)
|
|
14
|
+
if (m.stream === 0) c.buffer.encode(state, m.data)
|
|
15
15
|
},
|
|
16
16
|
decode (state, id) {
|
|
17
17
|
const command = c.utf8.decode(state)
|
|
18
|
-
const
|
|
19
|
-
c.
|
|
18
|
+
const stream = c.uint.decode(state)
|
|
19
|
+
const data = stream === 0 ? c.buffer.decode(state) : null
|
|
20
20
|
|
|
21
|
-
return { type: t.REQUEST, id, command, data }
|
|
21
|
+
return { type: t.REQUEST, id, command, stream, data }
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
const error = {
|
|
26
26
|
preencode (state, m) {
|
|
27
27
|
c.utf8.preencode(state, m.message)
|
|
28
|
-
c.utf8.preencode(state, m.code)
|
|
29
|
-
c.int.preencode(state, m.
|
|
28
|
+
c.utf8.preencode(state, m.code || '')
|
|
29
|
+
c.int.preencode(state, m.errno || 0)
|
|
30
30
|
},
|
|
31
31
|
encode (state, m) {
|
|
32
32
|
c.utf8.encode(state, m.message)
|
|
33
|
-
c.utf8.encode(state, m.code)
|
|
34
|
-
c.int.encode(state, m.
|
|
33
|
+
c.utf8.encode(state, m.code || '')
|
|
34
|
+
c.int.encode(state, m.errno || 0)
|
|
35
35
|
},
|
|
36
36
|
decode (state) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
37
|
+
const err = new Error(`${c.utf8.decode(state)}`)
|
|
38
|
+
err.code = c.utf8.decode(state)
|
|
39
|
+
err.errno = c.int.decode(state)
|
|
40
|
+
return err
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
const response = {
|
|
46
45
|
preencode (state, m) {
|
|
47
|
-
c.bool.preencode(state, m.error)
|
|
48
|
-
|
|
49
|
-
if (m.error) error.preencode(state, m)
|
|
50
|
-
else c.buffer.preencode(state, m.data)
|
|
46
|
+
c.bool.preencode(state, !!m.error)
|
|
47
|
+
c.uint.preencode(state, m.stream)
|
|
51
48
|
|
|
52
|
-
|
|
49
|
+
if (m.error) error.preencode(state, m.error)
|
|
50
|
+
else if (m.stream === 0) c.buffer.preencode(state, m.data)
|
|
53
51
|
},
|
|
54
52
|
encode (state, m) {
|
|
55
|
-
c.bool.encode(state, m.error)
|
|
53
|
+
c.bool.encode(state, !!m.error)
|
|
54
|
+
c.uint.encode(state, m.stream)
|
|
56
55
|
|
|
57
56
|
if (m.error) error.encode(state, m)
|
|
58
|
-
else c.buffer.encode(state, m.data)
|
|
59
|
-
|
|
60
|
-
c.uint.encode(state, 0) // Reserved
|
|
57
|
+
else if (m.stream === 0) c.buffer.encode(state, m.data)
|
|
61
58
|
},
|
|
62
59
|
decode (state) {
|
|
63
60
|
const error = c.bool.decode(state)
|
|
61
|
+
const stream = c.uint.decode(state)
|
|
62
|
+
|
|
63
|
+
if (error) return { stream, error: error.decode(state), data: null }
|
|
64
|
+
if (stream === 0) return { stream, error: null, data: c.buffer.decode(state) }
|
|
65
|
+
|
|
66
|
+
return { stream, error: null, data: null }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const stream = {
|
|
71
|
+
preencode (state, m) {
|
|
72
|
+
c.uint.preencode(state, m.stream)
|
|
64
73
|
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
|
|
74
|
+
if (m.stream & s.ERROR) error.preencode(state, m.error)
|
|
75
|
+
else if (m.stream & s.DATA) c.buffer.preencode(state, m.data)
|
|
76
|
+
},
|
|
77
|
+
encode (state, m) {
|
|
78
|
+
c.uint.encode(state, m.stream)
|
|
79
|
+
|
|
80
|
+
if (m.stream & s.ERROR) error.encode(state, m.error)
|
|
81
|
+
else if (m.stream & s.DATA) c.buffer.encode(state, m.data)
|
|
82
|
+
},
|
|
83
|
+
decode (state) {
|
|
84
|
+
const stream = c.uint.decode(state)
|
|
68
85
|
|
|
69
|
-
|
|
86
|
+
if (stream & s.ERROR) return { stream, error: error.decode(state), data: null }
|
|
87
|
+
if (stream & s.DATA) return { stream, error: null, data: c.buffer.decode(state) }
|
|
70
88
|
|
|
71
|
-
return { error,
|
|
89
|
+
return { stream, error: null, data: null }
|
|
72
90
|
}
|
|
73
91
|
}
|
|
74
92
|
|
|
@@ -84,6 +102,10 @@ exports.message = {
|
|
|
84
102
|
break
|
|
85
103
|
case t.RESPONSE:
|
|
86
104
|
response.preencode(state, m)
|
|
105
|
+
break
|
|
106
|
+
case t.STREAM:
|
|
107
|
+
stream.preencode(state, m)
|
|
108
|
+
break
|
|
87
109
|
}
|
|
88
110
|
},
|
|
89
111
|
encode (state, m) {
|
|
@@ -102,6 +124,10 @@ exports.message = {
|
|
|
102
124
|
break
|
|
103
125
|
case t.RESPONSE:
|
|
104
126
|
response.encode(state, m)
|
|
127
|
+
break
|
|
128
|
+
case t.STREAM:
|
|
129
|
+
stream.encode(state, m)
|
|
130
|
+
break
|
|
105
131
|
}
|
|
106
132
|
|
|
107
133
|
const end = state.start
|
|
@@ -124,9 +150,11 @@ exports.message = {
|
|
|
124
150
|
|
|
125
151
|
switch (type) {
|
|
126
152
|
case t.REQUEST:
|
|
127
|
-
return { type
|
|
153
|
+
return { type, id, ...request.decode(state, id) }
|
|
128
154
|
case t.RESPONSE:
|
|
129
|
-
return { type
|
|
155
|
+
return { type, id, ...response.decode(state, id) }
|
|
156
|
+
case t.STREAM:
|
|
157
|
+
return { type, id, ...stream.decode(state, id) }
|
|
130
158
|
default:
|
|
131
159
|
throw errors.UNKNOWN_MESSAGE(`Unknown message '${type}'`)
|
|
132
160
|
}
|
package/lib/outgoing-request.js
CHANGED
|
@@ -1,29 +1,42 @@
|
|
|
1
|
+
const b4a = require('b4a')
|
|
1
2
|
const errors = require('./errors')
|
|
2
3
|
|
|
3
4
|
module.exports = class RPCOutgoingRequest {
|
|
4
5
|
constructor (rpc, command) {
|
|
5
6
|
this.rpc = rpc
|
|
7
|
+
this.id = -1
|
|
6
8
|
this.command = command
|
|
7
9
|
this.sent = false
|
|
8
10
|
|
|
9
|
-
this._resolve = null
|
|
10
|
-
this._reject = null
|
|
11
|
-
this._promise = null
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
send (data) {
|
|
15
|
-
if (this.sent) throw errors.ALREADY_SENT('Response has already been sent')
|
|
16
|
-
this.sent = true
|
|
17
|
-
|
|
18
11
|
this._promise = new Promise((resolve, reject) => {
|
|
19
12
|
this._resolve = resolve
|
|
20
13
|
this._reject = reject
|
|
21
14
|
})
|
|
22
15
|
|
|
23
|
-
|
|
16
|
+
this._requestStream = null
|
|
17
|
+
this._responseStream = null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
send (data) {
|
|
21
|
+
if (this.sent) throw errors.ALREADY_SENT('Request has already been sent')
|
|
22
|
+
this.sent = true
|
|
23
|
+
|
|
24
|
+
this.rpc._sendRequest(this, typeof data === 'string' ? b4a.from(data) : data)
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
reply () {
|
|
27
28
|
return this._promise
|
|
28
29
|
}
|
|
30
|
+
|
|
31
|
+
createRequestStream (opts = {}) {
|
|
32
|
+
if (this._requestStream === null) this.rpc._createRequestStream(this, true, opts)
|
|
33
|
+
|
|
34
|
+
return this._requestStream
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
createResponseStream (opts = {}) {
|
|
38
|
+
if (this._requestStream === null) this.rpc._createResponseStream(this, false, opts)
|
|
39
|
+
|
|
40
|
+
return this._responseStream
|
|
41
|
+
}
|
|
29
42
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const { Writable } = require('bare-stream')
|
|
2
|
+
const { type: t, stream: s } = require('./constants')
|
|
3
|
+
|
|
4
|
+
module.exports = class RPCOutgoingStream extends Writable {
|
|
5
|
+
constructor (rpc, request, type, opts) {
|
|
6
|
+
super({ ...opts, eagerOpen: true })
|
|
7
|
+
|
|
8
|
+
this._rpc = rpc
|
|
9
|
+
this._request = request
|
|
10
|
+
this._type = type
|
|
11
|
+
this._mask = type === t.REQUEST ? s.REQUEST : s.RESPONSE
|
|
12
|
+
|
|
13
|
+
this._pendingOpen = null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
_open (cb) {
|
|
17
|
+
this._pendingOpen = cb
|
|
18
|
+
|
|
19
|
+
switch (this._type) {
|
|
20
|
+
case t.REQUEST:
|
|
21
|
+
this._rpc._sendMessage({
|
|
22
|
+
type: t.REQUEST,
|
|
23
|
+
id: this._request.id,
|
|
24
|
+
command: this._request.command,
|
|
25
|
+
stream: s.OPEN,
|
|
26
|
+
data: null
|
|
27
|
+
})
|
|
28
|
+
break
|
|
29
|
+
|
|
30
|
+
case t.RESPONSE:
|
|
31
|
+
this._rpc._sendMessage({
|
|
32
|
+
type: t.RESPONSE,
|
|
33
|
+
id: this._request.id,
|
|
34
|
+
error: false,
|
|
35
|
+
stream: s.OPEN,
|
|
36
|
+
data: null
|
|
37
|
+
})
|
|
38
|
+
break
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_continueOpen () {
|
|
43
|
+
if (this._pendingOpen === null) return
|
|
44
|
+
const cb = this._pendingOpen
|
|
45
|
+
this._pendingOpen = null
|
|
46
|
+
cb()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_write (data, encoding, cb) {
|
|
50
|
+
this._rpc._sendMessage({
|
|
51
|
+
type: t.STREAM,
|
|
52
|
+
id: this._request.id,
|
|
53
|
+
stream: this._mask | s.DATA,
|
|
54
|
+
error: null,
|
|
55
|
+
data
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
cb(null)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_final (cb) {
|
|
62
|
+
this._rpc._sendMessage({
|
|
63
|
+
type: t.STREAM,
|
|
64
|
+
id: this._request.id,
|
|
65
|
+
stream: this._mask | s.END,
|
|
66
|
+
error: null,
|
|
67
|
+
data: null
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
cb(null)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
_destroy (err, cb) {
|
|
74
|
+
if (err) {
|
|
75
|
+
this._rpc._sendMessage({
|
|
76
|
+
type: t.STREAM,
|
|
77
|
+
id: this._request.id,
|
|
78
|
+
stream: this._mask | s.CLOSE | s.ERROR,
|
|
79
|
+
error: err,
|
|
80
|
+
data: null
|
|
81
|
+
})
|
|
82
|
+
} else {
|
|
83
|
+
this._rpc._sendMessage({
|
|
84
|
+
type: t.STREAM,
|
|
85
|
+
id: this._request.id,
|
|
86
|
+
stream: this._mask | s.CLOSE,
|
|
87
|
+
error: null,
|
|
88
|
+
data: null
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
cb(null)
|
|
93
|
+
}
|
|
94
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-rpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "librpc ABI compatible RPC for Bare",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
@@ -25,11 +25,12 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://github.com/holepunchto/bare-rpc#readme",
|
|
27
27
|
"dependencies": {
|
|
28
|
+
"b4a": "^1.6.6",
|
|
29
|
+
"bare-stream": "^2.1.3",
|
|
28
30
|
"compact-encoding": "^2.15.0",
|
|
29
31
|
"safety-catch": "^1.0.2"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {
|
|
32
|
-
"bare-stream": "^2.1.3",
|
|
33
34
|
"brittle": "^3.2.1",
|
|
34
35
|
"standard": "^17.0.0"
|
|
35
36
|
}
|