Skip to content

Commit

Permalink
Merge f053094 into 8d3aca7
Browse files Browse the repository at this point in the history
  • Loading branch information
XuechunHou authored Aug 20, 2020
2 parents 8d3aca7 + f053094 commit 696cd52
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .changeset/blue-rice-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"firebase": minor
"@firebase/remote-config": minor
---

Issue 2393 - Add environment check to Remote-Config Module
9 changes: 9 additions & 0 deletions packages/firebase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,15 @@ declare namespace firebase.remoteConfig {
* Defines levels of Remote Config logging.
*/
export type LogLevel = 'debug' | 'error' | 'silent';
/**
* Returns true if current browser context supports initialization of remote config module
* (`firebase.remoteConfig()`).
*
* Returns false otherwise.
*
*
*/
function isSupported(): Promise<boolean>;
}

declare namespace firebase.functions {
Expand Down
37 changes: 31 additions & 6 deletions packages/remote-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import { ERROR_FACTORY, ErrorCode } from './src/errors';
import { RetryingClient } from './src/client/retrying_client';
import { Logger, LogLevel as FirebaseLogLevel } from '@firebase/logger';
import { name as packageName, version } from './package.json';
import {
isIndexedDBAvailable,
validateIndexedDBOpenable
} from '@firebase/util';
import {
Component,
ComponentType,
Expand All @@ -48,11 +52,11 @@ export function registerRemoteConfig(
firebaseInstance: _FirebaseNamespace
): void {
firebaseInstance.INTERNAL.registerComponent(
new Component(
'remoteConfig',
remoteConfigFactory,
ComponentType.PUBLIC
).setMultipleInstances(true)
new Component('remoteConfig', remoteConfigFactory, ComponentType.PUBLIC)
.setMultipleInstances(true)
.setServiceProps({
isSupported
})
);

firebaseInstance.registerVersion(packageName, version);
Expand All @@ -71,7 +75,10 @@ export function registerRemoteConfig(
if (typeof window === 'undefined') {
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_WINDOW);
}

// Guards against the SDK being used when indexedDB is not available.
if (!isIndexedDBAvailable()) {
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNAVAILABLE);
}
// Normalizes optional inputs.
const { projectId, apiKey, appId } = app.options;
if (!projectId) {
Expand Down Expand Up @@ -139,3 +146,21 @@ declare module '@firebase/app-types' {
remoteConfig(): RemoteConfigType;
}
}
/**
* this is a public static method provided to users that wraps two different checks:
*
* 1. check if IndexedDB is supported by the browser environment.
* 2. check if the current browser context is valid for using IndexedDB.
*
*/
async function isSupported(): Promise<boolean> {
if (!isIndexedDBAvailable()) {
return false;
}

try {
return await validateIndexedDBOpenable();
} catch (error) {
return false;
}
}
7 changes: 5 additions & 2 deletions packages/remote-config/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const enum ErrorCode {
FETCH_TIMEOUT = 'fetch-timeout',
FETCH_THROTTLE = 'fetch-throttle',
FETCH_PARSE = 'fetch-client-parse',
FETCH_STATUS = 'fetch-status'
FETCH_STATUS = 'fetch-status',
INDEXED_DB_UNAVAILABLE = 'indexed-db-unavailable'
}

const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
Expand Down Expand Up @@ -64,7 +65,9 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
'Fetch client could not parse response.' +
' Original error: {$originalErrorMessage}.',
[ErrorCode.FETCH_STATUS]:
'Fetch server returned an HTTP error status. HTTP status: {$httpStatus}.'
'Fetch server returned an HTTP error status. HTTP status: {$httpStatus}.',
[ErrorCode.INDEXED_DB_UNAVAILABLE]:
'Indexed DB is not supported by current browser'
};

// Note this is effectively a type system binding a code to params. This approach overlaps with the
Expand Down
50 changes: 29 additions & 21 deletions packages/remote-config/src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,36 @@ type ProjectNamespaceKeyFieldValue =
// Visible for testing.
export function openDatabase(): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onerror = event => {
reject(toFirebaseError(event, ErrorCode.STORAGE_OPEN));
};
request.onsuccess = event => {
resolve((event.target as IDBOpenDBRequest).result);
};
request.onupgradeneeded = event => {
const db = (event.target as IDBOpenDBRequest).result;
try {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onerror = event => {
reject(toFirebaseError(event, ErrorCode.STORAGE_OPEN));
};
request.onsuccess = event => {
resolve((event.target as IDBOpenDBRequest).result);
};
request.onupgradeneeded = event => {
const db = (event.target as IDBOpenDBRequest).result;

// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (event.oldVersion) {
case 0:
db.createObjectStore(APP_NAMESPACE_STORE, {
keyPath: 'compositeKey'
});
}
};
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (event.oldVersion) {
case 0:
db.createObjectStore(APP_NAMESPACE_STORE, {
keyPath: 'compositeKey'
});
}
};
} catch (error) {
reject(
ERROR_FACTORY.create(ErrorCode.STORAGE_OPEN, {
originalErrorMessage: error
})
);
}
});
}

Expand Down

0 comments on commit 696cd52

Please sign in to comment.