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
+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);
}