/* * Copyright: 2026 Contrast Security, Inc * Contact: support@contrastsecurity.com * License: Commercial * NOTICE: This Software and the patented inventions embodied within may only be * used as part of Contrast Security’s commercial offerings. Even though it is * made available through public repositories, use of this Software is subject to * the applicable End User Licensing Agreement found at * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed * between Contrast Security and the End User. The Software may not be reverse * engineered, modified, repackaged, sold, redistributed or otherwise used in a * way not consistent with the End User License Agreement. */ import { ComponentBase, Core as _Core, Hookable, Hooks, kComponentName } from '@contrast/core'; import { Logger } from '@contrast/logger'; interface Core extends _Core { logger: Logger; } /** A class or constructor function, as seen in `new.target`. */ type Constructor = abstract new (...args: any[]) => any; interface HookData { hooked(...args: P): R; orig(...args: P): R; funcKey: string; obj: T; /** `new.target` when called with `new`, otherwise `undefined`. Unlike `obj`, never `this`. */ newTarget?: Constructor; name: string; args: P; } interface ResultHookData extends HookData { result: R; } interface PreHook { (data: HookData): void; } /** * Call `next(data)` with the data the hook received to run the original, or the * next around hook, and return its result. It allocates nothing per call. */ interface AroundHook { (next: (data: HookData) => R, data: HookData): R; } interface PostHook { (data: ResultHookData): void } type Hook = PreHook | AroundHook | PostHook; interface PatchOptions { name: string; patchType: string; pre?: PreHook; around?: AroundHook; post?: PostHook; } interface PatcherHooks { /** Before the pre hooks of every call to a patched function that has hooks. Keep it cheap. */ onFunctionExec(data: HookData, record: PatchRecord): void; /** When a new patch is registered (not for ignored duplicates). `fn` is the hooked function. */ onPatch(funcKey: string, fn: (...args: any[]) => any): void; } /** What patcher knows about one wrapper: its original function and the hooks registered against it. */ interface PatchRecord { /** The original, unwrapped function. */ fn: (...args: any[]) => any; pre: Map>; around: Map>; post: Map>; /** The hooks in registration order. Replaced, never mutated, whenever the matching map changes. */ readonly preList: readonly PreHook[]; readonly postList: readonly PostHook[]; /** The around hooks composed into one function that ends in the original call, or null when there are none. */ readonly aroundChain: ((data: HookData) => unknown) | null; funcKeys: Set; } type PatchesMap = WeakMap<(...args: any[]) => any, PatchRecord>; declare class Patcher extends ComponentBase implements Hookable { static readonly [kComponentName]: 'patcher'; static isFunction(fn: unknown): fn is (...args: any[]) => any; static getDescriptor(obj: object, prop: PropertyKey): PropertyDescriptor | undefined; patch any>( fn: F, options: PatchOptions, ReturnType>, ): F | null; /** * Patches a class constructor. * Use with `typeof MyClass` or `() => MyClass` */ patch any>( ctor: C, options: PatchOptions, InstanceType>, ): C | null; patch( obj: T, method: K, options: T[K] extends abstract new (...args: infer P) => infer R ? PatchOptions : T[K] extends (...args: infer P) => infer R ? PatchOptions : PatchOptions, ): T | null; resetInstrumentation(): void; unpatch any>( fn: T, funcName: string, patchType: string, ): void; unwrap any>(fn: T): T; isContrastHooked any>(fn: T): boolean; hookedFunctionToString any>(fn: T): string; readonly hooks: Hooks; logger: Logger; perf: _Core['perf']; perfIsEnabled: boolean; /** Wrapper -> its {@link PatchRecord}. Lets us detect and unwrap without tagging functions. */ patches: PatchesMap; } declare namespace Patcher { export { Patcher, HookData, ResultHookData, PreHook, AroundHook, PostHook, Hook, PatchOptions, PatchRecord, PatchesMap, PatcherHooks, }; } export = Patcher;