/**
* Global Resource Tracker
*
* Tracks all Node.js resources (timers, intervals, etc.) to prevent hanging processes
* during testing and ensure proper cleanup on shutdown.
*/
export interface TrackedResource {
id: string;
type: 'timeout' | 'interval' | 'immediate';
handle: NodeJS.Timeout | NodeJS.Immediate;
source: string;
createdAt: Date;
description?: string;
}
declare class GlobalResourceTracker {
private resources;
private nextId;
private enabled;
/**
* Enable or disable tracking (useful for production environments)
*/
setEnabled(enabled: boolean): void;
/**
* Track a setTimeout
*/
trackTimeout(handle: NodeJS.Timeout, source: string, description?: string): string;
/**
* Track a setInterval
*/
trackInterval(handle: NodeJS.Timeout, source: string, description?: string): string;
/**
* Track a setImmediate
*/
trackImmediate(handle: NodeJS.Immediate, source: string, description?: string): string;
/**
* Untrack a resource (called when it's manually cleared)
*/
untrack(id: string): boolean;
/**
* Clear a specific resource
*/
clearResource(id: string): boolean;
/**
* Emergency cleanup - clear ALL tracked resources
*/
emergencyCleanup(): number;
/**
* Get diagnostic information about active resources
*/
getDiagnostics(): {
totalResources: number;
byType: Record<string, number>;
bySource: Record<string, number>;
oldestResource?: TrackedResource;
resources: TrackedResource[];
};
/**
* Log diagnostic information
* Uses stderr to avoid corrupting STDIO protocol
*/
logDiagnostics(): void;
}
export declare const globalResourceTracker: GlobalResourceTracker;
/**
* Wrapper functions that automatically track resources
* Use these instead of native setTimeout/setInterval in application code
*/
export declare function trackedSetTimeout(callback: (...args: any[]) => void, delay: number, source: string, description?: string): NodeJS.Timeout;
export declare function trackedSetInterval(callback: (...args: any[]) => void, delay: number, source: string, description?: string): NodeJS.Timeout;
export declare function trackedClearTimeout(handle: NodeJS.Timeout): void;
export declare function trackedClearInterval(handle: NodeJS.Timeout): void;
export {};