Skip to content

Commit

Permalink
Merge branch '1.0' into 1.0ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Apr 19, 2018
2 parents 39016c9 + c0e727e commit 3d74aa6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/web3-providers-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ This will expose the `Web3HttpProvider` object on the window object.
// in node.js
var Web3HttpProvider = require('web3-providers-http');

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


Expand Down
8 changes: 5 additions & 3 deletions packages/web3-providers-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ var XHR2 = require('xhr2');
/**
* HttpProvider should be used to send rpc calls over http
*/
var HttpProvider = function HttpProvider(host, timeout, headers) {
var HttpProvider = function HttpProvider(host, options) {
options = options || {};
this.host = host || 'http://localhost:8545';
this.timeout = timeout || 0;
this.timeout = options.timeout || 0;
this.headers = options.headers;
this.connected = false;
this.headers = headers;
};

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

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

if(this.headers) {
this.headers.forEach(function(header) {
Expand Down
19 changes: 15 additions & 4 deletions packages/web3-providers-ws/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var errors = require('web3-core-helpers').errors;
var Ws = null;
var _btoa = null;
var parseURL = null;
if (typeof window !== 'undefined') {
if (typeof window !== 'undefined' && typeof window.WebSocket !== 'undefined') {
Ws = window.WebSocket;
_btoa = btoa;
parseURL = function(url) {
Expand All @@ -39,8 +39,18 @@ if (typeof window !== 'undefined') {
_btoa = function(str) {
return Buffer(str).toString('base64');
};
// Web3 supports Node.js 5, so we need to use the legacy URL API
parseURL = require('url').parse;
var url = require('url');
if (url.URL) {
// Use the new Node 6+ API for parsing URLs that supports username/password
var newURL = url.URL;
parseURL = function(url) {
return new newURL(url);
};
}
else {
// Web3 supports Node.js 5, so fall back to the legacy URL API if necessary
parseURL = require('url').parse;
}
}
// Default connection ws://localhost:8546

Expand All @@ -60,11 +70,12 @@ var WebsocketProvider = function WebsocketProvider(url, options) {
// pass through with any additional headers supplied in constructor
var parsedURL = parseURL(url);
var headers = options.headers || {};
var protocol = options.protocol || undefined;
if (parsedURL.username && parsedURL.password) {
headers.authorization = 'Basic ' + _btoa(parsedURL.username + ':' + parsedURL.password);
}

this.connection = new Ws(url, undefined, undefined, headers);
this.connection = new Ws(url, protocol, undefined, headers);

this.addDefaultEvents();

Expand Down
2 changes: 1 addition & 1 deletion test/httpprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var HttpProvider = SandboxedModule.require('../packages/web3-providers-http', {
describe('web3-providers-http', function () {
describe('prepareRequest', function () {
it('should set request header', function () {
var provider = new HttpProvider('http://localhost:8545', 0 , [{name: 'Access-Control-Allow-Origin', value: '*'}]);
var provider = new HttpProvider('http://localhost:8545', {headers: [{name: 'Access-Control-Allow-Origin', value: '*'}]});
var result = provider._prepareRequest();

assert.equal(typeof result, 'object');
Expand Down

0 comments on commit 3d74aa6

Please sign in to comment.