@bytescale/sdk 3.42.0 → 3.43.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,77 @@ 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
+ var Scheduler = /*#__PURE__*/function () {
3088
+ function Scheduler() {
3089
+ Scheduler_classCallCheck(this, Scheduler);
3090
+ this.callbacks = {};
3091
+ this.nextId = 0;
3092
+ this.intervalId = undefined;
3093
+ }
3094
+ return Scheduler_createClass(Scheduler, [{
3095
+ key: "schedule",
3096
+ value: function schedule(epoch, callback) {
3097
+ var handle = this.nextId++;
3098
+ this.callbacks[handle] = {
3099
+ epoch: epoch,
3100
+ callback: callback
3101
+ };
3102
+ this.startInterval();
3103
+ return handle;
3104
+ }
3105
+ }, {
3106
+ key: "unschedule",
3107
+ value: function unschedule(handle) {
3108
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
3109
+ delete this.callbacks[handle];
3110
+ if (Object.keys(this.callbacks).length === 0) {
3111
+ this.stopInterval();
3112
+ }
3113
+ }
3114
+ }, {
3115
+ key: "startInterval",
3116
+ value: function startInterval() {
3117
+ var _this = this;
3118
+ if (this.intervalId === undefined) {
3119
+ this.intervalId = setInterval(function () {
3120
+ return _this.checkCallbacks();
3121
+ }, 1000);
3122
+ }
3123
+ }
3124
+ }, {
3125
+ key: "stopInterval",
3126
+ value: function stopInterval() {
3127
+ if (this.intervalId !== undefined) {
3128
+ clearInterval(this.intervalId);
3129
+ this.intervalId = undefined;
3130
+ }
3131
+ }
3132
+ }, {
3133
+ key: "checkCallbacks",
3134
+ value: function checkCallbacks() {
3135
+ var now = Date.now();
3136
+ for (var handleStr in this.callbacks) {
3137
+ var handle = parseInt(handleStr);
3138
+ if (this.callbacks[handle].epoch <= now) {
3139
+ try {
3140
+ this.callbacks[handle].callback();
3141
+ } catch (e) {
3142
+ ConsoleUtils.error("Unhandled error from scheduled callback: ".concat(e));
3143
+ }
3144
+ this.unschedule(handle);
3145
+ }
3146
+ }
3147
+ }
3148
+ }]);
3149
+ }();
3079
3150
  ;// CONCATENATED MODULE: ./src/public/browser/AuthManagerBrowser.ts
3080
3151
  function AuthManagerBrowser_empty() {}
3081
3152
  function AuthManagerBrowser_awaitIgnored(value, direct) {
@@ -3159,6 +3230,7 @@ function AuthManagerBrowser_toPrimitive(t, r) { if ("object" != AuthManagerBrows
3159
3230
 
3160
3231
 
3161
3232
 
3233
+
3162
3234
  var AuthManagerImpl = /*#__PURE__*/function () {
3163
3235
  function AuthManagerImpl(serviceWorkerUtils) {
3164
3236
  AuthManagerBrowser_classCallCheck(this, AuthManagerImpl);
@@ -3168,9 +3240,9 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3168
3240
  this.contentTypeJson = "application/json";
3169
3241
  this.contentTypeText = "text/plain";
3170
3242
  this.minJwtTtlSeconds = 10;
3171
- this.maxJwtTtlSeconds = 2147483; // Max value for window.setTimeout is 2147483647ms -- if we go over this, the timeout fires immediately.
3172
3243
  this.retryAuthAfterErrorSeconds = 5;
3173
3244
  this.refreshBeforeExpirySeconds = 20;
3245
+ this.scheduler = new Scheduler();
3174
3246
  this.authSessionMutex = AuthSessionState.getMutex();
3175
3247
  }
3176
3248
  return AuthManagerBrowser_createClass(AuthManagerImpl, [{
@@ -3227,7 +3299,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3227
3299
  AuthSessionState.setSession(undefined);
3228
3300
  session.isActive = false;
3229
3301
  if (session.accessTokenRefreshHandle !== undefined) {
3230
- clearTimeout(session.accessTokenRefreshHandle);
3302
+ _this2.scheduler.unschedule(session.accessTokenRefreshHandle);
3231
3303
  }
3232
3304
  return AuthManagerBrowser_await(_this2.deleteAccessToken(session.params), function () {
3233
3305
  return AuthManagerBrowser_invokeIgnored(function () {
@@ -3254,7 +3326,10 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3254
3326
  if (!session.isActive) {
3255
3327
  return;
3256
3328
  }
3257
- var timeout = _this3.retryAuthAfterErrorSeconds;
3329
+ var secondsFromNow = function secondsFromNow(seconds) {
3330
+ return Date.now() + seconds * 1000;
3331
+ };
3332
+ var expires = secondsFromNow(_this3.retryAuthAfterErrorSeconds);
3258
3333
  return AuthManagerBrowser_continueIgnored(AuthManagerBrowser_finallyRethrows(function () {
3259
3334
  return AuthManagerBrowser_catch(function () {
3260
3335
  var _getAccessToken = _this3.getAccessToken,
@@ -3276,7 +3351,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3276
3351
  key: "Authorization",
3277
3352
  value: "Bearer ".concat(jwt)
3278
3353
  }],
3279
- expires: Date.now() + setTokenResult.ttlSeconds * 1000,
3354
+ expires: secondsFromNow(setTokenResult.ttlSeconds),
3280
3355
  urlPrefix: "".concat(_this3.getCdnUrl(session.params), "/").concat(session.params.accountId, "/")
3281
3356
  }]
3282
3357
  }, session.authServiceWorker, _this3.serviceWorkerScriptFieldName), function () {
@@ -3290,13 +3365,12 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3290
3365
  }
3291
3366
  }, function () {
3292
3367
  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."));
3368
+ var actualTtl = Math.max(desiredTtl, _this3.minJwtTtlSeconds);
3369
+ if (desiredTtl !== actualTtl) {
3370
+ ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(actualTtl, " seconds before refreshing."));
3296
3371
  }
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!
3372
+ expires = secondsFromNow(actualTtl);
3298
3373
  // 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
3374
  session.accessToken = setTokenResult.accessToken;
3301
3375
  });
3302
3376
  });
@@ -3307,13 +3381,13 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3307
3381
  ConsoleUtils.warn("Unable to refresh JWT access token: ".concat(e));
3308
3382
  });
3309
3383
  }, function (_wasThrown, _result) {
3310
- session.accessTokenRefreshHandle = window.setTimeout(function () {
3384
+ session.accessTokenRefreshHandle = _this3.scheduler.schedule(expires, function () {
3311
3385
  _this3.refreshAccessToken(session).then(function () {},
3312
3386
  // Should not occur, as this method shouldn't throw errors.
3313
3387
  function (e) {
3314
3388
  return ConsoleUtils.error("Unexpected error when refreshing JWT access token: ".concat(e));
3315
3389
  });
3316
- }, timeout * 1000);
3390
+ });
3317
3391
  return AuthManagerBrowser_rethrow(_wasThrown, _result);
3318
3392
  }));
3319
3393
  }))));
@@ -3061,6 +3061,77 @@ 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
+ var Scheduler = /*#__PURE__*/function () {
3073
+ function Scheduler() {
3074
+ Scheduler_classCallCheck(this, Scheduler);
3075
+ this.callbacks = {};
3076
+ this.nextId = 0;
3077
+ this.intervalId = undefined;
3078
+ }
3079
+ return Scheduler_createClass(Scheduler, [{
3080
+ key: "schedule",
3081
+ value: function schedule(epoch, callback) {
3082
+ var handle = this.nextId++;
3083
+ this.callbacks[handle] = {
3084
+ epoch: epoch,
3085
+ callback: callback
3086
+ };
3087
+ this.startInterval();
3088
+ return handle;
3089
+ }
3090
+ }, {
3091
+ key: "unschedule",
3092
+ value: function unschedule(handle) {
3093
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
3094
+ delete this.callbacks[handle];
3095
+ if (Object.keys(this.callbacks).length === 0) {
3096
+ this.stopInterval();
3097
+ }
3098
+ }
3099
+ }, {
3100
+ key: "startInterval",
3101
+ value: function startInterval() {
3102
+ var _this = this;
3103
+ if (this.intervalId === undefined) {
3104
+ this.intervalId = setInterval(function () {
3105
+ return _this.checkCallbacks();
3106
+ }, 1000);
3107
+ }
3108
+ }
3109
+ }, {
3110
+ key: "stopInterval",
3111
+ value: function stopInterval() {
3112
+ if (this.intervalId !== undefined) {
3113
+ clearInterval(this.intervalId);
3114
+ this.intervalId = undefined;
3115
+ }
3116
+ }
3117
+ }, {
3118
+ key: "checkCallbacks",
3119
+ value: function checkCallbacks() {
3120
+ var now = Date.now();
3121
+ for (var handleStr in this.callbacks) {
3122
+ var handle = parseInt(handleStr);
3123
+ if (this.callbacks[handle].epoch <= now) {
3124
+ try {
3125
+ this.callbacks[handle].callback();
3126
+ } catch (e) {
3127
+ ConsoleUtils.error("Unhandled error from scheduled callback: ".concat(e));
3128
+ }
3129
+ this.unschedule(handle);
3130
+ }
3131
+ }
3132
+ }
3133
+ }]);
3134
+ }();
3064
3135
  ;// CONCATENATED MODULE: ./src/public/browser/AuthManagerBrowser.ts
3065
3136
  function AuthManagerBrowser_empty() {}
3066
3137
  function AuthManagerBrowser_awaitIgnored(value, direct) {
@@ -3144,6 +3215,7 @@ function AuthManagerBrowser_toPrimitive(t, r) { if ("object" != AuthManagerBrows
3144
3215
 
3145
3216
 
3146
3217
 
3218
+
3147
3219
  var AuthManagerImpl = /*#__PURE__*/function () {
3148
3220
  function AuthManagerImpl(serviceWorkerUtils) {
3149
3221
  AuthManagerBrowser_classCallCheck(this, AuthManagerImpl);
@@ -3153,9 +3225,9 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3153
3225
  this.contentTypeJson = "application/json";
3154
3226
  this.contentTypeText = "text/plain";
3155
3227
  this.minJwtTtlSeconds = 10;
3156
- this.maxJwtTtlSeconds = 2147483; // Max value for window.setTimeout is 2147483647ms -- if we go over this, the timeout fires immediately.
3157
3228
  this.retryAuthAfterErrorSeconds = 5;
3158
3229
  this.refreshBeforeExpirySeconds = 20;
3230
+ this.scheduler = new Scheduler();
3159
3231
  this.authSessionMutex = AuthSessionState.getMutex();
3160
3232
  }
3161
3233
  return AuthManagerBrowser_createClass(AuthManagerImpl, [{
@@ -3212,7 +3284,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3212
3284
  AuthSessionState.setSession(undefined);
3213
3285
  session.isActive = false;
3214
3286
  if (session.accessTokenRefreshHandle !== undefined) {
3215
- clearTimeout(session.accessTokenRefreshHandle);
3287
+ _this2.scheduler.unschedule(session.accessTokenRefreshHandle);
3216
3288
  }
3217
3289
  return AuthManagerBrowser_await(_this2.deleteAccessToken(session.params), function () {
3218
3290
  return AuthManagerBrowser_invokeIgnored(function () {
@@ -3239,7 +3311,10 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3239
3311
  if (!session.isActive) {
3240
3312
  return;
3241
3313
  }
3242
- var timeout = _this3.retryAuthAfterErrorSeconds;
3314
+ var secondsFromNow = function secondsFromNow(seconds) {
3315
+ return Date.now() + seconds * 1000;
3316
+ };
3317
+ var expires = secondsFromNow(_this3.retryAuthAfterErrorSeconds);
3243
3318
  return AuthManagerBrowser_continueIgnored(AuthManagerBrowser_finallyRethrows(function () {
3244
3319
  return AuthManagerBrowser_catch(function () {
3245
3320
  var _getAccessToken = _this3.getAccessToken,
@@ -3261,7 +3336,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3261
3336
  key: "Authorization",
3262
3337
  value: "Bearer ".concat(jwt)
3263
3338
  }],
3264
- expires: Date.now() + setTokenResult.ttlSeconds * 1000,
3339
+ expires: secondsFromNow(setTokenResult.ttlSeconds),
3265
3340
  urlPrefix: "".concat(_this3.getCdnUrl(session.params), "/").concat(session.params.accountId, "/")
3266
3341
  }]
3267
3342
  }, session.authServiceWorker, _this3.serviceWorkerScriptFieldName), function () {
@@ -3275,13 +3350,12 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3275
3350
  }
3276
3351
  }, function () {
3277
3352
  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."));
3353
+ var actualTtl = Math.max(desiredTtl, _this3.minJwtTtlSeconds);
3354
+ if (desiredTtl !== actualTtl) {
3355
+ ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(actualTtl, " seconds before refreshing."));
3281
3356
  }
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!
3357
+ expires = secondsFromNow(actualTtl);
3283
3358
  // 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
3359
  session.accessToken = setTokenResult.accessToken;
3286
3360
  });
3287
3361
  });
@@ -3292,13 +3366,13 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3292
3366
  ConsoleUtils.warn("Unable to refresh JWT access token: ".concat(e));
3293
3367
  });
3294
3368
  }, function (_wasThrown, _result) {
3295
- session.accessTokenRefreshHandle = window.setTimeout(function () {
3369
+ session.accessTokenRefreshHandle = _this3.scheduler.schedule(expires, function () {
3296
3370
  _this3.refreshAccessToken(session).then(function () {},
3297
3371
  // Should not occur, as this method shouldn't throw errors.
3298
3372
  function (e) {
3299
3373
  return ConsoleUtils.error("Unexpected error when refreshing JWT access token: ".concat(e));
3300
3374
  });
3301
- }, timeout * 1000);
3375
+ });
3302
3376
  return AuthManagerBrowser_rethrow(_wasThrown, _result);
3303
3377
  }));
3304
3378
  }))));
@@ -0,0 +1,10 @@
1
+ export declare class Scheduler {
2
+ private callbacks;
3
+ private nextId;
4
+ private intervalId;
5
+ schedule(epoch: number, callback: () => void): number;
6
+ unschedule(handle: number): void;
7
+ private startInterval;
8
+ private stopInterval;
9
+ private checkCallbacks;
10
+ }
@@ -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.43.0",
4
4
  "description": "Bytescale JavaScript SDK",
5
5
  "author": "Bytescale <hello@bytescale.com> (https://www.bytescale.com)",
6
6
  "license": "MIT",