Skip to content

Commit 2e8b394

Browse files
committed
jsonrpc: correctly handle non-array responses
1 parent 0a7301a commit 2e8b394

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,29 +261,39 @@ export class JsonrpcProvider implements JsonrpcInterface {
261261
}
262262
private async batchProcess() {
263263
await nextTick(); // this allows to collect as much requests as we can in single tick
264-
const cur = this.queue.splice(0, this.batchSize);
265-
if (!cur.length) return;
264+
const curr = this.queue.splice(0, this.batchSize);
265+
if (!curr.length) return;
266266
const json = await this.fetchJson(
267-
cur.map((i, j) => ({
267+
curr.map((i, j) => ({
268268
jsonrpc: '2.0',
269269
id: j,
270270
method: i.method,
271271
params: i.params,
272272
}))
273273
);
274+
if (!Array.isArray(json)) {
275+
const hasMsg = json.code && json.message;
276+
curr.forEach((req, index) => {
277+
const err = hasMsg
278+
? this.jsonError(json)
279+
: new Error('invalid response in batch request ' + index);
280+
req.reject(err);
281+
});
282+
return;
283+
}
274284
const processed = new Set();
275285
for (const res of json) {
276286
// Server sent broken ids. We cannot throw error here, since we will have unresolved promises
277287
// Also, this will break app state.
278-
if (!Number.isSafeInteger(res.id) || res.id < 0 || res.id >= cur.length) continue;
288+
if (!Number.isSafeInteger(res.id) || res.id < 0 || res.id >= curr.length) continue;
279289
if (processed.has(res.id)) continue; // multiple responses for same id
280-
const { reject, resolve } = cur[res.id];
290+
const { reject, resolve } = curr[res.id];
281291
processed.add(res.id);
282292
if (res && res.error) reject(this.jsonError(res.error));
283293
else resolve(res.result);
284294
}
285-
for (let i = 0; i < cur.length; i++) {
286-
if (!processed.has(i)) cur[i].reject(new Error(`response missing in batch request`));
295+
for (let i = 0; i < curr.length; i++) {
296+
if (!processed.has(i)) curr[i].reject(new Error(`response missing in batch request ` + i));
287297
}
288298
}
289299
private rpcBatch(method: string, params: RpcParams) {

0 commit comments

Comments
 (0)