diff options
Diffstat (limited to 'packages/shared')
| -rw-r--r-- | packages/shared/plugins.ts | 4 | ||||
| -rw-r--r-- | packages/shared/ratelimiting.ts | 38 |
2 files changed, 42 insertions, 0 deletions
diff --git a/packages/shared/plugins.ts b/packages/shared/plugins.ts index e04fd91e..2d03ee39 100644 --- a/packages/shared/plugins.ts +++ b/packages/shared/plugins.ts @@ -1,17 +1,20 @@ // Implementation inspired from Outline import type { QueueClient } from "./queueing"; +import type { RateLimitClient } from "./ratelimiting"; import logger from "./logger"; import { SearchIndexClient } from "./search"; export enum PluginType { Search = "search", Queue = "queue", + RateLimit = "ratelimit", } interface PluginTypeMap { [PluginType.Search]: SearchIndexClient; [PluginType.Queue]: QueueClient; + [PluginType.RateLimit]: RateLimitClient; } export interface TPlugin<T extends PluginType> { @@ -31,6 +34,7 @@ export class PluginManager { private static providers: ProviderMap = { [PluginType.Search]: [], [PluginType.Queue]: [], + [PluginType.RateLimit]: [], }; static register<T extends PluginType>(plugin: TPlugin<T>): void { diff --git a/packages/shared/ratelimiting.ts b/packages/shared/ratelimiting.ts new file mode 100644 index 00000000..3b22310b --- /dev/null +++ b/packages/shared/ratelimiting.ts @@ -0,0 +1,38 @@ +import { PluginManager, PluginType } from "./plugins"; + +export interface RateLimitConfig { + name: string; + windowMs: number; + maxRequests: number; +} + +export interface RateLimitResult { + allowed: boolean; + resetInSeconds?: number; +} + +export interface RateLimitClient { + /** + * Check if a request should be allowed based on rate limiting rules + * @param config Rate limit configuration + * @param key Unique rate limiting key (e.g., "ip:127.0.0.1:path:/api/v1") + * @returns Result indicating if the request is allowed and reset time if not + */ + checkRateLimit(config: RateLimitConfig, key: string): RateLimitResult; + + /** + * Reset rate limit for a specific key + * @param config Rate limit configuration + * @param key Unique rate limiting key + */ + reset(config: RateLimitConfig, key: string): void; + + /** + * Clear all rate limit entries + */ + clear(): void; +} + +export async function getRateLimitClient(): Promise<RateLimitClient | null> { + return PluginManager.getClient(PluginType.RateLimit); +} |
