Skip to content

new way to pass the configuration to the encodings #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var ENCONDING_OPTIONS = ['br', 'gzip', 'deflate']
var ENCODING_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