@firebase/app-check 0.5.15 → 0.5.16-canary.457fc2eeb

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.
@@ -28,11 +28,19 @@ const DEBUG_STATE = {
28
28
  initialized: false,
29
29
  enabled: false
30
30
  };
31
- function getState(app) {
32
- return APP_CHECK_STATES.get(app) || DEFAULT_STATE;
31
+ /**
32
+ * Gets a reference to the state object.
33
+ */
34
+ function getStateReference(app) {
35
+ return APP_CHECK_STATES.get(app) || Object.assign({}, DEFAULT_STATE);
33
36
  }
34
- function setState(app, state) {
37
+ /**
38
+ * Set once on initialization. The map should hold the same reference to the
39
+ * same object until this entry is deleted.
40
+ */
41
+ function setInitialState(app, state) {
35
42
  APP_CHECK_STATES.set(app, state);
43
+ return APP_CHECK_STATES.get(app);
36
44
  }
37
45
  function getDebugState() {
38
46
  return DEBUG_STATE;
@@ -247,7 +255,7 @@ function getRecaptcha(isEnterprise = false) {
247
255
  return self.grecaptcha;
248
256
  }
249
257
  function ensureActivated(app) {
250
- if (!getState(app).activated) {
258
+ if (!getStateReference(app).activated) {
251
259
  throw ERROR_FACTORY.create("use-before-activation" /* USE_BEFORE_ACTIVATION */, {
252
260
  appName: app.name
253
261
  });
@@ -682,7 +690,7 @@ function formatDummyToken(tokenErrorData) {
682
690
  async function getToken$2(appCheck, forceRefresh = false) {
683
691
  const app = appCheck.app;
684
692
  ensureActivated(app);
685
- const state = getState(app);
693
+ const state = getStateReference(app);
686
694
  /**
687
695
  * First check if there is a token in memory from a previous `getToken()` call.
688
696
  */
@@ -693,7 +701,7 @@ async function getToken$2(appCheck, forceRefresh = false) {
693
701
  * memory and unset the local variable `token`.
694
702
  */
695
703
  if (token && !isValid(token)) {
696
- setState(app, Object.assign(Object.assign({}, state), { token: undefined }));
704
+ state.token = undefined;
697
705
  token = undefined;
698
706
  }
699
707
  /**
@@ -740,7 +748,7 @@ async function getToken$2(appCheck, forceRefresh = false) {
740
748
  // Write debug token to indexedDB.
741
749
  await writeTokenToStorage(app, tokenFromDebugExchange);
742
750
  // Write debug token to state.
743
- setState(app, Object.assign(Object.assign({}, state), { token: tokenFromDebugExchange }));
751
+ state.token = tokenFromDebugExchange;
744
752
  return { token: tokenFromDebugExchange.token };
745
753
  }
746
754
  /**
@@ -760,7 +768,7 @@ async function getToken$2(appCheck, forceRefresh = false) {
760
768
  });
761
769
  shouldCallListeners = true;
762
770
  }
763
- token = await state.exchangeTokenPromise;
771
+ token = await getStateReference(app).exchangeTokenPromise;
764
772
  }
765
773
  catch (e) {
766
774
  if (e.code === `appCheck/${"throttled" /* THROTTLED */}`) {
@@ -806,7 +814,7 @@ async function getToken$2(appCheck, forceRefresh = false) {
806
814
  };
807
815
  // write the new token to the memory state as well as the persistent storage.
808
816
  // Only do it if we got a valid new token
809
- setState(app, Object.assign(Object.assign({}, state), { token }));
817
+ state.token = token;
810
818
  await writeTokenToStorage(app, token);
811
819
  }
812
820
  if (shouldCallListeners) {
@@ -816,13 +824,13 @@ async function getToken$2(appCheck, forceRefresh = false) {
816
824
  }
817
825
  function addTokenListener(appCheck, type, listener, onError) {
818
826
  const { app } = appCheck;
819
- const state = getState(app);
827
+ const state = getStateReference(app);
820
828
  const tokenObserver = {
821
829
  next: listener,
822
830
  error: onError,
823
831
  type
824
832
  };
825
- setState(app, Object.assign(Object.assign({}, state), { tokenObservers: [...state.tokenObservers, tokenObserver] }));
833
+ state.tokenObservers = [...state.tokenObservers, tokenObserver];
826
834
  // Invoke the listener async immediately if there is a valid token
827
835
  // in memory.
828
836
  if (state.token && isValid(state.token)) {
@@ -849,27 +857,27 @@ function addTokenListener(appCheck, type, listener, onError) {
849
857
  void state.cachedTokenPromise.then(() => initTokenRefresher(appCheck));
850
858
  }
851
859
  function removeTokenListener(app, listener) {
852
- const state = getState(app);
860
+ const state = getStateReference(app);
853
861
  const newObservers = state.tokenObservers.filter(tokenObserver => tokenObserver.next !== listener);
854
862
  if (newObservers.length === 0 &&
855
863
  state.tokenRefresher &&
856
864
  state.tokenRefresher.isRunning()) {
857
865
  state.tokenRefresher.stop();
858
866
  }
859
- setState(app, Object.assign(Object.assign({}, state), { tokenObservers: newObservers }));
867
+ state.tokenObservers = newObservers;
860
868
  }
861
869
  /**
862
870
  * Logic to create and start refresher as needed.
863
871
  */
864
872
  function initTokenRefresher(appCheck) {
865
873
  const { app } = appCheck;
866
- const state = getState(app);
874
+ const state = getStateReference(app);
867
875
  // Create the refresher but don't start it if `isTokenAutoRefreshEnabled`
868
876
  // is not true.
869
877
  let refresher = state.tokenRefresher;
870
878
  if (!refresher) {
871
879
  refresher = createTokenRefresher(appCheck);
872
- setState(app, Object.assign(Object.assign({}, state), { tokenRefresher: refresher }));
880
+ state.tokenRefresher = refresher;
873
881
  }
874
882
  if (!refresher.isRunning() && state.isTokenAutoRefreshEnabled) {
875
883
  refresher.start();
@@ -881,7 +889,7 @@ function createTokenRefresher(appCheck) {
881
889
  // Keep in mind when this fails for any reason other than the ones
882
890
  // for which we should retry, it will effectively stop the proactive refresh.
883
891
  async () => {
884
- const state = getState(app);
892
+ const state = getStateReference(app);
885
893
  // If there is no token, we will try to load it from storage and use it
886
894
  // If there is a token, we force refresh it because we know it's going to expire soon
887
895
  let result;
@@ -912,7 +920,7 @@ function createTokenRefresher(appCheck) {
912
920
  }, () => {
913
921
  return true;
914
922
  }, () => {
915
- const state = getState(app);
923
+ const state = getStateReference(app);
916
924
  if (state.token) {
917
925
  // issuedAtTime + (50% * total TTL) + 5 minutes
918
926
  let nextRefreshTimeMillis = state.token.issuedAtTimeMillis +
@@ -930,7 +938,7 @@ function createTokenRefresher(appCheck) {
930
938
  }, TOKEN_REFRESH_TIME.RETRIAL_MIN_WAIT, TOKEN_REFRESH_TIME.RETRIAL_MAX_WAIT);
931
939
  }
932
940
  function notifyTokenListeners(app, token) {
933
- const observers = getState(app).tokenObservers;
941
+ const observers = getStateReference(app).tokenObservers;
934
942
  for (const observer of observers) {
935
943
  try {
936
944
  if (observer.type === "EXTERNAL" /* EXTERNAL */ && token.error != null) {
@@ -986,7 +994,7 @@ class AppCheckService {
986
994
  this.heartbeatServiceProvider = heartbeatServiceProvider;
987
995
  }
988
996
  _delete() {
989
- const { tokenObservers } = getState(this.app);
997
+ const { tokenObservers } = getStateReference(this.app);
990
998
  for (const tokenObserver of tokenObservers) {
991
999
  removeTokenListener(this.app, tokenObserver.next);
992
1000
  }
@@ -1005,7 +1013,7 @@ function internalFactory(appCheck) {
1005
1013
  }
1006
1014
 
1007
1015
  const name = "@firebase/app-check";
1008
- const version = "0.5.15";
1016
+ const version = "0.5.16-canary.457fc2eeb";
1009
1017
 
1010
1018
  /**
1011
1019
  * @license
@@ -1026,9 +1034,9 @@ const version = "0.5.15";
1026
1034
  const RECAPTCHA_URL = 'https://www.google.com/recaptcha/api.js';
1027
1035
  const RECAPTCHA_ENTERPRISE_URL = 'https://www.google.com/recaptcha/enterprise.js';
1028
1036
  function initializeV3(app, siteKey) {
1029
- const state = getState(app);
1030
1037
  const initialized = new Deferred();
1031
- setState(app, Object.assign(Object.assign({}, state), { reCAPTCHAState: { initialized } }));
1038
+ const state = getStateReference(app);
1039
+ state.reCAPTCHAState = { initialized };
1032
1040
  const divId = makeDiv(app);
1033
1041
  const grecaptcha = getRecaptcha(false);
1034
1042
  if (!grecaptcha) {
@@ -1047,9 +1055,9 @@ function initializeV3(app, siteKey) {
1047
1055
  return initialized.promise;
1048
1056
  }
1049
1057
  function initializeEnterprise(app, siteKey) {
1050
- const state = getState(app);
1051
1058
  const initialized = new Deferred();
1052
- setState(app, Object.assign(Object.assign({}, state), { reCAPTCHAState: { initialized } }));
1059
+ const state = getStateReference(app);
1060
+ state.reCAPTCHAState = { initialized };
1053
1061
  const divId = makeDiv(app);
1054
1062
  const grecaptcha = getRecaptcha(true);
1055
1063
  if (!grecaptcha) {
@@ -1093,11 +1101,11 @@ function makeDiv(app) {
1093
1101
  async function getToken$1(app) {
1094
1102
  ensureActivated(app);
1095
1103
  // ensureActivated() guarantees that reCAPTCHAState is set
1096
- const reCAPTCHAState = getState(app).reCAPTCHAState;
1104
+ const reCAPTCHAState = getStateReference(app).reCAPTCHAState;
1097
1105
  const recaptcha = await reCAPTCHAState.initialized.promise;
1098
1106
  return new Promise((resolve, _reject) => {
1099
1107
  // Updated after initialization is complete.
1100
- const reCAPTCHAState = getState(app).reCAPTCHAState;
1108
+ const reCAPTCHAState = getStateReference(app).reCAPTCHAState;
1101
1109
  recaptcha.ready(() => {
1102
1110
  resolve(
1103
1111
  // widgetId is guaranteed to be available if reCAPTCHAState.initialized.promise resolved.
@@ -1117,9 +1125,9 @@ function renderInvisibleWidget(app, siteKey, grecaptcha, container) {
1117
1125
  sitekey: siteKey,
1118
1126
  size: 'invisible'
1119
1127
  });
1120
- const state = getState(app);
1121
- setState(app, Object.assign(Object.assign({}, state), { reCAPTCHAState: Object.assign(Object.assign({}, state.reCAPTCHAState), { // state.reCAPTCHAState is set in the initialize()
1122
- widgetId }) }));
1128
+ const state = getStateReference(app);
1129
+ state.reCAPTCHAState = Object.assign(Object.assign({}, state.reCAPTCHAState), { // state.reCAPTCHAState is set in the initialize()
1130
+ widgetId });
1123
1131
  }
1124
1132
  function loadReCAPTCHAV3Script(onload) {
1125
1133
  const script = document.createElement('script');
@@ -1451,7 +1459,7 @@ function initializeAppCheck(app = getApp(), options) {
1451
1459
  // If isTokenAutoRefreshEnabled is false, do not send any requests to the
1452
1460
  // exchange endpoint without an explicit call from the user either directly
1453
1461
  // or through another Firebase library (storage, functions, etc.)
1454
- if (getState(app).isTokenAutoRefreshEnabled) {
1462
+ if (getStateReference(app).isTokenAutoRefreshEnabled) {
1455
1463
  // Adding a listener will start the refresher and fetch a token if needed.
1456
1464
  // This gets a token ready and prevents a delay when an internal library
1457
1465
  // requests the token.
@@ -1472,12 +1480,14 @@ function initializeAppCheck(app = getApp(), options) {
1472
1480
  * false and can be set in the app config.
1473
1481
  */
1474
1482
  function _activate(app, provider, isTokenAutoRefreshEnabled) {
1475
- const state = getState(app);
1476
- const newState = Object.assign(Object.assign({}, state), { activated: true });
1477
- newState.provider = provider; // Read cached token from storage if it exists and store it in memory.
1478
- newState.cachedTokenPromise = readTokenFromStorage(app).then(cachedToken => {
1483
+ // Create an entry in the APP_CHECK_STATES map. Further changes should
1484
+ // directly mutate this object.
1485
+ const state = setInitialState(app, Object.assign({}, DEFAULT_STATE));
1486
+ state.activated = true;
1487
+ state.provider = provider; // Read cached token from storage if it exists and store it in memory.
1488
+ state.cachedTokenPromise = readTokenFromStorage(app).then(cachedToken => {
1479
1489
  if (cachedToken && isValid(cachedToken)) {
1480
- setState(app, Object.assign(Object.assign({}, getState(app)), { token: cachedToken }));
1490
+ state.token = cachedToken;
1481
1491
  // notify all listeners with the cached token
1482
1492
  notifyTokenListeners(app, { token: cachedToken.token });
1483
1493
  }
@@ -1486,12 +1496,11 @@ function _activate(app, provider, isTokenAutoRefreshEnabled) {
1486
1496
  // Use value of global `automaticDataCollectionEnabled` (which
1487
1497
  // itself defaults to false if not specified in config) if
1488
1498
  // `isTokenAutoRefreshEnabled` param was not provided by user.
1489
- newState.isTokenAutoRefreshEnabled =
1499
+ state.isTokenAutoRefreshEnabled =
1490
1500
  isTokenAutoRefreshEnabled === undefined
1491
1501
  ? app.automaticDataCollectionEnabled
1492
1502
  : isTokenAutoRefreshEnabled;
1493
- setState(app, newState);
1494
- newState.provider.initialize(app);
1503
+ state.provider.initialize(app);
1495
1504
  }
1496
1505
  /**
1497
1506
  * Set whether App Check will automatically refresh tokens as needed.
@@ -1504,7 +1513,7 @@ function _activate(app, provider, isTokenAutoRefreshEnabled) {
1504
1513
  */
1505
1514
  function setTokenAutoRefreshEnabled(appCheckInstance, isTokenAutoRefreshEnabled) {
1506
1515
  const app = appCheckInstance.app;
1507
- const state = getState(app);
1516
+ const state = getStateReference(app);
1508
1517
  // This will exist if any product libraries have called
1509
1518
  // `addTokenListener()`
1510
1519
  if (state.tokenRefresher) {
@@ -1515,7 +1524,7 @@ function setTokenAutoRefreshEnabled(appCheckInstance, isTokenAutoRefreshEnabled)
1515
1524
  state.tokenRefresher.stop();
1516
1525
  }
1517
1526
  }
1518
- setState(app, Object.assign(Object.assign({}, state), { isTokenAutoRefreshEnabled }));
1527
+ state.isTokenAutoRefreshEnabled = isTokenAutoRefreshEnabled;
1519
1528
  }
1520
1529
  /**
1521
1530
  * Get the current App Check token. Attaches to the most recent