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

protomux-rpc-router

v1.0.0

Published

Protomux RPC router with modular, stackable middleware

Downloads

116

Readme

Protomux RPC Router

Expose Protomux RPC responders on a HyperDHT server with a simple, composable middleware model.

Overview

This package wraps protomux-rpc responders with an onion-style middleware stack. You can attach:

  • Global middleware: applied to every RPC method.
  • Per-method middleware: applied only to a specific method.

Middleware composes in a nested “onion” where control flows inward to your handler and then unwinds outward, so cross-cutting concerns (logging, rate limiting, timeouts, auth, validation, etc.) can be layered in a predictable order to improve resilience.

High level:

request
  -> [global middleware 1]
    -> [global middleware 2]
      -> [method middleware 1]
        -> [method middleware 2]
          -> handler
        <- [method middleware 2]
      <- [method middleware 1]
    <- [global middleware 2]
  <- [global middleware 1]
response

Errors thrown in inner layers propagate up the same chain; the outer layer's catch/finally will execute and can observe or handle them (e.g., a logger).

Quick start

See example/index.js for a runnable demo.

API

const router = new ProtomuxRpcRouter()

Create a new router.

router.use(middleware)

Attach global middleware. This will wrap every method in the order provided (onion-style). Returns the router for chaining (router.use(middleware1).use(middleware2)).

  • middleware: object with onrequest(ctx, next), and optional onopen()/onclose() hooks; applied to every method.

const methodRegistration = router.method(name, options, handler)

Register an RPC method. Returns a MethodRegistration which can be further configured with .use(...).

  • name: string RPC method name.
  • options.requestEncoding: a compact-encoding encoder used to decode ctx.value before invoking the handler.
  • options.responseEncoding: a compact-encoding encoder used to encode the handler result.
  • handler: function (request, ctx) => any|Promise<any> that produces the response.

Note: calls protomux-rpc's respond method under the hood.

router.handleConnection(connection, protomuxRpcId=connection.publicKey)

Attach responders for all registered methods to an incoming HyperDHT connection.

  • connection: the incoming HyperDHT connection .
  • protomuxRpcId (optional): Buffer responder id; defaults to connection.publicKey.

router.ready()

Call the onopen of all middlewares. Call this after registering all methods and middlewares.

router.close()

Stop accepting new connections and call the onclose of all middlewares.

methodRegistration.use(middleware)

Append additional per-method middleware to an already registered method. Returns the registration for chaining (methodRegistration.use(middleware1).use(middleware2)).

  • middleware: object with onrequest(ctx, next), and optional onopen()/onclose().

Middleware interface

Middlewares are objects with an onrequest function and an optional onopen/onclose hook:

const middleware = {
  // Called for every request that this middleware wraps
  onrequest: async (ctx, next) => {
    // do work before
    const res = await next()
    // do work after
    return res
  },
  // [Optional] hook invoked when the router starts up, for initialisation logic
  onopen: async () => {},
  // [Optional] hook invoked when the router closes, for cleanup logic
  onclose: async () => {}
}
  • onrequest(ctx, next) => Promise<any>:
    • ctx includes:
      • ctx.method: string RPC method name.
      • ctx.value: the raw request object from protomux-rpc.
      • ctx.connection: the underlying connection.
    • next(): calls the next middleware/handler and resolves to the handler’s response (or throws).
  • onopen(): async open hook to initialize the resource.
  • onclose(): async close hook to cleanup the resource.