Skip to content

Commit 525a4e2

Browse files
committed
Cleanup
1 parent c8ffa75 commit 525a4e2

File tree

9 files changed

+134
-121
lines changed

9 files changed

+134
-121
lines changed

packages/util/defaults.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/util/defaults.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/util/defaults.mjs

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/util/package.json

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@
1919
},
2020
"default": "./dist/index.esm2017.js"
2121
},
22-
"./postinstall": {
23-
"types": "./defaults.d.ts",
24-
"require": "./defaults.js",
25-
"import": "./defaults.mjs",
26-
"default": "./defaults.mjs"
22+
"./autoinit_env": {
23+
"require": "./dist/autoinit_env.js",
24+
"import": "./dist/autoinit_env.mjs",
25+
"default": "./dist/autoinit_env.mjs"
2726
},
2827
"./package.json": "./package.json"
2928
},
3029
"files": [
31-
"dist",
32-
"defaults.*js",
33-
"defaults.d.ts",
34-
"postinstall.mjs"
30+
"dist"
3531
],
3632
"scripts": {
3733
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
@@ -48,7 +44,7 @@
4844
"trusted-type-check": "tsec -p tsconfig.json --noEmit",
4945
"api-report": "api-extractor run --local --verbose",
5046
"typings:public": "node ../../scripts/build/use_typings.js ./dist/util-public.d.ts",
51-
"postinstall": "node ./postinstall.mjs"
47+
"postinstall": "node ./dist/postinstall.js"
5248
},
5349
"license": "Apache-2.0",
5450
"dependencies": {

packages/util/postinstall.mjs

Lines changed: 0 additions & 98 deletions
This file was deleted.

packages/util/postinstall.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { writeFile, readFile } from "node:fs/promises";
2+
import { pathToFileURL } from "node:url";
3+
import { isAbsolute, join } from "node:path";
4+
import type { FirebaseOptions } from "@firebase/app";
5+
6+
async function getWebConfig(): Promise<FirebaseOptions|undefined> {
7+
// $FIREBASE_WEBAPP_CONFIG can be either a JSON representation of FirebaseOptions or the path
8+
// to a filename
9+
if (!process.env.FIREBASE_WEBAPP_CONFIG) {
10+
return undefined;
11+
}
12+
13+
let configFromEnvironment: Partial<FirebaseOptions>|undefined = undefined;
14+
if (process.env.FIREBASE_WEBAPP_CONFIG.startsWith("{\"")) {
15+
try {
16+
configFromEnvironment = JSON.parse(process.env.FIREBASE_WEBAPP_CONFIG);
17+
} catch(e) {
18+
console.error("FIREBASE_WEBAPP_CONFIG could not be parsed.", e);
19+
}
20+
} else {
21+
const fileName = process.env.FIREBASE_WEBAPP_CONFIG;
22+
const fileURL = pathToFileURL(isAbsolute(fileName) ? fileName : join(process.cwd(), fileName));
23+
const fileContents = await readFile(fileURL, "utf-8").catch((err) => {
24+
console.error(err);
25+
});
26+
if (fileContents) {
27+
try {
28+
configFromEnvironment = JSON.parse(fileContents);
29+
} catch(e) {
30+
console.error(`Contents of ${fileName} could not be parsed.`, e);
31+
}
32+
}
33+
}
34+
35+
// In Firebase App Hosting the config provided to the environment variable is up-to-date and
36+
// "complete" we should not reach out to the webConfig endpoint to freshen it
37+
if (process.env.X_GOOGLE_TARGET_PLATFORM === "fah") {
38+
return configFromEnvironment as FirebaseOptions;
39+
}
40+
41+
if (!configFromEnvironment) {
42+
return undefined;
43+
}
44+
const projectId = configFromEnvironment.projectId || "-";
45+
const appId = configFromEnvironment.appId;
46+
const apiKey = configFromEnvironment.apiKey;
47+
if (!appId || !apiKey) {
48+
return undefined;
49+
}
50+
51+
try {
52+
const response = await fetch(
53+
`https://firebase.googleapis.com/v1alpha/projects/${projectId}/apps/${appId}/webConfig`,
54+
{ headers: { "x-goog-api-key": apiKey } }
55+
);
56+
if (!response.ok) {
57+
return undefined;
58+
}
59+
const json = await response.json();
60+
return { ...json, apiKey };
61+
} catch(e) {
62+
return undefined;
63+
}
64+
}
65+
66+
getWebConfig().then(async (config) => {
67+
68+
const emulatorHosts = Object.entries({
69+
firestore: process.env.FIRESTORE_EMULATOR_HOST,
70+
database: process.env.FIREBASE_DATABASE_EMULATOR_HOST,
71+
storage: process.env.FIREBASE_STORAGE_EMULATOR_HOST,
72+
auth: process.env.FIREBASE_AUTH_EMULATOR_HOST,
73+
}).reduce(
74+
// We want a falsy value if none of the above are defined
75+
(current, [key, value]) => value ? { ...current, [key]: value } : current,
76+
undefined as Record<string,string>|undefined
77+
);
78+
79+
// getDefaults() will use this object, rather than fallback to other autoinit suppliers, if it's
80+
// truthy—if we've done nothing here, make it falsy.
81+
const defaults = (config || emulatorHosts) ? { config, emulatorHosts } : undefined;
82+
83+
await Promise.all([
84+
writeFile(join(__dirname, "autoinit_env.js"), `module.exports = ${JSON.stringify(defaults)}`),
85+
writeFile(join(__dirname, "autoinit_env.mjs"), `export default ${JSON.stringify(defaults)}`),
86+
]);
87+
88+
process.exit(0);
89+
90+
});

packages/util/rollup.config.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,33 @@ const nodeBuilds = [
7272
}
7373
];
7474

75-
export default [...browserBuilds, ...nodeBuilds];
75+
const autoinitBuild = [
76+
{
77+
input: './src/autoinit_env.ts',
78+
output: {
79+
file: './dist/autoinit_env.js',
80+
format: 'cjs',
81+
exports: 'default',
82+
},
83+
plugins: buildPlugins,
84+
},
85+
{
86+
input: './src/autoinit_env.ts',
87+
output: {
88+
file: './dist/autoinit_env.mjs',
89+
format: 'es',
90+
},
91+
plugins: buildPlugins,
92+
},
93+
{
94+
input: './postinstall.ts',
95+
output: {
96+
file: './dist/postinstall.js',
97+
format: 'cjs',
98+
},
99+
plugins: buildPlugins,
100+
external: () => true,
101+
}
102+
]
103+
104+
export default [...browserBuilds, ...nodeBuilds, ...autoinitBuild];

packages/util/src/autoinit_env.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { FirebaseDefaults } from "./defaults";
2+
3+
// This value is retrieved and hardcoded by the NPM postinstall script
4+
const firebaseDefaultConfig: FirebaseDefaults|undefined = undefined;
5+
6+
export default firebaseDefaultConfig;

packages/util/src/defaults.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import { base64Decode } from './crypt';
1919
import { getGlobal } from './global';
20-
// @ts-ignore
21-
import postinstallDefaults from "@firebase/util/postinstall";
20+
// @ts-expect-error
21+
import postinstallDefaults from "@firebase/util/autoinit_env";
2222

2323
/**
2424
* Keys for experimental properties on the `FirebaseDefaults` object.

0 commit comments

Comments
 (0)