Koa Compressor
raw JSON →Koa Compressor is a middleware for Koa.js designed to simplify HTTP response compression by *always* applying gzip compression. Unlike more configurable alternatives like `koa-compress`, this package explicitly ignores the `Accept-Encoding` header, does not set a `Vary` header, and lacks the `this.compress` option for manual control. It sets the `Content-Length` header on compressed bodies. The package is currently at version 1.0.3, with its last publish approximately seven years ago, indicating it is no longer actively maintained. This makes it primarily compatible with older Koa 1.x applications which rely on generator-based middleware, and it lacks support for modern Koa 2.x+ async/await middleware patterns or contemporary compression algorithms like Brotli or Zstandard.
Common errors
error TypeError: app.use() requires a generator function ↓
koa-compress which is compatible with modern Koa versions and maintained. error Error: Cannot find module 'koa-compressor' or import 'koa-compressor' ↓
npm install koa-compressor). If using Node.js without an ESM setup (which is likely for this older package), use const compressor = require('koa-compressor');. For ESM projects, this package is not suitable; use koa-compress instead. Warnings
breaking This package is fundamentally incompatible with Koa 2.x and later versions. `koa-compressor` was designed for Koa 1.x, which uses generator functions (`function*`) for middleware. Koa 2.x+ transitioned to async/await functions, leading to runtime errors if used directly. ↓
gotcha `koa-compressor` *always* compresses responses using gzip and intentionally ignores the `Accept-Encoding` header from the client. It also does not set a `Vary` header. This behavior is a core design choice but deviates significantly from standard HTTP compression middleware (`koa-compress`) and can lead to unexpected behavior if clients do not support gzip or if downstream caches expect a `Vary` header. ↓
deprecated The `koa-compressor` package is abandoned, with its last release approximately seven years ago. It no longer receives updates, bug fixes, or security patches, making it a potential security risk due to unpatched vulnerabilities in its dependencies or Node.js compatibility issues. ↓
Install
npm install koa-compressor yarn add koa-compressor pnpm add koa-compressor Imports
- compressor wrong
import compressor from 'koa-compressor';correctconst compressor = require('koa-compressor'); - KoaApp wrong
import Koa from 'koa'; const app = new Koa();correctconst Koa = require('koa'); const app = Koa();
Quickstart
const Koa = require('koa');
const compressor = require('koa-compressor');
const app = Koa(); // Koa 1.x application initialization
app.use(compressor());
app.use(function* () {
// Koa 1.x middleware uses generator functions (function*)
this.type = 'text/plain';
this.body = 'This response will always be gzipped by koa-compressor, regardless of Accept-Encoding headers.';
});
app.listen(3000, () => {
console.log('Koa 1.x app with koa-compressor listening on http://localhost:3000');
});