Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PATCH] Added keyPrefix option, plus some trivial fixes #11

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
node_modules/
package-lock.json
*.tsbuildinfo
Expand Down
11 changes: 6 additions & 5 deletions src/rateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ import { RedisStore } from "./redisStore.ts";
export const limit = <C extends Context, RT extends RedisType>(
userOptions?: OptionsInterface<C, RT>,
) => {
const options = { ...defaultOptions, ...userOptions };
const options = { ...defaultOptions, ...(userOptions ?? {}) };
Amir-Zouerami marked this conversation as resolved.
Show resolved Hide resolved
const store = options.storageClient === "MEMORY_STORE"
? new MemoryStore(options.timeFrame)
: new RedisStore(options.storageClient as RT, options.timeFrame);

const keyPrefix = userOptions?.keyPrefix ?? defaultOptions.keyPrefix;

const middlewareFunc = async (ctx: C, next: NextFunction) => {
const keyCheck = options.keyGenerator(ctx);
if (!keyCheck) {
const key = options.keyGenerator(ctx);
if (!key) {
return await next();
}

const key = "RATE_LIMITER" + keyCheck;
const hits = await store.increment(key);
const hits = await store.increment(keyPrefix + key);

if (hits === options.limit + 1 || (options.alwaysReply && hits > options.limit)) {
return options.onLimitExceeded(ctx, next);
Expand Down
15 changes: 11 additions & 4 deletions src/typesAndDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,24 @@ export interface OptionsInterface<C extends Context, RT extends RedisType> {

/**
* @param ctx Is the context object you get from grammy/telegraf.
* @returns A number in **string** format as the unique key (identifier).
* @description A function to generate a unique key for every user. You cound set it as any key you want (e.g group id)
* @returns A unique **string** key (identifier).
* @description A function to generate a unique key for every user. You could set it as any key you want (e.g. group id)
* @see [Getting Started](https://github.com/Amir-Zouerami/rateLimiter#-how-to-use)
*/
keyGenerator?: (ctx: C) => string | undefined;

/**
* @default "RATE_LIMITER"
* @description A string prefix that is getting added to the storage key after calling the `keyGenerator()`.
*/
keyPrefix?: string | undefined;
}

export const defaultOptions = {
export const defaultOptions: OptionsInterface<Context, RedisType> = {
Amir-Zouerami marked this conversation as resolved.
Show resolved Hide resolved
timeFrame: 1000,
limit: 1,
onLimitExceeded: (_ctx: Context, _next: NextFunction) => {},
storageClient: "MEMORY_STORE",
Amir-Zouerami marked this conversation as resolved.
Show resolved Hide resolved
keyGenerator: (ctx: Context) => ctx.from === undefined ? undefined : ctx.from.id.toString(),
keyGenerator: (ctx: Context) => ctx.from?.id.toString(),
Amir-Zouerami marked this conversation as resolved.
Show resolved Hide resolved
keyPrefix: "RATE_LIMITER",
};
Loading