Skip to content

Commit 2c6344c

Browse files
committed
Handle demo- projects
1 parent a4a09cf commit 2c6344c

File tree

1 file changed

+94
-100
lines changed

1 file changed

+94
-100
lines changed

packages/util/postinstall.js

+94-100
Original file line numberDiff line numberDiff line change
@@ -15,99 +15,100 @@
1515
* limitations under the License.
1616
*/
1717

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');
2221

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+
}
2726

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;
4038
}
39+
}
4140

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
4552
);
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.`
6777
);
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+
}
8780

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})`
10892
);
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 => {
111112
const emulatorHosts = {
112113
firestore: process.env.FIRESTORE_EMULATOR_HOST,
113114
database: process.env.FIREBASE_DATABASE_EMULATOR_HOST,
@@ -117,33 +118,26 @@ try {
117118

118119
const defaults = config && { config, emulatorHosts };
119120

120-
return Promise.all([
121+
await Promise.all([
121122
writeFile(
122123
join(__dirname, 'dist', 'autoinit_env.js'),
123124
`'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)};`
126127
),
127128
writeFile(
128129
join(__dirname, 'dist', 'autoinit_env.mjs'),
129130
`const postinstallDefaults = ${JSON.stringify(defaults)};
130-
export { postinstallDefaults };`
131+
export { postinstallDefaults };`
131132
)
132133
]);
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+
});
144137
} catch (e) {
145138
console.warn(
146139
'Unexpected error encountered in @firebase/util postinstall script, ignoring FIREBASE_WEBAPP_CONFIG\n',
147140
e
148141
);
142+
process.exit(0);
149143
}

0 commit comments

Comments
 (0)