@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.
package/dist/index.cjs.js CHANGED
@@ -9,7 +9,7 @@ var util = require('@firebase/util');
9
9
  var idb = require('idb');
10
10
 
11
11
  var name = "@firebase/installations";
12
- var version = "0.5.16";
12
+ var version = "0.6.0";
13
13
 
14
14
  /**
15
15
  * @license
@@ -28,7 +28,7 @@ var version = "0.5.16";
28
28
  * limitations under the License.
29
29
  */
30
30
  var PENDING_TIMEOUT_MS = 10000;
31
- var PACKAGE_VERSION = "w:" + version;
31
+ var PACKAGE_VERSION = "w:".concat(version);
32
32
  var INTERNAL_AUTH_VERSION = 'FIS_v2';
33
33
  var INSTALLATIONS_API_URL = 'https://firebaseinstallations.googleapis.com/v1';
34
34
  var TOKEN_EXPIRATION_BUFFER = 60 * 60 * 1000; // One hour
@@ -53,18 +53,18 @@ var SERVICE_NAME = 'Installations';
53
53
  */
54
54
  var _a;
55
55
  var ERROR_DESCRIPTION_MAP = (_a = {},
56
- _a["missing-app-config-values" /* MISSING_APP_CONFIG_VALUES */] = 'Missing App configuration value: "{$valueName}"',
57
- _a["not-registered" /* NOT_REGISTERED */] = 'Firebase Installation is not registered.',
58
- _a["installation-not-found" /* INSTALLATION_NOT_FOUND */] = 'Firebase Installation not found.',
59
- _a["request-failed" /* REQUEST_FAILED */] = '{$requestName} request failed with error "{$serverCode} {$serverStatus}: {$serverMessage}"',
60
- _a["app-offline" /* APP_OFFLINE */] = 'Could not process request. Application offline.',
61
- _a["delete-pending-registration" /* DELETE_PENDING_REGISTRATION */] = "Can't delete installation while there is a pending registration request.",
56
+ _a["missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */] = 'Missing App configuration value: "{$valueName}"',
57
+ _a["not-registered" /* ErrorCode.NOT_REGISTERED */] = 'Firebase Installation is not registered.',
58
+ _a["installation-not-found" /* ErrorCode.INSTALLATION_NOT_FOUND */] = 'Firebase Installation not found.',
59
+ _a["request-failed" /* ErrorCode.REQUEST_FAILED */] = '{$requestName} request failed with error "{$serverCode} {$serverStatus}: {$serverMessage}"',
60
+ _a["app-offline" /* ErrorCode.APP_OFFLINE */] = 'Could not process request. Application offline.',
61
+ _a["delete-pending-registration" /* ErrorCode.DELETE_PENDING_REGISTRATION */] = "Can't delete installation while there is a pending registration request.",
62
62
  _a);
63
63
  var ERROR_FACTORY = new util.ErrorFactory(SERVICE, SERVICE_NAME, ERROR_DESCRIPTION_MAP);
64
64
  /** Returns true if error is a FirebaseError that is based on an error from the server. */
65
65
  function isServerError(error) {
66
66
  return (error instanceof util.FirebaseError &&
67
- error.code.includes("request-failed" /* REQUEST_FAILED */));
67
+ error.code.includes("request-failed" /* ErrorCode.REQUEST_FAILED */));
68
68
  }
69
69
 
70
70
  /**
@@ -85,12 +85,12 @@ function isServerError(error) {
85
85
  */
86
86
  function getInstallationsEndpoint(_a) {
87
87
  var projectId = _a.projectId;
88
- return INSTALLATIONS_API_URL + "/projects/" + projectId + "/installations";
88
+ return "".concat(INSTALLATIONS_API_URL, "/projects/").concat(projectId, "/installations");
89
89
  }
90
90
  function extractAuthTokenInfoFromResponse(response) {
91
91
  return {
92
92
  token: response.token,
93
- requestStatus: 2 /* COMPLETED */,
93
+ requestStatus: 2 /* RequestStatus.COMPLETED */,
94
94
  expiresIn: getExpiresInFromResponseExpiresIn(response.expiresIn),
95
95
  creationTime: Date.now()
96
96
  };
@@ -104,7 +104,7 @@ function getErrorFromResponse(requestName, response) {
104
104
  case 1:
105
105
  responseJson = _a.sent();
106
106
  errorData = responseJson.error;
107
- return [2 /*return*/, ERROR_FACTORY.create("request-failed" /* REQUEST_FAILED */, {
107
+ return [2 /*return*/, ERROR_FACTORY.create("request-failed" /* ErrorCode.REQUEST_FAILED */, {
108
108
  requestName: requestName,
109
109
  serverCode: errorData.code,
110
110
  serverMessage: errorData.message,
@@ -155,7 +155,7 @@ function getExpiresInFromResponseExpiresIn(responseExpiresIn) {
155
155
  return Number(responseExpiresIn.replace('s', '000'));
156
156
  }
157
157
  function getAuthorizationHeader(refreshToken) {
158
- return INTERNAL_AUTH_VERSION + " " + refreshToken;
158
+ return "".concat(INTERNAL_AUTH_VERSION, " ").concat(refreshToken);
159
159
  }
160
160
 
161
161
  /**
@@ -216,7 +216,7 @@ function createInstallationRequest(_a, _b) {
216
216
  responseValue = _c.sent();
217
217
  registeredInstallationEntry = {
218
218
  fid: responseValue.fid || fid,
219
- registrationStatus: 2 /* COMPLETED */,
219
+ registrationStatus: 2 /* RequestStatus.COMPLETED */,
220
220
  refreshToken: responseValue.refreshToken,
221
221
  authToken: extractAuthTokenInfoFromResponse(responseValue.authToken)
222
222
  };
@@ -268,7 +268,7 @@ function sleep(ms) {
268
268
  * limitations under the License.
269
269
  */
270
270
  function bufferToBase64UrlSafe(array) {
271
- var b64 = btoa(String.fromCharCode.apply(String, tslib.__spreadArray([], tslib.__read(array))));
271
+ var b64 = btoa(String.fromCharCode.apply(String, tslib.__spreadArray([], tslib.__read(array), false)));
272
272
  return b64.replace(/\+/g, '-').replace(/\//g, '_');
273
273
  }
274
274
 
@@ -337,7 +337,7 @@ function encode(fidByteArray) {
337
337
  */
338
338
  /** Returns a string key that can be used to identify the app. */
339
339
  function getKey(appConfig) {
340
- return appConfig.appName + "!" + appConfig.appId;
340
+ return "".concat(appConfig.appName, "!").concat(appConfig.appId);
341
341
  }
342
342
 
343
343
  /**
@@ -625,7 +625,7 @@ function getInstallationEntry(installations) {
625
625
  function updateOrCreateInstallationEntry(oldEntry) {
626
626
  var entry = oldEntry || {
627
627
  fid: generateFid(),
628
- registrationStatus: 0 /* NOT_STARTED */
628
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
629
629
  };
630
630
  return clearTimedOutRequest(entry);
631
631
  }
@@ -637,10 +637,10 @@ function updateOrCreateInstallationEntry(oldEntry) {
637
637
  * to be registered.
638
638
  */
639
639
  function triggerRegistrationIfNecessary(installations, installationEntry) {
640
- if (installationEntry.registrationStatus === 0 /* NOT_STARTED */) {
640
+ if (installationEntry.registrationStatus === 0 /* RequestStatus.NOT_STARTED */) {
641
641
  if (!navigator.onLine) {
642
642
  // Registration required but app is offline.
643
- var registrationPromiseWithError = Promise.reject(ERROR_FACTORY.create("app-offline" /* APP_OFFLINE */));
643
+ var registrationPromiseWithError = Promise.reject(ERROR_FACTORY.create("app-offline" /* ErrorCode.APP_OFFLINE */));
644
644
  return {
645
645
  installationEntry: installationEntry,
646
646
  registrationPromise: registrationPromiseWithError
@@ -649,13 +649,13 @@ function triggerRegistrationIfNecessary(installations, installationEntry) {
649
649
  // Try registering. Change status to IN_PROGRESS.
650
650
  var inProgressEntry = {
651
651
  fid: installationEntry.fid,
652
- registrationStatus: 1 /* IN_PROGRESS */,
652
+ registrationStatus: 1 /* RequestStatus.IN_PROGRESS */,
653
653
  registrationTime: Date.now()
654
654
  };
655
655
  var registrationPromise = registerInstallation(installations, inProgressEntry);
656
656
  return { installationEntry: inProgressEntry, registrationPromise: registrationPromise };
657
657
  }
658
- else if (installationEntry.registrationStatus === 1 /* IN_PROGRESS */) {
658
+ else if (installationEntry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */) {
659
659
  return {
660
660
  installationEntry: installationEntry,
661
661
  registrationPromise: waitUntilFidRegistration(installations)
@@ -692,7 +692,7 @@ function registerInstallation(installations, installationEntry) {
692
692
  // Registration failed. Set FID as not registered.
693
693
  return [4 /*yield*/, set(installations.appConfig, {
694
694
  fid: installationEntry.fid,
695
- registrationStatus: 0 /* NOT_STARTED */
695
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
696
696
  })];
697
697
  case 5:
698
698
  // Registration failed. Set FID as not registered.
@@ -715,7 +715,7 @@ function waitUntilFidRegistration(installations) {
715
715
  entry = _b.sent();
716
716
  _b.label = 2;
717
717
  case 2:
718
- if (!(entry.registrationStatus === 1 /* IN_PROGRESS */)) return [3 /*break*/, 5];
718
+ if (!(entry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */)) return [3 /*break*/, 5];
719
719
  // createInstallation request still in progress.
720
720
  return [4 /*yield*/, sleep(100)];
721
721
  case 3:
@@ -726,7 +726,7 @@ function waitUntilFidRegistration(installations) {
726
726
  entry = _b.sent();
727
727
  return [3 /*break*/, 2];
728
728
  case 5:
729
- if (!(entry.registrationStatus === 0 /* NOT_STARTED */)) return [3 /*break*/, 7];
729
+ if (!(entry.registrationStatus === 0 /* RequestStatus.NOT_STARTED */)) return [3 /*break*/, 7];
730
730
  return [4 /*yield*/, getInstallationEntry(installations)];
731
731
  case 6:
732
732
  _a = _b.sent(), installationEntry = _a.installationEntry, registrationPromise = _a.registrationPromise;
@@ -753,7 +753,7 @@ function waitUntilFidRegistration(installations) {
753
753
  function updateInstallationRequest(appConfig) {
754
754
  return update(appConfig, function (oldEntry) {
755
755
  if (!oldEntry) {
756
- throw ERROR_FACTORY.create("installation-not-found" /* INSTALLATION_NOT_FOUND */);
756
+ throw ERROR_FACTORY.create("installation-not-found" /* ErrorCode.INSTALLATION_NOT_FOUND */);
757
757
  }
758
758
  return clearTimedOutRequest(oldEntry);
759
759
  });
@@ -762,13 +762,13 @@ function clearTimedOutRequest(entry) {
762
762
  if (hasInstallationRequestTimedOut(entry)) {
763
763
  return {
764
764
  fid: entry.fid,
765
- registrationStatus: 0 /* NOT_STARTED */
765
+ registrationStatus: 0 /* RequestStatus.NOT_STARTED */
766
766
  };
767
767
  }
768
768
  return entry;
769
769
  }
770
770
  function hasInstallationRequestTimedOut(installationEntry) {
771
- return (installationEntry.registrationStatus === 1 /* IN_PROGRESS */ &&
771
+ return (installationEntry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */ &&
772
772
  installationEntry.registrationTime + PENDING_TIMEOUT_MS < Date.now());
773
773
  }
774
774
 
@@ -837,7 +837,7 @@ function generateAuthTokenRequest(_a, installationEntry) {
837
837
  }
838
838
  function getGenerateAuthTokenEndpoint(appConfig, _a) {
839
839
  var fid = _a.fid;
840
- return getInstallationsEndpoint(appConfig) + "/" + fid + "/authTokens:generate";
840
+ return "".concat(getInstallationsEndpoint(appConfig), "/").concat(fid, "/authTokens:generate");
841
841
  }
842
842
 
843
843
  /**
@@ -870,14 +870,14 @@ function refreshAuthToken(installations, forceRefresh) {
870
870
  switch (_b.label) {
871
871
  case 0: return [4 /*yield*/, update(installations.appConfig, function (oldEntry) {
872
872
  if (!isEntryRegistered(oldEntry)) {
873
- throw ERROR_FACTORY.create("not-registered" /* NOT_REGISTERED */);
873
+ throw ERROR_FACTORY.create("not-registered" /* ErrorCode.NOT_REGISTERED */);
874
874
  }
875
875
  var oldAuthToken = oldEntry.authToken;
876
876
  if (!forceRefresh && isAuthTokenValid(oldAuthToken)) {
877
877
  // There is a valid token in the DB.
878
878
  return oldEntry;
879
879
  }
880
- else if (oldAuthToken.requestStatus === 1 /* IN_PROGRESS */) {
880
+ else if (oldAuthToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */) {
881
881
  // There already is a token request in progress.
882
882
  tokenPromise = waitUntilAuthTokenRequest(installations, forceRefresh);
883
883
  return oldEntry;
@@ -885,7 +885,7 @@ function refreshAuthToken(installations, forceRefresh) {
885
885
  else {
886
886
  // No token or token expired.
887
887
  if (!navigator.onLine) {
888
- throw ERROR_FACTORY.create("app-offline" /* APP_OFFLINE */);
888
+ throw ERROR_FACTORY.create("app-offline" /* ErrorCode.APP_OFFLINE */);
889
889
  }
890
890
  var inProgressEntry = makeAuthTokenRequestInProgressEntry(oldEntry);
891
891
  tokenPromise = fetchAuthTokenFromServer(installations, inProgressEntry);
@@ -925,7 +925,7 @@ function waitUntilAuthTokenRequest(installations, forceRefresh) {
925
925
  entry = _a.sent();
926
926
  _a.label = 2;
927
927
  case 2:
928
- if (!(entry.authToken.requestStatus === 1 /* IN_PROGRESS */)) return [3 /*break*/, 5];
928
+ if (!(entry.authToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */)) return [3 /*break*/, 5];
929
929
  // generateAuthToken still in progress.
930
930
  return [4 /*yield*/, sleep(100)];
931
931
  case 3:
@@ -937,7 +937,7 @@ function waitUntilAuthTokenRequest(installations, forceRefresh) {
937
937
  return [3 /*break*/, 2];
938
938
  case 5:
939
939
  authToken = entry.authToken;
940
- if (authToken.requestStatus === 0 /* NOT_STARTED */) {
940
+ if (authToken.requestStatus === 0 /* RequestStatus.NOT_STARTED */) {
941
941
  // The request timed out or failed in a different call. Try again.
942
942
  return [2 /*return*/, refreshAuthToken(installations, forceRefresh)];
943
943
  }
@@ -959,11 +959,11 @@ function waitUntilAuthTokenRequest(installations, forceRefresh) {
959
959
  function updateAuthTokenRequest(appConfig) {
960
960
  return update(appConfig, function (oldEntry) {
961
961
  if (!isEntryRegistered(oldEntry)) {
962
- throw ERROR_FACTORY.create("not-registered" /* NOT_REGISTERED */);
962
+ throw ERROR_FACTORY.create("not-registered" /* ErrorCode.NOT_REGISTERED */);
963
963
  }
964
964
  var oldAuthToken = oldEntry.authToken;
965
965
  if (hasAuthTokenRequestTimedOut(oldAuthToken)) {
966
- return tslib.__assign(tslib.__assign({}, oldEntry), { authToken: { requestStatus: 0 /* NOT_STARTED */ } });
966
+ return tslib.__assign(tslib.__assign({}, oldEntry), { authToken: { requestStatus: 0 /* RequestStatus.NOT_STARTED */ } });
967
967
  }
968
968
  return oldEntry;
969
969
  });
@@ -996,7 +996,7 @@ function fetchAuthTokenFromServer(installations, installationEntry) {
996
996
  _a.sent();
997
997
  return [3 /*break*/, 7];
998
998
  case 5:
999
- updatedInstallationEntry = tslib.__assign(tslib.__assign({}, installationEntry), { authToken: { requestStatus: 0 /* NOT_STARTED */ } });
999
+ updatedInstallationEntry = tslib.__assign(tslib.__assign({}, installationEntry), { authToken: { requestStatus: 0 /* RequestStatus.NOT_STARTED */ } });
1000
1000
  return [4 /*yield*/, set(installations.appConfig, updatedInstallationEntry)];
1001
1001
  case 6:
1002
1002
  _a.sent();
@@ -1009,10 +1009,10 @@ function fetchAuthTokenFromServer(installations, installationEntry) {
1009
1009
  }
1010
1010
  function isEntryRegistered(installationEntry) {
1011
1011
  return (installationEntry !== undefined &&
1012
- installationEntry.registrationStatus === 2 /* COMPLETED */);
1012
+ installationEntry.registrationStatus === 2 /* RequestStatus.COMPLETED */);
1013
1013
  }
1014
1014
  function isAuthTokenValid(authToken) {
1015
- return (authToken.requestStatus === 2 /* COMPLETED */ &&
1015
+ return (authToken.requestStatus === 2 /* RequestStatus.COMPLETED */ &&
1016
1016
  !isAuthTokenExpired(authToken));
1017
1017
  }
1018
1018
  function isAuthTokenExpired(authToken) {
@@ -1023,13 +1023,13 @@ function isAuthTokenExpired(authToken) {
1023
1023
  /** Returns an updated InstallationEntry with an InProgressAuthToken. */
1024
1024
  function makeAuthTokenRequestInProgressEntry(oldEntry) {
1025
1025
  var inProgressAuthToken = {
1026
- requestStatus: 1 /* IN_PROGRESS */,
1026
+ requestStatus: 1 /* RequestStatus.IN_PROGRESS */,
1027
1027
  requestTime: Date.now()
1028
1028
  };
1029
1029
  return tslib.__assign(tslib.__assign({}, oldEntry), { authToken: inProgressAuthToken });
1030
1030
  }
1031
1031
  function hasAuthTokenRequestTimedOut(authToken) {
1032
- return (authToken.requestStatus === 1 /* IN_PROGRESS */ &&
1032
+ return (authToken.requestStatus === 1 /* RequestStatus.IN_PROGRESS */ &&
1033
1033
  authToken.requestTime + PENDING_TIMEOUT_MS < Date.now());
1034
1034
  }
1035
1035
 
@@ -1185,7 +1185,7 @@ function deleteInstallationRequest(appConfig, installationEntry) {
1185
1185
  }
1186
1186
  function getDeleteEndpoint(appConfig, _a) {
1187
1187
  var fid = _a.fid;
1188
- return getInstallationsEndpoint(appConfig) + "/" + fid;
1188
+ return "".concat(getInstallationsEndpoint(appConfig), "/").concat(fid);
1189
1189
  }
1190
1190
 
1191
1191
  /**
@@ -1218,7 +1218,7 @@ function deleteInstallations(installations) {
1218
1218
  case 0:
1219
1219
  appConfig = installations.appConfig;
1220
1220
  return [4 /*yield*/, update(appConfig, function (oldEntry) {
1221
- if (oldEntry && oldEntry.registrationStatus === 0 /* NOT_STARTED */) {
1221
+ if (oldEntry && oldEntry.registrationStatus === 0 /* RequestStatus.NOT_STARTED */) {
1222
1222
  // Delete the unregistered entry without sending a deleteInstallation request.
1223
1223
  return undefined;
1224
1224
  }
@@ -1227,13 +1227,13 @@ function deleteInstallations(installations) {
1227
1227
  case 1:
1228
1228
  entry = _a.sent();
1229
1229
  if (!entry) return [3 /*break*/, 6];
1230
- if (!(entry.registrationStatus === 1 /* IN_PROGRESS */)) return [3 /*break*/, 2];
1230
+ if (!(entry.registrationStatus === 1 /* RequestStatus.IN_PROGRESS */)) return [3 /*break*/, 2];
1231
1231
  // Can't delete while trying to register.
1232
- throw ERROR_FACTORY.create("delete-pending-registration" /* DELETE_PENDING_REGISTRATION */);
1232
+ throw ERROR_FACTORY.create("delete-pending-registration" /* ErrorCode.DELETE_PENDING_REGISTRATION */);
1233
1233
  case 2:
1234
- if (!(entry.registrationStatus === 2 /* COMPLETED */)) return [3 /*break*/, 6];
1234
+ if (!(entry.registrationStatus === 2 /* RequestStatus.COMPLETED */)) return [3 /*break*/, 6];
1235
1235
  if (!!navigator.onLine) return [3 /*break*/, 3];
1236
- throw ERROR_FACTORY.create("app-offline" /* APP_OFFLINE */);
1236
+ throw ERROR_FACTORY.create("app-offline" /* ErrorCode.APP_OFFLINE */);
1237
1237
  case 3: return [4 /*yield*/, deleteInstallationRequest(appConfig, entry)];
1238
1238
  case 4:
1239
1239
  _a.sent();
@@ -1362,7 +1362,7 @@ function extractAppConfig(app) {
1362
1362
  };
1363
1363
  }
1364
1364
  function getMissingValueError(valueName) {
1365
- return ERROR_FACTORY.create("missing-app-config-values" /* MISSING_APP_CONFIG_VALUES */, {
1365
+ return ERROR_FACTORY.create("missing-app-config-values" /* ErrorCode.MISSING_APP_CONFIG_VALUES */, {
1366
1366
  valueName: valueName
1367
1367
  });
1368
1368
  }
@@ -1409,8 +1409,8 @@ var internalFactory = function (container) {
1409
1409
  return installationsInternal;
1410
1410
  };
1411
1411
  function registerInstallations() {
1412
- app._registerComponent(new component.Component(INSTALLATIONS_NAME, publicFactory, "PUBLIC" /* PUBLIC */));
1413
- app._registerComponent(new component.Component(INSTALLATIONS_NAME_INTERNAL, internalFactory, "PRIVATE" /* PRIVATE */));
1412
+ app._registerComponent(new component.Component(INSTALLATIONS_NAME, publicFactory, "PUBLIC" /* ComponentType.PUBLIC */));
1413
+ app._registerComponent(new component.Component(INSTALLATIONS_NAME_INTERNAL, internalFactory, "PRIVATE" /* ComponentType.PRIVATE */));
1414
1414
  }
1415
1415
 
1416
1416
  /**