Don't clobber global space in WS provider (#1922)
* Don't clbber global space in WS provider * Re-use getWSClass
This commit is contained in:
@@ -5,13 +5,12 @@
|
||||
|
||||
import { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitted, ProviderInterfaceEmitCb } from '../types';
|
||||
|
||||
import './polyfill';
|
||||
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { assert, isNull, isUndefined, logger } from '@polkadot/util';
|
||||
|
||||
import Coder from '../coder';
|
||||
import defaults from '../defaults';
|
||||
import getWSClass from './getWSClass';
|
||||
|
||||
type CallbackHandler = (error?: null | Error, value?: any) => void;
|
||||
|
||||
@@ -122,9 +121,11 @@ export default class WsProvider implements WSProviderInterface {
|
||||
* @description The [[WsProvider]] connects automatically by default, however if you decided otherwise, you may
|
||||
* connect manually using this method.
|
||||
*/
|
||||
public connect (): void {
|
||||
public async connect (): Promise<void> {
|
||||
try {
|
||||
this.#websocket = new WebSocket(this.#endpoint);
|
||||
const WS = await getWSClass();
|
||||
|
||||
this.#websocket = new WS(this.#endpoint);
|
||||
this.#websocket.onclose = this.#onSocketClose;
|
||||
this.#websocket.onerror = this.#onSocketError;
|
||||
this.#websocket.onmessage = this.#onSocketMessage;
|
||||
|
||||
+10
-7
@@ -5,10 +5,11 @@
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import { Global } from './../mock/types';
|
||||
import getWSClass from './getWSClass';
|
||||
|
||||
declare const global: Global;
|
||||
|
||||
describe('ws/polyfill', (): void => {
|
||||
describe('getWebScoket', (): void => {
|
||||
let origWs: Constructor<WebSocket>;
|
||||
|
||||
beforeEach((): void => {
|
||||
@@ -19,17 +20,19 @@ describe('ws/polyfill', (): void => {
|
||||
global.WebSocket = origWs;
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (with WebSocket)', (): void => {
|
||||
it('polyfills with no exceptions (with WebSocket)', async (): Promise<void> => {
|
||||
(global as any).WebSocket = undefined;
|
||||
require('./polyfill');
|
||||
|
||||
expect(global.WebSocket).toBeDefined();
|
||||
const WS = await getWSClass();
|
||||
|
||||
expect(WS).toBeDefined();
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (without WebSocket)', (): void => {
|
||||
it('polyfills with no exceptions (without WebSocket)', async (): Promise<void> => {
|
||||
(global as any).WebSocket = (): boolean => true;
|
||||
require('./polyfill');
|
||||
|
||||
expect(global.WebSocket).toBeDefined();
|
||||
const WS = await getWSClass();
|
||||
|
||||
expect(WS).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2020 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export default async function getWSClass (): Promise<typeof WebSocket> {
|
||||
if (typeof WebSocket === 'undefined') {
|
||||
const { w3cwebsocket } = await import('websocket');
|
||||
|
||||
return w3cwebsocket as unknown as typeof WebSocket;
|
||||
}
|
||||
|
||||
return WebSocket;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
if (typeof WebSocket === 'undefined') {
|
||||
(global as any).WebSocket = require('websocket').w3cwebsocket;
|
||||
}
|
||||
Reference in New Issue
Block a user