1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.FIREBASE_CONFIG_VAR = exports.deleteApp = exports.getApps = exports.getApp = exports.initializeApp = exports.defaultAppStore = exports.AppStore = void 0;
|
21 | const fs = require("fs");
|
22 | const validator = require("../utils/validator");
|
23 | const error_1 = require("../utils/error");
|
24 | const credential_internal_1 = require("./credential-internal");
|
25 | const firebase_app_1 = require("./firebase-app");
|
26 | const DEFAULT_APP_NAME = '[DEFAULT]';
|
27 | class AppStore {
|
28 | constructor() {
|
29 | this.appStore = new Map();
|
30 | }
|
31 | initializeApp(options, appName = DEFAULT_APP_NAME) {
|
32 | if (typeof options === 'undefined') {
|
33 | options = loadOptionsFromEnvVar();
|
34 | options.credential = (0, credential_internal_1.getApplicationDefault)();
|
35 | }
|
36 | if (typeof appName !== 'string' || appName === '') {
|
37 | 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.`);
|
38 | }
|
39 | else if (this.appStore.has(appName)) {
|
40 | if (appName === DEFAULT_APP_NAME) {
|
41 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.DUPLICATE_APP, 'The default Firebase app already exists. This means you called initializeApp() ' +
|
42 | 'more than once without providing an app name as the second argument. In most cases ' +
|
43 | 'you only need to call initializeApp() once. But if you do want to initialize ' +
|
44 | 'multiple apps, pass a second argument to initializeApp() to give each app a unique ' +
|
45 | 'name.');
|
46 | }
|
47 | else {
|
48 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.DUPLICATE_APP, `Firebase app named "${appName}" already exists. This means you called initializeApp() ` +
|
49 | 'more than once with the same app name as the second argument. Make sure you provide a ' +
|
50 | 'unique name every time you call initializeApp().');
|
51 | }
|
52 | }
|
53 | const app = new firebase_app_1.FirebaseApp(options, appName, this);
|
54 | this.appStore.set(app.name, app);
|
55 | return app;
|
56 | }
|
57 | getApp(appName = DEFAULT_APP_NAME) {
|
58 | if (typeof appName !== 'string' || appName === '') {
|
59 | 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.`);
|
60 | }
|
61 | else if (!this.appStore.has(appName)) {
|
62 | let errorMessage = (appName === DEFAULT_APP_NAME)
|
63 | ? 'The default Firebase app does not exist. ' : `Firebase app named "${appName}" does not exist. `;
|
64 | errorMessage += 'Make sure you call initializeApp() before using any of the Firebase services.';
|
65 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.NO_APP, errorMessage);
|
66 | }
|
67 | return this.appStore.get(appName);
|
68 | }
|
69 | getApps() {
|
70 |
|
71 | return Array.from(this.appStore.values());
|
72 | }
|
73 | deleteApp(app) {
|
74 | if (typeof app !== 'object' || app === null || !('options' in app)) {
|
75 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_ARGUMENT, 'Invalid app argument.');
|
76 | }
|
77 |
|
78 | const existingApp = getApp(app.name);
|
79 |
|
80 |
|
81 | return existingApp.delete();
|
82 | }
|
83 | clearAllApps() {
|
84 | const promises = [];
|
85 | this.getApps().forEach((app) => {
|
86 | promises.push(this.deleteApp(app));
|
87 | });
|
88 | return Promise.all(promises).then();
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 | removeApp(appName) {
|
96 | this.appStore.delete(appName);
|
97 | }
|
98 | }
|
99 | exports.AppStore = AppStore;
|
100 | exports.defaultAppStore = new AppStore();
|
101 | function initializeApp(options, appName = DEFAULT_APP_NAME) {
|
102 | return exports.defaultAppStore.initializeApp(options, appName);
|
103 | }
|
104 | exports.initializeApp = initializeApp;
|
105 | function getApp(appName = DEFAULT_APP_NAME) {
|
106 | return exports.defaultAppStore.getApp(appName);
|
107 | }
|
108 | exports.getApp = getApp;
|
109 | function getApps() {
|
110 | return exports.defaultAppStore.getApps();
|
111 | }
|
112 | exports.getApps = getApps;
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function deleteApp(app) {
|
131 | return exports.defaultAppStore.deleteApp(app);
|
132 | }
|
133 | exports.deleteApp = deleteApp;
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | exports.FIREBASE_CONFIG_VAR = 'FIREBASE_CONFIG';
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | function loadOptionsFromEnvVar() {
|
147 | const config = process.env[exports.FIREBASE_CONFIG_VAR];
|
148 | if (!validator.isNonEmptyString(config)) {
|
149 | return {};
|
150 | }
|
151 | try {
|
152 | const contents = config.startsWith('{') ? config : fs.readFileSync(config, 'utf8');
|
153 | return JSON.parse(contents);
|
154 | }
|
155 | catch (error) {
|
156 |
|
157 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS, 'Failed to parse app options file: ' + error);
|
158 | }
|
159 | }
|