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
} 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