bare-rpc 0.2.0 → 0.2.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 +76 -48
- package/lib/messages.js +1 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -22,7 +22,9 @@ module.exports = exports = class RPC {
|
|
|
22
22
|
this._pendingRequests = new Set()
|
|
23
23
|
this._pendingResponses = new Set()
|
|
24
24
|
|
|
25
|
-
this._buffer =
|
|
25
|
+
this._buffer = []
|
|
26
|
+
this._buffered = 0
|
|
27
|
+
this._frame = -1
|
|
26
28
|
|
|
27
29
|
if (typeof onrequest === 'function') {
|
|
28
30
|
onrequest = onrequest.bind(this)
|
|
@@ -129,58 +131,84 @@ module.exports = exports = class RPC {
|
|
|
129
131
|
}
|
|
130
132
|
|
|
131
133
|
_ondata(data) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
+
this._buffer.push(data)
|
|
135
|
+
this._buffered += data.byteLength
|
|
134
136
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
+
if (this._frame === -1) {
|
|
138
|
+
this._onbeforeframe()
|
|
139
|
+
} else {
|
|
140
|
+
this._onafterframe()
|
|
141
|
+
}
|
|
142
|
+
}
|
|
137
143
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
message = m.message.decode(state)
|
|
141
|
-
} catch (err) {
|
|
142
|
-
safetyCatch(err)
|
|
144
|
+
_onbeforeframe() {
|
|
145
|
+
if (this._buffered < 4) return
|
|
143
146
|
|
|
144
|
-
|
|
145
|
-
|
|
147
|
+
const buffer =
|
|
148
|
+
this._buffer.length === 1 ? this._buffer[0] : b4a.concat(this._buffer)
|
|
146
149
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
this._onrequest(request)
|
|
160
|
-
} catch (err) {
|
|
161
|
-
safetyCatch(err)
|
|
162
|
-
|
|
163
|
-
this._sendError(request, err)
|
|
164
|
-
}
|
|
165
|
-
break
|
|
166
|
-
}
|
|
167
|
-
case t.RESPONSE:
|
|
168
|
-
try {
|
|
169
|
-
this._onresponse(message)
|
|
170
|
-
} catch (err) {
|
|
171
|
-
safetyCatch(err)
|
|
172
|
-
}
|
|
173
|
-
break
|
|
174
|
-
case t.STREAM:
|
|
175
|
-
try {
|
|
176
|
-
this._onstream(message)
|
|
177
|
-
} catch (err) {
|
|
178
|
-
safetyCatch(err)
|
|
179
|
-
}
|
|
180
|
-
}
|
|
150
|
+
this._buffer = [buffer]
|
|
151
|
+
this._frame = 4 + c.uint32.decode(c.state(0, 4, buffer))
|
|
152
|
+
|
|
153
|
+
this._onafterframe()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
_onafterframe() {
|
|
157
|
+
while (this._frame >= 0 && this._frame <= this._buffered) {
|
|
158
|
+
const buffer =
|
|
159
|
+
this._buffer.length === 1 ? this._buffer[0] : b4a.concat(this._buffer)
|
|
160
|
+
|
|
161
|
+
const frame = this._frame
|
|
181
162
|
|
|
182
|
-
this.
|
|
183
|
-
|
|
163
|
+
this._buffered -= frame
|
|
164
|
+
this._buffer = this._buffered > 0 ? [buffer.subarray(frame)] : []
|
|
165
|
+
this._frame = -1
|
|
166
|
+
|
|
167
|
+
this._onmessage(buffer.subarray(0, frame))
|
|
168
|
+
this._onbeforeframe()
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
_onmessage(buffer) {
|
|
173
|
+
let message
|
|
174
|
+
try {
|
|
175
|
+
message = m.message.decode(c.state(0, buffer.length, buffer))
|
|
176
|
+
} catch (err) {
|
|
177
|
+
safetyCatch(err)
|
|
178
|
+
|
|
179
|
+
return this._stream.destroy(err)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
switch (message.type) {
|
|
183
|
+
case t.REQUEST:
|
|
184
|
+
const request = new IncomingRequest(
|
|
185
|
+
this,
|
|
186
|
+
message.id,
|
|
187
|
+
message.command,
|
|
188
|
+
message.data
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
this._onrequest(request)
|
|
193
|
+
} catch (err) {
|
|
194
|
+
safetyCatch(err)
|
|
195
|
+
|
|
196
|
+
this._sendError(request, err)
|
|
197
|
+
}
|
|
198
|
+
break
|
|
199
|
+
case t.RESPONSE:
|
|
200
|
+
try {
|
|
201
|
+
this._onresponse(message)
|
|
202
|
+
} catch (err) {
|
|
203
|
+
safetyCatch(err)
|
|
204
|
+
}
|
|
205
|
+
break
|
|
206
|
+
case t.STREAM:
|
|
207
|
+
try {
|
|
208
|
+
this._onstream(message)
|
|
209
|
+
} catch (err) {
|
|
210
|
+
safetyCatch(err)
|
|
211
|
+
}
|
|
184
212
|
}
|
|
185
213
|
}
|
|
186
214
|
|
package/lib/messages.js
CHANGED
|
@@ -142,11 +142,9 @@ exports.message = {
|
|
|
142
142
|
state.start = end
|
|
143
143
|
},
|
|
144
144
|
decode(state) {
|
|
145
|
-
if (state.end - state.start < 4) return null
|
|
146
|
-
|
|
147
145
|
const frame = c.uint32.decode(state)
|
|
148
146
|
|
|
149
|
-
if (state.end - state.start < frame)
|
|
147
|
+
if (state.end - state.start < frame) throw new RangeError('Out of bounds')
|
|
150
148
|
|
|
151
149
|
const type = c.uint.decode(state)
|
|
152
150
|
const id = c.uint.decode(state)
|