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

json-rpc-client-gen

v1.0.0

Published

A Node client stub generator for JSON-RPC systems

Readme

json-rpc-client-gen

A Node client stub generator for JSON-RPC Systems


NOTE This package is in experimental phase and currently only supports TCP/IP for communication.


This wraps the complexities of:

  1. Client to server network communication.
  2. Sending and parsing JSON-RPC network requests and responses.

Getting started

  1. Make sure than Node is installed. The binary can run on Node v12 but v18 or higher is recommended.

    • Currently, the project only supports ES Module resolution which is fully supported by Node v12 and higher.
  2. In your project, create a configuration file jsonrpcgen.config.json

    • The config file will be read from the CWD upwards.
    • See Configuration file section for options.
  3. Run the binary to generate the client stub for your RPC server.

    json-rpc-client-gen

    or

    npx json-rpc-client-gen

Sample Usage

import stub from './client-stub/index.js'

stub.on('connect', async () => {
  // Event Emitter data access
  stub.on('data', (data) => {
    console.log('data received:', data)
  })
  
  // Promises data access
  console.log(await Promise.all([stub.add(1, 2), stub.subtract(5, 2)]))
})

stub.connect()

Client stub

  • The client stub code generated currently only supports ESM.
  • There's some support pseudo-parallelism using Promise API static methods. But currently it only uses one socket stream, hence data is sent and obtained from the same layer 4 source. Support for TCP sockets pooling will be implemented in the future.
  • Methods will implicitly connect to the server if the socket is closed/destroyed, however the requests can be buffered in user memory if not yet connected. You can choose to explicitly call the connect method on the stub if you want appropriate stub lifecycle handling.

Stub error handling

Method response error

rejects with the error (see JSON-RPC specification) payload from the response.

Stub errors

Includes:

  • Receiving malformed messages
  • Timeouts
  • Socket errors

This will be emitted with the error event so appropriate on error handler can be given so it would not be thrown. This is emitted asynchronously so as to limit the interference with important synchronous operations of the consumer and the stub.

stub.on('error', (err) => {
   ...
})

Usage error

This will be thrown such as the wrong usage of the batch method (see Batching). This provides granular error handling for synchronous errors.

Methods result access

The results of the methods exposed by the stub can be accessed via:

  1. Promises
const data = await stub.add(1, 2)
const moreData = await Promise.all([stub.add(1, 2), stub.subtract(5, 3)])

console.log(data)
  1. Event Emitters
stub.on('data', (result) => {
  console.log(result)
})
  • Emitted result is the RAW JSON response.

Batching

  • As mentioned above, consumers can do their own batching using Promise API static methods.
  • The stub also exposes a batch method, an implementation of the JSON-RPC batch specification. This allows a consumer to pass RPC in object form. This would return a list of promises corresponding to the RPC at each index. See usage below.
// batching using promises
const [res1, res2] = await Promise.all([stubs.method1(param1), stub.method2(param1)])

// stub batching
const [res1, res2] = await stub.batch([
   { method: 'method1', params: { param1: 'param1' }, isNotification: true },
   { method: 'method2', params: { param1: 'param1' }, isNotification: false },
])

Promise batching vs Batch method

  • Promise batching will send the requests to the server one-by-one in a concurrent manner.
  • batch method will send the requests to the server in an array. This returns a list of promises.

batch method param

/**
 * params and isNotification fields are optional
 */
{
   method: 'method1', // string
   params: { param1: 'param1' }, // object of parameters for the RPC, can also be an array for positional arguments
   isNotification: true // whether the RPC is a notification
}

Configuration file

| Options | Type | Default | Description | Example | | ------- | ---- | ------- | ----------- | ------- | | source | string | - | JSON file for your function | ./src/functions.esrpc.json | | host | string | - | Target server hostname or IPv4/IPv6 (uses dns.lookup()) | localhost | | port | number | - | Target server port | 25 | | targetDir | string | - | Directory where client stub file will be emitted | ./src/client | | version (optional) |1.0 or 2.0 | 2.0 | JSON-RPC specification version | 2.0 | | socketTimeout (optional) | number | 300000 ms/5 mins | Period in ms where a socket will be idle before being destroyed | 10000 | connectionTimeout (optional) | number | 300000 ms/5 mins | Period in ms where a TCP handshake can be successful, rejects and emits an error if timed out | 10000 |

Functions definitions

Function signatures that your server supports are defined in a file in source parameter in the configuration file

{
   "method1": {
      "parameters": ["param1", "param2"]
   },
   "method2": {
      "parameters": ["param1", "param2"]
   }
}