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

@bklarjs/rate-limit

v1.0.2

Published

Rate-limiting middleware for the bklar web framework.

Downloads

22

Readme

@bklarjs/rate-limit

NPM Version License: MIT

Official rate-limiting middleware for the bklar framework. Protect your APIs from brute-force attacks and abuse by limiting the number of requests a client can make in a given time window.


✨ Features

  • 🚀 High Performance: Uses an efficient in-memory store (Map) for fast lookups.
  • 🔧 Highly Configurable: Customize the time window, maximum requests, error messages, and more.
  • 🆔 Flexible Client Identification: Defaults to using the client's IP address but allows for a custom keyGenerator function to rate-limit based on API keys, user IDs, etc.
  • 📋 Standard Headers: Automatically adds X-RateLimit-Limit, X-RateLimit-Remaining, and X-RateLimit-Reset headers to responses.
  • 🛡️ Full TypeScript Support: Strongly-typed configuration for a superior development experience.

📦 Installation

This package is designed to work with bklar. You'll need both installed in your project.

bun add bklar @bklarjs/rate-limit

🚀 Usage

The most common use case is to apply the rate-limiter globally to all requests.

import { Bklar } from "bklar";
import { Bklar as rateLimit } from "@bklarjs/rate-limit";

const app = Bklar();

// Apply the rate-limiter globally:
// Allow 100 requests per 15 minutes from each IP address.
app.use(
  rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100, // limit each IP to 100 requests per windowMs
  })
);

app.get("/", (ctx) => {
  return ctx.json({ message: "This endpoint is rate-limited." });
});

app.listen(3000);

If a client exceeds the limit, bklar will automatically respond with a 429 Too Many Requests status and a JSON error message.

Advanced Usage: Protecting Specific Routes

You can also apply different rate limits to specific routes or groups.

import { Bklar } from "bklar";
import { Bklar as rateLimit } from "@bklarjs/rate-limit";

const app = Bklar();

// A stricter rate limit for a sensitive endpoint like /login
const loginRateLimiter = rateLimit({
  windowMs: 10 * 60 * 1000, // 10 minutes
  max: 5, // Limit each IP to 5 login attempts per 10 minutes
  message: "Too many login attempts. Please try again in 10 minutes.",
});

app.post(
  "/login",
  (ctx) => {
    // ... login logic
  },
  {
    middlewares: [loginRateLimiter],
  }
);

⚙️ Configuration Options

  • windowMs: The time window in milliseconds. Defaults to 60000.
  • max: The maximum number of requests to allow during the windowMs. Defaults to 50.
  • message: Custom error message string. Defaults to "Too many requests, please try again later.".
  • standardHeaders: A boolean to enable/disable X-RateLimit-* headers. Defaults to true.
  • keyGenerator: A function (ctx: Context) => string to generate a unique key for client identification. Defaults to using X-Client-IP header.

Example: Custom Key Generator

Rate-limit based on a user ID from a JWT payload.

import { Bklar as rateLimit } from "@bklarjs/rate-limit";
import { jwt } from "@bklarjs/jwt";

// Assume authMiddleware populates ctx.state.jwt
const authMiddleware = jwt({ secret: "secret" });

const userRateLimiter = rateLimit({
  windowMs: 60 * 1000,
  max: 20,
  keyGenerator: (ctx) => {
    // Use the user's ID if authenticated, otherwise fall back to IP
    return ctx.state.jwt?.sub || ctx.req.headers.get("X-Client-IP");
  },
});

app.post(
  "/api/posts",
  (ctx) => {
    // ... create post
  },
  {
    middlewares: [authMiddleware, userRateLimiter],
  }
);

Note: The default store is in-memory, which means it is reset when the server restarts and is not shared across multiple server processes. For a distributed environment, you would need to implement a custom store (e.g., using Redis).

🤝 Contributing

Contributions are welcome! Please open an issue or submit a Pull Request to the main bklar repository.

📄 License

This project is licensed under the MIT License.