1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x7
 
 
 
 
 
x7
 
x23
x23
x23
 
x23
 
 
 
 
x23
x23
x23
x31
x31
x31
x31
x31
x31
x31
x31
x31
 
x161
x23
 
x42
x42
x42
x42
 
 
 
 
x42
x42
x46
x42
x96
x96
 
x54
x54
x54
x54
x54
x54
x42
 
x14
x14
x14
x14
x14
 
x14
x30
 
x30
x30
x30
 
x30
x30
 
x14
x17
x17
x17
x17
x17
x17
x17
x17
 
x14
x14
 
x14
x14
x14
x14
 
x84
 
x14
x33
 
 
 
 
 
 
 
x39
x39
x33
 
x14
x14
 
 
 
 
x7
x7
x7
 
x14
 
x14
x16
x16
x16
x16
x16
 
x19
x19
 
 
x19
x19
x19
x19
x14 |
I
I
I
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
/*!
* Adapted directly from negotiator at https://github.com/jshttp/negotiator/
* which is licensed as follows:
*
* (The MIT License)
*
* Copyright (c) 2012-2014 Federico Romero
* Copyright (c) 2012-2014 Isaac Z. Schlueter
* Copyright (c) 2014-2015 Douglas Christopher Wilson
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { compareSpecs, isQuality, type Specificity } from "./common.ts";
interface EncodingSpecificity extends Specificity {
encoding?: string;
}
const simpleEncodingRegExp = /^\s*([^\s;]+)\s*(?:;(.*))?$/;
function parseEncoding(
str: string,
i: number,
): EncodingSpecificity | undefined {
const match = simpleEncodingRegExp.exec(str);
if (!match) {
return undefined;
}
const encoding = match[1]!;
let q = 1;
if (match[2]) {
const params = match[2].split(";");
for (const param of params) {
const p = param.trim().split("=");
if (p[0] === "q" && p[1]) {
q = parseFloat(p[1]);
break;
}
}
}
return { encoding, o: undefined, q, i, s: undefined };
}
function specify(
encoding: string,
spec: EncodingSpecificity,
i = -1,
): Specificity | undefined {
if (!spec.encoding) {
return;
}
let s = 0;
if (spec.encoding.toLowerCase() === encoding.toLowerCase()) {
s = 1;
} else if (spec.encoding !== "*") {
return;
}
return {
i,
o: spec.i,
q: spec.q,
s,
};
}
function parseAcceptEncoding(accept: string): EncodingSpecificity[] {
const accepts = accept.split(",");
const parsedAccepts: EncodingSpecificity[] = [];
let hasIdentity = false;
let minQuality = 1;
for (const [i, accept] of accepts.entries()) {
const encoding = parseEncoding(accept.trim(), i);
if (encoding) {
parsedAccepts.push(encoding);
hasIdentity = hasIdentity || !!specify("identity", encoding);
minQuality = Math.min(minQuality, encoding.q || 1);
}
}
if (!hasIdentity) {
parsedAccepts.push({
encoding: "identity",
o: undefined,
q: minQuality,
i: accepts.length - 1,
s: undefined,
});
}
return parsedAccepts;
}
function getEncodingPriority(
encoding: string,
accepted: Specificity[],
index: number,
): Specificity {
let priority: Specificity = { o: -1, q: 0, s: 0, i: 0 };
for (const s of accepted) {
const spec = specify(encoding, s, index);
if (
spec &&
(priority.s! - spec.s! || priority.q - spec.q ||
priority.o! - spec.o!) <
0
) {
priority = spec;
}
}
return priority;
}
/** Given an `Accept-Encoding` string, parse out the encoding returning a
* negotiated encoding based on the `provided` encodings otherwise just a
* prioritized array of encodings. */
export function preferredEncodings(
accept: string,
provided?: string[],
): string[] {
const accepts = parseAcceptEncoding(accept);
if (!provided) {
return accepts
.filter(isQuality)
.sort(compareSpecs)
.map((spec) => spec.encoding!);
}
const priorities = provided.map((type, index) =>
getEncodingPriority(type, accepts, index)
);
return priorities
.filter(isQuality)
.sort(compareSpecs)
.map((priority) => provided[priorities.indexOf(priority)]!);
}
|