@@ -131,315 +131,310 @@ var https = require('https'),
return this ;
} ;
< < << << < HEAD
this . set_encoding = function ( encoding ) {
this . encoding = encoding ;
return this ;
} ;
this . follow_redirects = function ( max_redirects ) { = == === =
this . follow_redirects = function ( max_redirects ) { >>> >>> > d9794801436738f6bd2bcefd7bdd5820610b4e87
this . max_redirects = + max_redirects || 3 ;
this . follow = true ;
return this ;
} ;
this . stringify = stringify ;
this . retry = function ( ) {
this . retries ++ ;
if ( this . retries > this . max_retry ) {
logger . log ( 'error' , 'Reached max retries' ) ;
this . cb ( {
message : 'Reached max retries' ,
url : this . host + ':' + this . port + this . path
} ,
null ,
this ,
this . additional_arguments
) ;
return this ;
}
logger . log ( 'warn' , 'Retrying request' ) ;
return this . send ( this . data ) ;
} ;
this . follow_redirects = function ( max_redirects ) {
this . max_redirects = + max_redirects || 3 ;
this . follow = true ;
return this ;
} ;
< < << << < HEAD
this . pipe = function ( stream ) {
logger . log ( 'verbose' , 'Piping file..' ) ;
this . _stream = stream ;
return this ;
} ;
this . stringify = stringify ;
this . on = function ( event_type , next ) {
this . retry = function ( ) {
this . retries ++ ;
if ( this . retries > this . max_retry ) {
logger . log ( 'error' , 'Reached max retries' ) ;
this . cb ( {
message : 'Reached max retries' ,
url : this . host + ':' + this . port + this . path
} ,
null ,
this ,
this . additional_arguments
) ;
return this ;
}
logger . log ( 'warn' , 'Retrying request' ) ;
return this . send ( this . data ) ;
} ;
if ( this . callbacks [ event_type ] ) {
this . callbacks [ event_type ] = this . callbacks [ event_type ] . push ( next ) ;
}
< < << << < HEAD
this . pipe = function ( stream ) {
logger . log ( 'verbose' , 'Piping file..' ) ;
this . _stream = stream ;
return this ;
} ;
this . callbacks [ event_type ] = [ next ] ;
this . on = function ( event_type , next ) {
return this ;
} ;
if ( this . callbacks [ event_type ] ) {
this . callbacks [ event_type ] = this . callbacks [ event_type ] . push ( next ) ;
}
this . emit = function ( event_type , err , response ) {
if ( this . callbacks [ event_type ] ) {
this . callbacks [ event_type ] . forEach ( function ( callback ) {
callback ( err , response ) ;
} ) ;
}
this . callbacks [ event_type ] = [ next ] ;
return this ;
} ;
return this ;
} ;
this . send = function ( data ) { = == === =
this . send = function ( data ) { >>> >>> > d9794801436738f6bd2bcefd7bdd5820610b4e87
var new_path = this . path ,
self = this ,
protocol ,
payload ,
req ;
this . emit = function ( event_type , err , response ) {
if ( this . callbacks [ event_type ] ) {
this . callbacks [ event_type ] . forEach ( function ( callback ) {
callback ( err , response ) ;
} ) ;
}
this . started = true ;
this . data = data ;
return this ;
} ;
if ( data && this . method === 'GET' ) {
new_path += '?' + this . stringify ( data ) ;
}
else {
if ( ! this . headers [ 'Content-Type' ] ) {
payload = this . stringify ( data ) ;
this . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
this . headers [ 'Content-Length' ] = payload . length ;
}
else {
payload = JSON . stringify ( data ) ;
}
}
this . send = function ( data ) {
var new_path = this . path ,
self = this ,
protocol ,
payload ,
req ;
this . headers . Accept = 'application/json' ;
this . started = true ;
this . data = data ;
logger . log ( 'verbose' , this . method , this . host + ':' + this . port + new_path ) ;
if ( data && this . method === 'GET' ) {
new_path += '?' + this . stringify ( data ) ;
}
else {
if ( ! this . headers [ 'Content-Type' ] ) {
payload = this . stringify ( data ) ;
this . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
this . headers [ 'Content-Length' ] = payload . length ;
}
else {
payload = JSON . stringify ( data ) ;
}
}
if ( payload ) {
logger . log ( 'debug' , 'data\n' , payload ) ;
}
this . headers . Accept = 'application/json' ;
logger . log ( 'debug ' , 'headers\n' , this . headers ) ;
logger . log ( 'verbose ' , this . method , this . host + ':' + this . port + new_path ) ;
protocol = this . secure ? https : http ;
if ( payload ) {
logger . log ( 'debug' , 'data\n' , payload ) ;
}
this . request_opts . path = new_path ;
this . request_opts . method = this . method ;
this . request_opts . headers = this . headers ;
logger . log ( 'debug' , 'headers\n' , this . headers ) ;
console . log ( this . request_opts ) ;
protocol = this . secure ? https : http ;
try {
req = protocol . request ( this . request_opts ) ;
this . request_opts . path = new_path ;
this . request_opts . method = this . method ;
this . request_opts . headers = this . headers ;
req . on ( 'response' , function ( response ) {
var s = '' ;
try {
req = protocol . request ( this . request_opts ) ;
response . setEncoding ( self . encoding ) ;
req . on ( 'response' , function ( response ) {
var s = '' ;
response . on ( 'data' , function ( chunk ) {
s += chunk ;
response . setEncoding ( self . encoding ) ;
self . emit ( 'data' , null , chunk ) ;
response . on ( 'data' , function ( chunk ) {
s += chunk ;
} ) ;
self . emit ( 'data' , null , chunk ) ;
response . on ( 'close' , function ( ) {
logger . log ( 'error' , 'request closed' ) ;
self . emit ( 'close' , 'request closed' ) ;
self . retry ( ) ;
} ) ;
response . on ( 'error' , function ( err ) {
logger . log ( 'error' , 'Response error' , err ) ;
self . emit ( 'error' , err ) ;
self . retry ( ) ;
} ) ;
response . on ( 'end' , function ( ) {
var redir ,
temp ;
self . response_headers = response . headers ;
self . target_location = response . headers . location ;
if ( self . follow && self . response_headers . location ) {
if ( ! self . max_redirects ) {
self . cb ( {
message : 'Too many redirects'
} , s , self , self . additional_arguments ) ;
return ;
}
temp = self . response_headers . location . split ( '/' ) ;
redir = new Request ( 'GET' )
. to ( self . response_headers . location )
. follow_redirects ( self . max_redirects - 1 ) ;
if ( temp [ 0 ] === 'https:' ) {
redir = redir . secured ( ) ;
}
for ( temp in self . headers ) {
redir = redir . add_header ( temp , self . headers [ temp ] ) ;
}
if ( typeof ( self . _stream ) !== 'undefined' ) {
redir . pipe ( self . _stream ) ;
}
if ( typeof ( self . callbacks ) !== 'undefined' ) {
redir . callbacks = self . callbacks ;
}
redir . target_location = self . target_location ;
redir . set_encoding ( self . encoding ) ;
redir . then ( self . cb ) ;
}
else {
logger . log ( 'verbose' , 'Response' , response . statusCode ) ;
logger . log ( 'silly' , s ) ;
if ( response . headers [ 'content-type' ] && response . headers [
'content-type' ] . split ( ';' ) [ 0 ] ===
'application/json' ) {
if ( self . before_json ) {
s = self . before_json ( s ) ;
}
try {
s = JSON . parse ( s ) ;
}
catch ( e ) {
logger . log ( 'error' , 'JSON is invalid' ) ;
logger . log ( 'error' , e ) ;
e . statusCode = response . statusCode ;
return self . cb ( e , s , self , self . additional_arguments ) ;
}
}
if ( response . statusCode === 200 ) {
if ( typeof ( self . _stream ) !== 'undefined' ) {
response . pipe ( self . _stream ) ;
}
self . cb ( null , s , self , self . additional_arguments ) ;
}
else {
self . cb ( {
response : s ,
status_code : response . statusCode
} , null , self , self . additional_arguments ) ;
}
}
} ) ;
} ) ;
req . on ( 'error' , function ( err ) {
var retryable_errors = [
'ECONNREFUSED' ,
'ECONNRESET' ,
'ENOTFOUND' ,
'EADDRINFO' ,
'ETIMEDOUT' ,
'ESRCH'
] ;
logger . log ( 'error' , 'Request error' , err , self . host + ':' + self . port +
self . path ) ;
if ( ~ retryable_errors . indexOf ( err . code ) ) {
if ( self . retries < self . max_retry ) {
return self . retry ( ) ;
}
err . message = 'OMG. Server on ' + self . host + ':' + self . port +
' seems dead' ;
}
self . cb ( err , null , self , self . additional_arguments ) ;
} ) ;
req . on ( 'continue' , function ( ) {
logger . log ( 'error' , 'continue event emitted' ) ;
self . retry ( ) ;
} ) ;
req . on ( 'upgrade' , function ( ) {
logger . log ( 'error' , 'upgrade event emitted' ) ;
self . retry ( ) ;
} ) ;
req . on ( 'connect' , function ( ) {
logger . log ( 'error' , 'connect event emitted' ) ;
self . retry ( ) ;
} ) ;
if ( this . method !== 'GET' ) {
req . write ( payload ) ;
}
} ) ;
req . end ( ) ;
response . on ( 'close' , function ( ) {
logger . log ( 'error' , 'request closed' ) ;
self . emit ( 'close' , 'request closed' ) ;
self . retry ( ) ;
} ) ;
response . on ( 'error' , function ( err ) {
logger . log ( 'error' , 'Response error' , err ) ;
self . emit ( 'error' , err ) ;
self . retry ( ) ;
} ) ;
response . on ( 'end' , function ( ) {
var redir ,
temp ;
self . response_headers = response . headers ;
self . target_location = response . headers . location ;
if ( self . follow && self . response_headers . location ) {
if ( ! self . max_redirects ) {
self . cb ( {
message : 'Too many redirects'
} , s , self , self . additional_arguments ) ;
return ;
}
temp = self . response_headers . location . split ( '/' ) ;
redir = new Request ( 'GET' )
. to ( self . response_headers . location )
. follow_redirects ( self . max_redirects - 1 ) ;
if ( temp [ 0 ] === 'https:' ) {
redir = redir . secured ( ) ;
}
catch ( e ) {
logger . log ( 'error' , e ) ;
self . retry ( ) ;
for ( temp in self . headers ) {
redir = redir . add_header ( temp , self . headers [ temp ] ) ;
}
return this ;
} ;
} ,
attach = function ( object ) {
object . get = {
to : function ( host , port , path ) {
return new Request ( 'GET' ) . to ( host , port , path ) ;
if ( typeof ( self . _stream ) !== 'undefined' ) {
redir . pipe ( self . _stream ) ;
}
} ;
object . post = {
to : function ( host , port , path ) {
return new Request ( 'POST' ) . to ( host , port , path ) ;
if ( typeof ( self . callbacks ) !== 'undefined' ) {
redir . callbacks = self . callbacks ;
}
} ;
object . put = {
to : function ( host , port , path ) {
return new Request ( 'PUT' ) . to ( host , port , path ) ;
redir . target_location = self . target_location ;
redir . set_encoding ( self . encoding ) ;
redir . then ( self . cb ) ;
}
else {
logger . log ( 'verbose' , 'Response' , response . statusCode ) ;
logger . log ( 'silly' , s ) ;
if ( response . headers [ 'content-type' ] && response . headers [
'content-type' ] . split ( ';' ) [ 0 ] ===
'application/json' ) {
if ( self . before_json ) {
s = self . before_json ( s ) ;
}
try {
s = JSON . parse ( s ) ;
}
catch ( e ) {
logger . log ( 'error' , 'JSON is invalid' ) ;
logger . log ( 'error' , e ) ;
e . statusCode = response . statusCode ;
return self . cb ( e , s , self , self . additional_arguments ) ;
}
}
} ;
object . delete = {
to : function ( host , port , path ) {
return new Request ( 'DELETE' ) . to ( host , port , path ) ;
if ( response . statusCode === 200 ) {
if ( typeof ( self . _stream ) !== 'undefined' ) {
response . pipe ( self . _stream ) ;
}
self . cb ( null , s , self , self . additional_arguments ) ;
}
} ;
else {
object . request = function ( method ) {
this . to = function ( host , port , path ) {
return new Request ( method ) . to ( host , port , path ) ;
} ;
return this ;
} ;
self . cb ( {
response : s ,
status_code : response . statusCode
} , null , self , self . additional_arguments ) ;
}
}
} ) ;
} ) ;
req . on ( 'error' , function ( err ) {
var retryable_errors = [
'ECONNREFUSED' ,
'ECONNRESET' ,
'ENOTFOUND' ,
'EADDRINFO' ,
'ETIMEDOUT' ,
'ESRCH'
] ;
logger . log ( 'error' , 'Request error' , err , self . host + ':' + self . port +
self . path ) ;
if ( ~ retryable_errors . indexOf ( err . code ) ) {
if ( self . retries < self . max_retry ) {
return self . retry ( ) ;
}
err . message = 'OMG. Server on ' + self . host + ':' + self . port +
' seems dead' ;
}
self . cb ( err , null , self , self . additional_arguments ) ;
} ) ;
req . on ( 'continue' , function ( ) {
logger . log ( 'error' , 'continue event emitted' ) ;
self . retry ( ) ;
} ) ;
req . on ( 'upgrade' , function ( ) {
logger . log ( 'error' , 'upgrade event emitted' ) ;
self . retry ( ) ;
} ) ;
req . on ( 'connect' , function ( ) {
logger . log ( 'error' , 'connect event emitted' ) ;
self . retry ( ) ;
} ) ;
if ( this . method !== 'GET' ) {
req . write ( payload ) ;
}
req . end ( ) ;
}
catch ( e ) {
logger . log ( 'error' , e ) ;
self . retry ( ) ;
}
return this ;
} ;
} ,
attach = function ( object ) {
object . get = {
to : function ( host , port , path ) {
return new Request ( 'GET' ) . to ( host , port , path ) ;
}
} ;
object . post = {
to : function ( host , port , path ) {
return new Request ( 'POST' ) . to ( host , port , path ) ;
}
} ;
object . put = {
to : function ( host , port , path ) {
return new Request ( 'PUT' ) . to ( host , port , path ) ;
}
} ;
object . delete = {
to : function ( host , port , path ) {
return new Request ( 'DELETE' ) . to ( host , port , path ) ;
}
} ;
object . request = function ( method ) {
this . to = function ( host , port , path ) {
return new Request ( method ) . to ( host , port , path ) ;
} ;
return this ;
} ;
object . stringify = stringify ;
object . stringify = stringify ;
return object ;
} ;
return object ;
} ;
module . exports = function ( _logger ) {
logger = _logger || logger ;
return attach ( { } ) ;
} ;
module . exports = function ( _logger ) {
logger = _logger || logger ;
return attach ( { } ) ;
} ;
attach ( module . exports ) ;
attach ( module . exports ) ;