npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonrpc-lite

v2.2.0

Published

Parse and Serialize JSON-RPC2 messages in node.js or browser.

Downloads

51,223

Readme

JSON-RPC lite

Parse and Serialize JSON-RPC2 messages in node.js or browser.

Inspired by https://github.com/soggie/jsonrpc-serializer

NPM version Build Status Downloads

A implementation of JSON-RPC 2.0 specifications

Install

npm install jsonrpc-lite

API

const jsonrpc = require('jsonrpc-lite')

jsonrpc.request(id, method[, params])

Creates a JSON-RPC 2.0 request object, return JsonRpc object.

  • id: {String|Integer}
  • method: {String}
  • params: {Object|Array}, optional
const requestObj = jsonrpc.request('123', 'update', {list: [1, 2, 3]})
// {
//   jsonrpc: '2.0',
//   id: '123',
//   method: 'update',
//   params: {list: [1, 2, 3]}
// }

jsonrpc.notification(method[, params])

Creates a JSON-RPC 2.0 notification object, return JsonRpc object.

  • method: {String}
  • params: {Object|Array}, optional
const notificationObj = jsonrpc.notification('update', {list: [1, 2, 3]})
// {
//   jsonrpc: '2.0',
//   method: 'update',
//   params: {list: [1, 2, 3]}
// }

jsonrpc.success(id, result)

Creates a JSON-RPC 2.0 success response object, return JsonRpc object.

  • id: {String|Integer}
  • result: {Mixed}
const successObj = jsonrpc.success('123', 'OK')
// {
//   jsonrpc: '2.0',
//   id: '123',
//   result: 'OK',
// }

jsonrpc.error(id, error)

Creates a JSON-RPC 2.0 error response object, return JsonRpc object.

  • id: {String|Integer}
  • error: {JsonRpcError}
const errorObj = jsonrpc.error('123', new jsonrpc.JsonRpcError('some error', 99))
// {
//   jsonrpc: '2.0',
//   id: '123',
//   error: {code: 99, 'message': 'some error'},
// }

jsonrpc.parse(message)

Takes a JSON-RPC 2.0 payload (string) and tries to parse it into a JSON. If successful, determine what object is it (response, notification, success, error, or invalid), and return it's type and properly formatted object.

  • message: {String}

return an array, or an object of this format:

single parsed request:

{
  type: 'request',
  payload: {
    jsonrpc: '2.0',
    id: 123,
    method: 'update',
    params: {}
  }
}

batch parsed result:

[{
  type: 'request',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    method: 'update',
    params: [1, 2, 3]
  }
}, {
  type: 'notification',
  payload: {
    jsonrpc: '2.0',
    method: 'update',
    params: {_id: 'xxx'}
  }
}, {
  type: 'success',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    result: 'OK'
  }
}, {
  type: 'error',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    error: [jsonrpc.JsonRpcError object]
  }
}, {
  type: 'invalid',
  payload: [jsonrpc.JsonRpcError object]
}]

jsonrpc.parseObject(message)

Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON. If successful, determine what object is it (response, notification, success, error, or invalid), and return it's type and properly formatted object.

  • message: {Object}

return an JsonRpcParsed object with type and payload.

Class: jsonrpc.JsonRpc()

Class: jsonrpc.JsonRpcError(message, code[, data])

Create a JsonRpcError instance.

  • message: {String}
  • code: {Integer}
  • data: {Mixed} optional
const error = new jsonrpc.JsonRpcError('some error', 999)

Class Method: jsonrpc.JsonRpcError.invalidRequest([data])

Class Method: jsonrpc.JsonRpcError.methodNotFound([data])

Class Method: jsonrpc.JsonRpcError.invalidParams([data])

Class Method: jsonrpc.JsonRpcError.internalError([data])

Class Method: jsonrpc.JsonRpcError.parseError([data])