Skip to content

Commit

Permalink
Merge pull request #2995 from ethereum/enhancement/websocket-dependency
Browse files Browse the repository at this point in the history
websocket dependency updated
  • Loading branch information
nivida authored Aug 4, 2019
2 parents b92926c + 5c25af5 commit 0b3b6a9
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 25 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
20 changes: 13 additions & 7 deletions packages/web3-providers/src/factories/ProvidersModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
14 changes: 7 additions & 7 deletions packages/web3-providers/src/providers/WebsocketProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -48,7 +48,7 @@ export default class WebsocketProvider extends AbstractSocketProvider {
this.emit('reconnected');
}

await super.onConnect()
await super.onConnect();

this.reconnecting = false;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ export default class WebsocketProvider extends AbstractSocketProvider {

this.connection = connection;
this.registerEventListeners();
}, this.reconnectionTimeout);
}, this.reconnectDelay);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ describe('BatchRequestTest', () => {
batchRequest.add(abstractMethodMock);

batchRequest.execute();

});

it('calls execute and returns a rejected promise because of the provider', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -450,7 +450,6 @@ describe('WebsocketProviderTest', () => {

it('calls onConnect after the connection got lost', (done) => {
websocketProvider.on('reconnected', () => {

done();
});

Expand Down
4 changes: 3 additions & 1 deletion packages/web3-providers/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0b3b6a9

Please sign in to comment.