Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ module.exports.filter = shouldCompress
* @private
*/
var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
var SUPPORTED_ENCODING = ['br', 'gzip', 'deflate', 'identity']
var ENCONDING_OPTIONS = ['br', 'gzip', 'deflate']
var PREFERRED_ENCODING = ['br', 'gzip']

var encodingSupported = ['gzip', 'deflate', 'identity', 'br']

/**
* Compress response data with gzip / deflate.
*
Expand All @@ -50,12 +48,14 @@ var encodingSupported = ['gzip', 'deflate', 'identity', 'br']
*/

function compression (options) {
var opts = options || {}
var optsBrotli = {
...opts.brotli,
const opts = options || {}
const encodingOpts = opts?.encodings
const encodingSupported = ENCONDING_OPTIONS.filter(enc => encodingOpts?.[enc] !== false)
const optsBrotli = {
...encodingOpts?.br,
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 4, // set the default level to a reasonable value with balanced speed/ratio
...opts.brotli?.params
...encodingOpts?.br?.params
}
}

Expand Down Expand Up @@ -187,7 +187,7 @@ function compression (options) {

// compression method
var negotiator = new Negotiator(req)
var method = negotiator.encoding(SUPPORTED_ENCODING, PREFERRED_ENCODING)
var method = negotiator.encoding(encodingSupported, PREFERRED_ENCODING)

// if no method is found, use the default encoding
if (!req.headers['accept-encoding'] && encodingSupported.indexOf(enforceEncoding) !== -1) {
Expand All @@ -202,11 +202,13 @@ function compression (options) {

// compression stream
debug('%s compression', method)
stream = method === 'gzip'
? zlib.createGzip(opts)
: method === 'br'
? zlib.createBrotliCompress(optsBrotli)
: zlib.createDeflate(opts)
if (method === 'gzip') {
stream = zlib.createGzip(encodingOpts?.gzip)
} else if (method === 'br') {
stream = zlib.createBrotliCompress(optsBrotli)
} else if (method === 'deflate') {
stream = zlib.createDeflate(encodingOpts?.deflate)
}

// add buffered listeners to stream
addListeners(stream, stream.on, listeners)
Expand Down
Loading