JavaScript Processor

The JavaScript Processor is a core component of the EigenLayer-AVS Task Engine that enables custom JavaScript code execution within task workflows.

The JavaScript Processor is a core component of the EigenLayer-AVS Task Engine that enables custom JavaScript code execution within task workflows. This processor allows operators to execute arbitrary JavaScript logic as part of task execution, providing powerful capabilities for data transformation, validation, filtering, and custom business logic.

For information about other processors such as REST API calls or contract interactions, see REST API Processor and Contract Interaction Processors. For information about flow control and branching, see Branch and Flow Control.

Architecture and Integration#

The JavaScript Processor is implemented as part of the task engine's Virtual Machine (VM) system, specifically handling nodes of type CustomCodeNode with JavaScript as the language.

Sources:

The JavaScript Processor leverages the Goja library, a pure Go implementation of ECMAScript 5.1, to provide JavaScript execution capabilities without external dependencies. This approach ensures that JavaScript code can be executed reliably within the Go environment.

Core Components#

1. JSProcessor Structure#

The JSProcessor struct contains:

  • A CommonProcessor reference for accessing VM context and variables
  • A Goja JavaScript runtime instance for executing code

Sources:

2. Initialization and Variable Binding#

During initialization, the processor:

  1. Creates a new Goja JavaScript runtime
  2. Binds built-in functions through the macros.GetEnvs() method
  3. Binds all variables from previous steps to make them accessible in JavaScript

Sources:

3. Execution Process#

The execution workflow for a JavaScript node:

Sources:

Features and Capabilities#

1. JavaScript Execution#

The processor executes provided JavaScript code by wrapping it in a self-executing function:

This approach isolates the execution context while capturing the returned value.

Sources:

2. Data Transformation and Manipulation#

JavaScript code can perform various data transformations:

  • Array filtering and mapping
  • Object property access and manipulation
  • Mathematical operations
  • String manipulation
  • Logic operations

Tests show examples of:

  • Boolean expressions (return 3>2)
  • Array filtering (a.filter((i) => i >= 2))
  • Complex object manipulation with filter and map operations

Sources:

3. Access to Task Context#

JavaScript code has access to:

  • Results from previous task steps through variable bindings
  • System configuration values through apContext.configVars
  • Secret values (when properly configured)

Sources:

4. Return Value Handling#

The processor captures the JavaScript return value and:

  1. Converts it to a Go-compatible value
  2. Stores it in the VM's variable map for access by subsequent steps
  3. Includes it in the execution step's output data

Sources:

JavaScript Filter Processor#

In addition to the main JavaScript Processor, the system includes a specialized Filter Processor that uses JavaScript expressions for filtering arrays.

Sources:

Filter Processor Features#

The Filter Processor:

  1. Takes an input array and a filter expression
  2. Constructs a JavaScript filter statement using the expression
  3. Executes the filter operation on the input array
  4. Returns the filtered array as the result

Unlike the general JavaScript Processor, the Filter Processor is specifically designed for array filtering operations, providing a more focused API.

Sources:

Usage Examples#

1. Simple Boolean Logic#

This returns a boolean value (true).

2. Array Filtering#

This returns [2, 3].

3. Object Transformation#

This returns [{name: 'bob', age: 15}].

4. Accessing Configuration Variables#

This demonstrates accessing configuration variables or secrets.

Sources:

Template Rendering Considerations#

When using JavaScript object results in template strings in subsequent steps, objects are rendered as [object Object] rather than as JSON strings. This is important to consider when designing workflows that pass complex objects between steps.

Sources:

Implementation Notes#

JavaScript Runtime Initialization#

The JavaScript runtime is initialized with:

  1. System-provided utility functions from macros.GetEnvs()
  2. Variables from previous execution steps
  3. Configuration values in the apContext object

Sources:

Error Handling#

The processor provides comprehensive error handling:

  • JavaScript syntax errors are captured and reported
  • Execution errors are logged and included in the step output
  • Detailed timing information is recorded for performance monitoring

Sources:

Best Practices#

When using the JavaScript Processor:

  1. Keep code simple and focused: JavaScript nodes should perform specific transformations rather than complex operations
  2. Handle errors gracefully: Include error handling in your JavaScript code
  3. Be mindful of object serialization: Objects returned from JavaScript nodes are rendered as [object Object] in template strings
  4. Use appropriate return values: Return values that can be properly serialized to JSON
  5. Leverage built-in functions: Use the system-provided utility functions when available instead of reimplementing common operations

Technical Specifications#

AspectDetails
JavaScript EngineGoja (Pure Go ECMAScript 5.1 implementation)
ECMAScript SupportES5.1 + partial ES6
Variable AccessAll previous steps' outputs available as variables
Return Value TypesAny JSON-serializable value
Error HandlingComprehensive with detailed logs
Configuration AccessVia apContext.configVars object

The JavaScript Processor provides a powerful mechanism for custom logic within EigenLayer-AVS tasks, enabling complex transformations and business rules to be expressed directly within the task workflow.

Feature this wiki to auto refresh weekly

Try DeepWiki on your private codebase with Devin