1
0
Fork 0
forked from svrjs/svrjs

Fix problems with Brotli compression on Bun

This commit is contained in:
Dorian Niemiec 2024-04-07 15:31:27 +02:00
parent 8978515e49
commit 9576dc6406

16
svr.js
View file

@ -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.");