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

Prevented hostname from being changed when using BrowserPollConnection w/ emulator #6912

Merged
merged 11 commits into from
Jan 18, 2023
6 changes: 6 additions & 0 deletions .changeset/fresh-experts-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/database-compat": patch
"@firebase/database": patch
---

Fixed issue where hostname set by `connectDatabaseEmulator` was being overridden by longpolling response
10 changes: 9 additions & 1 deletion packages/database-compat/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,26 @@ describe('Database Tests', () => {
});

it('refFromURL() validates argument', () => {
// TODO: Remove all any references
const db = (firebase as any).database();
expect(() => {
const ref = (db as any).refFromURL();
}).to.throw(/Expects at least 1/);
});

it('can call useEmulator before use', () => {
const db = (firebase as any).database();
const db = firebase.database();
db.useEmulator('localhost', 1234);
// Cast as any as _delegate isn't a public property
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.true;
expect(db.ref().toString()).to.equal('http://localhost:1234/');
});

it('initializes usingEmulator to false before use', () => {
const db = firebase.database();
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.false;
});

it('cannot call useEmulator after use', () => {
const db = (firebase as any).database();

Expand Down
3 changes: 2 additions & 1 deletion packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ function repoManagerApplyEmulatorSettings(
repo.repoInfo_.webSocketOnly,
repo.repoInfo_.nodeAdmin,
repo.repoInfo_.persistenceKey,
repo.repoInfo_.includeNamespaceInQueryParams
repo.repoInfo_.includeNamespaceInQueryParams,
/*isUsingEmulator=*/ true
);

if (tokenProvider) {
Expand Down
3 changes: 2 additions & 1 deletion packages/database/src/core/RepoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export class RepoInfo {
public readonly webSocketOnly: boolean,
public readonly nodeAdmin: boolean = false,
public readonly persistenceKey: string = '',
public readonly includeNamespaceInQueryParams: boolean = false
public readonly includeNamespaceInQueryParams: boolean = false,
public readonly isUsingEmulator: boolean = false
) {
this._host = host.toLowerCase();
this._domain = this._host.substr(this._host.indexOf('.') + 1);
Expand Down
13 changes: 9 additions & 4 deletions packages/database/src/realtime/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,19 @@ export class Connection {
if (MESSAGE_DATA in controlData) {
const payload = controlData[MESSAGE_DATA];
if (cmd === SERVER_HELLO) {
this.onHandshake_(
payload as {
const handshakePayload = {
...(payload as {
ts: number;
v: string;
h: string;
s: string;
}
);
})
};
if (this.repoInfo_.isUsingEmulator) {
// Upon connecting, the emulator will pass the hostname that it's aware of, but we prefer the user's set hostname via `connectDatabaseEmulator` over what the emulator passes.
handshakePayload.h = this.repoInfo_.host;
maneesht marked this conversation as resolved.
Show resolved Hide resolved
}
this.onHandshake_(handshakePayload);
} else if (cmd === END_TRANSMISSION) {
this.log_('recvd end transmission on primary');
this.rx_ = this.secondaryConn_;
Expand Down