Skip to content

Commit

Permalink
Skip Buffer encoding when not generating ETag for small response
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 26, 2017
1 parent ce1f847 commit 2cc60e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Improve error message when autoloading invalid view engine
* Skip `Buffer` encoding when not generating ETag for small response

4.15.5 / 2017-09-24
===================
Expand Down
23 changes: 16 additions & 7 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ res.links = function(links){
res.send = function send(body) {
var chunk = body;
var encoding;
var len;
var req = this.req;
var type;

Expand Down Expand Up @@ -171,23 +170,33 @@ res.send = function send(body) {
}
}

// determine if ETag should be generated
var etagFn = app.get('etag fn')
var generateETag = !this.get('ETag') && typeof etagFn === 'function'

// populate Content-Length
var len
if (chunk !== undefined) {
if (!Buffer.isBuffer(chunk)) {
// convert chunk to Buffer; saves later double conversions
if (!generateETag && chunk.length < 1000) {
// just calculate length when no ETag + small chunk
len = Buffer.byteLength(chunk, encoding)
} else if (!Buffer.isBuffer(chunk)) {
// convert chunk to Buffer and calculate
chunk = new Buffer(chunk, encoding);
encoding = undefined;
len = chunk.length
} else {
// get length of Buffer
len = chunk.length
}

len = chunk.length;
this.set('Content-Length', len);
}

// populate ETag
var etag;
var generateETag = len !== undefined && app.get('etag fn');
if (typeof generateETag === 'function' && !this.get('ETag')) {
if ((etag = generateETag(chunk, encoding))) {
if (generateETag && len !== undefined) {
if ((etag = etagFn(chunk, encoding))) {
this.set('ETag', etag);
}
}
Expand Down

0 comments on commit 2cc60e9

Please sign in to comment.