|
| 1 | +// Copied from Next.js source code |
| 2 | +// https://github.com/vercel/next.js/blob/canary/packages/next/src/server/accept-header.ts |
| 3 | + |
| 4 | +interface Selection { |
| 5 | + pos: number; |
| 6 | + pref?: number; |
| 7 | + q: number; |
| 8 | + token: string; |
| 9 | +} |
| 10 | + |
| 11 | +interface Options { |
| 12 | + prefixMatch?: boolean; |
| 13 | + type: "accept-language"; |
| 14 | +} |
| 15 | + |
| 16 | +function parse( |
| 17 | + raw: string, |
| 18 | + preferences: string[] | undefined, |
| 19 | + options: Options, |
| 20 | +) { |
| 21 | + const lowers = new Map<string, { orig: string; pos: number }>(); |
| 22 | + const header = raw.replace(/[ \t]/g, ""); |
| 23 | + |
| 24 | + if (preferences) { |
| 25 | + let pos = 0; |
| 26 | + for (const preference of preferences) { |
| 27 | + const lower = preference.toLowerCase(); |
| 28 | + lowers.set(lower, { orig: preference, pos: pos++ }); |
| 29 | + if (options.prefixMatch) { |
| 30 | + const parts = lower.split("-"); |
| 31 | + while ((parts.pop(), parts.length > 0)) { |
| 32 | + const joined = parts.join("-"); |
| 33 | + if (!lowers.has(joined)) { |
| 34 | + lowers.set(joined, { orig: preference, pos: pos++ }); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const parts = header.split(","); |
| 42 | + const selections: Selection[] = []; |
| 43 | + const map = new Set<string>(); |
| 44 | + |
| 45 | + for (let i = 0; i < parts.length; ++i) { |
| 46 | + const part = parts[i]; |
| 47 | + if (!part) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + const params = part.split(";"); |
| 52 | + if (params.length > 2) { |
| 53 | + throw new Error(`Invalid ${options.type} header`); |
| 54 | + } |
| 55 | + |
| 56 | + let token = params[0].toLowerCase(); |
| 57 | + if (!token) { |
| 58 | + throw new Error(`Invalid ${options.type} header`); |
| 59 | + } |
| 60 | + |
| 61 | + const selection: Selection = { token, pos: i, q: 1 }; |
| 62 | + if (preferences && lowers.has(token)) { |
| 63 | + selection.pref = lowers.get(token)!.pos; |
| 64 | + } |
| 65 | + |
| 66 | + map.add(selection.token); |
| 67 | + |
| 68 | + if (params.length === 2) { |
| 69 | + const q = params[1]; |
| 70 | + const [key, value] = q.split("="); |
| 71 | + |
| 72 | + if (!value || (key !== "q" && key !== "Q")) { |
| 73 | + throw new Error(`Invalid ${options.type} header`); |
| 74 | + } |
| 75 | + |
| 76 | + const score = parseFloat(value); |
| 77 | + if (score === 0) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + if (Number.isFinite(score) && score <= 1 && score >= 0.001) { |
| 82 | + selection.q = score; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + selections.push(selection); |
| 87 | + } |
| 88 | + |
| 89 | + selections.sort((a, b) => { |
| 90 | + if (b.q !== a.q) { |
| 91 | + return b.q - a.q; |
| 92 | + } |
| 93 | + |
| 94 | + if (b.pref !== a.pref) { |
| 95 | + if (a.pref === undefined) { |
| 96 | + return 1; |
| 97 | + } |
| 98 | + |
| 99 | + if (b.pref === undefined) { |
| 100 | + return -1; |
| 101 | + } |
| 102 | + |
| 103 | + return a.pref - b.pref; |
| 104 | + } |
| 105 | + |
| 106 | + return a.pos - b.pos; |
| 107 | + }); |
| 108 | + |
| 109 | + const values = selections.map((selection) => selection.token); |
| 110 | + if (!preferences || !preferences.length) { |
| 111 | + return values; |
| 112 | + } |
| 113 | + |
| 114 | + const preferred: string[] = []; |
| 115 | + for (const selection of values) { |
| 116 | + if (selection === "*") { |
| 117 | + for (const [preference, value] of lowers) { |
| 118 | + if (!map.has(preference)) { |
| 119 | + preferred.push(value.orig); |
| 120 | + } |
| 121 | + } |
| 122 | + } else { |
| 123 | + const lower = selection.toLowerCase(); |
| 124 | + if (lowers.has(lower)) { |
| 125 | + preferred.push(lowers.get(lower)!.orig); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return preferred; |
| 131 | +} |
| 132 | + |
| 133 | +export function acceptLanguage(header = "", preferences?: string[]) { |
| 134 | + return ( |
| 135 | + parse(header, preferences, { |
| 136 | + type: "accept-language", |
| 137 | + prefixMatch: true, |
| 138 | + })[0] || undefined |
| 139 | + ); |
| 140 | +} |
0 commit comments