From d69a02486d1f714fa6cf78d55f38a26ae35a5d2a Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 2 Jul 2026 00:46:56 +0100 Subject: [PATCH] fix(response): keep the Blob content type instead of forcing octet-stream Assigning a Blob to ctx.body forced Content-Type: application/octet-stream and ignored the Blob's own type. A typed Blob such as new Blob(data, { type: 'image/png' }) was sent with the wrong content type, so browsers download the payload instead of rendering it. The Response body path already copies the body's own Content-Type, and the original feature request (#1777) specified ctx.type = blob.type. Use val.type when present and fall back to application/octet-stream for an untyped Blob. An explicitly preset Content-Type still wins. --- __tests__/response/body.test.js | 13 +++++++++++++ lib/response.js | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/__tests__/response/body.test.js b/__tests__/response/body.test.js index c63cb4d75..239a77689 100644 --- a/__tests__/response/body.test.js +++ b/__tests__/response/body.test.js @@ -302,6 +302,19 @@ describe('res.body=', () => { res.body = new Blob([new Uint8Array([1, 2, 3])], { type: 'application/octet-stream' }) assert.strictEqual(3, res.header['content-length']) }) + + it('should keep the Blob content type', () => { + const res = response() + res.body = new Blob([new Uint8Array([1, 2, 3])], { type: 'image/png' }) + assert.strictEqual('image/png', res.header['content-type']) + }) + + it('should not override an explicit content type', () => { + const res = response() + res.type = 'application/json' + res.body = new Blob(['x'], { type: 'image/png' }) + assert.strictEqual('application/json; charset=utf-8', res.header['content-type']) + }) }) describe('when a response is given', () => { diff --git a/lib/response.js b/lib/response.js index 9b6741476..275706e46 100644 --- a/lib/response.js +++ b/lib/response.js @@ -207,7 +207,7 @@ module.exports = { // blob if (val instanceof Blob) { - if (setType) this.type = 'bin' + if (setType) this.type = val.type || 'bin' this.length = val.size cleanupPreviousStream() return