Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo of autoinit on App Hosting #8483

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/util/defaults.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FirebaseOptions } from "@firebase/app-types";

declare const FirebaseDefaults: {
config?: FirebaseOptions;
emulatorHosts?: Record<string, string|undefined>;
} | undefined;

export default FirebaseDefaults;

Check failure on line 8 in packages/util/defaults.d.ts

View workflow job for this annotation

GitHub Actions / Lint

Prefer named exports
1 change: 1 addition & 0 deletions packages/util/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = undefined
1 change: 1 addition & 0 deletions packages/util/defaults.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default undefined
14 changes: 12 additions & 2 deletions packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@
},
"default": "./dist/index.esm2017.js"
},
"./postinstall": {
"types": "./defaults.d.ts",
"require": "./defaults.js",
"import": "./defaults.mjs",
"default": "./defaults.mjs"
},
"./package.json": "./package.json"
},
"files": [
"dist"
"dist",
"defaults.*js",
"defaults.d.ts",
"postinstall.mjs"
],
"scripts": {
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
Expand All @@ -40,7 +49,8 @@
"test:node": "TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha test/**/*.test.* --config ../../config/mocharc.node.js",
"trusted-type-check": "tsec -p tsconfig.json --noEmit",
"api-report": "api-extractor run --local --verbose",
"typings:public": "node ../../scripts/build/use_typings.js ./dist/util-public.d.ts"
"typings:public": "node ../../scripts/build/use_typings.js ./dist/util-public.d.ts",
"postinstall": "node ./postinstall.mjs"
},
"license": "Apache-2.0",
"dependencies": {
Expand Down
41 changes: 41 additions & 0 deletions packages/util/postinstall.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { writeFile } from "fs/promises";

let firebaseConfig = {};
if (process.env.FIREBASE_CONFIG?.startsWith("{")) {
// TODO probably want a more robust yaml parse
firebaseConfig = Object.fromEntries(process.env.FIREBASE_CONFIG.match(/[^(\:\{\},)]+\:[^(,})]+/g).map(it => {
const parts = it.split(":");
return [parts[0], parts.slice(1).join(":")]
}));
}

const projectId = firebaseConfig.projectId;
const appId = firebaseConfig.appId;
const apiKey = firebaseConfig.apiKey;

const config = projectId && appId && apiKey && await (await fetch(
`https://firebase.googleapis.com/v1alpha/projects/${projectId}/apps/${appId}/webConfig`,
{ headers: { "x-goog-api-key": apiKey } }
)).json();

if (config) {
config.apiKey = apiKey;
}

let emulatorHosts = {
firestore: process.env.FIRESTORE_EMULATOR_HOST,
database: process.env.FIREBASE_DATABASE_EMULATOR_HOST,
storage: process.env.FIREBASE_STORAGE_EMULATOR_HOST,
auth: process.env.FIREBASE_AUTH_EMULATOR_HOST,
};

if (!Object.values(emulatorHosts).filter(it => it).length) {
emulatorHosts = undefined;
}

const defaults = (config || emulatorHosts) && { config, emulatorHosts };

await Promise.all([
writeFile("./defaults.js", `module.exports = ${JSON.stringify(defaults)}`),
writeFile("./defaults.mjs", `export default ${JSON.stringify(defaults)}`),
]);
3 changes: 3 additions & 0 deletions packages/util/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import { base64Decode } from './crypt';
import { getGlobal } from './global';
// @ts-ignore
import postinstallDefaults from "@firebase/util/postinstall";

Check failure on line 21 in packages/util/src/defaults.ts

View workflow job for this annotation

GitHub Actions / Lint

'@firebase/util' should be listed in the project's dependencies. Run 'npm i -S @firebase/util' to add it

/**
* Keys for experimental properties on the `FirebaseDefaults` object.
Expand Down Expand Up @@ -100,6 +102,7 @@
export const getDefaults = (): FirebaseDefaults | undefined => {
try {
return (
postinstallDefaults ||
getDefaultsFromGlobal() ||
getDefaultsFromEnvVariable() ||
getDefaultsFromCookie()
Expand Down
Loading