Esta es una traducción de la página de documentación original en español. Ayúdanos a mejorarla.

4 Preprocesamiento con JavaScript

Descripción general

Esta sección proporciona detalles del preprocesamiento mediante JavaScript.

JavaScript preprocessing

JavaScript preprocessing is done by invoking JavaScript function with a single parameter 'value' and user provided function body. The preprocessing step result is the value returned from this function, for example, to perform Fahrenheit to Celsius conversion user must enter:

return (value - 32)  * 5 / 9

in JavaScript preprocessing parameters, which will be wrapped into a JavaScript function by server:

function (value)
       {
          return (value - 32) * 5 / 9
       }

The input parameter 'value' is always passed as a string. The return value is automatically coerced to string via ToString() method (if it fails then the error is returned as string value), with a few exceptions:

  • returning undefined value will result in an error
  • returning null value will cause the input value to be discarded, much like 'Discard value' preprocessing on 'Custom on fail' action.

Errors can be returned by throwing values/objects (normally either strings or Error objects).

For example:

if (value == 0)
           throw "Zero input value"
       return 1/value

Each script has a 10 second execution timeout (depending on the script it might take longer for the timeout to trigger); exceeding it will return error. A 64 megabyte heap limit is enforced.

The JavaScript preprocessing step bytecode is cached and reused when the step is applied next time. Any changes to the item's preprocessing steps will cause the cached script to be reset and recompiled later.

Consecutive runtime failures (3 in a row) will cause the engine to be reinitialized to mitigate the possibility of one script breaking the execution environment for the next scripts (this action is logged with DebugLevel 4 and higher).

JavaScript preprocessing is implemented with Duktape (https://duktape.org/) JavaScript engine.

See also: Additional JavaScript objects and global functions

Uso de macros en scripts

Es posible utilizar macros de usuario en el código JavaScript. Si un guión contiene macros de usuario, estas macros son resueltas por el servidor/proxy antes de ejecutar los pasos de preprocesamiento específicos. Tenga en cuenta que al realizar pruebas de pasos de preprocesamiento en la interfaz, los valores macro no se extraerán y deben ingresarse manualmente.

El contexto se ignora cuando una macro se reemplaza con su valor. El valor de la macro se inserta en el código tal cual, no es posible agregar escapes adicionales antes de colocar el valor en el código JavaScript. Tenga en cuenta que esto puede provocar errores de JavaScript en algunos casos.

En el siguiente ejemplo, si el valor recibido excede el valor de una macro {$THRESHOLD}, en su lugar se devolverá el valor del umbral (si está presente):

var threshold = '{$THRESHOLD}';
       return (!isNaN(threshold) && value > threshold) ? threshold : value;