Skip to content

Commit 45707eb

Browse files
nividanachomazzara
authored andcommitted
HttpProvider: CORS issue with Firefox and Safari (web3#3112)
* CORS error fixed
1 parent d156289 commit 45707eb

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ Released with 1.0.0-beta.37 code base.
6565
- Fix incorrectly populating chainId param with `net_version` when signing txs (#2378)
6666
- regeneratorRuntime error fixed (#3058)
6767
- Fix accessing event.name where event is undefined (#3014)
68+
- HttpProvider: CORS issue with Firefox and Safari (#2978)

dist/web3.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/web3-providers-http/src/index.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
var 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
2727
var http = require('http');
2828
var https = require('https');
2929

@@ -34,32 +34,38 @@ var https = require('https');
3434
var 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

5251
HttpProvider.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

Comments
 (0)