fix: use compression package for proper static file compression
This commit is contained in:
parent
9c8dc237c3
commit
2332aa9f1f
1 changed files with 9 additions and 83 deletions
|
|
@ -1,84 +1,10 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import zlib from "zlib";
|
||||
import compression from "compression";
|
||||
|
||||
export function compressionMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||
const acceptEncoding = req.headers["accept-encoding"] || "";
|
||||
|
||||
// Only compress if content-type suggests compressible content
|
||||
const originalSend = res.send;
|
||||
const originalJson = res.json;
|
||||
|
||||
const shouldCompress = (content: any): boolean => {
|
||||
const contentType = res.getHeader("content-type") as string;
|
||||
const contentLength = Buffer.byteLength(typeof content === "string" ? content : JSON.stringify(content));
|
||||
|
||||
// Only compress if content is large enough and is compressible type
|
||||
return contentLength > 1024 &&
|
||||
(contentType?.includes("text/") ||
|
||||
contentType?.includes("application/json") ||
|
||||
contentType?.includes("application/javascript"));
|
||||
};
|
||||
|
||||
const compress = (content: string, encoding: string): Buffer => {
|
||||
const buffer = Buffer.from(content, "utf8");
|
||||
|
||||
if (encoding === "br" && acceptEncoding.includes("br")) {
|
||||
return zlib.brotliCompressSync(buffer, {
|
||||
params: {
|
||||
[zlib.constants.BROTLI_PARAM_QUALITY]: 6,
|
||||
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: buffer.length,
|
||||
},
|
||||
});
|
||||
} else if (encoding === "gzip" && acceptEncoding.includes("gzip")) {
|
||||
return zlib.gzipSync(buffer, { level: 6 });
|
||||
}
|
||||
|
||||
return buffer;
|
||||
};
|
||||
|
||||
// Override res.send
|
||||
res.send = function(content: any) {
|
||||
if (shouldCompress(content)) {
|
||||
const stringContent = typeof content === "string" ? content : JSON.stringify(content);
|
||||
|
||||
if (acceptEncoding.includes("br")) {
|
||||
const compressed = compress(stringContent, "br");
|
||||
res.setHeader("Content-Encoding", "br");
|
||||
res.setHeader("Content-Length", compressed.length);
|
||||
return originalSend.call(this, compressed);
|
||||
} else if (acceptEncoding.includes("gzip")) {
|
||||
const compressed = compress(stringContent, "gzip");
|
||||
res.setHeader("Content-Encoding", "gzip");
|
||||
res.setHeader("Content-Length", compressed.length);
|
||||
return originalSend.call(this, compressed);
|
||||
}
|
||||
}
|
||||
|
||||
return originalSend.call(this, content);
|
||||
};
|
||||
|
||||
// Override res.json
|
||||
res.json = function(content: any) {
|
||||
if (shouldCompress(content)) {
|
||||
const stringContent = JSON.stringify(content);
|
||||
|
||||
if (acceptEncoding.includes("br")) {
|
||||
const compressed = compress(stringContent, "br");
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.setHeader("Content-Encoding", "br");
|
||||
res.setHeader("Content-Length", compressed.length);
|
||||
return originalSend.call(this, compressed);
|
||||
} else if (acceptEncoding.includes("gzip")) {
|
||||
const compressed = compress(stringContent, "gzip");
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.setHeader("Content-Encoding", "gzip");
|
||||
res.setHeader("Content-Length", compressed.length);
|
||||
return originalSend.call(this, compressed);
|
||||
}
|
||||
}
|
||||
|
||||
return originalJson.call(this, content);
|
||||
};
|
||||
|
||||
next();
|
||||
}
|
||||
export const compressionMiddleware = compression({
|
||||
level: 6,
|
||||
threshold: 1024,
|
||||
filter: (req: any, res: any) => {
|
||||
if (req.headers["x-no-compression"]) return false;
|
||||
return compression.filter(req, res);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue