fix(serve-static):supports directory contains `.` and not ends `/` (#3256)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent ac069d1 · Browse files · 10 files·+73 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+73 added−1 removed
- tests:+37 added−0 removed
- other:+36 added−1 removed
Provenance
- Commit
7a254bc- Tree
70dfece- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
ac069d1
10 files changed+73 added−1 removed
added:runtime_tests/bun/static/hello.world/index.html+1 added
1Hiadded:runtime_tests/bun/static/helloworld/index.html+1 added
1Himodified:runtime_tests/bun/index.test.tsx+12 added
143 unmodified lines144 const res = await app.request(new Request('http://localhost/dot-static/plain.txt'))145 expect(res.status).toBe(200)146 expect(await res.text()).toBe('Bun!!')147 })148+149+ it('Should return 200 response - /static/helloworld', async () => {150+ const res = await app.request('http://localhost/static/helloworld')151+ expect(res.status).toBe(200)152+ expect(await res.text()).toBe('Hi\n')153+ })154+155+ it('Should return 200 response - /static/hello.world', async () => {156+ const res = await app.request('http://localhost/static/hello.world')157+ expect(res.status).toBe(200)158+ expect(await res.text()).toBe('Hi\n')159+ })160 })161 162 // Bun support WebCrypto since v0.2.2163 // So, JWT middleware works well.269 unmodified linesadded:runtime_tests/deno/static/hello.world/index.html+1 added
1Hiadded:runtime_tests/deno/static/helloworld/index.html+1 added
1Himodified:runtime_tests/deno/middleware.test.tsx+8 added
121 unmodified linesExpand all12212312412512612712812913013113213313413513613725 unmodified lines121 unmodified linesExpand all url: 'http://localhost/static/%2e%2e/static/plain.txt', } as Request) assertEquals(res.status, 404) assertEquals(await res.text(), '404 Not Found')
res = await app.request('http://localhost/static/helloworld') assertEquals(res.status, 200) assertEquals(await res.text(), 'Hi\n')
res = await app.request('http://localhost/static/hello.world') assertEquals(res.status, 200) assertEquals(await res.text(), 'Hi\n')})
Deno.test('JWT Authentication middleware', async () => { const app = new Hono<{ Variables: { 'x-foo': string } }>()25 unmodified linesmodified:src/adapter/bun/serve-static.ts+10 added
1234567896 unmodified lines1617181920212223242526272829303132333435/* eslint-disable @typescript-eslint/ban-ts-comment */import { serveStatic as baseServeStatic } from '../../middleware/serve-static'import type { ServeStaticOptions } from '../../middleware/serve-static'import type { Env, MiddlewareHandler } from '../../types'import { stat } from 'node:fs/promises'
export const serveStatic = <E extends Env = Env>( options: ServeStaticOptions<E>): MiddlewareHandler => {6 unmodified lines } const pathResolve = (path: string) => { return `./${path}` } const isDir = async (path: string) => { let isDir try { const stats = await stat(path) isDir = stats.isDirectory() } catch {} return isDir } return baseServeStatic({ ...options, getContent, pathResolve, isDir, })(c, next) }}modified:src/adapter/deno/serve-static.ts+10 added-1 removed
2 unmodified lines3 import type { Env, MiddlewareHandler } from '../../types'4 5 // eslint-disable-next-line @typescript-eslint/ban-ts-comment6 // @ts-ignore7-const { open } = Deno7+const { open, lstatSync } = Deno8 9 export const serveStatic = <E extends Env = Env>(10 options: ServeStaticOptions<E>11 ): MiddlewareHandler => {9 unmodified lines21 }22 const pathResolve = (path: string) => {23 return `./${path}`24 }25+ const isDir = (path: string) => {26+ let isDir27+ try {28+ const stat = lstatSync(path)29+ isDir = stat.isDirectory30+ } catch {}31+ return isDir32+ }33 return baseServeStatic({34 ...options,35 getContent,36 pathResolve,37+ isDir,38 })(c, next)39 }40 }modified:src/middleware/serve-static/index.test.ts+17 added
13 unmodified lines14 getContent,15 pathResolve: (path) => {16 return `./${path}`17 },18+ isDir: (path) => {19+ return path === 'static/hello.world'20+ },21 })22 23 app.get('/static/*', serveStatic)24 14 unmodified lines39 expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)40 expect(await res.text()).toBe('Hello in ./static/sub/index.html')41 })42 43+ it('Should return 200 response - /static/helloworld', async () => {44+ const res = await app.request('/static/helloworld')45+ expect(res.status).toBe(200)46+ expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)47+ expect(await res.text()).toBe('Hello in ./static/helloworld/index.html')48+ })49+50+ it('Should return 200 response - /static/hello.world', async () => {51+ const res = await app.request('/static/hello.world')52+ expect(res.status).toBe(200)53+ expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)54+ expect(await res.text()).toBe('Hello in ./static/hello.world/index.html')55+ })56+57 it('Should decode URI strings - /static/%E7%82%8E.txt', async () => {58 const res = await app.request('/static/%E7%82%8E.txt')59 expect(res.status).toBe(200)60 expect(await res.text()).toBe('Hello in ./static/炎.txt')38 unmodified linesmodified:src/middleware/serve-static/index.ts+12 added
24 unmodified lines25 export const serveStatic = <E extends Env = Env>(26 options: ServeStaticOptions<E> & {27 getContent: (path: string, c: Context<E>) => Promise<Data | Response | null>28 pathResolve?: (path: string) => string29+ isDir?: (path: string) => boolean | undefined | Promise<boolean | undefined>30 }31 ): MiddlewareHandler => {32 return async (c, next) => {33 // Do nothing if Response is already set5 unmodified lines39 let filename = options.path ?? decodeURI(c.req.path)40 filename = options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename41 const root = options.root42 43+ // If it was Directory, force `/` on the end.44+ if (!filename.endsWith('/') && options.isDir) {45+ const path = getFilePathWithoutDefaultDocument({46+ filename,47+ root,48+ })49+ if (path && (await options.isDir(path))) {50+ filename = filename + '/'51+ }52+ }53+54 let path = getFilePath({55 filename,56 root,57 defaultDocument: DEFAULT_DOCUMENT,52 unmodified lines