2323 */
2424
2525var errors = require ( 'web3-core-helpers' ) . errors ;
26- var XHR2 = require ( 'xhr2-cookies' ) . XMLHttpRequest // jshint ignore: line
26+ var XHR2 = require ( 'xhr2-cookies' ) . XMLHttpRequest ; // jshint ignore: line
2727var http = require ( 'http' ) ;
2828var https = require ( 'https' ) ;
2929
@@ -34,32 +34,38 @@ var https = require('https');
3434var HttpProvider = function HttpProvider ( host , options ) {
3535 options = options || { } ;
3636
37- var keepAlive =
38- ( options . keepAlive === true || options . keepAlive !== false ) ?
39- true :
40- false ;
37+ var keepAlive = ( options . keepAlive === true || options . keepAlive !== false ) ? true : false ;
4138 this . host = host || 'http://localhost:8545' ;
4239 if ( this . host . substring ( 0 , 5 ) === "https" ) {
4340 this . httpsAgent = new https . Agent ( { keepAlive : keepAlive } ) ;
44- } else {
41+ } else {
4542 this . httpAgent = new http . Agent ( { keepAlive : keepAlive } ) ;
4643 }
44+
45+ this . withCredentials = options . withCredentials || false ;
4746 this . timeout = options . timeout || 0 ;
4847 this . headers = options . headers ;
4948 this . connected = false ;
5049} ;
5150
5251HttpProvider . prototype . _prepareRequest = function ( ) {
53- var request = new XHR2 ( ) ;
54- request . nodejsSet ( {
55- httpsAgent :this . httpsAgent ,
56- httpAgent :this . httpAgent
57- } ) ;
52+ var request ;
53+
54+ // the current runtime is a browser
55+ if ( typeof XMLHttpRequest !== 'undefined' ) {
56+ request = new XMLHttpRequest ( ) ;
57+ } else {
58+ request = new XHR2 ( ) ;
59+ request . nodejsSet ( {
60+ httpsAgent :this . httpsAgent ,
61+ httpAgent :this . httpAgent
62+ } ) ;
63+ }
5864
5965 request . open ( 'POST' , this . host , true ) ;
6066 request . setRequestHeader ( 'Content-Type' , 'application/json' ) ;
61- request . timeout = this . timeout && this . timeout !== 1 ? this . timeout : 0 ;
62- request . withCredentials = true ;
67+ request . timeout = this . timeout ;
68+ request . withCredentials = this . withCredentials ;
6369
6470 if ( this . headers ) {
6571 this . headers . forEach ( function ( header ) {
0 commit comments