Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit ac41383

Browse files
authored
Merge branch '1.0' into 1.0-csp-fix
2 parents 11c3001 + c0e727e commit ac41383

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

packages/web3-providers-http/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ This will expose the `Web3HttpProvider` object on the window object.
3131
// in node.js
3232
var Web3HttpProvider = require('web3-providers-http');
3333

34-
var http = new Web3HttpProvider('http://localhost:8545');
34+
var options = {
35+
timeout: 20000, // milliseconds,
36+
headers: [{name: 'Access-Control-Allow-Origin', value: '*'},{...}]
37+
};
38+
var http = new Web3HttpProvider('http://localhost:8545', options);
3539
```
3640

3741

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ var XHR2 = require('xhr2'); // jshint ignore: line
2828
/**
2929
* HttpProvider should be used to send rpc calls over http
3030
*/
31-
var HttpProvider = function HttpProvider(host, timeout, headers) {
31+
var HttpProvider = function HttpProvider(host, options) {
32+
options = options || {};
3233
this.host = host || 'http://localhost:8545';
33-
this.timeout = timeout || 0;
34+
this.timeout = options.timeout || 0;
35+
this.headers = options.headers;
3436
this.connected = false;
35-
this.headers = headers;
3637
};
3738

3839
HttpProvider.prototype._prepareRequest = function(){
3940
var request = new XHR2();
4041

4142
request.open('POST', this.host, true);
4243
request.setRequestHeader('Content-Type','application/json');
44+
request.timeout = this.timeout && this.timeout !== 1 ? this.timeout : 0;
4345

4446
if(this.headers) {
4547
this.headers.forEach(function(header) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ if (typeof window !== 'undefined' && typeof window.WebSocket !== 'undefined') {
4242
var url = require('url');
4343
if (url.URL) {
4444
// Use the new Node 6+ API for parsing URLs that supports username/password
45-
var URL = url.URL;
45+
var newURL = url.URL;
4646
parseURL = function(url) {
47-
return new URL(url);
47+
return new newURL(url);
4848
};
4949
}
5050
else {
@@ -70,11 +70,12 @@ var WebsocketProvider = function WebsocketProvider(url, options) {
7070
// pass through with any additional headers supplied in constructor
7171
var parsedURL = parseURL(url);
7272
var headers = options.headers || {};
73+
var protocol = options.protocol || undefined;
7374
if (parsedURL.username && parsedURL.password) {
7475
headers.authorization = 'Basic ' + _btoa(parsedURL.username + ':' + parsedURL.password);
7576
}
7677

77-
this.connection = new Ws(url, undefined, undefined, headers);
78+
this.connection = new Ws(url, protocol, undefined, headers);
7879

7980
this.addDefaultEvents();
8081

test/httpprovider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var HttpProvider = SandboxedModule.require('../packages/web3-providers-http', {
1414
describe('web3-providers-http', function () {
1515
describe('prepareRequest', function () {
1616
it('should set request header', function () {
17-
var provider = new HttpProvider('http://localhost:8545', 0 , [{name: 'Access-Control-Allow-Origin', value: '*'}]);
17+
var provider = new HttpProvider('http://localhost:8545', {headers: [{name: 'Access-Control-Allow-Origin', value: '*'}]});
1818
var result = provider._prepareRequest();
1919

2020
assert.equal(typeof result, 'object');

0 commit comments

Comments
 (0)