< Summary

Information
Class: Elsa.Expressions.JavaScript.ShellFeatures.JavaScriptFeature
Assembly: Elsa.Expressions.JavaScript
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/ShellFeatures/JavaScriptFeature.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 144
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_AllowClrAccess()100%210%
get_AllowConfigurationAccess()100%210%
get_ScriptCacheTimeout()100%210%
get_DisableWrappers()100%210%
get_DisableVariableCopying()100%210%
ConfigureServices(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/ShellFeatures/JavaScriptFeature.cs

#LineLine coverage
 1using CShells.Features;
 2using Elsa.Expressions.JavaScript.Activities;
 3using Elsa.Expressions.JavaScript.Contracts;
 4using Elsa.Expressions.JavaScript.Extensions;
 5using Elsa.Expressions.JavaScript.HostedServices;
 6using Elsa.Expressions.JavaScript.Options;
 7using Elsa.Expressions.JavaScript.Providers;
 8using Elsa.Expressions.JavaScript.Services;
 9using Elsa.Expressions.JavaScript.TypeDefinitions.Contracts;
 10using Elsa.Expressions.JavaScript.TypeDefinitions.Services;
 11using Elsa.Expressions.Options;
 12using Elsa.Extensions;
 13using Elsa.PackageManifest.Generator.Hints;
 14using Elsa.Workflows;
 15using Elsa.Workflows.Options;
 16using JetBrains.Annotations;
 17using Microsoft.Extensions.DependencyInjection;
 18using Elsa.Common.Serialization;
 19
 20namespace 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]
 30public 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)]
 044    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)]
 057    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)]
 072    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)]
 085    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)]
 098    public bool DisableVariableCopying { get; set; }
 99
 100    public void ConfigureServices(IServiceCollection services)
 101    {
 0102        services.Configure<JintOptions>(options =>
 0103        {
 0104            options.AllowClrAccess = AllowClrAccess;
 0105            options.AllowConfigurationAccess = AllowConfigurationAccess;
 0106            options.ScriptCacheTimeout = ScriptCacheTimeout;
 0107            options.DisableWrappers = DisableWrappers;
 0108            options.DisableVariableCopying = DisableVariableCopying;
 0109        });
 0110        services.Configure<ExpressionOptions>(JavaScriptExceptionTypeAliasRegistrar.Register);
 0111        services.Configure<SerializationTypeOptions>(JavaScriptExceptionTypeAliasRegistrar.Register);
 112
 113        // JavaScript services.
 0114        services
 0115            .AddScoped<IJavaScriptEvaluator, JintJavaScriptEvaluator>()
 0116            .AddExpressionDescriptorProvider<JavaScriptExpressionDescriptorProvider>();
 117
 118        // Type definition services.
 0119        services
 0120            .AddScoped<ITypeDefinitionService, TypeDefinitionService>()
 0121            .AddScoped<ITypeDescriber, TypeDescriber>()
 0122            .AddScoped<ITypeDefinitionDocumentRenderer, TypeDefinitionDocumentRenderer>()
 0123            .AddSingleton<ITypeAliasRegistry, Services.TypeAliasRegistry>()
 0124            .AddFunctionDefinitionProvider<CommonFunctionsDefinitionProvider>()
 0125            .AddFunctionDefinitionProvider<ActivityOutputFunctionsDefinitionProvider>()
 0126            .AddFunctionDefinitionProvider<RunJavaScriptFunctionsDefinitionProvider>()
 0127            .AddTypeDefinitionProvider<CommonTypeDefinitionProvider>()
 0128            .AddTypeDefinitionProvider<VariableTypeDefinitionProvider>()
 0129            .AddTypeDefinitionProvider<WorkflowVariablesTypeDefinitionProvider>()
 0130            .AddVariableDefinitionProvider<WorkflowVariablesVariableProvider>();
 131
 132        // Handlers.
 0133        services.AddNotificationHandlersFrom<JavaScriptFeature>();
 134
 135        // Type Script definitions.
 0136        services.AddFunctionDefinitionProvider<InputFunctionsDefinitionProvider>();
 137
 138        // UI property handlers.
 0139        services.AddScoped<IPropertyUIHandler, RunJavaScriptOptionsProvider>();
 140
 141        // Hosted services.
 0142        services.AddHostedService<RegisterVariableTypesWithJavaScriptHostedService>();
 0143    }
 144}