Custom compression middleware for Express/Connect with strict algorithm enforcement.
Unlike the standard compression package, xypriss-compression enforces which compression algorithms can be used. If you configure algorithms: ['br', 'deflate'], it will never use gzip, even if the client requests it.
The original compression package has no way to restrict which algorithms are used. It automatically chooses based on the client's Accept-Encoding header, ignoring your configuration.
Example of the problem:
// Using standard 'compression' package
const compression = require("compression");
app.use(
compression({
// This option doesn't exist!
// algorithms: ['br', 'deflate']
})
);
// Client requests gzip → Server uses gzip (no control!)With xypriss-compression:
import compression from "xypriss-compression-pluging";
app.use(
compression({
algorithms: ["br", "deflate"], // STRICTLY ENFORCED
})
);
// Client requests gzip → Server responds with identity (uncompressed)
// Client requests br → Server uses brotli
// Client requests deflate → Server uses deflatenpm install xypriss-compressionimport express from "express";
import compression from "xypriss-compression-pluging";
const app = express();
// Use with default settings (gzip, deflate)
app.use(compression());
app.listen(3000);import compression from "xypriss-compression-pluging";
app.use(
compression({
// Only allow Brotli and Deflate
algorithms: ["br", "deflate"],
// Compression level (1-9 for gzip/deflate, 0-11 for brotli)
level: 6,
// Minimum size to compress (1KB)
threshold: "1kb",
})
);app.use(
compression({
algorithms: ["br", "gzip"],
level: 9,
threshold: 2048,
// Custom filter
filter: (req, res) => {
// Don't compress images
const type = res.getHeader("Content-Type");
if (type && type.toString().startsWith("image/")) {
return false;
}
return true;
},
// Brotli-specific options
brotli: {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
},
},
// Gzip-specific options
gzip: {
memLevel: 8,
},
})
);Creates a compression middleware.
| Option | Type | Default | Description |
|---|---|---|---|
algorithms |
Array<'gzip' | 'deflate' | 'br'> |
['gzip', 'deflate'] |
Strictly enforced list of allowed algorithms |
level |
number |
6 |
Compression level (1-9 for gzip/deflate, 0-11 for brotli) |
threshold |
number | string |
1024 |
Minimum response size to compress |
filter |
Function |
shouldCompress |
Custom filter function |
brotli |
BrotliOptions |
{} |
Brotli-specific options |
gzip |
ZlibOptions |
{} |
Gzip-specific options |
deflate |
ZlibOptions |
{} |
Deflate-specific options |
- Client sends request with
Accept-Encoding: gzip, deflate, br - Middleware checks which algorithms are in the
algorithmsconfig - Selects best match from allowed algorithms (priority: br > gzip > deflate)
- If no match, responds with uncompressed data (identity)
When multiple algorithms are allowed and accepted by the client:
- Brotli (br) - Best compression ratio
- Gzip (gzip) - Good compression, widely supported
- Deflate (deflate) - Faster but less efficient
| Feature | compression |
xypriss-compression |
|---|---|---|
| Algorithm enforcement | No | Yes |
| TypeScript | No | Yes |
| Custom filters | Yes | Yes |
| Threshold | Yes | Yes |
| Brotli support | Yes | Yes |
// Only use approved algorithms
app.use(
compression({
algorithms: ["br"], // Company policy: Brotli only
})
);// Prefer speed over compression ratio
app.use(
compression({
algorithms: ["deflate"], // Fastest
level: 1,
})
);// Serve Brotli to modern browsers, nothing to old ones
app.use(
compression({
algorithms: ["br"],
filter: (req, res) => {
// Fallback to uncompressed for old browsers
return req.headers["user-agent"]?.includes("Chrome") || false;
},
})
);Enable debug logs:
DEBUG=xypriss:compression node app.jsMIT
Nehonix Team
- Website: nehonix.com
- GitHub: @Nehonix-Team
Based on expressjs/compression but with strict algorithm enforcement.