Skip to content

Commit

Permalink
Merge pull request #37 from lolgesten/fix-ident
Browse files Browse the repository at this point in the history
Handle identity and q=0
  • Loading branch information
tkoenig89 authored Jun 24, 2020
2 parents 7db7b0b + 9f75ea9 commit 352d31a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions test/encoding-selection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,14 @@ describe('encoding-selection', function () {
const result = findEncoding('gzip; q=0.6, deflate; q=1, br;q=0.5', [GZIP, BROTLI, DEFLATE], ['br']);
expect(result).to.be.deep.equal(BROTLI);
});

it('should treat identity as null', function () {
const result = findEncoding('identity;q=1, gzip;q=0.5', [GZIP]);
expect(result).to.be.null;
});

it('should not use encodings with q=0', function () {
const result = findEncoding('br;q=1, *;q=0', [GZIP]);
expect(result).to.be.null;
});
});
9 changes: 8 additions & 1 deletion util/encoding-selection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

// Indicates the identity function (i.e. no compression, nor modification)
const IDENTITY = 'identity';

/**
*
* @param {string} acceptEncoding Content of the accept-encoding header
Expand All @@ -18,6 +21,9 @@ function findEncoding(acceptEncoding, availableCompressions, preference) {

function findFirstMatchingCompression(sortedEncodingList, availableCompressions) {
for (const encoding of sortedEncodingList) {
if (encoding === IDENTITY) {
return null;
}
for (let i = 0; i < availableCompressions.length; i++) {
if (encoding === '*' || encoding === availableCompressions[i].encodingName) {
return availableCompressions[i];
Expand Down Expand Up @@ -58,6 +64,7 @@ function parseEncoding(acceptedEncoding) {
return acceptedEncoding.split(',')
.map(encoding => parseQuality(encoding))
.sort((encodingA, encodingB) => encodingB.q - encodingA.q)
.filter(encoding => encoding.q > 0)
.map(encoding => encoding.name);
}

Expand Down Expand Up @@ -85,4 +92,4 @@ function parseQuality(encoding) {

module.exports = {
findEncoding: findEncoding
};
};

0 comments on commit 352d31a

Please sign in to comment.