UNPKG

@pokt-network/pocket-js

Version:

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

121 lines 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Session = void 0; var node_1 = require("../node"); var session_header_1 = require("../input/session-header"); /** * * * @class Session */ var Session = /** @class */ (function () { /** * Request for Session. * @constructor * @param {SessionHeader} sessionHeader - Session Header object. * @param {string} sessionKey - Session Key. * @param {SessionNode[]} sessionNodes - Nodes for the session. */ function Session(sessionHeader, sessionKey, sessionNodes) { this.relayCount = 0; this.sessionHeader = sessionHeader; this.sessionKey = sessionKey; this.sessionNodes = sessionNodes; } /** * * Creates a Session object using a JSON string * @param {String} json - JSON string. * @returns {Session} - Session object. * @memberof Session */ Session.fromJSON = function (json) { var jsonObject = JSON.parse(json); var sessionHeader = session_header_1.SessionHeader.fromJSON(JSON.stringify(jsonObject.session.header)); var sessionNodes = []; if (jsonObject.session.nodes !== undefined && Array.isArray(jsonObject.session.nodes)) { for (var i = 0; i < jsonObject.session.nodes.length; i++) { sessionNodes.push(node_1.Node.fromJSON(JSON.stringify(jsonObject.session.nodes[i]))); } } return new Session(sessionHeader, jsonObject.session.key, sessionNodes); }; Session.prototype.relayPlus = function (v) { this.relayCount = this.relayCount + v; }; /** * Returns whether or not a node is part of this session * @param {Node} node - Node object to verify if exists in the current session. * @returns {boolean} whether or not the node is part of this session * @memberof Session */ Session.prototype.isNodeInSession = function (node) { for (var i = 0; i < this.sessionNodes.length; i++) { var sessionNode = this.sessionNodes[i]; if (sessionNode.address.toUpperCase() === node.address.toUpperCase()) { return true; } } return false; }; /** * Returns a random session node that haven't been part of the current consensus * @memberof Session */ Session.prototype.getUniqueSessionNode = function () { var nodes = this.sessionNodes; if (nodes !== undefined && nodes.length > 0) { var availableNodes_1 = []; nodes.forEach(function (node) { if (!node.alreadyInConsensus) { availableNodes_1.push(node); } }); if (availableNodes_1 !== undefined && availableNodes_1.length > 0) { return availableNodes_1[Math.floor(Math.random() * availableNodes_1.length)]; } else { return new Error("Failed to retrieve a Consensus ready Session node, list is empty"); } } return new Error("Failed to retrieve a Session node, list is empty"); }; /** * Returns a random session node * @memberof Session */ Session.prototype.getSessionNode = function () { var nodes = this.sessionNodes; if (nodes !== undefined && nodes.length > 0) { return nodes[Math.floor(Math.random() * nodes.length)]; } return new Error("Failed to retrieve a Session node, list is empty"); }; /** * * Creates a JSON object with the Session properties * @returns {JSON} - JSON Object. * @memberof Session */ Session.prototype.toJSON = function () { return { header: this.sessionHeader.toJSON(), key: this.sessionKey, nodes: JSON.parse(JSON.stringify(this.sessionNodes)) }; }; /** * * Check if the Session object is valid * @returns {boolean} - True or false. * @memberof Session */ Session.prototype.isValid = function () { return (this.sessionHeader.isValid() && this.sessionKey !== undefined && this.sessionNodes !== undefined); }; return Session; }()); exports.Session = Session; //# sourceMappingURL=session.js.map