pax_global_header00006660000000000000000000000064135352461730014523gustar00rootroot0000000000000052 comment=9cc4ef15197e9017dd84e2639bbb7a6e09f3adf7 timed-out-5.0.0/000077500000000000000000000000001353524617300134345ustar00rootroot00000000000000timed-out-5.0.0/.editorconfig000066400000000000000000000002571353524617300161150ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 timed-out-5.0.0/.gitattributes000066400000000000000000000000231353524617300163220ustar00rootroot00000000000000* text=auto eol=lf timed-out-5.0.0/.gitignore000066400000000000000000000000271353524617300154230ustar00rootroot00000000000000node_modules yarn.lock timed-out-5.0.0/.npmrc000066400000000000000000000000231353524617300145470ustar00rootroot00000000000000package-lock=false timed-out-5.0.0/.travis.yml000066400000000000000000000000651353524617300155460ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' timed-out-5.0.0/index.js000066400000000000000000000025761353524617300151130ustar00rootroot00000000000000'use strict'; module.exports = (request, time) => { if (request.timeoutTimer) { return request; } const delays = typeof time === 'number' ? {socket: time, connect: time} : time; const host = `to ${request.getHeaders().host}`; if (delays.connect !== undefined) { request.timeoutTimer = setTimeout(() => { request.abort(); const error = new Error(`Connection timed out on request ${host}`); error.code = 'ETIMEDOUT'; request.emit('error', error); }, delays.connect); } // Clear the connection timeout timer once a socket is assigned to the // request and is connected. request.on('socket', socket => { // Socket may come from `Agent` pool and may be already connected. if (!(socket.connecting || socket._connecting)) { connect(); return; } socket.once('connect', connect); }); const clear = () => { if (request.timeoutTimer) { clearTimeout(request.timeoutTimer); request.timeoutTimer = undefined; } }; const connect = () => { clear(); if (delays.socket !== undefined) { // Abort the request if there is no activity on the socket for more // than `delays.socket` milliseconds. request.setTimeout(delays.socket, () => { request.abort(); const error = new Error(`Socket timed out on request ${host}`); error.code = 'ESOCKETTIMEDOUT'; request.emit('error', error); }); } }; return request.on('error', clear); }; timed-out-5.0.0/license000066400000000000000000000022171353524617300150030ustar00rootroot00000000000000MIT License Copyright (c) Vsevolod Strukchinsky Copyright (c) Sindre Sorhus (sindresorhus.com) 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-5.0.0/package.json000066400000000000000000000006531353524617300157260ustar00rootroot00000000000000{ "name": "timed-out", "version": "5.0.0", "description": "Timeout HTTP/HTTPS requests", "license": "MIT", "repository": "sindresorhus/timed-out", "engines": { "node": ">=8" }, "scripts": { "test": "xo && mocha" }, "files": [ "index.js" ], "keywords": [ "http", "https", "timeout", "get", "url", "uri", "request", "simple" ], "devDependencies": { "mocha": "^6.2.0", "xo": "^0.24.0" } } timed-out-5.0.0/readme.md000066400000000000000000000017401353524617300152150ustar00rootroot00000000000000# 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 Emits Error object with `code` property equal `ETIMEDOUT` or `ESOCKETTIMEDOUT` when ClientRequest is hanged. ## Usage ```js const timedOut = require('timed-out'); const http = require('http'); const timedOut = require('timed-out'); const request = http.get('http://www.google.ru'); timedOut(request, 2000); // Sets a 2 seconds limit ``` ## API ### timedout(request, time) #### request *Required* Type: [`ClientRequest`](https://nodejs.org/api/http.html#http_class_http_clientrequest) The request to watch. #### time *Required* Type: `number | object` Time in milliseconds to wait for a `connect` event on the socket and also time to wait on inactive socket. Or you can pass an object with the following fields: - `connect` - Time to wait for a connection. - `socket` - Time to wait for activity on the socket. timed-out-5.0.0/test.js000066400000000000000000000125451353524617300147600ustar00rootroot00000000000000/* global describe, before, after, it */ 'use strict'; const {strict: assert} = require('assert'); const http = require('http'); const net = require('net'); const timeout = require('.'); const port = Math.floor((Math.random() * (60000 - 30000)) + 30000); it('should do HTTP request with a lot of time', done => { const request = http.get('http://google.com', response => { assert.ok(response.statusCode > 300 && response.statusCode < 399); done(); }); request.on('error', done); timeout(request, 1000); }); it.skip('should emit ETIMEDOUT when connection timeout expires', done => { // To prevent the connection from being established use a non-routable IP // address. See https://tools.ietf.org/html/rfc5737#section-3 const request = http.get('http://192.0.2.1'); request.on('error', error => { if (error.code === 'ETIMEDOUT') { assert.equal(error.message, 'Connection timed out on request to 192.0.2.1'); done(); } }); timeout(request, 200); }); describe('when connection is established', () => { let server; before(done => { server = http.createServer(); server.listen(port, done); }); after(done => { server.close(done); }); it('should emit ESOCKETTIMEDOUT (no data)', done => { server.once('request', () => {}); const request = http.get(`http://0.0.0.0:${port}`); request.on('error', error => { if (error.code === 'ESOCKETTIMEDOUT') { assert.equal(error.message, `Socket timed out on request to 0.0.0.0:${port}`); done(); } }); timeout(request, 200); }); it('should emit ESOCKETTIMEDOUT (only first chunk of body)', done => { server.once('request', (request, response) => { response.writeHead(200, {'content-type': 'text/plain'}); setTimeout(() => { response.write('chunk'); }, 100); }); let isCalled = false; let body = ''; const request = http.get(`http://0.0.0.0:${port}`); request.on('response', response => { isCalled = true; assert.equal(response.statusCode, 200); assert.equal(response.headers['content-type'], 'text/plain'); response.setEncoding('utf8'); response.on('data', chunk => { body += chunk; }); }); request.on('error', error => { if (error.code === 'ESOCKETTIMEDOUT') { assert.ok(isCalled); assert.equal(body, 'chunk'); assert.equal(error.message, `Socket timed out on request to 0.0.0.0:${port}`); done(); } }); timeout(request, {socket: 200, connect: 50}); }); it('should be able to only apply connect timeout', done => { server.once('request', (request, response) => { setTimeout(() => { response.writeHead(200); response.end('data'); }, 100); }); const request = http.get(`http://0.0.0.0:${port}`); request.on('error', done); request.on('finish', done); timeout(request, {connect: 50}); }); it('should be able to only apply socket timeout', done => { server.once('request', (request, response) => { setTimeout(() => { response.writeHead(200); response.end('data'); }, 200); }); const request = http.get(`http://0.0.0.0:${port}`); request.on('error', error => { if (error.code === 'ESOCKETTIMEDOUT') { assert.equal(error.message, `Socket timed out on request to 0.0.0.0:${port}`); done(); } }); timeout(request, {socket: 50}); }); // Different requests may reuse one socket if keep-alive is enabled it('should not add event handlers twice for the same socket', done => { server.on('request', (request, response) => { response.writeHead(200); response.end('data'); }); let socket = null; const keepAliveAgent = new http.Agent({ maxSockets: 1, keepAlive: true }); const requestOptions = { hostname: '0.0.0.0', port, agent: keepAliveAgent }; const request1 = http.get(requestOptions, response => { response.resume(); const request2 = http.get(requestOptions, response => { response.resume(); keepAliveAgent.destroy(); server.removeAllListeners('request'); done(); }); timeout(request2, 100); request2.on('socket', socket_ => { assert.equal(socket_, socket); assert.equal(socket_.listeners('connect').length, 0); }); }); timeout(request1, 100); request1.on('socket', socket_ => { socket = socket_; }); }); it('should set socket timeout if socket is already connected', done => { server.once('request', () => {}); const socket = net.connect(port, '0.0.0.0', () => { const request = http.get({ createConnection: () => socket, hostname: '0.0.0.0', port }); request.on('error', error => { if (error.code === 'ESOCKETTIMEDOUT') { done(); } }); timeout(request, 200); }); }); it.skip('should clear socket timeout for keep-alive sockets', done => { server.once('request', (request, response) => { response.writeHead(200); response.end('data'); }); let socket = null; const agent = new http.Agent({ keepAlive: true, maxSockets: 1 }); const options = { hostname: '0.0.0.0', agent, port }; const request = http.get(options, response => { assert.equal(socket.timeout, 100); response.resume(); response.on('end', () => { assert.equal(socket.destroyed, false); assert.equal(socket.timeout, -1); agent.destroy(); done(); }); }); timeout(request, 100); request.on('socket', socket_ => { socket_.once('connect', () => { assert.equal(socket_.timeout, 100); }); socket = socket_; }); }); });