|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types'; |
| 19 | +import { _FirebaseNamespace } from '@firebase/app-types/private'; |
| 20 | +import { FirebaseAuthInternal } from '@firebase/auth-interop-types'; |
| 21 | +import { Component, ComponentType } from '@firebase/component'; |
| 22 | +import * as types from '@firebase/database-types'; |
| 23 | +import { CONSTANTS, isNodeSdk } from '@firebase/util'; |
| 24 | +import { Client } from 'faye-websocket'; |
| 25 | + |
| 26 | +import { enableLogging } from '../exp/index'; |
| 27 | +import { Database } from '../src/api/Database'; |
| 28 | +import * as INTERNAL from '../src/api/internal'; |
| 29 | +import { DataSnapshot, Query, Reference } from '../src/api/Reference'; |
| 30 | +import * as TEST_ACCESS from '../src/api/test_access'; |
| 31 | +import { setSDKVersion } from '../src/core/version'; |
| 32 | +import { setWebSocketImpl } from '../src/realtime/WebSocketConnection'; |
| 33 | + |
| 34 | +import { name, version } from './package.json'; |
| 35 | + |
| 36 | +setWebSocketImpl(Client); |
| 37 | + |
| 38 | +const ServerValue = Database.ServerValue; |
| 39 | + |
| 40 | +/** |
| 41 | + * A one off register function which returns a database based on the app and |
| 42 | + * passed database URL. (Used by the Admin SDK) |
| 43 | + * |
| 44 | + * @param app - A valid FirebaseApp-like object |
| 45 | + * @param url - A valid Firebase databaseURL |
| 46 | + * @param version - custom version e.g. firebase-admin version |
| 47 | + * @param nodeAdmin - true if the SDK is being initialized from Firebase Admin. |
| 48 | + */ |
| 49 | +export function initStandalone( |
| 50 | + app: FirebaseApp, |
| 51 | + url: string, |
| 52 | + version: string, |
| 53 | + nodeAdmin = true |
| 54 | +) { |
| 55 | + CONSTANTS.NODE_ADMIN = nodeAdmin; |
| 56 | + return INTERNAL.initStandalone({ |
| 57 | + app, |
| 58 | + url, |
| 59 | + version, |
| 60 | + // firebase-admin-node's app.INTERNAL implements FirebaseAuthInternal interface |
| 61 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 62 | + customAuthImpl: (app as any).INTERNAL as FirebaseAuthInternal, |
| 63 | + namespace: { |
| 64 | + Reference, |
| 65 | + Query, |
| 66 | + Database, |
| 67 | + DataSnapshot, |
| 68 | + enableLogging, |
| 69 | + INTERNAL, |
| 70 | + ServerValue, |
| 71 | + TEST_ACCESS |
| 72 | + }, |
| 73 | + nodeAdmin |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +export function registerDatabase(instance: FirebaseNamespace) { |
| 78 | + // set SDK_VERSION |
| 79 | + setSDKVersion(instance.SDK_VERSION); |
| 80 | + |
| 81 | + // Register the Database Service with the 'firebase' namespace. |
| 82 | + const namespace = (instance as _FirebaseNamespace).INTERNAL.registerComponent( |
| 83 | + new Component( |
| 84 | + 'database-compat', |
| 85 | + (container, { instanceIdentifier: url }) => { |
| 86 | + /* Dependencies */ |
| 87 | + // getImmediate for FirebaseApp will always succeed |
| 88 | + const app = container.getProvider('app-compat').getImmediate(); |
| 89 | + const databaseExp = container |
| 90 | + .getProvider('database-exp') |
| 91 | + .getImmediate({ identifier: url }); |
| 92 | + return new Database(databaseExp, app); |
| 93 | + }, |
| 94 | + ComponentType.PUBLIC |
| 95 | + ) |
| 96 | + .setServiceProps( |
| 97 | + // firebase.database namespace properties |
| 98 | + { |
| 99 | + Reference, |
| 100 | + Query, |
| 101 | + Database, |
| 102 | + DataSnapshot, |
| 103 | + enableLogging, |
| 104 | + INTERNAL, |
| 105 | + ServerValue, |
| 106 | + TEST_ACCESS |
| 107 | + } |
| 108 | + ) |
| 109 | + .setMultipleInstances(true) |
| 110 | + ); |
| 111 | + |
| 112 | + instance.registerVersion(name, version, 'node'); |
| 113 | + |
| 114 | + if (isNodeSdk()) { |
| 115 | + module.exports = Object.assign({}, namespace, { initStandalone }); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +try { |
| 120 | + // If @firebase/app is not present, skip registering database. |
| 121 | + // It could happen when this package is used in firebase-admin which doesn't depend on @firebase/app. |
| 122 | + // Previously firebase-admin depends on @firebase/app, which causes version conflict on |
| 123 | + // @firebase/app when used together with the js sdk. More detail: |
| 124 | + // https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596 |
| 125 | + // eslint-disable-next-line import/no-extraneous-dependencies, @typescript-eslint/no-require-imports |
| 126 | + const firebase = require('@firebase/app-compat').default; |
| 127 | + registerDatabase(firebase); |
| 128 | +} catch (err) { |
| 129 | + // catch and ignore 'MODULE_NOT_FOUND' error in firebase-admin context |
| 130 | + // we can safely ignore this error because RTDB in firebase-admin works without @firebase/app |
| 131 | + if (err.code !== 'MODULE_NOT_FOUND') { |
| 132 | + throw err; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// Types to export for the admin SDK |
| 137 | +export { Database, Query, Reference, enableLogging, ServerValue }; |
| 138 | + |
| 139 | +export { OnDisconnect } from '../src/api/onDisconnect'; |
| 140 | + |
| 141 | +declare module '@firebase/app-compat' { |
| 142 | + interface FirebaseNamespace { |
| 143 | + database?: { |
| 144 | + (app?: FirebaseApp): types.FirebaseDatabase; |
| 145 | + enableLogging: typeof types.enableLogging; |
| 146 | + ServerValue: types.ServerValue; |
| 147 | + Database: typeof types.FirebaseDatabase; |
| 148 | + }; |
| 149 | + } |
| 150 | + interface FirebaseApp { |
| 151 | + database?(): types.FirebaseDatabase; |
| 152 | + } |
| 153 | +} |
| 154 | +export { DataSnapshot } from '../src/api/Reference'; |
0 commit comments