fix: yield to event loop after each SSE write to flush socket
The for-await loop over OpenAI stream chunks runs synchronously when data is buffered, causing res.write() calls to queue without flushing. Add setImmediate yield after each progress event so the event loop reaches its I/O phase and pushes data to the network immediately.
This commit is contained in:
@@ -28,14 +28,8 @@ function isJsonFile(file: { mimetype: string; originalname: string }): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
const SSE_PAD_TARGET = 4096;
|
||||
|
||||
function sseWrite(res: import('express').Response, data: Record<string, unknown>) {
|
||||
const payload = `data: ${JSON.stringify(data)}\n\n`;
|
||||
const pad = Math.max(0, SSE_PAD_TARGET - payload.length);
|
||||
// SSE comment lines (": ...") are ignored by the browser but push
|
||||
// data past proxy buffer thresholds so each event is delivered immediately.
|
||||
res.write(pad > 0 ? `: ${' '.repeat(pad)}\n${payload}` : payload);
|
||||
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
||||
}
|
||||
|
||||
const router = Router();
|
||||
|
||||
Reference in New Issue
Block a user