Skip to content

Commit

Permalink
Add timeout option
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
floatdrop committed Nov 22, 2014
1 parent 6c7ddf8 commit 14e0dd8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var zlib = require('zlib');
var PassThrough = require('stream').PassThrough;
var assign = require('object-assign');
var read = require('read-all-stream');
var timeout = require('timed-out');

module.exports = function (url, opts, cb) {
if (typeof opts === 'function') {
Expand Down Expand Up @@ -46,7 +47,7 @@ module.exports = function (url, opts, cb) {
var fn = parsedUrl.protocol === 'https:' ? https : http;
var arg = assign({}, parsedUrl, opts);

fn.get(arg, function (response) {
var req = fn.get(arg, function (response) {
var statusCode = response.statusCode;
var res = response;

Expand Down Expand Up @@ -84,6 +85,10 @@ module.exports = function (url, opts, cb) {

read(res, encoding, cb, response);
}).once('error', cb);

if (opts.timeout) {
timeout(req, opts.timeout);
}
};

get(url, opts, cb);
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha",
"coverage": "istanbul cover _mocha -- -R spec"
"test": "mocha"
},
"files": [
"index.js"
Expand All @@ -33,10 +32,10 @@
],
"dependencies": {
"object-assign": "^1.0.0",
"read-all-stream": "^0.1.0"
"read-all-stream": "^0.1.0",
"timed-out": "^1.0.0"
},
"devDependencies": {
"istanbul": "^0.3.2",
"mocha": "*"
}
}
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Default: `'utf8'`

Encoding to be used on `setEncoding` of the response data. If null, the body is returned as a Buffer.

##### options.timeout

Type: `number`

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Nov 23, 2014

Owner

What's the default timeout? Can you add it here?

This comment has been minimized.

Copy link
@floatdrop

floatdrop Nov 23, 2014

Author Contributor

There is no default timeout. Socket will be closed by server.timeout after 120000 ms of inactivity, not by a client.

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Nov 23, 2014

Owner

What if a server misbehaves and never closes? Does that mean node will hang for infinity? There must be some timeout somewhere.

This comment has been minimized.

Copy link
@floatdrop

floatdrop Nov 23, 2014

Author Contributor

Pretty much so. Here is an eample:

var http = require('http');

var server = http.createServer(function () {
    console.log('Got request');
});

server.timeout = 0;
server.listen(8080);

http.get('http://localhost:8080', function (res) {
    console.log("Got response: " + res.statusCode);
});

It will output Got request and hang.


Time in ms, after which request will be aborted and error event with `ETIMEDOUT` code will be emitted.

##### callback(err, data, response)

###### err
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,12 @@ it('should proxy errors to the stream', function (done) {
done();
});
});

it('should support timeout option', function (done) {
var stream = got('http://sindresorhus.com/', { timeout: 1 });

stream.on('error', function (error) {
assert.strictEqual(error.code, 'ETIMEDOUT');
done();
});
});

0 comments on commit 14e0dd8

Please sign in to comment.