From 82d7df4395d6bd5f569bfc4b3f8f394b0274f905 Mon Sep 17 00:00:00 2001 From: Maneesh Tewani Date: Wed, 12 Jul 2023 08:34:30 -0700 Subject: [PATCH] Included initStandalone (#7258) Added private `initStandalone` API into modular SDK --- .changeset/pretty-donkeys-brush.md | 6 ++ packages/database-compat/src/api/internal.ts | 27 +++++- packages/database/src/api.standalone.ts | 1 + packages/database/src/internal/index.ts | 99 ++++++++++++++++++++ 4 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 .changeset/pretty-donkeys-brush.md create mode 100644 packages/database/src/internal/index.ts diff --git a/.changeset/pretty-donkeys-brush.md b/.changeset/pretty-donkeys-brush.md new file mode 100644 index 00000000000..eebaca395f0 --- /dev/null +++ b/.changeset/pretty-donkeys-brush.md @@ -0,0 +1,6 @@ +--- +"@firebase/database-compat": patch +"@firebase/database": patch +--- + +Included `initStandalone` as an internal method to RTDB. diff --git a/packages/database-compat/src/api/internal.ts b/packages/database-compat/src/api/internal.ts index 6a8defcef7e..5e53c3cf762 100644 --- a/packages/database-compat/src/api/internal.ts +++ b/packages/database-compat/src/api/internal.ts @@ -14,7 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +import { + AppCheckInternalComponentName, + FirebaseAppCheckInternal +} from '@firebase/app-check-interop-types'; import { FirebaseApp } from '@firebase/app-types'; import { FirebaseAuthInternal, @@ -48,6 +51,7 @@ export function initStandalone({ url, version, customAuthImpl, + customAppCheckImpl, namespace, nodeAdmin = false }: { @@ -55,6 +59,7 @@ export function initStandalone({ url: string; version: string; customAuthImpl: FirebaseAuthInternal; + customAppCheckImpl?: FirebaseAppCheckInternal; namespace: T; nodeAdmin?: boolean; }): { @@ -63,24 +68,40 @@ export function initStandalone({ } { _setSDKVersion(version); + const container = new ComponentContainer('database-standalone'); /** * ComponentContainer('database-standalone') is just a placeholder that doesn't perform * any actual function. */ const authProvider = new Provider( 'auth-internal', - new ComponentContainer('database-standalone') + container ); authProvider.setComponent( new Component('auth-internal', () => customAuthImpl, ComponentType.PRIVATE) ); + let appCheckProvider: Provider = undefined; + if (customAppCheckImpl) { + appCheckProvider = new Provider( + 'app-check-internal', + container + ); + appCheckProvider.setComponent( + new Component( + 'app-check-internal', + () => customAppCheckImpl, + ComponentType.PRIVATE + ) + ); + } + return { instance: new Database( _repoManagerDatabaseFromApp( app, authProvider, - /* appCheckProvider= */ undefined, + appCheckProvider, url, nodeAdmin ), diff --git a/packages/database/src/api.standalone.ts b/packages/database/src/api.standalone.ts index c7726143f16..20b06e0c903 100644 --- a/packages/database/src/api.standalone.ts +++ b/packages/database/src/api.standalone.ts @@ -96,4 +96,5 @@ export { hijackHash as _TEST_ACCESS_hijackHash, forceRestClient as _TEST_ACCESS_forceRestClient } from './api/test_access'; +export * from './internal/index'; /* eslint-enable camelcase */ diff --git a/packages/database/src/internal/index.ts b/packages/database/src/internal/index.ts new file mode 100644 index 00000000000..99fa4e1369f --- /dev/null +++ b/packages/database/src/internal/index.ts @@ -0,0 +1,99 @@ +/** + * @license + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + FirebaseAppCheckInternal, + AppCheckInternalComponentName +} from '@firebase/app-check-interop-types'; +import { FirebaseApp } from '@firebase/app-types'; +import { + FirebaseAuthInternal, + FirebaseAuthInternalName +} from '@firebase/auth-interop-types'; +import { + Component, + ComponentContainer, + ComponentType, + Provider +} from '@firebase/component'; + +import { Database } from '../api.standalone'; +import { repoManagerDatabaseFromApp } from '../api/Database'; +import { setSDKVersion } from '../core/version'; + +/** + * Used by console to create a database based on the app, + * passed database URL and a custom auth implementation. + * @internal + * @param app - A valid FirebaseApp-like object + * @param url - A valid Firebase databaseURL + * @param version - custom version e.g. firebase-admin version + * @param customAppCheckImpl - custom app check implementation + * @param customAuthImpl - custom auth implementation + */ +export function _initStandalone({ + app, + url, + version, + customAuthImpl, + customAppCheckImpl, + nodeAdmin = false +}: { + app: FirebaseApp; + url: string; + version: string; + customAuthImpl: FirebaseAuthInternal; + customAppCheckImpl?: FirebaseAppCheckInternal; + nodeAdmin?: boolean; +}): Database { + setSDKVersion(version); + + /** + * ComponentContainer('database-standalone') is just a placeholder that doesn't perform + * any actual function. + */ + const componentContainer = new ComponentContainer('database-standalone'); + const authProvider = new Provider( + 'auth-internal', + componentContainer + ); + let appCheckProvider: Provider; + if (customAppCheckImpl) { + appCheckProvider = new Provider( + 'app-check-internal', + componentContainer + ); + appCheckProvider.setComponent( + new Component( + 'app-check-internal', + () => customAppCheckImpl, + ComponentType.PRIVATE + ) + ); + } + authProvider.setComponent( + new Component('auth-internal', () => customAuthImpl, ComponentType.PRIVATE) + ); + + return repoManagerDatabaseFromApp( + app, + authProvider, + appCheckProvider, + url, + nodeAdmin + ); +}