Merge pull request #350 from tbc0309/v2

This commit is contained in:
jxxghp
2025-06-29 07:52:27 +08:00
committed by GitHub

View File

@@ -22,17 +22,15 @@ app.use(
proxyReqPathResolver: (req) => {
return `/api${req.url}`
},
// 动态设置超时时间SSE无超时普通请求600秒超时
timeout: (req) => {
const isSSE = req.headers.accept && req.headers.accept.includes('text/event-stream');
return isSSE ? 0 : 600000;
},
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
proxyReqOpts.headers = proxyReqOpts.headers || {};
// 检测是否为SSE请求
const isSSE = srcReq.headers.accept && srcReq.headers.accept.includes('text/event-stream');
// 动态设置超时时间SSE无超时普通请求600秒超时
proxyReqOpts.timeout = isSSE ? 0 : 600000;
if (isSSE) {
// SSE请求的特殊头部设置
proxyReqOpts.headers['Cache-Control'] = 'no-cache';
@@ -62,7 +60,7 @@ app.use(
return proxyResData;
}
},
// 统一错误处理
// 统一错误处理(添加超时错误处理)
proxyErrorHandler: (err, res, next) => {
// 客户端断开连接的正常情况常见于SSE
if (err.code === 'ECONNRESET' || err.code === 'EPIPE') {
@@ -71,6 +69,15 @@ app.use(
return;
}
// 添加超时错误处理
if (err.code === 'ETIMEDOUT') {
console.log('Proxy request timed out:', err.code);
if (!res.headersSent) {
res.status(504).send('Gateway Timeout');
}
return;
}
// 其他错误正常处理
console.error('Proxy error:', err);
next(err);