From 427bb94989e496cfb1f21db0e7bf87b03ede9421 Mon Sep 17 00:00:00 2001 From: nivida Date: Fri, 2 Aug 2019 23:42:46 +0200 Subject: [PATCH 1/6] ProvidersModuleFactory updated --- packages/web3-providers/package.json | 2 +- .../src/factories/ProvidersModuleFactory.js | 32 ++++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/web3-providers/package.json b/packages/web3-providers/package.json index 2e4f9c20078..a91684633fc 100644 --- a/packages/web3-providers/package.json +++ b/packages/web3-providers/package.json @@ -28,7 +28,7 @@ "web3-core-helpers": "2.0.0-alpha", "web3-core-method": "2.0.0-alpha", "web3-utils": "2.0.0-alpha", - "websocket": "^1.0.28", + "websocket": "github:web3-js/WebSocket-Node#polyfill/globalThis", "xhr2-cookies": "1.1.0" }, "devDependencies": { diff --git a/packages/web3-providers/src/factories/ProvidersModuleFactory.js b/packages/web3-providers/src/factories/ProvidersModuleFactory.js index 123398668a5..70226d61fcc 100644 --- a/packages/web3-providers/src/factories/ProvidersModuleFactory.js +++ b/packages/web3-providers/src/factories/ProvidersModuleFactory.js @@ -121,24 +121,32 @@ export default class ProvidersModuleFactory { * @returns {WebsocketProvider} */ createWebsocketProvider(url, options = {}) { - let connection = ''; + let authToken; + let headers = options.headers || {}; + const urlObject = new URL(url); // runtime is of type node - if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) { - let headers = options.headers || {}; - const urlObject = new URL(url); - - if (!headers.authorization && urlObject.username && urlObject.password) { - const authToken = Buffer.from(`${urlObject.username}:${urlObject.password}`).toString('base64'); - headers.authorization = `Basic ${authToken}`; + if (!headers.authorization && urlObject.username && urlObject.password) { + if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) { + authToken = Buffer.from(`${urlObject.username}:${urlObject.password}`).toString('base64'); + } else { + authToken = btoa(`${urlObject.username}:${urlObject.password}`); } - connection = new W3CWebsocket(url, options.protocol, null, headers, options.requestOptions, options.clientConfig); - } else { - connection = new window.WebSocket(url, options.protocol); + headers.authorization = `Basic ${authToken}`; } - return new WebsocketProvider(connection, options.timeout); + return new WebsocketProvider( + new W3CWebsocket( + url, + options.protocol, + null, + headers, + options.requestOptions, + options.clientConfig + ), + options.timeout + ); } /** From ebb94c1da21a61db4aaf426047d2694daf1b2a03 Mon Sep 17 00:00:00 2001 From: nivida Date: Fri, 2 Aug 2019 23:57:18 +0200 Subject: [PATCH 2/6] constructors checked and ProvidersModuleFactory updated --- .../src/factories/ProvidersModuleFactory.js | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/web3-providers/src/factories/ProvidersModuleFactory.js b/packages/web3-providers/src/factories/ProvidersModuleFactory.js index 70226d61fcc..0c6967e61d0 100644 --- a/packages/web3-providers/src/factories/ProvidersModuleFactory.js +++ b/packages/web3-providers/src/factories/ProvidersModuleFactory.js @@ -121,31 +121,28 @@ export default class ProvidersModuleFactory { * @returns {WebsocketProvider} */ createWebsocketProvider(url, options = {}) { - let authToken; - let headers = options.headers || {}; - const urlObject = new URL(url); - // runtime is of type node - if (!headers.authorization && urlObject.username && urlObject.password) { - if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) { - authToken = Buffer.from(`${urlObject.username}:${urlObject.password}`).toString('base64'); - } else { - authToken = btoa(`${urlObject.username}:${urlObject.password}`); - } + if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) { + let headers = options.headers || {}; + const urlObject = new URL(url); - headers.authorization = `Basic ${authToken}`; + if (!headers.authorization && urlObject.username && urlObject.password) { + const authToken = Buffer.from(`${urlObject.username}:${urlObject.password}`).toString('base64'); + headers.authorization = `Basic ${authToken}`; + } } return new WebsocketProvider( new W3CWebsocket( url, options.protocol, - null, + options.origin, headers, options.requestOptions, options.clientConfig ), - options.timeout + options.timeout, + options.reconnectionTimeout ); } From 26884a90aedf4bcd734e271b08660bef16c69d85 Mon Sep 17 00:00:00 2001 From: nivida Date: Sat, 3 Aug 2019 00:01:29 +0200 Subject: [PATCH 3/6] CHANGELOG.md updated --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1a56c0cf1a..cad46588261 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,12 +66,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Length check of the PK added to the ``fromPrivateKey`` method of the ``Account`` model (#2928) -- WebsocketProvider options extended with ``requestOptions`` (#2938) +- WebsocketProvider options extended with ``requestOptions`` and ``origins`` (#2938, #2995) - ``changed`` listener added to Contract event subscriptions (#2960) ### Changed - fsevents bumbed to v1.2.9 (#2951) +- ``websocket`` dependency changed to github fork (#2995) ### Fixed From 7665a640b502accf6e7d8f543c286d7c7892ffce Mon Sep 17 00:00:00 2001 From: nivida Date: Sat, 3 Aug 2019 00:33:01 +0200 Subject: [PATCH 4/6] ProvidersModuleFactoryTest updated and linter executed --- .../src/factories/ProvidersModuleFactory.js | 3 ++- .../web3-providers/src/providers/Web3EthereumProvider.js | 2 +- .../web3-providers/src/providers/WebsocketProvider.js | 2 +- .../tests/src/batch-request/BatchRequestTest.js | 1 - .../tests/src/factories/ProvidersModuleFactoryTest.js | 3 ++- .../tests/src/providers/Web3EthereumProviderTest.js | 8 ++++++-- .../tests/src/providers/WebsocketProviderTest.js | 3 +-- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/web3-providers/src/factories/ProvidersModuleFactory.js b/packages/web3-providers/src/factories/ProvidersModuleFactory.js index 0c6967e61d0..c848fa5f31b 100644 --- a/packages/web3-providers/src/factories/ProvidersModuleFactory.js +++ b/packages/web3-providers/src/factories/ProvidersModuleFactory.js @@ -121,9 +121,10 @@ export default class ProvidersModuleFactory { * @returns {WebsocketProvider} */ createWebsocketProvider(url, options = {}) { + let headers = options.headers || {}; + // runtime is of type node if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) { - let headers = options.headers || {}; const urlObject = new URL(url); if (!headers.authorization && urlObject.username && urlObject.password) { diff --git a/packages/web3-providers/src/providers/Web3EthereumProvider.js b/packages/web3-providers/src/providers/Web3EthereumProvider.js index 3e60d5a3a7a..b5772cf610e 100644 --- a/packages/web3-providers/src/providers/Web3EthereumProvider.js +++ b/packages/web3-providers/src/providers/Web3EthereumProvider.js @@ -88,7 +88,7 @@ export default class Web3EthereumProvider extends AbstractSocketProvider { this.removeAllListeners(this.SOCKET_ACCOUNTS_CHANGED); this.removeAllListeners(this.SOCKET_NETWORK_CHANGED); - super.removeAllSocketListeners() + super.removeAllSocketListeners(); } /** diff --git a/packages/web3-providers/src/providers/WebsocketProvider.js b/packages/web3-providers/src/providers/WebsocketProvider.js index 19d99e706d5..ba071dafec1 100644 --- a/packages/web3-providers/src/providers/WebsocketProvider.js +++ b/packages/web3-providers/src/providers/WebsocketProvider.js @@ -48,7 +48,7 @@ export default class WebsocketProvider extends AbstractSocketProvider { this.emit('reconnected'); } - await super.onConnect() + await super.onConnect(); this.reconnecting = false; } diff --git a/packages/web3-providers/tests/src/batch-request/BatchRequestTest.js b/packages/web3-providers/tests/src/batch-request/BatchRequestTest.js index 5ba8d97dd0a..953536b6fbc 100644 --- a/packages/web3-providers/tests/src/batch-request/BatchRequestTest.js +++ b/packages/web3-providers/tests/src/batch-request/BatchRequestTest.js @@ -136,7 +136,6 @@ describe('BatchRequestTest', () => { batchRequest.add(abstractMethodMock); batchRequest.execute(); - }); it('calls execute and returns a rejected promise because of the provider', async () => { diff --git a/packages/web3-providers/tests/src/factories/ProvidersModuleFactoryTest.js b/packages/web3-providers/tests/src/factories/ProvidersModuleFactoryTest.js index 7f168d9329c..67c6539f6c1 100644 --- a/packages/web3-providers/tests/src/factories/ProvidersModuleFactoryTest.js +++ b/packages/web3-providers/tests/src/factories/ProvidersModuleFactoryTest.js @@ -67,7 +67,8 @@ describe('ProvidersModuleFactoryTest', () => { providersModuleFactory.createWebsocketProvider('ws://username:password@hallo:5544', { protocol: 'string', clientConfig: 'string', - requestOptions: null, + requestOptions: null, + origin: null }) ).toBeInstanceOf(WebsocketProvider); diff --git a/packages/web3-providers/tests/src/providers/Web3EthereumProviderTest.js b/packages/web3-providers/tests/src/providers/Web3EthereumProviderTest.js index 4b9d89f05b3..f3125a14d84 100644 --- a/packages/web3-providers/tests/src/providers/Web3EthereumProviderTest.js +++ b/packages/web3-providers/tests/src/providers/Web3EthereumProviderTest.js @@ -139,10 +139,14 @@ describe('Web3EthereumProviderTest', () => { ethereumProvider.removeAllSocketListeners(); expect(socketMock.removeListener).toHaveBeenNthCalledWith( - 1, 'accountsChanged', ethereumProvider.onAccountsChanged + 1, + 'accountsChanged', + ethereumProvider.onAccountsChanged ); expect(socketMock.removeListener).toHaveBeenNthCalledWith( - 2, 'networkChanged', ethereumProvider.onNetworkChanged + 2, + 'networkChanged', + ethereumProvider.onNetworkChanged ); }); diff --git a/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js b/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js index ac5ee7ac330..f3c59b52e97 100644 --- a/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js +++ b/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js @@ -22,7 +22,7 @@ describe('WebsocketProviderTest', () => { expect(websocketProvider.connection).toEqual(socketMock); expect(websocketProvider.timeout).toEqual(1); - + expect(websocketProvider.reconnectionTimeout).toEqual(1); expect(socketMock.addEventListener.mock.calls[0][0]).toEqual('message'); @@ -450,7 +450,6 @@ describe('WebsocketProviderTest', () => { it('calls onConnect after the connection got lost', (done) => { websocketProvider.on('reconnected', () => { - done(); }); From a563af0df2b574eec91ec3d0ab330e421988cff9 Mon Sep 17 00:00:00 2001 From: nivida Date: Sat, 3 Aug 2019 14:39:51 +0200 Subject: [PATCH 5/6] WebsocketProviderOptions interface updated --- packages/web3-providers/types/index.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/web3-providers/types/index.d.ts b/packages/web3-providers/types/index.d.ts index 85b337aabf4..51f9714e6d4 100644 --- a/packages/web3-providers/types/index.d.ts +++ b/packages/web3-providers/types/index.d.ts @@ -177,8 +177,10 @@ export interface HttpProviderOptions { export interface WebsocketProviderOptions { host?: string; timeout?: number; + reconnectionTimeout?: number; headers?: {}; protocol?: string; clientConfig?: string; - requestOptions?: object + requestOptions?: object, + origin?: string } From 5c25af5886bd27ff670649dfb779713b5a7bfacc Mon Sep 17 00:00:00 2001 From: nivida Date: Sat, 3 Aug 2019 14:48:24 +0200 Subject: [PATCH 6/6] reconnectionTimeout renamed to reconnectDelay --- .../src/factories/ProvidersModuleFactory.js | 2 +- .../src/providers/WebsocketProvider.js | 12 ++++++------ .../tests/src/providers/WebsocketProviderTest.js | 2 +- packages/web3-providers/types/index.d.ts | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/web3-providers/src/factories/ProvidersModuleFactory.js b/packages/web3-providers/src/factories/ProvidersModuleFactory.js index c848fa5f31b..69005963965 100644 --- a/packages/web3-providers/src/factories/ProvidersModuleFactory.js +++ b/packages/web3-providers/src/factories/ProvidersModuleFactory.js @@ -143,7 +143,7 @@ export default class ProvidersModuleFactory { options.clientConfig ), options.timeout, - options.reconnectionTimeout + options.reconnectDelay ); } diff --git a/packages/web3-providers/src/providers/WebsocketProvider.js b/packages/web3-providers/src/providers/WebsocketProvider.js index ba071dafec1..a0dca04fa0d 100644 --- a/packages/web3-providers/src/providers/WebsocketProvider.js +++ b/packages/web3-providers/src/providers/WebsocketProvider.js @@ -26,15 +26,15 @@ import isArray from 'lodash/isArray'; export default class WebsocketProvider extends AbstractSocketProvider { /** * @param {WebSocket} connection - * @param {Number} responseTimeout - * @param {Number} reconnectionTimeout + * @param {Number} timeout + * @param {Number} reconnectDelay * * @constructor */ - constructor(connection, responseTimeout, reconnectionTimeout = 5000) { - super(connection, responseTimeout); + constructor(connection, timeout, reconnectDelay = 5000) { + super(connection, timeout); this.host = this.connection.url; - this.reconnectionTimeout = reconnectionTimeout; + this.reconnectDelay = reconnectDelay; this.reconnecting = false; } @@ -126,7 +126,7 @@ export default class WebsocketProvider extends AbstractSocketProvider { this.connection = connection; this.registerEventListeners(); - }, this.reconnectionTimeout); + }, this.reconnectDelay); } /** diff --git a/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js b/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js index f3c59b52e97..1da19b629ec 100644 --- a/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js +++ b/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js @@ -23,7 +23,7 @@ describe('WebsocketProviderTest', () => { expect(websocketProvider.timeout).toEqual(1); - expect(websocketProvider.reconnectionTimeout).toEqual(1); + expect(websocketProvider.reconnectDelay).toEqual(1); expect(socketMock.addEventListener.mock.calls[0][0]).toEqual('message'); expect(socketMock.addEventListener.mock.calls[0][1]).toBeInstanceOf(Function); diff --git a/packages/web3-providers/types/index.d.ts b/packages/web3-providers/types/index.d.ts index 51f9714e6d4..b45e9cc221a 100644 --- a/packages/web3-providers/types/index.d.ts +++ b/packages/web3-providers/types/index.d.ts @@ -177,7 +177,7 @@ export interface HttpProviderOptions { export interface WebsocketProviderOptions { host?: string; timeout?: number; - reconnectionTimeout?: number; + reconnectDelay?: number; headers?: {}; protocol?: string; clientConfig?: string;