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 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 621e1711015..69005963965 100644 --- a/packages/web3-providers/src/factories/ProvidersModuleFactory.js +++ b/packages/web3-providers/src/factories/ProvidersModuleFactory.js @@ -121,24 +121,30 @@ export default class ProvidersModuleFactory { * @returns {WebsocketProvider} */ createWebsocketProvider(url, options = {}) { - let connection = ''; + 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) { const authToken = Buffer.from(`${urlObject.username}:${urlObject.password}`).toString('base64'); headers.authorization = `Basic ${authToken}`; } - - connection = new W3CWebsocket(url, options.protocol, null, headers, options.requestOptions, options.clientConfig); - } else { - connection = new window.WebSocket(url, options.protocol); } - return new WebsocketProvider(connection, options.timeout, options.reconnectionTimeout); + return new WebsocketProvider( + new W3CWebsocket( + url, + options.protocol, + options.origin, + headers, + options.requestOptions, + options.clientConfig + ), + options.timeout, + options.reconnectDelay + ); } /** 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..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; } @@ -48,7 +48,7 @@ export default class WebsocketProvider extends AbstractSocketProvider { this.emit('reconnected'); } - await super.onConnect() + await super.onConnect(); 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/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..1da19b629ec 100644 --- a/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js +++ b/packages/web3-providers/tests/src/providers/WebsocketProviderTest.js @@ -22,8 +22,8 @@ describe('WebsocketProviderTest', () => { expect(websocketProvider.connection).toEqual(socketMock); 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); @@ -450,7 +450,6 @@ describe('WebsocketProviderTest', () => { it('calls onConnect after the connection got lost', (done) => { websocketProvider.on('reconnected', () => { - done(); }); diff --git a/packages/web3-providers/types/index.d.ts b/packages/web3-providers/types/index.d.ts index 85b337aabf4..b45e9cc221a 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; + reconnectDelay?: number; headers?: {}; protocol?: string; clientConfig?: string; - requestOptions?: object + requestOptions?: object, + origin?: string }