+42
-16
lines changedFilter options
+42
-16
lines changed Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1
+
unreleased
2
+
==================
3
+
4
+
* Added an option preferred encodings array #59
5
+
1
6
0.6.3 / 2022-01-22
2
7
==================
3
8
Original file line number Diff line number Diff line change
@@ -44,13 +44,13 @@ Negotiator.prototype.charsets = function charsets(available) {
44
44
return preferredCharsets(this.request.headers['accept-charset'], available);
45
45
};
46
46
47
-
Negotiator.prototype.encoding = function encoding(available) {
48
-
var set = this.encodings(available);
47
+
Negotiator.prototype.encoding = function encoding(available, preferred) {
48
+
var set = this.encodings(available, preferred);
49
49
return set && set[0];
50
50
};
51
51
52
-
Negotiator.prototype.encodings = function encodings(available) {
53
-
return preferredEncodings(this.request.headers['accept-encoding'], available);
52
+
Negotiator.prototype.encodings = function encodings(available, preferred) {
53
+
return preferredEncodings(this.request.headers['accept-encoding'], available, preferred);
54
54
};
55
55
56
56
Negotiator.prototype.language = function language(available) {
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ function parseEncoding(str, i) {
96
96
*/
97
97
98
98
function getEncodingPriority(encoding, accepted, index) {
99
-
var priority = {o: -1, q: 0, s: 0};
99
+
var priority = {encoding: encoding, o: -1, q: 0, s: 0};
100
100
101
101
for (var i = 0; i < accepted.length; i++) {
102
102
var spec = specify(encoding, accepted[i], index);
@@ -123,6 +123,7 @@ function specify(encoding, spec, index) {
123
123
}
124
124
125
125
return {
126
+
encoding: encoding,
126
127
i: index,
127
128
o: spec.i,
128
129
q: spec.q,
@@ -135,14 +136,34 @@ function specify(encoding, spec, index) {
135
136
* @public
136
137
*/
137
138
138
-
function preferredEncodings(accept, provided) {
139
+
function preferredEncodings(accept, provided, preferred) {
139
140
var accepts = parseAcceptEncoding(accept || '');
140
141
142
+
var comparator = preferred ? function comparator (a, b) {
143
+
if (a.q !== b.q) {
144
+
return b.q - a.q // higher quality first
145
+
}
146
+
147
+
var aPreferred = preferred.indexOf(a.encoding)
148
+
var bPreferred = preferred.indexOf(b.encoding)
149
+
150
+
if (aPreferred === -1 && bPreferred === -1) {
151
+
// consider the original specifity/order
152
+
return (b.s - a.s) || (a.o - b.o) || (a.i - b.i)
153
+
}
154
+
155
+
if (aPreferred !== -1 && bPreferred !== -1) {
156
+
return aPreferred - bPreferred // consider the preferred order
157
+
}
158
+
159
+
return aPreferred === -1 ? 1 : -1 // preferred first
160
+
} : compareSpecs;
161
+
141
162
if (!provided) {
142
163
// sorted list of all encodings
143
164
return accepts
144
165
.filter(isQuality)
145
-
.sort(compareSpecs)
166
+
.sort(comparator)
146
167
.map(getFullEncoding);
147
168
}
148
169
@@ -151,7 +172,7 @@ function preferredEncodings(accept, provided) {
151
172
});
152
173
153
174
// sorted list of accepted encodings
154
-
return priorities.filter(isQuality).sort(compareSpecs).map(function getEncoding(priority) {
175
+
return priorities.filter(isQuality).sort(comparator).map(function getEncoding(priority) {
155
176
return provided[priorities.indexOf(priority)];
156
177
});
157
178
}
@@ -162,7 +183,7 @@ function preferredEncodings(accept, provided) {
162
183
*/
163
184
164
185
function compareSpecs(a, b) {
165
-
return (b.q - a.q) || (b.s - a.s) || (a.o - b.o) || (a.i - b.i) || 0;
186
+
return (b.q - a.q) || (b.s - a.s) || (a.o - b.o) || (a.i - b.i);
166
187
}
167
188
168
189
/**
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ function parseMediaType(str, i) {
69
69
70
70
// get the value, unwrapping quotes
71
71
var value = val && val[0] === '"' && val[val.length - 1] === '"'
72
-
? val.substr(1, val.length - 2)
72
+
? val.slice(1, -1)
73
73
: val;
74
74
75
75
if (key === 'q') {
@@ -238,8 +238,8 @@ function splitKeyValuePair(str) {
238
238
if (index === -1) {
239
239
key = str;
240
240
} else {
241
-
key = str.substr(0, index);
242
-
val = str.substr(index + 1);
241
+
key = str.slice(0, index);
242
+
val = str.slice(index + 1);
243
243
}
244
244
245
245
return [key, val];
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
1
1
{
2
2
"name": "negotiator",
3
3
"description": "HTTP content negotiation",
4
-
"version": "0.6.3",
4
+
"version": "0.6.4",
5
5
"contributors": [
6
6
"Douglas Christopher Wilson <doug@somethingdoug.com>",
7
7
"Federico Romero <federico.romero@outboxlabs.com>",
Original file line number Diff line number Diff line change
@@ -11787,9 +11787,9 @@
11787
11787
}
11788
11788
},
11789
11789
"node_modules/negotiator": {
11790
-
"version": "0.6.3",
11791
-
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
11792
-
"integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==",
11790
+
"version": "0.6.4",
11791
+
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.4.tgz",
11792
+
"integrity": "sha512-myRT3DiWPHqho5PrJaIRyaMv2kgYf0mUVgBNOYMuCH5Ki1yEiQaf/ZJuQ62nvpc44wL5WDbTX7yGJi1Neevw8w==",
11793
11793
"inBundle": true,
11794
11794
"license": "MIT",
11795
11795
"engines": {
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4