nl_tokens: createRequest is also called by the constructor after processOptions . This actually makes the request and processes the response so createRequest is a bit of a misnomer .
pl_tokens: function ( request ) { var timeout ; request . log . debug ( "Creating request .." ) ; request . log . debug ( request ) ; var reqParams = { host : request . host , port : request . port , method : request . method , path : request . path + ( request . query ? '?' + request . query : "" ) , headers : request . getHeaders ( ) , // Node's HTTP/S modules will ignore this, but we are using the // browserify-http module in the browser for both HTTP and HTTPS, and this // is how you differentiate the two. scheme : request . scheme , // Use a provided agent.  'Undefined' is the default, which uses a global // agent. agent : request . agent } ; if ( request . logCurl ) { logCurl ( request ) ; } var http = request . scheme == "http" ? HTTP : HTTPS ; // Set up the real request using the selected library. The request won't be // sent until we call `.end()`. request . _raw = http . request ( reqParams , function ( response ) { request . log . <mask> ( "Received response .." ) ; // We haven't timed out and we have a response, so make sure we clear the // timeout so it doesn't fire while we're processing the response. clearTimeout ( timeout ) ; // Construct a Shred `Response` object from the response. This will stream // the response, thus the need for the callback. We can access the response // entity safely once we're in the callback. response = new Response ( response , request , function ( response ) { // Set up some event magic. The precedence is given first to // status-specific handlers, then to responses for a given event, and then // finally to the more general `response` handler. In the last case, we // need to first make sure we're not dealing with a a redirect. var emit = function ( event ) { var emitter = request . emitter ; var textStatus = STATUS_CODES [ response . status ] ? STATUS_CODES [ response . status ] . toLowerCase ( ) : null ; if ( emitter . listeners ( response . status ) . length > 0 || emitter . listeners ( textStatus ) . length > 0 ) { emitter . emit ( response . status , response ) ; emitter . emit ( textStatus , response ) ; } else { if ( emitter . listeners ( event ) . length > 0 ) { emitter . emit ( event , response ) ; } else if ( ! response . isRedirect ) { emitter . emit ( "response" , response ) ; //console.warn("Request has no event listener for status code " + response.status); } } } ; // Next, check for a redirect. We simply repeat the request with the URL // given in the `Location` header. We fire a `redirect` event. if ( response . isRedirect ) { request . log . debug ( "Redirecting to " + response . getHeader ( "Location" ) ) ; request . url = response . getHeader ( "Location" ) ; emit ( "redirect" ) ; createRequest ( request ) ; // Okay, it's not a redirect. Is it an error of some kind? } else if ( response . isError ) { emit ( "error" ) ; } else { // It looks like we're good shape. Trigger the `success` event. emit ( "success" ) ; } } ) ; } ) ; // We're still setting up the request. Next, we're going to handle error cases // where we have no response. We don't emit an error event because that event // takes a response. We don't response handlers to have to check for a null // value. However, we [should introduce a different event // type](https://github.com/spire-io/shred/issues/3) for this type of error. request . _raw . on ( "error" , function ( error ) { request . emitter . emit ( "request_error" , error ) ; } ) ; request . _raw . on ( "socket" , function ( socket ) { request . emitter . emit ( "socket" , socket ) ; } ) ; // TCP timeouts should also trigger the "response_error" event. request . _raw . on ( 'socket' , function ( ) { request . _raw . socket . on ( 'timeout' , function ( ) { // This should trigger the "error" event on the raw request, which will // trigger the "response_error" on the shred request. request . _raw . abort ( ) ; } ) ; } ) ; // We're almost there. Next, we need to write the request entity to the // underlying request object. if ( request . content ) { request . log . debug ( "Streaming body: '" + request . content . data . slice ( 0 , 59 ) + "' ... " ) ; request . _raw . write ( request . content . data ) ; } // Finally, we need to set up the timeout. We do this last so that we don't // start the clock ticking until the last possible moment. if ( request . timeout ) { timeout = setTimeout ( function ( ) { request . log . debug ( "Timeout fired, aborting request ..." ) ; request . _raw . abort ( ) ; request . emitter . emit ( "timeout" , request ) ; } , request . timeout ) ; } // The `.end()` method will cause the request to fire. Technically, it might // have already sent the headers and body. request . log . debug ( "Sending request ..." ) ; request . _raw . end ( ) ; }