import { Debugger } from "debug"; import { RedisClientType } from "redis"; //#region src/EntrolyticsRedisClient.d.ts export declare const log: Debugger; export declare const DELETED = "__DELETED__"; export declare const CACHE_PREFIX = "entrolytics:"; interface EntrolyticsRedisClientOptions { url: string; prefix?: string; defaultTTL?: number; /** Max reconnection attempts before giving up */ maxReconnectAttempts?: number; /** Connection timeout in milliseconds */ connectTimeout?: number; } interface CacheStats { hits: number; misses: number; hitRate: number; } /** TTL options - accepts number (seconds) or redis-style options object */ type TTLOption = number | { EX?: number; PX?: number; EXAT?: number; PXAT?: number; }; export declare class EntrolyticsRedisClient { url: string; private redisClient; private connected; private connecting; private connectionPromise; prefix: string; defaultTTL: number; private stats; constructor({ url, prefix, defaultTTL, maxReconnectAttempts, connectTimeout }: EntrolyticsRedisClientOptions); /** Access the underlying redis client for advanced operations */ get client(): RedisClientType; /** Check if currently connected */ get isConnected(): boolean; private prefixKey; /** Extract TTL in seconds from TTLOption */ private extractTTL; connect(): Promise; disconnect(): Promise; get(key: string): Promise; /** * Get raw string value without JSON parsing */ getString(key: string): Promise; /** * Get multiple values at once */ mGet(keys: string[]): Promise<(string | null)[]>; /** * Set with expiry (convenience method for setEx pattern) */ setEx(key: string, seconds: number, value: string): Promise; /** * Set a value in cache * @param key - Cache key * @param value - Value to cache (will be JSON serialized) * @param ttl - TTL as number (seconds) or options object { EX?: number, PX?: number } */ set(key: string, value: unknown, ttl?: TTLOption): Promise; del(key: string): Promise; incr(key: string): Promise; incrBy(key: string, amount: number): Promise; decr(key: string): Promise; decrBy(key: string, amount: number): Promise; expire(key: string, seconds: number): Promise; ttl(key: string): Promise; exists(key: string): Promise; zAdd(key: string, score: number, value: string): Promise; zRemRangeByScore(key: string, min: number, max: number): Promise; zCard(key: string): Promise; zRange(key: string, start: number, stop: number): Promise; /** * Rate limiting helper * Returns true if the rate limit has been exceeded */ rateLimit(key: string, limit: number, windowSeconds: number): Promise; /** * Get remaining rate limit count */ getRateLimitRemaining(key: string, limit: number): Promise; /** * Cache-through pattern: fetch from cache or execute query and cache result */ fetch(key: string, query: () => Promise, ttl?: number): Promise; /** * Store data in cache with optional TTL */ store(key: string, data: unknown, ttl?: number): Promise; /** * Remove from cache - soft delete marks as deleted, hard delete removes entirely */ remove(key: string, soft?: boolean): Promise; /** * Invalidate multiple keys by pattern using SCAN (production-safe, non-blocking) */ invalidatePattern(pattern: string): Promise; /** * Get cache statistics */ getStats(): CacheStats; /** * Reset cache statistics */ resetStats(): void; /** * Ping Redis to check connection */ ping(): Promise; /** * Health check for Redis connection */ healthCheck(): Promise<{ ok: boolean; latency: number; error?: string; }>; /** * Flush all keys with the configured prefix */ flushPrefix(): Promise; } //#endregion //#region src/rate-limit.d.ts interface RateLimitConfig { /** * Unique identifier for this rate limit bucket */ bucket: string; /** * Maximum number of requests allowed */ limit: number; /** * Time window in seconds */ window: number; } interface RateLimitResult { success: boolean; limit: number; remaining: number; reset: number; } /** * Predefined rate limit configurations for Entrolytics */ export declare const RATE_LIMITS: { readonly CLI_TOKEN_EXCHANGE: { readonly bucket: "cli:token"; readonly limit: 5; readonly window: 60; }; readonly SHARE_TOKEN_GENERATION: { readonly bucket: "share:token"; readonly limit: 10; readonly window: 60; }; readonly LINK_CREATION: { readonly bucket: "link:create"; readonly limit: 20; readonly window: 60; }; readonly LINK_REDIRECT: { readonly bucket: "link:redirect"; readonly limit: 100; readonly window: 60; }; readonly EVENT_INGESTION: { readonly bucket: "event:ingest"; readonly limit: 1000; readonly window: 60; }; readonly PIXEL_FIRE: { readonly bucket: "pixel:fire"; readonly limit: 500; readonly window: 60; }; readonly API_GENERAL: { readonly bucket: "api:general"; readonly limit: 100; readonly window: 60; }; }; /** * Rate limiting using sliding window algorithm * * Uses Redis if client provided (distributed rate limiting), falls back to * in-memory Map (single instance) if client is null. * * @param client - Redis client instance or null for in-memory fallback * @param identifier - Unique identifier (e.g., IP address, user ID, website ID) * @param config - Rate limit configuration */ export declare function rateLimit(client: EntrolyticsRedisClient | null, identifier: string, config: RateLimitConfig): Promise; /** * Simple counter-based rate limiting (less accurate but simpler) * Uses INCR with expiry - good for high-volume, less precise scenarios * * @param client - Redis client instance * @param identifier - Unique identifier * @param limit - Maximum requests allowed * @param windowSeconds - Time window in seconds * @returns true if rate limit exceeded, false otherwise */ export declare function rateLimitSimple(client: EntrolyticsRedisClient | null, identifier: string, limit: number, windowSeconds: number): Promise; /** * Get rate limit headers for HTTP response */ export declare function getRateLimitHeaders(result: RateLimitResult): Record; /** * Create a rate limiter bound to a specific Redis client * Useful for creating app-specific rate limiters */ interface RateLimiter { check: (identifier: string, config: RateLimitConfig) => Promise; checkSimple: (identifier: string, limit: number, windowSeconds: number) => Promise; getRemaining: (identifier: string, config: RateLimitConfig) => Promise; } export declare function createRateLimiter(client: EntrolyticsRedisClient | null): RateLimiter; //#endregion export type { CacheStats, EntrolyticsRedisClientOptions, RateLimitConfig, RateLimitResult, RateLimiter, TTLOption }; //# sourceMappingURL=index.d.ts.map