Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime_tests/bun/abs-static/plain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bun!!!
12 changes: 12 additions & 0 deletions runtime_tests/bun/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions runtime_tests/deno/abs-static/plain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deno!!!
11 changes: 11 additions & 0 deletions runtime_tests/deno/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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',
Expand Down
5 changes: 2 additions & 3 deletions src/adapter/bun/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ export const serveStatic = <E extends Env = Env>(
): 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,
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/deno/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const serveStatic = <E extends Env = Env>(
console.warn(`${e}`)
}
}
const pathResolve = (path: string) => {
return `./${path}`
const pathResolve = (path: string, isAbsolutePath?: boolean) => {
return isAbsolutePath ? `/${path}` : `./${path}`
}
return baseServeStatic({
...options,
Expand Down
6 changes: 4 additions & 2 deletions src/middleware/serve-static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const defaultPathResolve = (path: string) => path
export const serveStatic = <E extends Env = Env>(
options: ServeStaticOptions<E> & {
getContent: (path: string, c: Context<E>) => Promise<Data | Response | null>
pathResolve?: (path: string) => string
pathResolve?: (path: string, isAbsolutePath?: boolean) => string
}
): MiddlewareHandler => {
return async (c, next) => {
Expand All @@ -50,7 +50,9 @@ export const serveStatic = <E extends Env = Env>(
}

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)
Expand Down