Repository navigation
Expand file tree
/
Copy pathnegotiateContentType.ts
More file actions
100 lines (85 loc) · 3.4 KB
/
Copy pathnegotiateContentType.ts
File metadata and controls
100 lines (85 loc) · 3.4 KB
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
/** Representation of a page chosen for a request's `Accept` header. */
export type PageRepresentation = 'html' | 'markdown' | 'not-acceptable';
/** One media range from an `Accept` header. */
interface MediaRange {
/** Top-level type, lower-cased, or `*`. */
type: string;
/** Subtype, lower-cased, or `*`. */
subtype: string;
/** Quality weight between 0 and 1. */
quality: number;
}
/** A media range's weight for one candidate type, with how precisely the range named it. */
interface RangeMatch {
/** Quality weight of the matching range. */
quality: number;
/** 3 for an exact `type/subtype`, 2 for a `type/*` range, 1 for the full wildcard. */
specificity: number;
}
/**
* Parses an `Accept` header into media ranges, skipping malformed entries.
* @param accept Raw `Accept` header value
* @returns The well-formed media ranges, in header order
*/
function parseAccept(accept: string): MediaRange[] {
const ranges: MediaRange[] = [];
for (const entry of accept.split(',')) {
const [mediaType, ...params] = entry.split(';').map((part) => part.trim().toLowerCase());
const [type, subtype] = mediaType.split('/');
const qualityParam = params.find((param) => param.startsWith('q='));
const quality = qualityParam === undefined ? 1 : Number(qualityParam.slice(2));
if (type && subtype && Number.isFinite(quality) && quality >= 0 && quality <= 1) {
ranges.push({ type, subtype, quality });
}
}
return ranges;
}
/**
* Finds the most specific media range matching a candidate type, as RFC 9110 section 12.5.1 requires.
* @param ranges Parsed media ranges
* @param candidate Candidate media type, such as `text/html`
* @returns The matching range's quality and specificity, or `null` when no range matches
*/
function matchRange(ranges: MediaRange[], candidate: string): RangeMatch | null {
const [type, subtype] = candidate.split('/');
let best: RangeMatch | null = null;
for (const range of ranges) {
let specificity = 0;
if (range.type === type && range.subtype === subtype) {
specificity = 3;
} else if (range.type === type && range.subtype === '*') {
specificity = 2;
} else if (range.type === '*' && range.subtype === '*') {
specificity = 1;
}
if (specificity > 0 && (best === null || specificity > best.specificity)) {
best = { quality: range.quality, specificity };
}
}
return best;
}
/**
* Chooses between the HTML and Markdown representations of a page for an `Accept` header.
*
* HTML is the default for a missing header, the full wildcard, and every browser. Markdown is chosen when it carries a
* higher weight than HTML, or the same weight with `text/markdown` named explicitly, since browsers never name it.
* @param accept Raw `Accept` header value, or `null` when the request has none
* @returns The representation to serve, or `not-acceptable` when the client accepts neither
*/
export function negotiateContentType(accept: string | null): PageRepresentation {
const ranges = parseAccept(accept ?? '');
if (ranges.length === 0) {
return 'html';
}
const html = matchRange(ranges, 'text/html');
const markdown = matchRange(ranges, 'text/markdown');
const htmlQuality = html?.quality ?? 0;
const markdownQuality = markdown?.quality ?? 0;
if (htmlQuality === 0 && markdownQuality === 0) {
return 'not-acceptable';
}
if (markdownQuality > htmlQuality || (markdownQuality === htmlQuality && markdown?.specificity === 3)) {
return 'markdown';
}
return 'html';
}