json-rpc-engine
Advanced tools
Comparing version 1.0.0 to 2.0.0
119
index.js
const async = require('async') | ||
const inherits = require('util').inherits | ||
module.exports = RpcEngine | ||
class RpcEngine { | ||
inherits(RpcEngine, Array) | ||
constructor () { | ||
this._middleware = [] | ||
} | ||
function RpcEngine(){} | ||
// | ||
// Public | ||
// | ||
RpcEngine.prototype.asMiddleware = function(){ | ||
const self = this | ||
return function engineAsMiddleware(req, res, next, end){ | ||
self.handle(req, function(err, engineRes){ | ||
if (err) return end(err) | ||
// copy engine result onto response | ||
res.result = engineRes.result | ||
end() | ||
}) | ||
push (middleware) { | ||
this._middleware.push(middleware) | ||
} | ||
} | ||
RpcEngine.prototype.handle = function(req, cb) { | ||
const self = this | ||
// batch request support | ||
if (Array.isArray(req)) { | ||
async.map(req, self._handle.bind(self), cb) | ||
} else { | ||
self._handle(req, cb) | ||
handle (req, cb) { | ||
// batch request support | ||
if (Array.isArray(req)) { | ||
async.map(req, this._handle.bind(this), cb) | ||
} else { | ||
this._handle(req, cb) | ||
} | ||
} | ||
} | ||
RpcEngine.prototype._handle = function(req, cb) { | ||
const self = this | ||
// create response obj | ||
const res = { | ||
id: req.id, | ||
jsonrpc: req.jsonrpc, | ||
} | ||
// pointer for the stack | ||
let middlewareIndex = 0 | ||
// for climbing back up the stack | ||
let returnHandlers = [] | ||
// flag for stack return | ||
let isComplete = false | ||
// | ||
// Private | ||
// | ||
// down stack of middleware, call and collect optional returnHandlers | ||
async.mapSeries(self, function eachMiddleware(middleware, cb){ | ||
if (isComplete) return cb() | ||
middleware(req, res, next, end) | ||
function next(returnHandler){ | ||
// add return handler | ||
returnHandlers.push(returnHandler) | ||
cb() | ||
_handle (req, cb) { | ||
// create response obj | ||
const res = { | ||
id: req.id, | ||
jsonrpc: req.jsonrpc, | ||
} | ||
function end(err){ | ||
// pointer for the stack | ||
let middlewareIndex = 0 | ||
// for climbing back up the stack | ||
let returnHandlers = [] | ||
// flag for stack return | ||
let isComplete = false | ||
// down stack of middleware, call and collect optional returnHandlers | ||
async.mapSeries(this._middleware, function eachMiddleware(middleware, cb){ | ||
if (isComplete) return cb() | ||
middleware(req, res, next, end) | ||
function next(returnHandler){ | ||
// add return handler | ||
returnHandlers.push(returnHandler) | ||
cb() | ||
} | ||
function end(err){ | ||
if (err) return cb(err) | ||
isComplete = true | ||
cb() | ||
} | ||
}, runReturnHandlers) | ||
// climbs the stack calling return handlers | ||
function runReturnHandlers(err){ | ||
if (err) return cb(err) | ||
isComplete = true | ||
cb() | ||
if (!isComplete) return cb(new Error('RpcEngine - nothing ended request')) | ||
let backStack = returnHandlers.filter(Boolean).reverse() | ||
async.eachSeries(backStack, function(handler, next){ | ||
handler(next) | ||
}, completeRequest) | ||
} | ||
}, runReturnHandlers) | ||
// climbs the stack calling return handlers | ||
function runReturnHandlers(err){ | ||
if (err) return cb(err) | ||
if (!isComplete) return cb(new Error('RpcEngine - nothing ended request')) | ||
let backStack = returnHandlers.filter(Boolean).reverse() | ||
async.eachSeries(backStack, function(handler, next){ | ||
handler(next) | ||
}, completeRequest) | ||
} | ||
// returns the result | ||
function completeRequest(){ | ||
cb(null, res) | ||
} | ||
// returns the result | ||
function completeRequest(){ | ||
cb(null, res) | ||
} | ||
} | ||
module.exports = RpcEngine |
{ | ||
"name": "json-rpc-engine", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "a tool for processing JSON RPC", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "node test/basic.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "author": "", |
@@ -14,3 +14,2 @@ # RpcEngine | ||
Build a stack of json rpc processors by pushing in RpcEngine middleware. | ||
RpcEngine is a subclass of Array so you can perform normal array manipulation functions. | ||
@@ -17,0 +16,0 @@ ```js |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
9189
8
209
75
1