From 45924b2fef72c25477bbac96c95334c919ea237c Mon Sep 17 00:00:00 2001 From: James Daniels Date: Thu, 16 Sep 2021 17:44:49 -0400 Subject: [PATCH 1/7] isSupported fixes --- packages/messaging/src/api/isSupported.ts | 4 ++-- packages/performance/src/services/api_service.ts | 9 ++------- packages/util/src/environment.ts | 2 +- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/messaging/src/api/isSupported.ts b/packages/messaging/src/api/isSupported.ts index dca882fa44d..d6c8627ae06 100644 --- a/packages/messaging/src/api/isSupported.ts +++ b/packages/messaging/src/api/isSupported.ts @@ -28,9 +28,9 @@ export async function isWindowSupported(): Promise { // might be prohibited to run. In these contexts, an error would be thrown during the messaging // instantiating phase, informing the developers to import/call isSupported for special handling. return ( + typeof window !== 'undefined' && + typeof indexedDB !== 'undefined' && (await validateIndexedDBOpenable()) && - 'indexedDB' in window && - indexedDB !== null && navigator.cookieEnabled && 'serviceWorker' in navigator && 'PushManager' in window && diff --git a/packages/performance/src/services/api_service.ts b/packages/performance/src/services/api_service.ts index f500e415e87..a8579e51d1b 100644 --- a/packages/performance/src/services/api_service.ts +++ b/packages/performance/src/services/api_service.ts @@ -16,7 +16,7 @@ */ import { ERROR_FACTORY, ErrorCode } from '../utils/errors'; -import { isIndexedDBAvailable } from '@firebase/util'; +import { isIndexedDBAvailable, areCookiesEnabled } from '@firebase/util'; import { consoleLogger } from '../utils/console_logger'; declare global { @@ -112,12 +112,7 @@ export class Api { } requiredApisAvailable(): boolean { - if ( - !fetch || - !Promise || - !this.navigator || - !this.navigator.cookieEnabled - ) { + if (!fetch || !Promise || !areCookiesEnabled()) { consoleLogger.info( 'Firebase Performance cannot start if browser does not support fetch and Promise or cookie is disabled.' ); diff --git a/packages/util/src/environment.ts b/packages/util/src/environment.ts index f2ad5306583..f825a9b616f 100644 --- a/packages/util/src/environment.ts +++ b/packages/util/src/environment.ts @@ -184,7 +184,7 @@ export function validateIndexedDBOpenable(): Promise { * @return true if cookie is enabled within current browser */ export function areCookiesEnabled(): boolean { - if (!navigator || !navigator.cookieEnabled) { + if (typeof navigator === 'undefined' || !navigator.cookieEnabled) { return false; } return true; From 82ff6d9f88a572af45b800acdcb1a5f813f9d28a Mon Sep 17 00:00:00 2001 From: James Daniels Date: Thu, 16 Sep 2021 17:52:19 -0400 Subject: [PATCH 2/7] Changesets --- .changeset/ninety-lions-report.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/ninety-lions-report.md diff --git a/.changeset/ninety-lions-report.md b/.changeset/ninety-lions-report.md new file mode 100644 index 00000000000..e337ea3d7c2 --- /dev/null +++ b/.changeset/ninety-lions-report.md @@ -0,0 +1,7 @@ +--- +'@firebase/messaging': patch +'@firebase/performance': patch +'@firebase/analytics': patch +--- + +checking isSupported led to runtime failures in certain environments From 3a1e30d76fb7d8f4f9a47151010b66c909e7e209 Mon Sep 17 00:00:00 2001 From: James Daniels Date: Thu, 16 Sep 2021 17:55:30 -0400 Subject: [PATCH 3/7] firebase/util changelog --- .changeset/sixty-flowers-melt.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sixty-flowers-melt.md diff --git a/.changeset/sixty-flowers-melt.md b/.changeset/sixty-flowers-melt.md new file mode 100644 index 00000000000..42740e3868a --- /dev/null +++ b/.changeset/sixty-flowers-melt.md @@ -0,0 +1,5 @@ +--- +'@firebase/util': patch +--- + +areCookiesEnabled could encounter runtime errors in certain enviornments From a3ccc9b8f10a33056e8c7b90eda33508445c9c7a Mon Sep 17 00:00:00 2001 From: James Daniels Date: Thu, 16 Sep 2021 18:19:18 -0400 Subject: [PATCH 4/7] indexedDb check was bad on AppCheck, dry up --- packages/firestore/src/local/simple_db.ts | 4 ++-- .../firestore/test/integration/util/helpers.ts | 3 ++- .../messaging-compat/src/messaging-compat.ts | 16 ++++++++++------ packages/messaging/src/api/isSupported.ts | 10 ++++++---- packages/util/src/environment.ts | 2 +- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/firestore/src/local/simple_db.ts b/packages/firestore/src/local/simple_db.ts index 6a53cc174d8..f03ac114dd5 100644 --- a/packages/firestore/src/local/simple_db.ts +++ b/packages/firestore/src/local/simple_db.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { getUA } from '@firebase/util'; +import { getUA, isIndexedDBAvailable } from '@firebase/util'; import { debugAssert } from '../util/assert'; import { Code, FirestoreError } from '../util/error'; @@ -158,7 +158,7 @@ export class SimpleDb { /** Returns true if IndexedDB is available in the current environment. */ static isAvailable(): boolean { - if (typeof indexedDB === 'undefined') { + if (!isIndexedDBAvailable()) { return false; } diff --git a/packages/firestore/test/integration/util/helpers.ts b/packages/firestore/test/integration/util/helpers.ts index 41969787f75..72455a2a82e 100644 --- a/packages/firestore/test/integration/util/helpers.ts +++ b/packages/firestore/test/integration/util/helpers.ts @@ -16,6 +16,7 @@ */ import * as firestore from '@firebase/firestore-types'; +import { isIndexedDBAvailable } from '@firebase/util'; import * as firebaseExport from './firebase_export'; import { @@ -44,7 +45,7 @@ function isIeOrEdge(): boolean { export function isPersistenceAvailable(): boolean { return ( typeof window === 'object' && - typeof window.indexedDB === 'object' && + isIndexedDBAvailable() && !isIeOrEdge() && (typeof process === 'undefined' || process.env?.INCLUDE_FIRESTORE_PERSISTENCE !== 'false') diff --git a/packages/messaging-compat/src/messaging-compat.ts b/packages/messaging-compat/src/messaging-compat.ts index 2910d61b341..cb3062b3b74 100644 --- a/packages/messaging-compat/src/messaging-compat.ts +++ b/packages/messaging-compat/src/messaging-compat.ts @@ -26,7 +26,13 @@ import { getToken, onMessage } from '@firebase/messaging'; -import { NextFn, Observer, Unsubscribe } from '@firebase/util'; +import { + areCookiesEnabled, + isIndexedDBAvailable, + NextFn, + Observer, + Unsubscribe +} from '@firebase/util'; import { onBackgroundMessage } from '@firebase/messaging/sw'; @@ -62,9 +68,8 @@ export function isSupported(): boolean { */ function isWindowSupported(): boolean { return ( - 'indexedDB' in window && - indexedDB !== null && - navigator.cookieEnabled && + isIndexedDBAvailable() && + areCookiesEnabled() && 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window && @@ -79,8 +84,7 @@ function isWindowSupported(): boolean { */ function isSwSupported(): boolean { return ( - 'indexedDB' in self && - indexedDB !== null && + isIndexedDBAvailable() && 'PushManager' in self && 'Notification' in self && ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') && diff --git a/packages/messaging/src/api/isSupported.ts b/packages/messaging/src/api/isSupported.ts index d6c8627ae06..afa441955d2 100644 --- a/packages/messaging/src/api/isSupported.ts +++ b/packages/messaging/src/api/isSupported.ts @@ -15,7 +15,10 @@ * limitations under the License. */ -import { validateIndexedDBOpenable } from '@firebase/util'; +import { + isIndexedDBAvailable, + validateIndexedDBOpenable +} from '@firebase/util'; /** * Checks if all required APIs exist in the browser. @@ -29,7 +32,7 @@ export async function isWindowSupported(): Promise { // instantiating phase, informing the developers to import/call isSupported for special handling. return ( typeof window !== 'undefined' && - typeof indexedDB !== 'undefined' && + isIndexedDBAvailable() && (await validateIndexedDBOpenable()) && navigator.cookieEnabled && 'serviceWorker' in navigator && @@ -52,9 +55,8 @@ export async function isSwSupported(): Promise { // might be prohibited to run. In these contexts, an error would be thrown during the messaging // instantiating phase, informing the developers to import/call isSupported for special handling. return ( + isIndexedDBAvailable() && (await validateIndexedDBOpenable()) && - 'indexedDB' in self && - indexedDB !== null && 'PushManager' in self && 'Notification' in self && ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') && diff --git a/packages/util/src/environment.ts b/packages/util/src/environment.ts index f825a9b616f..84fed3ca099 100644 --- a/packages/util/src/environment.ts +++ b/packages/util/src/environment.ts @@ -140,7 +140,7 @@ export function isSafari(): boolean { * @return true if indexedDB is supported by current browser/service worker context */ export function isIndexedDBAvailable(): boolean { - return 'indexedDB' in self && indexedDB != null; + return typeof indexedDB === 'object'; } /** From 4ccfc346fd968adde6c9b1b4178f074a3903dcac Mon Sep 17 00:00:00 2001 From: James Daniels Date: Thu, 16 Sep 2021 18:22:46 -0400 Subject: [PATCH 5/7] Changelog, fix in compat too --- .changeset/angry-moons-change.md | 6 ++++++ .changeset/ninety-lions-report.md | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 .changeset/angry-moons-change.md diff --git a/.changeset/angry-moons-change.md b/.changeset/angry-moons-change.md new file mode 100644 index 00000000000..1ab60829ca1 --- /dev/null +++ b/.changeset/angry-moons-change.md @@ -0,0 +1,6 @@ +--- +'@firebase/app-check': patch +'@firebase/app-check-compat': patch +--- + +AppCheck could encounter runtime errors when initialized in Node diff --git a/.changeset/ninety-lions-report.md b/.changeset/ninety-lions-report.md index e337ea3d7c2..b3496f08dfa 100644 --- a/.changeset/ninety-lions-report.md +++ b/.changeset/ninety-lions-report.md @@ -2,6 +2,9 @@ '@firebase/messaging': patch '@firebase/performance': patch '@firebase/analytics': patch +'@firebase/messaging-compat': patch +'@firebase/performance-compat': patch +'@firebase/analytics-compat': patch --- checking isSupported led to runtime failures in certain environments From ca62dc628cca2560dcdeb914dce1128fac6269fb Mon Sep 17 00:00:00 2001 From: James Daniels Date: Thu, 16 Sep 2021 18:26:52 -0400 Subject: [PATCH 6/7] Check for window on messaging-compat --- packages/messaging-compat/src/messaging-compat.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/messaging-compat/src/messaging-compat.ts b/packages/messaging-compat/src/messaging-compat.ts index cb3062b3b74..f99f4625380 100644 --- a/packages/messaging-compat/src/messaging-compat.ts +++ b/packages/messaging-compat/src/messaging-compat.ts @@ -68,6 +68,7 @@ export function isSupported(): boolean { */ function isWindowSupported(): boolean { return ( + typeof window !== 'undefined' && isIndexedDBAvailable() && areCookiesEnabled() && 'serviceWorker' in navigator && From be4c4432d8fb9db59121ec641afbe9cf534a2f8d Mon Sep 17 00:00:00 2001 From: James Daniels Date: Thu, 16 Sep 2021 20:58:30 -0400 Subject: [PATCH 7/7] Missed a messaging cookie use, split changelog a bit --- .changeset/lazy-bulldogs-tickle.md | 6 ++++++ .changeset/ninety-lions-report.md | 4 +--- packages/messaging/src/api/isSupported.ts | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 .changeset/lazy-bulldogs-tickle.md diff --git a/.changeset/lazy-bulldogs-tickle.md b/.changeset/lazy-bulldogs-tickle.md new file mode 100644 index 00000000000..7d3b9147050 --- /dev/null +++ b/.changeset/lazy-bulldogs-tickle.md @@ -0,0 +1,6 @@ +--- +'@firebase/performance': patch +'@firebase/performance-compat': patch +--- + +Performance could encounter runtime errors when initialized in certain environments diff --git a/.changeset/ninety-lions-report.md b/.changeset/ninety-lions-report.md index b3496f08dfa..5507eeeebf7 100644 --- a/.changeset/ninety-lions-report.md +++ b/.changeset/ninety-lions-report.md @@ -1,10 +1,8 @@ --- '@firebase/messaging': patch -'@firebase/performance': patch '@firebase/analytics': patch '@firebase/messaging-compat': patch -'@firebase/performance-compat': patch '@firebase/analytics-compat': patch --- -checking isSupported led to runtime failures in certain environments +checking isSupported led to runtime errors in certain environments diff --git a/packages/messaging/src/api/isSupported.ts b/packages/messaging/src/api/isSupported.ts index afa441955d2..cbb02af7f32 100644 --- a/packages/messaging/src/api/isSupported.ts +++ b/packages/messaging/src/api/isSupported.ts @@ -16,6 +16,7 @@ */ import { + areCookiesEnabled, isIndexedDBAvailable, validateIndexedDBOpenable } from '@firebase/util'; @@ -34,7 +35,7 @@ export async function isWindowSupported(): Promise { typeof window !== 'undefined' && isIndexedDBAvailable() && (await validateIndexedDBOpenable()) && - navigator.cookieEnabled && + areCookiesEnabled() && 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window &&