Use polyfills from polkadot-js (#2616)
* Use polyfills from polkadot-js * no empty catch * Non-working asjustments * Adjust WS mocking * Bump polyfills * Bumps * Restore common
This commit is contained in:
@@ -27,9 +27,9 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-contract#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/api": "1.34.0-beta.8",
|
||||
"@polkadot/rpc-core": "1.34.0-beta.8",
|
||||
"@polkadot/types": "1.34.0-beta.8",
|
||||
"@polkadot/api": "^1.34.0-beta.8",
|
||||
"@polkadot/rpc-core": "^1.34.0-beta.8",
|
||||
"@polkadot/types": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"bn.js": "^5.1.3",
|
||||
"rxjs": "^6.6.3"
|
||||
|
||||
@@ -28,10 +28,10 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-derive#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/api": "1.34.0-beta.8",
|
||||
"@polkadot/rpc-core": "1.34.0-beta.8",
|
||||
"@polkadot/rpc-provider": "1.34.0-beta.8",
|
||||
"@polkadot/types": "1.34.0-beta.8",
|
||||
"@polkadot/api": "^1.34.0-beta.8",
|
||||
"@polkadot/rpc-core": "^1.34.0-beta.8",
|
||||
"@polkadot/rpc-provider": "^1.34.0-beta.8",
|
||||
"@polkadot/types": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"@polkadot/util-crypto": "^3.4.1",
|
||||
"bn.js": "^5.1.3",
|
||||
|
||||
@@ -27,13 +27,13 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/api-derive": "1.34.0-beta.8",
|
||||
"@polkadot/api-derive": "^1.34.0-beta.8",
|
||||
"@polkadot/keyring": "^3.4.1",
|
||||
"@polkadot/metadata": "1.34.0-beta.8",
|
||||
"@polkadot/rpc-core": "1.34.0-beta.8",
|
||||
"@polkadot/rpc-provider": "1.34.0-beta.8",
|
||||
"@polkadot/types": "1.34.0-beta.8",
|
||||
"@polkadot/types-known": "1.34.0-beta.8",
|
||||
"@polkadot/metadata": "^1.34.0-beta.8",
|
||||
"@polkadot/rpc-core": "^1.34.0-beta.8",
|
||||
"@polkadot/rpc-provider": "^1.34.0-beta.8",
|
||||
"@polkadot/types": "^1.34.0-beta.8",
|
||||
"@polkadot/types-known": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"@polkadot/util-crypto": "^3.4.1",
|
||||
"bn.js": "^5.1.3",
|
||||
|
||||
@@ -27,8 +27,8 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-metadata#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/types": "1.34.0-beta.8",
|
||||
"@polkadot/types-known": "1.34.0-beta.8",
|
||||
"@polkadot/types": "^1.34.0-beta.8",
|
||||
"@polkadot/types-known": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"@polkadot/util-crypto": "^3.4.1",
|
||||
"bn.js": "^5.1.3"
|
||||
|
||||
@@ -27,9 +27,9 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-core#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/metadata": "1.34.0-beta.8",
|
||||
"@polkadot/rpc-provider": "1.34.0-beta.8",
|
||||
"@polkadot/types": "1.34.0-beta.8",
|
||||
"@polkadot/metadata": "^1.34.0-beta.8",
|
||||
"@polkadot/rpc-provider": "^1.34.0-beta.8",
|
||||
"@polkadot/types": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"memoizee": "^0.4.14",
|
||||
"rxjs": "^6.6.3"
|
||||
|
||||
@@ -27,14 +27,14 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-provider#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/metadata": "1.34.0-beta.8",
|
||||
"@polkadot/types": "1.34.0-beta.8",
|
||||
"@polkadot/metadata": "^1.34.0-beta.8",
|
||||
"@polkadot/types": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"@polkadot/util-crypto": "^3.4.1",
|
||||
"@polkadot/x-fetch": "^0.3.2",
|
||||
"@polkadot/x-ws": "^0.3.2",
|
||||
"bn.js": "^5.1.3",
|
||||
"cross-fetch": "^3.0.6",
|
||||
"eventemitter3": "^4.0.7",
|
||||
"websocket": "^1.0.32"
|
||||
"eventemitter3": "^4.0.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^3.4.1",
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
import { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitted, ProviderInterfaceEmitCb } from '../types';
|
||||
|
||||
import fetch from 'cross-fetch';
|
||||
import { assert, logger } from '@polkadot/util';
|
||||
import fetch from '@polkadot/x-fetch';
|
||||
|
||||
import Coder from '../coder';
|
||||
import defaults from '../defaults';
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Http from './';
|
||||
import { Mock } from './../mock/types';
|
||||
import { mockHttp, TEST_HTTP_URL } from '../../test/mockHttp';
|
||||
|
||||
import Http from './';
|
||||
|
||||
describe('send', (): void => {
|
||||
let http: Http;
|
||||
let mock: Mock;
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
import { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitted, ProviderInterfaceEmitCb } from '../types';
|
||||
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { assert, isNull, isUndefined, logger } from '@polkadot/util';
|
||||
import { assert, isChildClass, isNull, isUndefined, logger } from '@polkadot/util';
|
||||
import WS from '@polkadot/x-ws';
|
||||
|
||||
import Coder from '../coder';
|
||||
import defaults from '../defaults';
|
||||
import { getWSErrorString } from './errors';
|
||||
import { createWS } from './getWS';
|
||||
|
||||
interface SubscriptionHandler {
|
||||
callback: ProviderInterfaceCallback;
|
||||
@@ -143,10 +143,16 @@ export default class WsProvider implements ProviderInterface {
|
||||
* @description The [[WsProvider]] connects automatically by default, however if you decided otherwise, you may
|
||||
* connect manually using this method.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/require-await
|
||||
public async connect (): Promise<void> {
|
||||
try {
|
||||
this.#endpointIndex = (this.#endpointIndex + 1) % this.#endpoints.length;
|
||||
this.#websocket = await createWS(this.#endpoints[this.#endpointIndex], this.#headers);
|
||||
this.#websocket = typeof WebSocket !== 'undefined' && isChildClass(WebSocket, WS)
|
||||
? new WS(this.#endpoints[this.#endpointIndex])
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore - WS may be an instance of w3cwebsocket, which supports headers
|
||||
: new WS(this.#endpoints[this.#endpointIndex], undefined, undefined, this.#headers);
|
||||
|
||||
this.#websocket.onclose = this.#onSocketClose;
|
||||
this.#websocket.onerror = this.#onSocketError;
|
||||
this.#websocket.onmessage = this.#onSocketMessage;
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import WsProvider from './';
|
||||
import { Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import WsProvider from './';
|
||||
|
||||
function sleepMs (ms = 0): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
@@ -1,40 +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.
|
||||
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import { Global } from './../mock/types';
|
||||
import { getWSClass } from './getWS';
|
||||
|
||||
declare const global: Global;
|
||||
|
||||
describe('getWSClass', (): void => {
|
||||
let origWs: Constructor<WebSocket>;
|
||||
|
||||
beforeEach((): void => {
|
||||
origWs = global.WebSocket;
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
global.WebSocket = origWs;
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (with WebSocket)', async (): Promise<void> => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
(global as any).WebSocket = undefined;
|
||||
|
||||
const [WS] = await getWSClass();
|
||||
|
||||
expect(WS).toBeDefined();
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (without WebSocket)', async (): Promise<void> => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
(global as any).WebSocket = (): boolean => true;
|
||||
|
||||
const [WS] = await getWSClass();
|
||||
|
||||
expect(WS).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1,23 +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.
|
||||
|
||||
export async function getWSClass (): Promise<[typeof WebSocket, boolean]> {
|
||||
if (typeof WebSocket === 'undefined') {
|
||||
const { w3cwebsocket } = await import('websocket');
|
||||
|
||||
return [w3cwebsocket as unknown as typeof WebSocket, false];
|
||||
}
|
||||
|
||||
return [WebSocket, true];
|
||||
}
|
||||
|
||||
export async function createWS (endpoint: string, headers?: Record<string, string>): Promise<WebSocket> {
|
||||
const [WS, isDefault] = await getWSClass();
|
||||
|
||||
return isDefault
|
||||
? new WS(endpoint)
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore - WS may be an instance of w3cwebsocket, which supports headers
|
||||
: new WS(endpoint, undefined, undefined, headers);
|
||||
}
|
||||
@@ -2,10 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import WsProvider from './';
|
||||
import { Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import WsProvider from './';
|
||||
|
||||
let ws: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import WsProvider from './';
|
||||
import { Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import WsProvider from './';
|
||||
|
||||
let ws: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import WsProvider from './';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import WsProvider from './';
|
||||
|
||||
declare const global: Global;
|
||||
|
||||
let provider: WsProvider;
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import WsProvider from './';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import WsProvider from './';
|
||||
|
||||
declare const global: Global;
|
||||
|
||||
let ws: WsProvider;
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import WsProvider from './';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import WsProvider from './';
|
||||
|
||||
declare const global: Global;
|
||||
|
||||
let ws: WsProvider;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Server } from 'mock-socket';
|
||||
import { Server, WebSocket } from 'mock-socket';
|
||||
|
||||
const TEST_WS_URL = 'ws://localhost:9955';
|
||||
|
||||
@@ -28,6 +28,8 @@ interface ReplyDef {
|
||||
};
|
||||
}
|
||||
|
||||
global.WebSocket = WebSocket;
|
||||
|
||||
// should be JSONRPC def return
|
||||
function createError ({ error: { code, message }, id }: ErrorDef): any {
|
||||
return {
|
||||
|
||||
@@ -36,10 +36,10 @@
|
||||
"@babel/core": "^7.11.6",
|
||||
"@babel/register": "^7.11.5",
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/api": "1.34.0-beta.8",
|
||||
"@polkadot/metadata": "1.34.0-beta.8",
|
||||
"@polkadot/rpc-provider": "1.34.0-beta.8",
|
||||
"@polkadot/types": "1.34.0-beta.8",
|
||||
"@polkadot/api": "^1.34.0-beta.8",
|
||||
"@polkadot/metadata": "^1.34.0-beta.8",
|
||||
"@polkadot/rpc-provider": "^1.34.0-beta.8",
|
||||
"@polkadot/types": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"handlebars": "^4.7.6",
|
||||
"websocket": "^1.0.32",
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
import path from 'path';
|
||||
import yargs from 'yargs';
|
||||
import { createWS } from '@polkadot/rpc-provider/ws/getWS';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
import WS from '@polkadot/x-ws';
|
||||
|
||||
import generateConst from './generate/consts';
|
||||
import generateQuery from './generate/query';
|
||||
@@ -60,31 +60,31 @@ export default function main (): void {
|
||||
}).argv;
|
||||
|
||||
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
|
||||
createWS(endpoint)
|
||||
.then((websocket): void => {
|
||||
websocket.onclose = (event: { code: number; reason: string }): void => {
|
||||
console.error(`disconnected, code: '${event.code}' reason: '${event.reason}'`);
|
||||
process.exit(1);
|
||||
};
|
||||
try {
|
||||
const websocket = new WS(endpoint);
|
||||
|
||||
websocket.onerror = (event: any): void => {
|
||||
console.error(event);
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
websocket.onopen = (): void => {
|
||||
console.log('connected');
|
||||
websocket.send('{"id":"1","jsonrpc":"2.0","method":"state_getMetadata","params":[]}');
|
||||
};
|
||||
|
||||
websocket.onmessage = (message: any): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
generate(JSON.parse(message.data).result, pkg, output, isStrict);
|
||||
};
|
||||
})
|
||||
.catch((): void => {
|
||||
websocket.onclose = (event: { code: number; reason: string }): void => {
|
||||
console.error(`disconnected, code: '${event.code}' reason: '${event.reason}'`);
|
||||
process.exit(1);
|
||||
});
|
||||
};
|
||||
|
||||
websocket.onerror = (event: any): void => {
|
||||
console.error(event);
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
websocket.onopen = (): void => {
|
||||
console.log('connected');
|
||||
websocket.send('{"id":"1","jsonrpc":"2.0","method":"state_getMetadata","params":[]}');
|
||||
};
|
||||
|
||||
websocket.onmessage = (message: any): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
generate(JSON.parse(message.data).result, pkg, output, isStrict);
|
||||
};
|
||||
} catch (error) {
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-member-access
|
||||
generate(require(path.join(process.cwd(), endpoint)).result, pkg, output, isStrict);
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/types-known#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/types": "1.34.0-beta.8",
|
||||
"@polkadot/types": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"bn.js": "^5.1.3"
|
||||
},
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/types#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/metadata": "1.34.0-beta.8",
|
||||
"@polkadot/metadata": "^1.34.0-beta.8",
|
||||
"@polkadot/util": "^3.4.1",
|
||||
"@polkadot/util-crypto": "^3.4.1",
|
||||
"@types/bn.js": "^4.11.6",
|
||||
|
||||
Reference in New Issue
Block a user