diff --git a/README.md b/README.md index 2eb3845..1f826c5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 32c9c26..b4e0e1d 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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 } } @@ -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 diff --git a/package.json b/package.json index b4834a7..828d025 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/compression.js b/test/compression.js index 4969244..7a7c73d 100644 --- a/test/compression.js +++ b/test/compression.js @@ -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) { @@ -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') @@ -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') }) @@ -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') }) @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -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) {