webupdatev1
This commit is contained in:
@@ -1,30 +1,46 @@
|
||||
/**
|
||||
* Response Compression Middleware
|
||||
* Compresses API responses to reduce payload size
|
||||
* High-performance compression with Brotli support
|
||||
*/
|
||||
const compression = require("compression");
|
||||
const zlib = require("zlib");
|
||||
|
||||
const compressionMiddleware = compression({
|
||||
// Only compress responses larger than 1kb
|
||||
threshold: 1024,
|
||||
// Compression level (0-9, higher = better compression but slower)
|
||||
// Only compress responses larger than 512 bytes (lower threshold)
|
||||
threshold: 512,
|
||||
// Level 6 for gzip (balance between speed and ratio)
|
||||
level: 6,
|
||||
// Memory level
|
||||
memLevel: 8,
|
||||
// Use Brotli when available (better compression than gzip)
|
||||
brotli: {
|
||||
enabled: true,
|
||||
zlib: {
|
||||
[zlib.constants.BROTLI_PARAM_QUALITY]: 4, // 0-11, 4 is fast with good compression
|
||||
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
|
||||
},
|
||||
},
|
||||
// Filter function - don't compress already compressed formats
|
||||
filter: (req, res) => {
|
||||
if (req.headers["x-no-compression"]) {
|
||||
return false;
|
||||
}
|
||||
// Check content-type
|
||||
|
||||
const contentType = res.getHeader("Content-Type");
|
||||
if (!contentType) return compression.filter(req, res);
|
||||
|
||||
// Don't compress images, videos, or already compressed formats
|
||||
if (
|
||||
contentType.includes("image/") ||
|
||||
contentType.includes("video/") ||
|
||||
contentType.includes("application/zip") ||
|
||||
contentType.includes("application/pdf")
|
||||
) {
|
||||
const skipTypes = [
|
||||
"image/",
|
||||
"video/",
|
||||
"application/zip",
|
||||
"application/pdf",
|
||||
"application/octet-stream",
|
||||
"application/wasm",
|
||||
"font/",
|
||||
];
|
||||
|
||||
if (skipTypes.some((type) => contentType.includes(type))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user