Skip to content
Myr

accepts.ts

This file

Path

src/helper/accepts/accepts.ts

96 lines · 2.5 KiB · TypeScript

Last changed by

fix(accepts): match media types and language tags case-insensitively (#5376)Harshit Prakashbefore adoptionf5a5346

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–20why, one step back
1import type { Context } from '../../context'
2import { parseAccept } from '../../utils/accept'3import type { AcceptHeader } from '../../utils/headers'45export interface Accept {6 type: string7 params: Record<string, string>8 q: number9}1011export interface acceptsConfig {12 header: AcceptHeader13 supports: string[]14 default: string15}1617export interface acceptsOptions extends acceptsConfig {18 match?: (accepts: Accept[], config: acceptsConfig) => string19}20

fix(accepts): support wildcard media types and specificity ordering in defaultMatch (#5255)ADITYA KUMAR JHAbefore adoption28a9c12

line 21why, one step back
21const matchType = (acceptType: string, supportedType: string): boolean => {

fix(accepts): match media types and language tags case-insensitively (#5376)Harshit Prakashbefore adoptionf5a5346

lines 22–28why, one step back
22 // Media types and language tags are both case-insensitive (RFC 9110 §8.3.1,
23 // RFC 4647 §2.1), so compare them case-folded. `defaultMatch` returns the24 // caller's own `supports` entry, so its casing is preserved.25 const accept = acceptType.toLowerCase()26 const supported = supportedType.toLowerCase()2728 if (accept === supported) {

fix(accepts): support wildcard media types and specificity ordering in defaultMatch (#5255)ADITYA KUMAR JHAbefore adoption28a9c12

lines 29–30why, one step back
29 return true
30 }

fix(accepts): match media types and language tags case-insensitively (#5376)Harshit Prakashbefore adoptionf5a5346

line 31why, one step back
31 if (accept === '*/*' || accept === '*') {

fix(accepts): support wildcard media types and specificity ordering in defaultMatch (#5255)ADITYA KUMAR JHAbefore adoption28a9c12

lines 32–33why, one step back
32 return false
33 }

fix(accepts): match media types and language tags case-insensitively (#5376)Harshit Prakashbefore adoptionf5a5346

lines 34–36why, one step back
34 if (accept.endsWith('/*')) {
35 const [acceptMain] = accept.split('/')36 const [supportedMain] = supported.split('/')

fix(accepts): support wildcard media types and specificity ordering in defaultMatch (#5255)ADITYA KUMAR JHAbefore adoption28a9c12

lines 37–51why, one step back
37 return acceptMain === supportedMain
38 }39 return false40}4142const getSpecificity = (type: string): number => {43 if (type === '*/*' || type === '*') {44 return 145 }46 if (type.endsWith('/*')) {47 return 248 }49 return 350}51

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 52–53why, one step back
52export const defaultMatch = (accepts: Accept[], config: acceptsConfig): string => {
53 const { supports, default: defaultSupport } = config

fix(accepts, language): skip accept entries with quality 0 when matching (#5311)LuckTerencebefore adoptionf147de5

lines 54–63why, one step back
54 // A quality value of 0 means "not acceptable" (RFC 9110 §12.5.1), so such
55 // entries must never match, the same rule the compress middleware applies.56 const sortedAccepts = accepts57 .filter((accept) => accept.q > 0)58 .sort((a, b) => {59 if (b.q !== a.q) {60 return b.q - a.q61 }62 return getSpecificity(b.type) - getSpecificity(a.type)63 })

fix(accepts): support wildcard media types and specificity ordering in defaultMatch (#5255)ADITYA KUMAR JHAbefore adoption28a9c12

lines 64–71why, one step back
64
65 for (const accept of sortedAccepts) {66 const matched = supports.find((supported) => matchType(accept.type, supported))67 if (matched) {68 return matched69 }70 }71 return defaultSupport

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 72–96why, one step back
72}
7374/**75 * Match the accept header with the given options.76 * @example77 * ```ts78 * app.get('/users', (c) => {79 * const lang = accepts(c, {80 * header: 'Accept-Language',81 * supports: ['en', 'zh'],82 * default: 'en',83 * })84 * })85 * ```86 */87export const accepts = (c: Context, options: acceptsOptions): string => {88 const acceptHeader = c.req.header(options.header)89 if (!acceptHeader) {90 return options.default91 }92 const accepts = parseAccept(acceptHeader)93 const match = options.match || defaultMatch9495 return match(accepts, options)96}