Skip to content

Fix multiple database instances when using emulator #4247

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 1 commit into from
Dec 30, 2020
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
5 changes: 5 additions & 0 deletions .changeset/dirty-hotels-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/database': patch
---

Fix issue with multiple database instances when using Realtime Database emulator (#3681)
6 changes: 5 additions & 1 deletion packages/database/src/core/RepoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export class RepoInfo {

/** @return {string} */
toURLString(): string {
return (this.secure ? 'https://' : 'http://') + this.host;
const protocol = this.secure ? 'https://' : 'http://';
const query = this.includeNamespaceInQueryParams
? `?ns=${this.namespace}`
: '';
return `${protocol}${this.host}/${query}`;
}
}
22 changes: 22 additions & 0 deletions packages/database/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,35 @@ describe('Database Tests', () => {
expect(db2.ref().toString()).to.equal('https://foo2.bar.com/');
});

it('Different instances for different URLs (with FIREBASE_DATABASE_EMULATOR_HOST)', () => {
process.env['FIREBASE_DATABASE_EMULATOR_HOST'] = 'localhost:9000';
const db1 = defaultApp.database('http://foo1.bar.com');
const db2 = defaultApp.database('http://foo2.bar.com');
expect(db1.repo_.repoInfo_.toURLString()).to.equal(
'http://localhost:9000/?ns=foo1'
);
expect(db2.repo_.repoInfo_.toURLString()).to.equal(
'http://localhost:9000/?ns=foo2'
);
delete process.env['FIREBASE_DATABASE_EMULATOR_HOST'];
});

it('Cannot use same URL twice', () => {
defaultApp.database('http://foo.bar.com');
expect(() => {
defaultApp.database('http://foo.bar.com/');
}).to.throw(/Database initialized multiple times/i);
});

it('Cannot use same URL twice (with FIREBASE_DATABASE_EMULATOR_HOST)', () => {
process.env['FIREBASE_DATABASE_EMULATOR_HOST'] = 'localhost:9000';
defaultApp.database('http://foo.bar.com');
expect(() => {
defaultApp.database('http://foo.bar.com/');
}).to.throw(/Database initialized multiple times/i);
Comment on lines +175 to +177
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:( That this is banned makes me sad. But obviously unrelated to the PR.

delete process.env['FIREBASE_DATABASE_EMULATOR_HOST'];
});

it('Databases with legacy domain', () => {
expect(() => {
defaultApp.database('http://foo.firebase.com/');
Expand Down