Fix version number to 0.2.9 and add Brotli compression support (BUG-054)
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
This commit is contained in:
parent
e611609580
commit
170ed444de
5 changed files with 447 additions and 7 deletions
84
src/middleware/compression.ts
Normal file
84
src/middleware/compression.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import zlib from "zlib";
|
||||
|
||||
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();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue