fix: harden session event proxy errors
This commit is contained in:
+18
-3
@@ -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 代理失败',
|
||||
),
|
||||
});
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user