xypriss-compression-pluging
v1.0.3
Published
Custom compression middleware for XyPriss with strict algorithm enforcement
Maintainers
Readme
XyPriss Compression
Custom compression middleware for Express/Connect with strict algorithm enforcement.
Key Feature
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.
Problem Solved
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 deflateInstallation
npm install xypriss-compressionUsage
Basic Usage
import express from "express";
import compression from "xypriss-compression-pluging";
const app = express();
// Use with default settings (gzip, deflate)
app.use(compression());
app.listen(3000);With Algorithm Enforcement
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",
})
);Advanced Configuration
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,
},
})
);API
compression(options?)
Creates a compression middleware.
Options
| 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 |
How It Works
- 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)
Algorithm Selection Priority
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
Comparison with compression
| Feature | compression | xypriss-compression |
| --------------------- | ------------- | --------------------- |
| Algorithm enforcement | No | Yes |
| TypeScript | No | Yes |
| Custom filters | Yes | Yes |
| Threshold | Yes | Yes |
| Brotli support | Yes | Yes |
Use Cases
Security Compliance
// Only use approved algorithms
app.use(
compression({
algorithms: ["br"], // Company policy: Brotli only
})
);Performance Optimization
// Prefer speed over compression ratio
app.use(
compression({
algorithms: ["deflate"], // Fastest
level: 1,
})
);Modern Browsers Only
// 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;
},
})
);Debugging
Enable debug logs:
DEBUG=xypriss:compression node app.jsLicense
MIT
Author
Nehonix Team
- Website: nehonix.com
- GitHub: @Nehonix-Team
Credits
Based on expressjs/compression but with strict algorithm enforcement.
