brainy/.recovery-workspace/dist-backup-20250910-141917/utils/rateLimiter.d.ts
David Snelling 8ff382ca3b chore: recovery checkpoint - v3.0 API successfully recovered
CRITICAL CHECKPOINT - DO NOT PUSH TO GITHUB

Recovery Status:
- Successfully recovered brainy.ts from compiled JavaScript
- All core v3.0 API methods functional (add, get, update, delete, relate, find, etc.)
- Neural subsystem intact (562KB embedded patterns, NLP working)
- Augmentation pipeline operational (20+ augmentations)
- HNSW clustering system complete
- Triple Intelligence compiled (needs constructor fix)
- Test suite validates functionality

Changes preserved:
- 898 files with changes from last 3 days
- 144,475 insertions
- All augmentation improvements
- All test coverage enhancements
- Complete v3.0 feature set

This is a LOCAL checkpoint only - contains recovered work after corruption incident.
Created backup in .backups/brainy-full-20250910-151314.tar.gz

Branch: recovery-checkpoint-20250910-151433
Date: Wed Sep 10 03:18:04 PM PDT 2025
2025-09-10 15:18:04 -07:00

160 lines
3.7 KiB
TypeScript

/**
* Rate Limiter for Brainy API
*
* Provides rate limiting without external dependencies like Redis.
* - Uses in-memory storage for single instances
* - Can use S3/R2 for distributed rate limiting
*
* @module rateLimiter
*/
import { BaseStorageAdapter } from '../storage/adapters/baseStorageAdapter.js';
export interface RateLimitConfig {
/**
* Maximum number of requests allowed
*/
maxRequests: number;
/**
* Time window in milliseconds
*/
windowMs: number;
/**
* Optional message to return when rate limit is exceeded
*/
message?: string;
/**
* Whether to use distributed storage (S3/R2) for rate limiting
*/
distributed?: boolean;
/**
* Storage adapter for distributed rate limiting
*/
storage?: BaseStorageAdapter;
/**
* Key prefix for distributed storage
*/
keyPrefix?: string;
}
export interface RateLimitResult {
/**
* Whether the request is allowed
*/
allowed: boolean;
/**
* Number of requests remaining in the current window
*/
remaining: number;
/**
* Time when the rate limit window resets (Unix timestamp)
*/
resetTime: number;
/**
* Total limit for the window
*/
limit: number;
}
/**
* Simple in-memory rate limiter
*/
export declare class RateLimiter {
private config;
private requests;
private cleanupInterval;
constructor(config: RateLimitConfig);
/**
* Check if a request is allowed and update the rate limit
*/
checkLimit(identifier: string): Promise<RateLimitResult>;
/**
* Check rate limit using in-memory storage
*/
private checkMemoryLimit;
/**
* Check rate limit using distributed storage (S3/R2)
*/
private checkDistributedLimit;
/**
* Reset rate limit for a specific identifier
*/
reset(identifier: string): Promise<void>;
/**
* Start cleanup interval to remove expired entries
*/
private startCleanup;
/**
* Stop the rate limiter and cleanup
*/
destroy(): void;
}
/**
* Express/Connect middleware for rate limiting
*/
export declare function rateLimitMiddleware(config: RateLimitConfig): (req: any, res: any, next: any) => Promise<void>;
/**
* Create a rate limiter for use with Brainy
*
* @example
* ```typescript
* // For single instance (in-memory)
* const limiter = createRateLimiter({
* maxRequests: 100,
* windowMs: 15 * 60 * 1000 // 15 minutes
* })
*
* // For distributed (using S3/R2)
* const limiter = createRateLimiter({
* maxRequests: 100,
* windowMs: 15 * 60 * 1000,
* distributed: true,
* storage: myS3Adapter
* })
*
* // Check rate limit
* const result = await limiter.checkLimit('user-123')
* if (!result.allowed) {
* throw new Error('Rate limit exceeded')
* }
* ```
*/
export declare function createRateLimiter(config: RateLimitConfig): RateLimiter;
/**
* Preset configurations for common use cases
*/
export declare const RateLimitPresets: {
/**
* Default API rate limit: 100 requests per 15 minutes
*/
default: {
maxRequests: number;
windowMs: number;
};
/**
* Strict rate limit: 10 requests per minute
*/
strict: {
maxRequests: number;
windowMs: number;
};
/**
* Lenient rate limit: 1000 requests per hour
*/
lenient: {
maxRequests: number;
windowMs: number;
};
/**
* Search endpoint: 30 requests per minute
*/
search: {
maxRequests: number;
windowMs: number;
message: string;
};
/**
* Write operations: 20 requests per minute
*/
write: {
maxRequests: number;
windowMs: number;
message: string;
};
};