| | | 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.Expressions.Options; |
| | | 12 | | using Elsa.Extensions; |
| | | 13 | | using Elsa.PackageManifest.Generator.Hints; |
| | | 14 | | using Elsa.Workflows; |
| | | 15 | | using Elsa.Workflows.Options; |
| | | 16 | | using JetBrains.Annotations; |
| | | 17 | | using Microsoft.Extensions.DependencyInjection; |
| | | 18 | | using Elsa.Common.Serialization; |
| | | 19 | | |
| | | 20 | | namespace Elsa.Expressions.JavaScript.ShellFeatures; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Installs JavaScript integration. |
| | | 24 | | /// </summary> |
| | | 25 | | [ShellFeature( |
| | | 26 | | DisplayName = "JavaScript Expressions", |
| | | 27 | | Description = "Provides JavaScript expression evaluation capabilities for workflows", |
| | | 28 | | DependsOn = ["Mediator", "Expressions", "MemoryCache"])] |
| | | 29 | | [UsedImplicitly] |
| | | 30 | | public class JavaScriptFeature : IShellFeature |
| | | 31 | | { |
| | | 32 | | /// <summary> |
| | | 33 | | /// Enables access to any .NET class. Do not enable if you are executing workflows from untrusted sources (e.g. user |
| | | 34 | | /// |
| | | 35 | | /// See Jint docs for more: https://github.com/sebastienros/jint#accessing-net-assemblies-and-classes |
| | | 36 | | /// </summary> |
| | | 37 | | [ManifestSetting( |
| | | 38 | | DisplayName = "Allow CLR Access", |
| | | 39 | | Description = "Allow JavaScript expressions to access .NET classes.", |
| | | 40 | | Category = "Security", |
| | | 41 | | DefaultValue = "false", |
| | | 42 | | Advanced = true, |
| | | 43 | | RestartRequired = true)] |
| | 0 | 44 | | public bool AllowClrAccess { get; set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Enables access to .NET configuration via the <c>getConfig</c> function. |
| | | 48 | | /// Do not enable if you are executing workflows from untrusted sources (e.g user defined workflows). |
| | | 49 | | /// </summary> |
| | | 50 | | [ManifestSetting( |
| | | 51 | | DisplayName = "Allow Configuration Access", |
| | | 52 | | Description = "Allow JavaScript expressions to access application configuration through getConfig.", |
| | | 53 | | Category = "Security", |
| | | 54 | | DefaultValue = "false", |
| | | 55 | | Advanced = true, |
| | | 56 | | RestartRequired = true)] |
| | 0 | 57 | | public bool AllowConfigurationAccess { get; set; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// The timeout for script caching. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <remarks> |
| | | 63 | | /// The <c>ScriptCacheTimeout</c> property specifies the duration for which the scripts are cached in the Jint JavaS |
| | | 64 | | /// If the value of <c>ScriptCacheTimeout</c> is <c>null</c>, the scripts are cached indefinitely. If a time value i |
| | | 65 | | /// </remarks> |
| | | 66 | | [ManifestSetting( |
| | | 67 | | DisplayName = "Script Cache Timeout", |
| | | 68 | | Description = "Duration for which compiled JavaScript expressions stay cached.", |
| | | 69 | | Category = "Performance", |
| | | 70 | | DefaultValue = "1.00:00:00", |
| | | 71 | | RestartRequired = true)] |
| | 0 | 72 | | public TimeSpan? ScriptCacheTimeout { get; set; } = TimeSpan.FromDays(1); |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Disables the generation of variable wrappers. E.g. <c>getMyVariable()</c> will no longer be available for variab |
| | | 76 | | /// This is useful if your application requires the use of invalid JavaScript variable names. |
| | | 77 | | /// </summary> |
| | | 78 | | [ManifestSetting( |
| | | 79 | | DisplayName = "Disable Variable Wrappers", |
| | | 80 | | Description = "Disable generated JavaScript variable wrapper functions.", |
| | | 81 | | Category = "Runtime", |
| | | 82 | | DefaultValue = "false", |
| | | 83 | | Advanced = true, |
| | | 84 | | RestartRequired = true)] |
| | 0 | 85 | | public bool DisableWrappers { get; set; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Disables copying workflow variables into the Jint engine and copying them back into the workflow execution conte |
| | | 89 | | /// Disabling this option will increase performance but will also prevent you from accessing workflow variables from |
| | | 90 | | /// </summary> |
| | | 91 | | [ManifestSetting( |
| | | 92 | | DisplayName = "Disable Variable Copying", |
| | | 93 | | Description = "Disable copying workflow variables into and out of the JavaScript engine.", |
| | | 94 | | Category = "Performance", |
| | | 95 | | DefaultValue = "false", |
| | | 96 | | Advanced = true, |
| | | 97 | | RestartRequired = true)] |
| | 0 | 98 | | public bool DisableVariableCopying { get; set; } |
| | | 99 | | |
| | | 100 | | public void ConfigureServices(IServiceCollection services) |
| | | 101 | | { |
| | 0 | 102 | | services.Configure<JintOptions>(options => |
| | 0 | 103 | | { |
| | 0 | 104 | | options.AllowClrAccess = AllowClrAccess; |
| | 0 | 105 | | options.AllowConfigurationAccess = AllowConfigurationAccess; |
| | 0 | 106 | | options.ScriptCacheTimeout = ScriptCacheTimeout; |
| | 0 | 107 | | options.DisableWrappers = DisableWrappers; |
| | 0 | 108 | | options.DisableVariableCopying = DisableVariableCopying; |
| | 0 | 109 | | }); |
| | 0 | 110 | | services.Configure<ExpressionOptions>(JavaScriptExceptionTypeAliasRegistrar.Register); |
| | 0 | 111 | | services.Configure<SerializationTypeOptions>(JavaScriptExceptionTypeAliasRegistrar.Register); |
| | | 112 | | |
| | | 113 | | // JavaScript services. |
| | 0 | 114 | | services |
| | 0 | 115 | | .AddScoped<IJavaScriptEvaluator, JintJavaScriptEvaluator>() |
| | 0 | 116 | | .AddExpressionDescriptorProvider<JavaScriptExpressionDescriptorProvider>(); |
| | | 117 | | |
| | | 118 | | // Type definition services. |
| | 0 | 119 | | services |
| | 0 | 120 | | .AddScoped<ITypeDefinitionService, TypeDefinitionService>() |
| | 0 | 121 | | .AddScoped<ITypeDescriber, TypeDescriber>() |
| | 0 | 122 | | .AddScoped<ITypeDefinitionDocumentRenderer, TypeDefinitionDocumentRenderer>() |
| | 0 | 123 | | .AddSingleton<ITypeAliasRegistry, Services.TypeAliasRegistry>() |
| | 0 | 124 | | .AddFunctionDefinitionProvider<CommonFunctionsDefinitionProvider>() |
| | 0 | 125 | | .AddFunctionDefinitionProvider<ActivityOutputFunctionsDefinitionProvider>() |
| | 0 | 126 | | .AddFunctionDefinitionProvider<RunJavaScriptFunctionsDefinitionProvider>() |
| | 0 | 127 | | .AddTypeDefinitionProvider<CommonTypeDefinitionProvider>() |
| | 0 | 128 | | .AddTypeDefinitionProvider<VariableTypeDefinitionProvider>() |
| | 0 | 129 | | .AddTypeDefinitionProvider<WorkflowVariablesTypeDefinitionProvider>() |
| | 0 | 130 | | .AddVariableDefinitionProvider<WorkflowVariablesVariableProvider>(); |
| | | 131 | | |
| | | 132 | | // Handlers. |
| | 0 | 133 | | services.AddNotificationHandlersFrom<JavaScriptFeature>(); |
| | | 134 | | |
| | | 135 | | // Type Script definitions. |
| | 0 | 136 | | services.AddFunctionDefinitionProvider<InputFunctionsDefinitionProvider>(); |
| | | 137 | | |
| | | 138 | | // UI property handlers. |
| | 0 | 139 | | services.AddScoped<IPropertyUIHandler, RunJavaScriptOptionsProvider>(); |
| | | 140 | | |
| | | 141 | | // Hosted services. |
| | 0 | 142 | | services.AddHostedService<RegisterVariableTypesWithJavaScriptHostedService>(); |
| | 0 | 143 | | } |
| | | 144 | | } |