Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7df1eae5a1 |
No files matched your search
+10
-3
@@ -134,14 +134,21 @@ export function resolveCompression(
|
||||
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 {
|
||||
for (const param of params) {
|
||||
const eq = param.indexOf('=');
|
||||
|
||||
if (eq !== -1 && param.slice(0, eq).trim().toLowerCase() === 'q') {
|
||||
const weight = Number(param.slice(eq + 1).trim());
|
||||
return Number.isFinite(weight) ? weight : 0;
|
||||
const raw = param.slice(eq + 1).trim();
|
||||
return QVALUE.test(raw) ? Number(raw) : 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,13 @@ describe('negotiate() (RFC 9110 §12.5.3)', () => {
|
||||
['gzip;q=1, br;q=0.5', 'gzip'],
|
||||
['gzip;q=0.5, *', 'br'],
|
||||
['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'],
|
||||
])('%s → %s', (header, expected) => {
|
||||
expect(negotiate(header, both)).toBe(expected);
|
||||
|
||||
Reference in new issue
Block a user