Koa Compressor

raw JSON →
1.0.3 verified Thu Apr 23 auth: no javascript abandoned

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.

error TypeError: app.use() requires a generator function
cause `koa-compressor` is being used with Koa 2.x or later, which expects async/await middleware functions, but `koa-compressor`'s internal middleware is generator-based.
fix
This package is not compatible with Koa 2.x+. Downgrade Koa to 1.x (not recommended for new projects) or, preferably, switch to koa-compress which is compatible with modern Koa versions and maintained.
error Error: Cannot find module 'koa-compressor' or import 'koa-compressor'
cause Attempting to use `import` syntax in a CommonJS module or an incorrect path/missing installation.
fix
Ensure the package is installed (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.
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.
fix For Koa 2.x and later, use `koa-compress` (the actively maintained alternative) which supports async/await middleware and offers more configurable compression options, including Brotli and Zstandard. Do not attempt to use `koa-compressor` with modern Koa applications.
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.
fix If flexible compression (e.g., Brotli, Deflate), client `Accept-Encoding` negotiation, or proper `Vary` header handling is required, use `koa-compress`. `koa-compressor` is suitable only for very specific use cases where unconditional gzip compression is desired and `Accept-Encoding` can be safely ignored (e.g., SPDY/HTTP/2 with specific client constraints).
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.
fix Migrate to `koa-compress` for active maintenance, modern Koa compatibility, broader compression algorithm support, and ongoing security updates.
npm install koa-compressor
yarn add koa-compressor
pnpm add koa-compressor

Demonstrates a basic Koa 1.x application using `koa-compressor` middleware to unconditionally gzip compress all responses.

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');
});