Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ export const serveStatic = <E extends Env = any>(
c.header('Date', stats.birthtime.toUTCString())

const parts = range.replace(/bytes=/, '').split('-', 2)
const start = parts[0] ? parseInt(parts[0], 10) : 0
let end = parts[1] ? parseInt(parts[1], 10) : stats.size - 1
const start = parseInt(parts[0], 10) || 0
let end = parseInt(parts[1], 10) || size - 1
if (size < end - start + 1) {
end = size - 1
}
Expand Down
7 changes: 7 additions & 0 deletions test/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ describe('Serve Static Middleware', () => {
expect(res.text).toBe('This is plain.txt')
})

it('Should handle invalid range header gracefully without NaN error', async () => {
const res = await request(server).get('/static/plain.txt').set('range', 'hello')
expect(res.status).toBe(206)
expect(res.headers['content-length']).toBe('17')
expect(res.headers['content-range']).toBe('bytes 0-16/17')
})

it('Should handle the `onNotFound` option', async () => {
const res = await request(server).get('/on-not-found/foo.txt')
expect(res.status).toBe(404)
Expand Down
Loading