Skip to content
Myr

Skip Buffer encoding when not generating ETag for small response

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 80f1ea9 · Browse files · 2 files+17 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+17 added−7 removed

  • other:+17 added−7 removed

2 files+17 added−7 removed

modified:lib/response.js+16 added-7 removed
104 unmodified lines
105106res.send = function send(body) {107 var chunk = body;108 var encoding;109 var len;109 var req = this.req;110 var type;111112 // settings
56 unmodified lines
169 this.set('Content-Type', setCharset(type, 'utf-8'));170 }171 }172173 // determine if ETag should be generated174 var etagFn = app.get('etag fn')175 var generateETag = !this.get('ETag') && typeof etagFn === 'function'176177 // populate Content-Length178 var len179 if (chunk !== undefined) {180 if (!generateETag && chunk.length < 1000) {181 // just calculate length when no ETag + small chunk182 len = Buffer.byteLength(chunk, encoding)176 if (!Buffer.isBuffer(chunk)) {177 // convert chunk to Buffer; saves later double conversions183 } else if (!Buffer.isBuffer(chunk)) {184 // convert chunk to Buffer and calculate185 chunk = new Buffer(chunk, encoding);186 encoding = undefined;187 len = chunk.length188 } else {189 // get length of Buffer190 len = chunk.length191 }192182 len = chunk.length;193 this.set('Content-Length', len);194 }195196 // populate ETag197 var etag;188 var generateETag = len !== undefined && app.get('etag fn');189 if (typeof generateETag === 'function' && !this.get('ETag')) {190 if ((etag = generateETag(chunk, encoding))) {198 if (generateETag && len !== undefined) {199 if ((etag = etagFn(chunk, encoding))) {200 this.set('ETag', etag);201 }202 }203
882 unmodified lines
modified:History.md+1 added
1unreleased2==========34 * Improve error message when autoloading invalid view engine5 * Skip `Buffer` encoding when not generating ETag for small response674.15.5 / 2017-09-248===================9
3308 unmodified lines