Skip to content

Commit 48fb170

Browse files
authored
Merge cf00ae3 into 5250e80
2 parents 5250e80 + cf00ae3 commit 48fb170

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

packages/database/src/api/Database.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Provider } from '@firebase/component';
2727
import {
2828
getModularInstance,
2929
createMockUserToken,
30+
//deepEqual, DEDB replace
3031
EmulatorMockTokenOptions,
3132
getDefaultEmulatorHostnameAndPort
3233
} from '@firebase/util';
@@ -85,11 +86,12 @@ let useRestClient = false;
8586
function repoManagerApplyEmulatorSettings(
8687
repo: Repo,
8788
host: string,
88-
port: number,
89+
port: number, // DEDB remove
8990
tokenProvider?: AuthTokenProvider
9091
): void {
9192
repo.repoInfo_ = new RepoInfo(
92-
`${host}:${port}`,
93+
//host, DDB replace
94+
`${host}:${port}`, // DEDB remove
9395
/* secure= */ false,
9496
repo.repoInfo_.namespace,
9597
repo.repoInfo_.webSocketOnly,
@@ -350,13 +352,24 @@ export function connectDatabaseEmulator(
350352
): void {
351353
db = getModularInstance(db);
352354
db._checkNotDeleted('useEmulator');
355+
const hostAndPort = `${host}:${port}`;
356+
const repo = db._repoInternal;
357+
/*
353358
if (db._instanceStarted) {
359+
// If the instance has already been started, then silenty fail if this function is called again
360+
// with the same parameters. If the parameters differ then assert.
361+
if (
362+
hostAndPort === db._repoInternal.repoInfo_.host &&
363+
deepEqual(options, repo.repoInfo_.emulatorOptions)
364+
) {
365+
return;
366+
}
354367
fatal(
355-
'Cannot call useEmulator() after instance has already been initialized.'
368+
'connectDatabaseEmulator() cannot alter the emulator configuration after the database instance has started.'
356369
);
357370
}
371+
*/
358372

359-
const repo = db._repoInternal;
360373
let tokenProvider: EmulatorTokenProvider | undefined = undefined;
361374
if (repo.repoInfo_.nodeAdmin) {
362375
if (options.mockUserToken) {
@@ -374,7 +387,9 @@ export function connectDatabaseEmulator(
374387
}
375388

376389
// Modify the repo to apply emulator settings
377-
repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider);
390+
//repoManagerApplyEmulatorSettings(repo, hostAndPort, tokenProvider); // DDB Replace
391+
repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider); // DDB Remove
392+
378393
}
379394

380395
/**

packages/database/src/core/RepoInfo.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { assert } from '@firebase/util';
18+
import {
19+
assert,
20+
//EmulatorMockTokenOptions //DEDB replace
21+
} from '@firebase/util';
1922

2023
import { LONG_POLLING, WEBSOCKET } from '../realtime/Constants';
2124

@@ -28,6 +31,12 @@ import { each } from './util/util';
2831
export class RepoInfo {
2932
private _host: string;
3033
private _domain: string;
34+
/*
35+
//DEDB replace
36+
private _emulatorOptions: {
37+
mockUserToken?: EmulatorMockTokenOptions | string;
38+
};
39+
*/
3140
internalHost: string;
3241

3342
/**
@@ -50,6 +59,7 @@ export class RepoInfo {
5059
) {
5160
this._host = host.toLowerCase();
5261
this._domain = this._host.substr(this._host.indexOf('.') + 1);
62+
// this._emulatorOptions = {}; //DEDB replace
5363
this.internalHost =
5464
(PersistentStorage.get('host:' + host) as string) || this._host;
5565
}
@@ -78,6 +88,15 @@ export class RepoInfo {
7888
}
7989
}
8090

91+
/*
92+
//DEDB replace
93+
get emulatorOptions(): {
94+
mockUserToken?: EmulatorMockTokenOptions | string;
95+
} {
96+
return this._emulatorOptions;
97+
}
98+
*/
99+
81100
toString(): string {
82101
let str = this.toURLString();
83102
if (this.persistenceKey) {

packages/database/test/exp/integration.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Deferred } from '@firebase/util';
2020
import { expect, use } from 'chai';
2121
import chaiAsPromised from 'chai-as-promised';
2222

23+
//import { connectDatabaseEmulator } from '../../src/api/Database';
2324
import {
2425
child,
2526
get,
@@ -46,8 +47,10 @@ import { EventAccumulatorFactory } from '../helpers/EventAccumulator';
4647
import {
4748
DATABASE_ADDRESS,
4849
DATABASE_URL,
50+
//EMULATOR_PORT,
4951
getFreshRepo,
5052
getRWRefs,
53+
//isEmulatorActive,
5154
waitFor,
5255
waitUntil,
5356
writeAndValidate
@@ -138,6 +141,47 @@ describe('Database@exp Tests', () => {
138141
unsubscribe();
139142
});
140143

144+
/*it('can connected to emulator', async () => {
145+
if (isEmulatorActive()) {
146+
const db = getDatabase(defaultApp);
147+
connectDatabaseEmulator(db, 'localhost', parseInt(EMULATOR_PORT, 10));
148+
await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`));
149+
}
150+
});
151+
152+
it('can chnage emulator config before network operations', async () => {
153+
if (isEmulatorActive()) {
154+
const db = getDatabase(defaultApp);
155+
const port = parseInt(EMULATOR_PORT, 10);
156+
connectDatabaseEmulator(db, 'localhost', port + 1);
157+
connectDatabaseEmulator(db, 'localhost', port);
158+
await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`));
159+
}
160+
});
161+
162+
it('can connected to emulator after network operations with same parameters', async () => {
163+
if (isEmulatorActive()) {
164+
const db = getDatabase(defaultApp);
165+
const port = parseInt(EMULATOR_PORT, 10);
166+
connectDatabaseEmulator(db, 'localhost', port);
167+
await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`));
168+
connectDatabaseEmulator(db, 'localhost', port);
169+
}
170+
});
171+
172+
it('cannot connect to emulator after network operations with different parameters', async () => {
173+
if (isEmulatorActive()) {
174+
const db = getDatabase(defaultApp);
175+
const port = parseInt(EMULATOR_PORT, 10);
176+
connectDatabaseEmulator(db, 'localhost', port);
177+
await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`));
178+
expect(() => {
179+
connectDatabaseEmulator(db, 'localhost', 9001);
180+
}).to.throw();
181+
}
182+
});
183+
*/
184+
141185
it('can properly handle unknown deep merges', async () => {
142186
// Note: This test requires `testIndex` to be added as an index.
143187
// Please run `yarn test:setup` to ensure that this gets added.

packages/database/test/helpers/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { EventAccumulator } from './EventAccumulator';
3333

3434
// eslint-disable-next-line @typescript-eslint/no-require-imports
3535
export const TEST_PROJECT = require('../../../../config/project.json');
36-
const EMULATOR_PORT = process.env.RTDB_EMULATOR_PORT;
36+
export const EMULATOR_PORT = process.env.RTDB_EMULATOR_PORT;
3737
const EMULATOR_NAMESPACE = process.env.RTDB_EMULATOR_NAMESPACE;
3838
const USE_EMULATOR = !!EMULATOR_PORT;
3939

@@ -143,3 +143,7 @@ export async function waitUntil(cb: () => boolean, maxRetries = 5) {
143143
}
144144
});
145145
}
146+
147+
export function isEmulatorActive(): boolean {
148+
return USE_EMULATOR;
149+
}

0 commit comments

Comments
 (0)