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

@trt2/tinyjsonrpc-server

v1.0.0-alpha.1

Published

Test.

Readme

Tiny JSON-RPC Server

A fairly simple nodejs JSON-RPC server library without any specific server dependencies.

Usage

The library may be used to create a handler for JSON-RPC calls.

Basic handling of JSON-RPC calls:

const { 
    JSONRPC_ERRORCODES,
    createParseErrorResponse,
    JsonRpcRequestException,
    TinyJsonRpcServer 
} = require('@trt2/tinyjsonrpc-server');

const jsonRpcServer = new TinyJsonRpcServer();
jsonRpcServer.registerMethods({
    'add': function(params, requestContext) {
        return params.a + params.b;
    }
});

// The requestContext contains data that should be accessible
// to the registered functions. This is a convenient place to put
// either http information directly or authentication data etc.

let requestContext = {
    valueForRegisteredMethod: 'some text',
    httpRequest: {},    // could be used to get cookies etc
    httpResponse: {}    // could be used to set cookies etc
}

const jsonRpcResponse = jsonRpcServer.handleJsonRpcRequest({ 
        jsonrpc: '2.0', 
        method: 'add',
        params: { a: 2, b: 4 },
        id: 1
    }, requestContext);

// jsonRpcResponse:
// { jsonrpc: '2.0', result: 6, id: 1 }

Expressjs Example

const express = require('express');
const { 
    JSONRPC_ERRORCODES,
    JsonRpcRequestException,
    TinyJsonRpcServer 
} = require('@trt2/tinyjsonrpc-server'); 

const jsonRpcServer = new TinyJsonRpcServer();

function throwParamError(message, data) {
    throw new JsonRpcRequestException(JSONRPC_ERRORCODES.INVALID_PARAMS, message, data);
}

function jsonRpcAddMethod(params, requestContext) {
    isNaN(Number(params.a)) && throwParamError("Invalid parameter 'a', not a number");
    isNaN(Number(params.b)) && throwParamError("Invalid parameter 'b', not a number");

    return params.a + params.b;
}

jsonRpcServer.registerMethods({
    'my.namespace.add': jsonRpcAddMethod
});

function handleJsonRpcRequest(req, res) {
    const requestContext = {req, res};
    jsonRpcServer.handleJsonRpcRequest(req.body, requestContext)
        .then((response) => { 
            res.json(response);
            return response;
        })
        .catch((e) => {
            // This should never occur, handleJsonRpcRequest should take care of all errors
            // and produce a response.
            res.status(500).send({ error: 'Error occurred when processing request' });
        });
}


const app = express();
app.use(express.json());

app.post('/api/jsonrpc', handleJsonRpcRequest);

app.listen(3000, () => console.log('Example app listening on port 3000!'));

class TinyJsonRpcServer

The TinyJsonRpcServer class has the following methods:

registerMethods(methodObj)

/**
 * Register methods with this server.
 * This method accepts an object where the key is the public method name
 * and the value is a function.
 * 
 * The registered method will be given the following parameters:
 * function (params, requestContext) {
 *     return <result or Promise>;
 * }
 * 
 * If the method registered is the method of a javascript class, remember
 * to bind it to the correct "this":
 *   { "mymethod": this.mymethod.bind(this) }
 */

getRegisteredMethods()

/**
 * Returns the actual method object used by the server.
 * Values may be added and removed from this object.
 */

registerMethodCallback(methodCallback)

/**
 * Register a callback that will be called if there is no method
 * registered for the method in the json-rpc request.
 * 
 * The callback will be given the following parameters:
 * 
 * function (method, params, requestContext) {
 *     return <result, undefined or Promise>;
 * }
 * 
 * When undefined is returned, the response will be treated 
 * as if the method was not found.
 * 
 * The callback may be used in addition to or instead of the
 * registerMethods. The registered method will take precendece.
 * 
 * @param {*} methodCallback 
 */

getMethodCallback()

/**
 * Returns the registered method callback function
 */

handleJsonRpcRequest(request, requestContext={})

/**
 * Handle a JSON-RPC request and produce a response object/list.
 * This method returns a promise that should always be resolved, meaning
 * this will always produce a "final" JSON-RPC response that can be passed
 * back to the caller.
 * 
 * When the request produces no results (only notification requests), null 
 * is returned.
 * 
 * @param {*} request either string or JSON object
 * @param {*} requestContext optional context object passed to the handler methods
 */

function createErrorObject(code, message, data)

/**
 * Create an error object for use with JsonRpcRequestException
 * 
 * @param {*} code 
 * @param {*} message 
 * @param {*} data optional data object/value
 */