/** * Rate Limit Store - Pluggable backends for rate limiter state. * * - InMemoryRateLimitStore: Map-backed, zero-dep, single-instance (default) * - PGliteRateLimitStore: PostgreSQL-backed via PGlite, supports per-instance * persistence and can be extended with ElectricSQL for distributed sync. */ export interface WindowEntry { count: number; windowStart: number; } /** * Pluggable storage backend for rate limiter window entries. * Implementations must handle TTL-based expiry internally. */ export interface RateLimitStore { /** Get the current window entry for a key, or null if expired/missing. */ get(key: string): Promise; /** Set or overwrite the window entry for a key. */ set(key: string, entry: WindowEntry): Promise; /** Increment the count for a key. Returns the new count. */ increment(key: string): Promise; /** * Atomically increment count only if below limit. * Returns the new count and whether the increment happened. * This prevents race conditions where concurrent requests all pass * a non-atomic read-check-increment sequence. */ incrementIfBelow(key: string, limit: number): Promise<{ count: number; incremented: boolean; }>; /** Remove expired entries older than the given cutoff timestamp. */ cleanup(cutoffMs: number): Promise; /** Clear all entries. */ clear(): Promise; /** Release any resources (timers, connections). */ close(): Promise; } export declare class InMemoryRateLimitStore implements RateLimitStore { private windows; get(key: string): Promise; set(key: string, entry: WindowEntry): Promise; increment(key: string): Promise; incrementIfBelow(key: string, limit: number): Promise<{ count: number; incremented: boolean; }>; cleanup(cutoffMs: number): Promise; clear(): Promise; close(): Promise; } /** Minimal PGlite interface - avoids importing the full @electric-sql/pglite package. */ interface PGliteInstance { exec(query: string): Promise; query>(query: string, params?: unknown[]): Promise<{ rows: T[]; }>; close(): Promise; } interface PGliteRateLimitStoreOptions { /** PGlite instance (caller owns lifecycle unless closeOnDestroy is true). */ db: PGliteInstance; /** Close the PGlite instance when close() is called (default: false). */ closeOnDestroy?: boolean; } export declare class PGliteRateLimitStore implements RateLimitStore { private db; private ready; private closeOnDestroy; constructor(options: PGliteRateLimitStoreOptions); private init; get(key: string): Promise; set(key: string, entry: WindowEntry): Promise; increment(key: string): Promise; incrementIfBelow(key: string, limit: number): Promise<{ count: number; incremented: boolean; }>; cleanup(cutoffMs: number): Promise; clear(): Promise; close(): Promise; } export {}; //# sourceMappingURL=rate-limit-store.d.ts.map