-
Notifications
You must be signed in to change notification settings - Fork 3
/
rateLimiter.ts
56 lines (49 loc) · 1.5 KB
/
rateLimiter.ts
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
46
47
48
49
50
51
52
53
54
55
56
import { MemoryStore } from "./memoryStore.ts";
import {
Context,
defaultOptions,
NextFunction,
OptionsInterface,
RedisType,
} from "./typesAndDefaults.ts";
import { RedisStore } from "./redisStore.ts";
/**
*
* @param UserOptions an object of rateLimiter options:
* ```ts
* {
* timeFrame: 1000,
* limit: 1,
* onLimitExceeded: (ctx, next) => {},
* storageClient: "MEMORY_STORE",
* keyGenerator: (ctx) => ctx.from && ctx.from.id.toString(),
* }
* ```
*
* as explained in [customizability](https://github.com/Amir-Zouerami/rateLimiter#-customizability)
* @description A middleware function generator
* @returns a middleware function to be passed to `bot.use()`
*/
export const limit = <C extends Context, RT extends RedisType>(
userOptions?: OptionsInterface<C, RT>,
) => {
const options = { ...defaultOptions, ...userOptions };
const store = options.storageClient === "MEMORY_STORE"
? new MemoryStore(options.timeFrame)
: new RedisStore(options.storageClient as RT, options.timeFrame);
const middlewareFunc = async (ctx: C, next: NextFunction) => {
const keyCheck = options.keyGenerator(ctx);
if (!keyCheck) {
return await next();
}
const key = "RATE_LIMITER" + keyCheck;
const hits = await store.increment(key);
if (hits === options.limit + 1 || (options.alwaysReply && hits > options.limit)) {
return options.onLimitExceeded(ctx, next);
}
if (hits <= options.limit) {
return await next();
}
};
return middlewareFunc;
};