From 55869f49a65f1e279d92488fa6319c9fd4d8eac2 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sat, 18 Jan 2025 21:37:48 +0100 Subject: [PATCH] feat: Added check to support Uint8Array in response sending (#6285) Unified usage of ArrayBuffer.isView to comprehend Buffer and removed isView function check Co-authored-by: Wes Todd Added Uint8Array test with encoding fix: added history.md entry --- History.md | 1 + lib/response.js | 2 +- test/res.send.js | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index 25e29250f9..e08c52ee04 100644 --- a/History.md +++ b/History.md @@ -9,6 +9,7 @@ unreleased * Replace `methods` dependency with standard library * refactor: prefix built-in node module imports * Remove unused `depd` dependency +* Add support for `Uint8Array` in `res.send` 5.0.1 / 2024-10-08 ========== diff --git a/lib/response.js b/lib/response.js index f6f5740d2d..38f11e9237 100644 --- a/lib/response.js +++ b/lib/response.js @@ -130,7 +130,7 @@ res.send = function send(body) { case 'object': if (chunk === null) { chunk = ''; - } else if (Buffer.isBuffer(chunk)) { + } else if (ArrayBuffer.isView(chunk)) { if (!this.get('Content-Type')) { this.type('bin'); } diff --git a/test/res.send.js b/test/res.send.js index 860607c49b..78a69a5c66 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -177,6 +177,19 @@ describe('res', function(){ .expect(200, 'hey', done); }) + it('should accept Uint8Array', function(done){ + var app = express(); + app.use(function(req, res){ + const encodedHey = new TextEncoder().encode("hey"); + res.set("Content-Type", "text/plain").send(encodedHey); + }) + + request(app) + .get("/") + .expect("Content-Type", "text/plain; charset=utf-8") + .expect(200, "hey", done); + }) + it('should not override ETag', function (done) { var app = express()