Adds request-count limits to Express routes and middleware.
Downloads / mo 374.0M
Stars 3.3K
Last release 8.7.5
License MIT Strengths Limits public endpoints directly . Mount the middleware on password-reset routes or an entire API. Shares counters through store adapters . Use an external store across multiple processes or servers. Limitations Default counters reset on restart . Use an external store when request counts must survive restarts. One limiter uses one window . Create separate limiters for multiple time windows. $ npm install express-rate-limit@8.7.5 express Copy
import express from 'express';
import { rateLimit } from 'express-rate-limit';
const app = express();
app.use(rateLimit({ windowMs: 60_000, limit: 10 }));
app.get('/', (_req, res) => res.send('hello'));
app.listen(3000);
express-rate-limit examples From open-source projects on GitHub export function rateLimiter(
request: Request,
response: Response,
next: NextFunction
): void {
const tier = classifyRequest(request.method, request.path);
if (tier === 'exempt') {
next();
return;
}
limiters[tier](request, response, next);
}
Show full example Paces work with interval limits and hierarchical token buckets.
Downloads / mo 89.9M
Stars 1.6K
Last release 4.1.0
License MIT Strengths Waits or rejects immediately . Use removeTokens to wait and tryRemoveTokens to check capacity immediately. Shares parent and child budgets . A successful token removal charges the child and its finite ancestors. Limitations Counters stay within one instance . Processes, workers, and machines need a separate shared-limit solution. Limits starts rather than concurrency . Use a separate limiter to cap simultaneous in-flight work. $ npm install limiter@4.1.0 Copy
import { RateLimiter } from 'limiter';
const limiter = new RateLimiter({ tokensPerInterval: 1, interval: 'second' });
console.log(await limiter.removeTokens(1));
limiter examples From open-source projects on GitHub updateLimit(kBs: number) {
if (kBs < 0)
throw Error('invalid bytesPerSecond')
kBs *= 1000
return this.bucket = new TokenBucket({
bucketSize: kBs,
tokensPerInterval: kBs,
interval: 'second',
})
}
Show full example Queues jobs and controls their start rate and simultaneous execution.
Downloads / mo 59.2M
Stars 2.0K
Last release 2.19.5
License MIT Strengths Controls simultaneous work . Set maxConcurrent alongside minTime to pace jobs and cap concurrency. Creates separate limits per key . Use Group to manage independent limiters for different clients. Limitations Queued jobs disappear on exit . Redis shares limiter state, but queued jobs remain in the process. Retries keep concurrency slots occupied . A waiting retry still counts toward maxConcurrent. $ npm install bottleneck@2.19.5 Copy
import Bottleneck from 'bottleneck';
const limiter = new Bottleneck({ minTime: 100, maxConcurrent: 1 });
console.log(await limiter.schedule(() => Promise.resolve('hello')));
bottleneck examples From open-source projects on GitHub const createGroups = function (
Bottleneck: typeof TBottleneck,
common: CreateGroupsCommon,
) {
groups.global = new Bottleneck.Group({
id: "octokit-global",
maxConcurrent: 10,
...common,
});
groups.auth = new Bottleneck.Group({
id: "octokit-auth",
maxConcurrent: 1,
...common,
});
groups.search = new Bottleneck.Group({
id: "octokit-search",
maxConcurrent: 1,
minTime: 2000,
...common,
});
groups.write = new Bottleneck.Group({
id: "octokit-write",
maxConcurrent: 1,
minTime: 1000,
...common,
});
groups.notifications = new Bottleneck.Group({
id: "octokit-notifications",
maxConcurrent: 1,
minTime: 3000,
...common,
});
};
Show full example Adds endpoint quotas to NestJS through a guard, module, and decorators.
Downloads / mo 24.8M
Last release 6.8.0
License MIT $ npm install @nestjs/throttler@6.8.0 @nestjs/common@^11 @nestjs/core@^11 @nestjs/platform-express@^11 reflect-metadata rxjs Copy
import 'reflect-metadata';
import { Controller, Get, Module } from '@nestjs/common';
import { APP_GUARD, NestFactory } from '@nestjs/core';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
@Controller()
class HelloController {
@Get() hello() { return 'hello'; }
}
@Module({
imports: [ThrottlerModule.forRoot([{ ttl: 60_000, limit: 10 }])],
controllers: [HelloController],
providers: [{ provide: APP_GUARD, useClass: ThrottlerGuard }],
})
class AppModule {}
const app = await NestFactory.create(AppModule);
await app.listen(3000);
@nestjs/throttler examples From open-source projects on GitHub @Subscription(() => String, {
description: 'Subscription for infra config update',
resolve: (value) => value,
})
@SkipThrottle()
@UseGuards(GqlAuthGuard)
infraConfigUpdate(
@Args({
name: 'configName',
description: 'Infra config key',
type: () => InfraConfigEnum,
})
configName: string,
) {
return this.pubsub.asyncIterator(`infra_config/${configName}/updated`);
}
Show full example Limits Fastify routes with optional Redis integration and custom request keys.
Downloads / mo 19.6M
Last release 11.2.2
License MIT $ npm install @fastify/rate-limit@11.2.2 fastify@^5 Copy
import Fastify from 'fastify';
import rateLimit from '@fastify/rate-limit';
const app = Fastify();
await app.register(rateLimit, { max: 10, timeWindow: '1 minute' });
app.get('/', async () => 'hello');
await app.listen({ port: 3000 });
Enforces per-key quotas using memory, clustered workers, or shared stores.
Downloads / mo 18.2M
Stars 3.6K
Last release 11.5.0
License ISC Strengths Limits users or API keys . Consume points against an IP, user ID, or another string key. Coordinates workers through IPC . RateLimiterCluster sends worker requests to one process that handles limits. Limitations Store clients need separate setup . Create a database or cache connection before using its store limiter . Distributed queues can reorder requests . Strict FIFO is guaranteed only with Memory and Cluster limiters. $ npm install rate-limiter-flexible@11.5.0 Copy
import { RateLimiterMemory } from 'rate-limiter-flexible';
const limiter = new RateLimiterMemory({ points: 10, duration: 60 });
console.log(await limiter.consume('example-user'));
Checks quotas through Redis REST calls from HTTP and serverless hosts.
Downloads / mo 12.8M
Last release 2.2.1
License MIT $ npm install @upstash/ratelimit@2.2.1 @upstash/redis Copy
import { Ratelimit } from '@upstash/ratelimit';
import { Redis } from '@upstash/redis';
const limiter = new Ratelimit({ redis: Redis.fromEnv(), limiter: Ratelimit.slidingWindow(10, '10 s') });
console.log(await limiter.limit('example-user'));
@upstash/ratelimit examples From open-source projects on GitHub export async function rateLimit(identifier: string) {
// Rate limit through Upstash
const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(10, "10 s"),
analytics: true,
prefix: "@upstash/ratelimit",
});
return await ratelimit.limit(identifier);
}
Show full example Adds rate limiting middleware to Koa with optional Redis, Sequelize, or Mongoose stores.
Downloads / mo 1.1M
Last release 1.1.3
License MIT $ npm install koa2-ratelimit@1.1.3 koa Copy
const Koa = require('koa');
const { RateLimit } = require('koa2-ratelimit');
const app = new Koa();
app.use(RateLimit.middleware({ interval: { min: 1 }, max: 10 }));
app.use(ctx => { ctx.body = 'hello'; });
app.listen(3000);