Skip to content

Commit

Permalink
Fix TypeError in res.send when given Buffer and ETag header set
Browse files Browse the repository at this point in the history
fixes #3445
  • Loading branch information
davidwood authored and dougwilson committed Oct 10, 2017
1 parent 48aba21 commit b7817ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Fix `TypeError` in `res.send` when given `Buffer` and `ETag` header set

4.16.1 / 2017-09-29
===================

Expand Down
10 changes: 5 additions & 5 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@ res.send = function send(body) {
// populate Content-Length
var len
if (chunk !== undefined) {
if (!generateETag && chunk.length < 1000) {
if (Buffer.isBuffer(chunk)) {
// get length of Buffer
len = chunk.length
} else if (!generateETag && chunk.length < 1000) {
// just calculate length when no ETag + small chunk
len = Buffer.byteLength(chunk, encoding)
} else if (!Buffer.isBuffer(chunk)) {
} else {
// convert chunk to Buffer and calculate
chunk = Buffer.from(chunk, encoding)
encoding = undefined;
len = chunk.length
} else {
// get length of Buffer
len = chunk.length
}

this.set('Content-Length', len);
Expand Down
13 changes: 13 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ describe('res', function(){
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, 'hey', 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"')
.expect(200, 'hey', done)
})
})

describe('.send(Object)', function(){
Expand Down

0 comments on commit b7817ab

Please sign in to comment.