diff --git a/src/serve-static.ts b/src/serve-static.ts index c1eb50a..87d4fae 100644 --- a/src/serve-static.ts +++ b/src/serve-static.ts @@ -12,7 +12,7 @@ export type ServeStaticOptions = { path?: string index?: string // default is 'index.html' precompressed?: boolean - rewriteRequestPath?: (path: string) => string + rewriteRequestPath?: (path: string, c: Context) => string onFound?: (path: string, c: Context) => void | Promise onNotFound?: (path: string, c: Context) => void | Promise } @@ -56,7 +56,10 @@ const getStats = (path: string) => { return stats } -export const serveStatic = (options: ServeStaticOptions = { root: '' }): MiddlewareHandler => { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export const serveStatic = ( + options: ServeStaticOptions = { root: '' } +): MiddlewareHandler => { return async (c, next) => { // Do nothing if Response is already set if (c.finalized) { @@ -73,7 +76,7 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew } let path = getFilePathWithoutDefaultDocument({ - filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename, + filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename, root: options.root, }) @@ -87,7 +90,7 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew if (stats && stats.isDirectory()) { path = getFilePath({ - filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename, + filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename, root: options.root, defaultDocument: options.index ?? 'index.html', }) diff --git a/test/assets/static-with-context-path/plain.txt b/test/assets/static-with-context-path/plain.txt new file mode 100644 index 0000000..fd3af60 --- /dev/null +++ b/test/assets/static-with-context-path/plain.txt @@ -0,0 +1 @@ +This is plain.txt \ No newline at end of file diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index aedf691..6ba4f07 100644 --- a/test/serve-static.test.ts +++ b/test/serve-static.test.ts @@ -1,10 +1,15 @@ import { Hono } from 'hono' + import request from 'supertest' import { serveStatic } from './../src/serve-static' import { createAdaptorServer } from './../src/server' describe('Serve Static Middleware', () => { - const app = new Hono() + const app = new Hono<{ + Variables: { + path: string + } + }>() app.use( '/static/*', @@ -24,6 +29,19 @@ describe('Serve Static Middleware', () => { }) ) + app.use( + '/static-with-context-path-route/*', + async (c, next) => { + c.set('path', '/static-with-context-path') + await next() + }, + serveStatic({ + root: './test/assets', + rewriteRequestPath: (path, c) => + path.replace('static-with-context-path-route', c.get('path')), + }) + ) + let notFoundMessage = '' app.use( '/on-not-found/*', @@ -103,6 +121,13 @@ describe('Serve Static Middleware', () => { expect(res.status).toBe(404) }) + it('Should return 200 with rewriteRequestPath with the context', async () => { + const res = await request(server).get('/static-with-context-path-route/plain.txt') + expect(res.status).toBe(200) + expect(res.headers['content-type']).toBe('text/plain; charset=utf-8') + expect(res.text).toBe('This is plain.txt') + }) + it('Should return 200 response to HEAD request', async () => { const res = await request(server).head('/static/plain.txt') expect(res.status).toBe(200)