From 0de176b025a93db33159215a215325ee1bd984a5 Mon Sep 17 00:00:00 2001 From: David Godinez Date: Thu, 6 Mar 2025 13:14:11 -0700 Subject: [PATCH] fix: avoid exceeding call stack when exporting logs with large number of queries --- packages/log/src/LogExport.ts | 40 +++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/log/src/LogExport.ts b/packages/log/src/LogExport.ts index 08e9c68ed9..7b1e138454 100644 --- a/packages/log/src/LogExport.ts +++ b/packages/log/src/LogExport.ts @@ -26,14 +26,8 @@ function stringifyReplacer(blacklist: string[][]) { currPath.shift(); // check blacklists - for (let i = 0; i < blacklist.length; i += 1) { - if ( - currPath.length === blacklist[i].length && - currPath.every((v, index) => v === blacklist[i][index]) - ) { - // blacklist match - return undefined; - } + if (isOnBlackList(currPath, blacklist)) { + return undefined; } if (value instanceof Map) { @@ -45,6 +39,19 @@ function stringifyReplacer(blacklist: string[][]) { }; } +function isOnBlackList(currPath: string[], blacklist: string[][]): boolean { + for (let i = 0; i < blacklist.length; i += 1) { + if ( + currPath.length === blacklist[i].length && + currPath.every((v, index) => v === blacklist[i][index]) + ) { + // blacklist match + return true; + } + } + return false; +} + /** * Returns a new object that is safe to stringify * All circular references are replaced by the path to the value creating a circular ref @@ -90,12 +97,17 @@ function makeSafeToStringify( // The ref could point to this object or just to another child const curPath = `${path}.${key}`; potentiallyCircularValues.set(valRecord, curPath); - output[key] = makeSafeToStringify( - val as Record, - blacklist, - curPath, - potentiallyCircularValues - ); + // Convert the path to an array and remove the root + const curPathArray = curPath.split('.').slice(1); + // If the path is on the blacklist, it will eventually be replaced by undefined, so avoid the recursive call + if (!isOnBlackList(curPathArray, blacklist)) { + output[key] = makeSafeToStringify( + val as Record, + blacklist, + curPath, + potentiallyCircularValues + ); + } } } });