Skip to content
Myr

serve-static.ts

This file

Path

adapters/bun/src/serve-static.ts

32 lines · 933 bytes · TypeScript

Last changed by

feat(adapters): add @hono/bun as a workspace package (#5447)Yusuke Wadabefore adoptionfc2343d

History of this file

Take it

feat(adapters): add @hono/bun as a workspace package (#5447)Yusuke Wadabefore adoptionfc2343d

lines 1–32why, one step back
1/* eslint-disable @typescript-eslint/ban-ts-comment */
2import { stat } from 'node:fs/promises'3import { join } from 'node:path'4import { serveStatic as baseServeStatic } from 'hono/serve-static'5import type { ServeStaticOptions } from 'hono/serve-static'6import type { Env, MiddlewareHandler } from 'hono/types'78export 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 // @ts-ignore14 const file = Bun.file(path)15 return (await file.exists()) ? file : null16 }17 const isDir = async (path: string) => {18 let isDir19 try {20 const stats = await stat(path)21 isDir = stats.isDirectory()22 } catch {}23 return isDir24 }25 return baseServeStatic({26 ...options,27 getContent,28 join,29 isDir,30 })(c, next)31 }32}