-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
34 lines (28 loc) · 822 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
32
33
34
'use strict';
module.exports = function (req, time) {
if (req.timeoutTimer) { return req; }
var host = req._headers ? (' to ' + req._headers.host) : '';
req.timeoutTimer = setTimeout(function timeoutHandler() {
req.abort();
var e = new Error('Connection timed out on request' + host);
e.code = 'ETIMEDOUT';
req.emit('error', e);
}, time);
// Set additional timeout on socket - in case if remote
// server freeze after sending headers
req.setTimeout(time, function socketTimeoutHandler() {
req.abort();
var e = new Error('Socket timed out on request' + host);
e.code = 'ESOCKETTIMEDOUT';
req.emit('error', e);
});
function clear() {
if (req.timeoutTimer) {
clearTimeout(req.timeoutTimer);
req.timeoutTimer = null;
}
}
return req
.on('response', clear)
.on('error', clear);
};