Skip to content
Myr

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

10 files+73 added−1 removed

added:runtime_tests/bun/static/hello.world/index.html+1 added
added:runtime_tests/bun/static/helloworld/index.html+1 added
modified:runtime_tests/bun/index.test.tsx+12 added
143 unmodified lines
144 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 })148149 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 })154155 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})161162// Bun support WebCrypto since v0.2.2163// So, JWT middleware works well.
269 unmodified lines
added:runtime_tests/deno/static/hello.world/index.html+1 added
added:runtime_tests/deno/static/helloworld/index.html+1 added
modified:runtime_tests/deno/middleware.test.tsx+8 added
modified:src/adapter/bun/serve-static.ts+10 added
modified:src/adapter/deno/serve-static.ts+10 added-1 removed
2 unmodified lines
3import type { Env, MiddlewareHandler } from '../../types'45// eslint-disable-next-line @typescript-eslint/ban-ts-comment6// @ts-ignore7const { open } = Deno7const { open, lstatSync } = Deno89export const serveStatic = <E extends Env = Env>(10 options: ServeStaticOptions<E>11): MiddlewareHandler => {
9 unmodified lines
21 }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 lines
14 getContent,15 pathResolve: (path) => {16 return `./${path}`17 },18 isDir: (path) => {19 return path === 'static/hello.world'20 },21 })2223 app.get('/static/*', serveStatic)24
14 unmodified lines
39 expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)40 expect(await res.text()).toBe('Hello in ./static/sub/index.html')41 })4243 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 })4950 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 })5657 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 lines
modified:src/middleware/serve-static/index.ts+12 added
24 unmodified lines
25export 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 set
5 unmodified lines
39 let filename = options.path ?? decodeURI(c.req.path)40 filename = options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename41 const root = options.root4243 // 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 }5354 let path = getFilePath({55 filename,56 root,57 defaultDocument: DEFAULT_DOCUMENT,
52 unmodified lines