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 🙏

© 2025 – Pkg Stats / Ryan Hefner

jsonrpcserver

v1.1.0

Published

JSON-RPC Server (http://www.jsonrpc.org/specification) with endpoints implementation

Readme

JSON-RPC Server

Simple-to-use JSON-RPC Server with endpoints implementation (JSON-RPC Specification).

install

$ npm install jsonrpcserver --save

how to use

You can instantiate HTTP-server (default usage):

var logger = require('mylogger'),
    API = require('myapi'),
    JsonRpcServer = require('jsonrpcserver');
    
var rpcServerInstance = new JsonRpcServer(logger); //logger is an optional one

...

rpcServerInstance.register('/my/new/endpoint', {
    "context": new API(),
    "map": {
        "testMethod": {
            "handler": "test",
            "params": [
                {
                    "type": "string",
                    "required": true
                }
            ]
        },
        "yetAnotherMethod": {
            "handler": function(options, cb) {
                (typeof cb == 'function') && cb(null, options);
            },
            "params": {
                "first": {
                    "type": "number",
                    "required": true
                },
                "second": {
                    "type": "any",
                    "required": false,
                    "default": "Foo"
                }
            }
        }
    }
});

...

rpcServerInstance.init({
    handler: 80,
    timeout: 10
});

Or you can use jsonrpcserver as proxy:

var logger = require('mylogger'),
    API = require('myapi'),
    JsonRpcServer = require('jsonrpcserver');
    
var rpcServerInstance = new JsonRpcServer(logger); //logger is an optional one

...

rpcServerInstance.register('/my/new/endpoint', {
    "context": new API(),
    "map": {
        "testMethod": {
            "handler": "test",
            "params": [
                {
                    "type": "string",
                    "required": true
                }
            ]
        },
        "yetAnotherMethod": {
            "handler": function(options, cb) {
                (typeof cb == 'function') && cb(null, options);
            },
            "params": {
                "first": {
                    "type": "number",
                    "required": true
                },
                "second": {
                    "type": "any",
                    "required": false,
                    "default": "Foo"
                }
            }
        }
    }
});

...

// proxy my request 
var request = {
    method: "yetAnotherMethod",
    params: {
        first: 1,
        second: "Bar"
    },
    id: 1
};
rpcServerInstance.proxy('/my/new/endpoint', request, function(response) {
    console.log(response);
    
    //  {
    //      httpCode: 200,
    //      headers: {
    //          'Content-Type': 'application/json-rpc'
    //      },
    //      payload: {
    //          jsonrpc: '2.0',
    //          result: { ok: 1 },
    //          id: 1
    //      }
    //  }
});

api

constructor (logger) - create the server

  • logger - an optional parameter, it means reference to the logger instance; console will use by default

register (endpoint, map) - register endpoint

You can use register method (as well as unload) at any time in code, definition order (before or after init) doesn't matter.

  • endpoint - - endpoint URI, that will handle your requests
  • map - - API schema map, such as (type 'any' could use for non-strict type validation):
/**
 * @param map = {
 *      context: <object>,
 *      map: {
 *          methodName1: {
 *              handler: <functionName|callable>,
 *              //-- with named params ---
 *              params: {
 *                  param1: {
 *                      type: "string|number|boolean|object|array|any",
 *                      required: true|false,
 *                      default: <defaultValue>
 *                  },
 *              ...
 *              }
 *              //-- or with array-like params --
 *              params: [
 *                  {
 *                      type: "string|number|boolean|object|array|any",
 *                      required: true|false,
 *                      default: <defaultValue>
 *                  },
 *              ...
 *              ]
 *          },
 *          ...
 *      }
 * }
 */

unload (endpoint) - unload the endpoint

  • endpoint - - endpoint URI that would be unloaded

proxy (endpoint, payload, callback) - emulate the request

  • endpoint - - endpoint URI that would be unloaded
  • payload - - request object according to JSON-RPC 2.0 Specification
  • callback - - callback that handles the response

init (configuration) - instantiate the server

  • configuration - - an optional parameter, that describe server configuration (look below, with defaults):
{
    "handler": 80,  // it may be port, unix-socket path or instance of HTTP/HTTPS server, by default 80 port
    "https": false, // boolean flag sets true if we want set up the HTTPS server
    "key": null,    // the SSL-key path (for HTTPS only)
    "cert": null,   // the SSL-cert path (for HTTPS only)
    "timeout": 60   // the request timeout in sec, by default 60
}

todo

Not-implemented features:

  • batch requests support
  • getting SMD-schema

license

MIT