@firebase/app 0.7.17 → 0.7.18-20222320822

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,6 +6,7 @@ var component = require('@firebase/component');
6
6
  var tslib = require('tslib');
7
7
  var logger$1 = require('@firebase/logger');
8
8
  var util = require('@firebase/util');
9
+ var idb = require('idb');
9
10
 
10
11
  /**
11
12
  * @license
@@ -62,7 +63,7 @@ function isVersionServiceProvider(provider) {
62
63
  }
63
64
 
64
65
  var name$o = "@firebase/app";
65
- var version$1 = "0.7.17";
66
+ var version$1 = "0.7.18-20222320822";
66
67
 
67
68
  /**
68
69
  * @license
@@ -129,7 +130,7 @@ var name$2 = "@firebase/firestore";
129
130
  var name$1 = "@firebase/firestore-compat";
130
131
 
131
132
  var name = "firebase";
132
- var version = "9.6.7";
133
+ var version = "9.6.8-20222320822";
133
134
 
134
135
  /**
135
136
  * @license
@@ -271,6 +272,12 @@ function _registerComponent(component) {
271
272
  * @internal
272
273
  */
273
274
  function _getProvider(app, name) {
275
+ var heartbeatController = app.container
276
+ .getProvider('heartbeat')
277
+ .getImmediate({ optional: true });
278
+ if (heartbeatController) {
279
+ void heartbeatController.triggerHeartbeat();
280
+ }
274
281
  return app.container.getProvider(name);
275
282
  }
276
283
  /**
@@ -320,6 +327,10 @@ var ERRORS = (_a = {},
320
327
  _a["invalid-app-argument" /* INVALID_APP_ARGUMENT */] = 'firebase.{$appName}() takes either no argument or a ' +
321
328
  'Firebase App instance.',
322
329
  _a["invalid-log-argument" /* INVALID_LOG_ARGUMENT */] = 'First argument to `onLog` must be null or a function.',
330
+ _a["storage-open" /* STORAGE_OPEN */] = 'Error thrown when opening storage. Original error: {$originalErrorMessage}.',
331
+ _a["storage-get" /* STORAGE_GET */] = 'Error thrown when reading from storage. Original error: {$originalErrorMessage}.',
332
+ _a["storage-set" /* STORAGE_WRITE */] = 'Error thrown when writing to storage. Original error: {$originalErrorMessage}.',
333
+ _a["storage-delete" /* STORAGE_DELETE */] = 'Error thrown when deleting from storage. Original error: {$originalErrorMessage}.',
323
334
  _a);
324
335
  var ERROR_FACTORY = new util.ErrorFactory('app', 'Firebase', ERRORS);
325
336
 
@@ -625,6 +636,454 @@ function setLogLevel(logLevel) {
625
636
  logger$1.setLogLevel(logLevel);
626
637
  }
627
638
 
639
+ /**
640
+ * @license
641
+ * Copyright 2021 Google LLC
642
+ *
643
+ * Licensed under the Apache License, Version 2.0 (the "License");
644
+ * you may not use this file except in compliance with the License.
645
+ * You may obtain a copy of the License at
646
+ *
647
+ * http://www.apache.org/licenses/LICENSE-2.0
648
+ *
649
+ * Unless required by applicable law or agreed to in writing, software
650
+ * distributed under the License is distributed on an "AS IS" BASIS,
651
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
652
+ * See the License for the specific language governing permissions and
653
+ * limitations under the License.
654
+ */
655
+ var DB_NAME = 'firebase-heartbeat-database';
656
+ var DB_VERSION = 1;
657
+ var STORE_NAME = 'firebase-heartbeat-store';
658
+ var dbPromise = null;
659
+ function getDbPromise() {
660
+ if (!dbPromise) {
661
+ dbPromise = idb.openDb(DB_NAME, DB_VERSION, function (upgradeDB) {
662
+ // We don't use 'break' in this switch statement, the fall-through
663
+ // behavior is what we want, because if there are multiple versions between
664
+ // the old version and the current version, we want ALL the migrations
665
+ // that correspond to those versions to run, not only the last one.
666
+ // eslint-disable-next-line default-case
667
+ switch (upgradeDB.oldVersion) {
668
+ case 0:
669
+ upgradeDB.createObjectStore(STORE_NAME);
670
+ }
671
+ }).catch(function (e) {
672
+ throw ERROR_FACTORY.create("storage-open" /* STORAGE_OPEN */, {
673
+ originalErrorMessage: e.message
674
+ });
675
+ });
676
+ }
677
+ return dbPromise;
678
+ }
679
+ function readHeartbeatsFromIndexedDB(app) {
680
+ return tslib.__awaiter(this, void 0, void 0, function () {
681
+ var db, e_1;
682
+ return tslib.__generator(this, function (_a) {
683
+ switch (_a.label) {
684
+ case 0:
685
+ _a.trys.push([0, 2, , 3]);
686
+ return [4 /*yield*/, getDbPromise()];
687
+ case 1:
688
+ db = _a.sent();
689
+ return [2 /*return*/, db
690
+ .transaction(STORE_NAME)
691
+ .objectStore(STORE_NAME)
692
+ .get(computeKey(app))];
693
+ case 2:
694
+ e_1 = _a.sent();
695
+ throw ERROR_FACTORY.create("storage-get" /* STORAGE_GET */, {
696
+ originalErrorMessage: e_1.message
697
+ });
698
+ case 3: return [2 /*return*/];
699
+ }
700
+ });
701
+ });
702
+ }
703
+ function writeHeartbeatsToIndexedDB(app, heartbeatObject) {
704
+ return tslib.__awaiter(this, void 0, void 0, function () {
705
+ var db, tx, objectStore, e_2;
706
+ return tslib.__generator(this, function (_a) {
707
+ switch (_a.label) {
708
+ case 0:
709
+ _a.trys.push([0, 3, , 4]);
710
+ return [4 /*yield*/, getDbPromise()];
711
+ case 1:
712
+ db = _a.sent();
713
+ tx = db.transaction(STORE_NAME, 'readwrite');
714
+ objectStore = tx.objectStore(STORE_NAME);
715
+ return [4 /*yield*/, objectStore.put(heartbeatObject, computeKey(app))];
716
+ case 2:
717
+ _a.sent();
718
+ return [2 /*return*/, tx.complete];
719
+ case 3:
720
+ e_2 = _a.sent();
721
+ throw ERROR_FACTORY.create("storage-set" /* STORAGE_WRITE */, {
722
+ originalErrorMessage: e_2.message
723
+ });
724
+ case 4: return [2 /*return*/];
725
+ }
726
+ });
727
+ });
728
+ }
729
+ function deleteHeartbeatsFromIndexedDB(app) {
730
+ return tslib.__awaiter(this, void 0, void 0, function () {
731
+ var db, tx, e_3;
732
+ return tslib.__generator(this, function (_a) {
733
+ switch (_a.label) {
734
+ case 0:
735
+ _a.trys.push([0, 3, , 4]);
736
+ return [4 /*yield*/, getDbPromise()];
737
+ case 1:
738
+ db = _a.sent();
739
+ tx = db.transaction(STORE_NAME, 'readwrite');
740
+ return [4 /*yield*/, tx.objectStore(STORE_NAME).delete(computeKey(app))];
741
+ case 2:
742
+ _a.sent();
743
+ return [2 /*return*/, tx.complete];
744
+ case 3:
745
+ e_3 = _a.sent();
746
+ throw ERROR_FACTORY.create("storage-delete" /* STORAGE_DELETE */, {
747
+ originalErrorMessage: e_3.message
748
+ });
749
+ case 4: return [2 /*return*/];
750
+ }
751
+ });
752
+ });
753
+ }
754
+ function computeKey(app) {
755
+ return app.name + "!" + app.options.appId;
756
+ }
757
+
758
+ /**
759
+ * @license
760
+ * Copyright 2021 Google LLC
761
+ *
762
+ * Licensed under the Apache License, Version 2.0 (the "License");
763
+ * you may not use this file except in compliance with the License.
764
+ * You may obtain a copy of the License at
765
+ *
766
+ * http://www.apache.org/licenses/LICENSE-2.0
767
+ *
768
+ * Unless required by applicable law or agreed to in writing, software
769
+ * distributed under the License is distributed on an "AS IS" BASIS,
770
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
771
+ * See the License for the specific language governing permissions and
772
+ * limitations under the License.
773
+ */
774
+ var MAX_HEADER_BYTES = 1024;
775
+ // 30 days
776
+ var STORED_HEARTBEAT_RETENTION_MAX_MILLIS = 30 * 24 * 60 * 60 * 1000;
777
+ var HeartbeatServiceImpl = /** @class */ (function () {
778
+ function HeartbeatServiceImpl(container) {
779
+ var _this = this;
780
+ this.container = container;
781
+ /**
782
+ * In-memory cache for heartbeats, used by getHeartbeatsHeader() to generate
783
+ * the header string.
784
+ * Stores one record per date. This will be consolidated into the standard
785
+ * format of one record per user agent string before being sent as a header.
786
+ * Populated from indexedDB when the controller is instantiated and should
787
+ * be kept in sync with indexedDB.
788
+ * Leave public for easier testing.
789
+ */
790
+ this._heartbeatsCache = null;
791
+ var app = this.container.getProvider('app').getImmediate();
792
+ this._storage = new HeartbeatStorageImpl(app);
793
+ this._heartbeatsCachePromise = this._storage.read().then(function (result) {
794
+ _this._heartbeatsCache = result;
795
+ return result;
796
+ });
797
+ }
798
+ /**
799
+ * Called to report a heartbeat. The function will generate
800
+ * a HeartbeatsByUserAgent object, update heartbeatsCache, and persist it
801
+ * to IndexedDB.
802
+ * Note that we only store one heartbeat per day. So if a heartbeat for today is
803
+ * already logged, subsequent calls to this function in the same day will be ignored.
804
+ */
805
+ HeartbeatServiceImpl.prototype.triggerHeartbeat = function () {
806
+ return tslib.__awaiter(this, void 0, void 0, function () {
807
+ var platformLogger, userAgent, date, _a;
808
+ return tslib.__generator(this, function (_b) {
809
+ switch (_b.label) {
810
+ case 0:
811
+ platformLogger = this.container
812
+ .getProvider('platform-logger')
813
+ .getImmediate();
814
+ userAgent = platformLogger.getPlatformInfoString();
815
+ date = getUTCDateString();
816
+ if (!(this._heartbeatsCache === null)) return [3 /*break*/, 2];
817
+ _a = this;
818
+ return [4 /*yield*/, this._heartbeatsCachePromise];
819
+ case 1:
820
+ _a._heartbeatsCache = _b.sent();
821
+ _b.label = 2;
822
+ case 2:
823
+ if (this._heartbeatsCache.some(function (singleDateHeartbeat) { return singleDateHeartbeat.date === date; })) {
824
+ // Do not store a heartbeat if one is already stored for this day.
825
+ return [2 /*return*/];
826
+ }
827
+ else {
828
+ // There is no entry for this date. Create one.
829
+ this._heartbeatsCache.push({ date: date, userAgent: userAgent });
830
+ }
831
+ // Remove entries older than 30 days.
832
+ this._heartbeatsCache = this._heartbeatsCache.filter(function (singleDateHeartbeat) {
833
+ var hbTimestamp = new Date(singleDateHeartbeat.date).valueOf();
834
+ var now = Date.now();
835
+ return now - hbTimestamp <= STORED_HEARTBEAT_RETENTION_MAX_MILLIS;
836
+ });
837
+ return [2 /*return*/, this._storage.overwrite(this._heartbeatsCache)];
838
+ }
839
+ });
840
+ });
841
+ };
842
+ /**
843
+ * Returns a base64 encoded string which can be attached to the heartbeat-specific header directly.
844
+ * It also clears all heartbeats from memory as well as in IndexedDB.
845
+ *
846
+ * NOTE: It will read heartbeats from the heartbeatsCache, instead of from indexedDB to reduce latency
847
+ */
848
+ HeartbeatServiceImpl.prototype.getHeartbeatsHeader = function () {
849
+ return tslib.__awaiter(this, void 0, void 0, function () {
850
+ var _a, heartbeatsToSend, unsentEntries, headerString;
851
+ return tslib.__generator(this, function (_b) {
852
+ switch (_b.label) {
853
+ case 0:
854
+ if (!(this._heartbeatsCache === null)) return [3 /*break*/, 2];
855
+ return [4 /*yield*/, this._heartbeatsCachePromise];
856
+ case 1:
857
+ _b.sent();
858
+ _b.label = 2;
859
+ case 2:
860
+ // If it's still null, it's been cleared and has not been repopulated.
861
+ if (this._heartbeatsCache === null) {
862
+ return [2 /*return*/, ''];
863
+ }
864
+ _a = extractHeartbeatsForHeader(this._heartbeatsCache), heartbeatsToSend = _a.heartbeatsToSend, unsentEntries = _a.unsentEntries;
865
+ headerString = util.base64Encode(JSON.stringify({ version: 2, heartbeats: heartbeatsToSend }));
866
+ if (!(unsentEntries.length > 0)) return [3 /*break*/, 4];
867
+ // Store any unsent entries if they exist.
868
+ this._heartbeatsCache = unsentEntries;
869
+ // This seems more likely than deleteAll (below) to lead to some odd state
870
+ // since the cache isn't empty and this will be called again on the next request,
871
+ // and is probably safest if we await it.
872
+ return [4 /*yield*/, this._storage.overwrite(this._heartbeatsCache)];
873
+ case 3:
874
+ // This seems more likely than deleteAll (below) to lead to some odd state
875
+ // since the cache isn't empty and this will be called again on the next request,
876
+ // and is probably safest if we await it.
877
+ _b.sent();
878
+ return [3 /*break*/, 5];
879
+ case 4:
880
+ this._heartbeatsCache = null;
881
+ // Do not wait for this, to reduce latency.
882
+ void this._storage.deleteAll();
883
+ _b.label = 5;
884
+ case 5: return [2 /*return*/, headerString];
885
+ }
886
+ });
887
+ });
888
+ };
889
+ return HeartbeatServiceImpl;
890
+ }());
891
+ function getUTCDateString() {
892
+ var today = new Date();
893
+ // Returns date format 'YYYY-MM-DD'
894
+ return today.toISOString().substring(0, 10);
895
+ }
896
+ function extractHeartbeatsForHeader(heartbeatsCache, maxSize) {
897
+ var e_1, _a;
898
+ if (maxSize === void 0) { maxSize = MAX_HEADER_BYTES; }
899
+ // Heartbeats grouped by user agent in the standard format to be sent in
900
+ // the header.
901
+ var heartbeatsToSend = [];
902
+ // Single date format heartbeats that are not sent.
903
+ var unsentEntries = heartbeatsCache.slice();
904
+ var _loop_1 = function (singleDateHeartbeat) {
905
+ // Look for an existing entry with the same user agent.
906
+ var heartbeatEntry = heartbeatsToSend.find(function (hb) { return hb.userAgent === singleDateHeartbeat.userAgent; });
907
+ if (!heartbeatEntry) {
908
+ // If no entry for this user agent exists, create one.
909
+ heartbeatsToSend.push({
910
+ userAgent: singleDateHeartbeat.userAgent,
911
+ dates: [singleDateHeartbeat.date]
912
+ });
913
+ if (countBytes(heartbeatsToSend) > maxSize) {
914
+ // If the header would exceed max size, remove the added heartbeat
915
+ // entry and stop adding to the header.
916
+ heartbeatsToSend.pop();
917
+ return "break";
918
+ }
919
+ }
920
+ else {
921
+ heartbeatEntry.dates.push(singleDateHeartbeat.date);
922
+ // If the header would exceed max size, remove the added date
923
+ // and stop adding to the header.
924
+ if (countBytes(heartbeatsToSend) > maxSize) {
925
+ heartbeatEntry.dates.pop();
926
+ return "break";
927
+ }
928
+ }
929
+ // Pop unsent entry from queue. (Skipped if adding the entry exceeded
930
+ // quota and the loop breaks early.)
931
+ unsentEntries = unsentEntries.slice(1);
932
+ };
933
+ try {
934
+ for (var heartbeatsCache_1 = tslib.__values(heartbeatsCache), heartbeatsCache_1_1 = heartbeatsCache_1.next(); !heartbeatsCache_1_1.done; heartbeatsCache_1_1 = heartbeatsCache_1.next()) {
935
+ var singleDateHeartbeat = heartbeatsCache_1_1.value;
936
+ var state_1 = _loop_1(singleDateHeartbeat);
937
+ if (state_1 === "break")
938
+ break;
939
+ }
940
+ }
941
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
942
+ finally {
943
+ try {
944
+ if (heartbeatsCache_1_1 && !heartbeatsCache_1_1.done && (_a = heartbeatsCache_1.return)) _a.call(heartbeatsCache_1);
945
+ }
946
+ finally { if (e_1) throw e_1.error; }
947
+ }
948
+ return {
949
+ heartbeatsToSend: heartbeatsToSend,
950
+ unsentEntries: unsentEntries
951
+ };
952
+ }
953
+ var HeartbeatStorageImpl = /** @class */ (function () {
954
+ function HeartbeatStorageImpl(app) {
955
+ this.app = app;
956
+ this._canUseIndexedDBPromise = this.runIndexedDBEnvironmentCheck();
957
+ }
958
+ HeartbeatStorageImpl.prototype.runIndexedDBEnvironmentCheck = function () {
959
+ return tslib.__awaiter(this, void 0, void 0, function () {
960
+ return tslib.__generator(this, function (_a) {
961
+ if (!util.isIndexedDBAvailable()) {
962
+ return [2 /*return*/, false];
963
+ }
964
+ else {
965
+ return [2 /*return*/, util.validateIndexedDBOpenable()
966
+ .then(function () { return true; })
967
+ .catch(function () { return false; })];
968
+ }
969
+ });
970
+ });
971
+ };
972
+ /**
973
+ * Read all heartbeats.
974
+ */
975
+ HeartbeatStorageImpl.prototype.read = function () {
976
+ return tslib.__awaiter(this, void 0, void 0, function () {
977
+ var canUseIndexedDB, idbHeartbeatObject;
978
+ return tslib.__generator(this, function (_a) {
979
+ switch (_a.label) {
980
+ case 0: return [4 /*yield*/, this._canUseIndexedDBPromise];
981
+ case 1:
982
+ canUseIndexedDB = _a.sent();
983
+ if (!!canUseIndexedDB) return [3 /*break*/, 2];
984
+ return [2 /*return*/, []];
985
+ case 2: return [4 /*yield*/, readHeartbeatsFromIndexedDB(this.app)];
986
+ case 3:
987
+ idbHeartbeatObject = _a.sent();
988
+ return [2 /*return*/, (idbHeartbeatObject === null || idbHeartbeatObject === void 0 ? void 0 : idbHeartbeatObject.heartbeats) || []];
989
+ }
990
+ });
991
+ });
992
+ };
993
+ // overwrite the storage with the provided heartbeats
994
+ HeartbeatStorageImpl.prototype.overwrite = function (heartbeats) {
995
+ return tslib.__awaiter(this, void 0, void 0, function () {
996
+ var canUseIndexedDB;
997
+ return tslib.__generator(this, function (_a) {
998
+ switch (_a.label) {
999
+ case 0: return [4 /*yield*/, this._canUseIndexedDBPromise];
1000
+ case 1:
1001
+ canUseIndexedDB = _a.sent();
1002
+ if (!canUseIndexedDB) {
1003
+ return [2 /*return*/];
1004
+ }
1005
+ else {
1006
+ return [2 /*return*/, writeHeartbeatsToIndexedDB(this.app, { heartbeats: heartbeats })];
1007
+ }
1008
+ }
1009
+ });
1010
+ });
1011
+ };
1012
+ // add heartbeats
1013
+ HeartbeatStorageImpl.prototype.add = function (heartbeats) {
1014
+ return tslib.__awaiter(this, void 0, void 0, function () {
1015
+ var canUseIndexedDB, existingHeartbeats;
1016
+ return tslib.__generator(this, function (_a) {
1017
+ switch (_a.label) {
1018
+ case 0: return [4 /*yield*/, this._canUseIndexedDBPromise];
1019
+ case 1:
1020
+ canUseIndexedDB = _a.sent();
1021
+ if (!!canUseIndexedDB) return [3 /*break*/, 2];
1022
+ return [2 /*return*/];
1023
+ case 2: return [4 /*yield*/, this.read()];
1024
+ case 3:
1025
+ existingHeartbeats = _a.sent();
1026
+ return [2 /*return*/, writeHeartbeatsToIndexedDB(this.app, {
1027
+ heartbeats: tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(existingHeartbeats)), tslib.__read(heartbeats))
1028
+ })];
1029
+ }
1030
+ });
1031
+ });
1032
+ };
1033
+ // delete heartbeats
1034
+ HeartbeatStorageImpl.prototype.delete = function (heartbeats) {
1035
+ return tslib.__awaiter(this, void 0, void 0, function () {
1036
+ var canUseIndexedDB, existingHeartbeats;
1037
+ return tslib.__generator(this, function (_a) {
1038
+ switch (_a.label) {
1039
+ case 0: return [4 /*yield*/, this._canUseIndexedDBPromise];
1040
+ case 1:
1041
+ canUseIndexedDB = _a.sent();
1042
+ if (!!canUseIndexedDB) return [3 /*break*/, 2];
1043
+ return [2 /*return*/];
1044
+ case 2: return [4 /*yield*/, this.read()];
1045
+ case 3:
1046
+ existingHeartbeats = _a.sent();
1047
+ return [2 /*return*/, writeHeartbeatsToIndexedDB(this.app, {
1048
+ heartbeats: existingHeartbeats.filter(function (existingHeartbeat) { return !heartbeats.includes(existingHeartbeat); })
1049
+ })];
1050
+ }
1051
+ });
1052
+ });
1053
+ };
1054
+ // delete all heartbeats
1055
+ HeartbeatStorageImpl.prototype.deleteAll = function () {
1056
+ return tslib.__awaiter(this, void 0, void 0, function () {
1057
+ var canUseIndexedDB;
1058
+ return tslib.__generator(this, function (_a) {
1059
+ switch (_a.label) {
1060
+ case 0: return [4 /*yield*/, this._canUseIndexedDBPromise];
1061
+ case 1:
1062
+ canUseIndexedDB = _a.sent();
1063
+ if (!canUseIndexedDB) {
1064
+ return [2 /*return*/];
1065
+ }
1066
+ else {
1067
+ return [2 /*return*/, deleteHeartbeatsFromIndexedDB(this.app)];
1068
+ }
1069
+ }
1070
+ });
1071
+ });
1072
+ };
1073
+ return HeartbeatStorageImpl;
1074
+ }());
1075
+ /**
1076
+ * Calculate bytes of a HeartbeatsByUserAgent array after being wrapped
1077
+ * in a platform logging header JSON object, stringified, and converted
1078
+ * to base 64.
1079
+ */
1080
+ function countBytes(heartbeatsCache) {
1081
+ // base64 has a restricted set of characters, all of which should be 1 byte.
1082
+ return util.base64Encode(
1083
+ // heartbeatsCache wrapper properties
1084
+ JSON.stringify({ version: 2, heartbeats: heartbeatsCache })).length;
1085
+ }
1086
+
628
1087
  /**
629
1088
  * @license
630
1089
  * Copyright 2019 Google LLC
@@ -643,6 +1102,7 @@ function setLogLevel(logLevel) {
643
1102
  */
644
1103
  function registerCoreComponents(variant) {
645
1104
  _registerComponent(new component.Component('platform-logger', function (container) { return new PlatformLoggerServiceImpl(container); }, "PRIVATE" /* PRIVATE */));
1105
+ _registerComponent(new component.Component('heartbeat', function (container) { return new HeartbeatServiceImpl(container); }, "PRIVATE" /* PRIVATE */));
646
1106
  // Register `app` package.
647
1107
  registerVersion(name$o, version$1, variant);
648
1108
  // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation