UNPKG

@parity/api

Version:

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

111 lines (110 loc) 4.61 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 Logging = require('../../subscriptions').Logging; var JsonRpcBase = require('../jsonRpcBase'); var TransportError = require('../error'); /* global fetch */ var Http = /** @class */ (function (_super) { __extends(Http, _super); function Http(url, connectTimeout) { if (connectTimeout === void 0) { connectTimeout = 1000; } var _this = _super.call(this) || this; _this._connected = true; _this._url = url; _this._connectTimeout = connectTimeout; _this._pollConnection = _this._pollConnection.bind(_this); _this._pollConnection(); return _this; } Http.prototype._encodeOptions = function (method, params) { var json = this.encode(method, params); this.log(json); return { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Content-Length': json.length }, body: json }; }; Http.prototype._execute = function (method, params) { var _this = this; var request = this._encodeOptions(method, params); return Promise.race([ fetch(this._url, request), new Promise(function (_, reject) { return setTimeout(function () { return reject(new Error('timeout')); }, 10000); }) ]).catch(function (error) { _this._setDisconnected(); throw error; }) .then(function (response) { _this._setConnected(); if (response.status !== 200) { _this._setDisconnected(); _this.error(JSON.stringify({ status: response.status, statusText: response.statusText })); console.error(method + "(" + JSON.stringify(params) + "): " + response.status + ": " + response.statusText); throw new Error(response.status + ": " + response.statusText); } return response.json(); }) .then(function (response) { Logging.send(method, params, { request: request, response: response }); if (response.error) { _this.error(JSON.stringify(response)); console.error(method + "(" + JSON.stringify(params) + "): " + response.error.code + ": " + response.error.message); var error = new TransportError(method, response.error.code, response.error.message); throw error; } _this.log(JSON.stringify(response)); return response.result; }); }; Http.prototype._pollConnection = function () { var _this = this; if (this._connectTimeout <= 0) { return; } var nextTimeout = function () { return setTimeout(_this._pollConnection, _this._connectTimeout); }; this .execute('net_listening') .then(function () { return nextTimeout(); }) .catch(function () { return nextTimeout(); }); }; Object.defineProperty(Http.prototype, "url", { set: function (url) { this._url = url; }, enumerable: true, configurable: true }); return Http; }(JsonRpcBase)); module.exports = Http;