# Javascript Flow Processor

# Purpose

Asset Dependency Graph (Javascript Flow Processor)

The Javascript Asset allows you to define detailed business logic which you may want to apply to a flow of messages. Here are some examples:

  • Convert message data from one format to another
  • Filter information based on specific rules
  • Enrich individual data using specific rules and/or external data sources (e.g. reference data)
  • Route messages based on your own criteria
  • Gather metrics and statistics, and store and forward them to other targets

and basically anything else you can imagine here.

# Prerequisites

You need:

  • A Source Script which should be executed within this asset.
  • Knowledge on how to work with Javascript in layline.io. Please check the Javascript Language Reference to learn about this.

# Configuration

# Name & Description

Name & Description (Javascript)

  • Name : Name of the Asset. Whitespaces are not allowed in the name.

  • Description : Enter a description.

The Asset Usage box shows how many times this Asset is used and which parts are referencing it. Click to expand and then click to follow, if any.

# Input Ports

Input Ports (Javasscript)

A Javascript processor can have one or more input ports from which it receives data to process. It must have at least one input port.

A port can have a name and description. Names must exist and be unique within the processor.

You can add an input port by clicking on Add Port (1), or remove an input port by clicking on Delete. You cannot delete the port if it is the last one within the processor.

# Output Ports

As is true for Input Ports, a Javascript Processor can have one-to-many Output Ports to send messages on within the Workflow.

Output Ports (Javascript)

A Javascript processor can only have one output port from which it sends ingested data downstream in the Workkflow.

A port can have a name and description. Names must exist and be unique within the processor.

# Root Script

The Javascript Asset obviously needs a Script to be executed. Prior to version 1.0 of layline.io the Script was configured as part of this Asset. Starting with v1.0 all Scripts are defined in the Sources tab of the project (2):

Assigned Source Script (Javascript)

The root script to be executed within this Asset is then selected here:

Root Script (Javascript)

Javascript Language Reference

To understand how a Source must be structured to work in a Javascript Asset, please consult the Javascript Language Reference.

# Service Mappings

Javascripts may make use of Services which you may have configured here. These methods could be database operations, HTTP-request and whatever else Services do provide.

Let's say your Javascript invokes a HTTP-Service which provides a method to retrieve the current Bitcoin price via a REST-Api. Let's also assume that the name of the Service to be linked is BTCService.

  1. Add a Service Mapping by clicking on Add Service Mapping (1).
  2. Select the Service which you want to map (2).
  3. Provide a Logical Service Name. This is the name by which the Service is used in the underlying Javascript! If the name you enter here, is different to what you are using in your script, the script will not recognize the Service.

Service Mappings (Javascript)

  • Use synchronous services as default: This flag relates to whether the script-based invocation of service methods happens synchronously, or asynchronously (aka "Promise"). It's important to understand, that there is a tight link between this flag and how you invoke service methods in your script. Let's look at an example:

Synchronous Service Invocation

// Get the current bitcoin rate --> Attention: max 100 requests per day with free key!
const btcRate = null;

// GetBTCRate() returns the result
let response = services.BTCService.GetBTCRate({
    Parameter: {"X-CoinAPI-Key": "714E6F81-DD48-452C-BE51-4EBE4A6C2XYZ"}

if (response.data.StatusCode === 200 && response.data.Entity.BTCRate) {
    processor.logInfo('response.Entity:' + response.data.Entity);

Asynchronous Service Invocation

// Get the current bitcoin rate

// GetBTCRate() returns a Promise ....
    Parameter: {"X-CoinAPI-Key": "714E6F81-DD48-452C-BE50-4EBE4A6C2F1C"}
}).then(function (response) {
    btcRate = response.data.Entity.BTCRate; // response is a message structure
}).catch(function (error) {
}).finally(function () {

If you are not familiar with the concept of synchronous and asynchronous function calls, we can recommend reading more about it on MDN (opens new window).

# Arguments

You can pass arguments to the assigned script. This may be useful when reusing the same script in various different Javascript Assets and Workflows, but the script should behave slightly different in each of those instances. Passing arguments from a Javascript Asset to can provide this functionality. Please check the getArguments() method here, on how to retrieve arguments in the script.

Arguments (Javascript)

In case you are entering arguments (1), the editor will check for valid JSON and outline this in case it is invalid. You can format the JSON entries with a click on Format JSON (2).

Invalid JSON

Entering invalid JSON will cause problems when using the Arguments in the underlying script.

# Failure Handling

Please see section Forced Errors to understand how to use these settings.

Failure Handling (Javascript)

Can't find what you are looking for?

Please note, that the creation of the online documentation is Work-In-Progress. It is constantly being updated. Should you have questions or suggestions, please don't hesitate to contact us at support@layline.io .

Last Updated: 12/20/2022, 1:35:11 PM