From 0b35719bb6f15acdb7eed1d251f288b46d84f978 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Tue, 14 Jan 2025 14:22:15 -0500 Subject: [PATCH] refactor: simplify encoding negotiation logic --- index.js | 9 +-------- test/compression.js | 2 +- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 198c85b..b526202 100644 --- a/index.js +++ b/index.js @@ -44,8 +44,6 @@ var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/ var SUPPORTED_ENCODING = hasBrotliSupport ? ['br', 'gzip', 'deflate', 'identity'] : ['gzip', 'deflate', 'identity'] var PREFERRED_ENCODING = hasBrotliSupport ? ['br', 'gzip'] : ['gzip'] -var encodingSupported = ['*', 'gzip', 'deflate', 'identity', 'br'] - /** * Compress response data with gzip / deflate. * @@ -195,14 +193,9 @@ function compression (options) { } // compression method - var negotiator = new Negotiator(req) + var negotiator = new Negotiator({ headers: req.headers['accept-encoding'] ? req.headers : { 'accept-encoding': enforceEncoding } }) var method = negotiator.encoding(SUPPORTED_ENCODING, PREFERRED_ENCODING) - // if no method is found, use the default encoding - if (!req.headers['accept-encoding'] && encodingSupported.indexOf(enforceEncoding) !== -1) { - method = enforceEncoding === '*' ? 'gzip' : enforceEncoding - } - // negotiation failed if (!method || method === 'identity') { nocompress('not acceptable') diff --git a/test/compression.js b/test/compression.js index c2c1115..26fa24b 100644 --- a/test/compression.js +++ b/test/compression.js @@ -969,7 +969,7 @@ describe('compression()', function () { request(server) .get('/') .set('Accept-Encoding', '') - .expect('Content-Encoding', 'gzip') + .expect('Content-Encoding', 'br') .expect(200, 'hello, world', done) }) })