using Elsa.Expressions.Models;
using Elsa.Extensions;
using Jint;
namespace Elsa.JavaScript.Options;
///
/// Options for the Jint JavaScript engine.
///
public class JintOptions
{
///
/// 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
///
public bool AllowClrAccess { get; set; }
///
/// Enables access to .NET configuration via the getConfig function.
/// Do not enable if you are executing workflows from untrusted sources (e.g user defined workflows).
///
public bool AllowConfigurationAccess { get; set; }
///
/// The timeout for script caching.
///
///
/// The ScriptCacheTimeout 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 ScriptCacheTimeout is null, the scripts are cached indefinitely. If a time value is specified, the scripts will be recompiled after the specified duration has elapsed.
///
public TimeSpan? ScriptCacheTimeout { get; set; } = TimeSpan.FromDays(1);
///
/// A list of callbacks that are invoked when the Jint engine is created. Use this to configure the engine.
///
public Action ConfigureEngineCallback = (_, _) => { };
///
/// Configures the Jint engine.
///
/// A callback that is invoked when the Jint engine is created. Use this to configure the engine.
public JintOptions ConfigureEngine(Action configurator)
{
ConfigureEngineCallback += configurator;
return this;
}
///
/// Configures the Jint engine.
///
/// A callback that is invoked when the Jint engine is created. Use this to configure the engine.
public JintOptions ConfigureEngine(Action configurator)
{
return ConfigureEngine((engine, _) => configurator(engine));
}
///
/// Registers the specified type T with the engine.
///
public JintOptions RegisterType()
{
return ConfigureEngine(engine => engine.RegisterType());
}
///
/// Registers the specified type with the engine.
///
public JintOptions RegisterType(Type type)
{
return ConfigureEngine(engine => engine.RegisterType(type));
}
}