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/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 new file mode 100644 index 00000000000..5507eeeebf7 --- /dev/null +++ b/.changeset/ninety-lions-report.md @@ -0,0 +1,8 @@ +--- +'@firebase/messaging': patch +'@firebase/analytics': patch +'@firebase/messaging-compat': patch +'@firebase/analytics-compat': patch +--- + +checking isSupported led to runtime errors in certain environments 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 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..f99f4625380 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,9 @@ export function isSupported(): boolean { */ function isWindowSupported(): boolean { return ( - 'indexedDB' in window && - indexedDB !== null && - navigator.cookieEnabled && + typeof window !== 'undefined' && + isIndexedDBAvailable() && + areCookiesEnabled() && 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window && @@ -79,8 +85,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 dca882fa44d..cbb02af7f32 100644 --- a/packages/messaging/src/api/isSupported.ts +++ b/packages/messaging/src/api/isSupported.ts @@ -15,7 +15,11 @@ * limitations under the License. */ -import { validateIndexedDBOpenable } from '@firebase/util'; +import { + areCookiesEnabled, + isIndexedDBAvailable, + validateIndexedDBOpenable +} from '@firebase/util'; /** * Checks if all required APIs exist in the browser. @@ -28,10 +32,10 @@ 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' && + isIndexedDBAvailable() && (await validateIndexedDBOpenable()) && - 'indexedDB' in window && - indexedDB !== null && - navigator.cookieEnabled && + areCookiesEnabled() && 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window && @@ -52,9 +56,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/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..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'; } /** @@ -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;