negotiator
Advanced tools
Comparing version 0.4.9 to 0.5.0
@@ -5,7 +5,16 @@ module.exports = preferredCharsets; | ||
function parseAcceptCharset(accept) { | ||
return accept.split(',').map(function(e, i) { | ||
return parseCharset(e.trim(), i); | ||
}).filter(function(e) { | ||
return e; | ||
}); | ||
var accepts = accept.split(','); | ||
for (var i = 0, j = 0; i < accepts.length; i++) { | ||
var charset = parseCharset(accepts[i].trim(), i); | ||
if (charset) { | ||
accepts[j++] = charset; | ||
} | ||
} | ||
// trim accepts | ||
accepts.length = j; | ||
return accepts; | ||
} | ||
@@ -38,11 +47,13 @@ | ||
function getCharsetPriority(charset, accepted) { | ||
return (accepted.map(function(a) { | ||
return specify(charset, a); | ||
}).filter(Boolean).sort(function (a, b) { | ||
if(a.s == b.s) { | ||
return a.q > b.q ? -1 : 1; | ||
} else { | ||
return a.s > b.s ? -1 : 1; | ||
var priority = {i: -1, s: 0, q: 0}; | ||
for (var i = 0; i < accepted.length; i++) { | ||
var spec = specify(charset, accepted[i]); | ||
if (spec && (priority.s - spec.s || priority.q - spec.q || priority.i - spec.i) < 0) { | ||
priority = spec; | ||
} | ||
})[0] || {s: 0, q:0}); | ||
} | ||
return priority; | ||
} | ||
@@ -61,2 +72,3 @@ | ||
q: spec.q, | ||
i: spec.i | ||
} | ||
@@ -67,25 +79,27 @@ } | ||
// RFC 2616 sec 14.2: no header = * | ||
accept = parseAcceptCharset(accept === undefined ? '*' : accept || ''); | ||
if (provided) { | ||
return provided.map(function(type) { | ||
return [type, getCharsetPriority(type, accept)]; | ||
}).filter(function(pair) { | ||
return pair[1].q > 0; | ||
}).sort(function(a, b) { | ||
var pa = a[1]; | ||
var pb = b[1]; | ||
return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i); | ||
}).map(function(pair) { | ||
return pair[0]; | ||
var accepts = parseAcceptCharset(accept === undefined ? '*' : accept || ''); | ||
if (!provided) { | ||
// sorted list of all charsets | ||
return accepts.filter(isQuality).sort(compareSpecs).map(function getCharset(spec) { | ||
return spec.charset; | ||
}); | ||
} else { | ||
return accept.sort(function (a, b) { | ||
// revsort | ||
return (b.q - a.q) || (a.i - b.i); | ||
}).filter(function(type) { | ||
return type.q > 0; | ||
}).map(function(type) { | ||
return type.charset; | ||
}); | ||
} | ||
var priorities = provided.map(function getPriority(type) { | ||
return getCharsetPriority(type, accepts); | ||
}); | ||
// sorted list of accepted charsets | ||
return priorities.filter(isQuality).sort(compareSpecs).map(function getCharset(priority) { | ||
return provided[priorities.indexOf(priority)]; | ||
}); | ||
} | ||
function compareSpecs(a, b) { | ||
return (b.q - a.q) || (b.s - a.s) || (a.i - b.i) || 0; | ||
} | ||
function isQuality(spec) { | ||
return spec.q > 0; | ||
} |
@@ -5,37 +5,32 @@ module.exports = preferredEncodings; | ||
function parseAcceptEncoding(accept) { | ||
var acceptableEncodings; | ||
var accepts = accept.split(','); | ||
var hasIdentity = false; | ||
var minQuality = 1; | ||
if (accept) { | ||
acceptableEncodings = accept.split(',').map(function(e, i) { | ||
return parseEncoding(e.trim(), i); | ||
}); | ||
} else { | ||
acceptableEncodings = []; | ||
for (var i = 0, j = 0; i < accepts.length; i++) { | ||
var encoding = parseEncoding(accepts[i].trim(), i); | ||
if (encoding) { | ||
accepts[j++] = encoding; | ||
hasIdentity = hasIdentity || specify('identity', encoding); | ||
minQuality = Math.min(minQuality, encoding.q || 1); | ||
} | ||
} | ||
if (!acceptableEncodings.some(function(e) { | ||
return e && specify('identity', e); | ||
})) { | ||
if (!hasIdentity) { | ||
/* | ||
* If identity doesn't explicitly appear in the accept-encoding header, | ||
* it's added to the list of acceptable encoding with the lowest q | ||
* | ||
*/ | ||
var lowestQ = 1; | ||
for(var i = 0; i < acceptableEncodings.length; i++){ | ||
var e = acceptableEncodings[i]; | ||
if(e && e.q < lowestQ){ | ||
lowestQ = e.q; | ||
} | ||
} | ||
acceptableEncodings.push({ | ||
accepts[j++] = { | ||
encoding: 'identity', | ||
q: lowestQ / 2, | ||
}); | ||
q: minQuality, | ||
i: i | ||
}; | ||
} | ||
return acceptableEncodings.filter(function(e) { | ||
return e; | ||
}); | ||
// trim accepts | ||
accepts.length = j; | ||
return accepts; | ||
} | ||
@@ -69,11 +64,13 @@ | ||
function getEncodingPriority(encoding, accepted) { | ||
return (accepted.map(function(a) { | ||
return specify(encoding, a); | ||
}).filter(Boolean).sort(function (a, b) { | ||
if(a.s == b.s) { | ||
return a.q > b.q ? -1 : 1; | ||
} else { | ||
return a.s > b.s ? -1 : 1; | ||
var priority = {i: -1, s: 0, q: 0}; | ||
for (var i = 0; i < accepted.length; i++) { | ||
var spec = specify(encoding, accepted[i]); | ||
if (spec && (priority.s - spec.s || priority.q - spec.q || priority.i - spec.i) < 0) { | ||
priority = spec; | ||
} | ||
})[0] || {s: 0, q: 0}); | ||
} | ||
return priority; | ||
} | ||
@@ -92,2 +89,3 @@ | ||
q: spec.q, | ||
i: spec.i | ||
} | ||
@@ -97,25 +95,27 @@ }; | ||
function preferredEncodings(accept, provided) { | ||
accept = parseAcceptEncoding(accept || ''); | ||
if (provided) { | ||
return provided.map(function(type) { | ||
return [type, getEncodingPriority(type, accept)]; | ||
}).filter(function(pair) { | ||
return pair[1].q > 0; | ||
}).sort(function(a, b) { | ||
var pa = a[1]; | ||
var pb = b[1]; | ||
return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i); | ||
}).map(function(pair) { | ||
return pair[0]; | ||
var accepts = parseAcceptEncoding(accept || ''); | ||
if (!provided) { | ||
// sorted list of all encodings | ||
return accepts.filter(isQuality).sort(compareSpecs).map(function getEncoding(spec) { | ||
return spec.encoding; | ||
}); | ||
} else { | ||
return accept.sort(function (a, b) { | ||
// revsort | ||
return (b.q - a.q) || (a.i - b.i); | ||
}).filter(function(type){ | ||
return type.q > 0; | ||
}).map(function(type) { | ||
return type.encoding; | ||
}); | ||
} | ||
var priorities = provided.map(function getPriority(type) { | ||
return getEncodingPriority(type, accepts); | ||
}); | ||
// sorted list of accepted encodings | ||
return priorities.filter(isQuality).sort(compareSpecs).map(function getEncoding(priority) { | ||
return provided[priorities.indexOf(priority)]; | ||
}); | ||
} | ||
function compareSpecs(a, b) { | ||
return (b.q - a.q) || (b.s - a.s) || (a.i - b.i) || 0; | ||
} | ||
function isQuality(spec) { | ||
return spec.q > 0; | ||
} |
@@ -5,7 +5,16 @@ module.exports = preferredLanguages; | ||
function parseAcceptLanguage(accept) { | ||
return accept.split(',').map(function(e, i) { | ||
return parseLanguage(e.trim(), i); | ||
}).filter(function(e) { | ||
return e; | ||
}); | ||
var accepts = accept.split(','); | ||
for (var i = 0, j = 0; i < accepts.length; i++) { | ||
var langauge = parseLanguage(accepts[i].trim(), i); | ||
if (langauge) { | ||
accepts[j++] = langauge; | ||
} | ||
} | ||
// trim accepts | ||
accepts.length = j; | ||
return accepts; | ||
} | ||
@@ -42,11 +51,13 @@ | ||
function getLanguagePriority(language, accepted) { | ||
return (accepted.map(function(a){ | ||
return specify(language, a); | ||
}).filter(Boolean).sort(function (a, b) { | ||
if(a.s == b.s) { | ||
return a.q > b.q ? -1 : 1; | ||
} else { | ||
return a.s > b.s ? -1 : 1; | ||
var priority = {i: -1, s: 0, q: 0}; | ||
for (var i = 0; i < accepted.length; i++) { | ||
var spec = specify(language, accepted[i]); | ||
if (spec && (priority.s - spec.s || priority.q - spec.q || priority.i - spec.i) < 0) { | ||
priority = spec; | ||
} | ||
})[0] || {s: 0, q: 0}); | ||
} | ||
return priority; | ||
} | ||
@@ -71,2 +82,3 @@ | ||
q: spec.q, | ||
i: spec.i | ||
} | ||
@@ -77,28 +89,27 @@ }; | ||
// RFC 2616 sec 14.4: no header = * | ||
accept = parseAcceptLanguage(accept === undefined ? '*' : accept || ''); | ||
if (provided) { | ||
var accepts = parseAcceptLanguage(accept === undefined ? '*' : accept || ''); | ||
var ret = provided.map(function(type) { | ||
return [type, getLanguagePriority(type, accept)]; | ||
}).filter(function(pair) { | ||
return pair[1].q > 0; | ||
}).sort(function(a, b) { | ||
var pa = a[1]; | ||
var pb = b[1]; | ||
return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i); | ||
}).map(function(pair) { | ||
return pair[0]; | ||
if (!provided) { | ||
// sorted list of all languages | ||
return accepts.filter(isQuality).sort(compareSpecs).map(function getLanguage(spec) { | ||
return spec.full; | ||
}); | ||
return ret; | ||
} | ||
} else { | ||
return accept.sort(function (a, b) { | ||
// revsort | ||
return (b.q - a.q) || (a.i - b.i); | ||
}).filter(function(type) { | ||
return type.q > 0; | ||
}).map(function(type) { | ||
return type.full; | ||
}); | ||
} | ||
var priorities = provided.map(function getPriority(type) { | ||
return getLanguagePriority(type, accepts); | ||
}); | ||
// sorted list of accepted languages | ||
return priorities.filter(isQuality).sort(compareSpecs).map(function getLanguage(priority) { | ||
return provided[priorities.indexOf(priority)]; | ||
}); | ||
} | ||
function compareSpecs(a, b) { | ||
return (b.q - a.q) || (b.s - a.s) || (a.i - b.i) || 0; | ||
} | ||
function isQuality(spec) { | ||
return spec.q > 0; | ||
} |
@@ -5,7 +5,16 @@ module.exports = preferredMediaTypes; | ||
function parseAccept(accept) { | ||
return accept.split(',').map(function(e, i) { | ||
return parseMediaType(e.trim(), i); | ||
}).filter(function(e) { | ||
return e; | ||
}); | ||
var accepts = accept.split(','); | ||
for (var i = 0, j = 0; i < accepts.length; i++) { | ||
var mediaType = parseMediaType(accepts[i].trim(), i); | ||
if (mediaType) { | ||
accepts[j++] = mediaType; | ||
} | ||
} | ||
// trim accepts | ||
accepts.length = j; | ||
return accepts; | ||
}; | ||
@@ -48,11 +57,13 @@ | ||
function getMediaTypePriority(type, accepted) { | ||
return (accepted.map(function(a) { | ||
return specify(type, a); | ||
}).filter(Boolean).sort(function (a, b) { | ||
if(a.s == b.s) { | ||
return a.q > b.q ? -1 : 1; | ||
} else { | ||
return a.s > b.s ? -1 : 1; | ||
var priority = {i: -1, s: 0, q: 0}; | ||
for (var i = 0; i < accepted.length; i++) { | ||
var spec = specify(type, accepted[i]); | ||
if (spec && (priority.s - spec.s || priority.q - spec.q || priority.i - spec.i) < 0) { | ||
priority = spec; | ||
} | ||
})[0] || {s: 0, q: 0}); | ||
} | ||
return priority; | ||
} | ||
@@ -93,2 +104,3 @@ | ||
q: spec.q, | ||
i: spec.i, | ||
s: s, | ||
@@ -101,26 +113,27 @@ } | ||
// RFC 2616 sec 14.2: no header = */* | ||
accept = parseAccept(accept === undefined ? '*/*' : accept || ''); | ||
if (provided) { | ||
return provided.map(function(type) { | ||
return [type, getMediaTypePriority(type, accept)]; | ||
}).filter(function(pair) { | ||
return pair[1].q > 0; | ||
}).sort(function(a, b) { | ||
var pa = a[1]; | ||
var pb = b[1]; | ||
return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i); | ||
}).map(function(pair) { | ||
return pair[0]; | ||
}); | ||
var accepts = parseAccept(accept === undefined ? '*/*' : accept || ''); | ||
} else { | ||
return accept.sort(function (a, b) { | ||
// revsort | ||
return (b.q - a.q) || (a.i - b.i); | ||
}).filter(function(type) { | ||
return type.q > 0; | ||
}).map(function(type) { | ||
return type.full; | ||
if (!provided) { | ||
// sorted list of all types | ||
return accepts.filter(isQuality).sort(compareSpecs).map(function getType(spec) { | ||
return spec.full; | ||
}); | ||
} | ||
var priorities = provided.map(function getPriority(type) { | ||
return getMediaTypePriority(type, accepts); | ||
}); | ||
// sorted list of accepted types | ||
return priorities.filter(isQuality).sort(compareSpecs).map(function getType(priority) { | ||
return provided[priorities.indexOf(priority)]; | ||
}); | ||
} | ||
function compareSpecs(a, b) { | ||
return (b.q - a.q) || (b.s - a.s) || (a.i - b.i) || 0; | ||
} | ||
function isQuality(spec) { | ||
return spec.q > 0; | ||
} |
{ | ||
"name": "negotiator", | ||
"description": "HTTP content negotiation", | ||
"version": "0.4.9", | ||
"author": "Federico Romero <federico.romero@outboxlabs.com>", | ||
"contributors": ["Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)"], | ||
"repository": "jshttp/negotiator", | ||
"version": "0.5.0", | ||
"contributors": [ | ||
"Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"Federico Romero <federico.romero@outboxlabs.com>", | ||
"Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)" | ||
], | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -16,19 +19,21 @@ "http", | ||
], | ||
"license": "MIT", | ||
"repository": "jshttp/negotiator", | ||
"devDependencies": { | ||
"istanbul": "~0.3.2", | ||
"nodeunit": "0.8.x" | ||
"istanbul": "0.3.5", | ||
"nodeunit": "0.9.0" | ||
}, | ||
"files": [ | ||
"lib/", | ||
"HISTORY.md", | ||
"LICENSE", | ||
"index.js", | ||
"README.md" | ||
], | ||
"engines": { | ||
"node": ">= 0.6" | ||
}, | ||
"scripts": { | ||
"test": "nodeunit test", | ||
"test-cov": "istanbul cover ./node_modules/nodeunit/bin/nodeunit test" | ||
}, | ||
"engines": { | ||
"node": ">= 0.6" | ||
}, | ||
"main": "lib/negotiator.js", | ||
"files": [ | ||
"lib", | ||
"LICENSE" | ||
] | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
19444
9
422
0
1