UNPKG

@parity/light.js

Version:

A high-level reactive JS library optimized for light clients

138 lines (137 loc) 4.81 kB
"use strict"; // Copyright 2015-2019 Parity Technologies (UK) Ltd. // This file is part of Parity. // // SPDX-License-Identifier: MIT var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var EventEmitter = require("eventemitter3"); // Count the number of providers we have, so that each time we call createApi, // we create a new provider. var providerCount = 0; /** * Provider used for tests. * * @ignore */ var MockProvider = /** @class */ (function (_super) { __extends(MockProvider, _super); function MockProvider() { return _super !== null && _super.apply(this, arguments) || this; } MockProvider.prototype.send = function () { return Promise.resolve(); }; return MockProvider; }(EventEmitter)); exports.MockProvider = MockProvider; // List of JSONRPCs we want to mock var listOfMockRps = { eth: [ 'accounts', 'blockNumber', 'chainId', 'getBalance', 'getTransactionCount', 'newHeads', 'syncing' ], fake: ['method'], net: ['peerCount'], parity: ['accountsInfo', 'chain', 'postTransaction', 'versionInfo'] }; /** * Create a mock api object from the listOfMockRps above. * * @ignore */ var createApi = function (resolveValueOrFunction, isPubSub, isError) { var getResolveValue = function () { // TODO Casting as Function manually, or else we get: // "Cannot invoke an expression whose type lacks a call signature. Type 'Function | (() => any)' has no compatible call signatures." return typeof resolveValueOrFunction === 'function' ? resolveValueOrFunction() : resolveValueOrFunction; }; var result = Object.keys(listOfMockRps).reduce(function (apiObject, namespace) { // Create methods on apiObject apiObject[namespace] = {}; listOfMockRps[namespace].forEach(function (method) { apiObject[namespace][method] = isError ? function () { return Promise.reject(getResolveValue()); } : function () { return Promise.resolve(getResolveValue()); }; }); // Create pubsub on apiObject apiObject.pubsub = apiObject.pubsub || { unsubscribe: function () { return Promise.resolve(); } }; apiObject.pubsub[namespace] = {}; listOfMockRps[namespace].forEach(function (method) { apiObject.pubsub[namespace][method] = isError ? function (callback) { callback(getResolveValue(), null); return Promise.resolve(1); // Resolves to subscriptionId } : function (callback) { callback(null, getResolveValue()); return Promise.resolve(1); // Resolves to subscriptionId }; }); // For eth.syncing pubsub, always return false apiObject.pubsub.eth.syncing = function (callback) { callback(null, false); return Promise.resolve(1); }; return apiObject; }, { isPubSub: isPubSub, pollMethod: function () { return isError ? Promise.reject(getResolveValue()) : Promise.resolve(getResolveValue()); }, provider: { id: ++providerCount, on: function () { /* Do nothing. */ }, send: function () { /* Do nothing. */ } } }); result.resolveWith = resolveValueOrFunction.toString(); return result; }; /** * Mock api that resolves. * * @ignore */ exports.rejectApi = function (resolveValueOrFunction, isPubsub) { if (resolveValueOrFunction === void 0) { resolveValueOrFunction = new Error('bar'); } if (isPubsub === void 0) { isPubsub = true; } return createApi(resolveValueOrFunction, isPubsub, true); }; /** * Mock api that resolves. * * @ignore */ exports.resolveApi = function (resolveValueOrFunction, isPubsub) { if (resolveValueOrFunction === void 0) { resolveValueOrFunction = 'foo'; } if (isPubsub === void 0) { isPubsub = true; } return createApi(resolveValueOrFunction, isPubsub, false); };