From fb41a53f6ac72b3c55b1aa38bab3da40c9ff13ac Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Mon, 9 Jun 2025 22:45:23 +0700 Subject: [PATCH] fix(lib/patcher): call instead hook directly instead of proxy to prevent recursion --- lib/patcher/src/_internal.ts | 42 +++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/patcher/src/_internal.ts b/lib/patcher/src/_internal.ts index 530abb1..d86165e 100644 --- a/lib/patcher/src/_internal.ts +++ b/lib/patcher/src/_internal.ts @@ -65,11 +65,22 @@ export const patchedFunctionProxyHandler = { args = applyHooks(state.before, args) const { instead } = state - let result: ReturnType = Reflect.apply( - instead === undefined ? state.target : instead.proxy, - receiver, - args, - ) + let result: ReturnType + + if (instead === undefined) + result = Reflect.apply(state.target, receiver, args) + else + result = Reflect.apply( + // Call the instead hook directly, not through its proxy + instead.hook!, + receiver, + [ + args, + instead.next === undefined + ? state.target + : instead.next.proxy, + ], + ) result = applyHooks(state.after, result) @@ -83,11 +94,22 @@ export const patchedFunctionProxyHandler = { args = applyHooks(state.before, args) const { instead } = state - let result: InstanceType = Reflect.construct( - instead === undefined ? state.target : instead.proxy, - args, - ctor, - ) + let result: InstanceType + + if (instead === undefined) + result = Reflect.construct(state.target, args, ctor) + else + result = Reflect.construct( + // Call the instead hook directly, not through its proxy + instead.hook!, + [ + args, + instead.next === undefined + ? state.target + : instead.next.proxy, + ], + ctor, + ) result = applyHooks(state.after, result)