PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@polkadot/rpc-provider/cjs/http
Просмотр файла: index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpProvider = void 0;
const tslib_1 = require("tslib");
const util_1 = require("@polkadot/util");
const x_fetch_1 = require("@polkadot/x-fetch");
const index_js_1 = require("../coder/index.js");
const defaults_js_1 = tslib_1.__importDefault(require("../defaults.js"));
const lru_js_1 = require("../lru.js");
const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead';
const l = (0, util_1.logger)('api-http');
/**
* # @polkadot/rpc-provider
*
* @name HttpProvider
*
* @description The HTTP Provider allows sending requests using HTTP to a HTTP RPC server TCP port. It does not support subscriptions so you won't be able to listen to events such as new blocks or balance changes. It is usually preferable using the [[WsProvider]].
*
* @example
* <BR>
*
* ```javascript
* import Api from '@polkadot/api/promise';
* import { HttpProvider } from '@polkadot/rpc-provider';
*
* const provider = new HttpProvider('http://127.0.0.1:9933');
* const api = new Api(provider);
* ```
*
* @see [[WsProvider]]
*/
class HttpProvider {
__internal__callCache;
__internal__cacheCapacity;
__internal__coder;
__internal__endpoint;
__internal__headers;
__internal__stats;
/**
* @param {string} endpoint The endpoint url starting with http://
*/
constructor(endpoint = defaults_js_1.default.HTTP_URL, headers = {}, cacheCapacity) {
if (!/^(https|http):\/\//.test(endpoint)) {
throw new Error(`Endpoint should start with 'http://' or 'https://', received '${endpoint}'`);
}
this.__internal__coder = new index_js_1.RpcCoder();
this.__internal__endpoint = endpoint;
this.__internal__headers = headers;
this.__internal__callCache = new lru_js_1.LRUCache(cacheCapacity === 0 ? 0 : cacheCapacity || lru_js_1.DEFAULT_CAPACITY);
this.__internal__cacheCapacity = cacheCapacity === 0 ? 0 : cacheCapacity || lru_js_1.DEFAULT_CAPACITY;
this.__internal__stats = {
active: { requests: 0, subscriptions: 0 },
total: { bytesRecv: 0, bytesSent: 0, cached: 0, errors: 0, requests: 0, subscriptions: 0, timeout: 0 }
};
}
/**
* @summary `true` when this provider supports subscriptions
*/
get hasSubscriptions() {
return !!false;
}
/**
* @description Returns a clone of the object
*/
clone() {
return new HttpProvider(this.__internal__endpoint, this.__internal__headers);
}
/**
* @description Manually connect from the connection
*/
async connect() {
// noop
}
/**
* @description Manually disconnect from the connection
*/
async disconnect() {
// noop
}
/**
* @description Returns the connection stats
*/
get stats() {
return this.__internal__stats;
}
/**
* @summary `true` when this provider supports clone()
*/
get isClonable() {
return !!true;
}
/**
* @summary Whether the node is connected or not.
* @return {boolean} true if connected
*/
get isConnected() {
return !!true;
}
/**
* @summary Events are not supported with the HttpProvider, see [[WsProvider]].
* @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
*/
on(_type, _sub) {
l.error('HTTP Provider does not have \'on\' emitters, use WebSockets instead');
return util_1.noop;
}
/**
* @summary Send HTTP POST Request with Body to configured HTTP Endpoint.
*/
async send(method, params, isCacheable) {
this.__internal__stats.total.requests++;
const [, body] = this.__internal__coder.encodeJson(method, params);
if (this.__internal__cacheCapacity === 0) {
return this.__internal__send(body);
}
const cacheKey = isCacheable ? `${method}::${(0, util_1.stringify)(params)}` : '';
let resultPromise = isCacheable
? this.__internal__callCache.get(cacheKey)
: null;
if (!resultPromise) {
resultPromise = this.__internal__send(body);
if (isCacheable) {
this.__internal__callCache.set(cacheKey, resultPromise);
}
}
else {
this.__internal__stats.total.cached++;
}
return resultPromise;
}
async __internal__send(body) {
this.__internal__stats.active.requests++;
this.__internal__stats.total.bytesSent += body.length;
try {
const response = await (0, x_fetch_1.fetch)(this.__internal__endpoint, {
body,
headers: {
Accept: 'application/json',
'Content-Length': `${body.length}`,
'Content-Type': 'application/json',
...this.__internal__headers
},
method: 'POST'
});
if (!response.ok) {
throw new Error(`[${response.status}]: ${response.statusText}`);
}
const result = await response.text();
this.__internal__stats.total.bytesRecv += result.length;
const decoded = this.__internal__coder.decodeResponse(JSON.parse(result));
this.__internal__stats.active.requests--;
return decoded;
}
catch (e) {
this.__internal__stats.active.requests--;
this.__internal__stats.total.errors++;
throw e;
}
}
/**
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
*/
// eslint-disable-next-line @typescript-eslint/require-await
async subscribe(_types, _method, _params, _cb) {
l.error(ERROR_SUBSCRIBE);
throw new Error(ERROR_SUBSCRIBE);
}
/**
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
*/
// eslint-disable-next-line @typescript-eslint/require-await
async unsubscribe(_type, _method, _id) {
l.error(ERROR_SUBSCRIBE);
throw new Error(ERROR_SUBSCRIBE);
}
}
exports.HttpProvider = HttpProvider;
Выполнить команду
Для локальной разработки. Не используйте в интернете!