json-rpc-engine
Advanced tools
Comparing version 5.1.5 to 5.1.6
{ | ||
"name": "json-rpc-engine", | ||
"version": "5.1.5", | ||
"version": "5.1.6", | ||
"description": "a tool for processing JSON RPC", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
'use strict' | ||
const async = require('async') | ||
const SafeEventEmitter = require('safe-event-emitter') | ||
const { serializeError, ethErrors } = require('eth-json-rpc-errors') | ||
const { | ||
serializeError, EthereumRpcError, ERROR_CODES | ||
} = require('eth-json-rpc-errors') | ||
@@ -34,27 +36,26 @@ class RpcEngine extends SafeEventEmitter { | ||
async _handleBatch (reqs, cb) { | ||
const batchRes = [] | ||
for (const r of reqs) { | ||
try { | ||
let [err, res] = await this._promiseHandle(r) | ||
if (!res) { | ||
if (err) { | ||
throw err | ||
} else { | ||
throw ethErrors.rpc.internal('JsonRpcEngine: Request handler returned neither error nor response.') | ||
} | ||
} else { | ||
batchRes.push(res) | ||
} | ||
} catch (_err) { | ||
// some kind of fatal error | ||
return cb(_err, null) | ||
} | ||
// The order here is important | ||
try { | ||
const batchRes = await Promise.all( // 2. Wait for all requests to finish | ||
// 1. Begin executing each request in the order received | ||
reqs.map(this._promiseHandle.bind(this)) | ||
) | ||
cb(null, batchRes) // 3a. Return batch response | ||
} catch (err) { | ||
cb(err) // 3b. Some kind of fatal error; all requests are lost | ||
} | ||
cb(null, batchRes) | ||
} | ||
_promiseHandle (req) { | ||
return new Promise((resolve) => { | ||
return new Promise((resolve, reject) => { | ||
this._handle(req, (err, res) => { | ||
resolve([err, res]) | ||
if (!res) { // defensive programming | ||
reject(err || new EthereumRpcError( | ||
ERROR_CODES.rpc.internal, | ||
'JsonRpcEngine: Request handler returned neither error nor response.' | ||
)) | ||
} else { | ||
resolve(res) | ||
} | ||
}) | ||
@@ -101,3 +102,5 @@ }) | ||
const message = 'JsonRpcEngine: Response has no error or result for request:\n' + requestBody | ||
return cb(new Error(message)) | ||
return cb(new EthereumRpcError( | ||
ERROR_CODES.rpc.internal, message, req | ||
)) | ||
} | ||
@@ -107,3 +110,5 @@ if (!isComplete) { | ||
const message = 'JsonRpcEngine: Nothing ended request:\n' + requestBody | ||
return cb(new Error(message)) | ||
return cb(new EthereumRpcError( | ||
ERROR_CODES.rpc.internal, message, req | ||
)) | ||
} | ||
@@ -110,0 +115,0 @@ // continue |
@@ -1,2 +0,1 @@ | ||
const each = require('async/each') | ||
const JsonRpcEngine = require('./index') | ||
@@ -3,0 +2,0 @@ const asMiddleware = require('./asMiddleware') |
@@ -157,6 +157,6 @@ /* eslint-env mocha */ | ||
engine.push(function (req, res, next, end) { | ||
engine.push(function (req, res, _next, end) { | ||
if (req.id === 4) { | ||
delete res.result | ||
res.error = new Error() | ||
res.error = new Error('foobar') | ||
return end(res.error) | ||
@@ -163,0 +163,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33841
933
1