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 🙏

© 2025 – Pkg Stats / Ryan Hefner

protomux-rpc-middleware

v1.0.0

Published

Protomux RPC router recommended middlewares

Readme

Protomux RPC Router Middlewares

Collection of recommended middlewares for protomux-rpc-router to add logging, metrics, rate limiting & concurrent limiting.

Usage

Use the precomposed recommended middleware stack in your app with protomux-rpc-router:

const ProtomuxRpcRouter = require('protomux-rpc-router')
const recommended = require('protomux-rpc-middleware')
const pino = require('pino')

const router = new ProtomuxRpcRouter()

router.use(recommended())

Alternatively, override the default configuration:

router.use(
  recommended({
    logger: {
      instance: pino({ level: 'info', name: 'rpc' }),
      logIp: true
    },
    rateLimit: { capacity: 20, intervalMs: 50 },
    concurrentLimit: { capacity: 32 }
  })
)

router.method('echo', (req) => req)

Exports

  • Logger: Pino-based request logger middleware.
  • RateLimit: Token-bucket rate limiter middleware with byIp/byPublicKey factories.
  • ConcurrentLimit: Concurrency limiter for in-flight requests per key (byIp/byPublicKey).
  • recommended(options): Precomposed stack: Logger(console)RateLimit.byIp(10, 100)ConcurrentLimit.byIp(16). Accepts overrides via options.

API

const stack = recommended(options)

Create a composed middleware stack with sensible defaults, in order (outermost first):

Options (all optional):

  • logger: (object) logger configuration. Use false to disable logger
    • logger.instance (pino): pino or console compatible logger. default: console
    • logger.logIp (boolean): whether or not to log the IP of the clients. Default false.
  • rateLimit (object): configuration for RateLimit.byIp.
    • rateLimit.capacity (number): max tokens per IP bucket. Default 10.
    • rateLimit.intervalMs (number): milliseconds to refill 1 token. Default 100.
  • concurrentLimit (object): configuration for ConcurrentLimit.byIp.
    • concurrentLimit.capacity (number): max in-flight requests per IP. Default 16.

Example:

const pino = require('pino')
const recommended = require('protomux-rpc-middleware')

const stack = recommended({
  logger: pino({ level: 'debug', name: 'rpc' }),
  rateLimit: { capacity: 20, intervalMs: 50 },
  concurrentLimit: { capacity: 32 }
})

new Logger(logger, [options])

Create a logging middleware using either console or a pino logger.

  • logger: console or a pino logger instance to write logs.
  • options (optional): object configuring the middleware:
    • options.logIp (boolean, default false): include the connection IP in logs.

Static:

  • Logger.skip: a middleware that sets ctx.skipLog = true to suppress logging for the request.

const rateLimit = new RateLimit(capacity, intervalMs, toKey, [options])

Low-level constructor to customize keying.

  • capacity (number): maximum tokens per bucket.
  • intervalMs (number): milliseconds to refill 1 token.
  • toKey(ctx) (function): maps a ctx to a limiter key.
  • options (optional): metrics options:
    • options.nrRateLimitsMetricName (string, default rate_limit_number_rate_limits): gauge name tracking number of active buckets. Use when there are multiple rate limiters active.

rateLimit.on('rate-limit-refilled', (key, tokens) => {})

Emitted every refill interval for each tracked key after its bucket is incremented. tokens is the number of tokens after refill. When the bucket reaches capacity, the key is limiter key.

rateLimit.on('rate-limit-acquired', (key, tokens) => {})

Emitted when a request successfully consumes a token. tokens is the remaining tokens after the acquisition.

rateLimit.on('rate-limit-exceeded', (key) => {})

Emitted when a request is denied because no tokens are available for the limiter key.

RateLimit.byIp(capacity, intervalMs, [options])

Create a token-bucket rate limiter per request IP. See the RateLimit constructor for the parameters.

RateLimit.byPublicKey(capacity, intervalMs, [options])

Create a token-bucket rate limiter per remote public key. See the RateLimit constructor for the parameters.

new ConcurrentLimit(capacity, toKey)

Low-level constructor to customize keying.

  • capacity (number): maximum concurrent requests per key.
  • toKey(ctx) (function): maps a ctx to a concurrency key.

ConcurrentLimit.byIp(capacity)

Create a concurrent limiter per request IP. See the ConcurrentLimit constructor for the parameters.

ConcurrentLimit.byPublicKey(capacity)

Create a concurrent limiter per remote public key. See the ConcurrentLimit constructor for the parameters.