Links

Coding in Cloud Flow

For Cloud Flow, the Custom Code block is functional in Business or higher edition apps. This block can be tested in the Standard edition.
In Cloud Flow, the Custom Code block allows you to add Node.js based Javascript code for advanced custom functionality.

Node.js Libraries/SDKs

Your Zingy app's server-side backend includes a curated list of Node.js SDKs and Libraries that can be used within your app's Cloud Flow Web Hooks and Background Tasks using the Custom Code block.
For more information about the list of integrated libraries and sdks please see SDK/Library Integration.

Cloud Flow Variables and Functions

The variables and functions that are added to the Cloud Flow (using blocks) can also be referenced by the code within the Custom Code block.

Web Hook Response Streaming

Cloud Flow Web Hooks support advanced options for sending response to the requesting client as detailed below:

Piped (Pass-through)

When using the Piped (Pass-through) response streaming option, the response data needs to be provided as a Readable stream to the znResponsePipe function, the prototype for which is shown below:
znResponsePipe(
readable, /* stream.Readable */
contentType, /* string */
noAutoExit, /* boolean */
endCbFn /* function */
)
Parameter
Type
Description
readable
The stream to read from and pipe into the response
contentType
string
The value for the HTTP Content-Type header in the response.
noAutoExit
boolean
By default, the znResponsePipe function will auto exit the Web Hook once the stream is fully read and piped. This optional parameter allows you to disable this behavior and receive a callback notification at the function specified in the endCbFn parameter.
endCbFn
function
When noAutoExit is set to true, this function gets called when the stream has been read or upon error. A string parameter is passed to indicate the status.

Example

The example below shows a large CSV file being retrieved from an external source using the axios library. Since a responseType of stream was specified for axios, it provides a Readable stream at httpResp.data , that is passed to the znResponsePipe function. The contentType parameter is set to text/csv.
1
let axios = require('axios');
2
// URL of the large CSV file
3
let url = 'https://media.githubusercontent.com/media/datablist/sample-csv-files/main/files/customers/customers-100.csv';
4
axios({
5
method: 'get',
6
url: url,
7
responseType: 'stream'
8
}).then(function(httpResp) {
9
// Call znResponsePipe with the
10
// readable stream (httpResp.data)
11
znResponsePipe(httpResp.data, 'text/csv');
12
});
The above example conserves your Zingy app's server-side memory by directly streaming the large CSV file, instead of reading it fully and then transferring it as part of the Web Hook's response.

Chunked

When using the Chunked response streaming option, the response data is sent in batches (or chunks) to the requesting client. It sets the HTTP Transfer-Encoding header to chunked in the response.
The following functions facilitate this implementation:
znResponseChunkStart(
contentType /* string */
)
The znResponseChunkStart function, shown above, is used to initiate the response to the client. The contentType parameter specifies the value for the HTTP Content-Type header in the response.
znResponseChunkAdd(
data /* string/binary/etc */
)
The znResponseChunkAdd (above), is used to send the next chunk of data to the client. The data parameter specifies the chunk to be sent.
znResponseChunkEnd(
noAutoExit, /* boolean */
endCbFn /* function */
)
The znResponseChunkEnd function, shown above, is used to signal that all chunks have been sent to the client. By default, this function will auto exit the Web Hook. The noAuthExit parameter when set to true, prevents this and calls the function specified by the endCbFn parameter.

Example

The example below shows the same CSV file (as the Piped example above) being retrieved using the axios library. Since a responseType of stream was specified for axios, it provides a Readable stream at httpResp.data , that is piped into the csv-parser library.
The csv-parser library, parses the stream and produces an object for each line parsed. We are notified about this via the data event. In the handler for the data event, we convert the object to a JSON string and send as a chunk using the znResponseChunkAddfunction.
Upon receiving the end event, the znResponseChunkEnd function is called to close the response and exit the Web Hook.
1
let csv = require('csv-parser');
2
let axios = require('axios');
3
// URL of the large CSV file
4
let url = 'https://media.githubusercontent.com/media/datablist/sample-csv-files/main/files/customers/customers-100.csv';
5
axios({
6
method: 'get',
7
url: url,
8
responseType: 'stream'
9
}).then(function(httpResp) {
10
// call znResponseChunkStart to initiate the response
11
znResponseChunkStart('text/plain');
12
// Pipe the httpResp.data Readable stream into
13
// the Writeable stream provided by the csv-parser
14
httpResp.data.pipe(csv())
15
.on('data', (obj) => {
16
// convert to JSON and send chunk
17
// using znResponseChunkAdd
18
znResponseChunkAdd(JSON.stringify(obj));
19
})
20
.on('end', () => {
21
// call znResponseChunkEnd to signal end
22
znResponseChunkEnd(false, null);
23
});
24
});
25