123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- module.exports = preferredEncodings;
- preferredEncodings.preferredEncodings = preferredEncodings;
- function parseAcceptEncoding(accept) {
- var acceptableEncodings;
- if (accept) {
- acceptableEncodings = accept.split(',').map(function(e) {
- return parseEncoding(e.trim());
- });
- } else {
- acceptableEncodings = [];
- }
- if (!acceptableEncodings.some(function(e) {
- return e && specify('identity', e);
- })) {
- /*
- * 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({
- encoding: 'identity',
- q: lowestQ / 2,
- });
- }
- return acceptableEncodings.filter(function(e) {
- return e;
- });
- }
- function parseEncoding(s) {
- var match = s.match(/^\s*(\S+?)\s*(?:;(.*))?$/);
- if (!match) return null;
- var encoding = match[1];
- var q = 1;
- if (match[2]) {
- var params = match[2].split(';');
- for (var i = 0; i < params.length; i ++) {
- var p = params[i].trim().split('=');
- if (p[0] === 'q') {
- q = parseFloat(p[1]);
- break;
- }
- }
- }
- return {
- encoding: encoding,
- q: q
- };
- }
- 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;
- }
- })[0] || {s: 0, q: 0});
- }
- function specify(encoding, spec) {
- var s = 0;
- if(spec.encoding === encoding){
- s |= 1;
- } else if (spec.encoding !== '*' ) {
- return null
- }
- return {
- s: s,
- q: spec.q,
- }
- };
- 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];
- if(pa.q == pb.q) {
- return pa.s < pb.s ? 1 : -1;
- } else {
- return pa.q < pb.q ? 1 : -1;
- }
- }).map(function(pair) {
- return pair[0];
- });
- } else {
- return accept.sort(function (a, b) {
- // revsort
- return a.q < b.q ? 1 : -1;
- }).filter(function(type){
- return type.q > 0;
- }).map(function(type) {
- return type.encoding;
- });
- }
- }
|