Skip to content
Myr

serve-static.ts

This file

Path

adapters/deno/src/serve-static.ts

42 lines · 1.0 KiB · TypeScript

Last changed by

feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827

History of this file

Take it

feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827

lines 1–42why, one step back
1import { join } from 'node:path'
2import type { ServeStaticOptions } from 'hono/serve-static'3import { serveStatic as baseServeStatic } from 'hono/serve-static'4import type { Env, MiddlewareHandler } from 'hono/types'56const { open, lstatSync, errors } = Deno78export const serveStatic = <E extends Env = Env>(9 options: ServeStaticOptions<E> = {}10): MiddlewareHandler => {11 return async function serveStatic(c, next) {12 const getContent = async (path: string) => {13 try {14 if (isDir(path)) {15 return null16 }1718 const file = await open(path)19 return file.readable20 } catch (e) {21 if (!(e instanceof errors.NotFound)) {22 console.warn(`${e}`)23 }24 return null25 }26 }27 const isDir = (path: string) => {28 let isDir29 try {30 const stat = lstatSync(path)31 isDir = stat.isDirectory32 } catch {}33 return isDir34 }35 return baseServeStatic({36 ...options,37 getContent,38 join,39 isDir,40 })(c, next)41 }42}