This library provides a basic json rpc wrapper for any object class that needs to be exposed to the network. It builds a proxy with method validations and argument processing arround any variable that has methods. The provided library is infrastructure agnostic so it can be used on any possible transport layer.
npm install --save json-rpc-wrapper
npm run test
Wrapping a simple object through json rpc protocol would be similar to this:
const { RpcWrapper } = require('json-rpc-wrapper')
const myService = {
add: (...params) => params.reduce((acc, curr) => acc + curr, 0),
subtract: (a, b) => a - b,
div: ({a, b}) => a / b
}
const rpcProxy = new RpcWrapper(myService)
const req = '{"jsonrpc":"2.0","method":"add","params":[1,2,3],"id":"1"}' // data received through some protocol
const res = await rpcProxy.callReq(req) // {jsonrpc:'2.0',id:'1',result:6}
const json = JSON.stringify(res) // or res.toString() -> '{"jsonrpc":"2.0","id":"1","result":6}'
The wrapper also supports functions with callbacks, we just have to specify which methods expect callback functions:
const service = {
storeContent: (content, cb) => {
fs.writeFile('test.log', content + '\n', { flag: 'a' }, (err, res) => {
if (err) return cb(err)
return cb(null, true)
})
}
}
const rpcProxy = new RpcWrapper(service)
const req = '{"jsonrpc":"2.0","method":"storeContent","params":["some content"],"id":56}'
const res = await rpcProxy.callReq(req) // {jsonrpc:'2.0',id:56,result:true}
In additional wrapper also works with promises and async/await functions without an issue:
const service = {
store: async (order) => {
await storeOrder(order)
await updateStocks(order.products)
return true
}
}
const rpcProxy = new RpcWrapper(service)
const req = '{"jsonrpc":"2.0","method":"storeContent","params":{"user":1,"products":[{"id":1,"name":"p1"},{"id":2,"name":"p2"}]},"id":"xgsdoigh-dsgh-sdgjlsgj"}'
const res = await rpcProxy.callReq(req) // {jsonrpc:'2.0',id:'xgsdoigh-dsgh-sdgjlsgj',result:true}
Wrapper works also with any type of class instance without a problem. A special type of class that can be used is RpcServiceBase
abstract class that provides also method parameter validation during method invocation. Here's a quick example of usage of this class:
class ProductService extends RpcServiceBase {
constructor (products = []) {
super()
this.methods.push('create', 'find')
this.products = products
}
create (item) { return this.products.push(item) }
find (id) { return this.products.find(p => p.id === id) }
remove (id) { this.products = this.products.filter(p => p.id !== id) }
async areParamsValid (method, params) {
switch (method) {
case 'create':
if (!params.id || typeof params.id !== 'number') return false
if (!params.name || typeof params.name !== 'string') return false
return true
case 'find':
return params[0] && typeof params[0] === 'number'
default: return false
}
}
}
const myService = new ProductService([ { id: 1, name: 'Product 1' }, { id: 2, name: 'Product 2' } ])
const rpcProxy = new RpcWrapper(myService)
const req = JSON.stringify([
{ "jsonrpc": "2.0", "method": "create", "params": { "id": 4, "name": "Product 4" }, "id": "1" },
{ "jsonrpc": "2.0", "method": "remove", "params": [ 2 ], "id": "2" },
{ "jsonrpc": "2.0", "method": "find", "params": [ 1 ], "id": "3" },
{ "jsonrpc": "2.0", "method": "create", "params": { "id": 5, "name": "Product 4" } },
{ "jsonrpc": "2.0", "method": "find", "params": [ "abc" ], "id": 4 }
])
const rpcRes = await rpcProxy.callReq(req)
// [
// { "jsonrpc": "2.0", "id": "1", "result": null },
// { "jsonrpc": "2.0", "id": "2", "result": null },
// { "jsonrpc": "2.0", "id": "3", "result": { "id": 1, "name": "Product 1" } },
// { "jsonrpc": "2.0", "id": 4, "error": { "code": -32602, "message": "Invalid params" } }
// ]
const net = require('net')
const { RpcWrapper } = require('json-rpc-wrapper')
const service = {
ping: () => Date.now(),
echo: (params) => params
}
const rpcProxy = new RpcWrapper(service)
const server = net.createServer({ allowHalfOpen: true})
server.on('connection', async (socket) => {
socket.on('data', (data) => {
const raw = data.toString('utf-8')
const res = await rpcProxy.callReq(raw)
socket.write(JSON.stringify(res), () => socket.end())
})
})
server.listen(7070, () => {
console.log(`TCP server is listening on ::7070`)
})
const http = require('http')
const { RpcWrapper } = require('json-rpc-wrapper')
const service = {
ping: () => Date.now(),
echo: (params) => params
}
const rpcProxy = new RpcWrapper(service)
const server = http.createServer((req, res) => {
const chunks = []
req.on('data', (data) => {
chunks.push(data.toString('utf-8'))
})
req.on('end', async () => {
const payload = chunks.join('')
const rpcRes = await rpcProxy.callReq(payload)
res.setHeader('Content-Type', 'application/json')
res.write(JSON.stringify(rpcRes))
res.end()
})
})
server.listen(8080, () => {
console.log(`HTTP server is listening on 8080`)
})
The library can also be used directly into typescript without needing to install types package separately, all declaration files can be found in @types folder. Usage example:
import http = require('http')
import { RpcWrapper, RpcServiceBase } from 'json-rpc-wrapper';
interface Product { id: number; name: string; }
class ProductService extends RpcServiceBase {
protected products: Array<Product>
constructor(products: Array<Product> = []) {
super()
this.methods.push('create')
this.products = products
}
create(item: Product) { this.products.push(item) }
areParamsValid(method: string, params: any) {
switch (method) {
case 'create':
if (!params.id || typeof params.id !== 'number') return false
if (!params.name || typeof params.name !== 'string') return false
return true
default: return false
}
}
}
const myService = new ProductService([{ id: 1, name: 'Product 1' }])
const rpcProxy = new RpcWrapper(myService)
const server = http.createServer((req, res) => {
const chunks: Array<string> = []
req.on('data', (data) => {
chunks.push(data.toString('utf-8'))
})
req.on('end', async () => {
const payload = chunks.join('')
const rpcRes = await rpcProxy.callReq(payload)
res.write(JSON.stringify(rpcRes))
res.end()
})
})
server.listen(8080, () => {
console.log(`HTTP server is listening on ::8080`)
})
Additional detailed examples can be found in examples folder.
Also full documentation related classes can be found in docs/DEFINITIONS.md
- vigan-abd (vigan.abd@gmail.com)