Scripting
Nothing in this section is needed to build an automation: the built-in nodes and transforms cover most work without code, and where a script is the right tool, Esca can write it for you from a sentence. Read on when you want to write or understand one yourself.
A script is JavaScript that runs inside an automation, in a sandbox, in one of three places: a Script action, a Script condition, or a Script transform on a mapping. The place decides what the script can read and what its result means. Everything else is the same everywhere: where the code comes from, what it can log, how it fails, and the ceilings it runs under. That shared part is described here once, and the node and transform pages keep their own fields and link back.
A script has no access to the file system, the network, or the host, so it can't call an API or read a file itself. Fetch the data with an HTTP Request or Read File step first and hand the result to the script. The other ceilings are under Limits.
- Content API—the
dariobject a Script action gets, for building and editing content against this workspace's content types or a connected Brightspot CMS's content model, and how what it builds is handed to a Content Edit Save. - dari reference—every class and function on
dari, with its signature, generated from the code.
Where a script runs
| Where it runs | payload | input | dari | What the last expression becomes |
|---|---|---|---|---|
| Script action | What the step before sends | The node's input mappings, as input.fieldName | This workspace's content types, or a connected CMS's content model when Brightspot Connection is set | The node's output |
| Script condition | What the step before sends | The node's input mappings | This workspace's content types | The branch: a value goes to true; empty, zero, false or null goes to false |
| Script transform | Not available | The value so far: the mapping's source, or the result of the transform before this one | Not available | The mapping's new value |
Both names are always bound where they are offered: payload is null when the step before sent nothing, and a mapping under input whose value resolved to nothing is null. A script tests payload === null rather than guarding against an undefined name.
The result
A script's result is the value of its last expression. A top-level return is a syntax error, so end the script with the expression itself, and wrap an object literal in parentheses, or it reads as a block rather than a value:
1({ changed: true, differences: changes.byRecord });
Text, numbers, booleans, null, arrays and objects travel as they are, to become the step's output or the mapping's value. A Script action's result reaches later steps through the variable picker, which lists what its Output Type declares or the shape taken from one of its runs; see What later steps see. A script that builds content gives the step plain data, such as record.toState(), rather than the dari objects themselves; see Handing content to a Save.
Where the script comes from
| Source | What it is | Offered on |
|---|---|---|
| Inline | The script is written on the step or transform, in a Language you choose, and checked for syntax errors as you type. The default. Under the editor, Insert from Scripts… copies a saved script's code in, and Save to Scripts… saves what is there to the catalog as a new saved script. | Every place |
| Saved | A script from the Scripts catalog, chosen by name. The reference is live: the step runs whatever the saved script holds when it runs, so an edit in the catalog reaches every step that references it. The step shows the script's code read-only, with a link to edit it in the catalog, and on the canvas a Script step is labeled with the script's name. | Every place |
| Runtime | The code arrives at run time from a value produced earlier in the run: bind Source to a field holding it, such as a file fetched by an HTTP Request or a GitHub Get File Contents step. The route for code kept somewhere Esca has no connection to. The sandbox is unchanged; what changes is who can change the code, so trust the source as you trust the automation reading it. | Script action and condition |
A transform receives a value and nothing else, so it has no Runtime source. On an Inline script on a Script action or condition, Esca can write the script for you; a transform doesn't offer Esca's help. The code is needed to run, not to save: a step or transform whose script is empty saves, showing a needs-attention note, so a flow can be laid out before its code is written.
Logging
console.log(), console.info(), console.warn(), console.error() and console.debug() write to the step's run log, one timestamped entry per call at the matching level, so the log can be filtered down to a script's warnings or errors. console.debug entries are recorded only while Capture Debug Logs is on. Several arguments are joined with spaces, and an object is written as JSON. From a transform, each entry is labeled with its mapping and transform, such as Input · headline › Script, and shown under the transform in the mapping's trace. The output one run may write is capped; see Limits.
When a script fails
A script that can't run, or that throws, fails its step with the reason in the step's log:
- No code to run. An Inline script left empty, a Saved script that has been deleted, isn't available in this site, or has no code, or a Runtime source that resolved to nothing, to something that isn't text, or to empty text.
- A syntax error, reported by the sandbox. The editor flags syntax errors as you type, so an Inline script rarely reaches this; a Runtime one can.
- An error the script throws, or that the
dariobject throws when a field name or value is wrong, reported asScript error:followed by the message. - A ceiling crossed, named in the message, such as
Script execution timed out after 3000ms. See Limits.
How the failure plays out depends on the place. A Script action fails as any action does, taking the error path when there is one; see Failure, retry, and the error path. A Script condition fails the step rather than routing to false. A Script transform fails the step its mapping belongs to, and the mapping's trace shows the Script transform as the one that failed, with the message; see When a transform fails.
Limits
Scripts run with no access to the file system, the network, or the host. Several ceilings bound what one script may consume, because the server runs many at once and none of them should be able to slow the others down. Scripts are meant for transforming data, and the defaults sit well above what that needs.
| What is limited | Default |
|---|---|
| Time to run | 3 seconds |
| CPU time used | 1 second |
| Statements executed | 1,000,000 |
| Memory held at once | 32 MB |
| Memory allocated in total, including memory the script discards as it goes | 192 MB |
| Longest single string | 4,194,304 characters |
Largest single typed array, such as a Uint8Array | 1,048,576 elements |
| Console output captured per run | 64 KB |
| Length of the script itself | 262,144 characters |
For scale: a script that maps and filters a 50,000-item list and re-serializes it to JSON uses roughly 250,000 statements, under a tenth of a second, and under 4 MB of memory.
Crossing a ceiling fails the step with a message naming the one you crossed, so you can tell which value is in the way. Your administrator can change any of them, and the two memory ceilings and the CPU ceiling additionally depend on how the server is set up, but the message always names the ceiling that stopped the script.
If you are hitting a limit, the usual fix is to move work out of the script rather than raise the ceiling: filter a list before the script rather than inside it, or split one large script into several steps. The dari object's own work counts toward the ceilings too, so a script that assembles a very large content tree does more than its line count suggests.