fix: disable gzip and pad SSE events to prevent proxy buffering #9

Merged
admin merged 1 commits from fix/sse-gzip-buffering into main 2026-03-14 16:46:07 +00:00
2 changed files with 8 additions and 11 deletions

View File

@@ -28,17 +28,14 @@ function isJsonFile(file: { mimetype: string; originalname: string }): boolean {
);
}
function sseWrite(res: import('express').Response, data: Record<string, unknown>) {
res.write(`data: ${JSON.stringify(data)}\n\n`);
}
const SSE_PAD_TARGET = 4096;
/**
* Send a 2 KB comment block to push past any proxy buffering threshold.
* Nginx and other reverse proxies often buffer the first few KB before
* starting to stream — this padding forces the initial flush.
*/
function ssePadding(res: import('express').Response) {
res.write(`: ${' '.repeat(2048)}\n\n`);
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);
}
const router = Router();
@@ -71,7 +68,6 @@ router.post(
res.setHeader('X-Accel-Buffering', 'no');
res.socket?.setNoDelay(true);
res.flushHeaders();
ssePadding(res);
try {
const converted = await convertPdfToStatementStreaming(

View File

@@ -15,6 +15,7 @@ server {
proxy_connect_timeout 5s;
proxy_read_timeout 600s;
proxy_buffering off;
gzip off;
client_max_body_size 15m;
}