15
15
* limitations under the License.
16
16
*/
17
17
18
- try {
19
- const { writeFile, readFile } = require ( 'node:fs/promises' ) ;
20
- const { pathToFileURL } = require ( 'node:url' ) ;
21
- const { isAbsolute, join } = require ( 'node:path' ) ;
18
+ const { writeFile, readFile } = require ( 'node:fs/promises' ) ;
19
+ const { pathToFileURL } = require ( 'node:url' ) ;
20
+ const { isAbsolute, join } = require ( 'node:path' ) ;
22
21
23
- new Promise ( resolve => {
24
- if ( ! process . env . FIREBASE_WEBAPP_CONFIG ) {
25
- return resolve ( undefined ) ;
26
- }
22
+ async function getPartialConfig ( ) {
23
+ if ( ! process . env . FIREBASE_WEBAPP_CONFIG ) {
24
+ return undefined ;
25
+ }
27
26
28
- // Like FIREBASE_CONFIG (admin autoinit) FIREBASE_WEBAPP_CONFIG can be
29
- // either a JSON representation of FirebaseOptions or the path to a filename
30
- if ( process . env . FIREBASE_WEBAPP_CONFIG . startsWith ( '{"' ) ) {
31
- try {
32
- return resolve ( JSON . parse ( process . env . FIREBASE_WEBAPP_CONFIG ) ) ;
33
- } catch ( e ) {
34
- console . warn (
35
- 'FIREBASE_WEBAPP_CONFIG could not be parsed, ignoring.\n' ,
36
- e
37
- ) ;
38
- return resolve ( undefined ) ;
39
- }
27
+ // Like FIREBASE_CONFIG (admin autoinit) FIREBASE_WEBAPP_CONFIG can be
28
+ // either a JSON representation of FirebaseOptions or the path to a filename
29
+ if ( process . env . FIREBASE_WEBAPP_CONFIG . startsWith ( '{"' ) ) {
30
+ try {
31
+ return JSON . parse ( process . env . FIREBASE_WEBAPP_CONFIG ) ;
32
+ } catch ( e ) {
33
+ console . warn (
34
+ 'FIREBASE_WEBAPP_CONFIG could not be parsed, ignoring.\n' ,
35
+ e
36
+ ) ;
37
+ return undefined ;
40
38
}
39
+ }
41
40
42
- const fileName = process . env . FIREBASE_WEBAPP_CONFIG ;
43
- const fileURL = pathToFileURL (
44
- isAbsolute ( fileName ) ? fileName : join ( process . cwd ( ) , fileName )
41
+ const fileName = process . env . FIREBASE_WEBAPP_CONFIG ;
42
+ const fileURL = pathToFileURL (
43
+ isAbsolute ( fileName ) ? fileName : join ( process . cwd ( ) , fileName )
44
+ ) ;
45
+ try {
46
+ const fileContents = await readFile ( fileURL , 'utf-8' ) ;
47
+ return JSON . parse ( fileContents ) ;
48
+ } catch ( e ) {
49
+ console . warn (
50
+ `Contents of "${ fileName } " could not be parsed, ignoring FIREBASE_WEBAPP_CONFIG.\n` ,
51
+ e
45
52
) ;
46
- resolve (
47
- readFile ( fileURL , 'utf-8' ) . then (
48
- fileContents => {
49
- try {
50
- return JSON . parse ( fileContents ) ;
51
- } catch ( e ) {
52
- console . warn (
53
- `Contents of "${ fileName } " could not be parsed, ignoring FIREBASE_WEBAPP_CONFIG.\n` ,
54
- e
55
- ) ;
56
- return undefined ;
57
- }
58
- } ,
59
- e => {
60
- console . warn (
61
- `Contents of "${ fileName } " could not be parsed, ignoring FIREBASE_WEBAPP_CONFIG.\n` ,
62
- e
63
- ) ;
64
- return undefined ;
65
- }
66
- )
53
+ return undefined ;
54
+ }
55
+ }
56
+
57
+ async function getFullConfig ( partialConfig ) {
58
+ if ( ! partialConfig ) {
59
+ return undefined ;
60
+ }
61
+ // In Firebase App Hosting the config provided to the environment variable is up-to-date and
62
+ // "complete" we should not reach out to the webConfig endpoint to freshen it
63
+ if ( process . env . X_GOOGLE_TARGET_PLATFORM === 'fah' ) {
64
+ return partialConfig ;
65
+ }
66
+ const projectId = partialConfig . projectId || '-' ;
67
+ // If the projectId starts with demo- this is an demo project from the firebase emulators
68
+ // treat the config as whole
69
+ if ( projectId . startsWith ( 'demo-' ) ) {
70
+ return partialConfig ;
71
+ }
72
+ const appId = partialConfig . appId ;
73
+ const apiKey = partialConfig . apiKey ;
74
+ if ( ! appId || ! apiKey ) {
75
+ console . warn (
76
+ `Unable to fetch Firebase config, appId and apiKey are required, ignoring FIREBASE_WEBAPP_CONFIG.`
67
77
) ;
68
- } )
69
- . then ( partialConfig => {
70
- if ( ! partialConfig ) {
71
- return undefined ;
72
- }
73
- // In Firebase App Hosting the config provided to the environment variable is up-to-date and
74
- // "complete" we should not reach out to the webConfig endpoint to freshen it
75
- if ( process . env . X_GOOGLE_TARGET_PLATFORM === 'fah' ) {
76
- return partialConfig ;
77
- }
78
- const projectId = partialConfig . projectId || '-' ;
79
- const appId = partialConfig . appId ;
80
- const apiKey = partialConfig . apiKey ;
81
- if ( ! appId || ! apiKey ) {
82
- console . warn (
83
- `Unable to fetch Firebase config, appId and apiKey are required, ignoring FIREBASE_WEBAPP_CONFIG.`
84
- ) ;
85
- return undefined ;
86
- }
78
+ return undefined ;
79
+ }
87
80
88
- const url = `https://firebase.googleapis.com/v1alpha/projects/${ projectId } /apps/${ appId } /webConfig` ;
89
- return fetch ( url , { headers : { 'x-goog-api-key' : apiKey } } ) . then (
90
- response => {
91
- if ( ! response . ok ) {
92
- console . warn (
93
- `Unable to fetch Firebase config, ignoring FIREBASE_WEBAPP_CONFIG.`
94
- ) ;
95
- console . warn (
96
- `${ url } returned ${ response . statusText } (${ response . status } )`
97
- ) ;
98
- return undefined ;
99
- }
100
- return response . json ( ) . then ( json => ( { ...json , apiKey } ) ) ;
101
- } ,
102
- e => {
103
- console . warn (
104
- `Unable to fetch Firebase config, ignoring FIREBASE_WEBAPP_CONFIG.\n${ e . cause } `
105
- ) ;
106
- return undefined ;
107
- }
81
+ const url = `https://firebase.googleapis.com/v1alpha/projects/${ projectId } /apps/${ appId } /webConfig` ;
82
+ try {
83
+ const response = await fetch ( url , {
84
+ headers : { 'x-goog-api-key' : apiKey }
85
+ } ) ;
86
+ if ( ! response . ok ) {
87
+ console . warn (
88
+ `Unable to fetch Firebase config, ignoring FIREBASE_WEBAPP_CONFIG.`
89
+ ) ;
90
+ console . warn (
91
+ `${ url } returned ${ response . statusText } (${ response . status } )`
108
92
) ;
109
- } )
110
- . then ( config => {
93
+ try {
94
+ console . warn ( ( await response . json ( ) ) . error . message ) ;
95
+ } catch ( e ) { }
96
+ return undefined ;
97
+ }
98
+ const json = await response . json ( ) ;
99
+ return { ...json , apiKey } ;
100
+ } catch ( e ) {
101
+ console . warn (
102
+ `Unable to fetch Firebase config, ignoring FIREBASE_WEBAPP_CONFIG.\n` ,
103
+ e
104
+ ) ;
105
+ }
106
+ }
107
+
108
+ try {
109
+ getPartialConfig ( )
110
+ . then ( getFullConfig )
111
+ . then ( async config => {
111
112
const emulatorHosts = {
112
113
firestore : process . env . FIRESTORE_EMULATOR_HOST ,
113
114
database : process . env . FIREBASE_DATABASE_EMULATOR_HOST ,
@@ -117,33 +118,26 @@ try {
117
118
118
119
const defaults = config && { config, emulatorHosts } ;
119
120
120
- return Promise . all ( [
121
+ await Promise . all ( [
121
122
writeFile (
122
123
join ( __dirname , 'dist' , 'autoinit_env.js' ) ,
123
124
`'use strict';
124
- Object.defineProperty(exports, '__esModule', { value: true });
125
- exports.postinstallDefaults = ${ JSON . stringify ( defaults ) } ;`
125
+ Object.defineProperty(exports, '__esModule', { value: true });
126
+ exports.postinstallDefaults = ${ JSON . stringify ( defaults ) } ;`
126
127
) ,
127
128
writeFile (
128
129
join ( __dirname , 'dist' , 'autoinit_env.mjs' ) ,
129
130
`const postinstallDefaults = ${ JSON . stringify ( defaults ) } ;
130
- export { postinstallDefaults };`
131
+ export { postinstallDefaults };`
131
132
)
132
133
] ) ;
133
- } )
134
- . then (
135
- ( ) => process . exit ( 0 ) ,
136
- e => {
137
- console . warn (
138
- 'Unexpected error encountered in @firebase/util postinstall script, ignoring FIREBASE_WEBAPP_CONFIG.\n' ,
139
- e
140
- ) ;
141
- process . exit ( 0 ) ;
142
- }
143
- ) ;
134
+
135
+ process . exit ( 0 ) ;
136
+ } ) ;
144
137
} catch ( e ) {
145
138
console . warn (
146
139
'Unexpected error encountered in @firebase/util postinstall script, ignoring FIREBASE_WEBAPP_CONFIG\n' ,
147
140
e
148
141
) ;
142
+ process . exit ( 0 ) ;
149
143
}
0 commit comments