Skip to content

Commit 91dd763

Browse files
perf(eio): stream compressed polling responses
Pipe gzip/deflate output directly to the polling HTTP response instead of buffering compressed chunks in memory before sending them. This reduces memory usage for large compressed polling payloads while keeping the existing compression threshold and content negotiation behavior. See also: d11e17c (engine.io@1.6.0, Nov 2015)
1 parent ef02660 commit 91dd763

1 file changed

Lines changed: 19 additions & 30 deletions

File tree

packages/engine.io/lib/transports/polling.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -314,40 +314,29 @@ export class Polling extends Transport {
314314
return;
315315
}
316316

317-
this.compress(data, encoding, (err, data) => {
318-
if (err) {
319-
this.res.writeHead(500);
320-
this.res.end();
321-
callback(err);
322-
return;
323-
}
317+
debug("compressing");
324318

325-
headers["Content-Encoding"] = encoding;
326-
respond(data);
327-
});
328-
}
319+
headers["Content-Encoding"] = encoding;
320+
this.res.writeHead(200, this.headers(this.req, headers));
329321

330-
/**
331-
* Compresses data.
332-
*
333-
* @private
334-
*/
335-
private compress(data, encoding, callback) {
336-
debug("compressing");
322+
const stream = compressionMethods[encoding](this.httpCompression);
337323

338-
const buffers = [];
339-
let nread = 0;
324+
let isErrored = false;
340325

341-
compressionMethods[encoding](this.httpCompression)
342-
.on("error", callback)
343-
.on("data", function (chunk) {
344-
buffers.push(chunk);
345-
nread += chunk.length;
346-
})
347-
.on("end", function () {
348-
callback(null, Buffer.concat(buffers, nread));
349-
})
350-
.end(data);
326+
stream.on("error", (err) => {
327+
isErrored = true;
328+
this.res.end();
329+
callback(err);
330+
});
331+
332+
this.res.once("finish", () => {
333+
if (!isErrored) {
334+
callback();
335+
}
336+
});
337+
338+
stream.pipe(this.res);
339+
stream.end(data);
351340
}
352341

353342
/**

0 commit comments

Comments
 (0)