Skip to content

Commit e4fcc8d

Browse files
committed
fix(response): set content-type when unable to stream file
Safari will force download the stream error in response when `X-Content-Type: nosniff` header is set. To avoid this, we need to set explicit content type header
1 parent b4bee00 commit e4fcc8d

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/Response/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export class Response extends Macroable implements ResponseContract {
291291
finished = true
292292
destroy(body)
293293

294+
this.type('text')
294295
if (typeof (errorCallback) === 'function') {
295296
this.endResponse(...errorCallback(error))
296297
} else {
@@ -385,6 +386,8 @@ export class Response extends Macroable implements ResponseContract {
385386
*/
386387
return this.streamBody(createReadStream(filePath), errorCallback)
387388
} catch (error) {
389+
this.type('text')
390+
388391
if (typeof (errorCallback) === 'function') {
389392
this.endResponse(...errorCallback(error))
390393
} else {

test/response.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,9 @@ test.group('Response', (group) => {
551551
response.finish()
552552
})
553553

554-
const { text } = await supertest(server).get('/').expect(404)
554+
const { text, header } = await supertest(server).get('/').expect(404)
555555
assert.equal(text, 'File not found')
556+
assert.equal(header['content-type'], 'text/plain; charset=utf-8')
556557
})
557558

558559
test('return custom message and status when file is missing', async (assert) => {

0 commit comments

Comments
 (0)