Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
json-rpc-engine
Advanced tools
The json-rpc-engine npm package is a JavaScript library that provides a framework for building JSON-RPC 2.0 servers and clients. It allows you to create middleware stacks to handle JSON-RPC requests and responses, making it easier to manage and extend your JSON-RPC services.
Creating a JSON-RPC Engine
This feature allows you to create a new JSON-RPC engine instance, which serves as the core component for handling JSON-RPC requests and responses.
const { JsonRpcEngine } = require('json-rpc-engine');
const engine = new JsonRpcEngine();
Adding Middleware
You can add middleware to the JSON-RPC engine to handle specific methods or perform actions before passing the request to the next middleware. In this example, a middleware is added to handle the 'hello' method.
engine.push((req, res, next, end) => {
if (req.method === 'hello') {
res.result = 'world';
return end();
}
next();
});
Handling Requests
This feature demonstrates how to handle a JSON-RPC request using the engine. The request is processed through the middleware stack, and the response is returned via a callback function.
const request = { jsonrpc: '2.0', method: 'hello', id: 1 };
engine.handle(request, (err, res) => {
if (err) {
console.error(err);
} else {
console.log(res);
}
});
Jayson is a full-featured JSON-RPC 2.0/1.0 server and client for Node.js. It provides a more comprehensive set of features for building JSON-RPC services, including support for both HTTP and TCP transports, batch requests, and more. Compared to json-rpc-engine, Jayson offers a more extensive set of tools for different transport layers and use cases.
Jsonrpc-lite is a lightweight JSON-RPC 2.0 library for Node.js. It focuses on providing a simple and minimalistic approach to handling JSON-RPC requests and responses. While json-rpc-engine provides a middleware-based architecture, jsonrpc-lite is more straightforward and easier to use for basic JSON-RPC implementations.
Node-json-rpc is another JSON-RPC 2.0 server and client library for Node.js. It offers a simple API for creating JSON-RPC servers and clients, with support for both HTTP and WebSocket transports. Compared to json-rpc-engine, node-json-rpc provides a more traditional approach to building JSON-RPC services without the middleware stack.
a tool for processing JSON RPC
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))
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())
})
FAQs
A tool for processing JSON-RPC messages.
The npm package json-rpc-engine receives a total of 275,107 weekly downloads. As such, json-rpc-engine popularity was classified as popular.
We found that json-rpc-engine demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.