< 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: 33
Coverable lines: 33
Total lines: 106
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.Extensions;
 12using Elsa.Workflows;
 13using JetBrains.Annotations;
 14using Microsoft.Extensions.DependencyInjection;
 15
 16namespace 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]
 26public 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>
 033    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>
 039    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>
 048    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>
 054    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>
 060    public bool DisableVariableCopying { get; set; }
 61
 62    public void ConfigureServices(IServiceCollection services)
 63    {
 064        services.Configure<JintOptions>(options =>
 065        {
 066            options.AllowClrAccess = AllowClrAccess;
 067            options.AllowConfigurationAccess = AllowConfigurationAccess;
 068            options.ScriptCacheTimeout = ScriptCacheTimeout;
 069            options.DisableWrappers = DisableWrappers;
 070            options.DisableVariableCopying = DisableVariableCopying;
 071        });
 72
 73        // JavaScript services.
 074        services
 075            .AddScoped<IJavaScriptEvaluator, JintJavaScriptEvaluator>()
 076            .AddExpressionDescriptorProvider<JavaScriptExpressionDescriptorProvider>();
 77
 78        // Type definition services.
 079        services
 080            .AddScoped<ITypeDefinitionService, TypeDefinitionService>()
 081            .AddScoped<ITypeDescriber, TypeDescriber>()
 082            .AddScoped<ITypeDefinitionDocumentRenderer, TypeDefinitionDocumentRenderer>()
 083            .AddSingleton<ITypeAliasRegistry, TypeAliasRegistry>()
 084            .AddFunctionDefinitionProvider<CommonFunctionsDefinitionProvider>()
 085            .AddFunctionDefinitionProvider<ActivityOutputFunctionsDefinitionProvider>()
 086            .AddFunctionDefinitionProvider<RunJavaScriptFunctionsDefinitionProvider>()
 087            .AddTypeDefinitionProvider<CommonTypeDefinitionProvider>()
 088            .AddTypeDefinitionProvider<VariableTypeDefinitionProvider>()
 089            .AddTypeDefinitionProvider<WorkflowVariablesTypeDefinitionProvider>()
 090            .AddVariableDefinitionProvider<WorkflowVariablesVariableProvider>();
 91
 92        // Handlers.
 093        services.AddNotificationHandlersFrom<JavaScriptFeature>();
 94
 95        // Type Script definitions.
 096        services.AddFunctionDefinitionProvider<InputFunctionsDefinitionProvider>();
 97
 98        // UI property handlers.
 099        services.AddScoped<IPropertyUIHandler, RunJavaScriptOptionsProvider>();
 100
 101        // Hosted services.
 0102        services.AddHostedService<RegisterVariableTypesWithJavaScriptHostedService>();
 0103    }
 104}
 105
 106