import path from 'path' ; import mime from 'mime-types' ; import { RenderResult } from '../../type' ; import { ModernServerContext } from '../context' ; import { readFile } from './reader' ; export async function handleDirectory ( ctx , entryPath , urlPath ) { const { path : pathname } = ctx ; const filepath = path . join ( entryPath , trimLeft ( pathname , urlPath ) ) ; let content = await readFile ( filepath ) ; let contentType = mime . contentType ( path . extname ( filepath ) || '' ) ; if ( ! content ) { if ( pathname . endsWith ( '/' ) ) { content = await readFile ( `${ filepath }index.html` ) ; } else if ( ! pathname . includes ( '.' ) ) { content = await readFile ( `${ filepath }.html` ) ; if ( ! content ) { content = await readFile ( `${ filepath }/index.html` ) ; } } if ( content ) { contentType = mime . contentType ( 'html' ) ; } } if ( ! content ) { return null ; } return { content , contentType : contentType || '' , } ; } const trimLeft = ( str , prefix ) : string => { if ( str . startsWith ( prefix ) ) { return str . substring ( prefix . length ) ; } return str ; } ;