UNPKG

@parity/api

Version:

The Parity Promise-based API library for interfacing with Ethereum over RPC

137 lines (136 loc) 4.97 kB
"use strict"; // Copyright 2015-2019 Parity Technologies (UK) Ltd. // This file is part of Parity. 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 __()); }; })(); // Parity is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Parity is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Parity. If not, see <http://www.gnu.org/licenses/>. var JsonRpcEncoder = require('./jsonRpcEncoder'); var Logging = require('../subscriptions').Logging; var JsonRpcBase = /** @class */ (function (_super) { __extends(JsonRpcBase, _super); function JsonRpcBase() { var _this = _super.call(this) || this; _this._debug = false; _this._connected = false; _this._middlewareList = Promise.resolve([]); return _this; } JsonRpcBase.prototype.addMiddleware = function (Middleware) { var _this = this; this._middlewareList = Promise .all([ Middleware, this._middlewareList ]) .then(function (_a) { var Middleware = _a[0], middlewareList = _a[1]; // Do nothing if `handlerPromise` resolves to a null-y value. if (!Middleware) { return middlewareList; } // don't mutate the original array return middlewareList.concat([new Middleware(_this)]); }); }; JsonRpcBase.prototype._wrapSuccessResult = function (result) { return { id: this._id, jsonrpc: '2.0', result: result }; }; JsonRpcBase.prototype._wrapErrorResult = function (error) { return { id: this._id, jsonrpc: '2.0', error: { code: error.code, message: error.text } }; }; JsonRpcBase.prototype.execute = function (method, params) { var _this = this; return this._middlewareList.then(function (middlewareList) { for (var _i = 0, middlewareList_1 = middlewareList; _i < middlewareList_1.length; _i++) { var middleware = middlewareList_1[_i]; var res = middleware.handle(method, params); if (res != null) { return Promise .resolve(res) .then(function (res) { var result = _this._wrapSuccessResult(res); var json = _this.encode(method, params); Logging.send(method, params, { json: json, result: result }); return res; }); } } return _this._execute(method, params); }); }; JsonRpcBase.prototype._execute = function () { throw new Error('Missing implementation of JsonRpcBase#_execute'); }; JsonRpcBase.prototype._setConnected = function () { if (!this._connected) { this._connected = true; this.emit('open'); } }; JsonRpcBase.prototype._setDisconnected = function () { if (this._connected) { this._connected = false; this.emit('close'); } }; Object.defineProperty(JsonRpcBase.prototype, "isDebug", { get: function () { return this._debug; }, enumerable: true, configurable: true }); Object.defineProperty(JsonRpcBase.prototype, "isConnected", { get: function () { return this._connected; }, enumerable: true, configurable: true }); JsonRpcBase.prototype.setDebug = function (flag) { this._debug = flag; }; JsonRpcBase.prototype.error = function (error) { if (this.isDebug) { console.error(error); } }; JsonRpcBase.prototype.log = function (log) { if (this.isDebug) { console.log(log); } }; return JsonRpcBase; }(JsonRpcEncoder)); module.exports = JsonRpcBase;