fix: convert updateId function to iterative instead of recursive

This commit is contained in:
Eli Bosley
2024-09-13 09:34:23 -04:00
parent c794a1d1a1
commit 4857bc0478

View File

@@ -4,11 +4,25 @@ import { getServerIdentifier } from "@app/core/utils/server-identifier";
const serverId = getServerIdentifier();
const updateId = (obj: any) => {
if (obj && typeof obj === 'object') {
if ('id' in obj && typeof obj.id === 'string') {
obj.id = `${serverId}-${obj.id}`;
const stack = [obj];
let iterations = 0;
// Prevent infinite loops
while (stack.length > 0 && iterations < 100) {
const current = stack.pop();
if (current && typeof current === 'object') {
if ('id' in current && typeof current.id === 'string') {
current.id = `${serverId}-${current.id}`;
}
for (const value of Object.values(current)) {
if (value && typeof value === 'object') {
stack.push(value);
}
}
}
Object.values(obj).forEach((value) => updateId(value));
iterations++;
}
};