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

@nym.sh/jrpc

v1.1.0

Published

Typed JSON-RPC over async channels.

Readme

@nym.sh/jrpc

Typed JSON-RPC 2.0 over async channels.

@nym.sh/jrpc gives you a small client/server pair that works with any transport you can model as an async message stream plus a send() method. The transport could be a WebSocket, worker message port, IPC pipe, in-memory test channel, or something custom.

Install

bun add @nym.sh/jrpc
npm install @nym.sh/jrpc

Quick Start

Define the API as a TypeScript interface, create a server with matching handlers, and call it with a typed client.

import { JsonRpcClient, JsonRpcServer } from "@nym.sh/jrpc"
import type { JrpcChannel } from "@nym.sh/jrpc"

interface CalculatorApi {
	add(a: number, b: number): number
	hello(name: string): Promise<string>
}

declare const clientChannel: JrpcChannel
declare const serverChannel: JrpcChannel

const server = new JsonRpcServer<CalculatorApi>(
	{
		add(a, b) {
			return a + b
		},
		async hello(name) {
			return `Hello, ${name}.`
		},
	},
	serverChannel,
)

void server.start()

const client = new JsonRpcClient<CalculatorApi>(clientChannel)

const sum = await client.call("add", 2, 3)
const greeting = await client.call("hello", "Ada")

console.log(sum) // 5
console.log(greeting) // Hello, Ada.

client.call() is fully typed from the schema:

await client.call("add", 2, 3) // Promise<number>
await client.call("hello", "Ada") // Promise<string>

// TypeScript errors:
await client.call("missing")
await client.call("add", "2", "3")

Channels

A channel is an async generator of JSON-RPC messages with a send() method:

import type { JrpcMessage, JsonRpcMessage } from "@nym.sh/jrpc"

interface JrpcChannel extends AsyncGenerator<JrpcMessage, void, unknown> {
	send(msg: JsonRpcMessage): Promise<void>
}

Each endpoint gets its own channel. Calling send() on one endpoint should deliver the message to the other endpoint's async iterator.

For raw JSON transports, parse incoming payloads with deserializeJrpcMessage() instead of casting JSON.parse() yourself:

import { deserializeJrpcMessage } from "@nym.sh/jrpc"

const msg = deserializeJrpcMessage(event.data)

if (!msg) {
	return
}

The package also exports isJrpcMessage(), isJsonRpcRequest(), isJsonRpcResponse(), isJsonRpcSuccessResponse(), and isJsonRpcErrorResponse() when a channel adapter needs to validate or narrow an unknown value.

Here is a minimal in-memory channel pair that is useful for tests and examples:

import type { JrpcChannel, JrpcMessage, JsonRpcMessage } from "@nym.sh/jrpc"

class MemoryChannel implements JrpcChannel {
	private closed = false
	private peer?: MemoryChannel
	private queue: JrpcMessage[] = []
	private waiters: Array<(result: IteratorResult<JrpcMessage, void>) => void> = []

	link(peer: MemoryChannel): this {
		this.peer = peer
		return this
	}

	async send(msg: JsonRpcMessage): Promise<void> {
		if (!this.peer) {
			throw new Error("MemoryChannel is not linked.")
		}

		this.peer.push(msg)
	}

	async next(): Promise<IteratorResult<JrpcMessage, void>> {
		const msg = this.queue.shift()

		if (msg) {
			return { done: false, value: msg }
		}

		if (this.closed) {
			return { done: true, value: undefined }
		}

		return new Promise((resolve) => {
			this.waiters.push(resolve)
		})
	}

	async return(): Promise<IteratorResult<JrpcMessage, void>> {
		this.close()
		return { done: true, value: undefined }
	}

	async throw(error?: unknown): Promise<IteratorResult<JrpcMessage, void>> {
		this.close()
		throw error
	}

	close(): void {
		this.closed = true

		for (const resolve of this.waiters.splice(0)) {
			resolve({ done: true, value: undefined })
		}
	}

	[Symbol.asyncIterator](): AsyncGenerator<JrpcMessage, void, unknown> {
		return this
	}

	private push(msg: JrpcMessage): void {
		const resolve = this.waiters.shift()

		if (resolve) {
			resolve({ done: false, value: msg })
			return
		}

		this.queue.push(msg)
	}
}

export function createMemoryChannelPair(): [MemoryChannel, MemoryChannel] {
	const left = new MemoryChannel()
	const right = new MemoryChannel()

	left.link(right)
	right.link(left)

	return [left, right]
}

Bidirectional RPC

The same endpoint channel can have both a client and a server. This lets peers call each other, including re-entrant calls made from inside handlers.

import { JsonRpcClient, JsonRpcServer } from "@nym.sh/jrpc"

interface AppApi {
	formatName(name: string): string
}

interface PluginApi {
	describe(name: string): Promise<string>
}

const [appChannel, pluginChannel] = createMemoryChannelPair()

const appClient = new JsonRpcClient<PluginApi>(appChannel)
const pluginClient = new JsonRpcClient<AppApi>(pluginChannel)

const appServer = new JsonRpcServer<AppApi>(
	{
		formatName(name) {
			return name.toUpperCase()
		},
	},
	appChannel,
)

const pluginServer = new JsonRpcServer<PluginApi>(
	{
		async describe(name) {
			const formatted = await pluginClient.call("formatName", name)
			return `Plugin received ${formatted}`
		},
	},
	pluginChannel,
)

void appServer.start()
void pluginServer.start()

console.log(await appClient.call("describe", "Ada"))
// Plugin received ADA

Only one JsonRpcServer request handler can be registered per channel, but you can create multiple typed clients over the same channel if that helps organize local call sites.

Remote Errors

If a server handler throws, the client receives a JsonRpcRemoteError.

import { JsonRpcRemoteError } from "@nym.sh/jrpc"

try {
	await client.call("dangerousOperation")
} catch (error) {
	if (error instanceof JsonRpcRemoteError) {
		console.error(error.code, error.message, error.data)
	}
}

Built-in JSON-RPC error codes are exported as JsonRpcErrorCode:

import { JsonRpcErrorCode } from "@nym.sh/jrpc"

console.log(JsonRpcErrorCode.MethodNotFound) // -32601
console.log(JsonRpcErrorCode.InvalidParams) // -32602

API

new JsonRpcClient<Schema>(channel)

Creates a typed client for calling methods exposed by the remote endpoint.

const client = new JsonRpcClient<MyApi>(channel)
const result = await client.call("methodName", arg1, arg2)

Arguments are sent as positional JSON-RPC params.

new JsonRpcServer<Schema>(handlers, channel)

Registers handlers for requests received on the channel.

const server = new JsonRpcServer<MyApi>({ methodName() {} }, channel)
void server.start()

start() listens until the channel closes. Handler return values are sent as JSON-RPC results. undefined is serialized as null.

Development

bun install
bun test
bun run typecheck
bun run build