Skip to content

Commit 14ef1bc

Browse files
committed
Added ssl checks for RTDB
1 parent 248f654 commit 14ef1bc

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/database-compat/test/database.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,22 @@ describe('Database Tests', () => {
292292
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.false;
293293
});
294294

295+
it('uses ssl when useEmulator is called with https is specified', () => {
296+
const db = firebase.database();
297+
db.useEmulator('https://localhost', 80);
298+
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.true;
299+
expect((db as any)._delegate._repo.repoInfo_.host).to.equal('localhost:80');
300+
expect((db as any)._delegate._repo.repoInfo_.secure).to.be.true;
301+
});
302+
303+
it('uses ssl when useEmulator is called with wss is specified', () => {
304+
const db = firebase.database();
305+
db.useEmulator('wss://localhost', 80);
306+
expect((db as any)._delegate._repo.repoInfo_.isUsingEmulator).to.be.true;
307+
expect((db as any)._delegate._repo.repoInfo_.host).to.equal('localhost:80');
308+
expect((db as any)._delegate._repo.repoInfo_.secure).to.be.true;
309+
});
310+
295311
it('cannot call useEmulator after use', () => {
296312
const db = (firebase as any).database();
297313

packages/database/src/api/Database.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,19 @@ function repoManagerApplyEmulatorSettings(
8989
emulatorOptions: RepoInfoEmulatorOptions,
9090
tokenProvider?: AuthTokenProvider
9191
): void {
92+
let ssl = false;
93+
let finalHost = hostAndPort;
94+
if (/^https:\/\//.test(finalHost)) {
95+
ssl = true;
96+
finalHost = finalHost.substring(8);
97+
}
98+
if (/^wss:\/\//.test(finalHost)) {
99+
ssl = true;
100+
finalHost = finalHost.substring(6);
101+
}
92102
repo.repoInfo_ = new RepoInfo(
93-
hostAndPort,
94-
/* secure= */ false,
103+
finalHost,
104+
/* secure= */ ssl,
95105
repo.repoInfo_.namespace,
96106
repo.repoInfo_.webSocketOnly,
97107
repo.repoInfo_.nodeAdmin,
@@ -352,6 +362,7 @@ export function connectDatabaseEmulator(
352362
): void {
353363
db = getModularInstance(db);
354364
db._checkNotDeleted('useEmulator');
365+
355366
const hostAndPort = `${host}:${port}`;
356367
const repo = db._repoInternal;
357368
if (db._instanceStarted) {

0 commit comments

Comments
 (0)