@@ -19,43 +19,43 @@ const { writeFile, readFile } = require('node:fs/promises');
19
19
const { pathToFileURL } = require ( 'node:url' ) ;
20
20
const { isAbsolute, join } = require ( 'node:path' ) ;
21
21
22
- function getConfigFromEnv ( ) {
22
+ new Promise ( resolve => {
23
23
if ( ! process . env . FIREBASE_WEBAPP_CONFIG ) {
24
- return Promise . resolve ( undefined ) ;
24
+ return resolve ( undefined ) ;
25
25
}
26
26
27
27
// Like FIREBASE_CONFIG (admin autoinit) FIREBASE_WEBAPP_CONFIG can be
28
28
// either a JSON representation of FirebaseOptions or the path to a filename
29
29
if ( process . env . FIREBASE_WEBAPP_CONFIG . startsWith ( '{"' ) ) {
30
30
try {
31
- return Promise . resolve ( JSON . parse ( process . env . FIREBASE_WEBAPP_CONFIG ) ) ;
31
+ return resolve ( JSON . parse ( process . env . FIREBASE_WEBAPP_CONFIG ) ) ;
32
32
} catch ( e ) {
33
33
console . error ( 'FIREBASE_WEBAPP_CONFIG could not be parsed.\n' , e ) ;
34
- return Promise . resolve ( undefined ) ;
34
+ return resolve ( undefined ) ;
35
35
}
36
36
}
37
37
38
38
const fileName = process . env . FIREBASE_WEBAPP_CONFIG ;
39
39
const fileURL = pathToFileURL (
40
40
isAbsolute ( fileName ) ? fileName : join ( process . cwd ( ) , fileName )
41
41
) ;
42
- return readFile ( fileURL , 'utf-8' ) . then (
43
- fileContents => {
44
- try {
45
- return JSON . parse ( fileContents ) ;
46
- } catch ( e ) {
42
+ resolve (
43
+ readFile ( fileURL , 'utf-8' ) . then (
44
+ fileContents => {
45
+ try {
46
+ return JSON . parse ( fileContents ) ;
47
+ } catch ( e ) {
48
+ console . error ( `Contents of "${ fileName } " could not be parsed.\n` , e ) ;
49
+ return undefined ;
50
+ }
51
+ } ,
52
+ e => {
47
53
console . error ( `Contents of "${ fileName } " could not be parsed.\n` , e ) ;
48
54
return undefined ;
49
55
}
50
- } ,
51
- e => {
52
- console . error ( `Contents of "${ fileName } " could not be parsed.\n` , e ) ;
53
- return undefined ;
54
- }
56
+ )
55
57
) ;
56
- }
57
-
58
- getConfigFromEnv ( )
58
+ } )
59
59
. then ( partialConfig => {
60
60
if ( ! partialConfig ) {
61
61
return undefined ;
@@ -78,18 +78,21 @@ getConfigFromEnv()
78
78
return fetch (
79
79
`https://firebase.googleapis.com/v1alpha/projects/${ projectId } /apps/${ appId } /webConfig` ,
80
80
{ headers : { 'x-goog-api-key' : apiKey } }
81
- ) . then ( response => {
82
- if ( ! response . ok ) {
83
- console . error (
84
- `Unable to fetch Firebase config, API returned ${ response . statusText } (${ response . status } )`
85
- ) ;
81
+ ) . then (
82
+ response => {
83
+ if ( ! response . ok ) {
84
+ console . error (
85
+ `Unable to fetch Firebase config, API returned ${ response . statusText } (${ response . status } )`
86
+ ) ;
87
+ return undefined ;
88
+ }
89
+ return response . json ( ) . then ( json => ( { ...json , apiKey } ) ) ;
90
+ } ,
91
+ e => {
92
+ console . error ( `Unable to fetch Firebase config\n${ e . cause } ` ) ;
86
93
return undefined ;
87
94
}
88
- return response . json ( ) . then ( json => ( { ...json , apiKey } ) ) ;
89
- } , ( e ) => {
90
- console . error ( `Unable to fetch Firebase config\n${ e . cause } ` ) ;
91
- return undefined ;
92
- } ) ;
95
+ ) ;
93
96
} )
94
97
. then ( config => {
95
98
const emulatorHosts = Object . entries ( {
0 commit comments