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

@lock-sdk/rate-limit

v1.0.0

Published

Rate limiter module for Lock security framework

Downloads

4

Readme

Rate Limit

A powerful and flexible rate limiting module for the Lock security framework. Protect your backend services from abuse, brute-force attacks, DDoS attempts, and unexpected surges in traffic using a variety of strategies and pluggable storage options.

🚀 Features

  • ✅ Fixed, sliding, token, and leaky bucket strategies
  • 🌍 Country-based throttling via IP geolocation
  • 🌐 Distributed rate limit storage with Redis & Upstash support
  • 🛡️ Built-in DDoS heuristics and adaptive escalation
  • 🧠 Custom key generators and smart skip logic
  • 📦 Headers for standard and RFC-compliant rate info
  • 🔥 Fast in-memory caching via LRU strategies

🛠 Usage

Basic Example (100 requests per minute)

import { secure, rateLimit } from '@lock-sdk/main';

const middleware = secure()(
  rateLimit({
    limit: 100,
    windowMs: 60_000, // 1 minute
  })
);

⚙️ Configuration

| Option | Type | Description | | ----------------- | ------------------------------------------------------------------------------------ | ------------------------------------------------ | | limit | number | Max number of requests allowed in the window | | windowMs | number | Duration of window in milliseconds | | strategy | 'fixed-window' | 'sliding-window' | 'token-bucket' | 'leaky-bucket' | 'adaptive' | Algorithm used to track requests | | storage | 'memory' | 'redis' | 'upstash' | Backend for storing request counters | | headers | boolean | Whether to add rate-limit headers | | standardHeaders | boolean | Include legacy X-RateLimit-* headers | | statusCode | number | Response code on limit exceeded (default: 429) | | message | string | Error message when blocked | | resources | Record<string, { limit, windowMs }> | Route-specific throttles | | countryLimits | Record<string, { limit, windowMs }> | Per-country overrides | | geoProvider | { type: 'ipapi' | 'maxmind' } | Used for country detection | | ddosPrevention | object | Enable traffic heuristics for spike prevention | | keyGenerator | Function | Custom function to generate unique request key | | skipFunction | Function | Skip check conditionally (e.g., internal IPs) |

📡 Header Output

When enabled, Lock sets the following headers:

| Header | Description | | ----------------------- | ------------------------------------ | | X-RateLimit-Limit | Max requests per window | | X-RateLimit-Remaining | Requests remaining in current window | | X-RateLimit-Reset | Time (seconds) until window resets | | Retry-After | Delay before retrying (if blocked) |

🧪 Example: Per-resource Limits

rateLimit({
  limit: 100,
  windowMs: 60_000,
  resources: {
    '/api/login': { limit: 10, windowMs: 60_000 },
    '/api/posts': { limit: 500, windowMs: 60_000 },
  },
});

🌍 Example: Country-specific Throttling

rateLimit({
  limit: 100,
  windowMs: 60_000,
  geoProvider: { type: 'ipapi' },
  countryLimits: {
    US: { limit: 200, windowMs: 60_000 },
  },
});

🔐 DDoS Protection

Built-in heuristic-based protection:

ddosPrevention: {
  enabled: true,
  requestRateThreshold: 100,
  burstThreshold: 30,
  banDurationMs: 600_000
}

🧩 Storage Backends

Memory (Default)

storage: 'memory',
memoryOptions: {
  max: 10_000,
  ttl: 3600_000
}

Redis

storage: 'redis',
redis: {
  url: 'redis://localhost:6379',
  password: 'secret'
  //pass username if using Redis cloud
}

Upstash

storage: 'upstash',
upstash: {
  url: process.env.UPSTASH_REDIS_REST_URL!,
  token: process.env.UPSTASH_REDIS_REST_TOKEN!
}

💡 Pro Tip: Custom Key (e.g., User ID)

rateLimit({
  keyGenerator: async ctx => ctx.request.headers['x-user-id'] || 'anonymous',
});

🧪 Testing

Use any HTTP client like curl, Postman, or k6 to simulate burst requests and test blocking.

🛡 Maintained By

Lock Team