-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
31 lines (27 loc) · 932 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module.exports = function responseTimeout(options){
options = options || {};
options.time = options.time || 8000;
options.code = options.code || 500;
options.throwError = options.throwError || options.throwError === undefined;
return function responseTimeout(req, res, next){
var writeHead = res.writeHead
, at = req.method + ' ' + req.url
, timer = setTimeout(function(){
if (options.throwError){
next(new Error('Timeout ' + at));
} else {
res.writeHead(options.code);
res.end();
}
}, options.time);
req.clearTimeout = function(){
clearTimeout(timer);
};
res.writeHead = function(code, headers){
res.writeHead = writeHead;
req.clearTimeout();
res.writeHead(code, headers);
};
next();
}
};