UNPKG

tanstack-shadcn-table

Version:

A powerful, feature-rich React table component built on top of TanStack Table v8 with shadcn/ui styling. Optimized bundle size with 55% reduction through peer dependencies.

76 lines (75 loc) 2.15 kB
/** * Rate limiting utility class for preventing abuse and DoS attacks. * Tracks requests per identifier within a time window. * * @example * ```tsx * import { RateLimiter } from 'tanstack-shadcn-table/security/rate-limiter'; * * // Create a rate limiter: 100 requests per minute * const limiter = new RateLimiter(100, 60000); * * // Check if request is allowed * if (limiter.isAllowed('user-123')) { * // Process request * } else { * // Rate limit exceeded * } * * // Reset rate limit for specific identifier * limiter.reset('user-123'); * * // Reset all rate limits * limiter.reset(); * ``` * * @public */ export declare class RateLimiter { private requests; private readonly maxRequests; private readonly windowMs; /** * Creates a new RateLimiter instance. * * @param maxRequests - Maximum number of requests allowed in the time window (default: 100) * @param windowMs - Time window in milliseconds (default: 60000 = 1 minute) */ constructor(maxRequests?: number, windowMs?: number); /** * Checks if a request is allowed for the given identifier. * Automatically tracks the request if allowed. * * @param identifier - Unique identifier for the requester (e.g., user ID, IP address) * @returns `true` if the request is allowed, `false` if rate limit is exceeded * * @example * ```tsx * const limiter = new RateLimiter(10, 1000); // 10 requests per second * * if (limiter.isAllowed('user-123')) { * // Process request * } else { * console.warn('Rate limit exceeded'); * } * ``` */ isAllowed(identifier: string): boolean; /** * Resets rate limit tracking for a specific identifier or all identifiers. * * @param identifier - Optional identifier to reset. If not provided, resets all. * * @example * ```tsx * const limiter = new RateLimiter(10, 1000); * * // Reset specific user * limiter.reset('user-123'); * * // Reset all users * limiter.reset(); * ``` */ reset(identifier?: string): void; }