pax_global_header00006660000000000000000000000064130371505420014512gustar00rootroot0000000000000052 comment=2f00fdafb6af0a9b91fb23a04a9271766c5559a4 timed-out-4.0.1/000077500000000000000000000000001303715054200134235ustar00rootroot00000000000000timed-out-4.0.1/.editorconfig000066400000000000000000000003621303715054200161010ustar00rootroot00000000000000# editorconfig.org root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [package.json] indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false timed-out-4.0.1/.gitattributes000066400000000000000000000000161303715054200163130ustar00rootroot00000000000000* text eol=lf timed-out-4.0.1/.gitignore000066400000000000000000000000261303715054200154110ustar00rootroot00000000000000node_modules coverage timed-out-4.0.1/.travis.yml000066400000000000000000000000631303715054200155330ustar00rootroot00000000000000language: node_js node_js: - '4' - '5' - '6' timed-out-4.0.1/index.js000066400000000000000000000025311303715054200150710ustar00rootroot00000000000000'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); }; timed-out-4.0.1/license000066400000000000000000000021211303715054200147640ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Vsevolod Strukchinsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. timed-out-4.0.1/package.json000066400000000000000000000011521303715054200157100ustar00rootroot00000000000000{ "name": "timed-out", "version": "4.0.1", "description": "Emit `ETIMEDOUT` or `ESOCKETTIMEDOUT` when ClientRequest is hanged", "license": "MIT", "repository": "floatdrop/timed-out", "author": { "name": "Vsevolod Strukchinsky", "email": "floatdrop@gmail.com" }, "engines": { "node": ">=0.10.0" }, "scripts": { "test": "xo && mocha" }, "files": [ "index.js" ], "keywords": [ "http", "https", "get", "got", "url", "uri", "request", "util", "utility", "simple" ], "devDependencies": { "mocha": "*", "xo": "^0.16.0" } } timed-out-4.0.1/readme.md000066400000000000000000000017321303715054200152050ustar00rootroot00000000000000# timed-out [![Build Status](https://travis-ci.org/floatdrop/timed-out.svg?branch=master)](https://travis-ci.org/floatdrop/timed-out) > Timeout HTTP/HTTPS requests Emit Error object with `code` property equal `ETIMEDOUT` or `ESOCKETTIMEDOUT` when ClientRequest is hanged. ## Usage ```js var get = require('http').get; var timeout = require('timed-out'); var req = get('http://www.google.ru'); timeout(req, 2000); // Set 2 seconds limit ``` ### API #### timedout(request, time) ##### request *Required* Type: [`ClientRequest`](http://nodejs.org/api/http.html#http_class_http_clientrequest) The request to watch on. ##### time *Required* Type: `number` or `object` Time in milliseconds to wait for `connect` event on socket and also time to wait on inactive socket. Or you can pass Object with following fields: - `connect` - time to wait for connection - `socket` - time to wait for activity on socket ## License MIT © [Vsevolod Strukchinsky](floatdrop@gmail.com) timed-out-4.0.1/test.js000066400000000000000000000122731303715054200147450ustar00rootroot00000000000000/* global describe, before, after, it */ 'use strict'; var assert = require('assert'); var http = require('http'); var net = require('net'); var timeout = require('./'); it('should do HTTP request with a lot of time', function (done) { var req = http.get('http://google.com', function (res) { assert.ok(res.statusCode > 300 && res.statusCode < 399); done(); }); req.on('error', done); timeout(req, 1000); }); it('should emit ETIMEDOUT when connection timeout expires', function (done) { // To prevent the connection from being established use a non-routable IP // address. See https://tools.ietf.org/html/rfc5737#section-3 var req = http.get('http://192.0.2.1'); req.on('error', function (err) { if (err.code === 'ETIMEDOUT') { assert.equal(err.message, 'Connection timed out on request to 192.0.2.1'); done(); } }); timeout(req, 200); }); describe('when connection is established', function () { var server; before(function (done) { server = http.createServer(); server.listen(8081, done); }); after(function (done) { server.close(done); }); it('should emit ESOCKETTIMEDOUT (no data)', function (done) { server.once('request', function () {}); var req = http.get('http://0.0.0.0:8081'); req.on('error', function (err) { if (err.code === 'ESOCKETTIMEDOUT') { assert.equal(err.message, 'Socket timed out on request to 0.0.0.0:8081'); done(); } }); timeout(req, 200); }); it('should emit ESOCKETTIMEDOUT (only first chunk of body)', function (done) { server.once('request', function (req, res) { res.writeHead(200, {'content-type': 'text/plain'}); setTimeout(function () { res.write('chunk'); }, 100); }); var called = false; var body = ''; var req = http.get('http://0.0.0.0:8081'); req.on('response', function (res) { called = true; assert.equal(res.statusCode, 200); assert.equal(res.headers['content-type'], 'text/plain'); res.setEncoding('utf8'); res.on('data', function (chunk) { body += chunk; }); }); req.on('error', function (err) { if (err.code === 'ESOCKETTIMEDOUT') { assert.ok(called); assert.equal(body, 'chunk'); assert.equal(err.message, 'Socket timed out on request to 0.0.0.0:8081'); done(); } }); timeout(req, {socket: 200, connect: 50}); }); it('should be able to only apply connect timeout', function (done) { server.once('request', function (req, res) { setTimeout(function () { res.writeHead(200); res.end('data'); }, 100); }); var req = http.get('http://0.0.0.0:8081'); req.on('error', done); req.on('finish', done); timeout(req, {connect: 50}); }); it('should be able to only apply socket timeout', function (done) { server.once('request', function (req, res) { setTimeout(function () { res.writeHead(200); res.end('data'); }, 200); }); var req = http.get('http://0.0.0.0:8081'); req.on('error', function (err) { if (err.code === 'ESOCKETTIMEDOUT') { assert.equal(err.message, 'Socket timed out on request to 0.0.0.0:8081'); done(); } }); timeout(req, {socket: 50}); }); // Different requests may reuse one socket if keep-alive is enabled it('should not add event handlers twice for the same socket', function (done) { server.on('request', function (req, res) { res.writeHead(200); res.end('data'); }); var socket = null; var keepAliveAgent = new http.Agent({ maxSockets: 1, keepAlive: true }); var reqOpts = { hostname: '0.0.0.0', port: 8081, agent: keepAliveAgent }; var req1 = http.get(reqOpts, function (resp) { resp.resume(); var req2 = http.get(reqOpts, function (resp) { resp.resume(); keepAliveAgent.destroy(); server.removeAllListeners('request'); done(); }); timeout(req2, 100); req2.on('socket', function (sock) { assert.equal(sock, socket); assert.equal(sock.listeners('connect').length, 0); }); }); timeout(req1, 100); req1.on('socket', function (sock) { socket = sock; }); }); it('should set socket timeout if socket is already connected', function (done) { server.once('request', function () {}); var socket = net.connect(8081, '0.0.0.0', function () { var req = http.get({ createConnection: function () { return socket; }, hostname: '0.0.0.0', port: 8081 }); req.on('error', function (err) { if (err.code === 'ESOCKETTIMEDOUT') { done(); } }); timeout(req, 200); }); }); it('should clear socket timeout for keep-alive sockets', function (done) { server.once('request', function (req, res) { res.writeHead(200); res.end('data'); }); var socket = null; var agent = new http.Agent({ keepAlive: true, maxSockets: 1 }); var options = { hostname: '0.0.0.0', agent: agent, port: 8081 }; var req = http.get(options, function (res) { assert.equal(socket._idleTimeout, 100); res.resume(); res.on('end', function () { assert.equal(socket.destroyed, false); assert.equal(socket._idleTimeout, -1); agent.destroy(); done(); }); }); timeout(req, 100); req.on('socket', function (sock) { sock.once('connect', function () { assert.equal(sock._idleTimeout, 100); }); socket = sock; }); }); });