Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-rpc-engine

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-rpc-engine - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

lib/asMiddleware.js

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc