Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @private
*/

var accepts = require('accepts')
var Negotiator = require('negotiator')
var Buffer = require('safe-buffer').Buffer
var bytes = require('bytes')
var compressible = require('compressible')
Expand Down Expand Up @@ -174,13 +174,8 @@ function compression (options) {
}

// compression method
var accept = accepts(req)
var method = accept.encoding(['gzip', 'deflate', 'identity'])

// we really don't prefer deflate
if (method === 'deflate' && accept.encoding(['gzip'])) {
method = accept.encoding(['gzip', 'identity'])
}
var negotiator = new Negotiator(req)
var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip'])
Copy link
Member Author

@blakeembrey blakeembrey Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you add brotli, just add it here:

Suggested change
var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip'])
var method = negotiator.encoding(hasBrotli ? ['br', 'gzip', 'deflate', 'identity'] : ['gzip', 'deflate', 'identity'], hasBrotli ? ['br', 'gzip'] : ['gzip'])

Edit: Could be better to store in global vars, e.g.

var SUPPORTED_ENCODING = hasBrotli ? ['br', 'gzip', 'deflate', 'identity'] : ['gzip', 'deflate', 'identity']; 
var PREFERRED_ENCODING = hasBrotli ? ['br', 'gzip'] : ['gzip'];


// negotiation failed
if (!method || method === 'identity') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"license": "MIT",
"repository": "expressjs/compression",
"dependencies": {
"accepts": "~1.3.8",
"bytes": "3.1.2",
"compressible": "~2.0.18",
"debug": "2.6.9",
"negotiator": "~0.6.4",
"on-headers": "~1.0.2",
"safe-buffer": "5.2.1",
"vary": "~1.1.2"
Expand Down
Loading