From 9576dc64061991577102a0b1cc1df15cd2f01c5d Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sun, 7 Apr 2024 15:31:27 +0200 Subject: [PATCH] Fix problems with Brotli compression on Bun --- svr.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/svr.js b/svr.js index 691c437..f7a35fa 100644 --- a/svr.js +++ b/svr.js @@ -3713,7 +3713,16 @@ if (!cluster.isPrimary) { } else { try { var hdhds = getCustomHeaders(); - if (configJSON.enableCompression === true && ext != "br" && filelen > 256 && isCompressable && zlib.createBrotliCompress && acceptEncoding.match(/\bbr\b/)) { + var brNotImplementedBun = false; + // Bun 1.1 has definition for zlib.createBrotliCompress, but throws an error while invoking the function. + if (process.isBun && enableCompression) { + try { + zlib.createBrotliCompress(); + } catch (err) { + brNotImplementedBun = true; + } + } + if (configJSON.enableCompression === true && ext != "br" && filelen > 256 && isCompressable && !brNotImplementedBun && zlib.createBrotliCompress && acceptEncoding.match(/\bbr\b/)) { hdhds["Content-Encoding"] = "br"; } else if (configJSON.enableCompression === true && ext != "zip" && filelen > 256 && isCompressable && acceptEncoding.match(/\bdeflate\b/)) { hdhds["Content-Encoding"] = "deflate"; @@ -3755,9 +3764,8 @@ if (!cluster.isPrimary) { } }).on("open", function () { try { - res.writeHead(200, http.STATUS_CODES[200], hdhds); var resStream = {}; - if (ext != "br" && filelen > 256 && isCompressable && zlib.createBrotliCompress && acceptEncoding.match(/\bbr\b/)) { + if (ext != "br" && filelen > 256 && isCompressable && !brNotImplementedBun && zlib.createBrotliCompress && acceptEncoding.match(/\bbr\b/)) { resStream = zlib.createBrotliCompress(); resStream.pipe(res); } else if (ext != "zip" && filelen > 256 && isCompressable && acceptEncoding.match(/\bdeflate\b/)) { @@ -3778,12 +3786,14 @@ if (!cluster.isPrimary) { end: false }); } + res.writeHead(200, http.STATUS_CODES[200], hdhds); if (!resStream.write(head)) { resStream.on("drain", afterWriteCallback); } else { process.nextTick(afterWriteCallback); } } else { + res.writeHead(200, http.STATUS_CODES[200], hdhds); readStream.pipe(resStream); } serverconsole.resmessage("Client successfully received content.");