UNPKG

diffusion

Version:

Diffusion JavaScript client

83 lines (82 loc) 2.79 kB
"use strict"; /** * @module Session */ Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionId = void 0; var errors_1 = require("./../../errors/errors"); var Long = require("long"); var PADDING = '0000000000000000'; /** * A session ID */ var SessionId = /** @class */ (function () { /** * Create a new session ID * * @param server server part of the ID * @param value value part of the ID */ function SessionId(server, value) { this.server = server; this.value = value; } /** * Create a new session ID from a string * * @param str the string representation of a session ID * @return a new session ID * @throws an {@link IllegalArgumentError} if the string is not a valid session ID */ SessionId.fromString = function (str) { var parts = str.split('-'); var sessionId; if (parts.length !== 2) { throw new errors_1.IllegalArgumentError('SessionId must consist of two hexadecimal parts, joined with a \'-\''); } try { var server = Long.fromString(parts[0], false, 16); var value = Long.fromString(parts[1], false, 16); sessionId = new SessionId(server, value); } catch (e) { throw new errors_1.IllegalArgumentError('SessionId must consist of two hexadecimal parts, joined with a \'-\''); } return sessionId; }; /** * Determines if a given string is a valid SessionID. If true, the returned * value will be the parsed SessionID * * @param str the session id string * @returns `false` if the string cannot be parsed, or a constructed SessionID */ SessionId.validate = function (str) { if (str instanceof SessionId) { return str; } if (str.length === 0 || str.indexOf('-') < 0 || str.indexOf(' ') >= 0) { return false; } try { // attempt to construct an actual session id; catch any errors thrown by the Long#fromString methods return SessionId.fromString(str); } catch (e) { return false; } }; /** * Convert the session ID to a string * * @return a string representation of the session ID */ SessionId.prototype.toString = function () { // it appears that converting back into strings requires unsigned longs var server = PADDING + this.server.toUnsigned().toString(16); var client = PADDING + this.value.toUnsigned().toString(16); return (server.substr(server.length - 16) + '-' + client.substr(client.length - 16)).toLowerCase(); }; return SessionId; }()); exports.SessionId = SessionId;