Skip to content

Commit 7451194

Browse files
authored
feat: introduce CloudEvent data as first parameter for event functions (#172)
The 0.4.0 version of faas-js-runtime extracts the CloudEvent data from an incoming event and provides that as the first parameter when invoking a function which receives a CloudEvent. This commit bumps to that version as well as improves the overall readability and code documentation for the Node.js CloudEvent function. Signed-off-by: Lance Ball <lball@redhat.com>
1 parent 68351bd commit 7451194

File tree

9 files changed

+302
-148
lines changed

9 files changed

+302
-148
lines changed

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -1940,8 +1940,6 @@ knative.dev/caching v0.0.0-20190719140829-2032732871ff/go.mod h1:dHXFU6CGlLlbzaW
19401940
knative.dev/caching v0.0.0-20200116200605-67bca2c83dfa/go.mod h1:dHXFU6CGlLlbzaWc32g80cR92iuBSpsslDNBWI8C7eg=
19411941
knative.dev/caching v0.0.0-20200811171106-48c335fed9c8/go.mod h1:XonDcFC2DLSWP71f2y7oYnXUko5d5HsJRnZtkp0wY7g=
19421942
knative.dev/caching v0.0.0-20200831163411-f6154e7455e2/go.mod h1:jG64HWkBQmFlY8dk1CH884oHtaT/savnqxcZC8vBnvk=
1943-
knative.dev/client v0.17.0 h1:RR+eYVYGy6+LQ44wZcoNuUC6j2Ril7v954ffyK76On8=
1944-
knative.dev/client v0.17.0/go.mod h1:62s+OqEJ8mBSkP6c3ZmPB3jl1jmOyiKtTPwJUBXJQqY=
19451943
knative.dev/client v0.17.2 h1:1aRHQgw0D+FGqZ6Rhpq2nUntBjgc2B2s/HOFoVqoOw4=
19461944
knative.dev/client v0.17.2/go.mod h1:62s+OqEJ8mBSkP6c3ZmPB3jl1jmOyiKtTPwJUBXJQqY=
19471945
knative.dev/eventing v0.17.0 h1:yGJuwN+jWQPCf4XuL44vAjlemLHnbLTBKyhvRBmikpQ=

pkged.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/node/events/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The runtime will expose three endpoints.
1515
The health checks can be accessed in your browser at [http://localhost:8080/health/readiness]() and [http://localhost:8080/health/liveness](). You can use `curl` to `POST` an event to the function endpoint:
1616

1717
```console
18-
curl -X POST -d '{"hello": "world"}' \
18+
curl -X POST -d '{"name": "Tiger", "customerId": "0123456789"}' \
1919
-H'Content-type: application/json' \
2020
-H'Ce-id: 1' \
2121
-H'Ce-source: cloud-event-example' \

templates/node/events/index.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
'use strict';
22

3-
module.exports = async function (context) {
3+
/**
4+
* An example function that responds to incoming CloudEvents over HTTP. For example,
5+
* from the Knative event Broker. Try invoking with a request such as this.
6+
*
7+
* curl -X POST -d '{"name": "Tiger", "customerId": "0123456789"}' \
8+
* -H'Content-type: application/json' \
9+
* -H'Ce-id: 1' \
10+
* -H'Ce-source: cloud-event-example' \
11+
* -H'Ce-type: dev.knative.example' \
12+
* -H'Ce-specversion: 1.0' \
13+
* http://localhost:8080
14+
*
15+
* The event data is extracted from the incoming event and provided as the first
16+
* parameter to the function. The CloudEvent object itself may be accessed via the
17+
* context parameter, For example:
18+
*
19+
* const incomingEvent = context.cloudevent;
20+
*
21+
* @param {Object} customer the CloudEvent data. If the data content type is application/json
22+
* this will be converted to an Object via JSON.parse()
23+
* @param {Context} context the invocation context
24+
*/
25+
function processCustomer(customer, context) {
26+
console.log(customer, context)
427
if (!context.cloudevent) {
5-
return Promise.reject(new Error('No cloud event received'));
28+
return 'No cloud event received';
629
}
7-
context.log.info(`Cloud event received: ${JSON.stringify(context.cloudevent)}`);
8-
return new Promise((resolve, reject) => {
9-
setTimeout(_ => resolve({ data: context.cloudevent.data }), 500);
10-
});
30+
context.log.info('Processing customer', customer);
31+
context.log.info(`CloudEvent received: ${context.cloudevent.toString()}`);
32+
return { customer };
1133
};
34+
module.exports = processCustomer;

0 commit comments

Comments
 (0)