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