Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serve-static): mimes option for serve-static #135

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
8 changes: 7 additions & 1 deletion src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
root?: string
path?: string
mimes?: Record<string, string>
index?: string // default is 'index.html'
rewriteRequestPath?: (path: string) => string
onNotFound?: (path: string, c: Context) => void | Promise<void>
Expand Down Expand Up @@ -60,7 +61,12 @@
return next()
}

const mimeType = getMimeType(path)
let mimeType: string | undefined = undefined
if (options.mimes) {
mimeType = getMimeType(path, options.mimes) ?? getMimeType(path)

Check failure on line 66 in src/serve-static.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Expected 1 arguments, but got 2.

Check failure on line 66 in src/serve-static.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Expected 1 arguments, but got 2.
} else {
mimeType = getMimeType(path)
}
if (mimeType) {
c.header('Content-Type', mimeType)
}
Expand Down
Empty file.
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions test/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,30 @@ describe('Serve Static Middleware', () => {
expect(res.status).toBe(404)
})
})

describe('With `mimes` options', () => {
const mimes = {
m3u8: 'application/vnd.apple.mpegurl',
ts: 'video/mp2t',
}
const app = new Hono()
app.use('/static/*', serveStatic({ root: './assets', mimes }))

const server = createAdaptorServer(app)

it('Should return content-type of m3u8', async () => {
const res = await request(server).get('/static/video/morning-routine.m3u8')
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('application/vnd.apple.mpegurl')
})
it('Should return content-type of ts', async () => {
const res = await request(server).get('/static/video/morning-routine1.ts1')
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('video/mp2t')
})
it('Should return content-type of default on Hono', async () => {
const res = await request(server).get('/static/video/introduction.mp4')
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('video/mp4')
})
})
Loading