forked from svrjs/svrjs
Optimized HTTP compression functionality
This commit is contained in:
parent
ba3a247e49
commit
934f4dd475
1 changed files with 52 additions and 48 deletions
90
svr.js
90
svr.js
|
@ -3630,39 +3630,6 @@ if (!cluster.isPrimary) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to check if compression is allowed for the file
|
|
||||||
function canCompress(path, list) {
|
|
||||||
var canCompress = true;
|
|
||||||
for (var i = 0; i < list.length; i++) {
|
|
||||||
if (createRegex(list[i], true).test(path)) {
|
|
||||||
canCompress = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return canCompress;
|
|
||||||
}
|
|
||||||
|
|
||||||
var isCompressable = true;
|
|
||||||
try {
|
|
||||||
// Check for files not to compressed and compression enabling setting. Also check for browser quirks and adjust compression accordingly
|
|
||||||
if(configJSON.enableCompression !== true || !canCompress(href, dontCompress)) {
|
|
||||||
isCompressable = false; // Compression is disabled
|
|
||||||
} else if (ext != "html" && ext != "htm" && ext != "xhtml" && ext != "xht" && ext != "shtml") {
|
|
||||||
if (/^Mozilla\/4\.[0-9]+(( *\[[^)]*\] *| *)\([^)\]]*\))? *$/.test(req.headers["user-agent"]) && !(/https?:\/\/|[bB][oO][tT]|[sS][pP][iI][dD][eE][rR]|[sS][uU][rR][vV][eE][yY]|MSIE/.test(req.headers["user-agent"]))) {
|
|
||||||
isCompressable = false; // Netscape 4.x doesn't handle compressed data properly outside of HTML documents.
|
|
||||||
} else if (/^w3m\/[^ ]*$/.test(req.headers["user-agent"])) {
|
|
||||||
isCompressable = false; // w3m doesn't handle compressed data properly outside of HTML documents.
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (/^Mozilla\/4\.0[6-8](( *\[[^)]*\] *| *)\([^)\]]*\))? *$/.test(req.headers["user-agent"]) && !(/https?:\/\/|[bB][oO][tT]|[sS][pP][iI][dD][eE][rR]|[sS][uU][rR][vV][eE][yY]|MSIE/.test(req.headers["user-agent"]))) {
|
|
||||||
isCompressable = false; // Netscape 4.06-4.08 doesn't handle compressed data properly.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
callServerError(500, err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle partial content request
|
// Handle partial content request
|
||||||
if (ext != "html" && req.headers["range"]) {
|
if (ext != "html" && req.headers["range"]) {
|
||||||
try {
|
try {
|
||||||
|
@ -3740,22 +3707,59 @@ if (!cluster.isPrimary) {
|
||||||
callServerError(500, err);
|
callServerError(500, err);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Helper function to check if compression is allowed for the file
|
||||||
|
function canCompress(path, list) {
|
||||||
|
var canCompress = true;
|
||||||
|
for (var i = 0; i < list.length; i++) {
|
||||||
|
if (createRegex(list[i], true).test(path)) {
|
||||||
|
canCompress = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return canCompress;
|
||||||
|
}
|
||||||
|
|
||||||
|
var useBrotli = (ext != "br" && filelen > 256 && zlib.createBrotliCompress && acceptEncoding.match(/\bbr\b/));
|
||||||
|
var useDeflate = (ext != "zip" && filelen > 256 && zlib.createBrotliCompress && acceptEncoding.match(/\bdeflate\b/));
|
||||||
|
var useGzip = (ext != "gz" && filelen > 256 && zlib.createBrotliCompress && acceptEncoding.match(/\bgzip\b/));
|
||||||
|
|
||||||
|
var isCompressable = true;
|
||||||
try {
|
try {
|
||||||
var hdhds = getCustomHeaders();
|
// Check for files not to compressed and compression enabling setting. Also check for browser quirks and adjust compression accordingly
|
||||||
var brNotImplementedBun = false;
|
if((!useBrotli && !useDeflate && !useGzip) || configJSON.enableCompression !== true || !canCompress(href, dontCompress)) {
|
||||||
|
isCompressable = false; // Compression is disabled
|
||||||
|
} else if (ext != "html" && ext != "htm" && ext != "xhtml" && ext != "xht" && ext != "shtml") {
|
||||||
|
if (/^Mozilla\/4\.[0-9]+(( *\[[^)]*\] *| *)\([^)\]]*\))? *$/.test(req.headers["user-agent"]) && !(/https?:\/\/|[bB][oO][tT]|[sS][pP][iI][dD][eE][rR]|[sS][uU][rR][vV][eE][yY]|MSIE/.test(req.headers["user-agent"]))) {
|
||||||
|
isCompressable = false; // Netscape 4.x doesn't handle compressed data properly outside of HTML documents.
|
||||||
|
} else if (/^w3m\/[^ ]*$/.test(req.headers["user-agent"])) {
|
||||||
|
isCompressable = false; // w3m doesn't handle compressed data properly outside of HTML documents.
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (/^Mozilla\/4\.0[6-8](( *\[[^)]*\] *| *)\([^)\]]*\))? *$/.test(req.headers["user-agent"]) && !(/https?:\/\/|[bB][oO][tT]|[sS][pP][iI][dD][eE][rR]|[sS][uU][rR][vV][eE][yY]|MSIE/.test(req.headers["user-agent"]))) {
|
||||||
|
isCompressable = false; // Netscape 4.06-4.08 doesn't handle compressed data properly.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
callServerError(500, err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Bun 1.1 has definition for zlib.createBrotliCompress, but throws an error while invoking the function.
|
// Bun 1.1 has definition for zlib.createBrotliCompress, but throws an error while invoking the function.
|
||||||
if (process.isBun && configJSON.enableCompression === true) {
|
if (process.isBun && useBrotli && isCompressable) {
|
||||||
try {
|
try {
|
||||||
zlib.createBrotliCompress();
|
zlib.createBrotliCompress();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
brNotImplementedBun = true;
|
useBrotli = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (configJSON.enableCompression === true && ext != "br" && filelen > 256 && isCompressable && !brNotImplementedBun && zlib.createBrotliCompress && acceptEncoding.match(/\bbr\b/)) {
|
|
||||||
|
try {
|
||||||
|
var hdhds = getCustomHeaders();
|
||||||
|
if (useBrotli && isCompressable) {
|
||||||
hdhds["Content-Encoding"] = "br";
|
hdhds["Content-Encoding"] = "br";
|
||||||
} else if (configJSON.enableCompression === true && ext != "zip" && filelen > 256 && isCompressable && acceptEncoding.match(/\bdeflate\b/)) {
|
} else if (useDeflate && isCompressable) {
|
||||||
hdhds["Content-Encoding"] = "deflate";
|
hdhds["Content-Encoding"] = "deflate";
|
||||||
} else if (configJSON.enableCompression === true && ext != "gz" && filelen > 256 && isCompressable && acceptEncoding.match(/\bgzip\b/)) {
|
} else if (useGzip && isCompressable) {
|
||||||
hdhds["Content-Encoding"] = "gzip";
|
hdhds["Content-Encoding"] = "gzip";
|
||||||
} else {
|
} else {
|
||||||
if (ext == "html") {
|
if (ext == "html") {
|
||||||
|
@ -3794,13 +3798,13 @@ if (!cluster.isPrimary) {
|
||||||
}).on("open", function () {
|
}).on("open", function () {
|
||||||
try {
|
try {
|
||||||
var resStream = {};
|
var resStream = {};
|
||||||
if (ext != "br" && filelen > 256 && isCompressable && !brNotImplementedBun && zlib.createBrotliCompress && acceptEncoding.match(/\bbr\b/)) {
|
if (useBrotli && isCompressable) {
|
||||||
resStream = zlib.createBrotliCompress();
|
resStream = zlib.createBrotliCompress();
|
||||||
resStream.pipe(res);
|
resStream.pipe(res);
|
||||||
} else if (ext != "zip" && filelen > 256 && isCompressable && acceptEncoding.match(/\bdeflate\b/)) {
|
} else if (useDeflate && isCompressable) {
|
||||||
resStream = zlib.createDeflateRaw();
|
resStream = zlib.createDeflateRaw();
|
||||||
resStream.pipe(res);
|
resStream.pipe(res);
|
||||||
} else if (ext != "gz" && filelen > 256 && isCompressable && acceptEncoding.match(/\bgzip\b/)) {
|
} else if (useGzip && isCompressable) {
|
||||||
resStream = zlib.createGzip();
|
resStream = zlib.createGzip();
|
||||||
resStream.pipe(res);
|
resStream.pipe(res);
|
||||||
} else {
|
} else {
|
||||||
|
|
Reference in a new issue