UNPKG

sinch-rtc

Version:

RTC JavaScript/Web SDK

65 lines 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserInstance = void 0; const TimeService_1 = require("../utils/TimeService"); class UserInstance { static parse(data) { var _a; const json = JSON.parse(data); return new UserInstance(Object.assign(Object.assign({}, json.instance), { expireAt: ((_a = json.instance) === null || _a === void 0 ? void 0 : _a.expireAt) ? new Date(Date.parse(json.instance.expireAt)) : undefined }), json.config, new Date(Date.parse(json.iat))); } constructor(instance, config, iat) { this.instance = instance; this.config = config; this.iat = iat; this.timeService = TimeService_1.TimeServiceFactory.create(); } hasExpired() { return (this.instance.expireAt != undefined && this.instance.expireAt.getTime() < this.timeService.now.getTime()); } isShortLived(extendWindowDays) { // We distinguish between short lived and lonh lived instances. // If `sinch:rtc:instance:exp` JWT claim is not set in the JWT used for registering or updating the instance, we would try request // extending instance's TTL within a relatively long margin before the instance expires (180 days). // // However if instance is registered with some specific short `sinch:rtc:instance:exp` claim, it might be not possible to use this logic // as every instance would be prolonged every time it's created. // // In such scenario we prolong its TTL after 50% of it's livespan. // See `UserInstance.shouldProlong` method implementation. return this.Ttl <= (extendWindowDays + 1) * UserInstance.DAY_IN_MS; } get Ttl() { var _a, _b; return ((_a = this.instance) === null || _a === void 0 ? void 0 : _a.expireAt) ? ((_b = this.instance) === null || _b === void 0 ? void 0 : _b.expireAt.getTime()) - this.iat.getTime() : Number.MAX_VALUE; } shouldProlong(extendTtlWindowDays) { return (this.expiresIn() <= (this.isShortLived(extendTtlWindowDays) ? this.Ttl / 2 : extendTtlWindowDays * UserInstance.DAY_IN_MS)); } expiresIn() { return this.instance.expireAt ? this.instance.expireAt.getTime() - this.timeService.now.getTime() : Number.MAX_VALUE; } get id() { return this.instance.id; } toString() { return JSON.stringify({ instance: this.instance, config: this.config, iat: this.iat, }); } } exports.UserInstance = UserInstance; UserInstance.DAY_IN_MS = 24 * 3600 * 1000; //# sourceMappingURL=UserInstance.js.map