Skip to content

Commit

Permalink
create RepoInfoEmulatorOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
DellaBitta committed Feb 6, 2025
1 parent 1c270b1 commit 9738a5c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
30 changes: 21 additions & 9 deletions packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Provider } from '@firebase/component';
import {
getModularInstance,
createMockUserToken,
// deepEqual,
EmulatorMockTokenOptions,
getDefaultEmulatorHostnameAndPort
} from '@firebase/util';
Expand All @@ -38,7 +39,7 @@ import {
FirebaseAuthTokenProvider
} from '../core/AuthTokenProvider';
import { Repo, repoInterrupt, repoResume, repoStart } from '../core/Repo';
import { RepoInfo } from '../core/RepoInfo';
import { RepoInfo, RepoInfoEmulatorOptions} from '../core/RepoInfo';
import { parseRepoInfo } from '../core/util/libs/parser';
import { newEmptyPath, pathIsEmpty } from '../core/util/Path';
import {
Expand Down Expand Up @@ -84,19 +85,20 @@ let useRestClient = false;
*/
function repoManagerApplyEmulatorSettings(
repo: Repo,
host: string,
port: number,
tokenProvider?: AuthTokenProvider
hostAndPort: string,
emulatorOptions: RepoInfoEmulatorOptions,
tokenProvider?: AuthTokenProvider,
): void {
repo.repoInfo_ = new RepoInfo(
`${host}:${port}`,
hostAndPort,
/* secure= */ false,
repo.repoInfo_.namespace,
repo.repoInfo_.webSocketOnly,
repo.repoInfo_.nodeAdmin,
repo.repoInfo_.persistenceKey,
repo.repoInfo_.includeNamespaceInQueryParams,
/*isUsingEmulator=*/ true
/*isUsingEmulator=*/ true,
emulatorOptions
);

if (tokenProvider) {
Expand Down Expand Up @@ -350,13 +352,23 @@ 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 (
true
// 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) {
Expand All @@ -374,7 +386,7 @@ export function connectDatabaseEmulator(
}

// Modify the repo to apply emulator settings
repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider);
repoManagerApplyEmulatorSettings(repo, hostAndPort, options, tokenProvider);
}

/**
Expand Down
17 changes: 6 additions & 11 deletions packages/database/src/core/RepoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import { LONG_POLLING, WEBSOCKET } from '../realtime/Constants';
import { PersistentStorage } from './storage/storage';
import { each } from './util/util';

export interface RepoInfoEmulatorOptions {
mockUserToken?: string | EmulatorMockTokenOptions;
};

/**
* A class that holds metadata about a Repo object
*/
export class RepoInfo {
private _host: string;
private _domain: string;
private _emulatorOptions: {
mockUserToken?: EmulatorMockTokenOptions | string;
};
internalHost: string;

/**
Expand All @@ -49,11 +50,11 @@ export class RepoInfo {
public readonly nodeAdmin: boolean = false,
public readonly persistenceKey: string = '',
public readonly includeNamespaceInQueryParams: boolean = false,
public readonly isUsingEmulator: boolean = false
public readonly isUsingEmulator: boolean = false,
public readonly emulatorOptions: RepoInfoEmulatorOptions | null = null
) {
this._host = host.toLowerCase();
this._domain = this._host.substr(this._host.indexOf('.') + 1);
this._emulatorOptions = {};
this.internalHost =
(PersistentStorage.get('host:' + host) as string) || this._host;
}
Expand Down Expand Up @@ -82,12 +83,6 @@ export class RepoInfo {
}
}

get emulatorOptions(): {
mockUserToken?: EmulatorMockTokenOptions | string;
} {
return this._emulatorOptions;
}

toString(): string {
let str = this.toURLString();
if (this.persistenceKey) {
Expand Down

0 comments on commit 9738a5c

Please sign in to comment.