Handle dev TrackerHub poll timeouts in proxy
This commit is contained in:
+33
-8
@@ -10,7 +10,7 @@
|
||||
* /* → localhost:8081 (Modern.js SSR + HMR)
|
||||
*/
|
||||
import express from "express";
|
||||
import { createProxyMiddleware } from "http-proxy-middleware";
|
||||
import { createProxyMiddleware, responseInterceptor } from "http-proxy-middleware";
|
||||
import { HttpsProxyAgent } from "https-proxy-agent";
|
||||
import { execFile, spawn } from "node:child_process";
|
||||
import { resolve } from "node:path";
|
||||
@@ -159,6 +159,25 @@ function normalizeTrackerCookie(cookie) {
|
||||
return [...parts, "Path=/tracker/hub", "SameSite=Lax"].join("; ");
|
||||
}
|
||||
|
||||
function applyTrackerCookieHeaders(proxyRes, res) {
|
||||
const setCookie = proxyRes.headers["set-cookie"];
|
||||
if (Array.isArray(setCookie)) {
|
||||
res.setHeader("set-cookie", setCookie.map(normalizeTrackerCookie));
|
||||
} else if (typeof setCookie === "string") {
|
||||
res.setHeader("set-cookie", normalizeTrackerCookie(setCookie));
|
||||
}
|
||||
}
|
||||
|
||||
function isTrackerLongPollTimeout(proxyRes, req) {
|
||||
const requestUrl = req.originalUrl ?? req.url ?? "";
|
||||
return (
|
||||
req.method === "GET" &&
|
||||
(requestUrl.startsWith("/tracker/hub?id=") ||
|
||||
requestUrl.startsWith("/hub?id=")) &&
|
||||
proxyRes.statusCode === 504
|
||||
);
|
||||
}
|
||||
|
||||
// --- SignalR TrackerHub proxy ---
|
||||
// Browser-direct localhost → platform.test.aeroflot.ru fails CORS. Keep the
|
||||
// hub same-origin in development and let proxy-helper / gost route the
|
||||
@@ -170,15 +189,21 @@ const trackerProxy = createProxyMiddleware({
|
||||
ws: true,
|
||||
logLevel: "warn",
|
||||
pathRewrite: (path) => `/tracker${path}`,
|
||||
selfHandleResponse: true,
|
||||
on: {
|
||||
proxyRes(proxyRes) {
|
||||
const setCookie = proxyRes.headers["set-cookie"];
|
||||
if (Array.isArray(setCookie)) {
|
||||
proxyRes.headers["set-cookie"] = setCookie.map(normalizeTrackerCookie);
|
||||
} else if (typeof setCookie === "string") {
|
||||
proxyRes.headers["set-cookie"] = normalizeTrackerCookie(setCookie);
|
||||
proxyRes: responseInterceptor(async (buffer, proxyRes, req, res) => {
|
||||
applyTrackerCookieHeaders(proxyRes, res);
|
||||
|
||||
if (isTrackerLongPollTimeout(proxyRes, req)) {
|
||||
console.warn(`Tracker long poll timed out upstream, returning empty 200 for ${req.originalUrl ?? req.url}`);
|
||||
res.statusCode = 200;
|
||||
res.statusMessage = "OK";
|
||||
res.setHeader("content-type", "text/plain");
|
||||
return "";
|
||||
}
|
||||
},
|
||||
|
||||
return buffer;
|
||||
}),
|
||||
},
|
||||
...(SYSTEM_PROXY ? { agent: new HttpsProxyAgent(SYSTEM_PROXY) } : {}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user