@firebase/installations 0.5.5-canary.ff2f7d4c8 → 0.5.6

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
@@ -6,10 +6,9 @@ var app = require('@firebase/app');
6
6
  var component = require('@firebase/component');
7
7
  var tslib = require('tslib');
8
8
  var util = require('@firebase/util');
9
- var idb = require('idb');
10
9
 
11
10
  var name = "@firebase/installations";
12
- var version = "0.5.5-canary.ff2f7d4c8";
11
+ var version = "0.5.6";
13
12
 
14
13
  /**
15
14
  * @license
@@ -174,15 +173,28 @@ function getAuthorizationHeader(refreshToken) {
174
173
  * See the License for the specific language governing permissions and
175
174
  * limitations under the License.
176
175
  */
177
- function createInstallationRequest(appConfig, _a) {
178
- var fid = _a.fid;
176
+ function createInstallationRequest(_a, _b) {
177
+ var appConfig = _a.appConfig, heartbeatServiceProvider = _a.heartbeatServiceProvider;
178
+ var fid = _b.fid;
179
179
  return tslib.__awaiter(this, void 0, void 0, function () {
180
- var endpoint, headers, body, request, response, responseValue, registeredInstallationEntry;
181
- return tslib.__generator(this, function (_b) {
182
- switch (_b.label) {
180
+ var endpoint, headers, heartbeatService, heartbeatsHeader, body, request, response, responseValue, registeredInstallationEntry;
181
+ return tslib.__generator(this, function (_c) {
182
+ switch (_c.label) {
183
183
  case 0:
184
184
  endpoint = getInstallationsEndpoint(appConfig);
185
185
  headers = getHeaders(appConfig);
186
+ heartbeatService = heartbeatServiceProvider.getImmediate({
187
+ optional: true
188
+ });
189
+ if (!heartbeatService) return [3 /*break*/, 2];
190
+ return [4 /*yield*/, heartbeatService.getHeartbeatsHeader()];
191
+ case 1:
192
+ heartbeatsHeader = _c.sent();
193
+ if (heartbeatsHeader) {
194
+ headers.append('x-firebase-client', heartbeatsHeader);
195
+ }
196
+ _c.label = 2;
197
+ case 2:
186
198
  body = {
187
199
  fid: fid,
188
200
  authVersion: INTERNAL_AUTH_VERSION,
@@ -195,12 +207,12 @@ function createInstallationRequest(appConfig, _a) {
195
207
  body: JSON.stringify(body)
196
208
  };
197
209
  return [4 /*yield*/, retryIfServerError(function () { return fetch(endpoint, request); })];
198
- case 1:
199
- response = _b.sent();
200
- if (!response.ok) return [3 /*break*/, 3];
210
+ case 3:
211
+ response = _c.sent();
212
+ if (!response.ok) return [3 /*break*/, 5];
201
213
  return [4 /*yield*/, response.json()];
202
- case 2:
203
- responseValue = _b.sent();
214
+ case 4:
215
+ responseValue = _c.sent();
204
216
  registeredInstallationEntry = {
205
217
  fid: responseValue.fid || fid,
206
218
  registrationStatus: 2 /* COMPLETED */,
@@ -208,8 +220,8 @@ function createInstallationRequest(appConfig, _a) {
208
220
  authToken: extractAuthTokenInfoFromResponse(responseValue.authToken)
209
221
  };
210
222
  return [2 /*return*/, registeredInstallationEntry];
211
- case 3: return [4 /*yield*/, getErrorFromResponse('Create Installation', response)];
212
- case 4: throw _b.sent();
223
+ case 5: return [4 /*yield*/, getErrorFromResponse('Create Installation', response)];
224
+ case 6: throw _c.sent();
213
225
  }
214
226
  });
215
227
  });
@@ -445,15 +457,15 @@ var OBJECT_STORE_NAME = 'firebase-installations-store';
445
457
  var dbPromise = null;
446
458
  function getDbPromise() {
447
459
  if (!dbPromise) {
448
- dbPromise = idb.openDb(DATABASE_NAME, DATABASE_VERSION, function (upgradeDB) {
460
+ dbPromise = util.openDB(DATABASE_NAME, DATABASE_VERSION, function (db, oldVersion) {
449
461
  // We don't use 'break' in this switch statement, the fall-through
450
462
  // behavior is what we want, because if there are multiple versions between
451
463
  // the old version and the current version, we want ALL the migrations
452
464
  // that correspond to those versions to run, not only the last one.
453
465
  // eslint-disable-next-line default-case
454
- switch (upgradeDB.oldVersion) {
466
+ switch (oldVersion) {
455
467
  case 0:
456
- upgradeDB.createObjectStore(OBJECT_STORE_NAME);
468
+ db.createObjectStore(OBJECT_STORE_NAME);
457
469
  }
458
470
  });
459
471
  }
@@ -474,7 +486,7 @@ function set(appConfig, value) {
474
486
  objectStore = tx.objectStore(OBJECT_STORE_NAME);
475
487
  return [4 /*yield*/, objectStore.get(key)];
476
488
  case 2:
477
- oldValue = _a.sent();
489
+ oldValue = (_a.sent());
478
490
  return [4 /*yield*/, objectStore.put(value, key)];
479
491
  case 3:
480
492
  _a.sent();
@@ -532,7 +544,7 @@ function update(appConfig, updateFn) {
532
544
  store = tx.objectStore(OBJECT_STORE_NAME);
533
545
  return [4 /*yield*/, store.get(key)];
534
546
  case 2:
535
- oldValue = _a.sent();
547
+ oldValue = (_a.sent());
536
548
  newValue = updateFn(oldValue);
537
549
  if (!(newValue === undefined)) return [3 /*break*/, 4];
538
550
  return [4 /*yield*/, store.delete(key)];
@@ -575,15 +587,15 @@ function update(appConfig, updateFn) {
575
587
  * Updates and returns the InstallationEntry from the database.
576
588
  * Also triggers a registration request if it is necessary and possible.
577
589
  */
578
- function getInstallationEntry(appConfig) {
590
+ function getInstallationEntry(installations) {
579
591
  return tslib.__awaiter(this, void 0, void 0, function () {
580
592
  var registrationPromise, installationEntry;
581
593
  var _a;
582
594
  return tslib.__generator(this, function (_b) {
583
595
  switch (_b.label) {
584
- case 0: return [4 /*yield*/, update(appConfig, function (oldEntry) {
596
+ case 0: return [4 /*yield*/, update(installations.appConfig, function (oldEntry) {
585
597
  var installationEntry = updateOrCreateInstallationEntry(oldEntry);
586
- var entryWithPromise = triggerRegistrationIfNecessary(appConfig, installationEntry);
598
+ var entryWithPromise = triggerRegistrationIfNecessary(installations, installationEntry);
587
599
  registrationPromise = entryWithPromise.registrationPromise;
588
600
  return entryWithPromise.installationEntry;
589
601
  })];
@@ -621,7 +633,7 @@ function updateOrCreateInstallationEntry(oldEntry) {
621
633
  * If registrationPromise does not exist, the installationEntry is guaranteed
622
634
  * to be registered.
623
635
  */
624
- function triggerRegistrationIfNecessary(appConfig, installationEntry) {
636
+ function triggerRegistrationIfNecessary(installations, installationEntry) {
625
637
  if (installationEntry.registrationStatus === 0 /* NOT_STARTED */) {
626
638
  if (!navigator.onLine) {
627
639
  // Registration required but app is offline.
@@ -637,13 +649,13 @@ function triggerRegistrationIfNecessary(appConfig, installationEntry) {
637
649
  registrationStatus: 1 /* IN_PROGRESS */,
638
650
  registrationTime: Date.now()
639
651
  };
640
- var registrationPromise = registerInstallation(appConfig, inProgressEntry);
652
+ var registrationPromise = registerInstallation(installations, inProgressEntry);
641
653
  return { installationEntry: inProgressEntry, registrationPromise: registrationPromise };
642
654
  }
643
655
  else if (installationEntry.registrationStatus === 1 /* IN_PROGRESS */) {
644
656
  return {
645
657
  installationEntry: installationEntry,
646
- registrationPromise: waitUntilFidRegistration(appConfig)
658
+ registrationPromise: waitUntilFidRegistration(installations)
647
659
  };
648
660
  }
649
661
  else {
@@ -651,23 +663,23 @@ function triggerRegistrationIfNecessary(appConfig, installationEntry) {
651
663
  }
652
664
  }
653
665
  /** This will be executed only once for each new Firebase Installation. */
654
- function registerInstallation(appConfig, installationEntry) {
666
+ function registerInstallation(installations, installationEntry) {
655
667
  return tslib.__awaiter(this, void 0, void 0, function () {
656
668
  var registeredInstallationEntry, e_1;
657
669
  return tslib.__generator(this, function (_a) {
658
670
  switch (_a.label) {
659
671
  case 0:
660
672
  _a.trys.push([0, 2, , 7]);
661
- return [4 /*yield*/, createInstallationRequest(appConfig, installationEntry)];
673
+ return [4 /*yield*/, createInstallationRequest(installations, installationEntry)];
662
674
  case 1:
663
675
  registeredInstallationEntry = _a.sent();
664
- return [2 /*return*/, set(appConfig, registeredInstallationEntry)];
676
+ return [2 /*return*/, set(installations.appConfig, registeredInstallationEntry)];
665
677
  case 2:
666
678
  e_1 = _a.sent();
667
679
  if (!(isServerError(e_1) && e_1.customData.serverCode === 409)) return [3 /*break*/, 4];
668
680
  // Server returned a "FID can not be used" error.
669
681
  // Generate a new ID next time.
670
- return [4 /*yield*/, remove(appConfig)];
682
+ return [4 /*yield*/, remove(installations.appConfig)];
671
683
  case 3:
672
684
  // Server returned a "FID can not be used" error.
673
685
  // Generate a new ID next time.
@@ -675,7 +687,7 @@ function registerInstallation(appConfig, installationEntry) {
675
687
  return [3 /*break*/, 6];
676
688
  case 4:
677
689
  // Registration failed. Set FID as not registered.
678
- return [4 /*yield*/, set(appConfig, {
690
+ return [4 /*yield*/, set(installations.appConfig, {
679
691
  fid: installationEntry.fid,
680
692
  registrationStatus: 0 /* NOT_STARTED */
681
693
  })];
@@ -690,12 +702,12 @@ function registerInstallation(appConfig, installationEntry) {
690
702
  });
691
703
  }
692
704
  /** Call if FID registration is pending in another request. */
693
- function waitUntilFidRegistration(appConfig) {
705
+ function waitUntilFidRegistration(installations) {
694
706
  return tslib.__awaiter(this, void 0, void 0, function () {
695
707
  var entry, _a, installationEntry, registrationPromise;
696
708
  return tslib.__generator(this, function (_b) {
697
709
  switch (_b.label) {
698
- case 0: return [4 /*yield*/, updateInstallationRequest(appConfig)];
710
+ case 0: return [4 /*yield*/, updateInstallationRequest(installations.appConfig)];
699
711
  case 1:
700
712
  entry = _b.sent();
701
713
  _b.label = 2;
@@ -706,13 +718,13 @@ function waitUntilFidRegistration(appConfig) {
706
718
  case 3:
707
719
  // createInstallation request still in progress.
708
720
  _b.sent();
709
- return [4 /*yield*/, updateInstallationRequest(appConfig)];
721
+ return [4 /*yield*/, updateInstallationRequest(installations.appConfig)];
710
722
  case 4:
711
723
  entry = _b.sent();
712
724
  return [3 /*break*/, 2];
713
725
  case 5:
714
726
  if (!(entry.registrationStatus === 0 /* NOT_STARTED */)) return [3 /*break*/, 7];
715
- return [4 /*yield*/, getInstallationEntry(appConfig)];
727
+ return [4 /*yield*/, getInstallationEntry(installations)];
716
728
  case 6:
717
729
  _a = _b.sent(), installationEntry = _a.installationEntry, registrationPromise = _a.registrationPromise;
718
730
  if (registrationPromise) {
@@ -774,23 +786,30 @@ function hasInstallationRequestTimedOut(installationEntry) {
774
786
  * limitations under the License.
775
787
  */
776
788
  function generateAuthTokenRequest(_a, installationEntry) {
777
- var appConfig = _a.appConfig, platformLoggerProvider = _a.platformLoggerProvider;
789
+ var appConfig = _a.appConfig, heartbeatServiceProvider = _a.heartbeatServiceProvider;
778
790
  return tslib.__awaiter(this, void 0, void 0, function () {
779
- var endpoint, headers, platformLogger, body, request, response, responseValue, completedAuthToken;
791
+ var endpoint, headers, heartbeatService, heartbeatsHeader, body, request, response, responseValue, completedAuthToken;
780
792
  return tslib.__generator(this, function (_b) {
781
793
  switch (_b.label) {
782
794
  case 0:
783
795
  endpoint = getGenerateAuthTokenEndpoint(appConfig, installationEntry);
784
796
  headers = getHeadersWithAuth(appConfig, installationEntry);
785
- platformLogger = platformLoggerProvider.getImmediate({
797
+ heartbeatService = heartbeatServiceProvider.getImmediate({
786
798
  optional: true
787
799
  });
788
- if (platformLogger) {
789
- headers.append('x-firebase-client', platformLogger.getPlatformInfoString());
800
+ if (!heartbeatService) return [3 /*break*/, 2];
801
+ return [4 /*yield*/, heartbeatService.getHeartbeatsHeader()];
802
+ case 1:
803
+ heartbeatsHeader = _b.sent();
804
+ if (heartbeatsHeader) {
805
+ headers.append('x-firebase-client', heartbeatsHeader);
790
806
  }
807
+ _b.label = 2;
808
+ case 2:
791
809
  body = {
792
810
  installation: {
793
- sdkVersion: PACKAGE_VERSION
811
+ sdkVersion: PACKAGE_VERSION,
812
+ appId: appConfig.appId
794
813
  }
795
814
  };
796
815
  request = {
@@ -799,16 +818,16 @@ function generateAuthTokenRequest(_a, installationEntry) {
799
818
  body: JSON.stringify(body)
800
819
  };
801
820
  return [4 /*yield*/, retryIfServerError(function () { return fetch(endpoint, request); })];
802
- case 1:
821
+ case 3:
803
822
  response = _b.sent();
804
- if (!response.ok) return [3 /*break*/, 3];
823
+ if (!response.ok) return [3 /*break*/, 5];
805
824
  return [4 /*yield*/, response.json()];
806
- case 2:
825
+ case 4:
807
826
  responseValue = _b.sent();
808
827
  completedAuthToken = extractAuthTokenInfoFromResponse(responseValue);
809
828
  return [2 /*return*/, completedAuthToken];
810
- case 3: return [4 /*yield*/, getErrorFromResponse('Generate Auth Token', response)];
811
- case 4: throw _b.sent();
829
+ case 5: return [4 /*yield*/, getErrorFromResponse('Generate Auth Token', response)];
830
+ case 6: throw _b.sent();
812
831
  }
813
832
  });
814
833
  });
@@ -1041,7 +1060,7 @@ function getId(installations) {
1041
1060
  switch (_b.label) {
1042
1061
  case 0:
1043
1062
  installationsImpl = installations;
1044
- return [4 /*yield*/, getInstallationEntry(installationsImpl.appConfig)];
1063
+ return [4 /*yield*/, getInstallationEntry(installationsImpl)];
1045
1064
  case 1:
1046
1065
  _a = _b.sent(), installationEntry = _a.installationEntry, registrationPromise = _a.registrationPromise;
1047
1066
  if (registrationPromise) {
@@ -1090,7 +1109,7 @@ function getToken(installations, forceRefresh) {
1090
1109
  switch (_a.label) {
1091
1110
  case 0:
1092
1111
  installationsImpl = installations;
1093
- return [4 /*yield*/, completeInstallationRegistration(installationsImpl.appConfig)];
1112
+ return [4 /*yield*/, completeInstallationRegistration(installationsImpl)];
1094
1113
  case 1:
1095
1114
  _a.sent();
1096
1115
  return [4 /*yield*/, refreshAuthToken(installationsImpl, forceRefresh)];
@@ -1101,12 +1120,12 @@ function getToken(installations, forceRefresh) {
1101
1120
  });
1102
1121
  });
1103
1122
  }
1104
- function completeInstallationRegistration(appConfig) {
1123
+ function completeInstallationRegistration(installations) {
1105
1124
  return tslib.__awaiter(this, void 0, void 0, function () {
1106
1125
  var registrationPromise;
1107
1126
  return tslib.__generator(this, function (_a) {
1108
1127
  switch (_a.label) {
1109
- case 0: return [4 /*yield*/, getInstallationEntry(appConfig)];
1128
+ case 0: return [4 /*yield*/, getInstallationEntry(installations)];
1110
1129
  case 1:
1111
1130
  registrationPromise = (_a.sent()).registrationPromise;
1112
1131
  if (!registrationPromise) return [3 /*break*/, 3];
@@ -1367,11 +1386,11 @@ var publicFactory = function (container) {
1367
1386
  var app$1 = container.getProvider('app').getImmediate();
1368
1387
  // Throws if app isn't configured properly.
1369
1388
  var appConfig = extractAppConfig(app$1);
1370
- var platformLoggerProvider = app._getProvider(app$1, 'platform-logger');
1389
+ var heartbeatServiceProvider = app._getProvider(app$1, 'heartbeat');
1371
1390
  var installationsImpl = {
1372
1391
  app: app$1,
1373
1392
  appConfig: appConfig,
1374
- platformLoggerProvider: platformLoggerProvider,
1393
+ heartbeatServiceProvider: heartbeatServiceProvider,
1375
1394
  _delete: function () { return Promise.resolve(); }
1376
1395
  };
1377
1396
  return installationsImpl;