Skip to content

Commit fc2ff9b

Browse files
authored
refactor: remove obsolete brotli and http2 support checks (#221)
* refactor: remove obsolete brotli and http2 support checks * fix(docs): remove note about brotli support
1 parent bb29cff commit fc2ff9b

File tree

3 files changed

+22
-53
lines changed

3 files changed

+22
-53
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ The following compression codings are supported:
1515
- gzip
1616
- br (brotli)
1717

18-
**Note** Brotli is supported only since Node.js versions v11.7.0 and v10.16.0.
19-
2018
## Install
2119

2220
This is a [Node.js](https://nodejs.org/en/) module available through the

index.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,13 @@ var zlib = require('zlib')
2929
module.exports = compression
3030
module.exports.filter = shouldCompress
3131

32-
/**
33-
* @const
34-
* whether current node version has brotli support
35-
*/
36-
var hasBrotliSupport = 'createBrotliCompress' in zlib
37-
3832
/**
3933
* Module variables.
4034
* @private
4135
*/
4236
var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
43-
var SUPPORTED_ENCODING = hasBrotliSupport ? ['br', 'gzip', 'deflate', 'identity'] : ['gzip', 'deflate', 'identity']
44-
var PREFERRED_ENCODING = hasBrotliSupport ? ['br', 'gzip'] : ['gzip']
37+
var SUPPORTED_ENCODING = ['br', 'gzip', 'deflate', 'identity']
38+
var PREFERRED_ENCODING = ['br', 'gzip']
4539

4640
var encodingSupported = ['gzip', 'deflate', 'identity', 'br']
4741

@@ -55,16 +49,12 @@ var encodingSupported = ['gzip', 'deflate', 'identity', 'br']
5549

5650
function compression (options) {
5751
var opts = options || {}
58-
var optsBrotli = {}
59-
60-
if (hasBrotliSupport) {
61-
Object.assign(optsBrotli, opts.brotli)
62-
63-
var brotliParams = {}
64-
brotliParams[zlib.constants.BROTLI_PARAM_QUALITY] = 4
65-
66-
// set the default level to a reasonable value with balanced speed/ratio
67-
optsBrotli.params = Object.assign(brotliParams, optsBrotli.params)
52+
var optsBrotli = {
53+
...opts.brotli,
54+
params: {
55+
[zlib.constants.BROTLI_PARAM_QUALITY]: 4, // set the default level to a reasonable value with balanced speed/ratio
56+
...opts.brotli?.params
57+
}
6858
}
6959

7060
// options

test/compression.js

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,10 @@ var crypto = require('crypto')
55
var http = require('http')
66
var request = require('supertest')
77
var zlib = require('zlib')
8-
9-
var describeHttp2 = describe.skip
10-
try {
11-
var http2 = require('http2')
12-
describeHttp2 = describe
13-
} catch (err) {
14-
if (err) {
15-
console.log('http2 tests disabled.')
16-
}
17-
}
8+
var http2 = require('http2')
189

1910
var compression = require('..')
2011

21-
/**
22-
* @const
23-
* whether current node version has brotli support
24-
*/
25-
var hasBrotliSupport = 'createBrotliCompress' in zlib
26-
var brotli = hasBrotliSupport ? it : it.skip
27-
2812
describe('compression()', function () {
2913
it('should skip HEAD', function (done) {
3014
var server = createServer({ threshold: 0 }, function (req, res) {
@@ -321,7 +305,7 @@ describe('compression()', function () {
321305
.expect(200, done)
322306
})
323307

324-
describeHttp2('http2', function () {
308+
describe('http2', function () {
325309
it('should work with http2 server', function (done) {
326310
var server = createHttp2Server({ threshold: 0 }, function (req, res) {
327311
res.setHeader(http2.constants.HTTP2_HEADER_CONTENT_TYPE, 'text/plain')
@@ -517,7 +501,7 @@ describe('compression()', function () {
517501
})
518502

519503
describe('when "Accept-Encoding: br"', function () {
520-
brotli('should respond with br', function (done) {
504+
it('should respond with br', function (done) {
521505
var server = createServer({ threshold: 0 }, function (req, res) {
522506
res.setHeader('Content-Type', 'text/plain')
523507
res.end('hello, world')
@@ -531,7 +515,7 @@ describe('compression()', function () {
531515
})
532516

533517
describe('when "Accept-Encoding: br" and passing compression level', function () {
534-
brotli('should respond with br', function (done) {
518+
it('should respond with br', function (done) {
535519
var params = {}
536520
params[zlib.constants.BROTLI_PARAM_QUALITY] = 11
537521

@@ -546,7 +530,7 @@ describe('compression()', function () {
546530
.expect('Content-Encoding', 'br', done)
547531
})
548532

549-
brotli('shouldn\'t break compression when gzip is requested', function (done) {
533+
it('shouldn\'t break compression when gzip is requested', function (done) {
550534
var params = {}
551535
params[zlib.constants.BROTLI_PARAM_QUALITY] = 8
552536

@@ -591,8 +575,7 @@ describe('compression()', function () {
591575
})
592576

593577
describe('when "Accept-Encoding: gzip, br"', function () {
594-
var brotli = hasBrotliSupport ? it : it.skip
595-
brotli('should respond with br', function (done) {
578+
it('should respond with br', function (done) {
596579
var server = createServer({ threshold: 0 }, function (req, res) {
597580
res.setHeader('Content-Type', 'text/plain')
598581
res.end('hello, world')
@@ -604,9 +587,7 @@ describe('compression()', function () {
604587
.expect('Content-Encoding', 'br', done)
605588
})
606589

607-
brotli = hasBrotliSupport ? it.skip : it
608-
609-
brotli('should respond with gzip', function (done) {
590+
it.skip('should respond with gzip', function (done) {
610591
var server = createServer({ threshold: 0 }, function (req, res) {
611592
res.setHeader('Content-Type', 'text/plain')
612593
res.end('hello, world')
@@ -620,7 +601,7 @@ describe('compression()', function () {
620601
})
621602

622603
describe('when "Accept-Encoding: deflate, gzip, br"', function () {
623-
brotli('should respond with br', function (done) {
604+
it('should respond with br', function (done) {
624605
var server = createServer({ threshold: 0 }, function (req, res) {
625606
res.setHeader('Content-Type', 'text/plain')
626607
res.end('hello, world')
@@ -634,7 +615,7 @@ describe('compression()', function () {
634615
})
635616

636617
describe('when "Accept-Encoding: gzip;q=1, br;q=0.3"', function () {
637-
brotli('should respond with gzip', function (done) {
618+
it('should respond with gzip', function (done) {
638619
var server = createServer({ threshold: 0 }, function (req, res) {
639620
res.setHeader('Content-Type', 'text/plain')
640621
res.end('hello, world')
@@ -648,7 +629,7 @@ describe('compression()', function () {
648629
})
649630

650631
describe('when "Accept-Encoding: gzip, br;q=0.8"', function () {
651-
brotli('should respond with gzip', function (done) {
632+
it('should respond with gzip', function (done) {
652633
var server = createServer({ threshold: 0 }, function (req, res) {
653634
res.setHeader('Content-Type', 'text/plain')
654635
res.end('hello, world')
@@ -662,7 +643,7 @@ describe('compression()', function () {
662643
})
663644

664645
describe('when "Accept-Encoding: gzip;q=0.001"', function () {
665-
brotli('should respond with gzip', function (done) {
646+
it('should respond with gzip', function (done) {
666647
var server = createServer({ threshold: 0 }, function (req, res) {
667648
res.setHeader('Content-Type', 'text/plain')
668649
res.end('hello, world')
@@ -676,7 +657,7 @@ describe('compression()', function () {
676657
})
677658

678659
describe('when "Accept-Encoding: deflate, br"', function () {
679-
brotli('should respond with br', function (done) {
660+
it('should respond with br', function (done) {
680661
var server = createServer({ threshold: 0 }, function (req, res) {
681662
res.setHeader('Content-Type', 'text/plain')
682663
res.end('hello, world')
@@ -827,7 +808,7 @@ describe('compression()', function () {
827808
.end()
828809
})
829810

830-
brotli('should flush small chunks for brotli', function (done) {
811+
it('should flush small chunks for brotli', function (done) {
831812
var chunks = 0
832813
var next
833814
var server = createServer({ threshold: 0 }, function (req, res) {
@@ -933,7 +914,7 @@ describe('compression()', function () {
933914
.expect(200, 'hello, world', done)
934915
})
935916

936-
brotli('should compress when enforceEncoding is brotli', function (done) {
917+
it('should compress when enforceEncoding is brotli', function (done) {
937918
var server = createServer({ threshold: 0, enforceEncoding: 'br' }, function (req, res) {
938919
res.setHeader('Content-Type', 'text/plain')
939920
res.end('hello, world')

0 commit comments

Comments
 (0)