Gathering detailed insights and metrics for cutz-redis-ratelimit
Gathering detailed insights and metrics for cutz-redis-ratelimit
npm install cutz-redis-ratelimit
Typescript
Module System
Node Version
NPM Version
68.1
Supply Chain
98.3
Quality
77.9
Maintenance
100
Vulnerability
100
License
TypeScript (89.03%)
JavaScript (10.97%)
Total Downloads
251
Last Day
1
Last Week
7
Last Month
17
Last Year
251
1 Stars
8 Commits
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.2
Package Id
cutz-redis-ratelimit@1.0.2
Unpacked Size
13.54 kB
Size
4.26 kB
File Count
7
NPM Version
10.8.2
Node Version
22.8.0
Publised On
13 Sept 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
133.3%
7
Compared to previous week
Last month
240%
17
Compared to previous month
Last year
0%
251
Compared to previous year
2
2
cutz-redis-ratelimit
is a simple and efficient rate limiting library using Upstash Redis. It provides built-in methods for handling rate limiting, calculating retry times, and creating rate limit responses.
Install the package using npm:
1npm i cutz-redis-ratelimit
1import { Ratelimit } from "cutz-redis-ratelimit";
Create an instance of the Ratelimit
class by providing the necessary configuration options:
1const ratelimit = new Ratelimit({ 2 url: process.env.UPSTASH_REDIS_URL ?? "", 3 token: process.env.UPSTASH_REDIS_TOKEN ?? "", 4 time: "10 s", 5 maxRequests: 1, 6 logging: true, // optional 7 whitelist: ['127.0.0.1'], // optional 8 blacklist: ['203.0.113.1'], // optional 9 blacklistConfig: { // optional 10 blockDuration: 7200 * 1000, 11 message: "Your IP has been blacklisted.", 12 }, 13});
Use the ratelimit
instance to limit requests based on the client's IP address:
1import { NextResponse } from "next/server"; 2import { Ratelimit } from "cutz-redis-ratelimit"; 3 4const ratelimit = new Ratelimit({ 5 url: process.env.UPSTASH_REDIS_URL, 6 token: process.env.UPSTASH_REDIS_TOKEN, 7 windowSize: "10 s", 8 maxRequests: 1, 9}); 10 11export async function GET(req: Request) { 12 const ip = req.headers.get("x-forwarded-for") ?? ""; 13 const { success, reset } = await ratelimit.limit(ip); 14 15 if (!success) { 16 const retryAfter = ratelimit.getRetryAfter(reset); 17 return ratelimit.createRateLimitResponse(retryAfter); 18 } 19 20 try { 21 return NextResponse.json( 22 { message: "Request successful" }, 23 { status: 200 } 24 ); 25 } catch (error) { 26 return NextResponse.json( 27 { error: "Failed to do something...." }, 28 { status: 500 } 29 ); 30 } 31}
1constructor(options: RatelimitOptions)
1limit(ip: string): Promise<{ success: boolean; reset: number; }>
Limits the number of requests based on the client's IP address.
1getRetryAfter(reset: number): number
Calculates the retry-after time in seconds.
1createRateLimitResponse(retryAfter: number): Response
Creates a rate limit response with the appropriate headers.
1createBlacklistResponse(): Response
Creates a blacklist response with the appropriate message.
1updateConfig({ time, maxRequests }: { time: Duration; maxRequests: number }): void
Updates the rate limit configuration.
This project is licensed under the ISC License.
No vulnerabilities found.
No security vulnerabilities found.