Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,26 @@ var onHeaders = require('on-headers');
module.exports = function timeout(time, options) {
options = options || {};

time = typeof time === 'string'
? ms(time)
: Number(time || 5000);
if (typeof time !== 'function') { // allow computed
time = typeof time === 'string'
? ms(time)
: Number(time || 5000);
}

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

return function(req, res, next) {
var destroy = req.socket.destroy;

var computed_time = typeof time === 'function' ? time(req) : time;

var id = setTimeout(function(){
req.timedout = true;
req.emit('timeout', time);
}, time);
req.emit('timeout', computed_time);
}, computed_time);

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

req.clearTimeout = function(){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "connect-timeout",
"description": "timeout middleware",
"version": "1.4.0",
"version": "1.5.0",
"contributors": [
"Douglas Christopher Wilson <doug@somethingdoug.com>",
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
Expand Down
35 changes: 34 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ describe('timeout()', function(){
.expect(503, /45ms/, done)
})

it('should accept function timeout', function (done) {
var server = createServer(function(req) {
return req.url === '/' ? 45 : 100
})
request(server)
.get('/')
.expect(503, /45ms/, done)
})

describe('when below the timeout', function(){
it('should do nothing', function(done){
var server = createServer(null, function(req, res){
Expand All @@ -36,6 +45,17 @@ describe('timeout()', function(){
.get('/')
.expect(200, 'Hello', done)
})

it('should do nothing with computed time', function(done){
var server = createServer(function(req) {
return req.url === '/' ? 255 : 100
}, function(req, res){
res.end('Hello')
})
request(server)
.get('/')
.expect(200, 'Hello', done)
})
})

describe('when above the timeout', function(){
Expand All @@ -51,6 +71,19 @@ describe('timeout()', function(){
.expect(503, done)
})

it('should respond with 503 Request timeout with computed time', function(done){
var server = createServer(function(req) {
return req.url === '/' ? 255 : 100
}, null, function(req, res){
req.timedout.should.be.true
res.end('Hello')
})

request(server)
.get('/')
.expect(503, done)
})

it('should pass the error to next()', function(done){
var server = createServer(null, null, function(req, res){
req.timedout.should.be.true
Expand Down Expand Up @@ -204,7 +237,7 @@ function createServer(options, before, after) {
if (after) {
setTimeout(function(){
after(req, res)
}, (_ms + 100))
}, (typeof _ms === 'function' ? _ms(req) : _ms + 100))
}
})
})
Expand Down