| 15 | * [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4) |
| 16 | */ |
| 17 | export class AcceptEncoding implements HeaderValue, Iterable<[string, number]> { |
| 18 | #map!: Map<string, number> |
| 19 | |
| 20 | constructor(init?: string | AcceptEncodingInit) { |
| 21 | if (init) return AcceptEncoding.from(init) |
| 22 | this.#map = new Map() |
| 23 | } |
| 24 | |
| 25 | #sort() { |
| 26 | this.#map = new Map([...this.#map].sort((a, b) => b[1] - a[1])) |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * An array of all encodings in the header. |
| 31 | */ |
| 32 | get encodings(): string[] { |
| 33 | return Array.from(this.#map.keys()) |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * An array of all weights (q values) in the header. |
| 38 | */ |
| 39 | get weights(): number[] { |
| 40 | return Array.from(this.#map.values()) |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * The number of encodings in the header. |
| 45 | */ |
| 46 | get size(): number { |
| 47 | return this.#map.size |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns `true` if the header matches the given encoding (i.e. it is "acceptable"). |
| 52 | * |
| 53 | * @param encoding The encoding to check |
| 54 | * @returns `true` if the encoding is acceptable, `false` otherwise |
| 55 | */ |
| 56 | accepts(encoding: string): boolean { |
| 57 | return encoding.toLowerCase() === 'identity' || this.getWeight(encoding) > 0 |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Gets the weight an encoding. Performs wildcard matching so `*` matches all encodings. |
| 62 | * |
| 63 | * @param encoding The encoding to get |
| 64 | * @returns The weight of the encoding, or `0` if it is not in the header |
| 65 | */ |
| 66 | getWeight(encoding: string): number { |
| 67 | let lower = encoding.toLowerCase() |
| 68 | |
| 69 | for (let [enc, weight] of this) { |
| 70 | if (enc === lower || enc === '*' || lower === '*') { |
| 71 | return weight |
| 72 | } |
| 73 | } |
| 74 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…