index.ts
This file
Path
src/middleware/serve-static/index.ts
135 lines 路 4.0 KiB 路 TypeScript
Last changed by
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136/** * @module * Serve Static Middleware for Hono. */
import type { Context, Data } from '../../context'import type { Env, MiddlewareHandler } from '../../types'import { COMPRESSIBLE_CONTENT_TYPE_REGEX } from '../../utils/compress'import { getMimeType } from '../../utils/mime'import { tryDecodeURI } from '../../utils/url'import { defaultJoin } from './path'
export type ServeStaticOptions<E extends Env = Env> = { root?: string path?: string precompressed?: boolean /** Allow percent signs in the routed request path. Defaults to false. */ allowPercentInPath?: boolean mimes?: Record<string, string> rewriteRequestPath?: (path: string) => string onFound?: (path: string, c: Context<E>) => void | Promise<void> onNotFound?: (path: string, c: Context<E>) => void | Promise<void>}
const ENCODINGS = { br: '.br', zstd: '.zst', gzip: '.gz',} as constconst ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS) as (keyof typeof ENCODINGS)[]
const DEFAULT_DOCUMENT = 'index.html'
/** * This middleware is not directly used by the user. Create a wrapper specifying `getContent()` by the environment such as Deno or Bun. */export const serveStatic = <E extends Env = Env>( options: ServeStaticOptions<E> & { getContent: (path: string, c: Context<E>) => Promise<Data | Response | null> /** * * `join` option according to the runtime. Example `import { join } from 'node:path`. If not specified, it will fall back to the default join function.` */ join?: (...paths: string[]) => string /** * @deprecated Currently, `pathResolve` is no longer used. */ pathResolve?: (path: string) => string isDir?: (path: string) => boolean | undefined | Promise<boolean | undefined> }): MiddlewareHandler => { const root = options.root ?? './' const optionPath = options.path const join = options.join ?? defaultJoin
return async (c, next) => { // Do nothing if Response is already set if (c.finalized) { return next() }
if (c.req.method !== 'GET' && c.req.method !== 'HEAD') { return next() }
let filename: string
if (options.path) { filename = options.path } else { try { if (!options.allowPercentInPath && c.req.path.includes('%')) { throw new Error() } filename = tryDecodeURI(c.req.path) if (/(?:^|[\/\\])\.{1,2}(?:$|[\/\\])|[\/\\]{2,}|\\/.test(filename)) { throw new Error() } } catch { await options.onNotFound?.(c.req.path, c) return next() } }
let path = join( root, !optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename )
if (options.isDir && (await options.isDir(path))) { path = join(path, DEFAULT_DOCUMENT) }
const getContent = options.getContent let content = await getContent(path, c)
if (content instanceof Response) { return c.newResponse(content.body, content) }
if (content != null) { const mimeType = (options.mimes && getMimeType(path, options.mimes)) || getMimeType(path) c.header('Content-Type', mimeType || 'application/octet-stream')
if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) { const acceptEncodingSet = new Set( c.req .header('Accept-Encoding') ?.split(',') .map((encoding) => encoding.trim()) )
for (const encoding of ENCODINGS_ORDERED_KEYS) { if (!acceptEncodingSet.has(encoding)) { continue } const compressedContent = (await getContent(path + ENCODINGS[encoding], c)) as Data | null
if (compressedContent) { content = compressedContent c.header('Content-Encoding', encoding) c.header('Vary', 'Accept-Encoding', { append: true }) break } } } await options.onFound?.(path, c) return c.body(content) }
await options.onNotFound?.(path, c) await next() return }}