@makeomatic/json-rpc-engine
a tool for processing JSON RPC
Last updated a year ago by avvs .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @makeomatic/json-rpc-engine 
SYNC missed versions from official npm registry.

RpcEngine

a tool for processing JSON RPC

usage

const RpcEngine = require('json-rpc-engine')

let engine = new RpcEngine()

Build a stack of json rpc processors by pushing in RpcEngine middleware.

engine.push(function(req, res, next, end){
  res.result = 42
  end()
})

JSON RPC are handled asynchronously, stepping down the stack until complete.

let request = { id: 1, jsonrpc: '2.0', method: 'hello' }

engine.handle(request, function(err, res){
  // do something with res.result
})

RpcEngine middleware has direct access to the request and response objects. It can let processing continue down the stack with next() or complete the request with end().

engine.push(function(req, res, next, end){
  if (req.skipCache) return next()
  res.result = getResultFromCache(req)
  end()
})

By passing a 'return handler' to the next function, you can get a peek at the result before it returns.

engine.push(function(req, res, next, end){
  next(function(cb){
    insertIntoCache(res, cb)
  })
})

RpcEngines can be nested by converting them to middleware asMiddleware(engine)

const asMiddleware = require('json-rpc-engine/lib/asMiddleware')

let engine = new RpcEngine()
let subengine = new RpcEngine()
engine.push(asMiddleware(subengine))

gotchas

Handle errors via end(err), NOT next(err).

/* INCORRECT */
engine.push(function(req, res, next, end){
  next(new Error())
})

/* CORRECT */
engine.push(function(req, res, next, end){
  end(new Error())
})

Current Tags

  • 5.1.2                                ...           latest (a year ago)

3 Versions

  • 5.1.2                                ...           a year ago
  • 5.1.1                                ...           a year ago
  • 3.7.4                                ...           2 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |