Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a494d44a2 |
No files matched your search
+3
-10
@@ -134,21 +134,14 @@ export function resolveCompression(
|
|||||||
return { threshold, encodings: [...new Set(encodings)], encode: encode() };
|
return { threshold, encodings: [...new Set(encodings)], encode: encode() };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** `q` of one `Accept-Encoding` member, or 1 when absent. An unparseable `q` excludes it. */
|
||||||
* The qvalue grammar of RFC 9110 §12.4.2: `0` with up to three decimals, or `1` with up to three
|
|
||||||
* zeros. `Number()` alone would admit `2`, `1e0` or `-1`, and a `q=2` would outrank the server's
|
|
||||||
* preference.
|
|
||||||
*/
|
|
||||||
const QVALUE = /^(0(\.\d{0,3})?|1(\.0{0,3})?)$/;
|
|
||||||
|
|
||||||
/** `q` of one `Accept-Encoding` member, or 1 when absent. A malformed or out-of-range `q` excludes it. */
|
|
||||||
function weightOf(params: string[]): number {
|
function weightOf(params: string[]): number {
|
||||||
for (const param of params) {
|
for (const param of params) {
|
||||||
const eq = param.indexOf('=');
|
const eq = param.indexOf('=');
|
||||||
|
|
||||||
if (eq !== -1 && param.slice(0, eq).trim().toLowerCase() === 'q') {
|
if (eq !== -1 && param.slice(0, eq).trim().toLowerCase() === 'q') {
|
||||||
const raw = param.slice(eq + 1).trim();
|
const weight = Number(param.slice(eq + 1).trim());
|
||||||
return QVALUE.test(raw) ? Number(raw) : 0;
|
return Number.isFinite(weight) ? weight : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,13 +48,6 @@ describe('negotiate() (RFC 9110 §12.5.3)', () => {
|
|||||||
['gzip;q=1, br;q=0.5', 'gzip'],
|
['gzip;q=1, br;q=0.5', 'gzip'],
|
||||||
['gzip;q=0.5, *', 'br'],
|
['gzip;q=0.5, *', 'br'],
|
||||||
['br;q=bogus, gzip', 'gzip'],
|
['br;q=bogus, gzip', 'gzip'],
|
||||||
// RFC 9110 §12.4.2 qvalues only: a malformed or out-of-range q excludes the member.
|
|
||||||
['gzip;q=2, br;q=1', 'br'],
|
|
||||||
['gzip;q=1e0', undefined],
|
|
||||||
['gzip;q=0.5000', undefined],
|
|
||||||
['gzip;q=-1', undefined],
|
|
||||||
['gzip;q=0.500, br;q=0.4', 'gzip'],
|
|
||||||
['gzip;q=1.000, br;q=1.', 'br'],
|
|
||||||
['GZIP', 'gzip'],
|
['GZIP', 'gzip'],
|
||||||
])('%s → %s', (header, expected) => {
|
])('%s → %s', (header, expected) => {
|
||||||
expect(negotiate(header, both)).toBe(expected);
|
expect(negotiate(header, both)).toBe(expected);
|
||||||
|
|||||||
Reference in new issue
Block a user