< Summary

Information
Class: Elsa.Expressions.JavaScript.Handlers.ConfigureEngineWithCommonFunctions
Assembly: Elsa.Expressions.JavaScript
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Handlers/ConfigureEngineWithCommonFunctions.cs
Line coverage
92%
Covered lines: 46
Uncovered lines: 4
Coverable lines: 50
Total lines: 94
Line coverage: 92%
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
.ctor(...)100%11100%
HandleAsync(...)100%11100%
Serialize(...)100%11100%
CreateJsonSerializerOptions()100%11100%
StreamToBytes(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Handlers/ConfigureEngineWithCommonFunctions.cs

#LineLine coverage
 1using System.Text;
 2using System.Text.Encodings.Web;
 3using System.Text.Json;
 4using System.Text.Json.Serialization;
 5using System.Text.RegularExpressions;
 6using System.Text.Unicode;
 7using Elsa.Extensions;
 8using Elsa.Expressions.JavaScript.Helpers;
 9using Elsa.Expressions.JavaScript.Notifications;
 10using Elsa.Expressions.JavaScript.Options;
 11using Elsa.Mediator.Contracts;
 12using JetBrains.Annotations;
 13using Microsoft.Extensions.Options;
 14
 15namespace Elsa.Expressions.JavaScript.Handlers;
 16
 17/// <summary>
 18/// A handler that configures the Jint engine with common functions.
 19/// </summary>
 20[UsedImplicitly]
 52021public class ConfigureEngineWithCommonFunctions(IOptions<JintOptions> options) : INotificationHandler<EvaluatingJavaScri
 22{
 52023    private readonly JsonSerializerOptions _jsonSerializerOptions = CreateJsonSerializerOptions();
 24
 25    /// <inheritdoc />
 26    public Task HandleAsync(EvaluatingJavaScript notification, CancellationToken cancellationToken)
 27    {
 18828        var engine = notification.Engine;
 18829        var context = notification.Context;
 30
 31        // Add common functions.
 18932        engine.SetValue("getWorkflowDefinitionId", (Func<string>)(() => context.GetWorkflowExecutionContext().Workflow.I
 18933        engine.SetValue("getWorkflowDefinitionVersionId", (Func<string>)(() => context.GetWorkflowExecutionContext().Wor
 18934        engine.SetValue("getWorkflowDefinitionVersion", (Func<int>)(() => context.GetWorkflowExecutionContext().Workflow
 18935        engine.SetValue("getWorkflowInstanceId", (Func<string>)(() => context.GetActivityExecutionContext().WorkflowExec
 18936        engine.SetValue("setCorrelationId", (Action<string?>)(value => context.GetActivityExecutionContext().WorkflowExe
 19037        engine.SetValue("getCorrelationId", (Func<string?>)(() => context.GetActivityExecutionContext().WorkflowExecutio
 18938        engine.SetValue("setWorkflowInstanceName", (Action<string?>)(value => context.GetWorkflowExecutionContext().Name
 19039        engine.SetValue("getWorkflowInstanceName", (Func<string?>)(() => context.GetWorkflowExecutionContext().Name));
 18840        engine.SetValue("setVariable", (Action<string, object>)((name, value) =>
 18841        {
 242            engine.SyncVariablesContainer(options, name, value);
 243            context.SetVariableInScope(name, value);
 19044        }));
 24545        engine.SetValue("getVariable", (Func<string, object?>)(name => context.GetVariableInScope(name)));
 18846        engine.SetValue("getInput", (Func<string, object?>)(name => context.GetInput(name)));
 18847        engine.SetValue("getOutputFrom", (Func<string, string?, object?>)((activityIdName, outputName) => context.GetOut
 18848        engine.SetValue("getLastResult", (Func<object?>)(() => context.GetLastResult()));
 19349        engine.SetValue("isNullOrWhiteSpace", (Func<string, bool>)(value => string.IsNullOrWhiteSpace(value)));
 19350        engine.SetValue("isNullOrEmpty", (Func<string, bool>)(value => string.IsNullOrEmpty(value)));
 19351        engine.SetValue("toJson", (Func<object, string>)(value => Serialize(value)));
 18952        engine.SetValue("parseGuid", (Func<string, Guid>)(value => Guid.Parse(value)));
 19053        engine.SetValue("newGuid", (Func<Guid>)(() => Guid.NewGuid()));
 19054        engine.SetValue("newGuidString", (Func<string>)(() => Guid.NewGuid().ToString()));
 19055        engine.SetValue("newShortGuid", (Func<string>)(() => Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteA
 19156        engine.SetValue("bytesToString", (Func<byte[], string>)(value => Encoding.UTF8.GetString(value)));
 19057        engine.SetValue("bytesFromString", (Func<string, byte[]>)(value => Encoding.UTF8.GetBytes(value)));
 19158        engine.SetValue("bytesToBase64", (Func<byte[], string>)(value => Convert.ToBase64String(value)));
 19059        engine.SetValue("bytesFromBase64", (Func<string, byte[]>)(value => Convert.FromBase64String(value)));
 19160        engine.SetValue("stringToBase64", (Func<string, string>)(value =>  Convert.ToBase64String(Encoding.UTF8.GetBytes
 19061        engine.SetValue("stringFromBase64", (Func<string, string>)(value => Encoding.UTF8.GetString(Convert.FromBase64St
 18862        engine.SetValue("streamToBytes", (Func<Stream, byte[]>)(value => StreamToBytes(value)));
 18863        engine.SetValue("streamToBase64", (Func<Stream, string>)(value => Convert.ToBase64String(StreamToBytes(value))))
 64
 65        // Deprecated, use newGuidString instead.
 18966        engine.SetValue("getGuidString", (Func<string>)(() => Guid.NewGuid().ToString()));
 67
 68        // Deprecated, use newShortGuid instead.
 18969        engine.SetValue("getShortGuid", (Func<string>)(() => Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteA
 18870        return Task.CompletedTask;
 71    }
 72
 73    private string Serialize(object value)
 74    {
 575        return JsonSerializer.Serialize(value, _jsonSerializerOptions);
 76    }
 77
 78    private static JsonSerializerOptions CreateJsonSerializerOptions()
 79    {
 52080        var options = new JsonSerializerOptions
 52081        {
 52082            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
 52083        };
 52084        options.Converters.Add(new JsonStringEnumConverter());
 52085        return options;
 86    }
 87
 88    private byte[] StreamToBytes(Stream stream)
 89    {
 090        using var memoryStream = new MemoryStream();
 091        stream.CopyTo(memoryStream);
 092        return memoryStream.ToArray();
 093    }
 94}