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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x7
 
 
 
 
 
 
 
x7
 
x15
x15
x60
x15
 
x53
x53
x53
 
x53
 
 
 
 
 
x53
 
 
 
 
x53
x53
x53
x61
x61
 
 
x61
 
 
 
 
x61
x61
x61
x61
 
 
 
x61
 
x477
x53
 
x12
x12
 
x12
x12
x35
 
x35
x35
x35
x35
 
x12
x12
 
x17
x17
x17
 
x30
x30
x30
x30
 
x30
 
 
 
 
 
x30
 
x30
x36
x30
x60
x60
 
x30
x49
x45
x53
x53
 
x38
 
 
 
 
 
 
 
 
 
 
 
 
 
x38
x38
x38
x38
x38
x38
x30
 
x12
x12
x12
x12
 
x72
 
x12
x35
 
 
 
 
 
 
 
x39
x39
x35
 
x12
x12
 
x7
x7
x7
 
 
 
x12
x14
x14
x14
x14
x14
 
x15
x20
x15
 
x15
x15
x15
x15
x12 |
I
I
I
I
I
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 MediaTypeSpecificity extends Specificity {
type: string;
subtype: string;
params: { [param: string]: string | undefined };
}
const simpleMediaTypeRegExp = /^\s*([^\s\/;]+)\/([^;\s]+)\s*(?:;(.*))?$/;
function splitKeyValuePair(str: string): [string, string | undefined] {
const [key, value] = str.split("=");
return [key!.toLowerCase(), value];
}
function parseMediaType(
str: string,
i: number,
): MediaTypeSpecificity | undefined {
const match = simpleMediaTypeRegExp.exec(str);
if (!match) {
return;
}
const [, type, subtype, parameters] = match;
if (!type || !subtype) {
return;
}
const params: { [param: string]: string | undefined } = Object.create(null);
let q = 1;
if (parameters) {
const kvps = parameters.split(";").map((p) => p.trim()).map(
splitKeyValuePair,
);
for (const [key, val] of kvps) {
const value = val && val[0] === `"` && val[val.length - 1] === `"`
? val.slice(1, val.length - 1)
: val;
if (key === "q" && value) {
q = parseFloat(value);
break;
}
params[key] = value;
}
}
return { type, subtype, params, i, o: undefined, q, s: undefined };
}
function parseAccept(accept: string): MediaTypeSpecificity[] {
const accepts = accept.split(",").map((p) => p.trim());
const mediaTypes: MediaTypeSpecificity[] = [];
for (const [index, accept] of accepts.entries()) {
const mediaType = parseMediaType(accept.trim(), index);
if (mediaType) {
mediaTypes.push(mediaType);
}
}
return mediaTypes;
}
function getFullType(spec: MediaTypeSpecificity) {
return `${spec.type}/${spec.subtype}`;
}
function specify(
type: string,
spec: MediaTypeSpecificity,
index: number,
): Specificity | undefined {
const p = parseMediaType(type, index);
if (!p) {
return;
}
let s = 0;
if (spec.type.toLowerCase() === p.type.toLowerCase()) {
s |= 4;
} else if (spec.type !== "*") {
return;
}
if (spec.subtype.toLowerCase() === p.subtype.toLowerCase()) {
s |= 2;
} else if (spec.subtype !== "*") {
return;
}
const keys = Object.keys(spec.params);
if (keys.length) {
if (
keys.every((key) =>
(spec.params[key] ?? "").toLowerCase() ===
(p.params[key] ?? "").toLowerCase()
)
) {
s |= 1;
} else {
return;
}
}
return {
i: index,
o: spec.o,
q: spec.q,
s,
};
}
function getMediaTypePriority(
type: string,
accepted: MediaTypeSpecificity[],
index: number,
) {
let priority: Specificity = { o: -1, q: 0, s: 0, i: index };
for (const accepts of accepted) {
const spec = specify(type, accepts, index);
if (
spec &&
((priority.s ?? 0) - (spec.s ?? 0) ||
(priority.q ?? 0) - (spec.q ?? 0) ||
(priority.o ?? 0) - (spec.o ?? 0)) < 0
) {
priority = spec;
}
}
return priority;
}
export function preferredMediaTypes(
accept?: string | null,
provided?: string[],
): string[] {
const accepts = parseAccept(accept === undefined ? "*/*" : accept ?? "");
if (!provided) {
return accepts
.filter(isQuality)
.sort(compareSpecs)
.map(getFullType);
}
const priorities = provided.map((type, index) => {
return getMediaTypePriority(type, accepts, index);
});
return priorities
.filter(isQuality)
.sort(compareSpecs)
.map((priority) => provided[priorities.indexOf(priority)]!);
}
|