Skip to content

Commit 55869f4

Browse files
alexanderceruttiwesleytodd
authored andcommitted
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 <[email protected]> Added Uint8Array test with encoding fix: added history.md entry
1 parent af7cd90 commit 55869f4

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ unreleased
99
* Replace `methods` dependency with standard library
1010
* refactor: prefix built-in node module imports
1111
* Remove unused `depd` dependency
12+
* Add support for `Uint8Array` in `res.send`
1213

1314
5.0.1 / 2024-10-08
1415
==========

lib/response.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ res.send = function send(body) {
130130
case 'object':
131131
if (chunk === null) {
132132
chunk = '';
133-
} else if (Buffer.isBuffer(chunk)) {
133+
} else if (ArrayBuffer.isView(chunk)) {
134134
if (!this.get('Content-Type')) {
135135
this.type('bin');
136136
}

test/res.send.js

+13
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ describe('res', function(){
177177
.expect(200, 'hey', done);
178178
})
179179

180+
it('should accept Uint8Array', function(done){
181+
var app = express();
182+
app.use(function(req, res){
183+
const encodedHey = new TextEncoder().encode("hey");
184+
res.set("Content-Type", "text/plain").send(encodedHey);
185+
})
186+
187+
request(app)
188+
.get("/")
189+
.expect("Content-Type", "text/plain; charset=utf-8")
190+
.expect(200, "hey", done);
191+
})
192+
180193
it('should not override ETag', function (done) {
181194
var app = express()
182195

0 commit comments

Comments
 (0)