-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathrate-limit.ts
More file actions
45 lines (40 loc) · 1.38 KB
/
Copy pathrate-limit.ts
File metadata and controls
45 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
const hasRedis = process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN;
const redis = hasRedis ? Redis.fromEnv() : null;
// 5 login attempts per 15 minutes (Token Bucket)
export const authRateLimit = redis
? new Ratelimit({
redis,
limiter: Ratelimit.tokenBucket(5, "15 m", 5),
analytics: true,
prefix: "@upstash/ratelimit/auth",
})
: null;
// 30 API calls per minute (Token Bucket)
export const linksRateLimit = redis
? new Ratelimit({
redis,
limiter: Ratelimit.tokenBucket(30, "1 m", 30),
analytics: true,
prefix: "@upstash/ratelimit/links",
})
: null;
// 15 API calls per minute (Token Bucket)
export const usernameRateLimit = redis
? new Ratelimit({
redis,
limiter: Ratelimit.tokenBucket(15, "1 m", 15),
analytics: true,
prefix: "@upstash/ratelimit/username",
})
: null;
// In-memory fallback
const localFallbackMap = new Map<string, number>();
export function checkLocalRateLimit(ip: string, limit: number): boolean {
const key = `${ip}-${Math.floor(Date.now() / 60000)}`;
const current = localFallbackMap.get(key) || 0;
if (current >= limit) return false;
localFallbackMap.set(key, current + 1);
return true;
}