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 (codex): rewrite fallback-expression port defaults (process.env.PORT || N)

[._.]/ Adam Eivy475517df0aac
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index 695c407cd7..a9ed95f8ba 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -497,7 +497,8 @@ function findValuePortRegions(content) {
* `ports: {...}` object (arbitrary keys — matched by value),
* 2. `--port <n>` tokens in args strings,
* 3. port-ish env keys (`PORT`, `*_PORT`, `VITE_PORT`) and `port:`,
- * 4. top-level port constants (`const API_PORT = <n>`).
+ * 4. top-level port constants (`const API_PORT = <n>`),
+ * 5. fallback-expression defaults (`PORT: process.env.PORT || <n>`).
*
* Each old value is first swapped to a unique placeholder, then placeholders
* resolve to new values — so a remap like 6000→6001, 6001→6002 can't chain.
@@ -555,6 +556,18 @@ export function rewriteEcosystemPorts(content, remap) {
);
});
+ // 5) Fallback-expression defaults — `PORT: process.env.PORT || 4420`.
+ // parseEcosystemConfig resolves the `|| <n>` literal, so it must be
+ // rewritten too. The value isn't adjacent to the key (step 3 misses it),
+ // so match the port-ish key, then any same-value expression up to `||`.
+ // `[^,}\n]*?` keeps the match inside one value (no comma/brace/newline).
+ pairs.forEach(([oldP], i) => {
+ out = out.replace(
+ new RegExp(`(['"\`]?\\b${KEY}\\b['"\`]?\\s*:\\s*[^,}\\n]*?\\|\\|\\s*['"]?)${oldP}\\b`, 'g'),
+ `$1${ph(i)}`
+ );
+ });
+
// Resolve placeholders → new values.
pairs.forEach(([, newP], i) => {
out = out.split(ph(i)).join(String(newP));
diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.js
index cfcb2f26e9..ed17168389 100644
--- a/server/services/streamingDetect.test.js
+++ b/server/services/streamingDetect.test.js
@@ -368,6 +368,15 @@ module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: API_PORT, CD
expect(parseEcosystemConfig(out).processes[0].ports.api).toBe(6000);
});
+ it('rewrites fallback-expression defaults (process.env.PORT || N) the parser derives', () => {
+ const content = `module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: process.env.PORT || 4420, RETRIES: 4420 } }] };
+`;
+ const out = rewriteEcosystemPorts(content, [[4420, 6000]]);
+ expect(out).toContain('process.env.PORT || 6000');
+ expect(out).toContain('RETRIES: 4420'); // not a port key, and not a `|| N` fallback — untouched
+ expect(parseEcosystemConfig(out).processes[0].ports.api).toBe(6000);
+ });
+
it('is a no-op when remap is empty or only contains identity pairs', () => {
const content = `module.exports = { apps: [{ name: 'x', script: 's.js', env: { PORT: 5173 } }] };`;
expect(rewriteEcosystemPorts(content, [])).toBe(content);