cannectors

script

Inline or file-backed JavaScript transformations.

The script filter runs a JavaScript function on every record. It uses Goja, a pure-Go ECMAScript 5.1+ implementation — no eval, no Node.js APIs, no I/O. Just record → record.

Minimal example

filters:
  - type: script
    script: |
      function transform(record) {
        record.processed = true;
        return record;
      }

Options

propertytypedefaultdescription
typerequired
"script"Module type discriminator. Must be `script` for this module.
id
stringUnique identifier within the pipeline.
name
stringHuman-readable name.
description
string
enabled
booleantrueWhether module is active.
tags
array<string>
onError
string"fail"Default error action. Case-insensitive; normalized to lowercase by the runtime.
script
stringInline JavaScript source code containing a transform(record) function.
scriptFile
stringPath to JavaScript file containing the transform(record) function.

Provide exactly one of script or scriptFile — never both. The schema enforces this.

The transform function

The runtime calls transform(record) once per record. The returned value must be an object. Returning null, undefined, or a primitive is an execution error, handled according to onError.

To drop a record from a script, throw — the runtime catches and applies onError:

filters:
  - type: script
    onError: skip
    script: |
      function transform(record) {
        if (!record.email) {
          throw new Error('missing email');
        }
        return record;
      }

File-backed scripts

For non-trivial logic, factor the script out into a .js file:

filters:
  - type: script
    scriptFile: ./scripts/normalize-customer.js
scripts/normalize-customer.js
function transform(record) {
  if (record.email) {
    record.email = record.email.trim().toLowerCase();
  }
  if (record.country) {
    record.country = record.country.toUpperCase();
  }
  record.normalized_at = new Date().toISOString();
  return record;
}

What's available

Inside the script:

  • record is a plain JavaScript object — read and mutate freely.
  • Standard ES5+ APIs: JSON.stringify/parse, Math.*, Date.*, string methods, array methods.
  • console.error, console.warn, console.info, console.log, and console.debug are available and routed to the Cannectors logger.
  • No Node.js APIs (fs, http, require) and no network or file I/O.

Each record gets a fresh script execution. There's no shared state between records.

The script filter is the most expensive filter per record. Reach for mapping, condition, set, or remove first — they're orders of magnitude faster. Use script when the logic genuinely doesn't fit into those.

Examples

examples/12-script-inline-transform.yamlview source ↗
12-script-inline-transform.yaml
name: script-inline-transform
version: 1.0.0
description: Transform records using inline JavaScript.
tags:
  - script
  - inline
input:
  type: httpPolling
  schedule: "0 * * * *"
  endpoint: https://source.example.com/api/subscriptions
  dataField: subscriptions
filters:
  - type: script
    onError: skip
    script: |
      function transform(record) {
        record.normalizedStatus = String(record.status || "").toLowerCase();
        record.isActive = record.normalizedStatus === "active";
        record.planCode = String(record.plan || "unknown").toUpperCase();
        return record;
      }
output:
  type: httpRequest
  endpoint: https://destination.example.com/api/subscriptions
  method: POST
  requestMode: batch
examples/13-script-file-transform.yamlview source ↗
13-script-file-transform.yaml
name: script-file-transform
version: 1.0.0
description: Transform records with a JavaScript file referenced by scriptFile.
tags:
  - script
  - script-file
input:
  type: httpPolling
  schedule: "0 */2 * * *"
  endpoint: https://source.example.com/api/customers
  dataField: customers
filters:
  - type: script
    scriptFile: examples/assets/scripts/customer_enrichment.js
output:
  type: httpRequest
  endpoint: https://destination.example.com/api/customers/enriched
  method: POST
  requestMode: batch

Cross-references