Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,16 @@ res.send = function send(body) {
case 'boolean':
case 'number':
case 'object':

if (chunk === null) {
chunk = '';
} else if (ArrayBuffer.isView(chunk)) {
} else if (ArrayBuffer.isView(chunk) || chunk instanceof ArrayBuffer) {
if (!this.get('Content-Type')) {
this.type('bin');
}
if (chunk instanceof ArrayBuffer) {
chunk = Buffer.from(chunk);
}
} else {
return this.json(chunk);
}
Expand All @@ -166,9 +170,13 @@ res.send = function send(body) {
// Content-Length should be added only if there is no Transfer-Encoding header
var len;
if (chunk !== undefined && !this.get('Transfer-Encoding')) {

if (Buffer.isBuffer(chunk)) {
// get length of Buffer
len = chunk.length
} else if (ArrayBuffer.isView(chunk)) {
// get length of ArrayBuffer
len = chunk.byteLength
Comment on lines +177 to +179

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep normalizing typed-array views before ending the response

This branch skips the existing Buffer.from(chunk, encoding) conversion for every view, not just the newly supported raw ArrayBuffer. With the default app settings, res.send(new Uint16Array([0x0102, 0x0304])) previously returned 200 with bytes [2, 4]; it now reaches ServerResponse.end() with a Uint16Array and throws ERR_INVALID_ARG_TYPE, producing a 500. Node's response writer accepts Uint8Array/Buffer, but not Uint16Array or Int16Array.

Please preserve the view-to-Buffer conversion before calculating the final length and sending. This also prevents HEAD from advertising Content-Length: 4 for the previously two-byte representation. The current Uint8Array test does not cover this regression.

Automated review. Reproduced through real loopback HTTP on Node 24.18.0 against base a371447 and head 542756f, with Buffer/Uint8Array controls.

} else if (!generateETag && chunk.length < 1000) {
// just calculate length when no ETag + small chunk
len = Buffer.byteLength(chunk, encoding)
Expand Down
26 changes: 17 additions & 9 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,9 @@ describe('res', function(){
describe('.send(Buffer)', function(){
it('should send as octet-stream', function(done){
var app = express();

app.use(function(req, res){
res.send(Buffer.from('hello'))
});

request(app)
.get('/')
.expect(200)
Expand All @@ -180,11 +178,9 @@ describe('res', function(){

it('should set ETag', function (done) {
var app = express();

app.use(function (req, res) {
res.send(Buffer.alloc(999, '-'))
});

request(app)
.get('/')
.expect('ETag', 'W/"3e7-qPnkJ3CVdVhFJQvUBfF10TmVA7g"')
Expand All @@ -193,11 +189,9 @@ describe('res', function(){

it('should not override Content-Type', function(done){
var app = express();

app.use(function(req, res){
res.set('Content-Type', 'text/plain').send(Buffer.from('hey'))
});

request(app)
.get('/')
.expect('Content-Type', 'text/plain; charset=utf-8')
Expand All @@ -210,20 +204,34 @@ describe('res', function(){
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 handle a raw ArrayBuffer as binary data, not JSON', function(done){
var app = express();
app.use(function(req, res){
const arrayBuffer = new ArrayBuffer(3);
const view = new Uint8Array(arrayBuffer);
view.set([10, 20, 30]);
res.send(arrayBuffer);
});
request(app)
.get('/')
.expect(200)
.expect('Content-Type', 'application/octet-stream')
.expect(utils.shouldHaveBody(Buffer.from([10, 20, 30])))
.expect('Content-Length', "3")
.end(done);
})

it('should not override ETag', function (done) {
var app = express()

app.use(function (req, res) {
res.type('text/plain').set('ETag', '"foo"').send(Buffer.from('hey'))
})

request(app)
.get('/')
.expect('ETag', '"foo"')
Expand Down