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 fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243import { join } from 'node:path'import type { ServeStaticOptions } from 'hono/serve-static'import { serveStatic as baseServeStatic } from 'hono/serve-static'import type { Env, MiddlewareHandler } from 'hono/types'
const { open, lstatSync, errors } = Deno
export const serveStatic = <E extends Env = Env>( options: ServeStaticOptions<E> = {}): MiddlewareHandler => { return async function serveStatic(c, next) { const getContent = async (path: string) => { try { if (isDir(path)) { return null }
const file = await open(path) return file.readable } catch (e) { if (!(e instanceof errors.NotFound)) { console.warn(`${e}`) } return null } } const isDir = (path: string) => { let isDir try { const stat = lstatSync(path) isDir = stat.isDirectory } catch {} return isDir } return baseServeStatic({ ...options, getContent, join, isDir, })(c, next) }}