connect-settimeout
Advanced tools
Comparing version 0.0.0 to 0.0.1
module.exports = function(cb, duration, options){ | ||
options = options || {}; | ||
options.timeoutName = 'timeoutCheck' || options.timeoutName; | ||
options.timeoutName = options.timeoutName || 'timeoutCheck'; | ||
var timeoutName = options.timeoutName; | ||
return function(req, res, next){ | ||
res[timeoutName] = setTimeout(function(){ | ||
res.connectSetTimeouts = res.connectSetTimeouts || {}; | ||
res.connectSetTimeouts[timeoutName] = setTimeout(function(){ | ||
return cb(req, res); | ||
}, duration); | ||
res.on('finish', function(evt){ | ||
clearTimeout(res[timeoutName]); | ||
clearTimeout(res.connectSetTimeouts[timeoutName]); | ||
}); | ||
@@ -13,0 +14,0 @@ next(); |
{ | ||
"name": "connect-settimeout", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "a connect middleware that runs a provided function if a request lasts longer than a given duration", | ||
@@ -26,3 +26,3 @@ "main": "index.js", | ||
}, | ||
"devDependencies" : { | ||
"devDependencies": { | ||
"mocha": "1.15.1", | ||
@@ -36,5 +36,8 @@ "connect": "2.11.2", | ||
"mocha-lcov-reporter": "0.0.1", | ||
"underscore": "1.6.0", | ||
"jshint": "2.3.0" | ||
}, | ||
"homepage": "https://github.com/cainus/connect-settimeout" | ||
"homepage": "https://github.com/cainus/connect-settimeout", | ||
"dependencies": { | ||
} | ||
} |
connect-settimeout | ||
================== | ||
a connect middleware that runs a provided function if a request last longer than a given duration | ||
a connect middleware that runs a provided function if a request lasts longer than a given duration | ||
@@ -24,1 +24,2 @@ | ||
@@ -6,2 +6,3 @@ var http = require('http'); | ||
var should = require('should'); | ||
var _ = require('underscore'); | ||
var connectSetTimeout = require('../index'); | ||
@@ -23,3 +24,2 @@ var server; | ||
server = http.createServer(function (req, res) { | ||
console.log("in the server"); | ||
var middleware = connectSetTimeout(function(req, res){ | ||
@@ -29,3 +29,2 @@ slowFlag = true; | ||
middleware(req, res, function(){ | ||
console.log("in the next"); | ||
if (/slow/.test(req.url)){ | ||
@@ -56,2 +55,33 @@ return setTimeout(function(){ | ||
}); | ||
describe("with a different timeout name", function(){ | ||
afterEach(function(done){ | ||
server.close(function(){ | ||
done(); | ||
}); | ||
}); | ||
it("uses that timeout name", function(done){ | ||
this.timeout(10000); | ||
server = http.createServer(function (req, res) { | ||
var middleware = connectSetTimeout(function(req, res){ | ||
slowFlag = true; | ||
}, 2000, {timeoutName : 'blah'}); | ||
middleware(req, res, function(){ | ||
if (/slow/.test(req.url)){ | ||
return setTimeout(function(){ | ||
_.keys(res.connectSetTimeouts).should.eql(['blah']); | ||
res.end("slow"); | ||
}, 5000); | ||
} | ||
}); | ||
}).listen(port, function(err){ | ||
if (err) throw err; | ||
request(baseUrl + '/slow', function(err, res, body){ | ||
slowFlag.should.equal(true); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
7014
95
25
10