arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/server/node_modules/negotiator/lib/encoding.js
server/node_modules/negotiator/lib/encoding.jsRaw
  1module.exports = preferredEncodings;
  2preferredEncodings.preferredEncodings = preferredEncodings;
  3
  4function parseAcceptEncoding(accept) {
  5  var acceptableEncodings;
  6
  7  if (accept) {
  8    acceptableEncodings = accept.split(',').map(function(e, i) {
  9      return parseEncoding(e.trim(), i);
 10    });
 11  } else {
 12    acceptableEncodings = [];
 13  }
 14
 15  if (!acceptableEncodings.some(function(e) {
 16    return e && specify('identity', e);
 17  })) {
 18    /*
 19     * If identity doesn't explicitly appear in the accept-encoding header,
 20     * it's added to the list of acceptable encoding with the lowest q
 21     *
 22     */
 23    var lowestQ = 1;
 24
 25    for(var i = 0; i < acceptableEncodings.length; i++){
 26      var e = acceptableEncodings[i];
 27      if(e && e.q < lowestQ){
 28        lowestQ = e.q;
 29      }
 30    }
 31    acceptableEncodings.push({
 32      encoding: 'identity',
 33      q: lowestQ / 2,
 34    });
 35  }
 36
 37  return acceptableEncodings.filter(function(e) {
 38    return e;
 39  });
 40}
 41
 42function parseEncoding(s, i) {
 43  var match = s.match(/^\s*(\S+?)\s*(?:;(.*))?$/);
 44
 45  if (!match) return null;
 46
 47  var encoding = match[1];
 48  var q = 1;
 49  if (match[2]) {
 50    var params = match[2].split(';');
 51    for (var i = 0; i < params.length; i ++) {
 52      var p = params[i].trim().split('=');
 53      if (p[0] === 'q') {
 54        q = parseFloat(p[1]);
 55        break;
 56      }
 57    }
 58  }
 59
 60  return {
 61    encoding: encoding,
 62    q: q,
 63    i: i
 64  };
 65}
 66
 67function getEncodingPriority(encoding, accepted) {
 68  return (accepted.map(function(a) {
 69    return specify(encoding, a);
 70  }).filter(Boolean).sort(function (a, b) {
 71    if(a.s == b.s) {
 72      return a.q > b.q ? -1 : 1;
 73    } else {
 74      return a.s > b.s ? -1 : 1;
 75    }
 76  })[0] || {s: 0, q: 0});
 77}
 78
 79function specify(encoding, spec) {
 80  var s = 0;
 81  if(spec.encoding.toLowerCase() === encoding.toLowerCase()){
 82    s |= 1;
 83  } else if (spec.encoding !== '*' ) {
 84    return null
 85  }
 86
 87  return {
 88    s: s,
 89    q: spec.q,
 90  }
 91};
 92
 93function preferredEncodings(accept, provided) {
 94  accept = parseAcceptEncoding(accept || '');
 95  if (provided) {
 96    return provided.map(function(type) {
 97      return [type, getEncodingPriority(type, accept)];
 98    }).filter(function(pair) {
 99      return pair[1].q > 0;
100    }).sort(function(a, b) {
101      var pa = a[1];
102      var pb = b[1];
103      return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i);
104    }).map(function(pair) {
105      return pair[0];
106    });
107  } else {
108    return accept.sort(function (a, b) {
109      // revsort
110      return (b.q - a.q) || (a.i - b.i);
111    }).filter(function(type){
112      return type.q > 0;
113    }).map(function(type) {
114      return type.encoding;
115    });
116  }
117}