From 2b4ceee7644beebf1c5c0fcbe86aeb0c9c76f362 Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 1 Feb 2024 01:24:18 +0900 Subject: [PATCH 1/2] feat(serve-static): mimes option for serve-static --- src/serve-static.ts | 8 +++++- test/assets/static/video/introduction.mp4 | 0 test/assets/static/video/morning-routine.m3u8 | 0 test/assets/static/video/morning-routine1.ts | 0 test/serve-static.test.ts | 27 +++++++++++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/assets/static/video/introduction.mp4 create mode 100644 test/assets/static/video/morning-routine.m3u8 create mode 100644 test/assets/static/video/morning-routine1.ts 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.ts b/test/assets/static/video/morning-routine1.ts new file mode 100644 index 0000000..e69de29 diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index b26a6e8..d63a8bd 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.ts') + 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') + }) +}) From c49deb4816feba6dda3a287143855b9a97ab32a6 Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 1 Feb 2024 01:54:42 +0900 Subject: [PATCH 2/2] fix test --- .../static/video/{morning-routine1.ts => morning-routine1.ts1} | 0 test/serve-static.test.ts | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename test/assets/static/video/{morning-routine1.ts => morning-routine1.ts1} (100%) diff --git a/test/assets/static/video/morning-routine1.ts b/test/assets/static/video/morning-routine1.ts1 similarity index 100% rename from test/assets/static/video/morning-routine1.ts rename to test/assets/static/video/morning-routine1.ts1 diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index d63a8bd..1e58b3a 100644 --- a/test/serve-static.test.ts +++ b/test/serve-static.test.ts @@ -152,7 +152,7 @@ describe('With `mimes` options', () => { 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.ts') + 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') })