Reduce dev proxy response logging noise

This commit is contained in:
2026-04-30 09:24:47 +03:00
parent 4ea0cd8c17
commit a29cdb8018
+39 -8
View File
@@ -18,6 +18,7 @@ import { tmpdir } from "node:os";
const PUBLIC_PORT = 8080;
const MODERNJS_PORT = 8081;
const API_TARGET = process.env.API_TARGET || "https://flights.test.aeroflot.ru";
const DEBUG_PROXY_BODY = process.env.DEBUG_PROXY_BODY === "1";
// Shared cookie jar so the Ngenix WAF cookie challenge (`ngenix_valid` +
// 307-to-self) only runs once per dev-server lifetime, not per request.
@@ -138,9 +139,7 @@ app.use(["/api", "/flights"], (req, res) => {
});
function execCurlWithFallback(buildArgs, extraArgs, res) {
console.log(`execCurlWithFallback called`);
runCurl([...extraArgs, ...buildArgs(true)], (direct) => {
console.log(`execCurlWithFallback: direct result, isSuccessful=${isSuccessfulUpstream(direct)}`);
if (isSuccessfulUpstream(direct)) {
respondWithCurlResult(direct, res);
return;
@@ -154,10 +153,13 @@ function execCurlWithFallback(buildArgs, extraArgs, res) {
}
function runCurl(args, cb) {
console.log(`runCurl: args=${JSON.stringify(args).substring(0, 200)}...`);
execFile("/usr/bin/curl", args, { maxBuffer: 10 * 1024 * 1024, timeout: 30000 }, (err, stdout, stderr) => {
console.log(`runCurl callback: err=${!!err}, stdout length=${stdout ? stdout.length : 0}, stderr length=${stderr ? stderr.length : 0}`);
console.log(`runCurl stdout preview: ${stdout ? stdout.substring(0, 200) : 'null'}`);
if (err) {
console.warn(`curl failed: ${err.message}`);
}
if (stderr) {
console.warn(`curl stderr: ${stderr.substring(0, 300)}`);
}
cb({ err, stdout: stdout ?? "" });
});
}
@@ -171,7 +173,6 @@ function isSuccessfulUpstream({ err, stdout }) {
}
function respondWithCurlResult({ err, stdout }, res) {
console.log(`respondWithCurlResult: err=${!!err}, stdout length=${stdout ? stdout.length : 0}`);
if (err) {
res.status(502).json({ error: err.message });
return;
@@ -189,8 +190,12 @@ function respondWithCurlResult({ err, stdout }, res) {
const isJson = body.trimStart().startsWith("{") || body.trimStart().startsWith("[");
console.log(`isJson=${isJson}, status=${status}, body length=${body.length}`);
console.log(`body preview: ${body.substring(0, 200)}`);
console.log(
`proxy response: status=${status}, content=${isJson ? "json" : "html"}, bodyLength=${body.length}, ${summarizeBody(body)}`,
);
if (DEBUG_PROXY_BODY) {
console.log(`body preview: ${body.substring(0, 500)}`);
}
res.status(status);
res.set("Content-Type", isJson ? "application/json" : "text/html");
@@ -198,6 +203,32 @@ function respondWithCurlResult({ err, stdout }, res) {
res.send(body);
}
function summarizeBody(body) {
const trimmed = body.trimStart();
if (!trimmed) return "body=empty";
try {
const parsed = JSON.parse(trimmed);
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
if (typeof parsed.days === "string") {
const available = [...parsed.days].filter((d) => d === "1").length;
return `daysLength=${parsed.days.length}, availableDays=${available}`;
}
if (parsed.data && Array.isArray(parsed.data.routes)) {
return `routes=${parsed.data.routes.length}`;
}
return `jsonKeys=${Object.keys(parsed).slice(0, 5).join(",")}`;
}
if (Array.isArray(parsed)) {
return `jsonArrayLength=${parsed.length}`;
}
} catch {
// fall through to non-JSON summary
}
return `bodyPreview=${trimmed.substring(0, 80).replace(/\s+/g, " ")}`;
}
// --- Everything else → Modern.js ---
const modernProxy = createProxyMiddleware({
target: `http://localhost:${MODERNJS_PORT}`,