import { extname } from 'path' ; import Accept from '@hapi/accept' ; import { open } from './fs' ; declare module '@hapi/accept' { export function encodings ( encodingHeader ? , preferences ? ) ; } async function tryToOpenFile ( filePath ) { try { return await open ( filePath , 'r' ) ; } catch ( e ) { if ( e . code === 'ENOENT' ) { return undefined ; } else { throw e ; } } } export async function selectCompressedFile ( acceptEncodingHeader , path ) { let fd ; let fileEncoding ; const ext = extname ( path ) ; const supportedEncodings = Accept . encodings ( acceptEncodingHeader , [ 'br' , 'gzip' ] ) ; if ( ext === '.js' || ext === '.css' ) { if ( supportedEncodings [ 0 ] === 'br' ) { fileEncoding = 'br' ; fd = await tryToOpenFile ( `${ path }.br` ) ; } if ( ! fd && supportedEncodings . includes ( 'gzip' ) ) { fileEncoding = 'gzip' ; fd = await tryToOpenFile ( `${ path }.gz` ) ; } } if ( ! fd ) { fileEncoding = undefined ; fd = await open ( path , 'r' ) ; } return { fd , fileEncoding } ; }