mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-13 23:17:02 +03:00
migrate to typescript
This commit is contained in:
committed by
Dainius Dauksevicius
parent
c462323dd5
commit
08d27265fc
21
src/middleware/requestRateLimit.ts
Normal file
21
src/middleware/requestRateLimit.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import {getIP} from '../utils/getIP';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import rateLimit from 'express-rate-limit';
|
||||
import {RateLimitConfig} from '../types/config.model';
|
||||
|
||||
export function rateLimitMiddleware(limitConfig: RateLimitConfig): rateLimit.RateLimit {
|
||||
return rateLimit({
|
||||
windowMs: limitConfig.windowMs,
|
||||
max: limitConfig.max,
|
||||
message: limitConfig.message,
|
||||
statusCode: limitConfig.statusCode,
|
||||
headers: false,
|
||||
keyGenerator: (req) => {
|
||||
return getHash(getIP(req), 1);
|
||||
},
|
||||
skip: (/*req, res*/) => {
|
||||
// skip rate limit if running in test mode
|
||||
return process.env.npm_lifecycle_script === 'ts-node test.ts';
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user