Skip to content
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

Merged
merged 8 commits into from Oct 4, 2022

Conversation

colerogers
Copy link
Contributor

@colerogers colerogers commented Sep 30, 2022

This change tries to make the minimum amount of changes to the functions emulator to support v2 rtdb triggers. Once we get some breathing room in the future, I'd like to refactor how triggers are added and possibly merge some of the deploy/emulator code so we aren't re-building similar logic.

@codecov-commenter
Copy link

codecov-commenter commented Sep 30, 2022

Codecov Report

Base: 55.88% // Head: 55.47% // Decreases project coverage by -0.41% ⚠️

Coverage data is based on head (389d041) compared to base (aec8939).
Patch coverage: 12.90% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5045      +/-   ##
==========================================
- Coverage   55.88%   55.47%   -0.42%     
==========================================
  Files         305      307       +2     
  Lines       20324    20986     +662     
  Branches     4094     4327     +233     
==========================================
+ Hits        11358    11641     +283     
- Misses       8011     8385     +374     
- Partials      955      960       +5     
Impacted Files Coverage Δ
src/emulator/functionsEmulator.ts 6.51% <0.00%> (-0.15%) ⬇️
src/emulator/functionsEmulatorShared.ts 43.35% <42.85%> (+0.74%) ⬆️
src/functions/events/v2.ts 100.00% <100.00%> (ø)
src/deploy/index.ts 25.30% <0.00%> (-4.70%) ⬇️
src/emulator/controller.ts 10.55% <0.00%> (-2.60%) ⬇️
src/emulator/downloadableEmulators.ts 21.16% <0.00%> (-2.15%) ⬇️
src/deploy/functions/params.ts 37.67% <0.00%> (-0.54%) ⬇️
src/deploy/functions/services/index.ts 95.00% <0.00%> (-0.46%) ⬇️
src/emulator/auth/operations.ts 83.21% <0.00%> (-0.27%) ⬇️
... and 9 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@colerogers colerogers marked this pull request as ready for review October 3, 2022 19:44
Copy link
Contributor

@TheIronDev TheIronDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@@ -819,7 +862,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!;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this ! necessary here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We make def.resource string | undefined now


const ref = eventTrigger.eventFilterPathPatterns?.ref;
if (!ref) {
throw new FirebaseError("A database reference must be supplied.");
Copy link
Contributor

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?)

Copy link
Member

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.

Copy link
Contributor Author

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

namespacePattern: instance,
});

const apiPath = "/.settings/functionTriggers.json";
Copy link
Contributor

Choose a reason for hiding this comment

The 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 namespacePattern imply that it's a v2 trigger?

(whatever ends up being true - can we add a comment explaining this?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespacePattern implies that we are using the v2 interface. Under the hood, the ?ns=... query param will be ignored for v2 triggers, but I figured it would be better to show that explicitly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment explaining this


const ref = eventTrigger.eventFilterPathPatterns?.ref;
if (!ref) {
throw new FirebaseError("A database reference must be supplied.");
Copy link
Member

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.

}
if (EventUtils.isBinaryCloudEvent(req)) {
reqBody = EventUtils.extractBinaryCloudEventContext(req);
reqBody.data = req.body;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

@@ -50,10 +58,11 @@ export interface EventSchedule {
}

export interface EventTrigger {
resource: string;
resource: string | undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resource?: string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -1,4 +1,4 @@
export const PUBSUB_PUBLISH_EVENT = "google.cloud.pubsub.topic.v1.messagePublished" as const;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why as const was there in the first place? Was it used as a string literal type somewhere & isn't now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we had it like this, maybe leftover from a different implementation?

@colerogers colerogers enabled auto-merge (squash) October 4, 2022 19:54
@colerogers colerogers merged commit cd737c9 into master Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants