Skip to content

Commit 402437f

Browse files
committed
✨ feat: initialize ofn knative and async runtimes
1 parent dca4132 commit 402437f

27 files changed

+3171
-435
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules/
22
build/
33
.coverage/
4+
.history/
45
npm-debug.log
56
.nyc_output
67
.vscode

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ for writing portable Node.js functions
1212
The Functions Framework lets you write lightweight functions that run in many
1313
different environments, including:
1414

15-
* [Google Cloud Functions](https://cloud.google.com/functions/)
16-
* Your local development machine
17-
* [Cloud Run](https://cloud.google.com/run/) and [Cloud Run for Anthos](https://cloud.google.com/anthos/run)
18-
* [Knative](https://github.com/knative/)-based environments
19-
* [OpenFunction](https://github.com/OpenFunction/OpenFunction)
15+
* [Google Cloud Functions](https://cloud.google.com/functions/)
16+
* Your local development machine
17+
* [Cloud Run](https://cloud.google.com/run/) and [Cloud Run for Anthos](https://cloud.google.com/anthos/run)
18+
* [Knative](https://github.com/knative/)-based environments
19+
* [Dapr](https://dapr.io/)-based environments
20+
* [OpenFunction](https://github.com/OpenFunction/OpenFunction)
2021

2122
The framework allows you to go from:
2223

@@ -45,11 +46,11 @@ handling logic.
4546
4647
## Features
4748

48-
- Spin up a local development server for quick testing
49-
- Invoke a function in response to a request
50-
- Automatically unmarshal events conforming to the
49+
* Spin up a local development server for quick testing
50+
* Invoke a function in response to a request
51+
* Automatically unmarshal events conforming to the
5152
[CloudEvents](https://cloudevents.io/) spec
52-
- Portable between serverless platforms
53+
* Portable between serverless platforms
5354

5455
## Installation
5556

@@ -77,7 +78,7 @@ npm install @openfunction/functions-framework
7778
npx @openfunction/functions-framework --target=helloWorld
7879
```
7980

80-
1. Open http://localhost:8080/ in your browser and see _Hello, World_.
81+
1. Open <http://localhost:8080/> in your browser and see _Hello, World_.
8182

8283
### Quickstart: Set up a new project
8384

@@ -132,7 +133,7 @@ command-line arguments:
132133
1. Install [Docker](https://store.docker.com/search?type=edition&offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/).
133134

134135
1. Build a container from your function using the Functions [buildpacks](https://github.com/GoogleCloudPlatform/buildpacks):
135-
136+
136137
```sh
137138
pack build \
138139
--builder openfunction/builder-node:v2-16.13 \
@@ -142,14 +143,14 @@ command-line arguments:
142143
```
143144

144145
1. Start the built container:
145-
146+
146147
```sh
147148
docker run --rm -p 8080:8080 my-first-function
148149
# Output: Serving function...
149150
```
150151

151152
1. Send requests to this function using `curl` from another terminal window:
152-
153+
153154
```sh
154155
curl localhost:8080
155156
# Output: Hello, World!
@@ -249,7 +250,7 @@ To enable the CloudEvent functions, you must list the Functions Framework as a d
249250
```json
250251
{
251252
"dependencies": {
252-
"@openfunction/functions-framework": "~0.3.6"
253+
"@openfunction/functions-framework": "~0.4.0"
253254
}
254255
}
255256
```

docs/async-server.puml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
@startuml Async Server
2+
3+
box Function Process in Local Environment or Container
4+
control ENTRYPOINT
5+
participant Main
6+
participant AsyncServer
7+
participant DaprServer
8+
participant Restana [
9+
Web Server
10+
----
11+
""Restana""
12+
]
13+
end box
14+
15+
entity "Dapr Sidecar " as DaprSidecar
16+
17+
== OpenFunction Serving ==
18+
19+
ENTRYPOINT -> Main ** : execute
20+
note over ENTRYPOINT, Main: Pass through __CLI arguments__ and \ncontainer __environment variables__
21+
22+
Main -> Main : load user function file
23+
note left: ""function (ctx, data) {}""
24+
25+
Main -> AsyncServer ** : create
26+
note over Main, AsyncServer: Hand over __user function__ and __context__
27+
28+
AsyncServer -> DaprServer ** : ""new""
29+
note over AsyncServer, DaprServer: Extract __port__ from __context__ and pass down
30+
31+
DaprServer -> Restana ** : ""new""
32+
note over Restana: Super fast and minimalist framework \nfor building REST micro-services
33+
|||
34+
DaprServer --> DaprSidecar : Waiting till Dapr sidecar started
35+
note over DaprServer, DaprSidecar #FFAAAA: Using HTTP channel due to lack of good support of gRPC in Dapr Node.js SDK
36+
...
37+
AsyncServer -> DaprServer : register __user function__ as handler \nfor each of __inputs__ in __context__
38+
DaprServer -> Restana : add routes for Dapr style \nsubscriptions and input bindings
39+
40+
...
41+
42+
== OpenFunction Triggering ==
43+
44+
DaprSidecar <--] : sub / input data
45+
46+
DaprSidecar -> Restana ++ : Dapr request with "data"
47+
48+
Restana -> Restana ++ : invoke user function
49+
50+
alt
51+
Restana -> DaprSidecar ++ : publish data or invoke output binding
52+
DaprSidecar --> Restana -- : execution result
53+
end
54+
55+
return
56+
57+
return server app response
58+
59+
...
60+
61+
@enduml

docs/http-binding.puml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
@startuml HTTP Binding
2+
3+
box Function Process in Local Environment or Container
4+
control ENTRYPOINT
5+
participant Main
6+
participant Server
7+
participant Express [
8+
Web Server
9+
----
10+
""Express""
11+
]
12+
participant Interceptor
13+
participant "User Function" as UserFunction
14+
participant DaprClient
15+
end box
16+
17+
entity "Dapr Sidecar " as DaprSidecar
18+
19+
== OpenFunction Serving ==
20+
21+
ENTRYPOINT -> Main ** : execute
22+
note over ENTRYPOINT, Main: Pass through __CLI arguments__ and \ncontainer __environment variables__
23+
24+
Main -> Main : load user function file
25+
note left: ""function (req, res) {}""
26+
27+
Main -> Server ** : create
28+
note over Main, Server: Hand over __user function__, __function type__ \nand __context__ parsed from env variables
29+
30+
Server -> Express ** : new
31+
32+
Server -> Express : use init middleware
33+
note over Server, Express: Store context in ""res.locals.context""
34+
35+
Server -> Interceptor ** : new
36+
Server -> Express : use interceptor
37+
|||
38+
Server -> Express : use others middlewares
39+
|||
40+
Server -> UserFunction ** : wrap user function
41+
note over Server, UserFunction: Use Node.js ""Domain"" to run function and catch exceptions
42+
Server -> Express : bind wrapper to "/*" route
43+
44+
...
45+
46+
== OpenFunction Invocation ==
47+
48+
[-> Express ++ : HTTP request to "/"
49+
50+
Express -> UserFunction ++ : execute user function
51+
UserFunction --> Express -- : return execution result "data"
52+
53+
alt ""runtime"" = ""knative"" and ""outputs"" is not empty
54+
Express -> Interceptor ++ : invoke interceptor
55+
56+
Interceptor -> Interceptor : load context
57+
Interceptor -> DaprClient ** : new
58+
59+
loop each OpenFunction Output
60+
Interceptor -> DaprClient ++ : send "data"
61+
62+
DaprClient -> DaprSidecar ++ : invoke binding or publication with "data"
63+
DaprSidecar --> DaprClient -- : return result
64+
65+
DaprClient --> Interceptor -- : forward result
66+
end
67+
68+
Interceptor -> DaprClient !!
69+
Interceptor --> Express -- : return "data" as response
70+
end
71+
72+
[<- Express -- : send response
73+
74+
...
75+
76+
@enduml

0 commit comments

Comments
 (0)