@bytescale/sdk 3.42.0 → 3.44.0

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.
@@ -3076,6 +3076,85 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
3076
3076
  }
3077
3077
  }]);
3078
3078
  }();
3079
+ ;// CONCATENATED MODULE: ./src/private/Scheduler.ts
3080
+ function Scheduler_typeof(o) { "@babel/helpers - typeof"; return Scheduler_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, Scheduler_typeof(o); }
3081
+ function Scheduler_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3082
+ function Scheduler_defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, Scheduler_toPropertyKey(descriptor.key), descriptor); } }
3083
+ function Scheduler_createClass(Constructor, protoProps, staticProps) { if (protoProps) Scheduler_defineProperties(Constructor.prototype, protoProps); if (staticProps) Scheduler_defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
3084
+ function Scheduler_toPropertyKey(t) { var i = Scheduler_toPrimitive(t, "string"); return "symbol" == Scheduler_typeof(i) ? i : i + ""; }
3085
+ function Scheduler_toPrimitive(t, r) { if ("object" != Scheduler_typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != Scheduler_typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
3086
+
3087
+ /**
3088
+ * -------------------------
3089
+ * Q: Why not use 'setTimeout'?
3090
+ * -------------------------
3091
+ * A: setTimeout can be paused (e.g., during hibernation), risking JWT expiration before it triggers.
3092
+ * We therefore use a scheduler to check wall-clock time every second and execute the callback at the scheduled time.
3093
+ * -------------------------
3094
+ */
3095
+ var Scheduler = /*#__PURE__*/function () {
3096
+ function Scheduler() {
3097
+ Scheduler_classCallCheck(this, Scheduler);
3098
+ this.callbacks = {};
3099
+ this.nextId = 0;
3100
+ this.intervalId = undefined;
3101
+ }
3102
+ return Scheduler_createClass(Scheduler, [{
3103
+ key: "schedule",
3104
+ value: function schedule(epoch, callback) {
3105
+ var handle = this.nextId++;
3106
+ this.callbacks[handle] = {
3107
+ epoch: epoch,
3108
+ callback: callback
3109
+ };
3110
+ this.startInterval();
3111
+ return handle;
3112
+ }
3113
+ }, {
3114
+ key: "unschedule",
3115
+ value: function unschedule(handle) {
3116
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
3117
+ delete this.callbacks[handle];
3118
+ if (Object.keys(this.callbacks).length === 0) {
3119
+ this.stopInterval();
3120
+ }
3121
+ }
3122
+ }, {
3123
+ key: "startInterval",
3124
+ value: function startInterval() {
3125
+ var _this = this;
3126
+ if (this.intervalId === undefined) {
3127
+ this.intervalId = setInterval(function () {
3128
+ return _this.checkCallbacks();
3129
+ }, 1000);
3130
+ }
3131
+ }
3132
+ }, {
3133
+ key: "stopInterval",
3134
+ value: function stopInterval() {
3135
+ if (this.intervalId !== undefined) {
3136
+ clearInterval(this.intervalId);
3137
+ this.intervalId = undefined;
3138
+ }
3139
+ }
3140
+ }, {
3141
+ key: "checkCallbacks",
3142
+ value: function checkCallbacks() {
3143
+ var now = Date.now();
3144
+ for (var handleStr in this.callbacks) {
3145
+ var handle = parseInt(handleStr);
3146
+ if (this.callbacks[handle].epoch <= now) {
3147
+ try {
3148
+ this.callbacks[handle].callback();
3149
+ } catch (e) {
3150
+ ConsoleUtils.error("Unhandled error from scheduled callback: ".concat(e));
3151
+ }
3152
+ this.unschedule(handle);
3153
+ }
3154
+ }
3155
+ }
3156
+ }]);
3157
+ }();
3079
3158
  ;// CONCATENATED MODULE: ./src/public/browser/AuthManagerBrowser.ts
3080
3159
  function AuthManagerBrowser_empty() {}
3081
3160
  function AuthManagerBrowser_awaitIgnored(value, direct) {
@@ -3159,6 +3238,7 @@ function AuthManagerBrowser_toPrimitive(t, r) { if ("object" != AuthManagerBrows
3159
3238
 
3160
3239
 
3161
3240
 
3241
+
3162
3242
  var AuthManagerImpl = /*#__PURE__*/function () {
3163
3243
  function AuthManagerImpl(serviceWorkerUtils) {
3164
3244
  AuthManagerBrowser_classCallCheck(this, AuthManagerImpl);
@@ -3168,9 +3248,9 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3168
3248
  this.contentTypeJson = "application/json";
3169
3249
  this.contentTypeText = "text/plain";
3170
3250
  this.minJwtTtlSeconds = 10;
3171
- this.maxJwtTtlSeconds = 2147483; // Max value for window.setTimeout is 2147483647ms -- if we go over this, the timeout fires immediately.
3172
3251
  this.retryAuthAfterErrorSeconds = 5;
3173
3252
  this.refreshBeforeExpirySeconds = 20;
3253
+ this.scheduler = new Scheduler();
3174
3254
  this.authSessionMutex = AuthSessionState.getMutex();
3175
3255
  }
3176
3256
  return AuthManagerBrowser_createClass(AuthManagerImpl, [{
@@ -3227,7 +3307,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3227
3307
  AuthSessionState.setSession(undefined);
3228
3308
  session.isActive = false;
3229
3309
  if (session.accessTokenRefreshHandle !== undefined) {
3230
- clearTimeout(session.accessTokenRefreshHandle);
3310
+ _this2.scheduler.unschedule(session.accessTokenRefreshHandle);
3231
3311
  }
3232
3312
  return AuthManagerBrowser_await(_this2.deleteAccessToken(session.params), function () {
3233
3313
  return AuthManagerBrowser_invokeIgnored(function () {
@@ -3254,7 +3334,10 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3254
3334
  if (!session.isActive) {
3255
3335
  return;
3256
3336
  }
3257
- var timeout = _this3.retryAuthAfterErrorSeconds;
3337
+ var secondsFromNow = function secondsFromNow(seconds) {
3338
+ return Date.now() + seconds * 1000;
3339
+ };
3340
+ var expires = secondsFromNow(_this3.retryAuthAfterErrorSeconds);
3258
3341
  return AuthManagerBrowser_continueIgnored(AuthManagerBrowser_finallyRethrows(function () {
3259
3342
  return AuthManagerBrowser_catch(function () {
3260
3343
  var _getAccessToken = _this3.getAccessToken,
@@ -3276,7 +3359,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3276
3359
  key: "Authorization",
3277
3360
  value: "Bearer ".concat(jwt)
3278
3361
  }],
3279
- expires: Date.now() + setTokenResult.ttlSeconds * 1000,
3362
+ expires: secondsFromNow(setTokenResult.ttlSeconds),
3280
3363
  urlPrefix: "".concat(_this3.getCdnUrl(session.params), "/").concat(session.params.accountId, "/")
3281
3364
  }]
3282
3365
  }, session.authServiceWorker, _this3.serviceWorkerScriptFieldName), function () {
@@ -3290,13 +3373,12 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3290
3373
  }
3291
3374
  }, function () {
3292
3375
  var desiredTtl = setTokenResult.ttlSeconds - _this3.refreshBeforeExpirySeconds;
3293
- timeout = Math.max(desiredTtl, _this3.minJwtTtlSeconds);
3294
- if (desiredTtl !== timeout) {
3295
- ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(timeout, " seconds before refreshing."));
3376
+ var actualTtl = Math.max(desiredTtl, _this3.minJwtTtlSeconds);
3377
+ if (desiredTtl !== actualTtl) {
3378
+ ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(actualTtl, " seconds before refreshing."));
3296
3379
  }
3297
- // There's no need to print a warning for this: it's OK to silently request the JWT before it expires. Also, this is 24 days in this case!
3380
+ expires = secondsFromNow(actualTtl);
3298
3381
  // Set this at the end, as it's also used to signal 'isAuthSessionReady', so must be set after configuring the Service Worker, etc.
3299
- timeout = Math.min(timeout, _this3.maxJwtTtlSeconds);
3300
3382
  session.accessToken = setTokenResult.accessToken;
3301
3383
  });
3302
3384
  });
@@ -3307,13 +3389,15 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3307
3389
  ConsoleUtils.warn("Unable to refresh JWT access token: ".concat(e));
3308
3390
  });
3309
3391
  }, function (_wasThrown, _result) {
3310
- session.accessTokenRefreshHandle = window.setTimeout(function () {
3392
+ // 'setTimeout' can be paused (e.g., during hibernation), risking JWT expiration before it triggers. We use a
3393
+ // scheduler to check wall-clock time every second and execute the callback at the scheduled time (below).
3394
+ session.accessTokenRefreshHandle = _this3.scheduler.schedule(expires, function () {
3311
3395
  _this3.refreshAccessToken(session).then(function () {},
3312
3396
  // Should not occur, as this method shouldn't throw errors.
3313
3397
  function (e) {
3314
3398
  return ConsoleUtils.error("Unexpected error when refreshing JWT access token: ".concat(e));
3315
3399
  });
3316
- }, timeout * 1000);
3400
+ });
3317
3401
  return AuthManagerBrowser_rethrow(_wasThrown, _result);
3318
3402
  }));
3319
3403
  }))));
@@ -3061,6 +3061,85 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
3061
3061
  }
3062
3062
  }]);
3063
3063
  }();
3064
+ ;// CONCATENATED MODULE: ./src/private/Scheduler.ts
3065
+ function Scheduler_typeof(o) { "@babel/helpers - typeof"; return Scheduler_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, Scheduler_typeof(o); }
3066
+ function Scheduler_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3067
+ function Scheduler_defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, Scheduler_toPropertyKey(descriptor.key), descriptor); } }
3068
+ function Scheduler_createClass(Constructor, protoProps, staticProps) { if (protoProps) Scheduler_defineProperties(Constructor.prototype, protoProps); if (staticProps) Scheduler_defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
3069
+ function Scheduler_toPropertyKey(t) { var i = Scheduler_toPrimitive(t, "string"); return "symbol" == Scheduler_typeof(i) ? i : i + ""; }
3070
+ function Scheduler_toPrimitive(t, r) { if ("object" != Scheduler_typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != Scheduler_typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
3071
+
3072
+ /**
3073
+ * -------------------------
3074
+ * Q: Why not use 'setTimeout'?
3075
+ * -------------------------
3076
+ * A: setTimeout can be paused (e.g., during hibernation), risking JWT expiration before it triggers.
3077
+ * We therefore use a scheduler to check wall-clock time every second and execute the callback at the scheduled time.
3078
+ * -------------------------
3079
+ */
3080
+ var Scheduler = /*#__PURE__*/function () {
3081
+ function Scheduler() {
3082
+ Scheduler_classCallCheck(this, Scheduler);
3083
+ this.callbacks = {};
3084
+ this.nextId = 0;
3085
+ this.intervalId = undefined;
3086
+ }
3087
+ return Scheduler_createClass(Scheduler, [{
3088
+ key: "schedule",
3089
+ value: function schedule(epoch, callback) {
3090
+ var handle = this.nextId++;
3091
+ this.callbacks[handle] = {
3092
+ epoch: epoch,
3093
+ callback: callback
3094
+ };
3095
+ this.startInterval();
3096
+ return handle;
3097
+ }
3098
+ }, {
3099
+ key: "unschedule",
3100
+ value: function unschedule(handle) {
3101
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
3102
+ delete this.callbacks[handle];
3103
+ if (Object.keys(this.callbacks).length === 0) {
3104
+ this.stopInterval();
3105
+ }
3106
+ }
3107
+ }, {
3108
+ key: "startInterval",
3109
+ value: function startInterval() {
3110
+ var _this = this;
3111
+ if (this.intervalId === undefined) {
3112
+ this.intervalId = setInterval(function () {
3113
+ return _this.checkCallbacks();
3114
+ }, 1000);
3115
+ }
3116
+ }
3117
+ }, {
3118
+ key: "stopInterval",
3119
+ value: function stopInterval() {
3120
+ if (this.intervalId !== undefined) {
3121
+ clearInterval(this.intervalId);
3122
+ this.intervalId = undefined;
3123
+ }
3124
+ }
3125
+ }, {
3126
+ key: "checkCallbacks",
3127
+ value: function checkCallbacks() {
3128
+ var now = Date.now();
3129
+ for (var handleStr in this.callbacks) {
3130
+ var handle = parseInt(handleStr);
3131
+ if (this.callbacks[handle].epoch <= now) {
3132
+ try {
3133
+ this.callbacks[handle].callback();
3134
+ } catch (e) {
3135
+ ConsoleUtils.error("Unhandled error from scheduled callback: ".concat(e));
3136
+ }
3137
+ this.unschedule(handle);
3138
+ }
3139
+ }
3140
+ }
3141
+ }]);
3142
+ }();
3064
3143
  ;// CONCATENATED MODULE: ./src/public/browser/AuthManagerBrowser.ts
3065
3144
  function AuthManagerBrowser_empty() {}
3066
3145
  function AuthManagerBrowser_awaitIgnored(value, direct) {
@@ -3144,6 +3223,7 @@ function AuthManagerBrowser_toPrimitive(t, r) { if ("object" != AuthManagerBrows
3144
3223
 
3145
3224
 
3146
3225
 
3226
+
3147
3227
  var AuthManagerImpl = /*#__PURE__*/function () {
3148
3228
  function AuthManagerImpl(serviceWorkerUtils) {
3149
3229
  AuthManagerBrowser_classCallCheck(this, AuthManagerImpl);
@@ -3153,9 +3233,9 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3153
3233
  this.contentTypeJson = "application/json";
3154
3234
  this.contentTypeText = "text/plain";
3155
3235
  this.minJwtTtlSeconds = 10;
3156
- this.maxJwtTtlSeconds = 2147483; // Max value for window.setTimeout is 2147483647ms -- if we go over this, the timeout fires immediately.
3157
3236
  this.retryAuthAfterErrorSeconds = 5;
3158
3237
  this.refreshBeforeExpirySeconds = 20;
3238
+ this.scheduler = new Scheduler();
3159
3239
  this.authSessionMutex = AuthSessionState.getMutex();
3160
3240
  }
3161
3241
  return AuthManagerBrowser_createClass(AuthManagerImpl, [{
@@ -3212,7 +3292,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3212
3292
  AuthSessionState.setSession(undefined);
3213
3293
  session.isActive = false;
3214
3294
  if (session.accessTokenRefreshHandle !== undefined) {
3215
- clearTimeout(session.accessTokenRefreshHandle);
3295
+ _this2.scheduler.unschedule(session.accessTokenRefreshHandle);
3216
3296
  }
3217
3297
  return AuthManagerBrowser_await(_this2.deleteAccessToken(session.params), function () {
3218
3298
  return AuthManagerBrowser_invokeIgnored(function () {
@@ -3239,7 +3319,10 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3239
3319
  if (!session.isActive) {
3240
3320
  return;
3241
3321
  }
3242
- var timeout = _this3.retryAuthAfterErrorSeconds;
3322
+ var secondsFromNow = function secondsFromNow(seconds) {
3323
+ return Date.now() + seconds * 1000;
3324
+ };
3325
+ var expires = secondsFromNow(_this3.retryAuthAfterErrorSeconds);
3243
3326
  return AuthManagerBrowser_continueIgnored(AuthManagerBrowser_finallyRethrows(function () {
3244
3327
  return AuthManagerBrowser_catch(function () {
3245
3328
  var _getAccessToken = _this3.getAccessToken,
@@ -3261,7 +3344,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3261
3344
  key: "Authorization",
3262
3345
  value: "Bearer ".concat(jwt)
3263
3346
  }],
3264
- expires: Date.now() + setTokenResult.ttlSeconds * 1000,
3347
+ expires: secondsFromNow(setTokenResult.ttlSeconds),
3265
3348
  urlPrefix: "".concat(_this3.getCdnUrl(session.params), "/").concat(session.params.accountId, "/")
3266
3349
  }]
3267
3350
  }, session.authServiceWorker, _this3.serviceWorkerScriptFieldName), function () {
@@ -3275,13 +3358,12 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3275
3358
  }
3276
3359
  }, function () {
3277
3360
  var desiredTtl = setTokenResult.ttlSeconds - _this3.refreshBeforeExpirySeconds;
3278
- timeout = Math.max(desiredTtl, _this3.minJwtTtlSeconds);
3279
- if (desiredTtl !== timeout) {
3280
- ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(timeout, " seconds before refreshing."));
3361
+ var actualTtl = Math.max(desiredTtl, _this3.minJwtTtlSeconds);
3362
+ if (desiredTtl !== actualTtl) {
3363
+ ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(actualTtl, " seconds before refreshing."));
3281
3364
  }
3282
- // There's no need to print a warning for this: it's OK to silently request the JWT before it expires. Also, this is 24 days in this case!
3365
+ expires = secondsFromNow(actualTtl);
3283
3366
  // Set this at the end, as it's also used to signal 'isAuthSessionReady', so must be set after configuring the Service Worker, etc.
3284
- timeout = Math.min(timeout, _this3.maxJwtTtlSeconds);
3285
3367
  session.accessToken = setTokenResult.accessToken;
3286
3368
  });
3287
3369
  });
@@ -3292,13 +3374,15 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3292
3374
  ConsoleUtils.warn("Unable to refresh JWT access token: ".concat(e));
3293
3375
  });
3294
3376
  }, function (_wasThrown, _result) {
3295
- session.accessTokenRefreshHandle = window.setTimeout(function () {
3377
+ // 'setTimeout' can be paused (e.g., during hibernation), risking JWT expiration before it triggers. We use a
3378
+ // scheduler to check wall-clock time every second and execute the callback at the scheduled time (below).
3379
+ session.accessTokenRefreshHandle = _this3.scheduler.schedule(expires, function () {
3296
3380
  _this3.refreshAccessToken(session).then(function () {},
3297
3381
  // Should not occur, as this method shouldn't throw errors.
3298
3382
  function (e) {
3299
3383
  return ConsoleUtils.error("Unexpected error when refreshing JWT access token: ".concat(e));
3300
3384
  });
3301
- }, timeout * 1000);
3385
+ });
3302
3386
  return AuthManagerBrowser_rethrow(_wasThrown, _result);
3303
3387
  }));
3304
3388
  }))));
@@ -0,0 +1,18 @@
1
+ /**
2
+ * -------------------------
3
+ * Q: Why not use 'setTimeout'?
4
+ * -------------------------
5
+ * A: setTimeout can be paused (e.g., during hibernation), risking JWT expiration before it triggers.
6
+ * We therefore use a scheduler to check wall-clock time every second and execute the callback at the scheduled time.
7
+ * -------------------------
8
+ */
9
+ export declare class Scheduler {
10
+ private callbacks;
11
+ private nextId;
12
+ private intervalId;
13
+ schedule(epoch: number, callback: () => void): number;
14
+ unschedule(handle: number): void;
15
+ private startInterval;
16
+ private stopInterval;
17
+ private checkCallbacks;
18
+ }
@@ -9,9 +9,9 @@ declare class AuthManagerImpl implements AuthManagerInterface {
9
9
  private readonly contentTypeJson;
10
10
  private readonly contentTypeText;
11
11
  private readonly minJwtTtlSeconds;
12
- private readonly maxJwtTtlSeconds;
13
12
  private readonly retryAuthAfterErrorSeconds;
14
13
  private readonly refreshBeforeExpirySeconds;
14
+ private readonly scheduler;
15
15
  constructor(serviceWorkerUtils: ServiceWorkerUtils<AuthSwSetConfigDto>);
16
16
  isAuthSessionActive(): boolean;
17
17
  isAuthSessionReady(): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytescale/sdk",
3
- "version": "3.42.0",
3
+ "version": "3.44.0",
4
4
  "description": "Bytescale JavaScript SDK",
5
5
  "author": "Bytescale <hello@bytescale.com> (https://www.bytescale.com)",
6
6
  "license": "MIT",