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

@jnode/server-ratelimit

v1.0.0

Published

Official rate limit for JNS.

Readme

@jnode/server-ratelimit

Official rate limit package for JNS.

Installation

npm i @jnode/server-ratelimit

Quick start

Import

const { createServer, routerConstructors: r, handlerConstructors: h } = require('@jnode/server');
const { routerConstructors: rr } = require('@jnode/server-ratelimit');

Start a rate-limited server

const server = createServer(
  // apply rate limit: 5 requests per 5 seconds
  rr.RateLimit(
    // pass: what to do if the request is within limit
    h.Text('Welcome! You are not rate-limited.'),
    // fail: what to do if the request exceeds limit (default is 429)
    h.Text('Too many requests, please try again later.', { statusCode: 429 }),
    // limiter configuration
    'tb: 5, 5s'
  )
);

server.listen(8080);

Advanced configuration

const server = createServer(
  rr.RateLimit(
    h.Text('API accessed.'),
    429,
    // Configuration: 10 reqs / 1 min, GC every 5 mins, 
    // auto GC when > 2000 users, force GC every 2 hours
    'tb: 10, 1m, 5m, 2000, 2h',
    // identify by IP address specifically
    'address',
    { disableRetryHeader: false }
  )
);

server.listen(8080);

How it works?

@jnode/server-ratelimit provides a router that acts as a gatekeeper.

When a request arrives:

  1. It identifies the client (using the by parameter, usually by IP address).
  2. It checks the limiter (currently using a Token Bucket algorithm) to see if the client has tokens available.
  3. If tokens are available, it consumes one and returns the pass router/handler.
  4. If no tokens remain, it returns the fail router/handler and optionally sets the Retry-After header.

Reference

Router: RateLimit(pass[, fail, limiter, by, options])

  • pass router | handler-extended The router or handler to use if the rate limit has not been reached.
  • fail router | handler-extended The router or handler to use if the rate limit is exceeded. Default: 429.
  • limiter <string> | <Function>
    • If string, format is type: args. Currently supported type is tb (Token Bucket).
    • If function, signature is (identity) => true | number. Returns true to pass, or a number representing seconds until retry to fail.
    • Default: 'tb: 5, 5s'.
  • by <string> | <Function> How to identify the client.
    • 'auto': Uses ctx.identity.id if available, otherwise ctx.identity.address.
    • 'address': Uses ctx.identity.address.
    • Custom Function: (env, ctx) => identityString.
    • Default: 'auto'.
  • options <Object>
    • disableRetryHeader <boolean> If true, the Retry-After header will not be added to the response on failure. Default: false.

Token Bucket (tb) string format

The limiter string for tb follows this comma-separated argument structure: 'tb: tokens, resetTime, gcTime, autoGC, forceGCTime, gcChunkSize'

  • tokens: Number of requests allowed per period.
  • resetTime: Time duration before tokens refill (e.g., '5s', '1m', '1h').
  • gcTime: How often to run garbage collection for inactive identities. Default: '10m'.
  • autoGC: Max number of tracked identities before triggering GC. Default: 1000.
  • forceGCTime: Maximum time to wait before forcing GC regardless of autoGC. Default: '1h'.
  • gcChunkSize: Number of records to process per GC tick (to prevent blocking the event loop). Default: 500.

Time units supported: ms, s, m, h, d. If no unit is provided, it defaults to seconds.

identifyFunctions

A map of built-in identification strategies.

  • auto: (env, ctx) => ctx.identity.id ?? ctx.identity.address
  • address: (env, ctx) => ctx.identity.address

limiters

A map of available limiter classes.

  • tb: TokenBucketLimiter class.

Class: RateLimitRouter

The class used to create the rate limit router.

new RateLimitRouter(pass[, fail, limiter, by, options])

Same arguments as routerConstructors.RateLimit.

rateLimitRouter.route(env, ctx)

Performs the rate limit check and returns either the pass or fail result.