Skip to content

Commit

Permalink
perf: remove argument reassignment
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Aug 23, 2015
1 parent 3ddb6bd commit bf0741b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* perf: enable strict mode
* perf: remove argument reassignment

1.6.2 / 2015-05-11
==================
Expand Down
16 changes: 8 additions & 8 deletions index.js
Expand Up @@ -28,23 +28,23 @@ var onHeaders = require('on-headers');
*/

module.exports = function timeout(time, options) {
options = options || {};
var opts = options || {};

time = typeof time === 'string'
var delay = typeof time === 'string'
? ms(time)
: Number(time || 5000);

var respond = !('respond' in options) || options.respond === true;
var respond = !('respond' in opts) || opts.respond === true;

return function(req, res, next) {
var destroy = req.socket.destroy;
var id = setTimeout(function(){
req.timedout = true;
req.emit('timeout', time);
}, time);
req.emit('timeout', delay);
}, delay);

if (respond) {
req.on('timeout', onTimeout(time, next));
req.on('timeout', onTimeout(delay, next));
}

req.clearTimeout = function(){
Expand All @@ -66,11 +66,11 @@ module.exports = function timeout(time, options) {
};
};

function onTimeout(time, cb){
function onTimeout(delay, cb) {
return function(){
cb(createError(503, 'Response timeout', {
code: 'ETIMEDOUT',
timeout: time
timeout: delay
}));
};
}

0 comments on commit bf0741b

Please sign in to comment.