Skip to content

Commit 5eceefd

Browse files
authored
Merge pull request #10 from OpenFunction/feature/sync-trigger-async
feat: enable HTTP function trigger async functions
2 parents 9089abd + 8284d9f commit 5eceefd

File tree

16 files changed

+1115
-98
lines changed

16 files changed

+1115
-98
lines changed
File renamed without changes.

CONTRIBUTING.md

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,44 @@
11
# How to Contribute
22

3-
We'd love to accept your patches and contributions to this project. There are
4-
just a few small guidelines you need to follow.
3+
We'd love to accept your patches and contributions to this project. There are a few small guidelines you need to follow.
54

65
## Contributor License Agreement
76

8-
Contributions to this project must be accompanied by a Contributor License
9-
Agreement. You (or your employer) retain the copyright to your contribution;
10-
this simply gives us permission to use and redistribute your contributions as
11-
part of the project. Head over to <https://cla.developers.google.com/> to see
12-
your current agreements on file or to sign a new one.
7+
Contributions to this project must be accompanied by a Developer Certificate of Origin (DCO). You (or your employer) retain the copyright to your contribution; this gives us permission to use and redistribute your contributions as part of the project. Head over to <https://developercertificate.org/> to see your current agreements on file or to sign a new one.
138

14-
You generally only need to submit a CLA once, so if you've already submitted one
15-
(even if it was for a different project), you probably don't need to do it
16-
again.
9+
You only need to submit a DTO once, so if you've already submitted one (even if it was for a different project), you probably don't need to do it again.
1710

1811
## Code reviews
1912

20-
All submissions, including submissions by project members, require review. We
21-
use GitHub pull requests for this purpose. Consult
22-
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
23-
information on using pull requests.
13+
All submissions, including submissions by project members, require review. We use GitHub pull requests for this purpose. Consult [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more information on using pull requests.
2414

2515
## Testing
2616

2717
### Unit Tests
2818

2919
All pull requests should have an associated test to ensure foward compatibility.
3020

21+
> Make sure you have installed [Dapr](https://dapr.io/) before running unit tests, check out [Install Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/) for more details
22+
3123
To run an individual test, you can run a command such as the following:
3224

33-
```
25+
```sh
3426
npm run test -- -g 'loading function'
3527
```
3628

37-
### Conformance Tests
38-
39-
To run the conformance tests, first install Go 1.16+, then run the tests:
40-
41-
```
42-
npm run conformance
43-
```
44-
4529
### Manual Testing
4630

47-
When developing a feature locally, you can install a local version of the Functions Framework
48-
using `npm link`. First compile your local clone of the Functions Framework:
31+
When developing a feature locally, you can install a local version of the Functions Framework using `npm link`. First compile your local clone of the Functions Framework:
4932

5033
> You'll need to install typescript first by: `npm install typescript --save-dev`
5134
52-
```
35+
```sh
5336
npx tsc
5437
```
5538

5639
Then link the Functions Framework using `npm link`.
5740

58-
```
41+
```sh
5942
npm link
6043
```
6144

@@ -65,14 +48,15 @@ You can then run the Functions Framework locally using `functions-framework`.
6548

6649
This module is published using Release Please. When you merge a release PR, the npm package will be automatically published.
6750

68-
```shell
51+
```sh
6952
# Login to npm registry, contact repo admin for https://www.npmjs.com/ user name and password
7053
npm login
7154
# First run a dry run to find out errors
7255
npm publish ./ --access public --dry-run
7356
# Then publish the package
7457
npm publish --access public
7558
```
59+
7660
### Reverting a Publish
7761

7862
If the release process fails, you can revert the publish by running the following (i.e. unpublishing `1.10.0`):
@@ -101,5 +85,4 @@ The docs will be generated in [`docs/generated/`](docs/generated/).
10185

10286
## Community Guidelines
10387

104-
This project follows [Google's Open Source Community
105-
Guidelines](https://opensource.google.com/conduct/).
88+
This project follows [CNCF openness guidelines](https://www.cncf.io/blog/2019/06/11/cncf-openness-guidelines/).

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ ignored.
168168
| ------------------ | ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
169169
| `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080` |
170170
| `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function` |
171-
| `--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http` or `event` or `cloudevent` |
171+
| `--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http` or `event` or `cloudevent` or `openfunction` |
172172
| `--source` | `FUNCTION_SOURCE` | The path to the directory of your function. Default: `cwd` (the current working directory) |
173173

174174
You can set command-line flags in your `package.json` via the `start` script.
@@ -196,7 +196,52 @@ Dapr bindings allows you to trigger your applications or services with events co
196196
197197
Asynchronous function introduces Dapr pub/sub to provide a platform-agnostic API to send and receive messages. A typical use case is that you can leverage synchronous functions to receive an event in plain JSON or Cloud Events format, and then send the received event to a Dapr output binding or pub/sub component, most likely a message queue (e.g. Kafka, NATS Streaming, GCP PubSub, MQTT). Finally, the asynchronous function could be triggered from the message queue.
198198
199-
More details would be brought up to you in some quickstart samples, stay tuned.
199+
Async function use below function signature which is quite difference from that of Express style sync function:
200+
201+
```js
202+
function (ctx, data) {}
203+
```
204+
205+
* `ctx`: OpenFunction [context](https://github.com/OpenFunction/functions-framework-nodejs/blob/master/src/openfunction/function_context.ts) object
206+
* `ctx.send(payload, output?)`: Send `payload` to all or one specific `output` of Dapr Output [Binding](https://docs.dapr.io/reference/components-reference/supported-bindings/) or Pub [Broker](https://docs.dapr.io/reference/components-reference/supported-pubsub/)
207+
* Notice that `ctx.send` CAN be invoked where necessary, when you have certain outgoing data to send
208+
* `data`: Data recieved from Dapr Input Binding or Sub Broker
209+
210+
For more details about async function and demo, please check out our [Node.js Async Function Quickstart](https://openfunction-talks.netlify.app/2022/202-node-async/).
211+
212+
#### HTTP Trigger Async Function
213+
214+
Sync functions is triggered by HTTP request, so Dapr is not used in sync function input. Whenever there are functions output requirements, sync functions can also send output to Dapr output binding or pubsub components.
215+
216+
Here is another function sample:
217+
218+
* Users send a HTTP request to a [Knative Sync function](https://github.com/OpenFunction/samples/tree/main/functions/knative/with-output-binding).
219+
* This sync function handles the request and then send its output to Kafka through a Dapr Kafka output binding or pubsub component.
220+
* An [async function](https://github.com/OpenFunction/samples/tree/main/functions/async/bindings/kafka-input) is then triggered by this output event in Kafka (through a Dapr Kafka input binding or pubsub component)
221+
222+
![HTTP Trigger Async Function](https://raw.githubusercontent.com/OpenFunction/samples/main/images/knative-dapr.png)
223+
224+
Node.js Functions Framework also supports such use case, you can switch Express function signature to typical async style as below example indicates:
225+
226+
```js
227+
async function tryKnativeAsync(ctx, data) {
228+
// Receive and handle data from HTTP request's body
229+
console.log('Function should receive request: %o', data);
230+
231+
// Send output in async way via Dapr
232+
await ctx.send(data);
233+
234+
// Use `ctx.res` object to deal with HTTP response
235+
ctx.res.send(data);
236+
```
237+
238+
Remember that you also need set command-line flags `--signature-type=openfunction`, for example in your `package.json` via the `start` script:
239+
240+
```js
241+
"scripts": {
242+
"start": "functions-framework --signature-type=openfunction --target=tryKnativeAsync"
243+
}
244+
```
200245
201246
### Google Cloud Functions
202247
@@ -243,5 +288,3 @@ Contributions to this library are welcome and encouraged. See
243288
[ff_node_unit_link]: https://github.com/openfunction/functions-framework-nodejs/actions?query=workflow%3A"Node.js+Unit+CI"
244289
[ff_node_lint_img]: https://github.com/openfunction/functions-framework-nodejs/workflows/Node.js%20Lint%20CI/badge.svg
245290
[ff_node_lint_link]: https://github.com/openfunction/functions-framework-nodejs/actions?query=workflow%3A"Node.js+Lint+CI"
246-
[ff_node_conformance_img]: https://github.com/openfunction/functions-framework-nodejs/workflows/Node.js%20Conformance%20CI/badge.svg
247-
[ff_node_conformance_link]: https://github.com/openfunction/functions-framework-nodejs/actions?query=workflow%3A"Node.js+Conformance+CI"

0 commit comments

Comments
 (0)