Skip to content
This repository has been archived by the owner on Feb 13, 2018. It is now read-only.

Commit

Permalink
implemented socket timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Whitlock committed Mar 5, 2013
1 parent dd78d84 commit cf77fb3
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions lib/twitter.js
Expand Up @@ -6,7 +6,7 @@

// pseudo constants
//
var TWITTER_API_TIMEOUT = 5,
var TWITTER_API_TIMEOUT = 10000,
TWITTER_API_USERAGENT = 'Node/'+process.version.substr(1),
TWITTER_API_BASE = 'https://api.twitter.com/1.1';
TWITTER_STREAM_BASE = 'https://stream.twitter.com/1.1';
Expand Down Expand Up @@ -154,7 +154,16 @@ TwitterClient.prototype._rest = function( requestMethod, requestPath, requestArg
throw new Error('Twitter REST client not authenticated');
}
var requestUri = TWITTER_API_BASE + '/' + requestPath + '.json';
return this.call( requestMethod, requestUri, requestArgs, function( res ) {
if( 'function' !== typeof callback ){
callback = function(){
console.error('No callback for '+requestMethod+' '+requestUri);
}
}
return this.call( requestMethod, requestUri, requestArgs, function( res, err ) {
if( ! res ){
callback( null, err, 0 );
return;
}
// started to receive response from twitter
res.setEncoding('utf8');
var body = '';
Expand Down Expand Up @@ -202,11 +211,14 @@ TwitterClient.prototype.stream = function( requestPath, requestArgs, callback ){
}
if( 'function' !== typeof callback ){
callback = function( json ){
console.log( json );
json && console.log( json );
};
}
var client = this;
return this.call( requestMethod, requestUri, requestArgs, function(res){
if( ! res ){
callback();
}
client.response = res;
res.setEncoding('utf8');
if( 200 !== res.statusCode ){
Expand Down Expand Up @@ -250,6 +262,19 @@ TwitterClient.prototype.call = function( requestMethod, requestUri, requestArgs,
http.path += '?' + query;
}
var req = require('https').get( http, callback );
req.on('socket', function (socket) {
socket.setTimeout( TWITTER_API_TIMEOUT );
socket.on('timeout', function() {
req.abort();
} );
} );
req.on('error', function( e ) {
console.error( String(e) );
if( 'ECONNRESET' === e.code ){
e = { message: 'Request timeout', code: 408 };
}
callback( null, e );
} );
if( 'POST' === requestMethod && query ){
req.write( query );
req.write( '\n' );
Expand Down Expand Up @@ -283,7 +308,16 @@ TwitterClient.prototype.fetchAccessToken = function( verifier, callback ){
}

TwitterClient.prototype._oauthExchange = function( requestUri, requestArgs, callback ){
this.call( 'POST', requestUri, requestArgs, function( res ){
if( 'function' !== typeof callback ){
callback = function(){
console.error('No callback for POST '+requestUri);
}
}
this.call( 'POST', requestUri, requestArgs, function( res, err ){
if( ! res ){
callback( null, err||{}, 0 );
return;
}
// started to receive response from twitter
res.setEncoding('utf8');
var body = '';
Expand All @@ -293,7 +327,7 @@ TwitterClient.prototype._oauthExchange = function( requestUri, requestArgs, call
res.on('end', function(){
var params = require('querystring').parse( body );
var token = params.oauth_token && params.oauth_token_secret && new OAuthToken( params.oauth_token, params.oauth_token_secret );
'function' == typeof callback && callback( token, params, res.statusCode );
callback( token, params, res.statusCode );
} );
} );
}
Expand All @@ -316,3 +350,16 @@ exports.createOAuthToken = function( key, secret ){
exports.createClient = function(){
return new TwitterClient;
}


// expose some configuration setters

exports.setTimeout = function( ms ){
ms = Number(ms);
if( ! isNaN(ms) ){
TWITTER_API_TIMEOUT = ms;
}
return exports;
}


0 comments on commit cf77fb3

Please sign in to comment.