Skip to content

Commit

Permalink
perf: re-use options object when generating ETags
Browse files Browse the repository at this point in the history
closes #3313
closes #3314
  • Loading branch information
lpage authored and dougwilson committed Sep 28, 2017
1 parent 02a9d5f commit d9d09b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ unreleased
- Fix parsing & compacting very deep objects
* deps: setprototypeof@1.1.0
* deps: utils-merge@1.0.1
* perf: re-use options object when generating ETags

4.15.5 / 2017-09-24
===================
Expand Down
35 changes: 21 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ var querystring = require('querystring');
* @api private
*/

exports.etag = function (body, encoding) {
var buf = !Buffer.isBuffer(body)
? new Buffer(body, encoding)
: body;

return etag(buf, {weak: false});
};
exports.etag = createETagGenerator({ weak: false })

/**
* Return weak ETag for `body`.
Expand All @@ -48,13 +42,7 @@ exports.etag = function (body, encoding) {
* @api private
*/

exports.wetag = function wetag(body, encoding){
var buf = !Buffer.isBuffer(body)
? new Buffer(body, encoding)
: body;

return etag(buf, {weak: true});
};
exports.wetag = createETagGenerator({ weak: true })

/**
* Check if `path` looks absolute.
Expand Down Expand Up @@ -273,6 +261,25 @@ exports.setCharset = function setCharset(type, charset) {
return contentType.format(parsed);
};

/**
* Create an ETag generator function, generating ETags with
* the given options.
*
* @param {object} options
* @return {function}
* @private
*/

function createETagGenerator (options) {
return function generateETag (body, encoding) {
var buf = !Buffer.isBuffer(body)
? new Buffer(body, encoding)
: body

return etag(buf, options)
}
}

/**
* Parse an extended query string with qs.
*
Expand Down

0 comments on commit d9d09b8

Please sign in to comment.