From 64a2ef4a18b74a409a41f5616d523c52b7fabe15 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 17 Jan 2026 00:34:00 +0000 Subject: [PATCH] refactor: simplify env var substitution scan --- src/config/env-substitution.ts | 63 ++++++++++++++++------------------ 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/src/config/env-substitution.ts b/src/config/env-substitution.ts index 0fee5386c..f2f670d77 100644 --- a/src/config/env-substitution.ts +++ b/src/config/env-substitution.ts @@ -43,30 +43,17 @@ function isPlainObject(value: unknown): value is Record { ); } -function readEnvVarName( - value: string, - start: number, -): { name: string | null; end: number } { - const end = value.indexOf("}", start); - if (end === -1) { - return { name: null, end: -1 }; - } - - const name = value.slice(start, end); - if (!ENV_VAR_NAME_PATTERN.test(name)) { - return { name: null, end: -1 }; - } - - return { name, end }; -} - function substituteString(value: string, env: NodeJS.ProcessEnv, configPath: string): string { - let result = ""; + if (!value.includes("$")) { + return value; + } + + const chunks: string[] = []; for (let i = 0; i < value.length; i += 1) { const char = value[i]; if (char !== "$") { - result += char; + chunks.push(char); continue; } @@ -75,33 +62,41 @@ function substituteString(value: string, env: NodeJS.ProcessEnv, configPath: str // Escaped: $${VAR} -> ${VAR} if (next === "$" && afterNext === "{") { - const { name, end } = readEnvVarName(value, i + 3); - if (name !== null) { - result += `\${${name}}`; - i = end; - continue; + const start = i + 3; + const end = value.indexOf("}", start); + if (end !== -1) { + const name = value.slice(start, end); + if (ENV_VAR_NAME_PATTERN.test(name)) { + chunks.push(`\${${name}}`); + i = end; + continue; + } } } // Substitution: ${VAR} -> value if (next === "{") { - const { name, end } = readEnvVarName(value, i + 2); - if (name !== null) { - const envValue = env[name]; - if (envValue === undefined || envValue === "") { - throw new MissingEnvVarError(name, configPath); + const start = i + 2; + const end = value.indexOf("}", start); + if (end !== -1) { + const name = value.slice(start, end); + if (ENV_VAR_NAME_PATTERN.test(name)) { + const envValue = env[name]; + if (envValue === undefined || envValue === "") { + throw new MissingEnvVarError(name, configPath); + } + chunks.push(envValue); + i = end; + continue; } - result += envValue; - i = end; - continue; } } // Leave untouched if not a recognized pattern - result += char; + chunks.push(char); } - return result; + return chunks.join(""); } function substituteAny(value: unknown, env: NodeJS.ProcessEnv, path: string): unknown {