Skip to content

Commit

Permalink
Merge pull request #2625 from ethereum/fix/websocket-provider
Browse files Browse the repository at this point in the history
WebsocketProvider improvements
  • Loading branch information
nivida authored Apr 1, 2019
2 parents 549af26 + 5dbc214 commit 824befc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
/**
* @file AbstractSocketProvider
* @author Samuel Furter <samuel@ethereum.org>, Fabian Vogelsteller <fabian@ethereum.org>
* @author Samuel Furter <samuel@ethereum.org>
* @date 2018
*/

Expand Down Expand Up @@ -192,7 +192,7 @@ export default class AbstractSocketProvider extends EventEmitter {

delete this.subscriptions[subscriptionId];

this.subscriptions[this.getSubscriptionEvent(this.subscriptions[key].id)].id = subscriptionId;
this.subscriptions[key].id = subscriptionId;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,12 @@ export default class ProvidersModuleFactory {

// runtime is of type node
if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) {
let authToken;

let headers = options.headers || {};

const urlObject = new URL(url);

if (urlObject.username && urlObject.password) {
authToken = Buffer.from(`${urlObject.username}:${urlObject.password}`).toString('base64');
if (!headers.authorization && urlObject.username && urlObject.password) {
const authToken = Buffer.from(`${urlObject.username}:${urlObject.password}`).toString('base64');
headers.authorization = `Basic ${authToken}`;
} else if (urlObject.auth) {
headers.authorization = Buffer.from(urlObject.auth, 'base64');
}

connection = new W3CWebsocket(url, options.protocol, null, headers, null, options.clientConfig);
Expand Down
5 changes: 2 additions & 3 deletions packages/web3-providers/src/providers/WebsocketProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class WebsocketProvider extends AbstractSocketProvider {
* @param {CloseEvent} closeEvent
*/
onClose(closeEvent) {
if (closeEvent.code !== 1000) {
if (closeEvent.code !== 1000 || closeEvent.wasClean === false) {
this.reconnect();

return;
Expand Down Expand Up @@ -101,8 +101,7 @@ export default class WebsocketProvider extends AbstractSocketProvider {
this.connection._client.config
);
} else {
const protocol = this.connection.protocol || undefined;
connection = new this.connection.constructor(this.host, protocol);
connection = new this.connection.constructor(this.host, this.connection.protocol || undefined);
}

this.connection = connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,43 @@ describe('WebsocketProviderTest', () => {
5020
);

it(
'calls onClose with wasClean false',
(done) => {
const event = {code: 1000, wasClean: false};

setTimeout(() => {
expect(socketMock.addEventListener.mock.calls[0][0]).toEqual('message');
expect(socketMock.addEventListener.mock.calls[0][1]).toBeInstanceOf(Function);

expect(socketMock.addEventListener.mock.calls[1][0]).toEqual('open');
expect(socketMock.addEventListener.mock.calls[1][1]).toBeInstanceOf(Function);

expect(socketMock.addEventListener.mock.calls[2][0]).toEqual('open');
expect(socketMock.addEventListener.mock.calls[2][1]).toBeInstanceOf(Function);

expect(socketMock.addEventListener.mock.calls[3][0]).toEqual('close');
expect(socketMock.addEventListener.mock.calls[3][1]).toBeInstanceOf(Function);

expect(socketMock.addEventListener.mock.calls[4][0]).toEqual('error');
expect(socketMock.addEventListener.mock.calls[4][1]).toBeInstanceOf(Function);

expect(socketMock.host).toEqual('host');

expect(socketMock.protocol).toEqual('protocol');

expect(socketMock.removeEventListener).toHaveBeenCalled();

expect(websocketProvider.connection).toBeInstanceOf(Websocket);

done();
}, 5010);

websocketProvider.onClose(event);
},
5020
);

it(
'calls reconnect with an WebSocket connection',
(done) => {
Expand Down

0 comments on commit 824befc

Please sign in to comment.