diff --git a/packages/database/src/api/Database.ts b/packages/database/src/api/Database.ts index 72ae85c08a1..17f59e6da26 100644 --- a/packages/database/src/api/Database.ts +++ b/packages/database/src/api/Database.ts @@ -27,6 +27,7 @@ import { Provider } from '@firebase/component'; import { getModularInstance, createMockUserToken, + //deepEqual, DEDB replace EmulatorMockTokenOptions, getDefaultEmulatorHostnameAndPort } from '@firebase/util'; @@ -85,11 +86,12 @@ let useRestClient = false; function repoManagerApplyEmulatorSettings( repo: Repo, host: string, - port: number, + port: number, // DEDB remove tokenProvider?: AuthTokenProvider ): void { repo.repoInfo_ = new RepoInfo( - `${host}:${port}`, + //host, DDB replace + `${host}:${port}`, // DEDB remove /* secure= */ false, repo.repoInfo_.namespace, repo.repoInfo_.webSocketOnly, @@ -350,13 +352,24 @@ export function connectDatabaseEmulator( ): void { db = getModularInstance(db); db._checkNotDeleted('useEmulator'); + const hostAndPort = `${host}:${port}`; + const repo = db._repoInternal; + /* if (db._instanceStarted) { + // If the instance has already been started, then silenty fail if this function is called again + // with the same parameters. If the parameters differ then assert. + if ( + hostAndPort === db._repoInternal.repoInfo_.host && + deepEqual(options, repo.repoInfo_.emulatorOptions) + ) { + return; + } fatal( - 'Cannot call useEmulator() after instance has already been initialized.' + 'connectDatabaseEmulator() cannot alter the emulator configuration after the database instance has started.' ); } + */ - const repo = db._repoInternal; let tokenProvider: EmulatorTokenProvider | undefined = undefined; if (repo.repoInfo_.nodeAdmin) { if (options.mockUserToken) { @@ -374,7 +387,9 @@ export function connectDatabaseEmulator( } // Modify the repo to apply emulator settings - repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider); + //repoManagerApplyEmulatorSettings(repo, hostAndPort, tokenProvider); // DDB Replace + repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider); // DDB Remove + } /** diff --git a/packages/database/src/core/RepoInfo.ts b/packages/database/src/core/RepoInfo.ts index 9d4c1abe36b..53da592cf49 100644 --- a/packages/database/src/core/RepoInfo.ts +++ b/packages/database/src/core/RepoInfo.ts @@ -15,7 +15,10 @@ * limitations under the License. */ -import { assert } from '@firebase/util'; +import { + assert, + //EmulatorMockTokenOptions //DEDB replace +} from '@firebase/util'; import { LONG_POLLING, WEBSOCKET } from '../realtime/Constants'; @@ -28,6 +31,12 @@ import { each } from './util/util'; export class RepoInfo { private _host: string; private _domain: string; + /* + //DEDB replace + private _emulatorOptions: { + mockUserToken?: EmulatorMockTokenOptions | string; + }; + */ internalHost: string; /** @@ -50,6 +59,7 @@ export class RepoInfo { ) { this._host = host.toLowerCase(); this._domain = this._host.substr(this._host.indexOf('.') + 1); + // this._emulatorOptions = {}; //DEDB replace this.internalHost = (PersistentStorage.get('host:' + host) as string) || this._host; } @@ -78,6 +88,15 @@ export class RepoInfo { } } + /* + //DEDB replace + get emulatorOptions(): { + mockUserToken?: EmulatorMockTokenOptions | string; + } { + return this._emulatorOptions; + } + */ + toString(): string { let str = this.toURLString(); if (this.persistenceKey) { diff --git a/packages/database/test/exp/integration.test.ts b/packages/database/test/exp/integration.test.ts index adf5094f222..33ceb5032b0 100644 --- a/packages/database/test/exp/integration.test.ts +++ b/packages/database/test/exp/integration.test.ts @@ -20,6 +20,7 @@ import { Deferred } from '@firebase/util'; import { expect, use } from 'chai'; import chaiAsPromised from 'chai-as-promised'; +//import { connectDatabaseEmulator } from '../../src/api/Database'; import { child, get, @@ -46,8 +47,10 @@ import { EventAccumulatorFactory } from '../helpers/EventAccumulator'; import { DATABASE_ADDRESS, DATABASE_URL, + //EMULATOR_PORT, getFreshRepo, getRWRefs, + //isEmulatorActive, waitFor, waitUntil, writeAndValidate @@ -138,6 +141,47 @@ describe('Database@exp Tests', () => { unsubscribe(); }); + /*it('can connected to emulator', async () => { + if (isEmulatorActive()) { + const db = getDatabase(defaultApp); + connectDatabaseEmulator(db, 'localhost', parseInt(EMULATOR_PORT, 10)); + await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`)); + } + }); + + it('can chnage emulator config before network operations', async () => { + if (isEmulatorActive()) { + const db = getDatabase(defaultApp); + const port = parseInt(EMULATOR_PORT, 10); + connectDatabaseEmulator(db, 'localhost', port + 1); + connectDatabaseEmulator(db, 'localhost', port); + await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`)); + } + }); + + it('can connected to emulator after network operations with same parameters', async () => { + if (isEmulatorActive()) { + const db = getDatabase(defaultApp); + const port = parseInt(EMULATOR_PORT, 10); + connectDatabaseEmulator(db, 'localhost', port); + await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`)); + connectDatabaseEmulator(db, 'localhost', port); + } + }); + + it('cannot connect to emulator after network operations with different parameters', async () => { + if (isEmulatorActive()) { + const db = getDatabase(defaultApp); + const port = parseInt(EMULATOR_PORT, 10); + connectDatabaseEmulator(db, 'localhost', port); + await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`)); + expect(() => { + connectDatabaseEmulator(db, 'localhost', 9001); + }).to.throw(); + } + }); + */ + it('can properly handle unknown deep merges', async () => { // Note: This test requires `testIndex` to be added as an index. // Please run `yarn test:setup` to ensure that this gets added. diff --git a/packages/database/test/helpers/util.ts b/packages/database/test/helpers/util.ts index 73eb04a8c5e..883b6632b38 100644 --- a/packages/database/test/helpers/util.ts +++ b/packages/database/test/helpers/util.ts @@ -33,7 +33,7 @@ import { EventAccumulator } from './EventAccumulator'; // eslint-disable-next-line @typescript-eslint/no-require-imports export const TEST_PROJECT = require('../../../../config/project.json'); -const EMULATOR_PORT = process.env.RTDB_EMULATOR_PORT; +export const EMULATOR_PORT = process.env.RTDB_EMULATOR_PORT; const EMULATOR_NAMESPACE = process.env.RTDB_EMULATOR_NAMESPACE; const USE_EMULATOR = !!EMULATOR_PORT; @@ -143,3 +143,7 @@ export async function waitUntil(cb: () => boolean, maxRetries = 5) { } }); } + +export function isEmulatorActive(): boolean { + return USE_EMULATOR; +}