Skip to content

Commit

Permalink
Generate CloudEvents in the PubSub Emulator (#3767)
Browse files Browse the repository at this point in the history
Update PubSub Emulator to emit events in CloudEvent format given a trigger whose function signature is `cloudevent`.

Previously, the Functions Emulator considered 2 types of functions - `https` and `background`. With the introduction of [CloudEvents](https://cloud.google.com/functions/docs/writing/cloudevents) in Google Cloud Functions,  we now have 2 types of `background` functions - `legacy` vs `cloudevent`. For example:

```js
/* Legacy Events */
exports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message) => {
  // ...
});

/* CloudEvents */
exports.helloPubSub = functions.v2.pubsub.topic('topic-name').onPublish((cloudevent) => {
  const message = cloudevent.data.message;
});
```

To accommodate, we adopt concept of "function signature" introduced in Google Cloud Function's [Functions Framework](https://github.com/GoogleCloudPlatform/functions-framework-nodejs) to categorize each function trigger as one of `http`, `event`, or`cloudevent`.

When registering a function trigger with the Pubsub emulator, the functions's signature type will be included in the registration request, and the Pubsub emulator will use the info to emit an event in the format expected by the underlying function trigger. We also make minor changes to the Functions Emulator to selectively massage the incoming events in-route to the function trigger.

As an added bonus, we are now able to remove `triggerType` field in the `FunctionsRuntimeArg` as that info is now outdated and not needed when invoking an emulated function.
  • Loading branch information
taeold committed Sep 24, 2021
1 parent 00dcfb4 commit 54c822d
Show file tree
Hide file tree
Showing 14 changed files with 717 additions and 413 deletions.
12 changes: 9 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -85,6 +85,7 @@
"dependencies": {
"@google-cloud/pubsub": "^2.7.0",
"@types/archiver": "^5.1.0",
"JSONStream": "^1.2.1",
"abort-controller": "^3.0.0",
"ajv": "^6.12.6",
"archiver": "^5.0.0",
Expand All @@ -110,7 +111,6 @@
"google-auth-library": "^6.1.3",
"inquirer": "~6.3.1",
"js-yaml": "^3.13.1",
"JSONStream": "^1.2.1",
"jsonwebtoken": "^8.5.1",
"leven": "^3.1.0",
"lodash": "^4.17.21",
Expand Down Expand Up @@ -143,6 +143,7 @@
"ws": "^7.2.3"
},
"devDependencies": {
"@google/events": "^5.1.1",
"@manifoldco/swagger-to-ts": "^2.0.0",
"@types/body-parser": "^1.17.0",
"@types/chai": "^4.2.12",
Expand Down
4 changes: 2 additions & 2 deletions scripts/emulator-tests/functionsEmulator.spec.ts
Expand Up @@ -3,7 +3,7 @@ import * as express from "express";
import * as sinon from "sinon";
import * as supertest from "supertest";

import { EmulatedTriggerType } from "../../src/emulator/functionsEmulatorShared";
import { SignatureType } from "../../src/emulator/functionsEmulatorShared";
import { FunctionsEmulator, InvokeRuntimeOpts } from "../../src/emulator/functionsEmulator";
import { Emulators } from "../../src/emulator/types";
import { RuntimeWorker } from "../../src/emulator/functionsRuntimeWorker";
Expand Down Expand Up @@ -89,7 +89,7 @@ function useFunctions(triggers: () => {}): void {
functionsEmulator.startFunctionRuntime = (
triggerId: string,
targetName: string,
triggerType: EmulatedTriggerType,
triggerType: SignatureType,
proto?: any,
runtimeOpts?: InvokeRuntimeOpts
): RuntimeWorker => {
Expand Down

0 comments on commit 54c822d

Please sign in to comment.