Skip to content

Fixes an emulator issue where snapshot.ref could not point to multiple databases #1339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixes an emulator issue where snapshot.ref could not point to multiple databases (#1339).
5 changes: 1 addition & 4 deletions src/common/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ export class DataSnapshot implements database.DataSnapshot {
instance?: string
) {
const config = firebaseConfig();
if (app?.options?.databaseURL?.startsWith("http:")) {
// In this case we're dealing with an emulator
this.instance = app.options.databaseURL;
} else if (instance) {
if (instance) {
// SDK always supplies instance, but user's unit tests may not
this.instance = instance;
} else if (app) {
Expand Down
11 changes: 9 additions & 2 deletions src/v2/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ export function onChangedOperation<Ref extends string>(
// wrap the handler
const func = (raw: CloudEvent<unknown>) => {
const event = raw as RawRTDBCloudEvent;
const instanceUrl = `https://${event.instance}.${event.firebasedatabasehost}`;
const instanceUrl = getInstance(event);
const params = makeParams(event, pathPattern, instancePattern) as unknown as ParamsOf<Ref>;
const databaseEvent = makeChangedDatabaseEvent(event, instanceUrl, params);
return wrapTraceContext(handler)(databaseEvent);
Expand All @@ -492,7 +492,7 @@ export function onOperation<Ref extends string>(
// wrap the handler
const func = (raw: CloudEvent<unknown>) => {
const event = raw as RawRTDBCloudEvent;
const instanceUrl = `https://${event.instance}.${event.firebasedatabasehost}`;
const instanceUrl = getInstance(event);
const params = makeParams(event, pathPattern, instancePattern) as unknown as ParamsOf<Ref>;
const data = eventType === deletedEventType ? event.data.data : event.data.delta;
const databaseEvent = makeDatabaseEvent(event, data, instanceUrl, params);
Expand All @@ -505,3 +505,10 @@ export function onOperation<Ref extends string>(

return func;
}

function getInstance(event: RawRTDBCloudEvent) {
const emuHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
return emuHost
? `http://${emuHost}/?ns=${event.instance}`
: `https://${event.instance}.${event.firebasedatabasehost}`;
}