2023-10-07 10:47:36 +00:00
using Elsa.Expressions.Models ;
using Elsa.Extensions ;
using Jint ;
2023-01-04 19:36:38 +00:00
namespace Elsa.JavaScript.Options ;
2023-10-07 10:47:36 +00:00
/// <summary>
/// Options for the Jint JavaScript engine.
/// </summary>
2023-01-04 19:36:38 +00:00
public class JintOptions
2022-01-12 10:43:46 +00:00
{
2023-01-04 19:36:38 +00:00
/// <summary>
/// Enables access to any .NET class. Do not enable if you are executing workflows from untrusted sources (e.g. user defined workflows).
///
/// See Jint docs for more: https://github.com/sebastienros/jint#accessing-net-assemblies-and-classes
/// </summary>
public bool AllowClrAccess { get ; set ; }
2022-01-12 10:43:46 +00:00
2023-01-04 19:36:38 +00:00
/// <summary>
/// Enables access to .NET configuration via the <c>getConfig</c> function.
/// Do not enable if you are executing workflows from untrusted sources (e.g user defined workflows).
/// </summary>
public bool AllowConfigurationAccess { get ; set ; }
2023-10-07 10:47:36 +00:00
2024-04-25 08:26:35 +00:00
/// <summary>
/// The timeout for script caching.
/// </summary>
/// <remarks>
/// The <c>ScriptCacheTimeout</c> property specifies the duration for which the scripts are cached in the Jint JavaScript engine. When a script is executed, it is compiled and cached for future use. This caching improves performance by avoiding repetitive compilation of the same script.
/// If the value of <c>ScriptCacheTimeout</c> is <c>null</c>, the scripts are cached indefinitely. If a time value is specified, the scripts will be recompiled after the specified duration has elapsed.
/// </remarks>
public TimeSpan ? ScriptCacheTimeout { get ; set ; } = TimeSpan . FromDays ( 1 ) ;
2023-10-07 10:47:36 +00:00
/// <summary>
/// A list of callbacks that are invoked when the Jint engine is created. Use this to configure the engine.
/// </summary>
public Action < Engine , ExpressionExecutionContext > ConfigureEngineCallback = ( _ , _ ) = > { } ;
/// <summary>
/// Configures the Jint engine.
/// </summary>
/// <param name="configurator">A callback that is invoked when the Jint engine is created. Use this to configure the engine.</param>
public JintOptions ConfigureEngine ( Action < Engine , ExpressionExecutionContext > configurator )
{
ConfigureEngineCallback + = configurator ;
return this ;
}
/// <summary>
/// Configures the Jint engine.
/// </summary>
/// <param name="configurator">A callback that is invoked when the Jint engine is created. Use this to configure the engine.</param>
public JintOptions ConfigureEngine ( Action < Engine > configurator )
{
return ConfigureEngine ( ( engine , _ ) = > configurator ( engine ) ) ;
}
/// <summary>
/// Registers the specified type <c>T</c> with the engine.
/// </summary>
public JintOptions RegisterType < T > ( )
{
return ConfigureEngine ( engine = > engine . RegisterType < T > ( ) ) ;
}
/// <summary>
/// Registers the specified type with the engine.
/// </summary>
public JintOptions RegisterType ( Type type )
{
return ConfigureEngine ( engine = > engine . RegisterType ( type ) ) ;
}
2022-01-12 10:43:46 +00:00
}