Skip to content

support FIREBASE_DATABASE_EMULATOR_HOST env var #2005

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

Merged
merged 12 commits into from
Jul 22, 2019
Merged
11 changes: 10 additions & 1 deletion packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import { RepoInfo } from './RepoInfo';

/** @const {string} */
const DATABASE_URL_OPTION = 'databaseURL';
const PROJECT_ID_OPTION = 'projectId';
const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';
const DEFAULT_DATABASE_EMULATOR_PROJECT_ID: string = 'fake-server';

let _staticInstance: RepoManager;

Expand Down Expand Up @@ -80,7 +83,13 @@ export class RepoManager {
* @return {!Database}
*/
databaseFromApp(app: FirebaseApp, url?: string): Database {
const dbUrl: string = url || app.options[DATABASE_URL_OPTION];
let dbEmulatorUrl = process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
if (dbEmulatorUrl) {
dbEmulatorUrl = `${dbEmulatorUrl}?ns=${app.options[PROJECT_ID_OPTION] ||
DEFAULT_DATABASE_EMULATOR_PROJECT_ID}`;
}
const dbUrl: string =
dbEmulatorUrl || url || app.options[DATABASE_URL_OPTION];
if (dbUrl === undefined) {
fatal(
"Can't determine Firebase Database URL. Be sure to include " +
Expand Down
7 changes: 7 additions & 0 deletions packages/database/src/core/util/libs/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { Path } from '../Path';
import { RepoInfo } from '../../RepoInfo';
import { warnIfPageIsSecure, warn, fatal } from '../util';

const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';

/**
* @param {!string} pathString
* @return {string}
Expand Down Expand Up @@ -134,6 +136,11 @@ export const parseURL = function(
scheme = 'https',
port = 443;

// Don't use SSL if we are parsing an emulator URL.
if (process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR]) {
scheme = 'http';
}

// Don't do any validation here. The caller is responsible for validating the result of parsing.
if (typeof dataURL === 'string') {
// Parse scheme.
Expand Down