mitra-interactions-sdk 1.0.71 → 1.0.72

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.
package/dist/index.js CHANGED
@@ -50,12 +50,15 @@ function openAuthPopup(url, expectedOrigin) {
50
50
  }
51
51
  function autoConfigureFromLogin(response, authUrl, projectId) {
52
52
  if (!response.baseURL) return;
53
+ const previous = isConfigured() ? getConfig() : void 0;
53
54
  configureSdkMitra({
54
55
  baseURL: response.baseURL,
55
56
  token: response.token,
56
57
  authUrl,
57
58
  projectId,
58
- ...response.integrationURL ? { integrationURL: response.integrationURL } : {}
59
+ ...response.integrationURL ? { integrationURL: response.integrationURL } : {},
60
+ ...(previous == null ? void 0 : previous.onTokenRefresh) ? { onTokenRefresh: previous.onTokenRefresh } : {},
61
+ ...(previous == null ? void 0 : previous.onSessionExpired) ? { onSessionExpired: previous.onSessionExpired } : {}
59
62
  });
60
63
  }
61
64
  function buildAuthUrl(authUrl, method, projectId, opts) {
@@ -393,7 +396,8 @@ function withServerFunctionProof(headers, proof = readServerFunctionProof()) {
393
396
  }
394
397
 
395
398
  // src/session-watch.ts
396
- var RENEW_BEFORE_MS = 6e4;
399
+ var RENEW_BEFORE_MIN_MS = 6e4;
400
+ var RENEW_BEFORE_MAX_MS = 10 * 6e4;
397
401
  var RETRY_MS = 1e4;
398
402
  var MAX_TIMEOUT_MS = 2147483647;
399
403
  var refresher = null;
@@ -410,6 +414,14 @@ function limitedSessionExpiry(token) {
410
414
  if (!payload || typeof payload.authTime !== "number" || typeof payload.exp !== "number") return null;
411
415
  return payload.exp * 1e3;
412
416
  }
417
+ function renewBeforeMs() {
418
+ var _a;
419
+ if (!isConfigured()) return RENEW_BEFORE_MIN_MS;
420
+ const payload = decodeJwtPayload((_a = getConfig().token) != null ? _a : "");
421
+ const life = payload && typeof payload.exp === "number" && typeof payload.iat === "number" ? (payload.exp - payload.iat) * 1e3 : 0;
422
+ if (life <= 0) return RENEW_BEFORE_MIN_MS;
423
+ return Math.min(RENEW_BEFORE_MAX_MS, Math.max(RENEW_BEFORE_MIN_MS, Math.floor(life / 3)));
424
+ }
413
425
  function currentExpiry() {
414
426
  if (typeof window === "undefined" || !isConfigured()) return null;
415
427
  const config = getConfig();
@@ -432,7 +444,7 @@ function watchSession() {
432
444
  const expiry = currentExpiry();
433
445
  if (expiry == null) return;
434
446
  listenForReturn();
435
- let delay = Math.max(expiry - Date.now() - RENEW_BEFORE_MS, 0);
447
+ let delay = Math.max(expiry - Date.now() - renewBeforeMs(), 0);
436
448
  if (expiryAtLastAttempt != null && expiry <= expiryAtLastAttempt + 1e3) {
437
449
  delay = Math.max(expiry - Date.now() + 1e3, RETRY_MS);
438
450
  }
@@ -459,7 +471,7 @@ function onReturn() {
459
471
  const expiry = currentExpiry();
460
472
  if (expiry == null) return;
461
473
  const atSessionEnd = expiryAtLastAttempt != null && expiry <= expiryAtLastAttempt + 1e3;
462
- if (expiry - Date.now() <= RENEW_BEFORE_MS && (!atSessionEnd || Date.now() >= expiry)) {
474
+ if (expiry - Date.now() <= renewBeforeMs() && (!atSessionEnd || Date.now() >= expiry)) {
463
475
  void renewNow();
464
476
  } else {
465
477
  watchSession();