| | | 1 | | using Elsa.Expressions.Models; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Jint; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Expressions.JavaScript.Options; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Options for the Jint JavaScript engine. |
| | | 9 | | /// </summary> |
| | | 10 | | public class JintOptions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// A list of callbacks that are invoked when the Jint engine options is created. Use this to configure the engine o |
| | | 14 | | /// </summary> |
| | 473 | 15 | | internal Action<Jint.Options, ExpressionExecutionContext> ConfigureEngineOptionsCallback = (_, _) => { }; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// A list of callbacks that are invoked when the Jint engine is created. Use this to configure the engine. |
| | | 19 | | /// </summary> |
| | 473 | 20 | | internal Action<Engine, ExpressionExecutionContext> ConfigureEngineCallback = (_, _) => { }; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Enables access to any .NET class. Do not enable if you are executing workflows from untrusted sources (e.g. user |
| | | 24 | | /// |
| | | 25 | | /// See Jint docs for more: https://github.com/sebastienros/jint#accessing-net-assemblies-and-classes |
| | | 26 | | /// </summary> |
| | 291 | 27 | | public bool AllowClrAccess { get; set; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Enables access to .NET configuration via the <c>getConfig</c> function. |
| | | 31 | | /// Do not enable if you are executing workflows from untrusted sources (e.g user defined workflows). |
| | | 32 | | /// </summary> |
| | 285 | 33 | | public bool AllowConfigurationAccess { get; set; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The maximum wall-clock time a single JavaScript expression may run for. Set to <c>null</c> to remove the limit. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <remarks> |
| | | 39 | | /// Without a limit, an expression such as <c>while (true) {}</c> occupies the calling thread indefinitely. |
| | | 40 | | /// The default is deliberately generous so that legitimate expressions are unaffected; hosts that evaluate |
| | | 41 | | /// user-defined workflows are encouraged to lower it. |
| | | 42 | | /// </remarks> |
| | 478 | 43 | | public TimeSpan? ExecutionTimeout { get; set; } = TimeSpan.FromSeconds(30); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// The maximum number of statements a single JavaScript expression may execute. Set to <c>null</c> (the default) to |
| | | 47 | | /// </summary> |
| | 286 | 48 | | public int? MaxStatements { get; set; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// The maximum amount of memory, in bytes, a single JavaScript expression may allocate. Set to <c>null</c> (the def |
| | | 52 | | /// </summary> |
| | 286 | 53 | | public long? MemoryLimit { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// The maximum recursion depth allowed within a single JavaScript expression. Set to <c>null</c> (the default) to r |
| | | 57 | | /// </summary> |
| | | 58 | | /// <remarks> |
| | | 59 | | /// Unbounded recursion in a script exhausts the CLR stack, which cannot be recovered from. Hosts that evaluate |
| | | 60 | | /// user-defined workflows are encouraged to set a limit. |
| | | 61 | | /// </remarks> |
| | 286 | 62 | | public int? MaxRecursionDepth { get; set; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// The timeout for script caching. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// The <c>ScriptCacheTimeout</c> property specifies the duration for which the scripts are cached in the Jint JavaS |
| | | 69 | | /// If the value of <c>ScriptCacheTimeout</c> is <c>null</c>, the scripts are cached indefinitely. If a time value i |
| | | 70 | | /// </remarks> |
| | 642 | 71 | | public TimeSpan? ScriptCacheTimeout { get; set; } = TimeSpan.FromDays(1); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Disables the generation of variable wrappers. E.g. <c>getMyVariable()</c> will no longer be available for variab |
| | | 75 | | /// This is useful if your application requires the use of invalid JavaScript variable names. |
| | | 76 | | /// </summary> |
| | 860 | 77 | | public bool DisableWrappers { get; set; } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Disables copying workflow variables into the Jint engine and copying them back into the workflow execution conte |
| | | 81 | | /// Disabling this option will increase performance but will also prevent you from accessing workflow variables from |
| | | 82 | | /// </summary> |
| | 570 | 83 | | public bool DisableVariableCopying { get; set; } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Configures the Jint engine options. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="configurator">A callback that is invoked when the Jint engine options are created. Use this to conf |
| | | 89 | | public JintOptions ConfigureEngineOptions(Action<Jint.Options> configurator) |
| | | 90 | | { |
| | 0 | 91 | | ConfigureEngineOptionsCallback += (options, _) => configurator(options); |
| | 0 | 92 | | return this; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Configures the Jint engine options. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="configurator">A callback that is invoked when the Jint engine options are created. Use this to conf |
| | | 99 | | public JintOptions ConfigureEngineOptions(Action<Jint.Options, ExpressionExecutionContext> configurator) |
| | | 100 | | { |
| | 0 | 101 | | ConfigureEngineOptionsCallback += configurator; |
| | 0 | 102 | | return this; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Configures the Jint engine. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="configurator">A callback that is invoked when the Jint engine is created. Use this to configure the |
| | | 109 | | public JintOptions ConfigureEngine(Action<Engine, ExpressionExecutionContext> configurator) |
| | | 110 | | { |
| | 9 | 111 | | ConfigureEngineCallback += configurator; |
| | 9 | 112 | | return this; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Configures the Jint engine. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="configurator">A callback that is invoked when the Jint engine is created. Use this to configure the |
| | | 119 | | public JintOptions ConfigureEngine(Action<Engine> configurator) |
| | | 120 | | { |
| | 50 | 121 | | return ConfigureEngine((engine, _) => configurator(engine)); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Registers the specified type <c>T</c> with the engine. |
| | | 126 | | /// </summary> |
| | | 127 | | public JintOptions RegisterType<T>() |
| | | 128 | | { |
| | 0 | 129 | | return ConfigureEngine(engine => engine.RegisterType<T>()); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Registers the specified type with the engine. |
| | | 134 | | /// </summary> |
| | | 135 | | public JintOptions RegisterType(Type type) |
| | | 136 | | { |
| | 0 | 137 | | return ConfigureEngine(engine => engine.RegisterType(type)); |
| | | 138 | | } |
| | | 139 | | } |