@push.rocks/smartproxy
Version:
A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.
82 lines (81 loc) • 2.83 kB
TypeScript
/**
* Async utility functions for SmartProxy
* Provides non-blocking alternatives to synchronous operations
*/
/**
* Delays execution for the specified number of milliseconds
* Non-blocking alternative to busy wait loops
* @param ms - Number of milliseconds to delay
* @returns Promise that resolves after the delay
*/
export declare function delay(ms: number): Promise<void>;
/**
* Retry an async operation with exponential backoff
* @param fn - The async function to retry
* @param options - Retry options
* @returns The result of the function or throws the last error
*/
export declare function retryWithBackoff<T>(fn: () => Promise<T>, options?: {
maxAttempts?: number;
initialDelay?: number;
maxDelay?: number;
factor?: number;
onRetry?: (attempt: number, error: Error) => void;
}): Promise<T>;
/**
* Execute an async operation with a timeout
* @param fn - The async function to execute
* @param timeoutMs - Timeout in milliseconds
* @param timeoutError - Optional custom timeout error
* @returns The result of the function or throws timeout error
*/
export declare function withTimeout<T>(fn: () => Promise<T>, timeoutMs: number, timeoutError?: Error): Promise<T>;
/**
* Run multiple async operations in parallel with a concurrency limit
* @param items - Array of items to process
* @param fn - Async function to run for each item
* @param concurrency - Maximum number of concurrent operations
* @returns Array of results in the same order as input
*/
export declare function parallelLimit<T, R>(items: T[], fn: (item: T, index: number) => Promise<R>, concurrency: number): Promise<R[]>;
/**
* Debounce an async function
* @param fn - The async function to debounce
* @param delayMs - Delay in milliseconds
* @returns Debounced function with cancel method
*/
export declare function debounceAsync<T extends (...args: any[]) => Promise<any>>(fn: T, delayMs: number): T & {
cancel: () => void;
};
/**
* Create a mutex for ensuring exclusive access to a resource
*/
export declare class AsyncMutex {
private queue;
private locked;
acquire(): Promise<() => void>;
private release;
runExclusive<T>(fn: () => Promise<T>): Promise<T>;
}
/**
* Circuit breaker for protecting against cascading failures
*/
export declare class CircuitBreaker {
private options;
private failureCount;
private lastFailureTime;
private state;
constructor(options: {
failureThreshold: number;
resetTimeout: number;
onStateChange?: (state: 'closed' | 'open' | 'half-open') => void;
});
execute<T>(fn: () => Promise<T>): Promise<T>;
private onSuccess;
private onFailure;
private setState;
isOpen(): boolean;
getState(): 'closed' | 'open' | 'half-open';
recordSuccess(): void;
recordFailure(): void;
}