Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: brace-counting env-block scan handles nested braces

[._.]/ Adam Eivy8c2e0fc71bba
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index 9453ed3c57..ea6575770b 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -12,6 +12,44 @@ export const NON_PM2_TYPES = new Set(['ios-native', 'macos-native', 'xcode', 'sw
/** Check if an app type uses PM2 for process management */
export const usesPm2 = (type) => !NON_PM2_TYPES.has(type);
+/**
+ * Find the index of the `}` that matches the `{` at `openBraceIdx`, ignoring
+ * braces inside strings and comments. Returns -1 if no match.
+ */
+function findMatchingBrace(content, openBraceIdx) {
+ let depth = 0;
+ let inString = false;
+ let stringChar = null;
+ let inLineComment = false;
+ let inBlockComment = false;
+
+ for (let i = openBraceIdx; i < content.length; i++) {
+ const char = content[i];
+ const nextChar = i < content.length - 1 ? content[i + 1] : '';
+ const prevChar = i > 0 ? content[i - 1] : '';
+
+ if (!inString && !inBlockComment && char === '/' && nextChar === '/') { inLineComment = true; continue; }
+ if (inLineComment && char === '\n') { inLineComment = false; continue; }
+ if (!inString && !inLineComment && char === '/' && nextChar === '*') { inBlockComment = true; continue; }
+ if (inBlockComment && char === '*' && nextChar === '/') { inBlockComment = false; i++; continue; }
+ if (inLineComment || inBlockComment) continue;
+
+ if ((char === '"' || char === "'") && prevChar !== '\\') {
+ if (!inString) { inString = true; stringChar = char; }
+ else if (char === stringChar) { inString = false; stringChar = null; }
+ }
+
+ if (!inString) {
+ if (char === '{') depth++;
+ else if (char === '}') {
+ depth--;
+ if (depth === 0) return i;
+ }
+ }
+ }
+ return -1;
+}
+
/**
* Parse ecosystem.config.js/cjs to extract all processes with their ports
* Uses regex parsing since we can't safely execute arbitrary JS
@@ -217,11 +255,16 @@ export function parseEcosystemConfig(content) {
// Collect every `<NAME>_PORT` env-var first, label after — so CDP_PORT
// can influence the smart label chosen for PORT regardless of source order.
// The `_PORT` suffix requirement avoids matching identifiers like REPORT.
+ // Brace-counting (not `[^}]*`) so env values with nested objects/ternaries
+ // don't truncate the scan at the first inner `}`.
const envPorts = {};
- const envBlockRegex = /(?:env|env_development|env_production)\s*:\s*\{([^}]*)\}/g;
- let envBlockMatch;
- while ((envBlockMatch = envBlockRegex.exec(appBlock)) !== null) {
- const envContent = envBlockMatch[1];
+ const envHeaderRegex = /(?:env|env_development|env_production)\s*:\s*\{/g;
+ let envHeaderMatch;
+ while ((envHeaderMatch = envHeaderRegex.exec(appBlock)) !== null) {
+ const openIdx = envHeaderMatch.index + envHeaderMatch[0].length - 1;
+ const closeIdx = findMatchingBrace(appBlock, openIdx);
+ if (closeIdx < 0) continue;
+ const envContent = appBlock.substring(openIdx + 1, closeIdx);
const portKeyRegex = /\b(PORT|[A-Z][A-Z0-9_]*_PORT)\b\s*:\s*([^,}\n]+)/g;
let m;
while ((m = portKeyRegex.exec(envContent)) !== null) {
diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.js
index 8b43f65fed..f88d9be968 100644
--- a/server/services/streamingDetect.test.js
+++ b/server/services/streamingDetect.test.js
@@ -129,6 +129,31 @@ describe('parseEcosystemConfig', () => {
expect(proc.ports).toEqual({ api: 5580 });
});
+ it('captures *_PORT keys after a nested brace inside the env block', () => {
+ // Env values can contain object spreads/ternaries that introduce nested `}`.
+ // A naive `\\{[^}]*\\}` env-block regex would truncate at the inner `}` and
+ // miss any *_PORT key that follows. Brace-counting handles this correctly.
+ const content = `
+ module.exports = {
+ apps: [
+ {
+ name: 'nested-env-app',
+ env: {
+ ...(process.env.FEATURE_FLAG ? { ENABLED: 'true' } : { ENABLED: 'false' }),
+ PORT: 5600,
+ IPC_PORT: 5601,
+ },
+ },
+ ],
+ };
+ `;
+
+ const { processes } = parseEcosystemConfig(content);
+ const proc = processes.find(p => p.name === 'nested-env-app');
+ expect(proc.ports.api).toBe(5600);
+ expect(proc.ports.ipc).toBe(5601);
+ });
+
it('still honors explicit ports: { ... } literal map (does not double-extract from env)', () => {
const content = `
module.exports = {