Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebsocketProvider improvements #2625

Merged
merged 10 commits into from
Apr 1, 2019
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