import { NOT_APPLICABLE , UNKNOWN , UP_TO_DATE , OUTDATED } from './constants' ; import type { UsbDevice , DriverStatus } from './types' ; const REALTEK_UP_TO_DATE_VERSION = [ 10 , 38 ] ; const REALTEK_VID = parseInt ( '0BDA' , 16 ) ; const RE_REALTEK_PID = /^8[0|1]5[0-9]$/ ; export const isRealtekU2EAdapter = ( device ) : boolean => { return ( device . vendorId === REALTEK_VID && RE_REALTEK_PID . test ( device . productId . toString ( 16 ) ) ) ; } ; export const getDriverStatus = ( device ) : DriverStatus => { const { windowsDriverVersion } = device ; if ( ! isRealtekU2EAdapter ( device ) || typeof windowsDriverVersion === 'undefined' ) { return NOT_APPLICABLE ; } if ( windowsDriverVersion === null ) return UNKNOWN ; const versionParts = windowsDriverVersion . split ( '.' ) . map ( s => Number ( s ) ) ; if ( ! versionParts . every ( p => Number . isFinite ( p ) ) ) return UNKNOWN ; const upToDate = REALTEK_UP_TO_DATE_VERSION . reduce < boolean | null > ( ( result , subversion , index , collection ) => { if ( result === null ) { const target = versionParts [ index ] ?? 0 ; if ( target > subversion ) return true ; if ( target < subversion ) return false ; if ( index === collection . length - 1 ) return target >= subversion ; } return result ; } , null ) ; return upToDate ? UP_TO_DATE : OUTDATED ; } ;