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