| | | 1 | | using CShells.Features; |
| | | 2 | | using Elsa.Expressions.JavaScript.Activities; |
| | | 3 | | using Elsa.Expressions.JavaScript.Contracts; |
| | | 4 | | using Elsa.Expressions.JavaScript.Extensions; |
| | | 5 | | using Elsa.Expressions.JavaScript.HostedServices; |
| | | 6 | | using Elsa.Expressions.JavaScript.Options; |
| | | 7 | | using Elsa.Expressions.JavaScript.Providers; |
| | | 8 | | using Elsa.Expressions.JavaScript.Services; |
| | | 9 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Contracts; |
| | | 10 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Services; |
| | | 11 | | using Elsa.Extensions; |
| | | 12 | | using Elsa.Workflows; |
| | | 13 | | using JetBrains.Annotations; |
| | | 14 | | using Microsoft.Extensions.DependencyInjection; |
| | | 15 | | |
| | | 16 | | namespace Elsa.Expressions.JavaScript.ShellFeatures; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Installs JavaScript integration. |
| | | 20 | | /// </summary> |
| | | 21 | | [ShellFeature( |
| | | 22 | | DisplayName = "JavaScript Expressions", |
| | | 23 | | Description = "Provides JavaScript expression evaluation capabilities for workflows", |
| | | 24 | | DependsOn = ["Mediator", "Expressions", "MemoryCache"])] |
| | | 25 | | [UsedImplicitly] |
| | | 26 | | public class JavaScriptFeature : IShellFeature |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// Enables access to any .NET class. Do not enable if you are executing workflows from untrusted sources (e.g. user |
| | | 30 | | /// |
| | | 31 | | /// See Jint docs for more: https://github.com/sebastienros/jint#accessing-net-assemblies-and-classes |
| | | 32 | | /// </summary> |
| | 0 | 33 | | public bool AllowClrAccess { get; set; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Enables access to .NET configuration via the <c>getConfig</c> function. |
| | | 37 | | /// Do not enable if you are executing workflows from untrusted sources (e.g user defined workflows). |
| | | 38 | | /// </summary> |
| | 0 | 39 | | public bool AllowConfigurationAccess { get; set; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// The timeout for script caching. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <remarks> |
| | | 45 | | /// The <c>ScriptCacheTimeout</c> property specifies the duration for which the scripts are cached in the Jint JavaS |
| | | 46 | | /// If the value of <c>ScriptCacheTimeout</c> is <c>null</c>, the scripts are cached indefinitely. If a time value i |
| | | 47 | | /// </remarks> |
| | 0 | 48 | | public TimeSpan? ScriptCacheTimeout { get; set; } = TimeSpan.FromDays(1); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Disables the generation of variable wrappers. E.g. <c>getMyVariable()</c> will no longer be available for variab |
| | | 52 | | /// This is useful if your application requires the use of invalid JavaScript variable names. |
| | | 53 | | /// </summary> |
| | 0 | 54 | | public bool DisableWrappers { get; set; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Disables copying workflow variables into the Jint engine and copying them back into the workflow execution conte |
| | | 58 | | /// Disabling this option will increase performance but will also prevent you from accessing workflow variables from |
| | | 59 | | /// </summary> |
| | 0 | 60 | | public bool DisableVariableCopying { get; set; } |
| | | 61 | | |
| | | 62 | | public void ConfigureServices(IServiceCollection services) |
| | | 63 | | { |
| | 0 | 64 | | services.Configure<JintOptions>(options => |
| | 0 | 65 | | { |
| | 0 | 66 | | options.AllowClrAccess = AllowClrAccess; |
| | 0 | 67 | | options.AllowConfigurationAccess = AllowConfigurationAccess; |
| | 0 | 68 | | options.ScriptCacheTimeout = ScriptCacheTimeout; |
| | 0 | 69 | | options.DisableWrappers = DisableWrappers; |
| | 0 | 70 | | options.DisableVariableCopying = DisableVariableCopying; |
| | 0 | 71 | | }); |
| | | 72 | | |
| | | 73 | | // JavaScript services. |
| | 0 | 74 | | services |
| | 0 | 75 | | .AddScoped<IJavaScriptEvaluator, JintJavaScriptEvaluator>() |
| | 0 | 76 | | .AddExpressionDescriptorProvider<JavaScriptExpressionDescriptorProvider>(); |
| | | 77 | | |
| | | 78 | | // Type definition services. |
| | 0 | 79 | | services |
| | 0 | 80 | | .AddScoped<ITypeDefinitionService, TypeDefinitionService>() |
| | 0 | 81 | | .AddScoped<ITypeDescriber, TypeDescriber>() |
| | 0 | 82 | | .AddScoped<ITypeDefinitionDocumentRenderer, TypeDefinitionDocumentRenderer>() |
| | 0 | 83 | | .AddSingleton<ITypeAliasRegistry, TypeAliasRegistry>() |
| | 0 | 84 | | .AddFunctionDefinitionProvider<CommonFunctionsDefinitionProvider>() |
| | 0 | 85 | | .AddFunctionDefinitionProvider<ActivityOutputFunctionsDefinitionProvider>() |
| | 0 | 86 | | .AddFunctionDefinitionProvider<RunJavaScriptFunctionsDefinitionProvider>() |
| | 0 | 87 | | .AddTypeDefinitionProvider<CommonTypeDefinitionProvider>() |
| | 0 | 88 | | .AddTypeDefinitionProvider<VariableTypeDefinitionProvider>() |
| | 0 | 89 | | .AddTypeDefinitionProvider<WorkflowVariablesTypeDefinitionProvider>() |
| | 0 | 90 | | .AddVariableDefinitionProvider<WorkflowVariablesVariableProvider>(); |
| | | 91 | | |
| | | 92 | | // Handlers. |
| | 0 | 93 | | services.AddNotificationHandlersFrom<JavaScriptFeature>(); |
| | | 94 | | |
| | | 95 | | // Type Script definitions. |
| | 0 | 96 | | services.AddFunctionDefinitionProvider<InputFunctionsDefinitionProvider>(); |
| | | 97 | | |
| | | 98 | | // UI property handlers. |
| | 0 | 99 | | services.AddScoped<IPropertyUIHandler, RunJavaScriptOptionsProvider>(); |
| | | 100 | | |
| | | 101 | | // Hosted services. |
| | 0 | 102 | | services.AddHostedService<RegisterVariableTypesWithJavaScriptHostedService>(); |
| | 0 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | |