diff --git a/HISTORY.md b/HISTORY.md index fe4e94b..00193ae 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ unreleased ========== - * Add the defaultEncoding option for requests without `Accept-Encoding` header + * Add the enforceEncoding option for requests without `Accept-Encoding` header 1.7.5 / 2024-10-31 ========== diff --git a/README.md b/README.md index 91591b6..d32645e 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ The default value is `zlib.Z_DEFAULT_WINDOWBITS`, or `15`. See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning) regarding the usage. -##### defaultEncoding +##### enforceEncoding This is the default encoding to use when the client does not specify an encoding in the request's [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) header. diff --git a/index.js b/index.js index 6889b3f..bb420e4 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ function compression (options) { // options var filter = opts.filter || shouldCompress var threshold = bytes.parse(opts.threshold) - var defaultEncoding = opts.defaultEncoding || 'identity' + var enforceEncoding = opts.enforceEncoding || 'identity' if (threshold == null) { threshold = 1024 @@ -181,8 +181,8 @@ function compression (options) { var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip']) // if no method is found, use the default encoding - if (encodingSupported.indexOf(defaultEncoding) !== -1 && !req.headers['accept-encoding']) { - method = defaultEncoding === '*' ? 'gzip' : defaultEncoding + if (encodingSupported.indexOf(enforceEncoding) !== -1 && !req.headers['accept-encoding']) { + method = enforceEncoding === '*' ? 'gzip' : enforceEncoding } // negotiation failed @@ -191,10 +191,6 @@ function compression (options) { return } - if (opts.defaultEncoding) { - opts.defaultEncoding = undefined - } - // compression stream debug('%s compression', method) stream = method === 'gzip' diff --git a/test/compression.js b/test/compression.js index 6d1bb25..ad2b147 100644 --- a/test/compression.js +++ b/test/compression.js @@ -658,9 +658,9 @@ describe('compression()', function () { }) }) - describe('defaultEncoding', function () { + describe('enforceEncoding', function () { it('should compress the provided encoding and not the default encoding', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: 'deflate' }, function (req, res) { + var server = createServer({ threshold: 0, enforceEncoding: 'deflate' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) @@ -672,8 +672,8 @@ describe('compression()', function () { .expect(200, 'hello, world', done) }) - it('should not compress when defaultEncoding is identity', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: 'identity' }, function (req, res) { + it('should not compress when enforceEncoding is identity', function (done) { + var server = createServer({ threshold: 0, enforceEncoding: 'identity' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) @@ -685,8 +685,8 @@ describe('compression()', function () { .expect(200, 'hello, world', done) }) - it('should compress when defaultEncoding is gzip', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: 'gzip' }, function (req, res) { + it('should compress when enforceEncoding is gzip', function (done) { + var server = createServer({ threshold: 0, enforceEncoding: 'gzip' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) @@ -698,8 +698,8 @@ describe('compression()', function () { .expect(200, 'hello, world', done) }) - it('should compress when defaultEncoding is deflate', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: 'deflate' }, function (req, res) { + it('should compress when enforceEncoding is deflate', function (done) { + var server = createServer({ threshold: 0, enforceEncoding: 'deflate' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) @@ -711,8 +711,8 @@ describe('compression()', function () { .expect(200, 'hello, world', done) }) - it('should not compress when defaultEncoding is unknown', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: 'bogus' }, function (req, res) { + it('should not compress when enforceEncoding is unknown', function (done) { + var server = createServer({ threshold: 0, enforceEncoding: 'bogus' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) @@ -724,8 +724,8 @@ describe('compression()', function () { .expect(200, 'hello, world', done) }) - it('should be gzip if no accept-encoding is sent when defaultEncoding is *', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: '*' }, function (req, res) { + it('should be gzip if no accept-encoding is sent when enforceEncoding is *', function (done) { + var server = createServer({ threshold: 0, enforceEncoding: '*' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') })