Spaces:
Sleeping
Sleeping
Download src/libs/shared/presentation/http/middleware/security/RateLimiterMiddleware.ts from fouadkhalid123/asd: direct link, hf CLI and curl.
- Browser
- Download file 913 Bytes
-
https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/libs/shared/presentation/http/middleware/security/RateLimiterMiddleware.ts
- Command line
-
hf download hf://spaces/fouadkhalid123/asd/src/libs/shared/presentation/http/middleware/security/RateLimiterMiddleware.ts
-
curl -L -o RateLimiterMiddleware.ts https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/libs/shared/presentation/http/middleware/security/RateLimiterMiddleware.ts
913 Bytes
| import { Request, Response, NextFunction } from "express"; | |
| import { RateLimiterStore } from "../../../../infrastructure/redis/RateLimiterStore"; | |
| import { RateLimitExceededError } from "../../../../domain/errors/RateLimitExceededError"; | |
| const store = new RateLimiterStore(); | |
| export const RateLimiterMiddleware = async (req: Request, res: Response, next: NextFunction) => { | |
| try { | |
| const ip = req.ip || "unknown"; | |
| const tenantId = req.tenantContext!.organizationId; | |
| const key = `ratelimit:${ip}:${tenantId}`; | |
| const count = await store.increment(key, 60); | |
| if (count > 100) { | |
| throw new RateLimitExceededError(); | |
| } | |
| next(); | |
| } catch (error) { | |
| if (error instanceof RateLimitExceededError) { | |
| next(error); | |
| } else { | |
| console.error("RateLimiter Redis failure:", error); | |
| next(); | |
| } | |
| } | |
| }; | |