From 4857bc0478081a43796b45aaa3696fd0edc79d38 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Fri, 13 Sep 2024 09:34:23 -0400 Subject: [PATCH] fix: convert updateId function to iterative instead of recursive --- api/src/unraid-api/graph/id-prefix-plugin.ts | 22 ++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/api/src/unraid-api/graph/id-prefix-plugin.ts b/api/src/unraid-api/graph/id-prefix-plugin.ts index 7d664720c..72be25498 100644 --- a/api/src/unraid-api/graph/id-prefix-plugin.ts +++ b/api/src/unraid-api/graph/id-prefix-plugin.ts @@ -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++; } };