Skip to content

Commit

Permalink
improve parse options
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Nov 11, 2024
1 parent 983f41d commit df8ad33
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 39 deletions.
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,10 @@ The default value is `zlib.Z_DEFAULT_MEMLEVEL`, or `8`.
See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
regarding the usage.

##### params *(brotli only)* - [key-value object containing indexed Brotli parameters](https://nodejs.org/api/zlib.html#zlib_brotli_constants)

- `zlib.constants.BROTLI_PARAM_MODE`
- `zlib.constants.BROTLI_MODE_GENERIC` (default)
- `zlib.constants.BROTLI_MODE_TEXT`, adjusted for UTF-8 text
- `zlib.constants.BROTLI_MODE_FONT`, adjusted for WOFF 2.0 fonts
- `zlib.constants.BROTLI_PARAM_QUALITY`
- Ranges from `zlib.constants.BROTLI_MIN_QUALITY` to
`zlib.constants.BROTLI_MAX_QUALITY`, with a default of
`4` (which is not node's default but the most optimal).

Note that here the default is set to compression level 4. This is a balanced setting with a very good speed and a very good
compression ratio.
##### brotli

This specifies the options for configuring Brotli. See [Node.js documentation](https://nodejs.org/api/zlib.html#class-brotlioptions) for a complete list of available options.


##### strategy

Expand Down
15 changes: 6 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var Buffer = require('safe-buffer').Buffer
var bytes = require('bytes')
var compressible = require('compressible')
var debug = require('debug')('compression')
var objectAssign = require('object-assign')
var onHeaders = require('on-headers')
var vary = require('vary')
var zlib = require('zlib')
Expand Down Expand Up @@ -55,17 +54,16 @@ var PREFERRED_ENCODING = hasBrotliSupport ? ['br', 'gzip'] : ['gzip']

function compression (options) {
var opts = options || {}
var optsBrotli = opts.brotli || {}

if (hasBrotliSupport) {
// set the default level to a reasonable value with balanced speed/ratio
if (opts.params === undefined) {
opts = objectAssign({}, opts)
opts.params = {}
if (optsBrotli.params === undefined) {
optsBrotli.params = {}
}

if (opts.params[zlib.constants.BROTLI_PARAM_QUALITY] === undefined) {
opts.params = objectAssign({}, opts.params)
opts.params[zlib.constants.BROTLI_PARAM_QUALITY] = 4
if (optsBrotli.params[zlib.constants.BROTLI_PARAM_QUALITY] === undefined) {
optsBrotli.params[zlib.constants.BROTLI_PARAM_QUALITY] = 4
}
}

Expand Down Expand Up @@ -203,13 +201,12 @@ function compression (options) {
nocompress('not acceptable')
return
}

// compression stream
debug('%s compression', method)
stream = method === 'gzip'
? zlib.createGzip(opts)
: method === 'br'
? zlib.createBrotliCompress(opts)
? zlib.createBrotliCompress(optsBrotli)
: zlib.createDeflate(opts)

// add buffered listeners to stream
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"compressible": "~2.0.18",
"debug": "2.6.9",
"negotiator": "~0.6.4",
"object-assign": "~4.1.1",
"on-headers": "~1.0.2",
"safe-buffer": "5.2.1",
"vary": "~1.1.2"
Expand Down
32 changes: 16 additions & 16 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var compression = require('..')
* whether current node version has brotli support
*/
var hasBrotliSupport = 'createBrotliCompress' in zlib
var brotlit = hasBrotliSupport ? it : it.skip
var brotli = hasBrotliSupport ? it : it.skip

describe('compression()', function () {
it('should skip HEAD', function (done) {
Expand Down Expand Up @@ -473,7 +473,7 @@ describe('compression()', function () {
})

describe('when "Accept-Encoding: br"', function () {
brotlit('should respond with br', function (done) {
brotli('should respond with br', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand All @@ -487,11 +487,11 @@ describe('compression()', function () {
})

describe('when "Accept-Encoding: br" and passing compression level', function () {
brotlit('should respond with br', function (done) {
brotli('should respond with br', function (done) {
var params = {}
params[zlib.constants.BROTLI_PARAM_QUALITY] = 11

var server = createServer({ threshold: 0, params: params }, function (req, res) {
var server = createServer({ threshold: 0, brotli: { params: params } }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
Expand All @@ -502,11 +502,11 @@ describe('compression()', function () {
.expect('Content-Encoding', 'br', done)
})

brotlit('shouldn\'t break compression when gzip is requested', function (done) {
brotli('shouldn\'t break compression when gzip is requested', function (done) {
var params = {}
params[zlib.constants.BROTLI_PARAM_QUALITY] = 8

var server = createServer({ threshold: 0, params: params }, function (req, res) {
var server = createServer({ threshold: 0, brotli: { params: params } }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
Expand Down Expand Up @@ -547,8 +547,8 @@ describe('compression()', function () {
})

describe('when "Accept-Encoding: gzip, br"', function () {
var brotlit = hasBrotliSupport ? it : it.skip
brotlit('should respond with br', function (done) {
var brotli = hasBrotliSupport ? it : it.skip
brotli('should respond with br', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand All @@ -560,9 +560,9 @@ describe('compression()', function () {
.expect('Content-Encoding', 'br', done)
})

brotlit = hasBrotliSupport ? it.skip : it
brotli = hasBrotliSupport ? it.skip : it

brotlit('should respond with gzip', function (done) {
brotli('should respond with gzip', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand All @@ -576,7 +576,7 @@ describe('compression()', function () {
})

describe('when "Accept-Encoding: deflate, gzip, br"', function () {
brotlit('should respond with br', function (done) {
brotli('should respond with br', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand All @@ -590,7 +590,7 @@ describe('compression()', function () {
})

describe('when "Accept-Encoding: gzip;q=1, br;q=0.3"', function () {
brotlit('should respond with gzip', function (done) {
brotli('should respond with gzip', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand All @@ -604,7 +604,7 @@ describe('compression()', function () {
})

describe('when "Accept-Encoding: gzip, br;q=0.8"', function () {
brotlit('should respond with gzip', function (done) {
brotli('should respond with gzip', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand All @@ -618,7 +618,7 @@ describe('compression()', function () {
})

describe('when "Accept-Encoding: gzip;q=0.001"', function () {
brotlit('should respond with gzip', function (done) {
brotli('should respond with gzip', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand All @@ -632,7 +632,7 @@ describe('compression()', function () {
})

describe('when "Accept-Encoding: deflate, br"', function () {
brotlit('should respond with br', function (done) {
brotli('should respond with br', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
Expand Down Expand Up @@ -783,7 +783,7 @@ describe('compression()', function () {
.end()
})

brotlit('should flush small chunks for brotli', function (done) {
brotli('should flush small chunks for brotli', function (done) {
var chunks = 0
var next
var server = createServer({ threshold: 0 }, function (req, res) {
Expand Down

0 comments on commit df8ad33

Please sign in to comment.