Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

koa-compress

koajs1.6mMIT5.1.1TypeScript support: definitely-typed

Compress middleware for koa

readme

Koa Compress

Node.js CI codecov

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 // disable brotli
}))

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

changelog

SEE https://github.com/koajs/compress/releases

4.0.1 / 2020-04-29

  • fix issue with state being preserved between requests

4.0.0 / 2020-04-27

  • drop support for versions of node below 10
  • add brotli support for versions of node that support it
  • changed compression options; options is no longer passed to each compression function. Use options.gzip = {}, options.br = {}, etc.
  • added compression disabling option: set options.br = false

3.1.0 / 2020-04-15

  • support no-transform @Pawda

3.0.0 / 2018-04-14

  • republish 2.1.0 as it switches to async functions

2.1.0 / 2018-04-13 (unpublished)

  • do not compress if the response is not writable
  • switch testing frameworks to jest
  • implement linting

1.0.8 / 2014-09-14

  • bump compressible

1.0.7 / 2014-05-13

  • bump bytes

1.0.6 / 2014-04-24

  • refactor
  • use statuses

1.0.5 / 2014-04-24

  • bump bytes
  • refactor to use koa-is-json

1.0.4 / 2014-03-

  • skip compression if content-encoding is not set

1.0.3 / 2014-02-24

  • fix for threshold and JSON bodies @logicoder
  • remove app.jsonSpaces support

1.0.2 / 2014-01-21

  • fix for when identity;q=0

1.0.1 / 2014-01-14

  • update compressible

1.0.0 / 2013-13-21

  • use yield* next
  • use compressible