encoding-negotiator
Advanced tools
Comparing version
59
index.js
@@ -1,46 +0,47 @@ | ||
'use strict'; | ||
'use strict' | ||
const IDENTITY = 'identity'; | ||
function negotiate(header, supported) { | ||
const supportedEncodings = createMap(supported); | ||
function negotiate (header, supported) { | ||
if (!header) { | ||
return undefined | ||
} | ||
const supportedEncodings = createMap(supported) | ||
const acceptedEncodings = parse(header || '') | ||
.sort(comparator) | ||
.filter(isNonZeroQuality); | ||
.filter(isNonZeroQuality) | ||
return determinePreffered(acceptedEncodings, supportedEncodings) | ||
} | ||
function determinePreffered(acceptedEncodings, supportedEncodings) { | ||
function determinePreffered (acceptedEncodings, supportedEncodings) { | ||
for (const encoding of acceptedEncodings) { | ||
const selected = supportedEncodings[encoding.name]; | ||
const selected = supportedEncodings[encoding.name] | ||
if (selected) { | ||
return selected; | ||
return selected | ||
} | ||
} | ||
return IDENTITY; | ||
return null | ||
} | ||
function createMap(supported) { | ||
const supportedEncodings = { | ||
identity: IDENTITY, | ||
'*': supported[0] | ||
function createMap (supported) { | ||
const supportedEncodings = {} | ||
if (supported.length > 0) { | ||
supportedEncodings['*'] = supported[0] | ||
} | ||
for (const encoding of supported) { | ||
supportedEncodings[encoding] = encoding; | ||
supportedEncodings[encoding] = encoding | ||
} | ||
return supportedEncodings; | ||
return supportedEncodings | ||
} | ||
function parse(header) { | ||
const split = header.split(','); | ||
function parse (header) { | ||
const split = header.split(',') | ||
return split.map(parseEncoding) | ||
} | ||
function isNonZeroQuality(encoding) { | ||
return encoding.quality !== 0; | ||
function isNonZeroQuality (encoding) { | ||
return encoding.quality !== 0 | ||
} | ||
function parseEncoding(encoding) { | ||
const [ name, second ] = encoding.trim().split(';'); | ||
const quality = getQuality(second); | ||
function parseEncoding (encoding) { | ||
const [name, second] = encoding.trim().split(';') | ||
const quality = getQuality(second) | ||
return { | ||
@@ -52,12 +53,12 @@ name, | ||
function getQuality(second) { | ||
function getQuality (second) { | ||
if (!second) { | ||
return 1; | ||
return 1 | ||
} | ||
const [ , quality ] = second.trim().split('='); | ||
return parseFloat(quality); | ||
const [, quality] = second.trim().split('=') | ||
return parseFloat(quality) | ||
} | ||
function comparator(a, b) { | ||
return b.quality - a.quality; | ||
function comparator (a, b) { | ||
return b.quality - a.quality | ||
} | ||
@@ -64,0 +65,0 @@ |
{ | ||
"name": "encoding-negotiator", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "a negotiator for the accept-encoding header", | ||
"main": "index.js", | ||
"scripts": { | ||
"unit": "tap test/*.js", | ||
"unit": "ava test/*.js", | ||
"test": "npm run lint && npm run unit", | ||
"lint": "./node_modules/eslint/bin/eslint.js index.js test/*", | ||
"coveralls": "npm run unit -- --cov", | ||
"coverage-report": "npm run coveralls && tap --coverage-report=lcov" | ||
"lint": "standard index.js test/*", | ||
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls", | ||
"coverage-report": "nyc npm test && nyc report --reporter=html" | ||
}, | ||
@@ -24,5 +24,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "^2.0.0", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.13.0", | ||
"tap": "^11.0.0" | ||
"nyc": "^14.1.1", | ||
"standard": "^13.0.1" | ||
}, | ||
@@ -35,3 +36,4 @@ "engines": { | ||
"url": "git+https://github.com/SerayaEryn/encoding-negotiator.git" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
@@ -8,2 +8,4 @@ # encoding-negotiator | ||
[](https://www.npmjs.com/package/encoding-negotiator) | ||
[](https://standardjs.com) | ||
``` | ||
@@ -16,3 +18,3 @@ npm install encoding-negotiator | ||
encodingNegotiator.negotiate('compress;q=0.5, gzip;q=1.0', ['gzip', 'deflate']); //returns gzip | ||
encodingNegotiator.negotiate('compress;q=0.5, gzip;q=1.0', ['gzip', 'deflate', 'identity']); //returns gzip | ||
``` | ||
@@ -19,0 +21,0 @@ ## API |
116
test/test.js
@@ -1,94 +0,102 @@ | ||
'use strict'; | ||
'use strict' | ||
const t = require('tap'); | ||
const test = t.test; | ||
const enodingNegotiator = require('..'); | ||
const test = require('ava') | ||
const enodingNegotiator = require('..') | ||
test('should return identity', (t) => { | ||
t.plan(1); | ||
const header = 'identity;q=1'; | ||
const supported = ['gzip']; | ||
const header = 'identity;q=1' | ||
const supported = ['gzip', 'identity'] | ||
const result = enodingNegotiator.negotiate(header, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'identity'); | ||
t.is(result, 'identity') | ||
}) | ||
test('should return identity', (t) => { | ||
t.plan(1); | ||
const supported = ['gzip']; | ||
test('should return gzip', (t) => { | ||
const header = 'gzip;q=1, identity;q=0.5' | ||
const supported = ['gzip', 'deflate'] | ||
const result = enodingNegotiator.negotiate(undefined, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'identity'); | ||
t.is(result, 'gzip') | ||
}) | ||
test('should return gzip', (t) => { | ||
t.plan(1); | ||
const header = 'gzip;q=1, identity;q=0.5'; | ||
const supported = ['gzip', 'deflate']; | ||
test('should return deflate', (t) => { | ||
const header = 'deflate;q=0.5,identity; q=0.5' | ||
const supported = ['gzip', 'deflate'] | ||
const result = enodingNegotiator.negotiate(header, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'gzip'); | ||
t.is(result, 'deflate') | ||
}) | ||
test('should return gzip', (t) => { | ||
t.plan(1); | ||
const header = 'deflate;q=0.5,identity; q=0.5'; | ||
const supported = ['gzip', 'deflate']; | ||
test('"*" and ["gzip", "deflate"]', (t) => { | ||
const header = '*' | ||
const supported = ['gzip', 'deflate'] | ||
const result = enodingNegotiator.negotiate(header, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'deflate'); | ||
t.is(result, 'gzip') | ||
}) | ||
test('"*" and ["gzip", "deflate"]', (t) => { | ||
t.plan(1); | ||
const header = '*'; | ||
const supported = ['gzip', 'deflate']; | ||
test('"deflate;q=1.0, *" and ["gzip"]', (t) => { | ||
const header = 'deflate;q=1.0, *' | ||
const supported = ['gzip'] | ||
const result = enodingNegotiator.negotiate(header, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'gzip'); | ||
t.is(result, 'gzip') | ||
}) | ||
test('"deflate;q=1.0, *" and ["gzip"]', (t) => { | ||
t.plan(1); | ||
const header = 'deflate;q=1.0, *'; | ||
const supported = ['gzip']; | ||
test('should ignore invalid encoding if another valid encoding', (t) => { | ||
const header = 'test,br' | ||
const supported = ['br'] | ||
const result = enodingNegotiator.negotiate(header, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'gzip'); | ||
t.is(result, 'br') | ||
}) | ||
test('"gzip;q=0" and ["gzip"]', (t) => { | ||
t.plan(1); | ||
const header = 'gzip;q=0'; | ||
const supported = ['gzip']; | ||
const header = 'gzip;q=0' | ||
const supported = ['gzip', 'identity'] | ||
const result = enodingNegotiator.negotiate(header, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'identity'); | ||
t.is(result, null) | ||
}) | ||
test('unknown encoding', (t) => { | ||
const header = 'white rabbit' | ||
const supported = ['gzip', 'identity'] | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.is(result, null) | ||
}) | ||
test('return undefined if no header', (t) => { | ||
const supported = ['gzip', 'identity'] | ||
const result = enodingNegotiator.negotiate(undefined, supported) | ||
t.is(result, undefined) | ||
}) | ||
test('compress;q=0.5, gzip;q=1.0 and ["gzip", compress"]', (t) => { | ||
t.plan(1); | ||
const header = 'compress;q=0.5, gzip;q=1.0'; | ||
const supported = ['gzip', 'compress']; | ||
const header = 'compress;q=0.5, gzip;q=1.0' | ||
const supported = ['gzip', 'compress'] | ||
const result = enodingNegotiator.negotiate(header, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'gzip'); | ||
t.is(result, 'gzip') | ||
}) | ||
test('compress;q=0.5, gzip;q=1.0 and ["compress"]', (t) => { | ||
t.plan(1); | ||
const header = 'compress;q=0.5, gzip;q=1.0'; | ||
const supported = ['compress']; | ||
const header = 'compress;q=0.5, gzip;q=1.0' | ||
const supported = ['compress'] | ||
const result = enodingNegotiator.negotiate(header, supported); | ||
const result = enodingNegotiator.negotiate(header, supported) | ||
t.strictEquals(result, 'compress'); | ||
}) | ||
t.is(result, 'compress') | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
126
4.13%28
7.69%7200
-7.48%4
33.33%6
-14.29%