import { Middleware } from '../Router' ; interface KVStaticOptions { kv : KVNamespace ; maxAge ? : number ; } param options export function kvStatic ( options ) { return async ( ctx , next ) => { const contentTypes = { css : 'text/css' , js : 'application/javascript' , map : 'application/json' , json : 'application/json' , html : 'text/html' , png : 'image/png' , svg : 'image/svg+xml' , gif : 'image/gif' , jpg : 'image/jpg' , jpeg : 'image/jpg' , } as const ; const supportedExtensions = Object . keys ( contentTypes ) ; if ( ! supportedExtensions . some ( ( ext ) => ctx . event . request . url . endsWith ( '.' + ext ) ) ) { return next ( ) ; } const filename = ctx . event . request . url . substring ( ctx . event . request . url . lastIndexOf ( '/' ) + 1 ) ; const ext = filename . substring ( filename . lastIndexOf ( '.' ) + 1 ) ; const contentType = contentTypes [ ext as keyof typeof contentTypes ] || 'text' ; const body = await options . kv . get ( filename , 'stream' ) ; if ( ! body ) return ctx . end ( '' , { status : 404 } ) ; return ctx . end ( body , { headers : { 'Content-Type' : contentType , 'Cache-Control' : options . maxAge ? `max-age=${ options . maxAge }` : 'public' , } , } ) ; } ; }