UNPKG

@pokt-network/pocket-js

Version:

Pocket-js core package with the main functionalities to interact with the Pocket Network.

234 lines 12.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionManager = void 0; var session_header_1 = require("../rpc/models/input/session-header"); var session_1 = require("../rpc/models/output/session"); var dispatch_response_1 = require("../rpc/models/output/dispatch-response"); var rpc_1 = require("../rpc/rpc"); var dispatch_request_1 = require("../rpc/models/input/dispatch-request"); var type_guard_1 = require("../utils/type-guard"); var queue_1 = require("./queue"); var routing_table_1 = require("../routing-table/routing-table"); var rpc_2 = require("../rpc"); var js_sha3_1 = require("js-sha3"); /** * @class SessionManager */ var SessionManager = /** @class */ (function () { /** * Creates an instance of SessionManager. * @param {URL[]} dispatchers - Dispatcher's list. * @param {Configuration} configuration - Pocket Configuration. * @param {IKVStore} store - KVStore implementation. * @memberof SessionManager */ function SessionManager(dispatchers, configuration, store) { this.sessionMapKey = "SESSIONS_KEY"; this.routingTable = new routing_table_1.RoutingTable(dispatchers, configuration, store); this.sessionMap = new Map(); if (this.routingTable.store.has(this.sessionMapKey)) { this.sessionMap = this.routingTable.store.get(this.sessionMapKey); } else { this.sessionMap = new Map(); this.routingTable.store.add(this.sessionMapKey, this.sessionMap); } } /** * Adds a new node to the routing table dispatcher's list * @param {Node} dispatcher - New dispatcher. * @memberof SessionManager */ SessionManager.prototype.addNewDispatcher = function (dispatcher) { this.routingTable.addDispatcher(dispatcher.serviceURL); }; /** * Returns the routing table dispatcher's count * @returns {Number} - Dispatcher's count. * @memberof SessionManager */ SessionManager.prototype.getDispatchersCount = function () { return this.routingTable.dispatchersCount; }; /** * Update the current session using an already requested dispatch response. Returns a Promise with the Session object or an Error when something goes wrong. * @param {PocketAAT} pocketAAT - Pocket Authentication Token. * @param {string} chain - Name of the Blockchain. * @param {Configuration} configuration - Configuration object. * @returns {Promise} * @memberof SessionManager */ SessionManager.prototype.updateCurrentSession = function (session, pocketAAT, chain, configuration) { return __awaiter(this, void 0, void 0, function () { var key; return __generator(this, function (_a) { key = this.getSessionKey(pocketAAT, chain); return [2 /*return*/, this.saveSession(key, session, configuration)]; }); }); }; /** * Request a new session object. Returns a Promise with the Session object or an Error when something goes wrong. * @param {PocketAAT} pocketAAT - Pocket Authentication Token. * @param {string} chain - Name of the Blockchain. * @param {Configuration} configuration - Configuration object. * @param {Configuration} configuration - Configuration object. * @param {Configuration} retryCount - Amount of retries performed * @returns {Promise} * @memberof SessionManager */ SessionManager.prototype.requestNewSession = function (pocketAAT, chain, configuration, attemptCount, maxAttempts) { if (attemptCount === void 0) { attemptCount = 1; } return __awaiter(this, void 0, void 0, function () { var dispatcher, maxAttemptsAllowed, rpc, header, dispatchRequest, result, session, key; return __generator(this, function (_a) { switch (_a.label) { case 0: dispatcher = this.routingTable.getRandomDispatcher(); maxAttemptsAllowed = maxAttempts ? maxAttempts : this.getDispatchersCount() * 2; if (type_guard_1.typeGuard(dispatcher, Error)) { return [2 /*return*/, dispatcher]; } rpc = new rpc_1.RPC(new rpc_2.HttpRpcProvider(dispatcher)); header = new session_header_1.SessionHeader(pocketAAT.applicationPublicKey, chain, BigInt(0)); dispatchRequest = new dispatch_request_1.DispatchRequest(header); return [4 /*yield*/, rpc.client.dispatch(dispatchRequest, configuration.requestTimeOut, configuration.rejectSelfSignedCertificates)]; case 1: result = _a.sent(); if (!type_guard_1.typeGuard(result, dispatch_response_1.DispatchResponse)) return [3 /*break*/, 2]; session = void 0; try { session = session_1.Session.fromJSON(JSON.stringify(result.toJSON())); } catch (error) { return [2 /*return*/, error]; } if (session !== undefined) { key = this.getSessionKey(pocketAAT, chain); return [2 /*return*/, this.saveSession(key, session, configuration)]; } else { // Remove node from dispatcher if it failed 3 times return [2 /*return*/, new Error("Error decoding session from Dispatch response")]; } return [3 /*break*/, 5]; case 2: if (!(attemptCount < maxAttemptsAllowed)) return [3 /*break*/, 4]; return [4 /*yield*/, this.requestNewSession(pocketAAT, chain, configuration, attemptCount + 1, maxAttemptsAllowed)]; case 3: // Request the session again return [2 /*return*/, _a.sent()]; case 4: return [2 /*return*/, new Error("Unable to create a new session due to dispatchers failure.")]; case 5: return [2 /*return*/]; } }); }); }; /** * Returns the current session for an specific Blockchain. Request a new session object if there's no an active Session for the specified blockchain. Returns a Promise with the Session object or a RpcErrorResponse when something goes wrong. * @param {PocketAAT} pocketAAT - Pocket Authentication Token. * @param {string} chain - Name of the Blockchain. * @param {Configuration} configuration - Configuration object. * @returns {Promise} * @memberof SessionManager */ SessionManager.prototype.getCurrentSession = function (pocketAAT, chain, configuration, maxAttempts) { return __awaiter(this, void 0, void 0, function () { var key, currentSession; return __generator(this, function (_a) { switch (_a.label) { case 0: key = this.getSessionKey(pocketAAT, chain); if (!!this.sessionMap.has(key)) return [3 /*break*/, 2]; return [4 /*yield*/, this.requestNewSession(pocketAAT, chain, configuration, 1, maxAttempts)]; case 1: return [2 /*return*/, _a.sent()]; case 2: currentSession = this.sessionMap.get(key).front; if (!(currentSession !== undefined)) return [3 /*break*/, 3]; return [2 /*return*/, currentSession]; case 3: return [4 /*yield*/, this.requestNewSession(pocketAAT, chain, configuration, 1, maxAttempts)]; case 4: return [2 /*return*/, _a.sent()]; } }); }); }; /** * Creates an unique key using the PocketAAT object and the chain. * @param {PocketAAT} pocketAAT - Pocket Authentication Token. * @param {string} chain - Blockchain hash. * @memberof SessionManager */ SessionManager.prototype.getSessionKey = function (pocketAAT, chain) { var hash = js_sha3_1.sha3_256.create(); hash.update(JSON.stringify(pocketAAT).concat(chain)); return hash.toString(); }; /** * Removes the first Session in the queue for the specified blockchain. * @param {PocketAAT} pocketAAT - Pocket Authentication Token. * @param {string} chain - Blockchain hash. * @memberof SessionManager */ SessionManager.prototype.destroySession = function (pocketAAT, chain) { var key = this.getSessionKey(pocketAAT, chain); this.sessionMap.get(key).dequeue(); }; /** * Saves the given session to the session queue * @param {string} key - The key under which to save the session * @param {Session} session - The session to save * @param {Configuration} configuration - The configuration to use */ SessionManager.prototype.saveSession = function (key, session, configuration) { if (!this.sessionMap.has(key)) { this.sessionMap.set(key, new queue_1.Queue()); } // Check session queue length to pop the oldest element in the queue var sessionQueue = this.sessionMap.get(key); if (configuration.maxSessions !== 0 && sessionQueue.length === configuration.maxSessions) { sessionQueue.dequeue(); } // Append the new session to the queue sessionQueue.enqueue(session); return session; }; return SessionManager; }()); exports.SessionManager = SessionManager; //# sourceMappingURL=session-manager.js.map