14
14
* limitations under the License.
15
15
*/
16
16
17
+ import fs = require( 'fs' ) ;
17
18
import { deepExtend } from './utils/deep-copy' ;
18
19
import { AppErrorCodes , FirebaseAppError } from './utils/error' ;
19
20
import { AppHook , FirebaseApp , FirebaseAppOptions } from './firebase-app' ;
@@ -32,8 +33,18 @@ import {Database} from '@firebase/database';
32
33
import { Firestore } from '@google-cloud/firestore' ;
33
34
import { InstanceId } from './instance-id/instance-id' ;
34
35
36
+ import * as validator from './utils/validator' ;
37
+
35
38
const DEFAULT_APP_NAME = '[DEFAULT]' ;
36
39
40
+ /**
41
+ * Constant holding the environment variable name with the default config.
42
+ * If the environmet variable contains a string that starts with '{' it will be parsed as JSON,
43
+ * otherwise it will be assumed to be pointing to a file.
44
+ */
45
+ export const FIREBASE_CONFIG_VAR : string = 'FIREBASE_CONFIG' ;
46
+
47
+
37
48
let globalAppDefaultCred : ApplicationDefaultCredential ;
38
49
let globalCertCreds : { [ key : string ] : CertCredential } = { } ;
39
50
let globalRefreshTokenCreds : { [ key : string ] : RefreshTokenCredential } = { } ;
@@ -59,12 +70,20 @@ export class FirebaseNamespaceInternals {
59
70
/**
60
71
* Initializes the FirebaseApp instance.
61
72
*
62
- * @param {FirebaseAppOptions } options Options for the FirebaseApp instance.
73
+ * @param {FirebaseAppOptions } options Optional options for the FirebaseApp instance. If none present
74
+ * will try to initialize from the FIREBASE_CONFIG environment variable.
75
+ * If the environmet variable contains a string that starts with '{'
76
+ * it will be parsed as JSON,
77
+ * otherwise it will be assumed to be pointing to a file.
63
78
* @param {string } [appName] Optional name of the FirebaseApp instance.
64
79
*
65
80
* @return {FirebaseApp } A new FirebaseApp instance.
66
81
*/
67
- public initializeApp ( options : FirebaseAppOptions , appName = DEFAULT_APP_NAME ) : FirebaseApp {
82
+ public initializeApp ( options ?: FirebaseAppOptions , appName = DEFAULT_APP_NAME ) : FirebaseApp {
83
+ if ( typeof options === 'undefined' ) {
84
+ options = this . loadOptionsFromEnvVar ( ) ;
85
+ options . credential = new ApplicationDefaultCredential ( ) ;
86
+ }
68
87
if ( typeof appName !== 'string' || appName === '' ) {
69
88
throw new FirebaseAppError (
70
89
AppErrorCodes . INVALID_APP_NAME ,
@@ -227,6 +246,36 @@ export class FirebaseNamespaceInternals {
227
246
}
228
247
} ) ;
229
248
}
249
+
250
+ /**
251
+ * Parse the file pointed to by the FIREBASE_CONFIG_VAR, if it exists.
252
+ * Or if the FIREBASE_CONFIG_ENV contains a valid JSON object, parse it directly.
253
+ * If the environmet variable contains a string that starts with '{' it will be parsed as JSON,
254
+ * otherwise it will be assumed to be pointing to a file.
255
+ */
256
+ private loadOptionsFromEnvVar ( ) : FirebaseAppOptions {
257
+ let config = process . env [ FIREBASE_CONFIG_VAR ] ;
258
+ if ( ! validator . isNonEmptyString ( config ) ) {
259
+ return { } ;
260
+ }
261
+ try {
262
+ let contents ;
263
+ if ( config . startsWith ( '{' ) ) {
264
+ // Assume json object.
265
+ contents = config ;
266
+ } else {
267
+ // Assume filename.
268
+ contents = fs . readFileSync ( config , 'utf8' ) ;
269
+ }
270
+ return JSON . parse ( contents ) as FirebaseAppOptions ;
271
+ } catch ( error ) {
272
+ // Throw a nicely formed error message if the file contents cannot be parsed
273
+ throw new FirebaseAppError (
274
+ AppErrorCodes . INVALID_APP_OPTIONS ,
275
+ 'Failed to parse app options file: ' + error ,
276
+ ) ;
277
+ }
278
+ }
230
279
}
231
280
232
281
@@ -354,12 +403,15 @@ export class FirebaseNamespace {
354
403
/**
355
404
* Initializes the FirebaseApp instance.
356
405
*
357
- * @param {FirebaseAppOptions } options Options for the FirebaseApp instance.
406
+ * @param {FirebaseAppOptions } [options] Optional options for the FirebaseApp instance.
407
+ * If none present will try to initialize from the FIREBASE_CONFIG environment variable.
408
+ * If the environmet variable contains a string that starts with '{' it will be parsed as JSON,
409
+ * otherwise it will be assumed to be pointing to a file.
358
410
* @param {string } [appName] Optional name of the FirebaseApp instance.
359
411
*
360
412
* @return {FirebaseApp } A new FirebaseApp instance.
361
413
*/
362
- public initializeApp ( options : FirebaseAppOptions , appName ?: string ) : FirebaseApp {
414
+ public initializeApp ( options ? : FirebaseAppOptions , appName ?: string ) : FirebaseApp {
363
415
return this . INTERNAL . initializeApp ( options , appName ) ;
364
416
}
365
417
0 commit comments