diff --git a/src/serve-static.ts b/src/serve-static.ts index b72ef26..bafc07d 100644 --- a/src/serve-static.ts +++ b/src/serve-static.ts @@ -10,6 +10,7 @@ export type ServeStaticOptions = { */ root?: string path?: string + mimes?: Record index?: string // default is 'index.html' rewriteRequestPath?: (path: string) => string onNotFound?: (path: string, c: Context) => void | Promise @@ -60,7 +61,12 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew return next() } - const mimeType = getMimeType(path) + let mimeType: string | undefined = undefined + if (options.mimes) { + mimeType = getMimeType(path, options.mimes) ?? getMimeType(path) + } else { + mimeType = getMimeType(path) + } if (mimeType) { c.header('Content-Type', mimeType) } diff --git a/test/assets/static/video/introduction.mp4 b/test/assets/static/video/introduction.mp4 new file mode 100644 index 0000000..e69de29 diff --git a/test/assets/static/video/morning-routine.m3u8 b/test/assets/static/video/morning-routine.m3u8 new file mode 100644 index 0000000..e69de29 diff --git a/test/assets/static/video/morning-routine1.ts1 b/test/assets/static/video/morning-routine1.ts1 new file mode 100644 index 0000000..e69de29 diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index b26a6e8..1e58b3a 100644 --- a/test/serve-static.test.ts +++ b/test/serve-static.test.ts @@ -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') + }) +})