From 7196c96f8e49b2229e86e24251f6c69d3ceca858 Mon Sep 17 00:00:00 2001 From: oon00b <60419766+oon00b@users.noreply.github.com> Date: Sun, 7 Jul 2024 20:04:20 +0900 Subject: [PATCH] support serving file from absolute path for Deno and Bun --- runtime_tests/bun/abs-static/plain.txt | 1 + runtime_tests/bun/index.test.tsx | 12 ++++++++++++ runtime_tests/deno/abs-static/plain.txt | 1 + runtime_tests/deno/middleware.test.tsx | 11 +++++++++++ src/adapter/bun/serve-static.ts | 5 ++--- src/adapter/deno/serve-static.ts | 4 ++-- src/middleware/serve-static/index.ts | 6 ++++-- 7 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 runtime_tests/bun/abs-static/plain.txt create mode 100644 runtime_tests/deno/abs-static/plain.txt diff --git a/runtime_tests/bun/abs-static/plain.txt b/runtime_tests/bun/abs-static/plain.txt new file mode 100644 index 0000000000..4d2a664601 --- /dev/null +++ b/runtime_tests/bun/abs-static/plain.txt @@ -0,0 +1 @@ +Bun!!! \ No newline at end of file diff --git a/runtime_tests/bun/index.test.tsx b/runtime_tests/bun/index.test.tsx index 9aaaf26d09..d93d642640 100644 --- a/runtime_tests/bun/index.test.tsx +++ b/runtime_tests/bun/index.test.tsx @@ -106,6 +106,12 @@ describe('Serve Static Middleware', () => { rewriteRequestPath: (path) => path.replace(/^\/dot-static/, './.static'), }) ) + app.get( + '/abs-static/*', + serveStatic({ + root: `${process.cwd()}/runtime_tests/bun/`, + }) + ) beforeEach(() => onNotFound.mockClear()) @@ -145,6 +151,12 @@ describe('Serve Static Middleware', () => { expect(res.status).toBe(200) expect(await res.text()).toBe('Bun!!') }) + + it('Should return 200 response - /abs-static/plain.txt', async () => { + const res = await app.request(new Request('http://localhost/abs-static/plain.txt')) + expect(res.status).toBe(200) + expect(await res.text()).toBe('Bun!!!') + }) }) // Bun support WebCrypto since v0.2.2 diff --git a/runtime_tests/deno/abs-static/plain.txt b/runtime_tests/deno/abs-static/plain.txt new file mode 100644 index 0000000000..11ed6e1b70 --- /dev/null +++ b/runtime_tests/deno/abs-static/plain.txt @@ -0,0 +1 @@ +Deno!!! \ No newline at end of file diff --git a/runtime_tests/deno/middleware.test.tsx b/runtime_tests/deno/middleware.test.tsx index 534160c540..9ed8165154 100644 --- a/runtime_tests/deno/middleware.test.tsx +++ b/runtime_tests/deno/middleware.test.tsx @@ -93,6 +93,13 @@ Deno.test('Serve Static middleware', async () => { }) ) + app.get( + '/abs-static/*', + serveStatic({ + root: await Deno.realPath('./runtime_tests/deno'), + }) + ) + let res = await app.request('http://localhost/favicon.ico') assertEquals(res.status, 200) assertEquals(res.headers.get('Content-Type'), 'image/x-icon') @@ -117,6 +124,10 @@ Deno.test('Serve Static middleware', async () => { assertEquals(await res.text(), 'Deno!!') assertSpyCalls(onNotFound, 1) + res = await app.request('http://localhost/abs-static/plain.txt') + assertEquals(res.status, 200) + assertEquals(await res.text(), 'Deno!!!') + res = await app.fetch({ method: 'GET', url: 'http://localhost/static/%2e%2e/static/plain.txt', diff --git a/src/adapter/bun/serve-static.ts b/src/adapter/bun/serve-static.ts index 768dffef8b..7e3a27b43b 100644 --- a/src/adapter/bun/serve-static.ts +++ b/src/adapter/bun/serve-static.ts @@ -8,13 +8,12 @@ export const serveStatic = ( ): MiddlewareHandler => { return async function serveStatic(c, next) { const getContent = async (path: string) => { - path = `./${path}` // @ts-ignore const file = Bun.file(path) return (await file.exists()) ? file : null } - const pathResolve = (path: string) => { - return `./${path}` + const pathResolve = (path: string, isAbsolutePath?: boolean) => { + return isAbsolutePath ? `/${path}` : `./${path}` } return baseServeStatic({ ...options, diff --git a/src/adapter/deno/serve-static.ts b/src/adapter/deno/serve-static.ts index b037466b66..60af71fd84 100644 --- a/src/adapter/deno/serve-static.ts +++ b/src/adapter/deno/serve-static.ts @@ -19,8 +19,8 @@ export const serveStatic = ( console.warn(`${e}`) } } - const pathResolve = (path: string) => { - return `./${path}` + const pathResolve = (path: string, isAbsolutePath?: boolean) => { + return isAbsolutePath ? `/${path}` : `./${path}` } return baseServeStatic({ ...options, diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 134f4c4448..708474d98a 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -25,7 +25,7 @@ const defaultPathResolve = (path: string) => path export const serveStatic = ( options: ServeStaticOptions & { getContent: (path: string, c: Context) => Promise - pathResolve?: (path: string) => string + pathResolve?: (path: string, isAbsolutePath?: boolean) => string } ): MiddlewareHandler => { return async (c, next) => { @@ -50,7 +50,9 @@ export const serveStatic = ( } const getContent = options.getContent - const pathResolve = options.pathResolve ?? defaultPathResolve + const pathResolve = (p: string) => { + return (options.pathResolve ?? defaultPathResolve)(p, root?.startsWith('/')) + } path = pathResolve(path) let content = await getContent(path, c)