From c0e727eac7c16826c39fd43a03a4930b5ff6fff8 Mon Sep 17 00:00:00 2001 From: Fabian Vogelsteller Date: Thu, 19 Apr 2018 15:27:40 +0200 Subject: [PATCH] fixed http provider --- packages/web3-providers-http/README.md | 6 +++++- packages/web3-providers-http/src/index.js | 7 ++++--- packages/web3-providers-ws/src/index.js | 4 ++-- test/httpprovider.js | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/web3-providers-http/README.md b/packages/web3-providers-http/README.md index 528e883fb71..ac46853390c 100644 --- a/packages/web3-providers-http/README.md +++ b/packages/web3-providers-http/README.md @@ -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); ``` diff --git a/packages/web3-providers-http/src/index.js b/packages/web3-providers-http/src/index.js index 074ad2f918a..49113011421 100644 --- a/packages/web3-providers-http/src/index.js +++ b/packages/web3-providers-http/src/index.js @@ -28,11 +28,12 @@ var XHR2 = require('xhr2'); // 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(){ diff --git a/packages/web3-providers-ws/src/index.js b/packages/web3-providers-ws/src/index.js index c1c1cbb038b..915e833dc6f 100644 --- a/packages/web3-providers-ws/src/index.js +++ b/packages/web3-providers-ws/src/index.js @@ -42,9 +42,9 @@ if (typeof window !== 'undefined' && typeof window.WebSocket !== 'undefined') { var url = require('url'); if (url.URL) { // Use the new Node 6+ API for parsing URLs that supports username/password - var URL = url.URL; + var newURL = url.URL; parseURL = function(url) { - return new URL(url); + return new newURL(url); }; } else { diff --git a/test/httpprovider.js b/test/httpprovider.js index 590d2698242..2c2161a3ed6 100644 --- a/test/httpprovider.js +++ b/test/httpprovider.js @@ -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');