JAVASCRIPT
The JavaScript Step allows you to run arbitrary JavaScript code in order to perform minor data processing tasks such as filtering or manipulating data from other Steps in the Process. If you have complex logic to be implemented, please consider using a Workers, which can run independently as a service.
The code to execute is defined via the input.script
field in the Step Context. This snippet is expected to be an
anonymous function that receives two arguments: steps
and context
. The object returned from this function will be
stored in the output.result
field in the Step Context after execution.
Snippets provided to this Step may use the console.log()
statement to persist execution information in the Step
Context. Note that it is not currently feasible to use 3rd party libraries other than those in the default Node runtime.
If you have a custom cluster, we can enable any arbitrary libraries you want, please reach out to us for support on how
to do this. If your use-case requires 3rd party support, a Workers might be a good solution.
Video Overview
Example Walkthrough
Please find below an example of how this Step might be used to filter the results of the user_fetcher
step ref:
// steps is an object containing references to all other steps in the current process.
// context the ProcessContext attached to this Process
(steps, context) => {
// ProcessContext input is provided during Process invocation
const location = context.input.locationId;
// Other Step Contexts can be accessed using the `steps` argument
const data = steps.user_fetcher_ref.output.response;
// Manipulate the data
const filteredData = data.filter(it => it.locationId === locationId);
return {
id: locationId,
locations: filteredData
}
}
Input
Field | Description | Mandatory | JSON Example |
---|---|---|---|
script | The anonymous function to execute as part of this Step. | ✔ | (steps, context) => { return 'hello' } |
Output
Field | Description |
---|---|
result | The object that was returned by the function provided in input.script . |
logs | An array of log objects, each containing the ms elapsed since execution started and the log message. |
error | An error message in case of an unsuccessful execution. |
Unmeshed runs the scripts using a highly performant JavaScript engine. It achieves 10x the speed of GraalVM under load.
Using Helpers in JavaScript Tasks
In addition to the default steps
and context
parameters, your JavaScript code in Unmeshed can also receive a third
parameter called helpers
. This parameter provides you with a set of utility functions designed to simplify common
tasks, such as generating UUIDs, formatting dates, or performing other asynchronous operations.
For instance, you can use helper functions to generate version 5 UUIDs easily. The helper functions are accessible
via helpers.uuid.v5.generate
and can be called asynchronously within your code.
Below is an example of how to use the helpers parameter in a JavaScript task:
// (steps, context, helpers) will be provided as default inputs
async (steps, context, helpers) => {
// Generate a v5 UUID using the built-in DNS namespace.
const uuid1 = await helpers.uuid.v5.generate(helpers.uuid.NAMESPACE_DNS, "myid");
// Generate a v5 UUID using a custom namespace.
const uuid2 = await helpers.uuid.v5.generate("9ff2c80d-0d05-5aee-936e-99c96c83ad21", "myid");
// Generate UUIDs again (possibly for consistency or validation).
const uuid1a = await helpers.uuid.v5.generate(helpers.uuid.NAMESPACE_DNS, "myid");
const uuid2a = await helpers.uuid.v5.generate("9ff2c80d-0d05-5aee-936e-99c96c83ad21", "myid");
return {
"uuid1": uuid1,
"uuid2": uuid2,
"uuid1a": uuid1a,
"uuid2a": uuid2a,
"currentStepId": steps.__self.id,
}
}
Here the function needs to be marked as async and the helper function call requires an await as the underlying library requires this.