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 🙏

© 2026 – Pkg Stats / Ryan Hefner

sonrpc

v1.0.0

Published

Minimalistic JSON-RPC framework

Readme

sonrpc

Minimalistic library for creating JSON-RPC 2.0 API

Getting Started

//api/greet.js
module.exports = async ({
    name,
    city
}) => {
    return `Hi, ${name} from ${city}!`;
};
//index.js
const sonrpc = require('sonrpc');

const app = new sonrpc.Application();

app.launch();

Testing out, request to http://localhost:3000/api:

{
    "jsonrpc": "2.0",
    "method": "greet",
    "id": "1",
    "params": {
        "name": "John",
        "city": "London"
    }
}

Response:

{
    "jsonrpc": "2.0",
    "id": "1",
    "result": "Hi, John from London!"
}

See /example for more details

Modules

The Application class used in the example above is a built-in http server which accepts JSON-RPC requests in HTTP body. It also handles such cases as invalid JSON body in the request.

However, if you want to use JSON-RPC calls via any other transport (WebSockets, TCP) or platform (express, fastify), you can use just the Dispatcher class from sonrpc, which does all the handling of the RPC calls.

Configuration

Available sonrpc.Application options and their default values:

new sonrpc.Application({
    //HTTP URL where the requests will be processed 
    apiUrl: '/api',

    //HTTP port to start the server on
    httpPort: 3000,

    //Is 'application/json' content type required for incoming requests
    //If true and the header is not valid, a JSON-RPC error will be returned
    requireContentType: true,
});

Other options are automatically passed to Dispatcher and Loader objects.

Available sonrpc.Dispatcher options and their default values:

new sonrpc.Dispatcher({
    //Object with each property mapping to a method
    //<methodName, function>
    methods: {},

    //If true, batch requests are processed in parallel
    parallelBatches: true,

    //Error handler for non-RPC errors
    //signature: (error, request)
    //where 'error' is the error thrown 
    //'request' is JSON RPC 'Request' object
    //must return JSON-RPC 'Error' object

    //default error handler just
    //puts the error.message as JSON-RPC error message
    //and error.code as JSON-RPC error code (or null if omitted)
    errorHandler: defaultErrorHandler
});

Available sonrpc.Loader options and their default values:

new sonrpc.Loader({
    //If true, node.js 'require()' will be used for loading methods
    //Otherwise, node.js 'vm' module will be used
    //See below for more details
    useRequire: true,
    
    //Directory to search for RPC methods
    apiDir: './api',

    //Delimeter for method namespaces
    //If there is add.js file in math/ folder
    //The final method name will be 'math.add'
    namespaceDelimeter: '.'
});

Methods and method loading

In sonrpc, each method handler is just a simple async function. The parameters are passed either as an object, or as an array of arguments:

//As array
//params: [2,3]
async (a,b) => {
    return a + b;
};

//As object
//params: {a: 2, b: 3}
async ({
    a,b
}) => {
    return a + b;
};

The call is successfull if handler returns any value that can be serialized to JSON, then it is sent as result in JSON-RPC response object.

If error is thrown, the call is not successfull and JSON-RPC error object is sent back to client.

By default, sonrpc.Loader loades the method handlers via require(), so each method is just basically a module:

//api/method.js
module.exports = async (param1, param2) => {
    return ...;
};

But, you can disable this feature by providing useRequire: false option. In that case, node.js vm (and vm.Script to be specific) module capabilites will be used for loading and running method handlers. Also, the method definition changes a bit (module.exports is omitted):

async (param1, param2) => {
    return ...;
};

This is shorter and more convenient way. However, the script is run in separate scope, so such tools as require or module are not available. Therefore, a some sort of dependency injection must be added.

Author: Nikita Kogut (MrOnlineCoder)

License: MIT