fix: polish split-port implementation for merge

- Add 30s timeout to API bridge proxy requests to prevent resource exhaustion
- Extract healthcheck.mjs script (replaces inline node -e in Dockerfile + compose files)
- Add unit tests for runtime port resolution (14 tests, parsePort + resolveRuntimePorts)
- Fix formatting in declare global block
This commit is contained in:
diegosouzapw
2026-02-27 16:29:58 -03:00
parent 344e602b26
commit 01c1bbfe29
6 changed files with 141 additions and 15 deletions

View File

@@ -2,6 +2,8 @@ import http from "node:http";
import type { IncomingMessage, ServerResponse } from "node:http";
import { getRuntimePorts } from "@/lib/runtime/ports";
const PROXY_TIMEOUT_MS = 30_000; // 30s timeout to prevent resource exhaustion
const OPENAI_COMPAT_PATHS = [
/^\/v1(?:\/|$)/,
/^\/chat\/completions(?:\?|$)/,
@@ -25,6 +27,7 @@ function proxyRequest(req: IncomingMessage, res: ServerResponse, dashboardPort:
...req.headers,
host: `127.0.0.1:${dashboardPort}`,
},
timeout: PROXY_TIMEOUT_MS,
},
(targetRes) => {
res.writeHead(targetRes.statusCode || 502, targetRes.headers);
@@ -32,6 +35,18 @@ function proxyRequest(req: IncomingMessage, res: ServerResponse, dashboardPort:
}
);
targetReq.on("timeout", () => {
targetReq.destroy();
if (res.headersSent) return;
res.writeHead(504, { "content-type": "application/json" });
res.end(
JSON.stringify({
error: "api_bridge_timeout",
detail: `Proxy request timed out after ${PROXY_TIMEOUT_MS}ms`,
})
);
});
targetReq.on("error", (error) => {
if (res.headersSent) return;
res.writeHead(502, { "content-type": "application/json" });