You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 13, 2021. It is now read-only.
Non-streaming functions, more specifically "request-reply" functions, such as:
16
16
```js
@@ -68,6 +68,58 @@ The function **must** end the output streams when it is done emitting data or wh
68
68
(if the output streams are [`pipe`](https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options)'d from
69
69
input streams, then this is automatically managed by this invoker).
70
70
71
+
## Message support
72
+
73
+
A message is an object that contains both headers and a payload.
74
+
Message headers are a map with case-insensitive keys and multiple string values.
75
+
76
+
Since JavaScript and Node have no built-in type for messages or headers, riff uses the [@projectriff/message](https://github.com/projectriff/node-message/) npm module. To use messages, functions should install the `@projectriff/message` package:
77
+
```bash
78
+
npm install --save @projectriff/message
79
+
```
80
+
81
+
By default, request-reply functions accept and produce payloads.
82
+
They can be configured instead to **receive** either the entire message or the headers only.
83
+
84
+
> Streaming functions can only receive messages. Configuring them with `$argumentType` will trigger an error.
85
+
> However, they can produce either messages or payloads, just like request-reply functions.
// a request-reply function that produces a Message
111
+
module.exports=name=> {
112
+
returnMessage.builder()
113
+
.addHeader('X-Riff-Instance', instanceId)
114
+
.addHeader('X-Riff-Count', invocationCount++)
115
+
.payload(`Hello ${name}!`)
116
+
.build();
117
+
};
118
+
119
+
// even if the function receives payloads, it can still produce a message
120
+
module.exports.$argumentType='payload';
121
+
```
122
+
71
123
## Lifecycle
72
124
73
125
Functions that communicate with external services, like a database, can use the `$init` and `$destroy` lifecycle hooks
@@ -104,37 +156,6 @@ Note that the lifecycle hooks must be fields on the exported function.
104
156
The hooks may be either synchronous or async functions.
105
157
Lifecycle functions have up to **10 seconds** to complete their work, or the function invoker will abort.
106
158
107
-
## Argument transformers
108
-
109
-
Sometimes, the content-type information is not enough to extract the payload the user function is supposed to interact
110
-
with.
111
-
112
-
Argument transformers are custom functions that take a `Message` (as defined by [`@projectriff/message`](https://github.com/projectriff/node-message))
113
-
and return whatever the function needs.
114
-
115
-
The `Message` payload is the result of the first content-type-based conversion pass. For instance, if the input
116
-
content-type is `application/json` and its payload is `'{"key": "value"}'` the payload of the `Message` exposed to the
117
-
transformer will be the corresponding object representation (i.e. `{"key": "value"}`).
118
-
119
-
Argument transformers are declared this way:
120
-
121
-
```js
122
-
module.exports.$argumentTransformers= [
123
-
// transformer for first input
124
-
(message) => {
125
-
returnmessage.payload;
126
-
},
127
-
// transformer for second input
128
-
(message) => {
129
-
returnmessage.headers.getValue('x-some-header');
130
-
},
131
-
// ...
132
-
];
133
-
```
134
-
135
-
If `$argumentTransformers` is not declared, the default transformer assigned to each input extracts the `Message`
136
-
payload.
137
-
138
159
## Supported protocols
139
160
140
161
This invoker supports only streaming, and complies to [riff streaming protocol](https://github.com/projectriff/streaming-processor).
0 commit comments