Gathering detailed insights and metrics for compression
Gathering detailed insights and metrics for compression
Gathering detailed insights and metrics for compression
Gathering detailed insights and metrics for compression
npm install compression
Typescript
Module System
Min. Node Version
Node Version
NPM Version
98
Supply Chain
99
Quality
80.9
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
4,654,052,443
Last Day
1,093,714
Last Week
20,814,905
Last Month
90,721,441
Last Year
976,009,541
MIT License
2,796 Stars
421 Commits
243 Forks
57 Watchers
12 Branches
42 Contributors
Updated on Jul 01, 2025
Minified
Minified + Gzipped
Latest Version
1.8.0
Package Id
compression@1.8.0
Unpacked Size
26.81 kB
Size
8.92 kB
File Count
5
NPM Version
10.9.2
Node Version
23.5.0
Published on
Feb 10, 2025
Cumulative downloads
Total Downloads
Last Day
-10.7%
1,093,714
Compared to previous day
Last Week
-8.4%
20,814,905
Compared to previous week
Last Month
1.5%
90,721,441
Compared to previous month
Last Year
3.9%
976,009,541
Compared to previous year
Node.js compression middleware.
The following compression codings are supported:
Note Brotli is supported only since Node.js versions v11.7.0 and v10.16.0.
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
1$ npm install compression
1var compression = require('compression')
Returns the compression middleware using the given options
. The middleware
will attempt to compress response bodies for all requests that traverse through
the middleware, based on the given options
.
This middleware will never compress responses that include a Cache-Control
header with the no-transform
directive,
as compressing will transform the body.
compression()
accepts these properties in the options object. In addition to
those listed below, zlib options may be
passed in to the options object or
brotli options.
Type: Number
Default: zlib.constants.Z_DEFAULT_CHUNK
, or 16384
.
See Node.js documentation regarding the usage.
Type: Function
A function to decide if the response should be considered for compression.
This function is called as filter(req, res)
and is expected to return
true
to consider the response for compression, or false
to not compress
the response.
The default filter function uses the compressible
module to determine if res.getHeader('Content-Type')
is compressible.
Type: Number
Default: zlib.constants.Z_DEFAULT_COMPRESSION
, or -1
The level of zlib compression to apply to responses. A higher level will result in better compression, but will take longer to complete. A lower level will result in less compression, but will be much faster.
This is an integer in the range of 0
(no compression) to 9
(maximum
compression). The special value -1
can be used to mean the "default
compression level", which is a default compromise between speed and
compression (currently equivalent to level 6).
-1
Default compression level (also zlib.constants.Z_DEFAULT_COMPRESSION
).0
No compression (also zlib.constants.Z_NO_COMPRESSION
).1
Fastest compression (also zlib.constants.Z_BEST_SPEED
).2
3
4
5
6
(currently what zlib.constants.Z_DEFAULT_COMPRESSION
points to).7
8
9
Best compression (also zlib.constants.Z_BEST_COMPRESSION
).Note in the list above, zlib
is from zlib = require('zlib')
.
Type: Number
Default: zlib.constants.Z_DEFAULT_MEMLEVEL
, or 8
This specifies how much memory should be allocated for the internal compression
state and is an integer in the range of 1
(minimum level) and 9
(maximum
level).
See Node.js documentation regarding the usage.
Type: Object
This specifies the options for configuring Brotli. See Node.js documentation for a complete list of available options.
Type: Number
Default: zlib.constants.Z_DEFAULT_STRATEGY
This is used to tune the compression algorithm. This value only affects the compression ratio, not the correctness of the compressed output, even if it is not set appropriately.
zlib.constants.Z_DEFAULT_STRATEGY
Use for normal data.zlib.constants.Z_FILTERED
Use for data produced by a filter (or predictor).
Filtered data consists mostly of small values with a somewhat random
distribution. In this case, the compression algorithm is tuned to
compress them better. The effect is to force more Huffman coding and less
string matching; it is somewhat intermediate between zlib.constants.Z_DEFAULT_STRATEGY
and zlib.constants.Z_HUFFMAN_ONLY
.zlib.constants.Z_FIXED
Use to prevent the use of dynamic Huffman codes, allowing
for a simpler decoder for special applications.zlib.constants.Z_HUFFMAN_ONLY
Use to force Huffman encoding only (no string match).zlib.constants.Z_RLE
Use to limit match distances to one (run-length encoding).
This is designed to be almost as fast as zlib.constants.Z_HUFFMAN_ONLY
, but give
better compression for PNG image data.Note in the list above, zlib
is from zlib = require('zlib')
.
Type: Number
or String
Default: 1kb
The byte threshold for the response body size before compression is considered for the response. This is a number of bytes or any string accepted by the bytes module.
Note this is only an advisory setting; if the response size cannot be determined
at the time the response headers are written, then it is assumed the response is
over the threshold. To guarantee the response size can be determined, be sure
set a Content-Length
response header.
Type: Number
Default: zlib.constants.Z_DEFAULT_WINDOWBITS
, or 15
See Node.js documentation regarding the usage.
Type: String
Default: identity
This is the default encoding to use when the client does not specify an encoding in the request's Accept-Encoding header.
The default filter
function. This is used to construct a custom filter
function that is an extension of the default function.
1var compression = require('compression') 2var express = require('express') 3 4var app = express() 5 6app.use(compression({ filter: shouldCompress })) 7 8function shouldCompress (req, res) { 9 if (req.headers['x-no-compression']) { 10 // don't compress responses with this request header 11 return false 12 } 13 14 // fallback to standard filter function 15 return compression.filter(req, res) 16}
This module adds a res.flush()
method to force the partially-compressed
response to be flushed to the client.
When using this module with express, simply app.use
the module as
high as you like. Requests that pass through the middleware will be compressed.
1var compression = require('compression') 2var express = require('express') 3 4var app = express() 5 6// compress all responses 7app.use(compression()) 8 9// add all routes
1var compression = require('compression')({ threshold: 0 }) 2var http = require('http') 3 4function createServer (fn) { 5 return http.createServer(function (req, res) { 6 compression(req, res, function (err) { 7 if (err) { 8 res.statusCode = err.status || 500 9 res.end(err.message) 10 return 11 } 12 13 fn(req, res) 14 }) 15 }) 16} 17 18var server = createServer(function (req, res) { 19 res.setHeader('Content-Type', 'text/plain') 20 res.end('hello world!') 21}) 22 23server.listen(3000, () => { 24 console.log('> Listening at http://localhost:3000') 25})
Because of the nature of compression this module does not work out of the box with server-sent events. To compress content, a window of the output needs to be buffered up in order to get good compression. Typically when using server-sent events, there are certain block of data that need to reach the client.
You can achieve this by calling res.flush()
when you need the data written to
actually make it to the client.
1var compression = require('compression') 2var express = require('express') 3 4var app = express() 5 6// compress responses 7app.use(compression()) 8 9// server-sent event stream 10app.get('/events', function (req, res) { 11 res.setHeader('Content-Type', 'text/event-stream') 12 res.setHeader('Cache-Control', 'no-cache') 13 14 // send a ping approx every 2 seconds 15 var timer = setInterval(function () { 16 res.write('data: ping\n\n') 17 18 // !!! this is the important part 19 res.flush() 20 }, 2000) 21 22 res.on('close', function () { 23 clearInterval(timer) 24 }) 25})
The Express.js project welcomes all constructive contributions. Contributions take many forms, from code for bug fixes and enhancements, to additions and fixes to documentation, additional tests, triaging incoming pull requests and issues, and more!
See the Contributing Guide for more technical details on contributing.
No vulnerabilities found.
Reason
update tool detected
Details
Reason
no binaries found in the repo
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
project has 19 contributing companies or organizations
Details
Reason
Found 24/25 approved changesets -- score normalized to 9
Reason
6 commit(s) and 4 issue activity found in the last 90 days -- score normalized to 8
Reason
SAST tool detected but not run on all commits
Details
Reason
25 out of 28 merged PRs checked by a CI test -- score normalized to 8
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
branch protection not enabled on development/release branches
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2025-06-30T21:24:23Z
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More