diff --git a/History.md b/History.md index 2859c77c74..f51da8dfb3 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +unreleased +========== + + * Fix `TypeError` in `res.send` when given `Buffer` and `ETag` header set + 4.16.1 / 2017-09-29 =================== diff --git a/lib/response.js b/lib/response.js index 832044be9a..9c1796d37b 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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); diff --git a/test/res.send.js b/test/res.send.js index 7aa8d7d90e..2410554248 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -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(){