Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid exceeding call stack when exporting logs with large number of queries #2382

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions packages/log/src/LogExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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<string, unknown>,
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<string, unknown>,
blacklist,
curPath,
potentiallyCircularValues
);
}
}
}
});
Expand Down
Loading