Skip to content

Commit

Permalink
feat: Added check to support Uint8Array in response sending (#6285)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexandercerutti authored and wesleytodd committed Feb 10, 2025
1 parent af7cd90 commit 55869f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
==========
Expand Down
2 changes: 1 addition & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
13 changes: 13 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 55869f4

Please sign in to comment.