firebase-namespace.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * @license
  5. * Copyright 2017 Google Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. exports.defaultNamespace = exports.FirebaseNamespace = exports.FirebaseNamespaceInternals = void 0;
  21. const lifecycle_1 = require("./lifecycle");
  22. const credential_factory_1 = require("./credential-factory");
  23. const index_1 = require("../utils/index");
  24. /**
  25. * Internals of a FirebaseNamespace instance.
  26. */
  27. class FirebaseNamespaceInternals {
  28. constructor(appStore) {
  29. this.appStore = appStore;
  30. }
  31. /**
  32. * Initializes the App instance.
  33. *
  34. * @param options - Optional options for the App instance. If none present will try to initialize
  35. * from the FIREBASE_CONFIG environment variable. If the environment variable contains a string
  36. * that starts with '{' it will be parsed as JSON, otherwise it will be assumed to be pointing
  37. * to a file.
  38. * @param appName - Optional name of the FirebaseApp instance.
  39. *
  40. * @returns A new App instance.
  41. */
  42. initializeApp(options, appName) {
  43. const app = this.appStore.initializeApp(options, appName);
  44. return extendApp(app);
  45. }
  46. /**
  47. * Returns the App instance with the provided name (or the default App instance
  48. * if no name is provided).
  49. *
  50. * @param appName - Optional name of the FirebaseApp instance to return.
  51. * @returns The App instance which has the provided name.
  52. */
  53. app(appName) {
  54. const app = this.appStore.getApp(appName);
  55. return extendApp(app);
  56. }
  57. /*
  58. * Returns an array of all the non-deleted App instances.
  59. */
  60. get apps() {
  61. return this.appStore.getApps().map((app) => extendApp(app));
  62. }
  63. }
  64. exports.FirebaseNamespaceInternals = FirebaseNamespaceInternals;
  65. const firebaseCredential = {
  66. cert: credential_factory_1.cert, refreshToken: credential_factory_1.refreshToken, applicationDefault: credential_factory_1.applicationDefault
  67. };
  68. /**
  69. * Global Firebase context object.
  70. */
  71. class FirebaseNamespace {
  72. /* tslint:enable */
  73. constructor(appStore) {
  74. // Hack to prevent Babel from modifying the object returned as the default admin namespace.
  75. /* tslint:disable:variable-name */
  76. this.__esModule = true;
  77. /* tslint:enable:variable-name */
  78. this.credential = firebaseCredential;
  79. this.SDK_VERSION = (0, index_1.getSdkVersion)();
  80. /* tslint:disable */
  81. // TODO(jwenger): Database is the only consumer of firebase.Promise. We should update it to use
  82. // use the native Promise and then remove this.
  83. this.Promise = Promise;
  84. this.INTERNAL = new FirebaseNamespaceInternals(appStore ?? new lifecycle_1.AppStore());
  85. }
  86. /**
  87. * Gets the `Auth` service namespace. The returned namespace can be used to get the
  88. * `Auth` service for the default app or an explicitly specified app.
  89. */
  90. get auth() {
  91. const fn = (app) => {
  92. return this.ensureApp(app).auth();
  93. };
  94. const auth = require('../auth/auth').Auth;
  95. return Object.assign(fn, { Auth: auth });
  96. }
  97. /**
  98. * Gets the `Database` service namespace. The returned namespace can be used to get the
  99. * `Database` service for the default app or an explicitly specified app.
  100. */
  101. get database() {
  102. const fn = (app) => {
  103. return this.ensureApp(app).database();
  104. };
  105. // eslint-disable-next-line @typescript-eslint/no-var-requires
  106. return Object.assign(fn, require('@firebase/database-compat/standalone'));
  107. }
  108. /**
  109. * Gets the `Messaging` service namespace. The returned namespace can be used to get the
  110. * `Messaging` service for the default app or an explicitly specified app.
  111. */
  112. get messaging() {
  113. const fn = (app) => {
  114. return this.ensureApp(app).messaging();
  115. };
  116. const messaging = require('../messaging/messaging').Messaging;
  117. return Object.assign(fn, { Messaging: messaging });
  118. }
  119. /**
  120. * Gets the `Storage` service namespace. The returned namespace can be used to get the
  121. * `Storage` service for the default app or an explicitly specified app.
  122. */
  123. get storage() {
  124. const fn = (app) => {
  125. return this.ensureApp(app).storage();
  126. };
  127. const storage = require('../storage/storage').Storage;
  128. return Object.assign(fn, { Storage: storage });
  129. }
  130. /**
  131. * Gets the `Firestore` service namespace. The returned namespace can be used to get the
  132. * `Firestore` service for the default app or an explicitly specified app.
  133. */
  134. get firestore() {
  135. let fn = (app) => {
  136. return this.ensureApp(app).firestore();
  137. };
  138. // eslint-disable-next-line @typescript-eslint/no-var-requires
  139. const firestore = require('@google-cloud/firestore');
  140. fn = Object.assign(fn, firestore.Firestore);
  141. // `v1beta1` and `v1` are lazy-loaded in the Firestore SDK. We use the same trick here
  142. // to avoid triggering this lazy-loading upon initialization.
  143. Object.defineProperty(fn, 'v1beta1', {
  144. get: () => {
  145. return firestore.v1beta1;
  146. },
  147. });
  148. Object.defineProperty(fn, 'v1', {
  149. get: () => {
  150. return firestore.v1;
  151. },
  152. });
  153. return fn;
  154. }
  155. /**
  156. * Gets the `MachineLearning` service namespace. The returned namespace can be
  157. * used to get the `MachineLearning` service for the default app or an
  158. * explicityly specified app.
  159. */
  160. get machineLearning() {
  161. const fn = (app) => {
  162. return this.ensureApp(app).machineLearning();
  163. };
  164. const machineLearning = require('../machine-learning/machine-learning').MachineLearning;
  165. return Object.assign(fn, { MachineLearning: machineLearning });
  166. }
  167. /**
  168. * Gets the `Installations` service namespace. The returned namespace can be used to get the
  169. * `Installations` service for the default app or an explicitly specified app.
  170. */
  171. get installations() {
  172. const fn = (app) => {
  173. return this.ensureApp(app).installations();
  174. };
  175. const installations = require('../installations/installations').Installations;
  176. return Object.assign(fn, { Installations: installations });
  177. }
  178. /**
  179. * Gets the `InstanceId` service namespace. The returned namespace can be used to get the
  180. * `Instance` service for the default app or an explicitly specified app.
  181. */
  182. get instanceId() {
  183. const fn = (app) => {
  184. return this.ensureApp(app).instanceId();
  185. };
  186. const instanceId = require('../instance-id/instance-id').InstanceId;
  187. return Object.assign(fn, { InstanceId: instanceId });
  188. }
  189. /**
  190. * Gets the `ProjectManagement` service namespace. The returned namespace can be used to get the
  191. * `ProjectManagement` service for the default app or an explicitly specified app.
  192. */
  193. get projectManagement() {
  194. const fn = (app) => {
  195. return this.ensureApp(app).projectManagement();
  196. };
  197. const projectManagement = require('../project-management/project-management').ProjectManagement;
  198. return Object.assign(fn, { ProjectManagement: projectManagement });
  199. }
  200. /**
  201. * Gets the `SecurityRules` service namespace. The returned namespace can be used to get the
  202. * `SecurityRules` service for the default app or an explicitly specified app.
  203. */
  204. get securityRules() {
  205. const fn = (app) => {
  206. return this.ensureApp(app).securityRules();
  207. };
  208. const securityRules = require('../security-rules/security-rules').SecurityRules;
  209. return Object.assign(fn, { SecurityRules: securityRules });
  210. }
  211. /**
  212. * Gets the `RemoteConfig` service namespace. The returned namespace can be used to get the
  213. * `RemoteConfig` service for the default app or an explicitly specified app.
  214. */
  215. get remoteConfig() {
  216. const fn = (app) => {
  217. return this.ensureApp(app).remoteConfig();
  218. };
  219. const remoteConfig = require('../remote-config/remote-config').RemoteConfig;
  220. return Object.assign(fn, { RemoteConfig: remoteConfig });
  221. }
  222. /**
  223. * Gets the `AppCheck` service namespace. The returned namespace can be used to get the
  224. * `AppCheck` service for the default app or an explicitly specified app.
  225. */
  226. get appCheck() {
  227. const fn = (app) => {
  228. return this.ensureApp(app).appCheck();
  229. };
  230. const appCheck = require('../app-check/app-check').AppCheck;
  231. return Object.assign(fn, { AppCheck: appCheck });
  232. }
  233. // TODO: Change the return types to app.App in the following methods.
  234. /**
  235. * Initializes the FirebaseApp instance.
  236. *
  237. * @param options - Optional options for the FirebaseApp instance.
  238. * If none present will try to initialize from the FIREBASE_CONFIG environment variable.
  239. * If the environment variable contains a string that starts with '{' it will be parsed as JSON,
  240. * otherwise it will be assumed to be pointing to a file.
  241. * @param appName - Optional name of the FirebaseApp instance.
  242. *
  243. * @returns A new FirebaseApp instance.
  244. */
  245. initializeApp(options, appName) {
  246. return this.INTERNAL.initializeApp(options, appName);
  247. }
  248. /**
  249. * Returns the FirebaseApp instance with the provided name (or the default FirebaseApp instance
  250. * if no name is provided).
  251. *
  252. * @param appName - Optional name of the FirebaseApp instance to return.
  253. * @returns The FirebaseApp instance which has the provided name.
  254. */
  255. app(appName) {
  256. return this.INTERNAL.app(appName);
  257. }
  258. /*
  259. * Returns an array of all the non-deleted FirebaseApp instances.
  260. */
  261. get apps() {
  262. return this.INTERNAL.apps;
  263. }
  264. ensureApp(app) {
  265. if (typeof app === 'undefined') {
  266. app = this.app();
  267. }
  268. return app;
  269. }
  270. }
  271. exports.FirebaseNamespace = FirebaseNamespace;
  272. /**
  273. * In order to maintain backward compatibility, we instantiate a default namespace instance in
  274. * this module, and delegate all app lifecycle operations to it. In a future implementation where
  275. * the old admin namespace is no longer supported, we should remove this.
  276. *
  277. * @internal
  278. */
  279. exports.defaultNamespace = new FirebaseNamespace(lifecycle_1.defaultAppStore);
  280. function extendApp(app) {
  281. const result = app;
  282. if (result.__extended) {
  283. return result;
  284. }
  285. result.auth = () => {
  286. const fn = require('../auth/index').getAuth;
  287. return fn(app);
  288. };
  289. result.appCheck = () => {
  290. const fn = require('../app-check/index').getAppCheck;
  291. return fn(app);
  292. };
  293. result.database = (url) => {
  294. const fn = require('../database/index').getDatabaseWithUrl;
  295. return fn(url, app);
  296. };
  297. result.messaging = () => {
  298. const fn = require('../messaging/index').getMessaging;
  299. return fn(app);
  300. };
  301. result.storage = () => {
  302. const fn = require('../storage/index').getStorage;
  303. return fn(app);
  304. };
  305. result.firestore = () => {
  306. const fn = require('../firestore/index').getFirestore;
  307. return fn(app);
  308. };
  309. result.instanceId = () => {
  310. const fn = require('../instance-id/index').getInstanceId;
  311. return fn(app);
  312. };
  313. result.installations = () => {
  314. const fn = require('../installations/index').getInstallations;
  315. return fn(app);
  316. };
  317. result.machineLearning = () => {
  318. const fn = require('../machine-learning/index').getMachineLearning;
  319. return fn(app);
  320. };
  321. result.projectManagement = () => {
  322. const fn = require('../project-management/index').getProjectManagement;
  323. return fn(app);
  324. };
  325. result.securityRules = () => {
  326. const fn = require('../security-rules/index').getSecurityRules;
  327. return fn(app);
  328. };
  329. result.remoteConfig = () => {
  330. const fn = require('../remote-config/index').getRemoteConfig;
  331. return fn(app);
  332. };
  333. result.__extended = true;
  334. return result;
  335. }