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

Add Pub/Sub support + HTTPS testing #925

Merged
merged 2 commits into from
Aug 2, 2021
Merged

Add Pub/Sub support + HTTPS testing #925

merged 2 commits into from
Aug 2, 2021

Conversation

inlined
Copy link
Member

@inlined inlined commented Jul 30, 2021

While adding Pub/Sub support I noticed that there were now v2/ tests.
Added those tests + fixes that they uncovered.

Also fixed issue where 'firebase-functions/logger' wasn't working as
an import in my test project.

While adding Pub/Sub support I noticed that there were now v2/ tests.
Added those tests + fixes that they uncovered.

Also fixed issue where 'firebase-functions/logger' wasn't working as
an import in my test project.
@inlined inlined requested a review from mbleigh July 30, 2021 20:55
@google-cla google-cla bot added the cla: yes label Jul 30, 2021
@@ -22,6 +22,9 @@

/** @internal */
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is going to be a package export, perhaps we should call it core.ts instead of base.ts? Base to me seems like a class that will be extended whereas this is more shared interfaces.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure


/** The resource which published this event. */
source: string;

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't there also be a subject?: string here? Also, have you thought about how we want to handle a replacement for context.params in a v2 world since it won't be in the payload and isn't a part of CloudEvent?

Copy link
Member Author

Choose a reason for hiding this comment

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

RE subject, yes. I was just documenting the fields I saw in prod and subject was missing for P/S events.
RE params, why not event.params?

@@ -44,12 +44,16 @@ export type HttpsHandler = (
request: Request,
response: express.Response
) => void | Promise<void>;
export type CallableHandler = (
data: any,
export type CallableHandler<T> = (
Copy link
Contributor

Choose a reason for hiding this comment

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

We could make this CallableHandler<T,U> and let both the request and response types be specified.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was going to do that in a followup conversation per Chat, but can do it now.

*
* @param data Payload of a Pub/Sub message.
*/
export class Message {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be generic as well, allowing folks to specify onMessagePublished<T>({data: {json: T}})?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm worried that this will affect readability more than it improves ergonomics, but we can try it out.

constructor(data: any) {
this.messageId = data.messageId;
this.data = data.data;
(this.attributes = data.attributes || {}), (this._json = data.json);
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is super weird, what's going on here? I would expect the comma to be a syntax error...

Copy link
Member Author

Choose a reason for hiding this comment

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

My fingers may have run faster than my brain and code complete probably had a hand. In this case, a comma is the same as a semicolon though the parens are necessary due to order of operations. Same with C++.

}

const func = (raw: CloudEvent<unknown>) => {
const messagePublisheData = raw.data as {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing a d at the end of "Published"

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed


func.run = callback;

func.__trigger = 'silencing transpiler';
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added comments


it('should create a complex trigger with appropraite values', () => {
const result = pubsub.onMessagePublished(
{
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should extract out a common "kitchen sink" trigger that can be used across tests...

import { FULL_OPTIONS, FULL_TRIGGER } from "./helpers";

pubsub.onMessagePublished({ ...FULL_OPTIONS, topic: 'topic' });
expect(result.__trigger).to.deep.equal({ ...FULL_TRIGGER, eventTrigger: EVENT_TRIGGER });

Copy link
Member Author

Choose a reason for hiding this comment

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

I can buy it


export function onMessagePublished(
topicOrOptions: string | PubSubOptions,
callback: (event: CloudEvent<MessagePublishedData>) => any | Promise<any>
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer "handler" to "callback" I think

Copy link
Member Author

Choose a reason for hiding this comment

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

Me too

context: CallableContext
) => any | Promise<any>;

export type HttpsFunction = HttpsHandler & { __trigger: unknown };
export interface CallableFunction<T> extends HttpsHandler {
__trigger: unknown;
run(data: T, context: CallableContext): any | Promise<any>;
Copy link
Contributor

Choose a reason for hiding this comment

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

I probably knew this at one point, but what's with .run?

Copy link
Member Author

@inlined inlined left a comment

Choose a reason for hiding this comment

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

Finally someone willing to stand up to me in code reviews! 😄


it('should create a complex trigger with appropraite values', () => {
const result = pubsub.onMessagePublished(
{
Copy link
Member Author

Choose a reason for hiding this comment

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

I can buy it

@@ -22,6 +22,9 @@

/** @internal */
Copy link
Member Author

Choose a reason for hiding this comment

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

Sure


/** The resource which published this event. */
source: string;

Copy link
Member Author

Choose a reason for hiding this comment

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

RE subject, yes. I was just documenting the fields I saw in prod and subject was missing for P/S events.
RE params, why not event.params?

@@ -44,12 +44,16 @@ export type HttpsHandler = (
request: Request,
response: express.Response
) => void | Promise<void>;
export type CallableHandler = (
data: any,
export type CallableHandler<T> = (
Copy link
Member Author

Choose a reason for hiding this comment

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

I was going to do that in a followup conversation per Chat, but can do it now.

*
* @param data Payload of a Pub/Sub message.
*/
export class Message {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm worried that this will affect readability more than it improves ergonomics, but we can try it out.

*/
get json(): any {
if (typeof this._json === 'undefined') {
this._json = JSON.parse(
Copy link
Member Author

Choose a reason for hiding this comment

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

I like it.


export function onMessagePublished(
topicOrOptions: string | PubSubOptions,
callback: (event: CloudEvent<MessagePublishedData>) => any | Promise<any>
Copy link
Member Author

Choose a reason for hiding this comment

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

Me too

} else {
topic = topicOrOptions.topic;
opts = { ...topicOrOptions };
delete (opts as any).topic;
Copy link
Member Author

Choose a reason for hiding this comment

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

I could equally easily just say opts = topicOrOptions. optionsToTriggerAnnotations will skip over anything that's unknown. I definitely don't want that function altering its parameters though. I just thought here that clone = {... original }; delete clone.unwanted was a fine way of saying _.omit(original, "unwanted") and removing the unwanted field is just defensive programming.

}

const func = (raw: CloudEvent<unknown>) => {
const messagePublisheData = raw.data as {
Copy link
Member Author

Choose a reason for hiding this comment

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

fixed


func.run = callback;

func.__trigger = 'silencing transpiler';
Copy link
Member Author

Choose a reason for hiding this comment

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

Added comments

@inlined inlined merged commit 3d70007 into master Aug 2, 2021
@inlined inlined deleted the inlined.pubsub branch August 11, 2021 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants