@firebase/app 0.7.18 → 0.7.19-canary.3198d58dc

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @firebase/app
2
2
 
3
+ ## 0.7.19
4
+
5
+ ### Patch Changes
6
+
7
+ - [`2d672cead`](https://github.com/firebase/firebase-js-sdk/commit/2d672cead167187cb714cd89b638c0884ba58f03) [#6061](https://github.com/firebase/firebase-js-sdk/pull/6061) - Remove idb dependency and replace with our own code.
8
+
9
+ * [`927c1afc1`](https://github.com/firebase/firebase-js-sdk/commit/927c1afc103e4f9b8a75320d3946a4c840445a2a) [#6039](https://github.com/firebase/firebase-js-sdk/pull/6039) - Fix heartbeat controller to ensure not sending more than one a day.
10
+
11
+ * Updated dependencies [[`2d672cead`](https://github.com/firebase/firebase-js-sdk/commit/2d672cead167187cb714cd89b638c0884ba58f03)]:
12
+ - @firebase/util@1.5.0
13
+ - @firebase/component@0.5.11
14
+
3
15
  ## 0.7.18
4
16
 
5
17
  ### Patch Changes
@@ -16,7 +16,7 @@
16
16
  */
17
17
  import { ComponentContainer } from '@firebase/component';
18
18
  import { FirebaseApp } from './public-types';
19
- import { HeartbeatsByUserAgent, HeartbeatService, HeartbeatStorage, SingleDateHeartbeat } from './types';
19
+ import { HeartbeatsByUserAgent, HeartbeatService, HeartbeatsInIndexedDB, HeartbeatStorage, SingleDateHeartbeat } from './types';
20
20
  export declare class HeartbeatServiceImpl implements HeartbeatService {
21
21
  private readonly container;
22
22
  /**
@@ -33,14 +33,14 @@ export declare class HeartbeatServiceImpl implements HeartbeatService {
33
33
  * be kept in sync with indexedDB.
34
34
  * Leave public for easier testing.
35
35
  */
36
- _heartbeatsCache: SingleDateHeartbeat[] | null;
36
+ _heartbeatsCache: HeartbeatsInIndexedDB | null;
37
37
  /**
38
38
  * the initialization promise for populating heartbeatCache.
39
39
  * If getHeartbeatsHeader() is called before the promise resolves
40
40
  * (hearbeatsCache == null), it should wait for this promise
41
41
  * Leave public for easier testing.
42
42
  */
43
- _heartbeatsCachePromise: Promise<SingleDateHeartbeat[]>;
43
+ _heartbeatsCachePromise: Promise<HeartbeatsInIndexedDB>;
44
44
  constructor(container: ComponentContainer);
45
45
  /**
46
46
  * Called to report a heartbeat. The function will generate
@@ -54,7 +54,8 @@ export declare class HeartbeatServiceImpl implements HeartbeatService {
54
54
  * Returns a base64 encoded string which can be attached to the heartbeat-specific header directly.
55
55
  * It also clears all heartbeats from memory as well as in IndexedDB.
56
56
  *
57
- * NOTE: It will read heartbeats from the heartbeatsCache, instead of from indexedDB to reduce latency
57
+ * NOTE: Consuming product SDKs should not send the header if this method
58
+ * returns an empty string.
58
59
  */
59
60
  getHeartbeatsHeader(): Promise<string>;
60
61
  }
@@ -70,11 +71,9 @@ export declare class HeartbeatStorageImpl implements HeartbeatStorage {
70
71
  /**
71
72
  * Read all heartbeats.
72
73
  */
73
- read(): Promise<SingleDateHeartbeat[]>;
74
- overwrite(heartbeats: SingleDateHeartbeat[]): Promise<void>;
75
- add(heartbeats: SingleDateHeartbeat[]): Promise<void>;
76
- delete(heartbeats: SingleDateHeartbeat[]): Promise<void>;
77
- deleteAll(): Promise<void>;
74
+ read(): Promise<HeartbeatsInIndexedDB>;
75
+ overwrite(heartbeatsObject: HeartbeatsInIndexedDB): Promise<void>;
76
+ add(heartbeatsObject: HeartbeatsInIndexedDB): Promise<void>;
78
77
  }
79
78
  /**
80
79
  * Calculate bytes of a HeartbeatsByUserAgent array after being wrapped
@@ -18,4 +18,3 @@ import { FirebaseApp } from './public-types';
18
18
  import { HeartbeatsInIndexedDB } from './types';
19
19
  export declare function readHeartbeatsFromIndexedDB(app: FirebaseApp): Promise<HeartbeatsInIndexedDB | undefined>;
20
20
  export declare function writeHeartbeatsToIndexedDB(app: FirebaseApp, heartbeatObject: HeartbeatsInIndexedDB): Promise<void>;
21
- export declare function deleteHeartbeatsFromIndexedDB(app: FirebaseApp): Promise<void>;
@@ -37,20 +37,19 @@ export interface HeartbeatService {
37
37
  getHeartbeatsHeader(): Promise<string>;
38
38
  }
39
39
  export interface HeartbeatsByUserAgent {
40
- userAgent: string;
40
+ agent: string;
41
41
  dates: string[];
42
42
  }
43
43
  export interface SingleDateHeartbeat {
44
- userAgent: string;
44
+ agent: string;
45
45
  date: string;
46
46
  }
47
47
  export interface HeartbeatStorage {
48
- overwrite(heartbeats: SingleDateHeartbeat[]): Promise<void>;
49
- add(heartbeats: SingleDateHeartbeat[]): Promise<void>;
50
- delete(heartbeats: SingleDateHeartbeat[]): Promise<void>;
51
- deleteAll(): Promise<void>;
52
- read(): Promise<SingleDateHeartbeat[]>;
48
+ overwrite(heartbeats: HeartbeatsInIndexedDB): Promise<void>;
49
+ add(heartbeats: HeartbeatsInIndexedDB): Promise<void>;
50
+ read(): Promise<HeartbeatsInIndexedDB>;
53
51
  }
54
52
  export interface HeartbeatsInIndexedDB {
53
+ lastSentHeartbeatDate?: string;
55
54
  heartbeats: SingleDateHeartbeat[];
56
55
  }
@@ -16,7 +16,7 @@
16
16
  */
17
17
  import { ComponentContainer } from '@firebase/component';
18
18
  import { FirebaseApp } from './public-types';
19
- import { HeartbeatsByUserAgent, HeartbeatService, HeartbeatStorage, SingleDateHeartbeat } from './types';
19
+ import { HeartbeatsByUserAgent, HeartbeatService, HeartbeatsInIndexedDB, HeartbeatStorage, SingleDateHeartbeat } from './types';
20
20
  export declare class HeartbeatServiceImpl implements HeartbeatService {
21
21
  private readonly container;
22
22
  /**
@@ -33,14 +33,14 @@ export declare class HeartbeatServiceImpl implements HeartbeatService {
33
33
  * be kept in sync with indexedDB.
34
34
  * Leave public for easier testing.
35
35
  */
36
- _heartbeatsCache: SingleDateHeartbeat[] | null;
36
+ _heartbeatsCache: HeartbeatsInIndexedDB | null;
37
37
  /**
38
38
  * the initialization promise for populating heartbeatCache.
39
39
  * If getHeartbeatsHeader() is called before the promise resolves
40
40
  * (hearbeatsCache == null), it should wait for this promise
41
41
  * Leave public for easier testing.
42
42
  */
43
- _heartbeatsCachePromise: Promise<SingleDateHeartbeat[]>;
43
+ _heartbeatsCachePromise: Promise<HeartbeatsInIndexedDB>;
44
44
  constructor(container: ComponentContainer);
45
45
  /**
46
46
  * Called to report a heartbeat. The function will generate
@@ -54,7 +54,8 @@ export declare class HeartbeatServiceImpl implements HeartbeatService {
54
54
  * Returns a base64 encoded string which can be attached to the heartbeat-specific header directly.
55
55
  * It also clears all heartbeats from memory as well as in IndexedDB.
56
56
  *
57
- * NOTE: It will read heartbeats from the heartbeatsCache, instead of from indexedDB to reduce latency
57
+ * NOTE: Consuming product SDKs should not send the header if this method
58
+ * returns an empty string.
58
59
  */
59
60
  getHeartbeatsHeader(): Promise<string>;
60
61
  }
@@ -70,11 +71,9 @@ export declare class HeartbeatStorageImpl implements HeartbeatStorage {
70
71
  /**
71
72
  * Read all heartbeats.
72
73
  */
73
- read(): Promise<SingleDateHeartbeat[]>;
74
- overwrite(heartbeats: SingleDateHeartbeat[]): Promise<void>;
75
- add(heartbeats: SingleDateHeartbeat[]): Promise<void>;
76
- delete(heartbeats: SingleDateHeartbeat[]): Promise<void>;
77
- deleteAll(): Promise<void>;
74
+ read(): Promise<HeartbeatsInIndexedDB>;
75
+ overwrite(heartbeatsObject: HeartbeatsInIndexedDB): Promise<void>;
76
+ add(heartbeatsObject: HeartbeatsInIndexedDB): Promise<void>;
78
77
  }
79
78
  /**
80
79
  * Calculate bytes of a HeartbeatsByUserAgent array after being wrapped
@@ -18,4 +18,3 @@ import { FirebaseApp } from './public-types';
18
18
  import { HeartbeatsInIndexedDB } from './types';
19
19
  export declare function readHeartbeatsFromIndexedDB(app: FirebaseApp): Promise<HeartbeatsInIndexedDB | undefined>;
20
20
  export declare function writeHeartbeatsToIndexedDB(app: FirebaseApp, heartbeatObject: HeartbeatsInIndexedDB): Promise<void>;
21
- export declare function deleteHeartbeatsFromIndexedDB(app: FirebaseApp): Promise<void>;
@@ -37,20 +37,19 @@ export interface HeartbeatService {
37
37
  getHeartbeatsHeader(): Promise<string>;
38
38
  }
39
39
  export interface HeartbeatsByUserAgent {
40
- userAgent: string;
40
+ agent: string;
41
41
  dates: string[];
42
42
  }
43
43
  export interface SingleDateHeartbeat {
44
- userAgent: string;
44
+ agent: string;
45
45
  date: string;
46
46
  }
47
47
  export interface HeartbeatStorage {
48
- overwrite(heartbeats: SingleDateHeartbeat[]): Promise<void>;
49
- add(heartbeats: SingleDateHeartbeat[]): Promise<void>;
50
- delete(heartbeats: SingleDateHeartbeat[]): Promise<void>;
51
- deleteAll(): Promise<void>;
52
- read(): Promise<SingleDateHeartbeat[]>;
48
+ overwrite(heartbeats: HeartbeatsInIndexedDB): Promise<void>;
49
+ add(heartbeats: HeartbeatsInIndexedDB): Promise<void>;
50
+ read(): Promise<HeartbeatsInIndexedDB>;
53
51
  }
54
52
  export interface HeartbeatsInIndexedDB {
53
+ lastSentHeartbeatDate?: string;
55
54
  heartbeats: SingleDateHeartbeat[];
56
55
  }
@@ -1,8 +1,7 @@
1
1
  import { Component, ComponentContainer } from '@firebase/component';
2
2
  import { Logger, setUserLogHandler, setLogLevel as setLogLevel$1 } from '@firebase/logger';
3
- import { ErrorFactory, deepEqual, base64Encode, isIndexedDBAvailable, validateIndexedDBOpenable } from '@firebase/util';
3
+ import { ErrorFactory, deepEqual, openDB, base64urlEncodeWithoutPadding, isIndexedDBAvailable, validateIndexedDBOpenable } from '@firebase/util';
4
4
  export { FirebaseError } from '@firebase/util';
5
- import { openDb } from 'idb';
6
5
 
7
6
  /**
8
7
  * @license
@@ -58,7 +57,7 @@ function isVersionServiceProvider(provider) {
58
57
  }
59
58
 
60
59
  const name$o = "@firebase/app";
61
- const version$1 = "0.7.18";
60
+ const version$1 = "0.7.19-canary.3198d58dc";
62
61
 
63
62
  /**
64
63
  * @license
@@ -125,7 +124,7 @@ const name$2 = "@firebase/firestore";
125
124
  const name$1 = "@firebase/firestore-compat";
126
125
 
127
126
  const name = "firebase";
128
- const version = "9.6.8";
127
+ const version = "9.6.9-canary.3198d58dc";
129
128
 
130
129
  /**
131
130
  * @license
@@ -589,15 +588,15 @@ const STORE_NAME = 'firebase-heartbeat-store';
589
588
  let dbPromise = null;
590
589
  function getDbPromise() {
591
590
  if (!dbPromise) {
592
- dbPromise = openDb(DB_NAME, DB_VERSION, upgradeDB => {
591
+ dbPromise = openDB(DB_NAME, DB_VERSION, (db, oldVersion) => {
593
592
  // We don't use 'break' in this switch statement, the fall-through
594
593
  // behavior is what we want, because if there are multiple versions between
595
594
  // the old version and the current version, we want ALL the migrations
596
595
  // that correspond to those versions to run, not only the last one.
597
596
  // eslint-disable-next-line default-case
598
- switch (upgradeDB.oldVersion) {
597
+ switch (oldVersion) {
599
598
  case 0:
600
- upgradeDB.createObjectStore(STORE_NAME);
599
+ db.createObjectStore(STORE_NAME);
601
600
  }
602
601
  }).catch(e => {
603
602
  throw ERROR_FACTORY.create("storage-open" /* STORAGE_OPEN */, {
@@ -635,19 +634,6 @@ async function writeHeartbeatsToIndexedDB(app, heartbeatObject) {
635
634
  });
636
635
  }
637
636
  }
638
- async function deleteHeartbeatsFromIndexedDB(app) {
639
- try {
640
- const db = await getDbPromise();
641
- const tx = db.transaction(STORE_NAME, 'readwrite');
642
- await tx.objectStore(STORE_NAME).delete(computeKey(app));
643
- return tx.complete;
644
- }
645
- catch (e) {
646
- throw ERROR_FACTORY.create("storage-delete" /* STORAGE_DELETE */, {
647
- originalErrorMessage: e.message
648
- });
649
- }
650
- }
651
637
  function computeKey(app) {
652
638
  return `${app.name}!${app.options.appId}`;
653
639
  }
@@ -704,21 +690,23 @@ class HeartbeatServiceImpl {
704
690
  .getImmediate();
705
691
  // This is the "Firebase user agent" string from the platform logger
706
692
  // service, not the browser user agent.
707
- const userAgent = platformLogger.getPlatformInfoString();
693
+ const agent = platformLogger.getPlatformInfoString();
708
694
  const date = getUTCDateString();
709
695
  if (this._heartbeatsCache === null) {
710
696
  this._heartbeatsCache = await this._heartbeatsCachePromise;
711
697
  }
712
- if (this._heartbeatsCache.some(singleDateHeartbeat => singleDateHeartbeat.date === date)) {
713
- // Do not store a heartbeat if one is already stored for this day.
698
+ // Do not store a heartbeat if one is already stored for this day
699
+ // or if a header has already been sent today.
700
+ if (this._heartbeatsCache.lastSentHeartbeatDate === date ||
701
+ this._heartbeatsCache.heartbeats.some(singleDateHeartbeat => singleDateHeartbeat.date === date)) {
714
702
  return;
715
703
  }
716
704
  else {
717
705
  // There is no entry for this date. Create one.
718
- this._heartbeatsCache.push({ date, userAgent });
706
+ this._heartbeatsCache.heartbeats.push({ date, agent });
719
707
  }
720
708
  // Remove entries older than 30 days.
721
- this._heartbeatsCache = this._heartbeatsCache.filter(singleDateHeartbeat => {
709
+ this._heartbeatsCache.heartbeats = this._heartbeatsCache.heartbeats.filter(singleDateHeartbeat => {
722
710
  const hbTimestamp = new Date(singleDateHeartbeat.date).valueOf();
723
711
  const now = Date.now();
724
712
  return now - hbTimestamp <= STORED_HEARTBEAT_RETENTION_MAX_MILLIS;
@@ -729,31 +717,36 @@ class HeartbeatServiceImpl {
729
717
  * Returns a base64 encoded string which can be attached to the heartbeat-specific header directly.
730
718
  * It also clears all heartbeats from memory as well as in IndexedDB.
731
719
  *
732
- * NOTE: It will read heartbeats from the heartbeatsCache, instead of from indexedDB to reduce latency
720
+ * NOTE: Consuming product SDKs should not send the header if this method
721
+ * returns an empty string.
733
722
  */
734
723
  async getHeartbeatsHeader() {
735
724
  if (this._heartbeatsCache === null) {
736
725
  await this._heartbeatsCachePromise;
737
726
  }
738
- // If it's still null, it's been cleared and has not been repopulated.
739
- if (this._heartbeatsCache === null) {
727
+ // If it's still null or the array is empty, there is no data to send.
728
+ if (this._heartbeatsCache === null ||
729
+ this._heartbeatsCache.heartbeats.length === 0) {
740
730
  return '';
741
731
  }
732
+ const date = getUTCDateString();
742
733
  // Extract as many heartbeats from the cache as will fit under the size limit.
743
- const { heartbeatsToSend, unsentEntries } = extractHeartbeatsForHeader(this._heartbeatsCache);
744
- const headerString = base64Encode(JSON.stringify({ version: 2, heartbeats: heartbeatsToSend }));
734
+ const { heartbeatsToSend, unsentEntries } = extractHeartbeatsForHeader(this._heartbeatsCache.heartbeats);
735
+ const headerString = base64urlEncodeWithoutPadding(JSON.stringify({ version: 2, heartbeats: heartbeatsToSend }));
736
+ // Store last sent date to prevent another being logged/sent for the same day.
737
+ this._heartbeatsCache.lastSentHeartbeatDate = date;
745
738
  if (unsentEntries.length > 0) {
746
739
  // Store any unsent entries if they exist.
747
- this._heartbeatsCache = unsentEntries;
748
- // This seems more likely than deleteAll (below) to lead to some odd state
740
+ this._heartbeatsCache.heartbeats = unsentEntries;
741
+ // This seems more likely than emptying the array (below) to lead to some odd state
749
742
  // since the cache isn't empty and this will be called again on the next request,
750
743
  // and is probably safest if we await it.
751
744
  await this._storage.overwrite(this._heartbeatsCache);
752
745
  }
753
746
  else {
754
- this._heartbeatsCache = null;
747
+ this._heartbeatsCache.heartbeats = [];
755
748
  // Do not wait for this, to reduce latency.
756
- void this._storage.deleteAll();
749
+ void this._storage.overwrite(this._heartbeatsCache);
757
750
  }
758
751
  return headerString;
759
752
  }
@@ -771,11 +764,11 @@ function extractHeartbeatsForHeader(heartbeatsCache, maxSize = MAX_HEADER_BYTES)
771
764
  let unsentEntries = heartbeatsCache.slice();
772
765
  for (const singleDateHeartbeat of heartbeatsCache) {
773
766
  // Look for an existing entry with the same user agent.
774
- const heartbeatEntry = heartbeatsToSend.find(hb => hb.userAgent === singleDateHeartbeat.userAgent);
767
+ const heartbeatEntry = heartbeatsToSend.find(hb => hb.agent === singleDateHeartbeat.agent);
775
768
  if (!heartbeatEntry) {
776
769
  // If no entry for this user agent exists, create one.
777
770
  heartbeatsToSend.push({
778
- userAgent: singleDateHeartbeat.userAgent,
771
+ agent: singleDateHeartbeat.agent,
779
772
  dates: [singleDateHeartbeat.date]
780
773
  });
781
774
  if (countBytes(heartbeatsToSend) > maxSize) {
@@ -824,59 +817,46 @@ class HeartbeatStorageImpl {
824
817
  async read() {
825
818
  const canUseIndexedDB = await this._canUseIndexedDBPromise;
826
819
  if (!canUseIndexedDB) {
827
- return [];
820
+ return { heartbeats: [] };
828
821
  }
829
822
  else {
830
823
  const idbHeartbeatObject = await readHeartbeatsFromIndexedDB(this.app);
831
- return (idbHeartbeatObject === null || idbHeartbeatObject === void 0 ? void 0 : idbHeartbeatObject.heartbeats) || [];
824
+ return idbHeartbeatObject || { heartbeats: [] };
832
825
  }
833
826
  }
834
827
  // overwrite the storage with the provided heartbeats
835
- async overwrite(heartbeats) {
836
- const canUseIndexedDB = await this._canUseIndexedDBPromise;
837
- if (!canUseIndexedDB) {
838
- return;
839
- }
840
- else {
841
- return writeHeartbeatsToIndexedDB(this.app, { heartbeats });
842
- }
843
- }
844
- // add heartbeats
845
- async add(heartbeats) {
828
+ async overwrite(heartbeatsObject) {
829
+ var _a;
846
830
  const canUseIndexedDB = await this._canUseIndexedDBPromise;
847
831
  if (!canUseIndexedDB) {
848
832
  return;
849
833
  }
850
834
  else {
851
- const existingHeartbeats = await this.read();
835
+ const existingHeartbeatsObject = await this.read();
852
836
  return writeHeartbeatsToIndexedDB(this.app, {
853
- heartbeats: [...existingHeartbeats, ...heartbeats]
837
+ lastSentHeartbeatDate: (_a = heartbeatsObject.lastSentHeartbeatDate) !== null && _a !== void 0 ? _a : existingHeartbeatsObject.lastSentHeartbeatDate,
838
+ heartbeats: heartbeatsObject.heartbeats
854
839
  });
855
840
  }
856
841
  }
857
- // delete heartbeats
858
- async delete(heartbeats) {
842
+ // add heartbeats
843
+ async add(heartbeatsObject) {
844
+ var _a;
859
845
  const canUseIndexedDB = await this._canUseIndexedDBPromise;
860
846
  if (!canUseIndexedDB) {
861
847
  return;
862
848
  }
863
849
  else {
864
- const existingHeartbeats = await this.read();
850
+ const existingHeartbeatsObject = await this.read();
865
851
  return writeHeartbeatsToIndexedDB(this.app, {
866
- heartbeats: existingHeartbeats.filter(existingHeartbeat => !heartbeats.includes(existingHeartbeat))
852
+ lastSentHeartbeatDate: (_a = heartbeatsObject.lastSentHeartbeatDate) !== null && _a !== void 0 ? _a : existingHeartbeatsObject.lastSentHeartbeatDate,
853
+ heartbeats: [
854
+ ...existingHeartbeatsObject.heartbeats,
855
+ ...heartbeatsObject.heartbeats
856
+ ]
867
857
  });
868
858
  }
869
859
  }
870
- // delete all heartbeats
871
- async deleteAll() {
872
- const canUseIndexedDB = await this._canUseIndexedDBPromise;
873
- if (!canUseIndexedDB) {
874
- return;
875
- }
876
- else {
877
- return deleteHeartbeatsFromIndexedDB(this.app);
878
- }
879
- }
880
860
  }
881
861
  /**
882
862
  * Calculate bytes of a HeartbeatsByUserAgent array after being wrapped
@@ -885,7 +865,7 @@ class HeartbeatStorageImpl {
885
865
  */
886
866
  function countBytes(heartbeatsCache) {
887
867
  // base64 has a restricted set of characters, all of which should be 1 byte.
888
- return base64Encode(
868
+ return base64urlEncodeWithoutPadding(
889
869
  // heartbeatsCache wrapper properties
890
870
  JSON.stringify({ version: 2, heartbeats: heartbeatsCache })).length;
891
871
  }