From 22fa5a57a588c3c5c2a72ec5ef539797c2847688 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 6 Apr 2018 15:46:27 +0900 Subject: Refactor RedundantRpcSubprovider into RedundantSubprovider --- packages/subproviders/src/index.ts | 2 +- .../subproviders/src/subproviders/redundant_rpc.ts | 70 ---------------------- .../src/subproviders/redundant_subprovider.ts | 65 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 71 deletions(-) delete mode 100644 packages/subproviders/src/subproviders/redundant_rpc.ts create mode 100644 packages/subproviders/src/subproviders/redundant_subprovider.ts (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index 9786347e6..1711387f1 100644 --- a/packages/subproviders/src/index.ts +++ b/packages/subproviders/src/index.ts @@ -7,7 +7,7 @@ import { LedgerEthereumClient } from './types'; export { EmptyWalletSubprovider } from './subproviders/empty_wallet_subprovider'; export { FakeGasEstimateSubprovider } from './subproviders/fake_gas_estimate_subprovider'; export { InjectedWeb3Subprovider } from './subproviders/injected_web3'; -export { RedundantRPCSubprovider } from './subproviders/redundant_rpc'; +export { RedundantSubprovider } from './subproviders/redundant_subprovider'; export { LedgerSubprovider } from './subproviders/ledger'; export { GanacheSubprovider } from './subproviders/ganache'; export { Subprovider } from './subproviders/subprovider'; diff --git a/packages/subproviders/src/subproviders/redundant_rpc.ts b/packages/subproviders/src/subproviders/redundant_rpc.ts deleted file mode 100644 index f8ff0915d..000000000 --- a/packages/subproviders/src/subproviders/redundant_rpc.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { JSONRPCRequestPayload } from '@0xproject/types'; -import { promisify } from '@0xproject/utils'; -import * as _ from 'lodash'; -import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); - -import { Callback } from '../types'; - -import { Subprovider } from './subprovider'; - -/** - * This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine) subprovider interface. - * It attempts to handle each JSON RPC request by sequentially attempting to receive a valid response from one of a - * set of JSON RPC endpoints. - */ -export class RedundantRPCSubprovider extends Subprovider { - private _rpcs: RpcSubprovider[]; - private static async _firstSuccessAsync( - rpcs: RpcSubprovider[], - payload: JSONRPCRequestPayload, - next: Callback, - ): Promise { - let lastErr: Error | undefined; - for (const rpc of rpcs) { - try { - const data = await promisify(rpc.handleRequest.bind(rpc))(payload, next); - return data; - } catch (err) { - lastErr = err; - continue; - } - } - if (!_.isUndefined(lastErr)) { - throw lastErr; - } - } - /** - * Instantiates a new RedundantRPCSubprovider - * @param endpoints JSON RPC endpoints to attempt. Attempts are made in the order of the endpoints. - */ - constructor(endpoints: string[]) { - super(); - this._rpcs = _.map(endpoints, endpoint => { - return new RpcSubprovider({ - rpcUrl: endpoint, - }); - }); - } - /** - * This method conforms to the web3-provider-engine interface. - * It is called internally by the ProviderEngine when it is this subproviders - * turn to handle a JSON RPC request. - * @param payload JSON RPC payload - * @param next Callback to call if this subprovider decides not to handle the request - * @param end Callback to call if subprovider handled the request and wants to pass back the request. - */ - // tslint:disable-next-line:async-suffix - public async handleRequest( - payload: JSONRPCRequestPayload, - next: Callback, - end: (err: Error | null, data?: any) => void, - ): Promise { - const rpcsCopy = this._rpcs.slice(); - try { - const data = await RedundantRPCSubprovider._firstSuccessAsync(rpcsCopy, payload, next); - end(null, data); - } catch (err) { - end(err); - } - } -} diff --git a/packages/subproviders/src/subproviders/redundant_subprovider.ts b/packages/subproviders/src/subproviders/redundant_subprovider.ts new file mode 100644 index 000000000..37c8bba5a --- /dev/null +++ b/packages/subproviders/src/subproviders/redundant_subprovider.ts @@ -0,0 +1,65 @@ +import { JSONRPCRequestPayload } from '@0xproject/types'; +import { promisify } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { Callback } from '../types'; + +import { Subprovider } from './subprovider'; + +/** + * This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine) subprovider interface. + * It attempts to handle each JSON RPC request by sequentially attempting to receive a valid response from one of a + * set of JSON RPC endpoints. + */ +export class RedundantSubprovider extends Subprovider { + private _subproviders: Subprovider[]; + private static async _firstSuccessAsync( + subproviders: Subprovider[], + payload: JSONRPCRequestPayload, + next: Callback, + ): Promise { + let lastErr: Error | undefined; + for (const subprovider of subproviders) { + try { + const data = await promisify(subprovider.handleRequest.bind(subprovider))(payload, next); + return data; + } catch (err) { + lastErr = err; + continue; + } + } + if (!_.isUndefined(lastErr)) { + throw lastErr; + } + } + /** + * Instantiates a new RedundantSubprovider + * @param endpoints JSON RPC endpoints to attempt. Attempts are made in the order of the endpoints. + */ + constructor(subproviders: Subprovider[]) { + super(); + this._subproviders = subproviders; + } + /** + * This method conforms to the web3-provider-engine interface. + * It is called internally by the ProviderEngine when it is this subproviders + * turn to handle a JSON RPC request. + * @param payload JSON RPC payload + * @param next Callback to call if this subprovider decides not to handle the request + * @param end Callback to call if subprovider handled the request and wants to pass back the request. + */ + // tslint:disable-next-line:async-suffix + public async handleRequest( + payload: JSONRPCRequestPayload, + next: Callback, + end: (err: Error | null, data?: any) => void, + ): Promise { + const subprovidersCopy = this._subproviders.slice(); + try { + const data = await RedundantSubprovider._firstSuccessAsync(subprovidersCopy, payload, next); + end(null, data); + } catch (err) { + end(err); + } + } +} -- cgit v1.2.3