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 fileTake it
123456789101112131415161718192021222324252627282930313233/* eslint-disable @typescript-eslint/ban-ts-comment */import { stat } from 'node:fs/promises'import { join } from 'node:path'import { serveStatic as baseServeStatic } from 'hono/serve-static'import type { ServeStaticOptions } from 'hono/serve-static'import type { Env, MiddlewareHandler } from 'hono/types'
export const serveStatic = <E extends Env = Env>( options: ServeStaticOptions<E> = {}): MiddlewareHandler => { return async function serveStatic(c, next) { const getContent = async (path: string) => { // @ts-ignore const file = Bun.file(path) return (await file.exists()) ? file : null } const isDir = async (path: string) => { let isDir try { const stats = await stat(path) isDir = stats.isDirectory() } catch {} return isDir } return baseServeStatic({ ...options, getContent, join, isDir, })(c, next) }}