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
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
Node Version
NPM Version
2,775 Stars
407 Commits
242 Forks
58 Watching
3 Branches
41 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-6.7%
3,518,160
Compared to previous day
Last week
0.8%
20,090,348
Compared to previous week
Last month
10.7%
84,880,372
Compared to previous month
Last year
-0.7%
930,734,903
Compared to previous year
Node.js compression middleware.
The following compression codings are supported:
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.
The default value is zlib.Z_DEFAULT_CHUNK
, or 16384
.
See Node.js documentation regarding the usage.
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.
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.Z_DEFAULT_COMPRESSION
).0
No compression (also zlib.Z_NO_COMPRESSION
).1
Fastest compression (also zlib.Z_BEST_SPEED
).2
3
4
5
6
(currently what zlib.Z_DEFAULT_COMPRESSION
points to).7
8
9
Best compression (also zlib.Z_BEST_COMPRESSION
).The default value is zlib.Z_DEFAULT_COMPRESSION
, or -1
.
Note in the list above, zlib
is from zlib = require('zlib')
.
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).
The default value is zlib.Z_DEFAULT_MEMLEVEL
, or 8
.
See Node.js documentation regarding the usage.
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.Z_DEFAULT_STRATEGY
Use for normal data.zlib.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.Z_DEFAULT_STRATEGY
and zlib.Z_HUFFMAN_ONLY
.zlib.Z_FIXED
Use to prevent the use of dynamic Huffman codes, allowing
for a simpler decoder for special applications.zlib.Z_HUFFMAN_ONLY
Use to force Huffman encoding only (no string match).zlib.Z_RLE
Use to limit match distances to one (run-length encoding).
This is designed to be almost as fast as zlib.Z_HUFFMAN_ONLY
, but give
better compression for PNG image data.Note in the list above, zlib
is from zlib = require('zlib')
.
The byte threshold for the response body size before compression is considered
for the response, defaults to 1kb
. 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.
The default value is zlib.Z_DEFAULT_WINDOWBITS
, or 15
.
See Node.js documentation regarding the usage.
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() 5app.use(compression({ filter: shouldCompress })) 6 7function shouldCompress (req, res) { 8 if (req.headers['x-no-compression']) { 9 // don't compress responses with this request header 10 return false 11 } 12 13 // fallback to standard filter function 14 return compression.filter(req, res) 15}
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 or connect, 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
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})
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
17 different organizations found -- score normalized to 10
Details
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
13 commit(s) out of 30 and 6 issue activity out of 30 found in the last 90 days -- score normalized to 10
Reason
no vulnerabilities detected
Reason
12 out of 14 merged PRs checked by a CI test -- score normalized to 8
Reason
SAST tool detected but not run on all commits
Details
Reason
found 15 unreviewed changesets out of 29 -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Score
Last Scanned on 2024-11-25T21:23:39Z
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