@j-o-r/sh
Version:
Execute shell commands on Linux-based systems from javascript
101 lines (100 loc) • 3.01 kB
TypeScript
export default AsyncTracker;
export type AsyncHookItem = {
/**
* - Unique async ID.
*/
key: number;
/**
* - Async type (e.g., 'PROMISE').
*/
type: string;
/**
* - Triggering async ID.
*/
triggerAsyncId: number;
/**
* - Call stack at init.
*/
stack: string;
/**
* - Associated resource (e.g., Promise).
*/
resource: any;
};
export type SystemTypes = "PROMISE" | "TIMEOUT" | "PROCESSNEXTTICK" | "TICKOBJECT" | "SCRIPT" | "QUERYWRAP" | "FILEHANDLE" | "HTTP2SESSION" | "HTTP2STREAM" | "ZLIB" | "UDPSENDWRAP" | "WRITEWRAP" | "SHUTDOWNWRAP" | "PROMISEEXECUTOR" | "TCPCONNECTWRAP" | "GETADDRINFOREQWRAP" | "GETNAMEINFOREQWRAP" | "IMMEDIATE" | "TCPWRAP" | "TCPSERVERWRAP" | "UDPWRAP" | "FSREQCALLBACK" | "HTTPPARSER" | "PIPEWRAP" | "PIPECONNECTWRAP" | "STREAMWRAP" | "TTYWRAP" | "PROCESS" | "SIGNALWRAP" | "TIMERWRAP";
/**
* Tracks unresolved async operations using Node's async_hooks.
*
* Detects leaks like hanging Promises, Timers. Used by {@link Test}.
*
* @example
* const tracker = new AsyncTracker();
* tracker.enable('PROMISE');
* // Run code...
* console.log(tracker.report(true)); // Unresolved count + details
*/
declare class AsyncTracker {
/**
* Enables tracking (optionally filter by type).
*
* @param {SystemTypes} [type] - Filter to specific type (e.g., 'PROMISE').
* @throws {Error} Unknown type.
* @example
* tracker.enable('PROMISE');
*/
enable(type?: SystemTypes): void;
/**
* Disables tracking.
*
* @example
* tracker.disable();
*/
disable(): void;
/**
* Clears tracked items.
*
* @example
* tracker.reset();
*/
reset(): void;
/**
* Reports unresolved count (+ verbose details).
*
* @param {boolean} [verbose=false] - Print stack/resource per item.
* @returns {number} Unresolved count.
* @example
* if (tracker.report(true) > 0) console.error('Leaks!');
*/
report(verbose?: boolean): number;
/**
* Gets unresolved items (filtered).
*
* Temporarily disables hook during query.
*
* @param {SystemTypes} [type] - Filter type.
* @returns {AsyncHookItem[]} Array of items.
* @example
* const leaks = tracker.getUnresolved('PROMISE');
*/
getUnresolved(type?: SystemTypes): AsyncHookItem[];
/**
* Gets description for type.
*
* @param {SystemTypes} type - Type.
* @returns {string} Description.
* @throws {Error} Unknown.
* @example
* tracker.getTypeDescription('PROMISE'); // 'Promise'
*/
getTypeDescription(type: SystemTypes): string;
/**
* Registers custom type description.
*
* @param {string} type - Type key (uppercased).
* @param {string} description - Description.
* @example
* tracker.addCustomType('MYTYPE', 'My async op');
*/
addCustomType(type: string, description: string): void;
#private;
}