What is koa-compress?
The koa-compress package is a middleware for the Koa framework that provides HTTP compression. It supports various compression algorithms like gzip, deflate, and brotli, which can significantly reduce the size of the response body and improve the performance of web applications.
What are koa-compress's main functionalities?
Basic Gzip Compression
This code demonstrates how to set up basic gzip compression in a Koa application using the koa-compress middleware. The middleware is added to the Koa app, and it will automatically compress the response body using gzip.
const Koa = require('koa');
const compress = require('koa-compress');
const app = new Koa();
app.use(compress());
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Custom Compression Options
This code shows how to configure custom options for the compression middleware. The 'threshold' option specifies the minimum response size in bytes to compress, and the 'flush' option is used to control the zlib flush mode.
const Koa = require('koa');
const compress = require('koa-compress');
const app = new Koa();
app.use(compress({
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH
}));
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Brotli Compression
This code demonstrates how to enable Brotli compression with custom parameters. The 'br' option is used to configure Brotli-specific settings, such as the compression quality.
const Koa = require('koa');
const compress = require('koa-compress');
const zlib = require('zlib');
const app = new Koa();
app.use(compress({
br: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
}
}
}));
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Other packages similar to koa-compress
compression
The 'compression' package is a middleware for Express.js that provides HTTP compression. It supports gzip and deflate algorithms. Compared to koa-compress, it is designed specifically for Express.js and does not support Brotli compression out of the box.
shrink-ray-current
The 'shrink-ray-current' package is a middleware for Node.js that provides HTTP compression using gzip, deflate, and Brotli algorithms. It is similar to koa-compress in terms of supported algorithms but can be used with various frameworks, not just Koa.
Koa Compress
Compress middleware for Koa
Example
const compress = require('koa-compress')
const Koa = require('koa')
const app = new Koa()
app.use(compress({
filter (content_type) {
return /text/i.test(content_type)
},
threshold: 2048,
gzip: {
flush: require('zlib').constants.Z_SYNC_FLUSH
},
deflate: {
flush: require('zlib').constants.Z_SYNC_FLUSH,
},
br: false
}))
Maintainers
Options
filter<Function>
function (mimeType: string): Boolean {
}
An optional function that checks the response content type to decide whether to compress.
By default, it uses compressible.
options.threshold<String|Number>
Minimum response size in bytes to compress.
Default 1024
bytes or 1kb
.
options[encoding]<Object>
The current encodings are, in order of preference: br
, gzip
, deflate
.
Setting options[encoding] = {}
will pass those options to the encoding function.
Setting options[encoding] = false
will disable that encoding.
options.br
Brotli compression is supported in node v11.7.0+, which includes it natively.
As of v5.1.0, the default quality level is 4 for performance reasons.
options.defaultEncoding<String>
An optional string, which specifies what encoders to use for requests without
Accept-Encoding.
Default identity
.
The standard dictates to treat such requests as *
meaning that all compressions are permissible,
yet it causes very practical problems when debugging servers with manual tools like curl
, wget
, and so on.
If you want to enable the standard behavior, just set defaultEncoding
to *
.
Manually turning compression on and off
You can always enable compression by setting ctx.compress = true
.
You can always disable compression by setting ctx.compress = false
.
This bypasses the filter check.
app.use((ctx, next) => {
ctx.compress = true
ctx.body = fs.createReadStream(file)
})