serve-static.ts
This file
Path
src/adapter/bun/serve-static.ts
35 lines 路 1.0 KiB 路 TypeScript
Last changed by
feat(adapters): add @hono/bun as a workspace package (#5447)Yusuke Wadabefore adoptionfc2343d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536/* eslint-disable @typescript-eslint/ban-ts-comment */import { stat } from 'node:fs/promises'import { join } from 'node:path'import { serveStatic as baseServeStatic } from '../../middleware/serve-static'import type { ServeStaticOptions } from '../../middleware/serve-static'import type { Env, MiddlewareHandler } from '../../types'
/** * @deprecated `hono/bun` will be removed in v5. Install `@hono/bun` and import from there instead. */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) }}