@ -1,4 +1,5 @@
var Promise , S , addErrorData , bhttpAPI , bhttpErrors , concatStream , createCookieJar , debug , debugRequest , debugResponse , devNull , doPayloadRequest , doRedirect , errors , extend , formData , formFixArray , http , https , isStream , makeRequest , ofTypes , packageConfig , prepareCleanup , prepareDefaults , prepareOptions , preparePayload , prepareProtocol , prepareRequest , prepareSession , prepareUrl , processResponse , querystring , redirectGet , redirectUnchanged , sink , spy , stream , streamLength , toughCookie , urlUtil , util , _ ;
// Generated by CoffeeScript 1.9.3
var Promise , S , _ , addErrorData , bhttpAPI , bhttpErrors , concatStream , createCookieJar , debug , debugRequest , debugResponse , devNull , doPayloadRequest , doRedirect , errors , extend , formData , formFixArray , http , https , isStream , makeRequest , ofTypes , packageConfig , prepareCleanup , prepareDefaults , prepareOptions , preparePayload , prepareProtocol , prepareRequest , prepareSession , prepareUrl , processResponse , querystring , redirectGet , redirectUnchanged , sink , spy , stream , streamLength , toughCookie , urlUtil , util ;
urlUtil = require ( "url" ) ;
@ -90,10 +91,10 @@ errors.create({
} ) ;
ofTypes = function ( obj , types ) {
var match, type , _i , _len ;
var i, len , match , type ;
match = false ;
for ( _ i = 0 , _ len = types . length ; _ i < _ len; _ i++ ) {
type = types [ _ i] ;
for ( i = 0 , len = types . length ; i < len; i++ ) {
type = types [ i] ;
match = match || obj instanceof type ;
}
return match ;
@ -140,27 +141,27 @@ prepareSession = function(request, response, requestState) {
prepareDefaults = function ( request , response , requestState ) {
debugRequest ( "preparing defaults" ) ;
return Promise [ "try" ] ( function ( ) {
var _ base, _ base1, _ base2, _ ref, _ ref1, _ ref2, _ ref3, _ ref4, _ ref5, _ ref6, _ ref7;
var base, base1, base2, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7;
request . responseOptions = {
discardResponse : ( _ ref = request . options . discardResponse ) != null ? _ ref : false ,
keepRedirectResponses : ( _ ref1 = request . options . keepRedirectResponses ) != null ? _ ref1 : false ,
followRedirects : ( _ ref2 = request . options . followRedirects ) != null ? _ ref2 : true ,
noDecode : ( _ ref3 = request . options . noDecode ) != null ? _ ref3 : false ,
decodeJSON : ( _ ref4 = request . options . decodeJSON ) != null ? _ ref4 : false ,
stream : ( _ ref5 = request . options . stream ) != null ? _ ref5 : false ,
justPrepare : ( _ ref6 = request . options . justPrepare ) != null ? _ ref6 : false ,
redirectLimit : ( _ ref7 = request . options . redirectLimit ) != null ? _ ref7 : 10 ,
discardResponse : ( ref = request . options . discardResponse ) != null ? ref : false ,
keepRedirectResponses : ( ref1 = request . options . keepRedirectResponses ) != null ? ref1 : false ,
followRedirects : ( ref2 = request . options . followRedirects ) != null ? ref2 : true ,
noDecode : ( ref3 = request . options . noDecode ) != null ? ref3 : false ,
decodeJSON : ( ref4 = request . options . decodeJSON ) != null ? ref4 : false ,
stream : ( ref5 = request . options . stream ) != null ? ref5 : false ,
justPrepare : ( ref6 = request . options . justPrepare ) != null ? ref6 : false ,
redirectLimit : ( ref7 = request . options . redirectLimit ) != null ? ref7 : 10 ,
onDownloadProgress : request . options . onDownloadProgress ,
responseTimeout : request . options . responseTimeout
} ;
if ( ( _ base = request . options ) . allowChunkedMultipart == null ) {
_ base. allowChunkedMultipart = false ;
if ( ( base = request . options ) . allowChunkedMultipart == null ) {
base. allowChunkedMultipart = false ;
}
if ( ( _ base1 = request . options ) . forceMultipart == null ) {
_ base1. forceMultipart = false ;
if ( ( base1 = request . options ) . forceMultipart == null ) {
base1. forceMultipart = false ;
}
if ( ( _ base2 = request . options . headers ) [ "user-agent" ] == null ) {
_ base2[ "user-agent" ] = "bhttp/" + packageConfig . version ;
if ( ( base2 = request . options . headers ) [ "user-agent" ] == null ) {
base2[ "user-agent" ] = "bhttp/" + packageConfig . version ;
}
request . options . method = request . options . method . toLowerCase ( ) ;
return Promise . resolve ( [ request , response , requestState ] ) ;
@ -170,7 +171,7 @@ prepareDefaults = function(request, response, requestState) {
prepareUrl = function ( request , response , requestState ) {
debugRequest ( "preparing URL" ) ;
return Promise [ "try" ] ( function ( ) {
var urlOptions, _ref ;
var ref, urlOptions;
urlOptions = urlUtil . parse ( request . url , true ) ;
_ . extend ( request . options , {
hostname : urlOptions . hostname ,
@ -178,7 +179,7 @@ prepareUrl = function(request, response, requestState) {
} ) ;
request . options . path = urlUtil . format ( {
pathname : urlOptions . pathname ,
query : ( _ ref = request . options . query ) != null ? _ ref : urlOptions . query
query : ( ref = request . options . query ) != null ? ref : urlOptions . query
} ) ;
request . protocol = S ( urlOptions . protocol ) . chompRight ( ":" ) . toString ( ) ;
return Promise . resolve ( [ request , response , requestState ] ) ;
@ -188,7 +189,7 @@ prepareUrl = function(request, response, requestState) {
prepareProtocol = function ( request , response , requestState ) {
debugRequest ( "preparing protocol" ) ;
return Promise [ "try" ] ( function ( ) {
var _ base;
var base;
request . protocolModule = ( function ( ) {
switch ( request . protocol ) {
case "http" :
@ -202,8 +203,8 @@ prepareProtocol = function(request, response, requestState) {
if ( request . protocolModule == null ) {
return Promise . reject ( ) ( new bhttpErrors . UnsupportedProtocolError ( "The protocol specified (" + protocol + ") is not currently supported by this module." ) ) ;
}
if ( ( _ base = request . options ) . port == null ) {
_ base. port = ( function ( ) {
if ( ( base = request . options ) . port == null ) {
base. port = ( function ( ) {
switch ( request . protocol ) {
case "http" :
return 80 ;
@ -219,7 +220,7 @@ prepareProtocol = function(request, response, requestState) {
prepareOptions = function ( request , response , requestState ) {
debugRequest ( "preparing options" ) ;
return Promise [ "try" ] ( function ( ) {
var _ base;
var base;
if ( ( ( request . options . formFields != null ) || ( request . options . files != null ) ) && ( ( request . options . inputStream != null ) || ( request . options . inputBuffer != null ) ) ) {
return Promise . reject ( addErrorData ( new bhttpErrors . ConflictingOptionsError ( "You cannot define both formFields/files and a raw inputStream or inputBuffer." ) , request , response , requestState ) ) ;
}
@ -227,8 +228,8 @@ prepareOptions = function(request, response, requestState) {
return Promise . reject ( addErrorData ( new bhttpErrors . ConflictingOptionsError ( "You cannot use both encodeJSON and a raw inputStream or inputBuffer." , void 0 , "If you meant to JSON-encode the stream, you will currently have to do so manually." ) , request , response , requestState ) ) ;
}
if ( request . responseOptions . stream ) {
if ( ( _ base = request . options ) . agent == null ) {
_ base. agent = false ;
if ( ( base = request . options ) . agent == null ) {
base. agent = false ;
}
}
return Promise . resolve ( [ request , response , requestState ] ) ;
@ -238,7 +239,7 @@ prepareOptions = function(request, response, requestState) {
preparePayload = function ( request , response , requestState ) {
debugRequest ( "preparing payload" ) ;
return Promise [ "try" ] ( function ( ) {
var containsStreams , fieldName , fieldValue , formDataObject , multipart, streamOptions , valueElement , _i , _len , _ref , _ref1 , _ref2 ;
var containsStreams , fieldName , fieldValue , formDataObject , i, len , multipart , ref , ref1 , ref2 , streamOptions , valueElement ;
request . onUploadProgress = request . options . onUploadProgress ;
multipart = request . options . forceMultipart || ( request . options . files != null ) ;
multipart = multipart || _ . any ( request . options . formFields , function ( item ) {
@ -251,13 +252,13 @@ preparePayload = function(request, response, requestState) {
if ( request . options . encodeJSON && containsStreams ) {
return Promise . reject ( ) ( new bhttpErrors . ConflictingOptionsError ( "Sending a JSON-encoded payload containing data from a stream is not currently supported." , void 0 , "Either don't use encodeJSON, or read your stream into a string or Buffer." ) ) ;
}
if ( ( _ ref = request . options . method ) !== "get" && _ ref !== "head" && _ ref !== "delete" ) {
if ( ( ref = request . options . method ) !== "get" && ref !== "head" && ref !== "delete" ) {
if ( ( request . options . encodeJSON || ( request . options . formFields != null ) ) && ! multipart ) {
debugRequest ( "got url-encodable form-data" ) ;
if ( request . options . encodeJSON ) {
debugRequest ( "... but encodeJSON was set, so we will send JSON instead" ) ;
request . options . headers [ "content-type" ] = "application/json" ;
request . payload = JSON . stringify ( ( _ ref1 = request . options . formFields ) != null ? _ ref1 : null ) ;
request . payload = JSON . stringify ( ( ref1 = request . options . formFields ) != null ? ref1 : null ) ;
} else if ( ! _ . isEmpty ( request . options . formFields ) ) {
request . options . headers [ "content-type" ] = "application/x-www-form-urlencoded" ;
request . payload = querystring . stringify ( formFixArray ( request . options . formFields ) ) ;
@ -269,14 +270,14 @@ preparePayload = function(request, response, requestState) {
} else if ( ( request . options . formFields != null ) && multipart ) {
debugRequest ( "got multipart form-data" ) ;
formDataObject = new formData ( ) ;
_ ref2 = formFixArray ( request . options . formFields ) ;
for ( fieldName in _ ref2) {
fieldValue = _ ref2[ fieldName ] ;
ref2 = formFixArray ( request . options . formFields ) ;
for ( fieldName in ref2) {
fieldValue = ref2[ fieldName ] ;
if ( ! _ . isArray ( fieldValue ) ) {
fieldValue = [ fieldValue ] ;
}
for ( _ i = 0 , _ len = fieldValue . length ; _ i < _ len; _ i++ ) {
valueElement = fieldValue [ _ i] ;
for ( i = 0 , len = fieldValue . length ; i < len; i++ ) {
valueElement = fieldValue [ i] ;
if ( valueElement . _bhttpStreamWrapper != null ) {
streamOptions = valueElement . options ;
valueElement = valueElement . stream ;
@ -300,10 +301,10 @@ preparePayload = function(request, response, requestState) {
} else if ( request . options . inputStream != null ) {
debugRequest ( "got inputStream" ) ;
return Promise [ "try" ] ( function ( ) {
var _ ref3;
var ref3;
request . payloadStream = request . options . inputStream ;
if ( ( request . payloadStream . _bhttpStreamWrapper != null ) && ( ( request . payloadStream . options . contentLength != null ) || ( request . payloadStream . options . knownLength != null ) ) ) {
return Promise . resolve ( ( _ ref3 = request . payloadStream . options . contentLength ) != null ? _ ref3 : request . payloadStream . options . knownLength ) ;
return Promise . resolve ( ( ref3 = request . payloadStream . options . contentLength ) != null ? ref3 : request . payloadStream . options . knownLength ) ;
} else {
return streamLength ( request . options . inputStream ) ;
}
@ -338,16 +339,16 @@ preparePayload = function(request, response, requestState) {
prepareCleanup = function ( request , response , requestState ) {
debugRequest ( "preparing cleanup" ) ;
return Promise [ "try" ] ( function ( ) {
var fixedHeaders , key, value , _i , _len , _ref , _ref1 ;
_ ref = [ "query" , "formFields" , "files" , "encodeJSON" , "inputStream" , "inputBuffer" , "discardResponse" , "keepRedirectResponses" , "followRedirects" , "noDecode" , "decodeJSON" , "allowChunkedMultipart" , "forceMultipart" , "onUploadProgress" , "onDownloadProgress" ] ;
for ( _ i = 0 , _ len = _ ref. length ; _ i < _ len; _ i++ ) {
key = _ ref[ _ i] ;
var fixedHeaders , i, key , len , ref , ref1 , value ;
ref = [ "query" , "formFields" , "files" , "encodeJSON" , "inputStream" , "inputBuffer" , "discardResponse" , "keepRedirectResponses" , "followRedirects" , "noDecode" , "decodeJSON" , "allowChunkedMultipart" , "forceMultipart" , "onUploadProgress" , "onDownloadProgress" ] ;
for ( i = 0 , len = ref. length ; i < len; i++ ) {
key = ref[ i] ;
delete request . options [ key ] ;
}
fixedHeaders = { } ;
_ ref1 = request . options . headers ;
for ( key in _ ref1) {
value = _ ref1[ key ] ;
ref1 = request . options . headers ;
for ( key in ref1) {
value = ref1[ key ] ;
fixedHeaders [ key . toLowerCase ( ) ] = value ;
}
request . options . headers = fixedHeaders ;
@ -444,26 +445,26 @@ processResponse = function(request, response, requestState) {
var cookieHeader , promises ;
if ( ( request . cookieJar != null ) && ( response . headers [ "set-cookie" ] != null ) ) {
promises = ( function ( ) {
var _ i, _ len, _ ref, _ results;
_ ref = response . headers [ "set-cookie" ] ;
_ results = [ ] ;
for ( _ i = 0 , _ len = _ ref. length ; _ i < _ len; _ i++ ) {
cookieHeader = _ ref[ _ i] ;
var i, len, ref, results;
ref = response . headers [ "set-cookie" ] ;
results = [ ] ;
for ( i = 0 , len = ref. length ; i < len; i++ ) {
cookieHeader = ref[ i] ;
debugResponse ( "storing cookie: %s" , cookieHeader ) ;
_ results. push ( request . cookieJar . set ( cookieHeader , request . url ) ) ;
results. push ( request . cookieJar . set ( cookieHeader , request . url ) ) ;
}
return _ results;
return results;
} ) ( ) ;
return Promise . all ( promises ) ;
} else {
return Promise . resolve ( ) ;
}
} ) . then ( function ( ) {
var completedBytes , progressStream , totalBytes, _ref , _ref1 ;
var completedBytes , progressStream , ref, ref1 , totalBytes ;
response . request = request ;
response . requestState = requestState ;
response . redirectHistory = requestState . redirectHistory ;
if ( ( ( _ ref = response . statusCode ) === 301 || _ ref === 302 || _ ref === 303 || _ ref === 307 ) && request . responseOptions . followRedirects ) {
if ( ( ( ref = response . statusCode ) === 301 || ref === 302 || ref === 303 || ref === 307 ) && request . responseOptions . followRedirects ) {
if ( requestState . redirectHistory . length >= ( request . responseOptions . redirectLimit - 1 ) ) {
return Promise . reject ( addErrorData ( new bhttpErrors . RedirectError ( "The maximum amount of redirects ({request.responseOptions.redirectLimit}) was reached." ) ) ) ;
}
@ -486,7 +487,7 @@ processResponse = function(request, response, requestState) {
case 303 :
return redirectGet ( request , response , requestState ) ;
case 307 :
if ( request . containsStreams && ( ( _ ref1 = request . options . method ) !== "get" && _ ref1 !== "head" ) ) {
if ( request . containsStreams && ( ( ref1 = request . options . method ) !== "get" && ref1 !== "head" ) ) {
return Promise . reject ( addErrorData ( new bhttpErrors . RedirectError ( "Encountered a 307 redirect for POST, PUT or DELETE, but your payload contained (single-use) streams. We therefore can't automatically follow the redirect." ) , request , response , requestState ) ) ;
} else {
return redirectUnchanged ( request , response , requestState ) ;
@ -511,7 +512,7 @@ processResponse = function(request, response, requestState) {
} ) ;
}
return new Promise ( function ( resolve , reject ) {
var attachProgressStream, _on, _progressStreamAttached , _resume ;
var _on, _progressStreamAttached , _resume , attachProgressStream ;
_resume = response . resume . bind ( response ) ;
_on = response . on . bind ( response ) ;
_progressStreamAttached = false ;
@ -540,8 +541,8 @@ processResponse = function(request, response, requestState) {
return reject ( err ) ;
} ) ;
return response . pipe ( concatStream ( function ( body ) {
var err , _ ref2;
if ( request . responseOptions . decodeJSON || ( ( ( _ ref2 = response . headers [ "content-type" ] ) != null ? _ ref2 : "" ) . split ( ";" ) [ 0 ] === "application/json" && ! request . responseOptions . noDecode ) ) {
var err , ref2;
if ( request . responseOptions . decodeJSON || ( ( ( ref2 = response . headers [ "content-type" ] ) != null ? ref2 : "" ) . split ( ";" ) [ 0 ] === "application/json" && ! request . responseOptions . noDecode ) ) {
try {
response . body = JSON . parse ( body ) ;
} catch ( _error ) {
@ -575,12 +576,12 @@ doPayloadRequest = function(url, data, options, callback) {
redirectGet = function ( request , response , requestState ) {
debugResponse ( "following forced-GET redirect to %s" , response . headers [ "location" ] ) ;
return Promise [ "try" ] ( function ( ) {
var key, options , _i , _len , _ ref;
var i, key , len , options , ref;
options = _ . clone ( requestState . originalOptions ) ;
options . method = "get" ;
_ ref = [ "inputBuffer" , "inputStream" , "files" , "formFields" ] ;
for ( _ i = 0 , _ len = _ ref. length ; _ i < _ len; _ i++ ) {
key = _ ref[ _ i] ;
ref = [ "inputBuffer" , "inputStream" , "files" , "formFields" ] ;
for ( i = 0 , len = ref. length ; i < len; i++ ) {
key = ref[ i] ;
delete options [ key ] ;
}
return doRedirect ( request , response , requestState , options ) ;
@ -674,7 +675,7 @@ bhttpAPI = {
options . method = "patch" ;
return doPayloadRequest . bind ( this ) ( url , data , options , callback ) ;
} ,
"delete" : function ( url , data, options, callback ) {
"delete" : function ( url , options, callback ) {
if ( options == null ) {
options = { } ;
}
@ -690,7 +691,7 @@ bhttpAPI = {
_doRequest : function ( url , options , requestState ) {
return Promise [ "try" ] ( ( function ( _this ) {
return function ( ) {
var re quest, response , _ref ;
var re f, re quest, response ;
request = {
url : url ,
options : _ . clone ( options )
@ -703,7 +704,7 @@ bhttpAPI = {
} ;
}
if ( requestState . sessionOptions == null ) {
requestState . sessionOptions = ( _ ref = _this . _sessionOptions ) != null ? _ ref : { } ;
requestState . sessionOptions = ( ref = _this . _sessionOptions ) != null ? ref : { } ;
}
return prepareRequest ( request , response , requestState ) ;
} ;