Skip to content

add FIREBASE_DATABASE_EMULATOR_HOST_VAR #596

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 6 commits into from
Jul 19, 2019
Merged
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
12 changes: 12 additions & 0 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import {Database} from '@firebase/database';

import * as validator from '../utils/validator';

/**
* This variable is redefined in the firebase-js-sdk. Before modifying this
* definition, please consult the definition in firebase-js-sdk and ensure that
* they are consistent.
*
* https://github.com/firebase/firebase-js-sdk
*/
export const FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';

/**
* Internals of a Database instance.
Expand Down Expand Up @@ -79,6 +87,10 @@ export class DatabaseService implements FirebaseServiceInterface {
} else if (typeof this.appInternal.options.databaseURL !== 'undefined') {
return this.appInternal.options.databaseURL;
}
const dbEmulatorUrl = process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
if (dbEmulatorUrl) {
return 'http://' + dbEmulatorUrl;
}
throw new FirebaseDatabaseError({
code: 'invalid-argument',
message: 'Can\'t determine Firebase Database URL.',
Expand Down
18 changes: 18 additions & 0 deletions test/unit/firebase-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {Database} from '@firebase/database';
import {InstanceId} from '../../src/instance-id/instance-id';
import {ProjectManagement} from '../../src/project-management/project-management';
import { FirebaseAppError, AppErrorCodes } from '../../src/utils/error';
import { FIREBASE_DATABASE_EMULATOR_HOST_VAR } from '../../src/database/database';

chai.should();
chai.use(sinonChai);
Expand Down Expand Up @@ -434,12 +435,29 @@ describe('FirebaseApp', () => {
});

it('should throw when databaseURL is not set', () => {
delete process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
const app = firebaseNamespace.initializeApp(mocks.appOptionsNoDatabaseUrl, mocks.appName);
expect(() => {
app.database();
}).to.throw('Can\'t determine Firebase Database URL.');
});

it('should use FIREBASE_DATABASE_EMULATOR_HOST when databaseURL not set', () => {
const url = 'localhost.com:9000?ns=test';
process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR] = url;
const app = firebaseNamespace.initializeApp(mocks.appOptionsNoDatabaseUrl, mocks.appName);
const db: Database = app.database();
expect(db.ref().toString()).to.equal('http://localhost.com:9000/');
});

it('should prefer databaseURL when FIREBASE_DATABASE_EMULATOR_HOST set', () => {
const url = 'localhost.com:9000?ns=test';
process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR] = url;
const app = firebaseNamespace.initializeApp(mocks.appOptions, mocks.appName);
const db: Database = app.database();
expect(db.ref().toString()).to.equal('https://databasename.firebaseio.com/');
});

it('should return a cached version of Database on subsequent calls', () => {
const app = firebaseNamespace.initializeApp(mocks.appOptions, mocks.appName);
const db1: Database = app.database();
Expand Down
3 changes: 3 additions & 0 deletions test/unit/firebase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import * as mocks from '../resources/mocks';
import * as firebaseAdmin from '../../src/index';
import {ApplicationDefaultCredential, CertCredential, RefreshTokenCredential} from '../../src/auth/credential';

import {FIREBASE_DATABASE_EMULATOR_HOST_VAR} from '../../src/database/database';

chai.should();
chai.use(chaiAsPromised);

Expand Down Expand Up @@ -168,6 +170,7 @@ describe('Firebase', () => {
});

it('should throw given no databaseURL key when initializing the app', () => {
delete process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
firebaseAdmin.initializeApp(mocks.appOptionsNoDatabaseUrl);

expect(() => {
Expand Down