@tiba-spark/client-shared-lib 26.1.0-307 → 26.1.0-309

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -112406,6 +112406,32 @@ class AuthTokenService {
112406
112406
  this.setConflictDetected = (error) => new SetConflictDetected(error);
112407
112407
  this.clearConflictDetected = () => new ClearConflictDetected();
112408
112408
  }
112409
+ // refreshInterval: early expirty offset
112410
+ isTokenExpired(token, refreshInterval = 0) {
112411
+ const expiration = this.parseJwtExp(token);
112412
+ if (expiration === null) {
112413
+ return true;
112414
+ }
112415
+ return new Date() > new Date(moment.unix(expiration).add(-refreshInterval * 0.1, 'milliseconds').format());
112416
+ }
112417
+ parseJwtExp(token) {
112418
+ try {
112419
+ const payloadSegment = token.split('.')[1];
112420
+ if (!payloadSegment) {
112421
+ return null;
112422
+ }
112423
+ const base64 = payloadSegment.replace(/-/g, '+').replace(/_/g, '/');
112424
+ const padding = '='.repeat((4 - (base64.length % 4)) % 4);
112425
+ if (typeof atob === 'undefined') {
112426
+ return null;
112427
+ }
112428
+ const payload = JSON.parse(atob(base64 + padding));
112429
+ return typeof payload.exp === 'number' ? payload.exp : null;
112430
+ }
112431
+ catch {
112432
+ return null;
112433
+ }
112434
+ }
112409
112435
  handleRequest(req, next) {
112410
112436
  req = this.setRequestHeaders(req);
112411
112437
  let request = next.handle(req);
@@ -113306,12 +113332,14 @@ class SignalR {
113306
113332
  this.zoneChanged = (zoneDto, parkId) => new ZoneChangedAction(zoneDto, parkId);
113307
113333
  this.initializeSmartParkConnection = (smartpark) => new AddSmartparkAction(smartpark);
113308
113334
  this.facilityRemoteCommandExecuted = (facilityRemoteCommandEvent) => new FacilityRemoteCommandExecutedAction(facilityRemoteCommandEvent);
113335
+ this.signalRRefreshToken = () => new SignalRRefreshTokenAction();
113309
113336
  this.store = injector.get(Store);
113310
113337
  this.realtimeEventService = injector.get(RealtimeEventService);
113311
113338
  this.notificationEventService = injector.get(NotificationEventService);
113312
113339
  this.sessionStorageService = injector.get(SessionStorageService);
113313
113340
  this.appConfig = injector.get(AppConfigService);
113314
113341
  this.generalActionService = injector.get(GeneralActionService);
113342
+ this.authTokenService = injector.get(AuthTokenService);
113315
113343
  }
113316
113344
  initRealtimeEventListeners() {
113317
113345
  const config = this.appConfig.getConfig();
@@ -113325,7 +113353,7 @@ class SignalR {
113325
113353
  skipNegotiation: true, // Skip the negotiation step
113326
113354
  transport: HttpTransportType.WebSockets // Use WebSockets directly
113327
113355
  })
113328
- .withAutomaticReconnect(new TibaRetryPolicy(maxConsecutiveFailedRetryCount))
113356
+ .withAutomaticReconnect(new TibaRetryPolicy(maxConsecutiveFailedRetryCount, this.injector))
113329
113357
  .build();
113330
113358
  this.connection.keepAliveIntervalInMilliseconds = 8000;
113331
113359
  this.connection.serverTimeoutInMilliseconds = 30000;
@@ -113557,8 +113585,14 @@ class SignalR {
113557
113585
  this.handleRealTimeAwaitingSubscriptions();
113558
113586
  this.handleNotificationAwaitingSubscription();
113559
113587
  })
113560
- .catch(async (e) => {
113561
- this.retryConnection(connection, 1);
113588
+ .catch(async (error) => {
113589
+ if (isUnauthorizedOrExpired(error, this.sessionStorageService.accessToken, this.authTokenService)) {
113590
+ datadogLogs.logger.info(`SignalR initial connect -> unauthorized. Message: ${error?.message}`);
113591
+ this.signalRRefreshToken();
113592
+ }
113593
+ else {
113594
+ this.retryConnection(connection, 1);
113595
+ }
113562
113596
  });
113563
113597
  }
113564
113598
  _disconnect(connection) {
@@ -113581,10 +113615,16 @@ class SignalR {
113581
113615
  this.handleRealTimeAwaitingSubscriptions();
113582
113616
  this.handleNotificationAwaitingSubscription();
113583
113617
  })
113584
- .catch(() => {
113585
- const config = this.appConfig.getConfig();
113586
- if (retryCount <= config.signalR.maxConsecutiveFailedRetryCount) {
113587
- this.retryConnection(connection, retryCount + 1);
113618
+ .catch((error) => {
113619
+ if (isUnauthorizedOrExpired(error, this.sessionStorageService.accessToken, this.authTokenService)) {
113620
+ datadogLogs.logger.info(`SignalR retry connect -> unauthorized. Message: ${error?.message}`);
113621
+ this.signalRRefreshToken();
113622
+ }
113623
+ else {
113624
+ const config = this.appConfig.getConfig();
113625
+ if (retryCount <= config.signalR.maxConsecutiveFailedRetryCount) {
113626
+ this.retryConnection(connection, retryCount + 1);
113627
+ }
113588
113628
  }
113589
113629
  });
113590
113630
  }, getSecondsBackoff(retryCount));
@@ -113648,24 +113688,26 @@ __decorate([
113648
113688
  __decorate([
113649
113689
  Dispatch()
113650
113690
  ], SignalR.prototype, "facilityRemoteCommandExecuted", void 0);
113691
+ __decorate([
113692
+ Dispatch()
113693
+ ], SignalR.prototype, "signalRRefreshToken", void 0);
113651
113694
  class TibaRetryPolicy {
113652
- constructor(maxConsecutiveFailedRetryCount) {
113695
+ constructor(maxConsecutiveFailedRetryCount, injector) {
113653
113696
  this.signalRRefreshToken = () => new SignalRRefreshTokenAction();
113654
113697
  this.maxConsecutiveFailedRetryCount = maxConsecutiveFailedRetryCount;
113698
+ this.sessionStorageService = injector.get(SessionStorageService);
113699
+ this.authTokenService = injector.get(AuthTokenService);
113655
113700
  }
113656
113701
  nextRetryDelayInMilliseconds(retryContext) {
113657
113702
  let milliseconds = null;
113658
113703
  const currentRetryCount = retryContext.previousRetryCount + 1;
113659
- if (currentRetryCount < this.maxConsecutiveFailedRetryCount) {
113660
- const unauthorized = retryContext.retryReason.message.includes(`Status code '${HttpStatusCode$4.Unauthorized}'`);
113661
- if (unauthorized) {
113662
- datadogLogs.logger.info(`TibaRetryPolicy -> unauthorized token. Message: ${retryContext.retryReason.message}`);
113663
- this.signalRRefreshToken();
113664
- }
113665
- else {
113666
- datadogLogs.logger.info(`TibaRetryPolicy -> failed negotiation #${currentRetryCount}. Message: ${retryContext.retryReason.message}`);
113667
- milliseconds = getSecondsBackoff(currentRetryCount);
113668
- }
113704
+ if (isUnauthorizedOrExpired(retryContext.retryReason, this.sessionStorageService.accessToken, this.authTokenService)) {
113705
+ datadogLogs.logger.info(`TibaRetryPolicy -> unauthorized token. Message: ${retryContext.retryReason.message}`);
113706
+ this.signalRRefreshToken();
113707
+ }
113708
+ else if (currentRetryCount < this.maxConsecutiveFailedRetryCount) {
113709
+ datadogLogs.logger.info(`TibaRetryPolicy -> failed negotiation #${currentRetryCount}. Message: ${retryContext.retryReason.message}`);
113710
+ milliseconds = getSecondsBackoff(currentRetryCount);
113669
113711
  }
113670
113712
  return milliseconds;
113671
113713
  }
@@ -113684,6 +113726,10 @@ function getSecondsBackoff(retryCount) {
113684
113726
  const [minSeconds, maxSeconds] = delaysTableInSeconds[retryCount] || [0, 0];
113685
113727
  return (Math.floor(Math.random() * (maxSeconds - minSeconds + 1)) + minSeconds) * ONE_SECOND_IN_MILLISECONDS;
113686
113728
  }
113729
+ function isUnauthorizedOrExpired(error, token, authTokenService) {
113730
+ const unauthorized = error?.message?.includes(`Status code '${HttpStatusCode$4.Unauthorized}'`);
113731
+ return unauthorized || authTokenService.isTokenExpired(token);
113732
+ }
113687
113733
 
113688
113734
  class ParkService {
113689
113735
  constructor(injector) {