koa-compress (3.1.0)

Published 2026-02-24 13:59:30 +00:00 by atheaadmin

Installation

registry=
npm install koa-compress@3.1.0
"koa-compress": "3.1.0"

About this package

Koa Compress

Node.js CI codecov

Compress middleware for Koa

Example

var compress = require('koa-compress')
var Koa = require('koa')

var app = new Koa()
app.use(compress({
  filter: function (content_type) {
  	return /text/i.test(content_type)
  },
  threshold: 2048,
  flush: require('zlib').Z_SYNC_FLUSH
}))

Options

The options are passed to zlib: http://nodejs.org/api/zlib.html#zlib_options

filter

An optional function that checks the response content type to decide whether to compress. By default, it uses compressible.

threshold

Minimum response size in bytes to compress. Default 1024 bytes or 1kb.

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

Dependencies

Dependencies

ID Version
bytes ^3.0.0
compressible ^2.0.0
koa-is-json ^1.0.0
statuses ^1.0.0

Development Dependencies

ID Version
eslint ^6.0.0
eslint-config-standard ^14.1.1
eslint-plugin-import ^2.9.0
eslint-plugin-node ^11.0.0
eslint-plugin-promise ^4.0.0
eslint-plugin-standard ^4.0.1
jest ^25.3.0
koa ^2.0.1
supertest ^4.0.2
Details
npm
2026-02-24 13:59:30 +00:00
0
Jonathan Ong
MIT
3.0 KiB
Assets (1)
Versions (2) View all
5.1.1 2026-02-24
3.1.0 2026-02-24