Skip to content

Commit 6300d4c

Browse files
committed
Fix refFromURL
1 parent bfbdaac commit 6300d4c

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

packages/database/src/api/Database.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class Database implements FirebaseService {
102102
* @param port the emulator port (ex: 8080)
103103
*/
104104
useEmulator(host: string, port: number): void {
105+
this.checkDeleted_('useEmulator');
105106
if (this.instanceStarted_) {
106107
fatal(
107108
'Cannot call useEmulator() after instance has already been initialized.'
@@ -156,14 +157,15 @@ export class Database implements FirebaseService {
156157
validateUrl(apiName, 1, parsedURL);
157158

158159
const repoInfo = parsedURL.repoInfo;
159-
if (repoInfo.host !== this.repo_.repoInfo_.host) {
160+
const expectedHost = this.repo_.originalHost;
161+
if (repoInfo.host !== expectedHost) {
160162
fatal(
161163
apiName +
162164
': Host name does not match the current database: ' +
163165
'(found ' +
164166
repoInfo.host +
165167
' but expected ' +
166-
(this.repo_.repoInfo_ as RepoInfo).host +
168+
expectedHost +
167169
')'
168170
);
169171
}
@@ -175,7 +177,7 @@ export class Database implements FirebaseService {
175177
* @param {string} apiName
176178
*/
177179
private checkDeleted_(apiName: string) {
178-
if (this.repo_ === null) {
180+
if (this.repoInternal_ === null) {
179181
fatal('Cannot call ' + apiName + ' on a deleted database.');
180182
}
181183
}

packages/database/src/core/Repo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export class Repo {
5757
/** Key for uniquely identifying this repo, used in RepoManager */
5858
readonly key: string;
5959

60+
/** Record of the original host, which does not change even if useEmulator mutates the repo */
61+
readonly originalHost: string;
62+
6063
dataUpdateCount = 0;
6164
private infoSyncTree_: SyncTree;
6265
private serverSyncTree_: SyncTree;
@@ -90,6 +93,7 @@ export class Repo {
9093
) {
9194
// This key is intentionally not updated if RepoInfo is later changed or replaced
9295
this.key = this.repoInfo_.toURLString();
96+
this.originalHost = this.repoInfo_.host;
9397
}
9498

9599
start(): void {

packages/database/test/database.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,12 @@ describe('Database Tests', () => {
278278
db.useEmulator('localhost', 1234);
279279
}).to.throw(/Cannot call useEmulator/);
280280
});
281+
282+
it('refFromURL returns an emulated ref with useEmulator', () => {
283+
const db = (firebase as any).database();
284+
db.useEmulator('localhost', 1234);
285+
286+
const ref = db.refFromURL(DATABASE_ADDRESS + '/path/to/data');
287+
expect(ref.toString()).to.equal(`http://localhost:1234/path/to/data`);
288+
});
281289
});

0 commit comments

Comments
 (0)