fix: harden session event proxy errors

This commit is contained in:
john
2026-07-03 16:34:05 +08:00
parent ebf06b376b
commit 9e4074474b
2 changed files with 121 additions and 3 deletions
+18 -3
View File
@@ -390,6 +390,20 @@ function sendProxyResponse(res, upstream) {
Readable.fromWeb(upstream.body).pipe(res);
}
function writeSseProxyErrorAndEnd(res, message) {
if (res.writableEnded) return;
if (res.headersSent) {
try {
res.write(`event: error\ndata: ${JSON.stringify({ message })}\n\n`);
} catch {
// The client may already be gone. Ending below is still harmless.
}
res.end();
return;
}
res.status(502).json({ message });
}
function extractSessionId(req, body) {
const fromParams = req.params?.sessionId ?? req.params?.id;
if (fromParams) return fromParams;
@@ -1756,12 +1770,13 @@ export function createTkmindProxy({
if (!res.writableEnded) res.end();
return;
}
res.status(502).json({
message: sanitizeUserFacingProxyMessage(
writeSseProxyErrorAndEnd(
res,
sanitizeUserFacingProxyMessage(
err instanceof Error ? err.message : 'SSE 代理失败',
'SSE 代理失败',
),
});
);
}
};