encoding.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. module.exports = preferredEncodings;
  2. preferredEncodings.preferredEncodings = preferredEncodings;
  3. function parseAcceptEncoding(accept) {
  4. var accepts = accept.split(',');
  5. var hasIdentity = false;
  6. var minQuality = 1;
  7. for (var i = 0, j = 0; i < accepts.length; i++) {
  8. var encoding = parseEncoding(accepts[i].trim(), i);
  9. if (encoding) {
  10. accepts[j++] = encoding;
  11. hasIdentity = hasIdentity || specify('identity', encoding);
  12. minQuality = Math.min(minQuality, encoding.q || 1);
  13. }
  14. }
  15. if (!hasIdentity) {
  16. /*
  17. * If identity doesn't explicitly appear in the accept-encoding header,
  18. * it's added to the list of acceptable encoding with the lowest q
  19. */
  20. accepts[j++] = {
  21. encoding: 'identity',
  22. q: minQuality,
  23. i: i
  24. };
  25. }
  26. // trim accepts
  27. accepts.length = j;
  28. return accepts;
  29. }
  30. function parseEncoding(s, i) {
  31. var match = s.match(/^\s*(\S+?)\s*(?:;(.*))?$/);
  32. if (!match) return null;
  33. var encoding = match[1];
  34. var q = 1;
  35. if (match[2]) {
  36. var params = match[2].split(';');
  37. for (var i = 0; i < params.length; i ++) {
  38. var p = params[i].trim().split('=');
  39. if (p[0] === 'q') {
  40. q = parseFloat(p[1]);
  41. break;
  42. }
  43. }
  44. }
  45. return {
  46. encoding: encoding,
  47. q: q,
  48. i: i
  49. };
  50. }
  51. function getEncodingPriority(encoding, accepted, index) {
  52. var priority = {o: -1, q: 0, s: 0};
  53. for (var i = 0; i < accepted.length; i++) {
  54. var spec = specify(encoding, accepted[i], index);
  55. if (spec && (priority.s - spec.s || priority.q - spec.q || priority.o - spec.o) < 0) {
  56. priority = spec;
  57. }
  58. }
  59. return priority;
  60. }
  61. function specify(encoding, spec, index) {
  62. var s = 0;
  63. if(spec.encoding.toLowerCase() === encoding.toLowerCase()){
  64. s |= 1;
  65. } else if (spec.encoding !== '*' ) {
  66. return null
  67. }
  68. return {
  69. i: index,
  70. o: spec.i,
  71. q: spec.q,
  72. s: s
  73. }
  74. };
  75. function preferredEncodings(accept, provided) {
  76. var accepts = parseAcceptEncoding(accept || '');
  77. if (!provided) {
  78. // sorted list of all encodings
  79. return accepts.filter(isQuality).sort(compareSpecs).map(function getEncoding(spec) {
  80. return spec.encoding;
  81. });
  82. }
  83. var priorities = provided.map(function getPriority(type, index) {
  84. return getEncodingPriority(type, accepts, index);
  85. });
  86. // sorted list of accepted encodings
  87. return priorities.filter(isQuality).sort(compareSpecs).map(function getEncoding(priority) {
  88. return provided[priorities.indexOf(priority)];
  89. });
  90. }
  91. function compareSpecs(a, b) {
  92. return (b.q - a.q) || (b.s - a.s) || (a.o - b.o) || (a.i - b.i) || 0;
  93. }
  94. function isQuality(spec) {
  95. return spec.q > 0;
  96. }