UNPKG

@socketsecurity/lib

Version:

Core utilities and infrastructure for Socket.dev security tools

67 lines (66 loc) 2.57 kB
/** * Suppress MaxListenersExceededWarning messages. * This is useful in tests or scripts where multiple listeners are expected. * * @example * import { suppressMaxListenersWarning } from '@socketsecurity/lib/suppress-warnings' * * suppressMaxListenersWarning() */ export declare function suppressMaxListenersWarning(): void; /** * Suppress all process warnings of a specific type. * * @param warningType - The warning type to suppress (e.g., 'DeprecationWarning', 'ExperimentalWarning') * * @example * import { suppressWarningType } from '@socketsecurity/lib/suppress-warnings' * * suppressWarningType('ExperimentalWarning') */ export declare function suppressWarningType(warningType: string): void; /** * Set max listeners on an EventTarget (like AbortSignal) to avoid TypeError. * * By manually setting `kMaxEventTargetListeners` on the target we avoid: * TypeError [ERR_INVALID_ARG_TYPE]: The "emitter" argument must be an * instance of EventEmitter or EventTarget. Received an instance of * AbortSignal * * in some patch releases of Node 18-23 when calling events.getMaxListeners(). * See https://github.com/nodejs/node/pull/56807. * * Instead of calling events.setMaxListeners(n, target) we set the symbol * property directly to avoid depending on 'node:events' module. * * @param target - The EventTarget or AbortSignal to configure * @param maxListeners - Maximum number of listeners (defaults to 10, the Node.js default) * * @example * import { setMaxEventTargetListeners } from '@socketsecurity/lib/suppress-warnings' * * const controller = new AbortController() * setMaxEventTargetListeners(controller.signal) */ export declare function setMaxEventTargetListeners(target: EventTarget | AbortSignal | undefined, maxListeners?: number): void; /** * Restore the original process.emitWarning function. * Call this to re-enable all warnings after suppressing them. */ export declare function restoreWarnings(): void; /** * Suppress warnings temporarily within a callback. * * @param warningType - The warning type to suppress * @param callback - Function to execute with warnings suppressed * @returns The result of the callback * * @example * import { withSuppressedWarnings } from '@socketsecurity/lib/suppress-warnings' * * const result = await withSuppressedWarnings('ExperimentalWarning', async () => { * // Code that triggers experimental warnings * return someValue * }) */ export declare function withSuppressedWarnings<T>(warningType: string, callback: () => T | Promise<T>): Promise<T>;