
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
koa-compress
Advanced tools
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.
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);
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.
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.
Compress middleware for Koa
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 // disable brotli
}))
function (mimeType: string): Boolean {
}
An optional function that checks the response content type to decide whether to compress. By default, it uses compressible.
Minimum response size in bytes to compress.
Default 1024
bytes or 1kb
.
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.
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.
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 *
.
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)
})
FAQs
Compress middleware for koa
The npm package koa-compress receives a total of 0 weekly downloads. As such, koa-compress popularity was classified as not popular.
We found that koa-compress demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.