Merge pull request 'fix: disable gzip and pad SSE events to prevent proxy buffering' (#9) from fix/sse-gzip-buffering into main

Reviewed-on: #9
This commit was merged in pull request #9.
This commit is contained in:
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>) { const SSE_PAD_TARGET = 4096;
res.write(`data: ${JSON.stringify(data)}\n\n`);
}
/** function sseWrite(res: import('express').Response, data: Record<string, unknown>) {
* Send a 2 KB comment block to push past any proxy buffering threshold. const payload = `data: ${JSON.stringify(data)}\n\n`;
* Nginx and other reverse proxies often buffer the first few KB before const pad = Math.max(0, SSE_PAD_TARGET - payload.length);
* starting to stream — this padding forces the initial flush. // SSE comment lines (": ...") are ignored by the browser but push
*/ // data past proxy buffer thresholds so each event is delivered immediately.
function ssePadding(res: import('express').Response) { res.write(pad > 0 ? `: ${' '.repeat(pad)}\n${payload}` : payload);
res.write(`: ${' '.repeat(2048)}\n\n`);
} }
const router = Router(); const router = Router();
@@ -71,7 +68,6 @@ router.post(
res.setHeader('X-Accel-Buffering', 'no'); res.setHeader('X-Accel-Buffering', 'no');
res.socket?.setNoDelay(true); res.socket?.setNoDelay(true);
res.flushHeaders(); res.flushHeaders();
ssePadding(res);
try { try {
const converted = await convertPdfToStatementStreaming( const converted = await convertPdfToStatementStreaming(

View File

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