Skip to content

Commit

Permalink
merged 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Jun 27, 2018
2 parents bd61fed + f98fe14 commit f4bbafe
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 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
2 changes: 1 addition & 1 deletion packages/web3-providers-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"main": "src/index.js",
"dependencies": {
"web3-core-helpers": "1.0.0-beta.34",
"xhr2": "0.1.4"
"xhr2-cookies": "1.1.0"
}
}
12 changes: 7 additions & 5 deletions packages/web3-providers-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@
*/

var errors = require('web3-core-helpers').errors;
var XHR2 = require('xhr2'); // jshint ignore: line
var XHR2 = require('xhr2-cookies').XMLHttpRequest // jshint ignore: line

/**
* 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;
request.withCredentials = true;

if(this.headers) {
this.headers.forEach(function(header) {
Expand All @@ -61,7 +64,6 @@ HttpProvider.prototype.send = function (payload, callback) {
var _this = this;
var request = this._prepareRequest();


request.onreadystatechange = function() {
if (request.readyState === 4 && request.timeout !== 1) {
var result = request.responseText;
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/helpers/FakeXHR2.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ FakeXHR2.prototype.send = function (payload) {
}
};

module.exports = FakeXHR2;
module.exports = {XMLHttpRequest: FakeXHR2};
4 changes: 2 additions & 2 deletions test/httpprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var SandboxedModule = require('sandboxed-module');
SandboxedModule.registerBuiltInSourceTransformer('istanbul');
var HttpProvider = SandboxedModule.require('../packages/web3-providers-http', {
requires: {
'xhr2': require('./helpers/FakeXHR2'),
'xhr2-cookies': require('./helpers/FakeXHR2'),
// 'xmlhttprequest': require('./helpers/FakeXMLHttpRequest')
},
singleOnly: true
Expand All @@ -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 f4bbafe

Please sign in to comment.