@firebase/installations 0.5.16 → 0.6.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.
@@ -4,7 +4,7 @@ import { ErrorFactory, FirebaseError } from '@firebase/util';
4
4
  import { openDB } from 'idb';
5
5
 
6
6
  const name = "@firebase/installations";
7
- const version = "0.5.16";
7
+ const version = "0.6.0";
8
8
 
9
9
  /**
10
10
  * @license
@@ -47,18 +47,18 @@ const SERVICE_NAME = 'Installations';
47
47
  * limitations under the License.
48
48
  */
49
49
  const ERROR_DESCRIPTION_MAP = {
50
- ["missing-app-config-values" /* MISSING_APP_CONFIG_VALUES */]: 'Missing App configuration value: "{$valueName}"',
51
- ["not-registered" /* NOT_REGISTERED */]: 'Firebase Installation is not registered.',
52
- ["installation-not-found" /* INSTALLATION_NOT_FOUND */]: 'Firebase Installation not found.',
53
- ["request-failed" /* REQUEST_FAILED */]: '{$requestName} request failed with error "{$serverCode} {$serverStatus}: {$serverMessage}"',
54
- ["app-offline" /* APP_OFFLINE */]: 'Could not process request. Application offline.',
55
- ["delete-pending-registration" /* DELETE_PENDING_REGISTRATION */]: "Can't delete installation while there is a pending registration request."
50
+ ["missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */]: 'Missing App configuration value: "{$valueName}"',
51
+ ["not-registered" /* ErrorCode.NOT_REGISTERED */]: 'Firebase Installation is not registered.',
52
+ ["installation-not-found" /* ErrorCode.INSTALLATION_NOT_FOUND */]: 'Firebase Installation not found.',
53
+ ["request-failed" /* ErrorCode.REQUEST_FAILED */]: '{$requestName} request failed with error "{$serverCode} {$serverStatus}: {$serverMessage}"',
54
+ ["app-offline" /* ErrorCode.APP_OFFLINE */]: 'Could not process request. Application offline.',
55
+ ["delete-pending-registration" /* ErrorCode.DELETE_PENDING_REGISTRATION */]: "Can't delete installation while there is a pending registration request."
56
56
  };
57
57
  const ERROR_FACTORY = new ErrorFactory(SERVICE, SERVICE_NAME, ERROR_DESCRIPTION_MAP);
58
58
  /** Returns true if error is a FirebaseError that is based on an error from the server. */
59
59
  function isServerError(error) {
60
60
  return (error instanceof FirebaseError &&
61
- error.code.includes("request-failed" /* REQUEST_FAILED */));
61
+ error.code.includes("request-failed" /* ErrorCode.REQUEST_FAILED */));
62
62
  }
63
63
 
64
64
  /**
@@ -83,7 +83,7 @@ function getInstallationsEndpoint({ projectId }) {
83
83
  function extractAuthTokenInfoFromResponse(response) {
84
84
  return {
85
85
  token: response.token,
86
- requestStatus: 2 /* COMPLETED */,
86
+ requestStatus: 2 /* RequestStatus.COMPLETED */,
87
87
  expiresIn: getExpiresInFromResponseExpiresIn(response.expiresIn),
88
88
  creationTime: Date.now()
89
89
  };
@@ -91,7 +91,7 @@ function extractAuthTokenInfoFromResponse(response) {
91
91
  async function getErrorFromResponse(requestName, response) {
92
92
  const responseJson = await response.json();
93
93
  const errorData = responseJson.error;
94
- return ERROR_FACTORY.create("request-failed" /* REQUEST_FAILED */, {
94
+ return ERROR_FACTORY.create("request-failed" /* ErrorCode.REQUEST_FAILED */, {
95
95
  requestName,
96
96
  serverCode: errorData.code,
97
97
  serverMessage: errorData.message,
@@ -176,7 +176,7 @@ async function createInstallationRequest({ appConfig, heartbeatServiceProvider }
176
176
  const responseValue = await response.json();
177
177
  const registeredInstallationEntry = {
178
178
  fid: responseValue.fid || fid,
179
- registrationStatus: 2 /* COMPLETED */,
179
+ registrationStatus: 2 /* RequestStatus.COMPLETED */,
180
180
  refreshToken: responseValue.refreshToken,
181
181
  authToken: extractAuthTokenInfoFromResponse(responseValue.authToken)
182
182
  };
@@ -514,7 +514,7 @@ async function getInstallationEntry(installations) {
514
514
  function updateOrCreateInstallationEntry(oldEntry) {
515
515
  const entry = oldEntry || {
516
516
  fid: generateFid(),
517
- registrationStatus: 0 /* NOT_STARTED */
517
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
518
518
  };
519
519
  return clearTimedOutRequest(entry);
520
520
  }
@@ -526,10 +526,10 @@ function updateOrCreateInstallationEntry(oldEntry) {
526
526
  * to be registered.
527
527
  */
528
528
  function triggerRegistrationIfNecessary(installations, installationEntry) {
529
- if (installationEntry.registrationStatus === 0 /* NOT_STARTED */) {
529
+ if (installationEntry.registrationStatus === 0 /* RequestStatus.NOT_STARTED */) {
530
530
  if (!navigator.onLine) {
531
531
  // Registration required but app is offline.
532
- const registrationPromiseWithError = Promise.reject(ERROR_FACTORY.create("app-offline" /* APP_OFFLINE */));
532
+ const registrationPromiseWithError = Promise.reject(ERROR_FACTORY.create("app-offline" /* ErrorCode.APP_OFFLINE */));
533
533
  return {
534
534
  installationEntry,
535
535
  registrationPromise: registrationPromiseWithError
@@ -538,13 +538,13 @@ function triggerRegistrationIfNecessary(installations, installationEntry) {
538
538
  // Try registering. Change status to IN_PROGRESS.
539
539
  const inProgressEntry = {
540
540
  fid: installationEntry.fid,
541
- registrationStatus: 1 /* IN_PROGRESS */,
541
+ registrationStatus: 1 /* RequestStatus.IN_PROGRESS */,
542
542
  registrationTime: Date.now()
543
543
  };
544
544
  const registrationPromise = registerInstallation(installations, inProgressEntry);
545
545
  return { installationEntry: inProgressEntry, registrationPromise };
546
546
  }
547
- else if (installationEntry.registrationStatus === 1 /* IN_PROGRESS */) {
547
+ else if (installationEntry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */) {
548
548
  return {
549
549
  installationEntry,
550
550
  registrationPromise: waitUntilFidRegistration(installations)
@@ -570,7 +570,7 @@ async function registerInstallation(installations, installationEntry) {
570
570
  // Registration failed. Set FID as not registered.
571
571
  await set(installations.appConfig, {
572
572
  fid: installationEntry.fid,
573
- registrationStatus: 0 /* NOT_STARTED */
573
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
574
574
  });
575
575
  }
576
576
  throw e;
@@ -582,12 +582,12 @@ async function waitUntilFidRegistration(installations) {
582
582
  // IndexedDB changes (yet, see https://github.com/WICG/indexed-db-observers),
583
583
  // so we need to poll.
584
584
  let entry = await updateInstallationRequest(installations.appConfig);
585
- while (entry.registrationStatus === 1 /* IN_PROGRESS */) {
585
+ while (entry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */) {
586
586
  // createInstallation request still in progress.
587
587
  await sleep(100);
588
588
  entry = await updateInstallationRequest(installations.appConfig);
589
589
  }
590
- if (entry.registrationStatus === 0 /* NOT_STARTED */) {
590
+ if (entry.registrationStatus === 0 /* RequestStatus.NOT_STARTED */) {
591
591
  // The request timed out or failed in a different call. Try again.
592
592
  const { installationEntry, registrationPromise } = await getInstallationEntry(installations);
593
593
  if (registrationPromise) {
@@ -611,7 +611,7 @@ async function waitUntilFidRegistration(installations) {
611
611
  function updateInstallationRequest(appConfig) {
612
612
  return update(appConfig, oldEntry => {
613
613
  if (!oldEntry) {
614
- throw ERROR_FACTORY.create("installation-not-found" /* INSTALLATION_NOT_FOUND */);
614
+ throw ERROR_FACTORY.create("installation-not-found" /* ErrorCode.INSTALLATION_NOT_FOUND */);
615
615
  }
616
616
  return clearTimedOutRequest(oldEntry);
617
617
  });
@@ -620,13 +620,13 @@ function clearTimedOutRequest(entry) {
620
620
  if (hasInstallationRequestTimedOut(entry)) {
621
621
  return {
622
622
  fid: entry.fid,
623
- registrationStatus: 0 /* NOT_STARTED */
623
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
624
624
  };
625
625
  }
626
626
  return entry;
627
627
  }
628
628
  function hasInstallationRequestTimedOut(installationEntry) {
629
- return (installationEntry.registrationStatus === 1 /* IN_PROGRESS */ &&
629
+ return (installationEntry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */ &&
630
630
  installationEntry.registrationTime + PENDING_TIMEOUT_MS < Date.now());
631
631
  }
632
632
 
@@ -710,14 +710,14 @@ async function refreshAuthToken(installations, forceRefresh = false) {
710
710
  let tokenPromise;
711
711
  const entry = await update(installations.appConfig, oldEntry => {
712
712
  if (!isEntryRegistered(oldEntry)) {
713
- throw ERROR_FACTORY.create("not-registered" /* NOT_REGISTERED */);
713
+ throw ERROR_FACTORY.create("not-registered" /* ErrorCode.NOT_REGISTERED */);
714
714
  }
715
715
  const oldAuthToken = oldEntry.authToken;
716
716
  if (!forceRefresh && isAuthTokenValid(oldAuthToken)) {
717
717
  // There is a valid token in the DB.
718
718
  return oldEntry;
719
719
  }
720
- else if (oldAuthToken.requestStatus === 1 /* IN_PROGRESS */) {
720
+ else if (oldAuthToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */) {
721
721
  // There already is a token request in progress.
722
722
  tokenPromise = waitUntilAuthTokenRequest(installations, forceRefresh);
723
723
  return oldEntry;
@@ -725,7 +725,7 @@ async function refreshAuthToken(installations, forceRefresh = false) {
725
725
  else {
726
726
  // No token or token expired.
727
727
  if (!navigator.onLine) {
728
- throw ERROR_FACTORY.create("app-offline" /* APP_OFFLINE */);
728
+ throw ERROR_FACTORY.create("app-offline" /* ErrorCode.APP_OFFLINE */);
729
729
  }
730
730
  const inProgressEntry = makeAuthTokenRequestInProgressEntry(oldEntry);
731
731
  tokenPromise = fetchAuthTokenFromServer(installations, inProgressEntry);
@@ -748,13 +748,13 @@ async function waitUntilAuthTokenRequest(installations, forceRefresh) {
748
748
  // IndexedDB changes (yet, see https://github.com/WICG/indexed-db-observers),
749
749
  // so we need to poll.
750
750
  let entry = await updateAuthTokenRequest(installations.appConfig);
751
- while (entry.authToken.requestStatus === 1 /* IN_PROGRESS */) {
751
+ while (entry.authToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */) {
752
752
  // generateAuthToken still in progress.
753
753
  await sleep(100);
754
754
  entry = await updateAuthTokenRequest(installations.appConfig);
755
755
  }
756
756
  const authToken = entry.authToken;
757
- if (authToken.requestStatus === 0 /* NOT_STARTED */) {
757
+ if (authToken.requestStatus === 0 /* RequestStatus.NOT_STARTED */) {
758
758
  // The request timed out or failed in a different call. Try again.
759
759
  return refreshAuthToken(installations, forceRefresh);
760
760
  }
@@ -773,11 +773,11 @@ async function waitUntilAuthTokenRequest(installations, forceRefresh) {
773
773
  function updateAuthTokenRequest(appConfig) {
774
774
  return update(appConfig, oldEntry => {
775
775
  if (!isEntryRegistered(oldEntry)) {
776
- throw ERROR_FACTORY.create("not-registered" /* NOT_REGISTERED */);
776
+ throw ERROR_FACTORY.create("not-registered" /* ErrorCode.NOT_REGISTERED */);
777
777
  }
778
778
  const oldAuthToken = oldEntry.authToken;
779
779
  if (hasAuthTokenRequestTimedOut(oldAuthToken)) {
780
- return Object.assign(Object.assign({}, oldEntry), { authToken: { requestStatus: 0 /* NOT_STARTED */ } });
780
+ return Object.assign(Object.assign({}, oldEntry), { authToken: { requestStatus: 0 /* RequestStatus.NOT_STARTED */ } });
781
781
  }
782
782
  return oldEntry;
783
783
  });
@@ -797,7 +797,7 @@ async function fetchAuthTokenFromServer(installations, installationEntry) {
797
797
  await remove(installations.appConfig);
798
798
  }
799
799
  else {
800
- const updatedInstallationEntry = Object.assign(Object.assign({}, installationEntry), { authToken: { requestStatus: 0 /* NOT_STARTED */ } });
800
+ const updatedInstallationEntry = Object.assign(Object.assign({}, installationEntry), { authToken: { requestStatus: 0 /* RequestStatus.NOT_STARTED */ } });
801
801
  await set(installations.appConfig, updatedInstallationEntry);
802
802
  }
803
803
  throw e;
@@ -805,10 +805,10 @@ async function fetchAuthTokenFromServer(installations, installationEntry) {
805
805
  }
806
806
  function isEntryRegistered(installationEntry) {
807
807
  return (installationEntry !== undefined &&
808
- installationEntry.registrationStatus === 2 /* COMPLETED */);
808
+ installationEntry.registrationStatus === 2 /* RequestStatus.COMPLETED */);
809
809
  }
810
810
  function isAuthTokenValid(authToken) {
811
- return (authToken.requestStatus === 2 /* COMPLETED */ &&
811
+ return (authToken.requestStatus === 2 /* RequestStatus.COMPLETED */ &&
812
812
  !isAuthTokenExpired(authToken));
813
813
  }
814
814
  function isAuthTokenExpired(authToken) {
@@ -819,13 +819,13 @@ function isAuthTokenExpired(authToken) {
819
819
  /** Returns an updated InstallationEntry with an InProgressAuthToken. */
820
820
  function makeAuthTokenRequestInProgressEntry(oldEntry) {
821
821
  const inProgressAuthToken = {
822
- requestStatus: 1 /* IN_PROGRESS */,
822
+ requestStatus: 1 /* RequestStatus.IN_PROGRESS */,
823
823
  requestTime: Date.now()
824
824
  };
825
825
  return Object.assign(Object.assign({}, oldEntry), { authToken: inProgressAuthToken });
826
826
  }
827
827
  function hasAuthTokenRequestTimedOut(authToken) {
828
- return (authToken.requestStatus === 1 /* IN_PROGRESS */ &&
828
+ return (authToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */ &&
829
829
  authToken.requestTime + PENDING_TIMEOUT_MS < Date.now());
830
830
  }
831
831
 
@@ -963,20 +963,20 @@ function getDeleteEndpoint(appConfig, { fid }) {
963
963
  async function deleteInstallations(installations) {
964
964
  const { appConfig } = installations;
965
965
  const entry = await update(appConfig, oldEntry => {
966
- if (oldEntry && oldEntry.registrationStatus === 0 /* NOT_STARTED */) {
966
+ if (oldEntry && oldEntry.registrationStatus === 0 /* RequestStatus.NOT_STARTED */) {
967
967
  // Delete the unregistered entry without sending a deleteInstallation request.
968
968
  return undefined;
969
969
  }
970
970
  return oldEntry;
971
971
  });
972
972
  if (entry) {
973
- if (entry.registrationStatus === 1 /* IN_PROGRESS */) {
973
+ if (entry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */) {
974
974
  // Can't delete while trying to register.
975
- throw ERROR_FACTORY.create("delete-pending-registration" /* DELETE_PENDING_REGISTRATION */);
975
+ throw ERROR_FACTORY.create("delete-pending-registration" /* ErrorCode.DELETE_PENDING_REGISTRATION */);
976
976
  }
977
- else if (entry.registrationStatus === 2 /* COMPLETED */) {
977
+ else if (entry.registrationStatus === 2 /* RequestStatus.COMPLETED */) {
978
978
  if (!navigator.onLine) {
979
- throw ERROR_FACTORY.create("app-offline" /* APP_OFFLINE */);
979
+ throw ERROR_FACTORY.create("app-offline" /* ErrorCode.APP_OFFLINE */);
980
980
  }
981
981
  else {
982
982
  await deleteInstallationRequest(appConfig, entry);
@@ -1089,7 +1089,7 @@ function extractAppConfig(app) {
1089
1089
  };
1090
1090
  }
1091
1091
  function getMissingValueError(valueName) {
1092
- return ERROR_FACTORY.create("missing-app-config-values" /* MISSING_APP_CONFIG_VALUES */, {
1092
+ return ERROR_FACTORY.create("missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */, {
1093
1093
  valueName
1094
1094
  });
1095
1095
  }
@@ -1136,8 +1136,8 @@ const internalFactory = (container) => {
1136
1136
  return installationsInternal;
1137
1137
  };
1138
1138
  function registerInstallations() {
1139
- _registerComponent(new Component(INSTALLATIONS_NAME, publicFactory, "PUBLIC" /* PUBLIC */));
1140
- _registerComponent(new Component(INSTALLATIONS_NAME_INTERNAL, internalFactory, "PRIVATE" /* PRIVATE */));
1139
+ _registerComponent(new Component(INSTALLATIONS_NAME, publicFactory, "PUBLIC" /* ComponentType.PUBLIC */));
1140
+ _registerComponent(new Component(INSTALLATIONS_NAME_INTERNAL, internalFactory, "PRIVATE" /* ComponentType.PRIVATE */));
1141
1141
  }
1142
1142
 
1143
1143
  /**