bare-rpc 1.1.0 → 1.3.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 +2 -11
- package/index.js +12 -45
- package/lib/messages.js +3 -5
- package/lib/outgoing-request.js +1 -3
- package/package.json +9 -4
package/index.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import Buffer, { BufferEncoding } from 'bare-buffer'
|
|
2
|
-
import {
|
|
3
|
-
Readable,
|
|
4
|
-
ReadableOptions,
|
|
5
|
-
Writable,
|
|
6
|
-
WritableOptions,
|
|
7
|
-
Duplex
|
|
8
|
-
} from 'bare-stream'
|
|
2
|
+
import { Readable, ReadableOptions, Writable, WritableOptions, Duplex } from 'bare-stream'
|
|
9
3
|
|
|
10
4
|
declare const constants: {
|
|
11
5
|
type: { REQUEST: 1; RESPONSE: 2; STREAM: 3 }
|
|
@@ -106,10 +100,7 @@ interface RPC {
|
|
|
106
100
|
}
|
|
107
101
|
|
|
108
102
|
declare class RPC {
|
|
109
|
-
constructor(
|
|
110
|
-
stream: Duplex,
|
|
111
|
-
onrequest: (req: RPCIncomingRequest) => void | Promise<void>
|
|
112
|
-
)
|
|
103
|
+
constructor(stream: Duplex, onrequest: (req: RPCIncomingRequest) => void | Promise<void>)
|
|
113
104
|
}
|
|
114
105
|
|
|
115
106
|
declare namespace RPC {
|
package/index.js
CHANGED
|
@@ -39,10 +39,7 @@ module.exports = exports = class RPC {
|
|
|
39
39
|
this._ondata = this._ondata.bind(this)
|
|
40
40
|
this._ondrain = this._ondrain.bind(this)
|
|
41
41
|
|
|
42
|
-
this._stream
|
|
43
|
-
.on('error', this._onerror)
|
|
44
|
-
.on('data', this._ondata)
|
|
45
|
-
.on('drain', this._ondrain)
|
|
42
|
+
this._stream.on('error', this._onerror).on('data', this._ondata).on('drain', this._ondrain)
|
|
46
43
|
}
|
|
47
44
|
|
|
48
45
|
event(command) {
|
|
@@ -92,25 +89,13 @@ module.exports = exports = class RPC {
|
|
|
92
89
|
if (isInitiator) {
|
|
93
90
|
this._outgoingRequests.set(request.id, request)
|
|
94
91
|
|
|
95
|
-
request._requestStream = new OutgoingStream(
|
|
96
|
-
this,
|
|
97
|
-
request,
|
|
98
|
-
t.REQUEST,
|
|
99
|
-
opts
|
|
100
|
-
)
|
|
92
|
+
request._requestStream = new OutgoingStream(this, request, t.REQUEST, opts)
|
|
101
93
|
} else {
|
|
102
94
|
this._incomingRequests.set(request.id, request)
|
|
103
95
|
|
|
104
|
-
request._requestStream = new IncomingStream(
|
|
105
|
-
this,
|
|
106
|
-
request,
|
|
107
|
-
t.REQUEST,
|
|
108
|
-
opts
|
|
109
|
-
)
|
|
96
|
+
request._requestStream = new IncomingStream(this, request, t.REQUEST, opts)
|
|
110
97
|
|
|
111
|
-
request._requestStream.on('close', () =>
|
|
112
|
-
this._incomingRequests.delete(request.id)
|
|
113
|
-
)
|
|
98
|
+
request._requestStream.on('close', () => this._incomingRequests.delete(request.id))
|
|
114
99
|
}
|
|
115
100
|
}
|
|
116
101
|
|
|
@@ -128,25 +113,13 @@ module.exports = exports = class RPC {
|
|
|
128
113
|
if (isInitiator) {
|
|
129
114
|
this._outgoingResponses.set(request.id, request)
|
|
130
115
|
|
|
131
|
-
request._responseStream = new OutgoingStream(
|
|
132
|
-
this,
|
|
133
|
-
request,
|
|
134
|
-
t.RESPONSE,
|
|
135
|
-
opts
|
|
136
|
-
)
|
|
116
|
+
request._responseStream = new OutgoingStream(this, request, t.RESPONSE, opts)
|
|
137
117
|
} else {
|
|
138
118
|
this._incomingResponses.set(request.id, request)
|
|
139
119
|
|
|
140
|
-
request._responseStream = new IncomingStream(
|
|
141
|
-
this,
|
|
142
|
-
request,
|
|
143
|
-
t.RESPONSE,
|
|
144
|
-
opts
|
|
145
|
-
)
|
|
120
|
+
request._responseStream = new IncomingStream(this, request, t.RESPONSE, opts)
|
|
146
121
|
|
|
147
|
-
request._responseStream.on('close', () =>
|
|
148
|
-
this._incomingResponses.delete(request.id)
|
|
149
|
-
)
|
|
122
|
+
request._responseStream.on('close', () => this._incomingResponses.delete(request.id))
|
|
150
123
|
}
|
|
151
124
|
}
|
|
152
125
|
|
|
@@ -180,8 +153,7 @@ module.exports = exports = class RPC {
|
|
|
180
153
|
_onbeforeframe() {
|
|
181
154
|
if (this._buffered < 4) return
|
|
182
155
|
|
|
183
|
-
const buffer =
|
|
184
|
-
this._buffer.length === 1 ? this._buffer[0] : b4a.concat(this._buffer)
|
|
156
|
+
const buffer = this._buffer.length === 1 ? this._buffer[0] : b4a.concat(this._buffer)
|
|
185
157
|
|
|
186
158
|
this._buffer = [buffer]
|
|
187
159
|
this._frame = 4 + c.uint32.decode(c.state(0, 4, buffer))
|
|
@@ -192,8 +164,7 @@ module.exports = exports = class RPC {
|
|
|
192
164
|
_onafterframe() {
|
|
193
165
|
if (this._buffered < this._frame) return
|
|
194
166
|
|
|
195
|
-
const buffer =
|
|
196
|
-
this._buffer.length === 1 ? this._buffer[0] : b4a.concat(this._buffer)
|
|
167
|
+
const buffer = this._buffer.length === 1 ? this._buffer[0] : b4a.concat(this._buffer)
|
|
197
168
|
|
|
198
169
|
const frame = this._frame
|
|
199
170
|
|
|
@@ -216,16 +187,11 @@ module.exports = exports = class RPC {
|
|
|
216
187
|
}
|
|
217
188
|
|
|
218
189
|
switch (message.type) {
|
|
219
|
-
case t.REQUEST:
|
|
190
|
+
case t.REQUEST: {
|
|
220
191
|
const request =
|
|
221
192
|
message.id === 0
|
|
222
193
|
? new IncomingEvent(this, message.command, message.data)
|
|
223
|
-
: new IncomingRequest(
|
|
224
|
-
this,
|
|
225
|
-
message.id,
|
|
226
|
-
message.command,
|
|
227
|
-
message.data
|
|
228
|
-
)
|
|
194
|
+
: new IncomingRequest(this, message.id, message.command, message.data)
|
|
229
195
|
|
|
230
196
|
try {
|
|
231
197
|
await this._onrequest(request)
|
|
@@ -236,6 +202,7 @@ module.exports = exports = class RPC {
|
|
|
236
202
|
else this._stream.destroy(err)
|
|
237
203
|
}
|
|
238
204
|
break
|
|
205
|
+
}
|
|
239
206
|
case t.RESPONSE:
|
|
240
207
|
try {
|
|
241
208
|
this._onresponse(message)
|
package/lib/messages.js
CHANGED
|
@@ -95,10 +95,7 @@ exports.header = {
|
|
|
95
95
|
|
|
96
96
|
state.start = frame
|
|
97
97
|
|
|
98
|
-
c.uint32.encode(
|
|
99
|
-
state,
|
|
100
|
-
end - start + (hasData && m.data ? m.data.byteLength : 0)
|
|
101
|
-
)
|
|
98
|
+
c.uint32.encode(state, end - start + (hasData && m.data ? m.data.byteLength : 0))
|
|
102
99
|
|
|
103
100
|
state.start = end
|
|
104
101
|
}
|
|
@@ -137,7 +134,7 @@ exports.message = {
|
|
|
137
134
|
return { type, id, stream, error: null, data: null }
|
|
138
135
|
}
|
|
139
136
|
|
|
140
|
-
case t.STREAM:
|
|
137
|
+
case t.STREAM: {
|
|
141
138
|
const stream = c.uint.decode(state)
|
|
142
139
|
|
|
143
140
|
if (stream & s.ERROR) {
|
|
@@ -149,6 +146,7 @@ exports.message = {
|
|
|
149
146
|
}
|
|
150
147
|
|
|
151
148
|
return { type, id, stream, error: null, data: null }
|
|
149
|
+
}
|
|
152
150
|
|
|
153
151
|
default:
|
|
154
152
|
throw errors.UNKNOWN_MESSAGE(`Unknown message '${type}'`)
|
package/lib/outgoing-request.js
CHANGED
|
@@ -44,9 +44,7 @@ module.exports = class RPCOutgoingRequest {
|
|
|
44
44
|
|
|
45
45
|
this.received = true
|
|
46
46
|
|
|
47
|
-
return encoding
|
|
48
|
-
? this._promise.then((data) => c.decode(encoding, data))
|
|
49
|
-
: this._promise
|
|
47
|
+
return encoding ? this._promise.then((data) => c.decode(encoding, data)) : this._promise
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
createRequestStream(opts = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-rpc",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "librpc ABI compatible RPC for Bare",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
"default": "./index.js"
|
|
9
9
|
},
|
|
10
10
|
"./package": "./package.json",
|
|
11
|
+
"./constants": "./lib/constants.js",
|
|
11
12
|
"./errors": {
|
|
12
13
|
"types": "./lib/errors.d.ts",
|
|
13
14
|
"default": "./lib/errors.js"
|
|
14
|
-
}
|
|
15
|
+
},
|
|
16
|
+
"./messages": "./lib/messages.js"
|
|
15
17
|
},
|
|
16
18
|
"files": [
|
|
17
19
|
"index.js",
|
|
@@ -19,7 +21,9 @@
|
|
|
19
21
|
"lib"
|
|
20
22
|
],
|
|
21
23
|
"scripts": {
|
|
22
|
-
"
|
|
24
|
+
"format": "prettier --write . && lunte --fix",
|
|
25
|
+
"lint": "prettier --check . && lunte",
|
|
26
|
+
"test": "brittle-bare --coverage test.js"
|
|
23
27
|
},
|
|
24
28
|
"repository": {
|
|
25
29
|
"type": "git",
|
|
@@ -41,8 +45,9 @@
|
|
|
41
45
|
"bare-buffer": "^3.0.1",
|
|
42
46
|
"bare-ipc": "^1.1.0",
|
|
43
47
|
"brittle": "^3.2.1",
|
|
48
|
+
"lunte": "^1.7.0",
|
|
44
49
|
"prettier": "^3.4.2",
|
|
45
|
-
"prettier-config-
|
|
50
|
+
"prettier-config-holepunch": "^2.0.0"
|
|
46
51
|
},
|
|
47
52
|
"peerDependencies": {
|
|
48
53
|
"bare-buffer": "*"
|