json-rpc-engine
a tool for processing JSON RPC
Last updated 8 years ago by kumavis .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ npm install json-rpc-engine 
SYNC missed versions from official npm registry.

json-rpc-engine

A tool for processing JSON-RPC requests and responses.

Usage

const { JsonRpcEngine } = require('json-rpc-engine')

let engine = new JsonRpcEngine()

Build a stack of JSON-RPC processors by pushing middleware to the engine.

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

Requests are handled asynchronously, stepping down the stack until complete.

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

engine.handle(request, function(err, response){
  // Do something with response.result, or handle response.error
})

// There is also a Promise signature
const response = await engine.handle(request)

Middleware have direct access to the request and response objects. They 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)
  })
})

Engines can be nested by converting them to middleware using JsonRpcEngine.asMiddleware():

const engine = new JsonRpcEngine()
const subengine = new JsonRpcEngine()
engine.push(subengine.asMiddleware())

async Middleware

If you require your middleware function to be async, use createAsyncMiddleware:

const { createAsyncMiddleware } = require('json-rpc-engine')

let engine = new RpcEngine()
engine.push(createAsyncMiddleware(async (req, res, next) => {
  res.result = 42
  next()
}))

async middleware do not take an end callback. Instead, the request ends if the middleware returns without calling next():

engine.push(createAsyncMiddleware(async (req, res, next) => {
  res.result = 42
  /* The request will end when this returns */
}))

The next callback of async middleware also don't take return handlers. Instead, you can await next(). When the execution of the middleware resumes, you can work with the response again.

engine.push(createAsyncMiddleware(async (req, res, next) => {
  res.result = 42
  await next()
  /* Your return handler logic goes here */
  addToMetrics(res)
}))

You can freely mix callback-based and async middleware:

engine.push(function(req, res, next, end){
  if (!isCached(req)) {
    return next((cb) => {
      insertIntoCache(res, cb)
    })
  }
  res.result = getResultFromCache(req)
  end()
})

engine.push(createAsyncMiddleware(async (req, res, next) => {
  res.result = 42
  await next()
  addToMetrics(res)
}))

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())
})

However, next() will detect errors on the response object, and cause end(res.error) to be called.

engine.push(function(req, res, next, end){
  res.error = new Error()
  next() /* This will cause end(res.error) to be called. */
})

Current Tags

  • 6.1.0                                ...           latest (4 years ago)

36 Versions

  • 6.1.0                                ...           4 years ago
  • 6.0.0                                ...           4 years ago
  • 5.4.0                                ...           4 years ago
  • 5.3.0                                ...           4 years ago
  • 5.2.0                                ...           4 years ago
  • 5.1.8                                ...           5 years ago
  • 5.1.6                                ...           5 years ago
  • 5.1.5                                ...           5 years ago
  • 5.1.4                                ...           5 years ago
  • 5.1.3                                ...           5 years ago
  • 5.1.1                                ...           5 years ago
  • 5.1.0                                ...           5 years ago
  • 5.0.0                                ...           6 years ago
  • 4.0.0                                ...           6 years ago
  • 3.8.0                                ...           6 years ago
  • 3.7.4                                ...           6 years ago
  • 3.7.3                                ...           6 years ago
  • 3.7.2                                ...           6 years ago
  • 3.7.1                                ...           6 years ago
  • 3.7.0                                ...           6 years ago
  • 3.6.3                                ...           7 years ago
  • 3.6.2                                ...           7 years ago
  • 3.6.1                                ...           7 years ago
  • 3.6.0                                ...           7 years ago
  • 3.5.0                                ...           7 years ago
  • 3.4.0                                ...           7 years ago
  • 3.3.1                                ...           7 years ago
  • 3.3.0                                ...           7 years ago
  • 3.2.0                                ...           7 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.1                                ...           7 years ago
  • 3.0.0                                ...           8 years ago
  • 2.2.0                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE