Skip to content

Commit d3e5503

Browse files
committed
Combine two fields, add a test, accept localhost in refFromURL
1 parent 6300d4c commit d3e5503

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

packages/database/src/api/Database.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { fatal } from '../core/util/util';
19-
import { parseRepoInfo } from '../core/util/libs/parser';
19+
import { parseDatabaseURL, parseRepoInfo } from '../core/util/libs/parser';
2020
import { Path } from '../core/util/Path';
2121
import { Reference } from './Reference';
2222
import { Repo } from '../core/Repo';
@@ -156,16 +156,21 @@ export class Database implements FirebaseService {
156156
const parsedURL = parseRepoInfo(url, this.repo_.repoInfo_.nodeAdmin);
157157
validateUrl(apiName, 1, parsedURL);
158158

159-
const repoInfo = parsedURL.repoInfo;
160-
const expectedHost = this.repo_.originalHost;
161-
if (repoInfo.host !== expectedHost) {
159+
const newHost = parsedURL.repoInfo.host;
160+
const originalHost = parseDatabaseURL(this.repo_.productionUrl).host;
161+
const currentHost = this.repo_.repoInfo_.host;
162+
if (newHost !== originalHost && newHost !== currentHost) {
163+
const expected = originalHost === currentHost
164+
? originalHost
165+
: `${originalHost} or ${currentHost}`;
166+
162167
fatal(
163168
apiName +
164169
': Host name does not match the current database: ' +
165170
'(found ' +
166-
repoInfo.host +
171+
newHost +
167172
' but expected ' +
168-
expectedHost +
173+
expected +
169174
')'
170175
);
171176
}

packages/database/src/core/Repo.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ const INTERRUPT_REASON = 'repo_interrupt';
5454
* A connection to a single data repository.
5555
*/
5656
export class Repo {
57-
/** Key for uniquely identifying this repo, used in RepoManager */
58-
readonly key: string;
59-
6057
/** Record of the original host, which does not change even if useEmulator mutates the repo */
61-
readonly originalHost: string;
58+
readonly productionUrl: string;
6259

6360
dataUpdateCount = 0;
6461
private infoSyncTree_: SyncTree;
@@ -91,9 +88,7 @@ export class Repo {
9188
public app: FirebaseApp,
9289
public authTokenProvider_: AuthTokenProvider
9390
) {
94-
// This key is intentionally not updated if RepoInfo is later changed or replaced
95-
this.key = this.repoInfo_.toURLString();
96-
this.originalHost = this.repoInfo_.host;
91+
this.productionUrl = this.repoInfo_.toURLString();
9792
}
9893

9994
start(): void {

packages/database/src/core/RepoManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ export class RepoManager {
176176
deleteRepo(repo: Repo) {
177177
const appRepos = safeGet(this.repos_, repo.app.name);
178178
// This should never happen...
179-
if (!appRepos || safeGet(appRepos, repo.key) !== repo) {
179+
if (!appRepos || safeGet(appRepos, repo.productionUrl) !== repo) {
180180
fatal(
181181
`Database ${repo.app.name}(${repo.repoInfo_}) has already been deleted.`
182182
);
183183
}
184184
repo.interrupt();
185-
delete appRepos[repo.key];
185+
delete appRepos[repo.productionUrl];
186186
}
187187

188188
/**

packages/database/test/database.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,12 @@ describe('Database Tests', () => {
286286
const ref = db.refFromURL(DATABASE_ADDRESS + '/path/to/data');
287287
expect(ref.toString()).to.equal(`http://localhost:1234/path/to/data`);
288288
});
289+
290+
it('refFromURL accepts an emulated ref with useEmulator', () => {
291+
const db = (firebase as any).database();
292+
db.useEmulator('localhost', 1234);
293+
294+
const ref = db.refFromURL('http://localhost:1234/path/to/data');
295+
expect(ref.toString()).to.equal(`http://localhost:1234/path/to/data`);
296+
});
289297
});

0 commit comments

Comments
 (0)