Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5bdbb9e
Added support for brotli ('br') content-encoding
danielgindi Jul 10, 2020
6ef8cef
Update README.md
danielgindi Jul 12, 2020
02c06c2
Update README.md
danielgindi Jul 13, 2020
4df713b
Update README.md
danielgindi Jul 13, 2020
87076af
Apply default value also when params is specified
danielgindi Jul 13, 2020
fffe4c7
Increase coverage for specifying params
danielgindi Jul 14, 2020
9145a55
Updated brotli detection method
danielgindi Aug 25, 2020
0bb402e
Prefer br over gzip and deflate
danielgindi Aug 30, 2020
bbcd9c4
feat: use "koa-compress" logic to determine the preferred encoding
nicksrandall Sep 14, 2020
767c62a
test: adding one more test case br/gzip with quality params
nicksrandall Sep 14, 2020
78ad84a
chore: fix linting errors
nicksrandall Sep 14, 2020
4c359b8
fix: hand write encodings lib to be compatible with node 0.8
nicksrandall Sep 14, 2020
8340cde
Fix: fixing lint errors in new lib
nicksrandall Sep 14, 2020
04ab713
Fix: fixing lint errors in new lib
nicksrandall Sep 14, 2020
b024cce
implemented required encoding negotiator without 3rd party dependency
danielgindi Dec 19, 2020
9af45dd
Merge branch 'master' into feature/brotli
bjohansebas Oct 19, 2024
25b68b8
fix
bjohansebas Oct 19, 2024
3d30ab0
use negotiator
bjohansebas Oct 19, 2024
2cabcc3
Merge branch 'master' into feature/brotli
UlisesGascon Oct 20, 2024
662c09d
improve negotiateEnconding
bjohansebas Oct 24, 2024
0ecf49f
Merge branch 'feature/brotli' of github.com:bjohansebas/compression i…
bjohansebas Oct 24, 2024
395ead9
Merge branch 'master' of github.com:expressjs/compression into featur…
bjohansebas Oct 25, 2024
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
Prev Previous commit
Next Next commit
Fix: fixing lint errors in new lib
  • Loading branch information
nicksrandall authored and danielgindi committed Sep 14, 2020
commit 8340cdefbac062ee558bd0b3c7565c9d48eca398
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function compression (options) {
}

// compression method
var encodings = new Encodings();
var encodings = new Encodings()
encodings.parseAcceptEncoding(req.headers['accept-encoding'] || 'identity')
var method = encodings.getPreferredContentEncoding()

Expand Down
89 changes: 42 additions & 47 deletions lib/encodings.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,98 @@
// NOTE: Most of this code was ported from "koa-compress"
// See: https://github.com/koajs/compress

"use strict";
var zlib = require("zlib");
'use strict'
var zlib = require('zlib')

module.exports = Encodings;
module.exports = Encodings

/**
* @const
* whether current node version has brotli support
*/
var hasBrotliSupport = "createBrotliCompress" in zlib;
var hasBrotliSupport = 'createBrotliCompress' in zlib

function Encodings() {
this.encodingWeights = [];
function Encodings () {
this.encodingWeights = []
}

Encodings.supportedEncodings = {
gzip: true,
deflate: true,
identity: true
};
}

Encodings.preferredEncodings = ["br", "gzip", "deflate", "identity"];
Encodings.preferredEncodings = ['br', 'gzip', 'deflate', 'identity']

if (hasBrotliSupport) {
Encodings.supportedEncodings.br = true;
Encodings.supportedEncodings.br = true
}

Encodings.reDirective = /^\s*(gzip|compress|deflate|br|identity|\*)\s*(?:;\s*q\s*=\s*(\d(?:\.\d)?))?\s*$/;
Encodings.reDirective = /^\s*(gzip|compress|deflate|br|identity|\*)\s*(?:;\s*q\s*=\s*(\d(?:\.\d*)?))?\s*$/

Encodings.hasBrotliSupport = hasBrotliSupport;
Encodings.hasBrotliSupport = hasBrotliSupport

Encodings.prototype.parseAcceptEncoding = function (acceptEncoding) {
var acceptEncoding = acceptEncoding || "";
acceptEncoding = acceptEncoding || ''

var encodingWeights = this.encodingWeights,
reDirective = Encodings.reDirective;
acceptEncoding.split(",").forEach(function (directive) {
var match = reDirective.exec(directive);
if (!match) return; // not a supported encoding above
var encodingWeights = this.encodingWeights
var reDirective = Encodings.reDirective
acceptEncoding.split(',').forEach(function (directive) {
var match = reDirective.exec(directive)
if (!match) return // not a supported encoding above

var encoding = match[1];
var encoding = match[1]

// weight must be in [0, 1]
var weight = match[2] && !isNaN(match[2]) ? parseFloat(match[2], 10) : 1;
weight = Math.max(weight, 0);
weight = Math.min(weight, 1);
var weight = match[2] && !isNaN(match[2]) ? parseFloat(match[2], 10) : 1
weight = Math.max(weight, 0)
weight = Math.min(weight, 1)

encodingWeights.push({ encoding: encoding, weight: weight });
});
};
encodingWeights.push({ encoding: encoding, weight: weight })
})
}

Encodings.prototype.getPreferredContentEncoding = function () {
var encodingWeights = this.encodingWeights;
var encodingWeights = this.encodingWeights

var acceptedEncodings = encodingWeights
// sort by weight
.sort(function (a, b) {
return b.weight - a.weight;
return b.weight - a.weight
})
// filter by supported encodings
.filter(function (record) {
return Encodings.supportedEncodings[record.encoding];
});
return Encodings.supportedEncodings[record.encoding]
})

// group them by weights
var weightClasses = {};
var weightList = [];
var weightClasses = {}
var weightList = []
acceptedEncodings.forEach(function (record) {
var weight = record.weight;
var weight = record.weight
if (!weightClasses.hasOwnProperty(weight)) {
weightClasses[weight] = [];
weightList.push(weight);
weightClasses[weight] = []
weightList.push(weight)
}
weightClasses[weight].push(record.encoding);
});
weightClasses[weight].push(record.encoding)
})

// search by weight, descending
var weights = weightList.sort(function (a, b) {
return b - a;
});
return b - a
})

for (var i = 0; i < weights.length; i++) {
// encodings at this weight
var encodings = weightClasses[weights[i]];
var encodings = weightClasses[weights[i]]

// return the first encoding in the preferred list
for (var j = 0; j < Encodings.preferredEncodings.length; j++) {
var preferredEncoding = Encodings.preferredEncodings[j];
if (encodings.indexOf(preferredEncoding) >= 0) return preferredEncoding;
var preferredEncoding = Encodings.preferredEncodings[j]
if (encodings.indexOf(preferredEncoding) >= 0) return preferredEncoding
}
}

// no encoding matches, check to see if the client set identity, q=0
if (encodingWeights["identity"] && encodingWeights["identity"].weight === 0) {
throw new Error("Please accept br, gzip, deflate, or identity.");
}

// by default, return nothing
return "identity";
};
return 'identity'
}
15 changes: 15 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,21 @@ describe('compression()', function () {
})
})

describe('when "Accept-Encoding: gzip;q=0.001"', function () {
var brotlit = hasBrotliSupport ? it : it.skip
brotlit('should respond with gzip', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip;q=0.001')
.expect('Content-Encoding', 'gzip', done)
})
})

describe('when "Accept-Encoding: deflate, br"', function () {
var brotlit = hasBrotliSupport ? it : it.skip
brotlit('should respond with br', function (done) {
Expand Down