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
Adds emulator support for v2 rtdb triggers #5045
Changes from all commits
23376cb
41e7a69
181fdc5
810fea0
a6e36dd
15b0f4f
2d1dc4b
49caa0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add functions emulator support for RTDB v2 triggers (#5045). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -589,7 +589,9 @@ export class FunctionsEmulator implements EmulatorInstance { | |
added = await this.addRealtimeDatabaseTrigger( | ||
this.args.projectId, | ||
key, | ||
definition.eventTrigger | ||
definition.eventTrigger, | ||
signature, | ||
definition.region | ||
); | ||
break; | ||
case Constants.SERVICE_PUBSUB: | ||
|
@@ -721,21 +723,12 @@ export class FunctionsEmulator implements EmulatorInstance { | |
} | ||
} | ||
|
||
async addRealtimeDatabaseTrigger( | ||
projectId: string, | ||
key: string, | ||
eventTrigger: EventTrigger | ||
): Promise<boolean> { | ||
const databaseEmu = EmulatorRegistry.get(Emulators.DATABASE); | ||
if (!databaseEmu) { | ||
return false; | ||
} | ||
|
||
const result: string[] | null = DATABASE_PATH_PATTERN.exec(eventTrigger.resource); | ||
private getV1DatabaseApiAttributes(projectId: string, key: string, eventTrigger: EventTrigger) { | ||
const result: string[] | null = DATABASE_PATH_PATTERN.exec(eventTrigger.resource!); | ||
if (result === null || result.length !== 3) { | ||
this.logger.log( | ||
"WARN", | ||
`Event function "${key}" has malformed "resource" member. ` + `${eventTrigger.resource}` | ||
`Event function "${key}" has malformed "resource" member. ` + `${eventTrigger.resource!}` | ||
); | ||
throw new FirebaseError(`Event function ${key} has malformed resource member`); | ||
} | ||
|
@@ -748,24 +741,76 @@ export class FunctionsEmulator implements EmulatorInstance { | |
topic: `projects/${projectId}/topics/${key}`, | ||
}); | ||
|
||
logger.debug(`addRealtimeDatabaseTrigger[${instance}]`, JSON.stringify(bundle)); | ||
|
||
let setTriggersPath = "/.settings/functionTriggers.json"; | ||
let apiPath = "/.settings/functionTriggers.json"; | ||
if (instance !== "") { | ||
setTriggersPath += `?ns=${instance}`; | ||
apiPath += `?ns=${instance}`; | ||
} else { | ||
this.logger.log( | ||
"WARN", | ||
`No project in use. Registering function for sentinel namespace '${Constants.DEFAULT_DATABASE_EMULATOR_NAMESPACE}'` | ||
); | ||
} | ||
|
||
return { bundle, apiPath, instance }; | ||
} | ||
|
||
private getV2DatabaseApiAttributes( | ||
projectId: string, | ||
key: string, | ||
eventTrigger: EventTrigger, | ||
region: string | ||
) { | ||
const instance = | ||
eventTrigger.eventFilters?.instance || eventTrigger.eventFilterPathPatterns?.instance; | ||
if (!instance) { | ||
throw new FirebaseError("A database instance must be supplied."); | ||
} | ||
|
||
const ref = eventTrigger.eventFilterPathPatterns?.ref; | ||
if (!ref) { | ||
throw new FirebaseError("A database reference must be supplied."); | ||
} | ||
|
||
// The 'namespacePattern' determines that we are using the v2 interface | ||
const bundle = JSON.stringify({ | ||
name: `projects/${projectId}/locations/${region}/triggers/${key}`, | ||
path: ref, | ||
event: eventTrigger.eventType, | ||
topic: `projects/${projectId}/topics/${key}`, | ||
namespacePattern: instance, | ||
}); | ||
|
||
// The query parameter '?ns=${instance}' is ignored in v2 | ||
const apiPath = "/.settings/functionTriggers.json"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does difference in how we construct the apiPath for v1 and v2 mean that v2 rtdb emulation only works on the default RTDB instance? or existence of (whatever ends up being true - can we add a comment explaining this?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a comment explaining this |
||
|
||
return { bundle, apiPath, instance }; | ||
} | ||
|
||
async addRealtimeDatabaseTrigger( | ||
projectId: string, | ||
key: string, | ||
eventTrigger: EventTrigger, | ||
signature: SignatureType, | ||
region: string | ||
): Promise<boolean> { | ||
const databaseEmu = EmulatorRegistry.get(Emulators.DATABASE); | ||
if (!databaseEmu) { | ||
return false; | ||
} | ||
|
||
const { bundle, apiPath, instance } = | ||
signature === "cloudevent" | ||
? this.getV2DatabaseApiAttributes(projectId, key, eventTrigger, region) | ||
: this.getV1DatabaseApiAttributes(projectId, key, eventTrigger); | ||
|
||
logger.debug(`addRealtimeDatabaseTrigger[${instance}]`, JSON.stringify(bundle)); | ||
|
||
const client = new Client({ | ||
urlPrefix: `http://${EmulatorRegistry.getInfoHostString(databaseEmu.getInfo())}`, | ||
auth: false, | ||
}); | ||
try { | ||
await client.post(setTriggersPath, bundle, { headers: { Authorization: "Bearer owner" } }); | ||
await client.post(apiPath, bundle, { headers: { Authorization: "Bearer owner" } }); | ||
} catch (err: any) { | ||
this.logger.log("WARN", "Error adding Realtime Database function: " + err); | ||
throw err; | ||
|
@@ -819,7 +864,7 @@ export class FunctionsEmulator implements EmulatorInstance { | |
logger.debug(`addPubsubTrigger`, JSON.stringify({ eventTrigger })); | ||
|
||
// "resource":\"projects/{PROJECT_ID}/topics/{TOPIC_ID}"; | ||
const resource = eventTrigger.resource; | ||
const resource = eventTrigger.resource!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this ! necessary here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We make def.resource |
||
let topic; | ||
if (schedule) { | ||
// In production this topic looks like | ||
|
@@ -852,8 +897,8 @@ export class FunctionsEmulator implements EmulatorInstance { | |
addStorageTrigger(projectId: string, key: string, eventTrigger: EventTrigger): boolean { | ||
logger.debug(`addStorageTrigger`, JSON.stringify({ eventTrigger })); | ||
|
||
const bucket = eventTrigger.resource.startsWith("projects/_/buckets/") | ||
? eventTrigger.resource.split("/")[3] | ||
const bucket = eventTrigger.resource!.startsWith("projects/_/buckets/") | ||
? eventTrigger.resource!.split("/")[3] | ||
: eventTrigger.resource; | ||
const eventTriggerId = `${projectId}:${eventTrigger.eventType}:${bucket}`; | ||
const triggers = this.multicastTriggers[eventTriggerId] || []; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1048,11 +1048,9 @@ async function main(): Promise<void> { | |
case "cloudevent": | ||
const rawBody = (req as RequestWithRawBody).rawBody; | ||
let reqBody = JSON.parse(rawBody.toString()); | ||
if (req.headers["content-type"]?.includes("cloudevent")) { | ||
if (EventUtils.isBinaryCloudEvent(req)) { | ||
reqBody = EventUtils.extractBinaryCloudEventContext(req); | ||
reqBody.data = req.body; | ||
} | ||
if (EventUtils.isBinaryCloudEvent(req)) { | ||
reqBody = EventUtils.extractBinaryCloudEventContext(req); | ||
reqBody.data = req.body; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we actually wanted a third option: check for contenttype to include cloudevent but then throw that structured encoding is not supported if isBinaryCloudEvent is false. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are actually removing the check for content type to include cloudevent since it's not in the spec - https://cloud.google.com/eventarc/docs/workflows/cloudevents#payload-format |
||
} | ||
await processBackground(trigger, reqBody, FUNCTION_SIGNATURE); | ||
res.send({ status: "acknowledged" }); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: What does it mean for these values to not exist (in other words - can these conditions be triggered if users are using the function sdk correctly?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but I appreciate treating this as untrusted input. We could have new SDK authors pop these assertions because they have bugs in their implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, using the Functions SDK as it currently is will never achieve this state