@@ -442,9 +442,11 @@ export function getNodeVersion() {
442442export async function asyncDetect ( options ?: {
443443 userAgent ?: string
444444 hints ?: UserAgentDataHints [ ]
445- httpHeaders ?: Record < string , string | undefined >
445+ httpHeaders ?: RequestHeaders
446446} ) {
447- const info = detect ( options ?. userAgent )
447+ const info = detect (
448+ options ?. userAgent ?? options ?. httpHeaders ?. [ 'user-agent' ] ?? options ?. httpHeaders ?. [ 'User-Agent' ] ,
449+ )
448450 if ( ! info )
449451 return info
450452
@@ -517,6 +519,18 @@ export async function lookupClientUserAgentHints(hints?: UserAgentDataHints[]) {
517519 return await userAgentData . getHighEntropyValues ( hints )
518520}
519521
522+ type _HTTPHeaderName = 'Sec-CH-UA' | 'Sec-CH-UA-Mobile' | 'Sec-CH-UA-Platform' | 'Sec-CH-UA-Arch' | 'Sec-CH-UA-Bitness' | 'Sec-CH-UA-Model' | 'Sec-CH-UA-Platform-Version' | 'Sec-CH-UA-Full-Version-List'
523+
524+ export type HTTPHeaderName =
525+ | _HTTPHeaderName
526+ | Lowercase < _HTTPHeaderName >
527+ // eslint-disable-next-line @typescript-eslint/ban-types
528+ | ( string & { } )
529+
530+ export type RequestHeaders = Partial <
531+ Record < HTTPHeaderName , string | undefined >
532+ >
533+
520534const uaAgentHints : Record < string , keyof UserAgentDataInfo > = {
521535 'Sec-CH-UA' : 'brands' ,
522536 'Sec-CH-UA-Mobile' : 'mobile' ,
@@ -528,7 +542,7 @@ const uaAgentHints: Record<string, keyof UserAgentDataInfo> = {
528542 'Sec-CH-UA-Full-Version-List' : 'fullVersionList' ,
529543}
530544
531- const serverAcceptCHHeaders : Record < UserAgentDataHints , string > = {
545+ const serverAcceptCHHeaders : Record < UserAgentDataHints , HTTPHeaderName > = {
532546 architecture : 'Sec-CH-UA-Arch' ,
533547 bitness : 'Sec-CH-UA-Bitness' ,
534548 model : 'Sec-CH-UA-Model' ,
@@ -539,9 +553,9 @@ const serverAcceptCHHeaders: Record<UserAgentDataHints, string> = {
539553/**
540554 * @see https://github.com/WICG/ua-client-hints
541555 */
542- export function lookupServerUserAgentHints ( httpHeaders : Record < string , string | undefined > ) {
556+ export function lookupServerUserAgentHints ( httpHeaders : RequestHeaders ) {
543557 return Object . entries ( uaAgentHints ) . reduce ( ( acc , [ header , key ] ) => {
544- const value = httpHeaders [ header ]
558+ const value = httpHeaders [ header ] ?? httpHeaders [ header . toLowerCase ( ) ]
545559 if ( value && value . length ) {
546560 if ( key === 'brands' || key === 'fullVersionList' ) {
547561 const parts = value . split ( ',' )
@@ -582,7 +596,7 @@ export function serverResponseHeadersForUserAgentHints(
582596 . filter ( ( [ key ] ) => hints . includes ( key as UserAgentDataHints ) )
583597 . map ( ( [ _ , header ] ) => header )
584598
585- return headers . length ? < Record < string , string > > { 'Accept-CH' : headers . join ( ', ' ) } : undefined
599+ return headers . length ? < RequestHeaders > { 'Accept-CH' : headers . join ( ', ' ) } : undefined
586600}
587601
588602export function getServerVersion ( ) {
0 commit comments