0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/deps/npm/node_modules/timed-out/index.js
Rebecca Turner 468ab4519e deps: upgrade npm to 6.1.0
PR-URL: https://github.com/nodejs/node/pull/20190
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2018-05-24 23:24:45 -07:00

56 lines
1.3 KiB
JavaScript

'use strict';
module.exports = function (req, time) {
if (req.timeoutTimer) {
return req;
}
var delays = isNaN(time) ? time : {socket: time, connect: time};
var host = req._headers ? (' to ' + req._headers.host) : '';
if (delays.connect !== undefined) {
req.timeoutTimer = setTimeout(function timeoutHandler() {
req.abort();
var e = new Error('Connection timed out on request' + host);
e.code = 'ETIMEDOUT';
req.emit('error', e);
}, delays.connect);
}
// Clear the connection timeout timer once a socket is assigned to the
// request and is connected.
req.on('socket', function assign(socket) {
// Socket may come from Agent pool and may be already connected.
if (!(socket.connecting || socket._connecting)) {
connect();
return;
}
socket.once('connect', connect);
});
function clear() {
if (req.timeoutTimer) {
clearTimeout(req.timeoutTimer);
req.timeoutTimer = null;
}
}
function connect() {
clear();
if (delays.socket !== undefined) {
// Abort the request if there is no activity on the socket for more
// than `delays.socket` milliseconds.
req.setTimeout(delays.socket, function socketTimeoutHandler() {
req.abort();
var e = new Error('Socket timed out on request' + host);
e.code = 'ESOCKETTIMEDOUT';
req.emit('error', e);
});
}
}
return req.on('error', clear);
};