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
3 changes: 3 additions & 0 deletions src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const createStreamBody = (stream: ReadStream) => {
stream.on('data', (chunk) => {
controller.enqueue(chunk)
})
stream.on('error', (err) => {
controller.error(err)
})
stream.on('end', () => {
controller.close()
})
Expand Down
29 changes: 29 additions & 0 deletions test/serve-static.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Hono } from 'hono'
import request from 'supertest'
import { chmodSync, statSync } from 'node:fs'
import path from 'node:path'
import { serveStatic } from './../src/serve-static'
import { createAdaptorServer } from './../src/server'
Expand Down Expand Up @@ -331,6 +332,34 @@ describe('Serve Static Middleware', () => {
expect(res.status).toBe(200)
})
})

describe('Stream error handling', () => {
const testFile = path.join(__dirname, 'assets', 'static', 'plain.txt')
console.log(testFile)
let originalMode: number

beforeEach(() => {
const stats = statSync(testFile)
originalMode = stats.mode
// Remove read permission to trigger stream error
chmodSync(testFile, 0o000)
})

afterEach(() => {
chmodSync(testFile, originalMode)
})

// Skip on Windows as chmod doesn't work for file permissions
;(process.platform === 'win32' ? it.skip : it)(
'Should handle read permission errors gracefully',
async () => {
const app = new Hono()
app.use('/static/*', serveStatic({ root: './test/assets' }))
const server = createAdaptorServer(app)
await expect(request(server).get('/static/plain.txt')).rejects.toThrow()
}
)
})
})

describe('Serve Static Middleware with wrong path', () => {
Expand Down
Loading