123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.FIREBASE_CONFIG_VAR = exports.deleteApp = exports.getApps = exports.getApp = exports.initializeApp = exports.defaultAppStore = exports.AppStore = void 0;
- const fs = require("fs");
- const validator = require("../utils/validator");
- const error_1 = require("../utils/error");
- const credential_internal_1 = require("./credential-internal");
- const firebase_app_1 = require("./firebase-app");
- const DEFAULT_APP_NAME = '[DEFAULT]';
- class AppStore {
- constructor() {
- this.appStore = new Map();
- }
- initializeApp(options, appName = DEFAULT_APP_NAME) {
- if (typeof options === 'undefined') {
- options = loadOptionsFromEnvVar();
- options.credential = (0, credential_internal_1.getApplicationDefault)();
- }
- if (typeof appName !== 'string' || appName === '') {
- throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_NAME, `Invalid Firebase app name "${appName}" provided. App name must be a non-empty string.`);
- }
- else if (this.appStore.has(appName)) {
- if (appName === DEFAULT_APP_NAME) {
- throw new error_1.FirebaseAppError(error_1.AppErrorCodes.DUPLICATE_APP, 'The default Firebase app already exists. This means you called initializeApp() ' +
- 'more than once without providing an app name as the second argument. In most cases ' +
- 'you only need to call initializeApp() once. But if you do want to initialize ' +
- 'multiple apps, pass a second argument to initializeApp() to give each app a unique ' +
- 'name.');
- }
- else {
- throw new error_1.FirebaseAppError(error_1.AppErrorCodes.DUPLICATE_APP, `Firebase app named "${appName}" already exists. This means you called initializeApp() ` +
- 'more than once with the same app name as the second argument. Make sure you provide a ' +
- 'unique name every time you call initializeApp().');
- }
- }
- const app = new firebase_app_1.FirebaseApp(options, appName, this);
- this.appStore.set(app.name, app);
- return app;
- }
- getApp(appName = DEFAULT_APP_NAME) {
- if (typeof appName !== 'string' || appName === '') {
- throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_NAME, `Invalid Firebase app name "${appName}" provided. App name must be a non-empty string.`);
- }
- else if (!this.appStore.has(appName)) {
- let errorMessage = (appName === DEFAULT_APP_NAME)
- ? 'The default Firebase app does not exist. ' : `Firebase app named "${appName}" does not exist. `;
- errorMessage += 'Make sure you call initializeApp() before using any of the Firebase services.';
- throw new error_1.FirebaseAppError(error_1.AppErrorCodes.NO_APP, errorMessage);
- }
- return this.appStore.get(appName);
- }
- getApps() {
-
- return Array.from(this.appStore.values());
- }
- deleteApp(app) {
- if (typeof app !== 'object' || app === null || !('options' in app)) {
- throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_ARGUMENT, 'Invalid app argument.');
- }
-
- const existingApp = getApp(app.name);
-
-
- return existingApp.delete();
- }
- clearAllApps() {
- const promises = [];
- this.getApps().forEach((app) => {
- promises.push(this.deleteApp(app));
- });
- return Promise.all(promises).then();
- }
-
- removeApp(appName) {
- this.appStore.delete(appName);
- }
- }
- exports.AppStore = AppStore;
- exports.defaultAppStore = new AppStore();
- function initializeApp(options, appName = DEFAULT_APP_NAME) {
- return exports.defaultAppStore.initializeApp(options, appName);
- }
- exports.initializeApp = initializeApp;
- function getApp(appName = DEFAULT_APP_NAME) {
- return exports.defaultAppStore.getApp(appName);
- }
- exports.getApp = getApp;
- function getApps() {
- return exports.defaultAppStore.getApps();
- }
- exports.getApps = getApps;
- function deleteApp(app) {
- return exports.defaultAppStore.deleteApp(app);
- }
- exports.deleteApp = deleteApp;
- exports.FIREBASE_CONFIG_VAR = 'FIREBASE_CONFIG';
- function loadOptionsFromEnvVar() {
- const config = process.env[exports.FIREBASE_CONFIG_VAR];
- if (!validator.isNonEmptyString(config)) {
- return {};
- }
- try {
- const contents = config.startsWith('{') ? config : fs.readFileSync(config, 'utf8');
- return JSON.parse(contents);
- }
- catch (error) {
-
- throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS, 'Failed to parse app options file: ' + error);
- }
- }
|