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:
Jaco Greeff
2020-02-22 13:56:37 +01:00
committed by GitHub
parent deddc6dc79
commit 51686f0cd4
8 changed files with 54 additions and 31 deletions
+5 -4
View File
@@ -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;
@@ -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;
}
-7
View File
@@ -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;
}
+2
View File
@@ -39,12 +39,14 @@
"@polkadot/api": "1.4.0-beta.27",
"@polkadot/jsonrpc": "1.4.0-beta.27",
"@polkadot/metadata": "1.4.0-beta.27",
"@polkadot/rpc-provider": "1.4.0-beta.27",
"@polkadot/types": "1.4.0-beta.27",
"@polkadot/util": "^2.4.1",
"websocket": "^1.0.31",
"yargs": "^15.1.0"
},
"devDependencies": {
"@types/websocket": "^1.0.0",
"@types/yargs": "^15.0.3"
}
}
+2 -1
View File
@@ -30,10 +30,11 @@ export default function main (): void {
const { ws } = yargs
.usage('Usage: [options]')
.wrap(120)
.strict()
.options({
ws: {
default: 'ws://127.0.0.1:9944',
description: 'The API endpoint to connect to, e.g. wss://poc3-rpc.polkadot.io',
description: 'The API endpoint to connect to, e.g. wss://kusama-rpc.polkadot.io',
type: 'string',
required: true
}
+15 -12
View File
@@ -4,16 +4,13 @@
import path from 'path';
import yargs from 'yargs';
import getWSClass from '@polkadot/rpc-provider/ws/getWSClass';
import { formatNumber } from '@polkadot/util';
import generateConst from './generate/consts';
import generateQuery from './generate/query';
import generateTx from './generate/tx';
if (typeof WebSocket === 'undefined') {
(global as any).WebSocket = require('websocket').w3cwebsocket;
}
let websocket: any = null;
function generate (metaHex: string, pkg: string | undefined, output: string, isStrict?: boolean): void {
@@ -67,19 +64,25 @@ export default function main (): void {
type: 'string'
},
strict: {
description: 'Turns on stirct mode, not outputting genric versions',
description: 'Turns on stict mode, no output of catch-all generic versions',
type: 'boolean'
}
}).argv;
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
websocket = new WebSocket(endpoint);
websocket.onclose = onSocketClose;
websocket.onerror = onSocketError;
websocket.onopen = onSocketOpen;
websocket.onmessage = (message: any): void => {
generate(JSON.parse(message.data).result, pkg, output, isStrict);
};
getWSClass()
.then((WS): void => {
websocket = new WS(endpoint);
websocket.onclose = onSocketClose;
websocket.onerror = onSocketError;
websocket.onopen = onSocketOpen;
websocket.onmessage = (message: any): void => {
generate(JSON.parse(message.data).result, pkg, output, isStrict);
};
})
.catch((): void => {
process.exit(1);
});
} else {
generate(require(path.join(process.cwd(), endpoint)).result, pkg, output, isStrict);
}