Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/send-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Context, Env } from 'hono'
import type { StatusCode } from 'hono/utils/http-status'
import { getMimeType } from 'hono/utils/mime'
import type { Stats } from 'node:fs'
import { createReadStream } from 'node:fs'
import { createStreamBody, getStats } from './serve-static'

export const sendFile = async <E extends Env = Env>(
c: Context<E, string, {}>,
path: string,
options: {
onNotFound?: (path: string, c: Context<E>) => Response | Promise<Response>
emptyBody?: {
[method: string]: StatusCode
}
} = {
emptyBody: {
HEAD: 204,
OPTIONS: 204,
},
}
): Promise<Response> => {
const stats: Stats | undefined = getStats(path)
if (!stats?.isFile()) {
return options.onNotFound ? options.onNotFound(path, c) : c.notFound()
}

const mimeType: string = getMimeType(path) || 'application/octet-stream'
c.header('Content-Type', mimeType)

const size = stats.size

const { emptyBody } = options
for (const [k, v] of Object.entries(emptyBody ?? {})) {
if (k === c.req.method) {
c.header('Content-Length', size.toString())
return c.body(null, v)
}
}

const range = c.req.header('range') || ''

if (!range) {
c.header('Content-Length', size.toString())
return c.body(createStreamBody(createReadStream(path)), 200)
}

c.header('Accept-Ranges', 'bytes')
c.header('Date', stats.birthtime.toUTCString())

const parts = range.replace(/bytes=/, '').split('-', 2)
const start = parts[0] ? Number.parseInt(parts[0], 10) : 0
let end = parts[1] ? Number.parseInt(parts[1], 10) : stats.size - 1
if (size < end - start + 1) {
end = size - 1
}

const chunksize = end - start + 1
const stream = createReadStream(path, { start, end })

c.header('Content-Length', chunksize.toString())
c.header('Content-Range', `bytes ${start}-${end}/${stats.size}`)

return c.body(createStreamBody(stream), 206)
}
4 changes: 2 additions & 2 deletions src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ENCODINGS = {
} as const
const ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS) as (keyof typeof ENCODINGS)[]

const createStreamBody = (stream: ReadStream) => {
export const createStreamBody = (stream: ReadStream) => {
const body = new ReadableStream({
start(controller) {
stream.on('data', (chunk) => {
Expand All @@ -48,7 +48,7 @@ const addCurrentDirPrefix = (path: string) => {
return `./${path}`
}

const getStats = (path: string) => {
export const getStats = (path: string) => {
let stats: Stats | undefined
try {
stats = lstatSync(path)
Expand Down