Compression plugin for Hono
Drop-in replacement of the built-in Compress Middleware, but with some extra...
- all available compression formats (
zstd,brotli,gzip,deflate) - ultra-fast and 100% type-safe ✨
- best format auto-selection
- streaming response support
- configurable compression level and zlib options
- double-compressed content protection
- content size threshold and custom filtering
- Cloudflare Workers and Deno Deploy runtime detection
- works with Node, Deno and Bun
Leave a star on GitHub if you like it 🙏
npm
npm install hono-compressYarn
yarn add hono-compresspnpm
pnpm add hono-compressBun
bun add hono-compressDeno
deno add hono-compressimport { Hono } from 'hono'
import { compress } from 'hono-compress'
const app = new Hono()
app.use(compress())compress({
encoding,
encodings,
force,
threshold,
zstdLevel,
brotliLevel,
gzipLevel,
options,
filter,
})Defaults to undefined.
The compression format encoding to use to compress the response content. Can be one of the following:
zstdbrgzipdeflate
If not defined, all the formats declared in the option encodings are allowed.
This option is provided primarily to maintain compatibility with hono/compress, use the option encodings to set the wanted compression formats.
Defaults to ['zstd', 'br', 'gzip', 'deflate'].
The compression format encodings allowed to be used to compress the response content.
The first format matching the request accept-encoding is chosen to be used to compress the response content.
Defaults to false.
Forces content compression even if the request accept-encoding and the response content-type cannot be determined.
Use with caution.
Defaults to 1024.
The minimum size in bytes for a response content to be compressed.
Defaults to 2.
Zstandard algorithm compression level (encoding zstd).
Refer to the zstd manual for more details.
Defaults to 4.
Brotli algorithm compression level (encoding br).
Refer to the Brotli specification for more details.
Defaults to 6.
Gzip algorithms compression level (encoding gzip and deflate).
Refer to the zlib manual for more details.
Defaults to {}.
Options passed to the node compression engine to compress content.
Refer to the node zlib documentation for more details.
Defaults to undefined.
An optional function callback to state if the response content should be compressed or not.
Boolean
By default, content compression is disabled on Cloudflare Workers and Deno Deploy, a custom filter can be used to bypass this behavior and force the response to be always compressed:
import type { Context } from 'hono'
compress({
filter: (c: Context) => true,
})This project is a fork of bun-compression, which itself is a fork of elysia-compression.
Both projects were unmaintained and lacked many of the features I was looking for, so I started with them, but ended up improving and expanding many parts, eventually rewriting them from scratch.
This project was also inspired by hono/compress, expressjs/compression and elysia-compress.