1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.Encodings = void 0;
|
8 | var Encodings;
|
9 | (function (Encodings) {
|
10 | function getEncodingHeaderValue(encodings) {
|
11 | if (encodings.length === 1) {
|
12 | return encodings[0].name;
|
13 | }
|
14 | const distribute = encodings.length - 1;
|
15 | if (distribute > 1000) {
|
16 | throw new Error(`Quality value can only have three decimal digits but trying to distribute ${encodings.length} elements.`);
|
17 | }
|
18 | const digits = Math.ceil(Math.log10(distribute));
|
19 | const factor = Math.pow(10, digits);
|
20 | const diff = Math.floor((1 / distribute) * factor) / factor;
|
21 | const result = [];
|
22 | let q = 1;
|
23 | for (const encoding of encodings) {
|
24 | result.push(`${encoding.name};q=${q === 1 || q === 0 ? q.toFixed(0) : q.toFixed(digits)}`);
|
25 | q = q - diff;
|
26 | }
|
27 | return result.join(', ');
|
28 | }
|
29 | Encodings.getEncodingHeaderValue = getEncodingHeaderValue;
|
30 | function parseEncodingHeaderValue(value) {
|
31 | const map = new Map();
|
32 | const encodings = value.split(/\s*,\s*/);
|
33 | for (const value of encodings) {
|
34 | const [encoding, q] = parseEncoding(value);
|
35 | if (encoding === '*') {
|
36 | continue;
|
37 | }
|
38 | let values = map.get(q);
|
39 | if (values === undefined) {
|
40 | values = [];
|
41 | map.set(q, values);
|
42 | }
|
43 | values.push(encoding);
|
44 | }
|
45 | const keys = Array.from(map.keys());
|
46 | keys.sort((a, b) => b - a);
|
47 | const result = [];
|
48 | for (const key of keys) {
|
49 | result.push(...map.get(key));
|
50 | }
|
51 | return result;
|
52 | }
|
53 | Encodings.parseEncodingHeaderValue = parseEncodingHeaderValue;
|
54 | function parseEncoding(value) {
|
55 | let q = 1;
|
56 | let encoding;
|
57 | const index = value.indexOf(';q=');
|
58 | if (index !== -1) {
|
59 | const parsed = parseFloat(value.substr(index));
|
60 | if (!Number.isNaN(parsed)) {
|
61 | q = parsed;
|
62 | }
|
63 | encoding = value.substr(0, index);
|
64 | }
|
65 | else {
|
66 | encoding = value;
|
67 | }
|
68 | return [encoding, q];
|
69 | }
|
70 | })(Encodings || (exports.Encodings = Encodings = {}));
|