< 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: 148
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.Caching.ShellFeatures;
 3using Elsa.Expressions.JavaScript.Activities;
 4using Elsa.Expressions.JavaScript.Contracts;
 5using Elsa.Expressions.JavaScript.Extensions;
 6using Elsa.Expressions.JavaScript.HostedServices;
 7using Elsa.Expressions.JavaScript.Options;
 8using Elsa.Expressions.JavaScript.Providers;
 9using Elsa.Expressions.JavaScript.Services;
 10using Elsa.Expressions.JavaScript.TypeDefinitions.Contracts;
 11using Elsa.Expressions.JavaScript.TypeDefinitions.Services;
 12using Elsa.Expressions.Options;
 13using Elsa.Extensions;
 14using Elsa.Workflows;
 15using JetBrains.Annotations;
 16using Microsoft.Extensions.DependencyInjection;
 17using Elsa.Common.Serialization;
 18using Elsa.Common.ShellFeatures;
 19using Elsa.Expressions.ShellFeatures;
 20using Elsa.Platform.PackageManifest.Generator.Hints;
 21
 22namespace 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]
 34public 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)]
 048    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)]
 061    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)]
 076    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)]
 089    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)]
 0102    public bool DisableVariableCopying { get; set; }
 103
 104    public void ConfigureServices(IServiceCollection services)
 105    {
 0106        services.Configure<JintOptions>(options =>
 0107        {
 0108            options.AllowClrAccess = AllowClrAccess;
 0109            options.AllowConfigurationAccess = AllowConfigurationAccess;
 0110            options.ScriptCacheTimeout = ScriptCacheTimeout;
 0111            options.DisableWrappers = DisableWrappers;
 0112            options.DisableVariableCopying = DisableVariableCopying;
 0113        });
 0114        services.Configure<ExpressionOptions>(JavaScriptExceptionTypeAliasRegistrar.Register);
 0115        services.Configure<SerializationTypeOptions>(JavaScriptExceptionTypeAliasRegistrar.Register);
 116
 117        // JavaScript services.
 0118        services
 0119            .AddScoped<IJavaScriptEvaluator, JintJavaScriptEvaluator>()
 0120            .AddExpressionDescriptorProvider<JavaScriptExpressionDescriptorProvider>();
 121
 122        // Type definition services.
 0123        services
 0124            .AddScoped<ITypeDefinitionService, TypeDefinitionService>()
 0125            .AddScoped<ITypeDescriber, TypeDescriber>()
 0126            .AddScoped<ITypeDefinitionDocumentRenderer, TypeDefinitionDocumentRenderer>()
 0127            .AddSingleton<ITypeAliasRegistry, Services.TypeAliasRegistry>()
 0128            .AddFunctionDefinitionProvider<CommonFunctionsDefinitionProvider>()
 0129            .AddFunctionDefinitionProvider<ActivityOutputFunctionsDefinitionProvider>()
 0130            .AddFunctionDefinitionProvider<RunJavaScriptFunctionsDefinitionProvider>()
 0131            .AddTypeDefinitionProvider<CommonTypeDefinitionProvider>()
 0132            .AddTypeDefinitionProvider<VariableTypeDefinitionProvider>()
 0133            .AddTypeDefinitionProvider<WorkflowVariablesTypeDefinitionProvider>()
 0134            .AddVariableDefinitionProvider<WorkflowVariablesVariableProvider>();
 135
 136        // Handlers.
 0137        services.AddNotificationHandlersFrom<JavaScriptFeature>();
 138
 139        // Type Script definitions.
 0140        services.AddFunctionDefinitionProvider<InputFunctionsDefinitionProvider>();
 141
 142        // UI property handlers.
 0143        services.AddScoped<IPropertyUIHandler, RunJavaScriptOptionsProvider>();
 144
 145        // Hosted services.
 0146        services.AddHostedService<RegisterVariableTypesWithJavaScriptHostedService>();
 0147    }
 148}