@bytescale/sdk 3.40.0 → 3.42.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.
@@ -2572,31 +2572,7 @@ function ServiceWorkerUtils_await(value, then, direct) {
2572
2572
  }
2573
2573
  return then ? value.then(then) : value;
2574
2574
  }
2575
- function ServiceWorkerUtils_catch(body, recover) {
2576
- try {
2577
- var result = body();
2578
- } catch (e) {
2579
- return recover(e);
2580
- }
2581
- if (result && result.then) {
2582
- return result.then(void 0, recover);
2583
- }
2584
- return result;
2585
- }
2586
- function ServiceWorkerUtils_invoke(body, then) {
2587
- var result = body();
2588
- if (result && result.then) {
2589
- return result.then(then);
2590
- }
2591
- return then(result);
2592
- }
2593
2575
  function ServiceWorkerUtils_empty() {}
2594
- function ServiceWorkerUtils_awaitIgnored(value, direct) {
2595
- if (!direct) {
2596
- return value && value.then ? value.then(ServiceWorkerUtils_empty) : Promise.resolve();
2597
- }
2598
- }
2599
- var _iteratorSymbol = /*#__PURE__*/typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator")) : "@@iterator";
2600
2576
  function ServiceWorkerUtils_settle(pact, state, value) {
2601
2577
  if (!pact.s) {
2602
2578
  if (value instanceof ServiceWorkerUtils_Pact) {
@@ -2658,6 +2634,137 @@ var ServiceWorkerUtils_Pact = /*#__PURE__*/function () {
2658
2634
  };
2659
2635
  return _Pact;
2660
2636
  }();
2637
+ function _switch(discriminant, cases) {
2638
+ var dispatchIndex = -1;
2639
+ var awaitBody;
2640
+ outer: {
2641
+ for (var i = 0; i < cases.length; i++) {
2642
+ var test = cases[i][0];
2643
+ if (test) {
2644
+ var testValue = test();
2645
+ if (testValue && testValue.then) {
2646
+ break outer;
2647
+ }
2648
+ if (testValue === discriminant) {
2649
+ dispatchIndex = i;
2650
+ break;
2651
+ }
2652
+ } else {
2653
+ // Found the default case, set it as the pending dispatch case
2654
+ dispatchIndex = i;
2655
+ }
2656
+ }
2657
+ if (dispatchIndex !== -1) {
2658
+ do {
2659
+ var body = cases[dispatchIndex][1];
2660
+ while (!body) {
2661
+ dispatchIndex++;
2662
+ body = cases[dispatchIndex][1];
2663
+ }
2664
+ var result = body();
2665
+ if (result && result.then) {
2666
+ awaitBody = true;
2667
+ break outer;
2668
+ }
2669
+ var fallthroughCheck = cases[dispatchIndex][2];
2670
+ dispatchIndex++;
2671
+ } while (fallthroughCheck && !fallthroughCheck());
2672
+ return result;
2673
+ }
2674
+ }
2675
+ var pact = new ServiceWorkerUtils_Pact();
2676
+ var reject = ServiceWorkerUtils_settle.bind(null, pact, 2);
2677
+ (awaitBody ? result.then(_resumeAfterBody) : testValue.then(_resumeAfterTest)).then(void 0, reject);
2678
+ return pact;
2679
+ function _resumeAfterTest(value) {
2680
+ for (;;) {
2681
+ if (value === discriminant) {
2682
+ dispatchIndex = i;
2683
+ break;
2684
+ }
2685
+ if (++i === cases.length) {
2686
+ if (dispatchIndex !== -1) {
2687
+ break;
2688
+ } else {
2689
+ ServiceWorkerUtils_settle(pact, 1, result);
2690
+ return;
2691
+ }
2692
+ }
2693
+ test = cases[i][0];
2694
+ if (test) {
2695
+ value = test();
2696
+ if (value && value.then) {
2697
+ value.then(_resumeAfterTest).then(void 0, reject);
2698
+ return;
2699
+ }
2700
+ } else {
2701
+ dispatchIndex = i;
2702
+ }
2703
+ }
2704
+ do {
2705
+ var body = cases[dispatchIndex][1];
2706
+ while (!body) {
2707
+ dispatchIndex++;
2708
+ body = cases[dispatchIndex][1];
2709
+ }
2710
+ var result = body();
2711
+ if (result && result.then) {
2712
+ result.then(_resumeAfterBody).then(void 0, reject);
2713
+ return;
2714
+ }
2715
+ var fallthroughCheck = cases[dispatchIndex][2];
2716
+ dispatchIndex++;
2717
+ } while (fallthroughCheck && !fallthroughCheck());
2718
+ ServiceWorkerUtils_settle(pact, 1, result);
2719
+ }
2720
+ function _resumeAfterBody(result) {
2721
+ for (;;) {
2722
+ var fallthroughCheck = cases[dispatchIndex][2];
2723
+ if (!fallthroughCheck || fallthroughCheck()) {
2724
+ break;
2725
+ }
2726
+ dispatchIndex++;
2727
+ var body = cases[dispatchIndex][1];
2728
+ while (!body) {
2729
+ dispatchIndex++;
2730
+ body = cases[dispatchIndex][1];
2731
+ }
2732
+ result = body();
2733
+ if (result && result.then) {
2734
+ result.then(_resumeAfterBody).then(void 0, reject);
2735
+ return;
2736
+ }
2737
+ }
2738
+ ServiceWorkerUtils_settle(pact, 1, result);
2739
+ }
2740
+ }
2741
+ function ServiceWorkerUtils_continue(value, then) {
2742
+ return value && value.then ? value.then(then) : then(value);
2743
+ }
2744
+ function ServiceWorkerUtils_catch(body, recover) {
2745
+ try {
2746
+ var result = body();
2747
+ } catch (e) {
2748
+ return recover(e);
2749
+ }
2750
+ if (result && result.then) {
2751
+ return result.then(void 0, recover);
2752
+ }
2753
+ return result;
2754
+ }
2755
+ function ServiceWorkerUtils_invoke(body, then) {
2756
+ var result = body();
2757
+ if (result && result.then) {
2758
+ return result.then(then);
2759
+ }
2760
+ return then(result);
2761
+ }
2762
+ function ServiceWorkerUtils_awaitIgnored(value, direct) {
2763
+ if (!direct) {
2764
+ return value && value.then ? value.then(ServiceWorkerUtils_empty) : Promise.resolve();
2765
+ }
2766
+ }
2767
+ var _iteratorSymbol = /*#__PURE__*/typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator")) : "@@iterator";
2661
2768
  function ServiceWorkerUtils_isSettledPact(thenable) {
2662
2769
  return thenable instanceof ServiceWorkerUtils_Pact && thenable.s & 1;
2663
2770
  }
@@ -2750,8 +2857,10 @@ function _forOf(target, body, check) {
2750
2857
  return body(values[i]);
2751
2858
  }, check);
2752
2859
  }
2753
- function ServiceWorkerUtils_continue(value, then) {
2754
- return value && value.then ? value.then(then) : then(value);
2860
+ function ServiceWorkerUtils_continueIgnored(value) {
2861
+ if (value && value.then) {
2862
+ return value.then(ServiceWorkerUtils_empty);
2863
+ }
2755
2864
  }
2756
2865
  function ServiceWorkerUtils_typeof(o) { "@babel/helpers - typeof"; return ServiceWorkerUtils_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; }, ServiceWorkerUtils_typeof(o); }
2757
2866
  function ServiceWorkerUtils_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -2760,6 +2869,7 @@ function ServiceWorkerUtils_createClass(Constructor, protoProps, staticProps) {
2760
2869
  function ServiceWorkerUtils_toPropertyKey(t) { var i = ServiceWorkerUtils_toPrimitive(t, "string"); return "symbol" == ServiceWorkerUtils_typeof(i) ? i : i + ""; }
2761
2870
  function ServiceWorkerUtils_toPrimitive(t, r) { if ("object" != ServiceWorkerUtils_typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != ServiceWorkerUtils_typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
2762
2871
 
2872
+
2763
2873
  var ServiceWorkerUtils = /*#__PURE__*/function () {
2764
2874
  function ServiceWorkerUtils() {
2765
2875
  ServiceWorkerUtils_classCallCheck(this, ServiceWorkerUtils);
@@ -2774,10 +2884,13 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2774
2884
  value: function sendMessage(message, config, serviceWorkerScriptFieldName) {
2775
2885
  try {
2776
2886
  var _this = this;
2777
- return ServiceWorkerUtils_await(config.serviceWorkerScope !== undefined ? _this.getActiveServiceWorkerElseRegister(config, message) : _this.registerServiceWorkerValidated(config.serviceWorkerScript, message, serviceWorkerScriptFieldName), function (result) {
2778
- if (!result.messageSent) {
2779
- result.serviceWorker.postMessage(message);
2780
- }
2887
+ return ServiceWorkerUtils_await(_this.ensureActiveServiceWorkerExists(message, config, serviceWorkerScriptFieldName), function (result) {
2888
+ // Message delivery is at least once. We must always assume this as there is only 1 service worker instance and
2889
+ // potentially multiple browser tabs with AuthManager sessions running. As such, even if we ensure exactly-once
2890
+ // delivery within a single tab, there will still be at-least-once delivery across all. As such, we simplfy our
2891
+ // code by using at-least-once delivery here too (hence the following 'postMessage' will already have been called
2892
+ // in the scenario where a new worker is newly installed).
2893
+ result.serviceWorker.postMessage(message);
2781
2894
  return result.config;
2782
2895
  });
2783
2896
  } catch (e) {
@@ -2785,34 +2898,41 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2785
2898
  }
2786
2899
  }
2787
2900
  }, {
2788
- key: "registerServiceWorkerValidated",
2789
- value: function registerServiceWorkerValidated(serviceWorkerScript, init, serviceWorkerScriptFieldName) {
2901
+ key: "ensureActiveServiceWorkerExists",
2902
+ value: function ensureActiveServiceWorkerExists(initMessage, config, serviceWorkerScriptFieldName) {
2790
2903
  try {
2904
+ var _exit = false,
2905
+ _interrupt = false;
2791
2906
  var _this2 = this;
2792
- if (!serviceWorkerScript.startsWith("/")) {
2793
- throw new Error("The '".concat(serviceWorkerScriptFieldName, "' field must start with a '/' and reference a script at the root of your website."));
2794
- }
2795
- var forwardSlashCount = serviceWorkerScript.split("/").length - 1;
2796
- if (forwardSlashCount > 1) {
2797
- ConsoleUtils.warn("The '".concat(serviceWorkerScriptFieldName, "' field should be a root script (e.g. '/script.js'). The Bytescale SDK can only authorize requests originating from webpages that are at the same level as the script or below."));
2798
- }
2799
- return ServiceWorkerUtils_await(_this2.registerServiceWorker(serviceWorkerScript, init));
2800
- } catch (e) {
2801
- return Promise.reject(e);
2802
- }
2803
- }
2804
- }, {
2805
- key: "getActiveServiceWorkerElseRegister",
2806
- value: function getActiveServiceWorkerElseRegister(config, init) {
2807
- try {
2808
- var _this3 = this;
2809
- return ServiceWorkerUtils_await(_this3.getActiveServiceWorker(config.serviceWorkerScope), function (serviceWorker) {
2810
- return serviceWorker !== undefined ? {
2811
- serviceWorker: serviceWorker,
2812
- messageSent: false,
2813
- config: config
2814
- } : ServiceWorkerUtils_await(_this3.registerServiceWorker(config.serviceWorkerScript, init));
2815
- });
2907
+ return ServiceWorkerUtils_await(ServiceWorkerUtils_continue(_switch(config.type, [[function () {
2908
+ return "Uninitialized";
2909
+ }, function () {
2910
+ {
2911
+ return ServiceWorkerUtils_await(_this2.unregisterOnHardReload(), function () {
2912
+ _interrupt = true;
2913
+ });
2914
+ }
2915
+ }], [function () {
2916
+ return "Initialized";
2917
+ }, function () {
2918
+ {
2919
+ return ServiceWorkerUtils_await(_this2.getActiveServiceWorker(config.serviceWorkerScope), function (serviceWorker) {
2920
+ if (serviceWorker !== undefined) {
2921
+ var _serviceWorker$config = {
2922
+ serviceWorker: serviceWorker,
2923
+ config: config
2924
+ };
2925
+ _exit = true;
2926
+ return _serviceWorker$config;
2927
+ }
2928
+ _interrupt = true;
2929
+ });
2930
+ }
2931
+ }], [void 0, function () {
2932
+ assertUnreachable(config);
2933
+ }, ServiceWorkerUtils_empty]]), function (_result) {
2934
+ return _exit ? _result : ServiceWorkerUtils_await(_this2.registerServiceWorker(config.serviceWorkerScript, initMessage, serviceWorkerScriptFieldName));
2935
+ }));
2816
2936
  } catch (e) {
2817
2937
  return Promise.reject(e);
2818
2938
  }
@@ -2826,20 +2946,25 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2826
2946
  */
2827
2947
  }, {
2828
2948
  key: "registerServiceWorker",
2829
- value: function registerServiceWorker(serviceWorkerScript, init) {
2949
+ value: function registerServiceWorker(serviceWorkerScript, initMessage, serviceWorkerScriptFieldName) {
2830
2950
  try {
2831
- var _this4 = this;
2951
+ var _this3 = this;
2952
+ if (!serviceWorkerScript.startsWith("/")) {
2953
+ throw new Error("The '".concat(serviceWorkerScriptFieldName, "' field must start with a '/' and reference a script at the root of your website."));
2954
+ }
2955
+ var forwardSlashCount = serviceWorkerScript.split("/").length - 1;
2956
+ if (forwardSlashCount > 1) {
2957
+ ConsoleUtils.warn("The '".concat(serviceWorkerScriptFieldName, "' field should be a root script (e.g. '/script.js'). The Bytescale SDK can only authorize requests originating from webpages that are at the same level as the script or below."));
2958
+ }
2832
2959
  return ServiceWorkerUtils_await(ServiceWorkerUtils_catch(function () {
2833
2960
  return ServiceWorkerUtils_await(navigator.serviceWorker.register(serviceWorkerScript), function (registration) {
2834
- return ServiceWorkerUtils_await(_this4.waitForActiveServiceWorker(registration, init), function (_ref) {
2835
- var serviceWorker = _ref.serviceWorker,
2836
- messageSent = _ref.messageSent;
2961
+ return ServiceWorkerUtils_await(_this3.waitForActiveServiceWorker(registration, initMessage), function (_this3$waitForActiveS) {
2837
2962
  return {
2838
- serviceWorker: serviceWorker,
2839
- messageSent: messageSent,
2963
+ serviceWorker: _this3$waitForActiveS,
2840
2964
  config: {
2841
2965
  serviceWorkerScript: serviceWorkerScript,
2842
- serviceWorkerScope: registration.scope
2966
+ serviceWorkerScope: registration.scope,
2967
+ type: "Initialized"
2843
2968
  }
2844
2969
  };
2845
2970
  });
@@ -2853,9 +2978,9 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2853
2978
  }
2854
2979
  }, {
2855
2980
  key: "waitForActiveServiceWorker",
2856
- value: function waitForActiveServiceWorker(registration, init) {
2981
+ value: function waitForActiveServiceWorker(registration, initMessage) {
2857
2982
  try {
2858
- var _exit = false;
2983
+ var _exit2 = false;
2859
2984
  // We must check the 'installing' state before the 'active' state (see comment below).
2860
2985
  // The state will be 'installing' when the service worker is installed for the first time, or if there have been
2861
2986
  // code changes to the service worker, else the state will be 'active'.
@@ -2867,31 +2992,29 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2867
2992
  var sw = e.target;
2868
2993
  if (sw.state === "activated") {
2869
2994
  installing.removeEventListener("statechange", stateChangeHandler);
2870
- resolve({
2871
- messageSent: true,
2872
- serviceWorker: sw
2873
- });
2995
+ resolve(sw);
2874
2996
  }
2875
2997
  };
2876
2998
  installing.addEventListener("statechange", stateChangeHandler);
2877
2999
  });
2878
- installing.postMessage(init);
3000
+ // We send the message during the INSTALL phase instead of ACTIVE phase to ensure that when replacing an
3001
+ // existing Service Worker instance, the new instance will be ready to run (with config received from this
3002
+ // 'postMessage' call) prior to it being activated, so that it can immediately start attaching auth headers to
3003
+ // intercepted 'fetch' requests.
3004
+ installing.postMessage(initMessage);
2879
3005
  return ServiceWorkerUtils_await(waitForActive, function (_await$waitForActive) {
2880
- _exit = true;
3006
+ _exit2 = true;
2881
3007
  return _await$waitForActive;
2882
3008
  });
2883
3009
  }
2884
- }, function (_result) {
2885
- if (_exit) return _result;
3010
+ }, function (_result2) {
3011
+ if (_exit2) return _result2;
2886
3012
  // We must check the 'installing' state before the 'active' state, because if we've just installed a new service
2887
3013
  // worker, then the new service worker will be in the 'installing' slot whereas the old service worker will be in
2888
3014
  // the 'active' slot. So, if we checked this first, we would always return the old service worker, and therefore the
2889
3015
  // new service worker would never be initialized.
2890
3016
  if (registration.active !== null) {
2891
- return {
2892
- serviceWorker: registration.active,
2893
- messageSent: false
2894
- };
3017
+ return registration.active;
2895
3018
  }
2896
3019
  // We expect the service worker to use 'skipWaiting', so we don't expect 'waiting' service workers.
2897
3020
  throw new Error("Service worker was neither 'installing' or 'active'.");
@@ -2904,29 +3027,47 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2904
3027
  key: "getActiveServiceWorker",
2905
3028
  value: function getActiveServiceWorker(serviceWorkerScope) {
2906
3029
  try {
2907
- // Existing active Service Workers will not receive 'fetch' events in sessions that start with a hard reload, so
2908
- // we must unregister them and register a new one. (See: https://github.com/mswjs/msw/issues/98#issuecomment-612118211)
3030
+ var _this4 = this;
3031
+ var _a, _b;
3032
+ return ServiceWorkerUtils_await(_this4.getActiveRegistrations(), function (_this4$getActiveRegis) {
3033
+ var registrations = _this4$getActiveRegis.filter(function (x) {
3034
+ return x.scope === serviceWorkerScope;
3035
+ });
3036
+ return (_b = (_a = registrations[0]) === null || _a === void 0 ? void 0 : _a.active) !== null && _b !== void 0 ? _b : undefined;
3037
+ });
3038
+ } catch (e) {
3039
+ return Promise.reject(e);
3040
+ }
3041
+ }
3042
+ /**
3043
+ * Existing active Service Workers will not receive 'fetch' events in sessions that start with a hard reload, so
3044
+ * we must unregister them and register a new one. (See: https://github.com/mswjs/msw/issues/98#issuecomment-612118211)
3045
+ */
3046
+ }, {
3047
+ key: "unregisterOnHardReload",
3048
+ value: function unregisterOnHardReload() {
3049
+ try {
3050
+ var _this5 = this;
2909
3051
  var isHardReload = navigator.serviceWorker.controller === null;
3052
+ if (!isHardReload) {
3053
+ return ServiceWorkerUtils_await();
3054
+ }
3055
+ return ServiceWorkerUtils_await(_this5.getActiveRegistrations(), function (registrations) {
3056
+ return ServiceWorkerUtils_continueIgnored(_forOf(registrations, function (registration) {
3057
+ return ServiceWorkerUtils_awaitIgnored(registration.unregister());
3058
+ }));
3059
+ });
3060
+ } catch (e) {
3061
+ return Promise.reject(e);
3062
+ }
3063
+ }
3064
+ }, {
3065
+ key: "getActiveRegistrations",
3066
+ value: function getActiveRegistrations() {
3067
+ try {
2910
3068
  return ServiceWorkerUtils_await(navigator.serviceWorker.getRegistrations(), function (registrations) {
2911
- var _exit2 = false;
2912
- return ServiceWorkerUtils_continue(_forOf(registrations, function (registration) {
2913
- return function () {
2914
- if (registration.active !== null && registration.scope === serviceWorkerScope) {
2915
- return function () {
2916
- if (isHardReload) {
2917
- return ServiceWorkerUtils_awaitIgnored(registration.unregister());
2918
- } else {
2919
- var _registration$active = registration.active;
2920
- _exit2 = true;
2921
- return _registration$active;
2922
- }
2923
- }();
2924
- }
2925
- }();
2926
- }, function () {
2927
- return _exit2;
2928
- }), function (_result4) {
2929
- return _exit2 ? _result4 : undefined;
3069
+ return registrations.filter(function (registration) {
3070
+ return registration.active !== null;
2930
3071
  });
2931
3072
  });
2932
3073
  } catch (e) {
@@ -3037,6 +3178,12 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3037
3178
  value: function isAuthSessionActive() {
3038
3179
  return AuthSessionState.getSession() !== undefined;
3039
3180
  }
3181
+ }, {
3182
+ key: "isAuthSessionReady",
3183
+ value: function isAuthSessionReady() {
3184
+ var _a;
3185
+ return ((_a = AuthSessionState.getSession()) === null || _a === void 0 ? void 0 : _a.accessToken) !== undefined;
3186
+ }
3040
3187
  }, {
3041
3188
  key: "beginAuthSession",
3042
3189
  value: function beginAuthSession(params) {
@@ -3054,7 +3201,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3054
3201
  isActive: true,
3055
3202
  authServiceWorker: params.serviceWorkerScript !== undefined && _this.serviceWorkerUtils.canUseServiceWorkers() ? {
3056
3203
  serviceWorkerScript: params.serviceWorkerScript,
3057
- serviceWorkerScope: undefined
3204
+ type: "Uninitialized"
3058
3205
  } : undefined
3059
3206
  };
3060
3207
  AuthSessionState.setSession(newSession);
@@ -3122,7 +3269,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3122
3269
  return AuthManagerBrowser_await(_this3.setAccessToken(session.params, jwt, setCookie), function (setTokenResult) {
3123
3270
  return AuthManagerBrowser_invoke(function () {
3124
3271
  if (session.authServiceWorker !== undefined) {
3125
- return AuthManagerBrowser_awaitIgnored(_this3.serviceWorkerUtils.sendMessage({
3272
+ return AuthManagerBrowser_await(_this3.serviceWorkerUtils.sendMessage({
3126
3273
  type: "SET_BYTESCALE_AUTH_CONFIG",
3127
3274
  config: [{
3128
3275
  headers: [{
@@ -3132,7 +3279,14 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3132
3279
  expires: Date.now() + setTokenResult.ttlSeconds * 1000,
3133
3280
  urlPrefix: "".concat(_this3.getCdnUrl(session.params), "/").concat(session.params.accountId, "/")
3134
3281
  }]
3135
- }, session.authServiceWorker, _this3.serviceWorkerScriptFieldName));
3282
+ }, session.authServiceWorker, _this3.serviceWorkerScriptFieldName), function () {
3283
+ // Allow time for the service worker to receive and process the message. Since this is asynchronous and not
3284
+ // synchronized, we need to wait for a sufficient amount of time to ensure the service worker is ready to
3285
+ // authenticate requests, so that after 'beginAuthSession' completes, users can start making requests.
3286
+ return AuthManagerBrowser_awaitIgnored(new Promise(function (resolve) {
3287
+ return setTimeout(resolve, 100);
3288
+ }));
3289
+ });
3136
3290
  }
3137
3291
  }, function () {
3138
3292
  var desiredTtl = setTokenResult.ttlSeconds - _this3.refreshBeforeExpirySeconds;
@@ -3141,6 +3295,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3141
3295
  ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(timeout, " seconds before refreshing."));
3142
3296
  }
3143
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!
3298
+ // Set this at the end, as it's also used to signal 'isAuthSessionReady', so must be set after configuring the Service Worker, etc.
3144
3299
  timeout = Math.min(timeout, _this3.maxJwtTtlSeconds);
3145
3300
  session.accessToken = setTokenResult.accessToken;
3146
3301
  });
@@ -2557,31 +2557,7 @@ function ServiceWorkerUtils_await(value, then, direct) {
2557
2557
  }
2558
2558
  return then ? value.then(then) : value;
2559
2559
  }
2560
- function ServiceWorkerUtils_catch(body, recover) {
2561
- try {
2562
- var result = body();
2563
- } catch (e) {
2564
- return recover(e);
2565
- }
2566
- if (result && result.then) {
2567
- return result.then(void 0, recover);
2568
- }
2569
- return result;
2570
- }
2571
- function ServiceWorkerUtils_invoke(body, then) {
2572
- var result = body();
2573
- if (result && result.then) {
2574
- return result.then(then);
2575
- }
2576
- return then(result);
2577
- }
2578
2560
  function ServiceWorkerUtils_empty() {}
2579
- function ServiceWorkerUtils_awaitIgnored(value, direct) {
2580
- if (!direct) {
2581
- return value && value.then ? value.then(ServiceWorkerUtils_empty) : Promise.resolve();
2582
- }
2583
- }
2584
- var _iteratorSymbol = /*#__PURE__*/typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator")) : "@@iterator";
2585
2561
  function ServiceWorkerUtils_settle(pact, state, value) {
2586
2562
  if (!pact.s) {
2587
2563
  if (value instanceof ServiceWorkerUtils_Pact) {
@@ -2643,6 +2619,137 @@ var ServiceWorkerUtils_Pact = /*#__PURE__*/function () {
2643
2619
  };
2644
2620
  return _Pact;
2645
2621
  }();
2622
+ function _switch(discriminant, cases) {
2623
+ var dispatchIndex = -1;
2624
+ var awaitBody;
2625
+ outer: {
2626
+ for (var i = 0; i < cases.length; i++) {
2627
+ var test = cases[i][0];
2628
+ if (test) {
2629
+ var testValue = test();
2630
+ if (testValue && testValue.then) {
2631
+ break outer;
2632
+ }
2633
+ if (testValue === discriminant) {
2634
+ dispatchIndex = i;
2635
+ break;
2636
+ }
2637
+ } else {
2638
+ // Found the default case, set it as the pending dispatch case
2639
+ dispatchIndex = i;
2640
+ }
2641
+ }
2642
+ if (dispatchIndex !== -1) {
2643
+ do {
2644
+ var body = cases[dispatchIndex][1];
2645
+ while (!body) {
2646
+ dispatchIndex++;
2647
+ body = cases[dispatchIndex][1];
2648
+ }
2649
+ var result = body();
2650
+ if (result && result.then) {
2651
+ awaitBody = true;
2652
+ break outer;
2653
+ }
2654
+ var fallthroughCheck = cases[dispatchIndex][2];
2655
+ dispatchIndex++;
2656
+ } while (fallthroughCheck && !fallthroughCheck());
2657
+ return result;
2658
+ }
2659
+ }
2660
+ var pact = new ServiceWorkerUtils_Pact();
2661
+ var reject = ServiceWorkerUtils_settle.bind(null, pact, 2);
2662
+ (awaitBody ? result.then(_resumeAfterBody) : testValue.then(_resumeAfterTest)).then(void 0, reject);
2663
+ return pact;
2664
+ function _resumeAfterTest(value) {
2665
+ for (;;) {
2666
+ if (value === discriminant) {
2667
+ dispatchIndex = i;
2668
+ break;
2669
+ }
2670
+ if (++i === cases.length) {
2671
+ if (dispatchIndex !== -1) {
2672
+ break;
2673
+ } else {
2674
+ ServiceWorkerUtils_settle(pact, 1, result);
2675
+ return;
2676
+ }
2677
+ }
2678
+ test = cases[i][0];
2679
+ if (test) {
2680
+ value = test();
2681
+ if (value && value.then) {
2682
+ value.then(_resumeAfterTest).then(void 0, reject);
2683
+ return;
2684
+ }
2685
+ } else {
2686
+ dispatchIndex = i;
2687
+ }
2688
+ }
2689
+ do {
2690
+ var body = cases[dispatchIndex][1];
2691
+ while (!body) {
2692
+ dispatchIndex++;
2693
+ body = cases[dispatchIndex][1];
2694
+ }
2695
+ var result = body();
2696
+ if (result && result.then) {
2697
+ result.then(_resumeAfterBody).then(void 0, reject);
2698
+ return;
2699
+ }
2700
+ var fallthroughCheck = cases[dispatchIndex][2];
2701
+ dispatchIndex++;
2702
+ } while (fallthroughCheck && !fallthroughCheck());
2703
+ ServiceWorkerUtils_settle(pact, 1, result);
2704
+ }
2705
+ function _resumeAfterBody(result) {
2706
+ for (;;) {
2707
+ var fallthroughCheck = cases[dispatchIndex][2];
2708
+ if (!fallthroughCheck || fallthroughCheck()) {
2709
+ break;
2710
+ }
2711
+ dispatchIndex++;
2712
+ var body = cases[dispatchIndex][1];
2713
+ while (!body) {
2714
+ dispatchIndex++;
2715
+ body = cases[dispatchIndex][1];
2716
+ }
2717
+ result = body();
2718
+ if (result && result.then) {
2719
+ result.then(_resumeAfterBody).then(void 0, reject);
2720
+ return;
2721
+ }
2722
+ }
2723
+ ServiceWorkerUtils_settle(pact, 1, result);
2724
+ }
2725
+ }
2726
+ function ServiceWorkerUtils_continue(value, then) {
2727
+ return value && value.then ? value.then(then) : then(value);
2728
+ }
2729
+ function ServiceWorkerUtils_catch(body, recover) {
2730
+ try {
2731
+ var result = body();
2732
+ } catch (e) {
2733
+ return recover(e);
2734
+ }
2735
+ if (result && result.then) {
2736
+ return result.then(void 0, recover);
2737
+ }
2738
+ return result;
2739
+ }
2740
+ function ServiceWorkerUtils_invoke(body, then) {
2741
+ var result = body();
2742
+ if (result && result.then) {
2743
+ return result.then(then);
2744
+ }
2745
+ return then(result);
2746
+ }
2747
+ function ServiceWorkerUtils_awaitIgnored(value, direct) {
2748
+ if (!direct) {
2749
+ return value && value.then ? value.then(ServiceWorkerUtils_empty) : Promise.resolve();
2750
+ }
2751
+ }
2752
+ var _iteratorSymbol = /*#__PURE__*/typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator")) : "@@iterator";
2646
2753
  function ServiceWorkerUtils_isSettledPact(thenable) {
2647
2754
  return thenable instanceof ServiceWorkerUtils_Pact && thenable.s & 1;
2648
2755
  }
@@ -2735,8 +2842,10 @@ function _forOf(target, body, check) {
2735
2842
  return body(values[i]);
2736
2843
  }, check);
2737
2844
  }
2738
- function ServiceWorkerUtils_continue(value, then) {
2739
- return value && value.then ? value.then(then) : then(value);
2845
+ function ServiceWorkerUtils_continueIgnored(value) {
2846
+ if (value && value.then) {
2847
+ return value.then(ServiceWorkerUtils_empty);
2848
+ }
2740
2849
  }
2741
2850
  function ServiceWorkerUtils_typeof(o) { "@babel/helpers - typeof"; return ServiceWorkerUtils_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; }, ServiceWorkerUtils_typeof(o); }
2742
2851
  function ServiceWorkerUtils_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -2745,6 +2854,7 @@ function ServiceWorkerUtils_createClass(Constructor, protoProps, staticProps) {
2745
2854
  function ServiceWorkerUtils_toPropertyKey(t) { var i = ServiceWorkerUtils_toPrimitive(t, "string"); return "symbol" == ServiceWorkerUtils_typeof(i) ? i : i + ""; }
2746
2855
  function ServiceWorkerUtils_toPrimitive(t, r) { if ("object" != ServiceWorkerUtils_typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != ServiceWorkerUtils_typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
2747
2856
 
2857
+
2748
2858
  var ServiceWorkerUtils = /*#__PURE__*/function () {
2749
2859
  function ServiceWorkerUtils() {
2750
2860
  ServiceWorkerUtils_classCallCheck(this, ServiceWorkerUtils);
@@ -2759,10 +2869,13 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2759
2869
  value: function sendMessage(message, config, serviceWorkerScriptFieldName) {
2760
2870
  try {
2761
2871
  var _this = this;
2762
- return ServiceWorkerUtils_await(config.serviceWorkerScope !== undefined ? _this.getActiveServiceWorkerElseRegister(config, message) : _this.registerServiceWorkerValidated(config.serviceWorkerScript, message, serviceWorkerScriptFieldName), function (result) {
2763
- if (!result.messageSent) {
2764
- result.serviceWorker.postMessage(message);
2765
- }
2872
+ return ServiceWorkerUtils_await(_this.ensureActiveServiceWorkerExists(message, config, serviceWorkerScriptFieldName), function (result) {
2873
+ // Message delivery is at least once. We must always assume this as there is only 1 service worker instance and
2874
+ // potentially multiple browser tabs with AuthManager sessions running. As such, even if we ensure exactly-once
2875
+ // delivery within a single tab, there will still be at-least-once delivery across all. As such, we simplfy our
2876
+ // code by using at-least-once delivery here too (hence the following 'postMessage' will already have been called
2877
+ // in the scenario where a new worker is newly installed).
2878
+ result.serviceWorker.postMessage(message);
2766
2879
  return result.config;
2767
2880
  });
2768
2881
  } catch (e) {
@@ -2770,34 +2883,41 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2770
2883
  }
2771
2884
  }
2772
2885
  }, {
2773
- key: "registerServiceWorkerValidated",
2774
- value: function registerServiceWorkerValidated(serviceWorkerScript, init, serviceWorkerScriptFieldName) {
2886
+ key: "ensureActiveServiceWorkerExists",
2887
+ value: function ensureActiveServiceWorkerExists(initMessage, config, serviceWorkerScriptFieldName) {
2775
2888
  try {
2889
+ var _exit = false,
2890
+ _interrupt = false;
2776
2891
  var _this2 = this;
2777
- if (!serviceWorkerScript.startsWith("/")) {
2778
- throw new Error("The '".concat(serviceWorkerScriptFieldName, "' field must start with a '/' and reference a script at the root of your website."));
2779
- }
2780
- var forwardSlashCount = serviceWorkerScript.split("/").length - 1;
2781
- if (forwardSlashCount > 1) {
2782
- ConsoleUtils.warn("The '".concat(serviceWorkerScriptFieldName, "' field should be a root script (e.g. '/script.js'). The Bytescale SDK can only authorize requests originating from webpages that are at the same level as the script or below."));
2783
- }
2784
- return ServiceWorkerUtils_await(_this2.registerServiceWorker(serviceWorkerScript, init));
2785
- } catch (e) {
2786
- return Promise.reject(e);
2787
- }
2788
- }
2789
- }, {
2790
- key: "getActiveServiceWorkerElseRegister",
2791
- value: function getActiveServiceWorkerElseRegister(config, init) {
2792
- try {
2793
- var _this3 = this;
2794
- return ServiceWorkerUtils_await(_this3.getActiveServiceWorker(config.serviceWorkerScope), function (serviceWorker) {
2795
- return serviceWorker !== undefined ? {
2796
- serviceWorker: serviceWorker,
2797
- messageSent: false,
2798
- config: config
2799
- } : ServiceWorkerUtils_await(_this3.registerServiceWorker(config.serviceWorkerScript, init));
2800
- });
2892
+ return ServiceWorkerUtils_await(ServiceWorkerUtils_continue(_switch(config.type, [[function () {
2893
+ return "Uninitialized";
2894
+ }, function () {
2895
+ {
2896
+ return ServiceWorkerUtils_await(_this2.unregisterOnHardReload(), function () {
2897
+ _interrupt = true;
2898
+ });
2899
+ }
2900
+ }], [function () {
2901
+ return "Initialized";
2902
+ }, function () {
2903
+ {
2904
+ return ServiceWorkerUtils_await(_this2.getActiveServiceWorker(config.serviceWorkerScope), function (serviceWorker) {
2905
+ if (serviceWorker !== undefined) {
2906
+ var _serviceWorker$config = {
2907
+ serviceWorker: serviceWorker,
2908
+ config: config
2909
+ };
2910
+ _exit = true;
2911
+ return _serviceWorker$config;
2912
+ }
2913
+ _interrupt = true;
2914
+ });
2915
+ }
2916
+ }], [void 0, function () {
2917
+ assertUnreachable(config);
2918
+ }, ServiceWorkerUtils_empty]]), function (_result) {
2919
+ return _exit ? _result : ServiceWorkerUtils_await(_this2.registerServiceWorker(config.serviceWorkerScript, initMessage, serviceWorkerScriptFieldName));
2920
+ }));
2801
2921
  } catch (e) {
2802
2922
  return Promise.reject(e);
2803
2923
  }
@@ -2811,20 +2931,25 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2811
2931
  */
2812
2932
  }, {
2813
2933
  key: "registerServiceWorker",
2814
- value: function registerServiceWorker(serviceWorkerScript, init) {
2934
+ value: function registerServiceWorker(serviceWorkerScript, initMessage, serviceWorkerScriptFieldName) {
2815
2935
  try {
2816
- var _this4 = this;
2936
+ var _this3 = this;
2937
+ if (!serviceWorkerScript.startsWith("/")) {
2938
+ throw new Error("The '".concat(serviceWorkerScriptFieldName, "' field must start with a '/' and reference a script at the root of your website."));
2939
+ }
2940
+ var forwardSlashCount = serviceWorkerScript.split("/").length - 1;
2941
+ if (forwardSlashCount > 1) {
2942
+ ConsoleUtils.warn("The '".concat(serviceWorkerScriptFieldName, "' field should be a root script (e.g. '/script.js'). The Bytescale SDK can only authorize requests originating from webpages that are at the same level as the script or below."));
2943
+ }
2817
2944
  return ServiceWorkerUtils_await(ServiceWorkerUtils_catch(function () {
2818
2945
  return ServiceWorkerUtils_await(navigator.serviceWorker.register(serviceWorkerScript), function (registration) {
2819
- return ServiceWorkerUtils_await(_this4.waitForActiveServiceWorker(registration, init), function (_ref) {
2820
- var serviceWorker = _ref.serviceWorker,
2821
- messageSent = _ref.messageSent;
2946
+ return ServiceWorkerUtils_await(_this3.waitForActiveServiceWorker(registration, initMessage), function (_this3$waitForActiveS) {
2822
2947
  return {
2823
- serviceWorker: serviceWorker,
2824
- messageSent: messageSent,
2948
+ serviceWorker: _this3$waitForActiveS,
2825
2949
  config: {
2826
2950
  serviceWorkerScript: serviceWorkerScript,
2827
- serviceWorkerScope: registration.scope
2951
+ serviceWorkerScope: registration.scope,
2952
+ type: "Initialized"
2828
2953
  }
2829
2954
  };
2830
2955
  });
@@ -2838,9 +2963,9 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2838
2963
  }
2839
2964
  }, {
2840
2965
  key: "waitForActiveServiceWorker",
2841
- value: function waitForActiveServiceWorker(registration, init) {
2966
+ value: function waitForActiveServiceWorker(registration, initMessage) {
2842
2967
  try {
2843
- var _exit = false;
2968
+ var _exit2 = false;
2844
2969
  // We must check the 'installing' state before the 'active' state (see comment below).
2845
2970
  // The state will be 'installing' when the service worker is installed for the first time, or if there have been
2846
2971
  // code changes to the service worker, else the state will be 'active'.
@@ -2852,31 +2977,29 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2852
2977
  var sw = e.target;
2853
2978
  if (sw.state === "activated") {
2854
2979
  installing.removeEventListener("statechange", stateChangeHandler);
2855
- resolve({
2856
- messageSent: true,
2857
- serviceWorker: sw
2858
- });
2980
+ resolve(sw);
2859
2981
  }
2860
2982
  };
2861
2983
  installing.addEventListener("statechange", stateChangeHandler);
2862
2984
  });
2863
- installing.postMessage(init);
2985
+ // We send the message during the INSTALL phase instead of ACTIVE phase to ensure that when replacing an
2986
+ // existing Service Worker instance, the new instance will be ready to run (with config received from this
2987
+ // 'postMessage' call) prior to it being activated, so that it can immediately start attaching auth headers to
2988
+ // intercepted 'fetch' requests.
2989
+ installing.postMessage(initMessage);
2864
2990
  return ServiceWorkerUtils_await(waitForActive, function (_await$waitForActive) {
2865
- _exit = true;
2991
+ _exit2 = true;
2866
2992
  return _await$waitForActive;
2867
2993
  });
2868
2994
  }
2869
- }, function (_result) {
2870
- if (_exit) return _result;
2995
+ }, function (_result2) {
2996
+ if (_exit2) return _result2;
2871
2997
  // We must check the 'installing' state before the 'active' state, because if we've just installed a new service
2872
2998
  // worker, then the new service worker will be in the 'installing' slot whereas the old service worker will be in
2873
2999
  // the 'active' slot. So, if we checked this first, we would always return the old service worker, and therefore the
2874
3000
  // new service worker would never be initialized.
2875
3001
  if (registration.active !== null) {
2876
- return {
2877
- serviceWorker: registration.active,
2878
- messageSent: false
2879
- };
3002
+ return registration.active;
2880
3003
  }
2881
3004
  // We expect the service worker to use 'skipWaiting', so we don't expect 'waiting' service workers.
2882
3005
  throw new Error("Service worker was neither 'installing' or 'active'.");
@@ -2889,29 +3012,47 @@ var ServiceWorkerUtils = /*#__PURE__*/function () {
2889
3012
  key: "getActiveServiceWorker",
2890
3013
  value: function getActiveServiceWorker(serviceWorkerScope) {
2891
3014
  try {
2892
- // Existing active Service Workers will not receive 'fetch' events in sessions that start with a hard reload, so
2893
- // we must unregister them and register a new one. (See: https://github.com/mswjs/msw/issues/98#issuecomment-612118211)
3015
+ var _this4 = this;
3016
+ var _a, _b;
3017
+ return ServiceWorkerUtils_await(_this4.getActiveRegistrations(), function (_this4$getActiveRegis) {
3018
+ var registrations = _this4$getActiveRegis.filter(function (x) {
3019
+ return x.scope === serviceWorkerScope;
3020
+ });
3021
+ return (_b = (_a = registrations[0]) === null || _a === void 0 ? void 0 : _a.active) !== null && _b !== void 0 ? _b : undefined;
3022
+ });
3023
+ } catch (e) {
3024
+ return Promise.reject(e);
3025
+ }
3026
+ }
3027
+ /**
3028
+ * Existing active Service Workers will not receive 'fetch' events in sessions that start with a hard reload, so
3029
+ * we must unregister them and register a new one. (See: https://github.com/mswjs/msw/issues/98#issuecomment-612118211)
3030
+ */
3031
+ }, {
3032
+ key: "unregisterOnHardReload",
3033
+ value: function unregisterOnHardReload() {
3034
+ try {
3035
+ var _this5 = this;
2894
3036
  var isHardReload = navigator.serviceWorker.controller === null;
3037
+ if (!isHardReload) {
3038
+ return ServiceWorkerUtils_await();
3039
+ }
3040
+ return ServiceWorkerUtils_await(_this5.getActiveRegistrations(), function (registrations) {
3041
+ return ServiceWorkerUtils_continueIgnored(_forOf(registrations, function (registration) {
3042
+ return ServiceWorkerUtils_awaitIgnored(registration.unregister());
3043
+ }));
3044
+ });
3045
+ } catch (e) {
3046
+ return Promise.reject(e);
3047
+ }
3048
+ }
3049
+ }, {
3050
+ key: "getActiveRegistrations",
3051
+ value: function getActiveRegistrations() {
3052
+ try {
2895
3053
  return ServiceWorkerUtils_await(navigator.serviceWorker.getRegistrations(), function (registrations) {
2896
- var _exit2 = false;
2897
- return ServiceWorkerUtils_continue(_forOf(registrations, function (registration) {
2898
- return function () {
2899
- if (registration.active !== null && registration.scope === serviceWorkerScope) {
2900
- return function () {
2901
- if (isHardReload) {
2902
- return ServiceWorkerUtils_awaitIgnored(registration.unregister());
2903
- } else {
2904
- var _registration$active = registration.active;
2905
- _exit2 = true;
2906
- return _registration$active;
2907
- }
2908
- }();
2909
- }
2910
- }();
2911
- }, function () {
2912
- return _exit2;
2913
- }), function (_result4) {
2914
- return _exit2 ? _result4 : undefined;
3054
+ return registrations.filter(function (registration) {
3055
+ return registration.active !== null;
2915
3056
  });
2916
3057
  });
2917
3058
  } catch (e) {
@@ -3022,6 +3163,12 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3022
3163
  value: function isAuthSessionActive() {
3023
3164
  return AuthSessionState.getSession() !== undefined;
3024
3165
  }
3166
+ }, {
3167
+ key: "isAuthSessionReady",
3168
+ value: function isAuthSessionReady() {
3169
+ var _a;
3170
+ return ((_a = AuthSessionState.getSession()) === null || _a === void 0 ? void 0 : _a.accessToken) !== undefined;
3171
+ }
3025
3172
  }, {
3026
3173
  key: "beginAuthSession",
3027
3174
  value: function beginAuthSession(params) {
@@ -3039,7 +3186,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3039
3186
  isActive: true,
3040
3187
  authServiceWorker: params.serviceWorkerScript !== undefined && _this.serviceWorkerUtils.canUseServiceWorkers() ? {
3041
3188
  serviceWorkerScript: params.serviceWorkerScript,
3042
- serviceWorkerScope: undefined
3189
+ type: "Uninitialized"
3043
3190
  } : undefined
3044
3191
  };
3045
3192
  AuthSessionState.setSession(newSession);
@@ -3107,7 +3254,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3107
3254
  return AuthManagerBrowser_await(_this3.setAccessToken(session.params, jwt, setCookie), function (setTokenResult) {
3108
3255
  return AuthManagerBrowser_invoke(function () {
3109
3256
  if (session.authServiceWorker !== undefined) {
3110
- return AuthManagerBrowser_awaitIgnored(_this3.serviceWorkerUtils.sendMessage({
3257
+ return AuthManagerBrowser_await(_this3.serviceWorkerUtils.sendMessage({
3111
3258
  type: "SET_BYTESCALE_AUTH_CONFIG",
3112
3259
  config: [{
3113
3260
  headers: [{
@@ -3117,7 +3264,14 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3117
3264
  expires: Date.now() + setTokenResult.ttlSeconds * 1000,
3118
3265
  urlPrefix: "".concat(_this3.getCdnUrl(session.params), "/").concat(session.params.accountId, "/")
3119
3266
  }]
3120
- }, session.authServiceWorker, _this3.serviceWorkerScriptFieldName));
3267
+ }, session.authServiceWorker, _this3.serviceWorkerScriptFieldName), function () {
3268
+ // Allow time for the service worker to receive and process the message. Since this is asynchronous and not
3269
+ // synchronized, we need to wait for a sufficient amount of time to ensure the service worker is ready to
3270
+ // authenticate requests, so that after 'beginAuthSession' completes, users can start making requests.
3271
+ return AuthManagerBrowser_awaitIgnored(new Promise(function (resolve) {
3272
+ return setTimeout(resolve, 100);
3273
+ }));
3274
+ });
3121
3275
  }
3122
3276
  }, function () {
3123
3277
  var desiredTtl = setTokenResult.ttlSeconds - _this3.refreshBeforeExpirySeconds;
@@ -3126,6 +3280,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3126
3280
  ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(timeout, " seconds before refreshing."));
3127
3281
  }
3128
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!
3283
+ // Set this at the end, as it's also used to signal 'isAuthSessionReady', so must be set after configuring the Service Worker, etc.
3129
3284
  timeout = Math.min(timeout, _this3.maxJwtTtlSeconds);
3130
3285
  session.accessToken = setTokenResult.accessToken;
3131
3286
  });
@@ -3092,6 +3092,11 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3092
3092
  value: function isAuthSessionActive() {
3093
3093
  return false;
3094
3094
  }
3095
+ }, {
3096
+ key: "isAuthSessionReady",
3097
+ value: function isAuthSessionReady() {
3098
+ return false;
3099
+ }
3095
3100
  }]);
3096
3101
  }();
3097
3102
  /**
@@ -3087,6 +3087,11 @@ var AuthManagerImpl = /*#__PURE__*/function () {
3087
3087
  value: function isAuthSessionActive() {
3088
3088
  return false;
3089
3089
  }
3090
+ }, {
3091
+ key: "isAuthSessionReady",
3092
+ value: function isAuthSessionReady() {
3093
+ return false;
3094
+ }
3090
3095
  }]);
3091
3096
  }();
3092
3097
  /**
@@ -2,8 +2,7 @@ import { ServiceWorkerConfig, ServiceWorkerConfigInitialized } from "./model/Ser
2
2
  export declare class ServiceWorkerUtils<TMessage> {
3
3
  canUseServiceWorkers(): boolean;
4
4
  sendMessage(message: TMessage, config: ServiceWorkerConfig, serviceWorkerScriptFieldName: string): Promise<ServiceWorkerConfigInitialized>;
5
- private registerServiceWorkerValidated;
6
- private getActiveServiceWorkerElseRegister;
5
+ private ensureActiveServiceWorkerExists;
7
6
  /**
8
7
  * Idempotent.
9
8
  *
@@ -14,4 +13,10 @@ export declare class ServiceWorkerUtils<TMessage> {
14
13
  private registerServiceWorker;
15
14
  private waitForActiveServiceWorker;
16
15
  private getActiveServiceWorker;
16
+ /**
17
+ * Existing active Service Workers will not receive 'fetch' events in sessions that start with a hard reload, so
18
+ * we must unregister them and register a new one. (See: https://github.com/mswjs/msw/issues/98#issuecomment-612118211)
19
+ */
20
+ private unregisterOnHardReload;
21
+ private getActiveRegistrations;
17
22
  }
@@ -104,4 +104,8 @@ export interface AuthManagerInterface {
104
104
  * Checks if an authenticated Bytescale API and Bytescale CDN session is active.
105
105
  */
106
106
  isAuthSessionActive: () => boolean;
107
+ /**
108
+ * Checks if an authenticated Bytescale API and Bytescale CDN session is active and ready to authenticate HTTP requests.
109
+ */
110
+ isAuthSessionReady: () => boolean;
107
111
  }
@@ -1,9 +1,10 @@
1
1
  export type ServiceWorkerConfig = ServiceWorkerConfigUninitialized | ServiceWorkerConfigInitialized;
2
2
  export interface ServiceWorkerConfigUninitialized {
3
- serviceWorkerScope: undefined;
4
3
  serviceWorkerScript: string;
4
+ type: "Uninitialized";
5
5
  }
6
6
  export interface ServiceWorkerConfigInitialized {
7
- serviceWorkerScope: string;
7
+ serviceWorkerScope: string | undefined;
8
8
  serviceWorkerScript: string;
9
+ type: "Initialized";
9
10
  }
@@ -1,6 +1,5 @@
1
1
  import { ServiceWorkerConfigInitialized } from "./ServiceWorkerConfig";
2
2
  export interface ServiceWorkerInitStatus {
3
3
  config: ServiceWorkerConfigInitialized;
4
- messageSent: boolean;
5
4
  serviceWorker: ServiceWorker;
6
5
  }
@@ -14,6 +14,7 @@ declare class AuthManagerImpl implements AuthManagerInterface {
14
14
  private readonly refreshBeforeExpirySeconds;
15
15
  constructor(serviceWorkerUtils: ServiceWorkerUtils<AuthSwSetConfigDto>);
16
16
  isAuthSessionActive(): boolean;
17
+ isAuthSessionReady(): boolean;
17
18
  beginAuthSession(params: BeginAuthSessionParams): Promise<void>;
18
19
  endAuthSession(): Promise<void>;
19
20
  private refreshAccessToken;
@@ -3,6 +3,7 @@ declare class AuthManagerImpl implements AuthManagerInterface {
3
3
  beginAuthSession(_params: BeginAuthSessionParams): Promise<void>;
4
4
  endAuthSession(): Promise<void>;
5
5
  isAuthSessionActive(): boolean;
6
+ isAuthSessionReady(): boolean;
6
7
  }
7
8
  /**
8
9
  * Alternative way of implementing a static class (i.e. all methods static). We do this so we can use a interface on the class (interfaces can't define static methods).
@@ -2626,6 +2626,11 @@ var AuthManagerImpl = /*#__PURE__*/function () {
2626
2626
  value: function isAuthSessionActive() {
2627
2627
  return false;
2628
2628
  }
2629
+ }, {
2630
+ key: "isAuthSessionReady",
2631
+ value: function isAuthSessionReady() {
2632
+ return false;
2633
+ }
2629
2634
  }]);
2630
2635
  }();
2631
2636
  /**
@@ -2611,6 +2611,11 @@ var AuthManagerImpl = /*#__PURE__*/function () {
2611
2611
  value: function isAuthSessionActive() {
2612
2612
  return false;
2613
2613
  }
2614
+ }, {
2615
+ key: "isAuthSessionReady",
2616
+ value: function isAuthSessionReady() {
2617
+ return false;
2618
+ }
2614
2619
  }]);
2615
2620
  }();
2616
2621
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytescale/sdk",
3
- "version": "3.40.0",
3
+ "version": "3.42.0",
4
4
  "description": "Bytescale JavaScript SDK",
5
5
  "author": "Bytescale <hello@bytescale.com> (https://www.bytescale.com)",
6
6
  "license": "MIT",