Skip to content

Commit

Permalink
isSupported fixes (#5506)
Browse files Browse the repository at this point in the history
* isSupported fixes

* Changesets

* firebase/util changelog

* indexedDb check was bad on AppCheck, dry up

* Changelog, fix in compat too

* Check for window on messaging-compat

* Missed a messaging cookie use, split changelog a bit
  • Loading branch information
jamesdaniels committed Sep 22, 2021
1 parent 4d2a54f commit b835b4c
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .changeset/angry-moons-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/app-check': patch
'@firebase/app-check-compat': patch
---

AppCheck could encounter runtime errors when initialized in Node
6 changes: 6 additions & 0 deletions .changeset/lazy-bulldogs-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/performance': patch
'@firebase/performance-compat': patch
---

Performance could encounter runtime errors when initialized in certain environments
8 changes: 8 additions & 0 deletions .changeset/ninety-lions-report.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions .changeset/sixty-flowers-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/util': patch
---

areCookiesEnabled could encounter runtime errors in certain enviornments
4 changes: 2 additions & 2 deletions packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import * as firestore from '@firebase/firestore-types';
import { isIndexedDBAvailable } from '@firebase/util';

import * as firebaseExport from './firebase_export';
import {
Expand Down Expand Up @@ -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')
Expand Down
17 changes: 11 additions & 6 deletions packages/messaging-compat/src/messaging-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 &&
Expand All @@ -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') &&
Expand Down
15 changes: 9 additions & 6 deletions packages/messaging/src/api/isSupported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -28,10 +32,10 @@ export async function isWindowSupported(): Promise<boolean> {
// 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 &&
Expand All @@ -52,9 +56,8 @@ export async function isSwSupported(): Promise<boolean> {
// 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') &&
Expand Down
9 changes: 2 additions & 7 deletions packages/performance/src/services/api_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.'
);
Expand Down
4 changes: 2 additions & 2 deletions packages/util/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ export function validateIndexedDBOpenable(): Promise<boolean> {
* @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;
Expand Down

0 comments on commit b835b4c

Please sign in to comment.