diff --git a/src/serve-static.ts b/src/serve-static.ts index a2c2dd0..5de5639 100644 --- a/src/serve-static.ts +++ b/src/serve-static.ts @@ -149,8 +149,8 @@ export const serveStatic = ( 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 } diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index af93429..76e72e0 100644 --- a/test/serve-static.test.ts +++ b/test/serve-static.test.ts @@ -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)