| | | 1 | | using System.Dynamic; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Expressions.JavaScript.Extensions; |
| | | 6 | | using Elsa.Expressions.JavaScript.Helpers; |
| | | 7 | | using Elsa.Expressions.JavaScript.Notifications; |
| | | 8 | | using Elsa.Expressions.JavaScript.Options; |
| | | 9 | | using Elsa.Mediator.Contracts; |
| | | 10 | | using Elsa.Workflows.Activities; |
| | | 11 | | using JetBrains.Annotations; |
| | | 12 | | using Jint.Native; |
| | | 13 | | using Microsoft.Extensions.Options; |
| | | 14 | | |
| | | 15 | | namespace Elsa.Expressions.JavaScript.Handlers; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// A handler that configures the Jint engine with workflow variables. |
| | | 19 | | /// </summary> |
| | | 20 | | [UsedImplicitly] |
| | 520 | 21 | | public partial class ConfigureEngineWithVariables(IOptions<JintOptions> options) : INotificationHandler<EvaluatingJavaSc |
| | | 22 | | { |
| | 373 | 23 | | private bool IsEnabled => options.Value is { DisableWrappers: false, DisableVariableCopying: false }; |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public Task HandleAsync(EvaluatingJavaScript notification, CancellationToken cancellationToken) |
| | | 27 | | { |
| | 188 | 28 | | if (!IsEnabled) |
| | 0 | 29 | | return Task.CompletedTask; |
| | | 30 | | |
| | 188 | 31 | | CopyVariablesIntoEngine(notification); |
| | 188 | 32 | | return Task.CompletedTask; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public Task HandleAsync(EvaluatedJavaScript notification, CancellationToken cancellationToken) |
| | | 36 | | { |
| | 185 | 37 | | if (!IsEnabled) |
| | 0 | 38 | | return Task.CompletedTask; |
| | | 39 | | |
| | 185 | 40 | | CopyVariablesIntoWorkflowExecutionContext(notification); |
| | 185 | 41 | | return Task.CompletedTask; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | private void CopyVariablesIntoWorkflowExecutionContext(EvaluatedJavaScript notification) |
| | | 45 | | { |
| | 185 | 46 | | var context = notification.Context; |
| | 185 | 47 | | var engine = notification.Engine; |
| | 185 | 48 | | var variablesContainer = (IDictionary<string, object?>)engine.GetValue("variables").ToObject()!; |
| | 185 | 49 | | var inputNames = GetInputNames(context).FilterInvalidVariableNames().Distinct().ToList(); |
| | | 50 | | |
| | 396 | 51 | | foreach (var (variableName, variableValue) in variablesContainer) |
| | | 52 | | { |
| | 13 | 53 | | if (inputNames.Contains(variableName)) |
| | | 54 | | continue; |
| | | 55 | | |
| | 13 | 56 | | var processedValue = variableValue is JsObject jsValue ? jsValue.ToObject() : variableValue ?? context.GetVa |
| | 13 | 57 | | context.SetVariable(variableName, processedValue); |
| | | 58 | | } |
| | 185 | 59 | | } |
| | | 60 | | |
| | | 61 | | private void CopyVariablesIntoEngine(EvaluatingJavaScript notification) |
| | | 62 | | { |
| | 188 | 63 | | var engine = notification.Engine; |
| | 188 | 64 | | var context = notification.Context; |
| | 188 | 65 | | var expression = notification.Expression; |
| | 188 | 66 | | var variableNames = GetUsedVariableNames(context, expression).ToList(); |
| | 188 | 67 | | var variablesContainer = (IDictionary<string, object?>)new ExpandoObject(); |
| | | 68 | | |
| | 392 | 69 | | foreach (var variableName in variableNames) |
| | | 70 | | { |
| | 8 | 71 | | var variableValue = context.GetVariableInScope(variableName); |
| | 8 | 72 | | variableValue = ObjectConverterHelper.ProcessVariableValue(engine, variableValue); |
| | 8 | 73 | | variablesContainer[variableName] = variableValue; |
| | | 74 | | } |
| | | 75 | | |
| | 188 | 76 | | engine.SetValue("variables", variablesContainer); |
| | 188 | 77 | | } |
| | | 78 | | |
| | | 79 | | private IEnumerable<string> GetUsedVariableNames(ExpressionExecutionContext context, string expression) |
| | | 80 | | { |
| | 188 | 81 | | var variableNames = context.GetVariableNamesInScope().FilterInvalidVariableNames(); |
| | | 82 | | |
| | 188 | 83 | | var variableNamesInScript = ExtractVariableNamesRegex().Matches(expression) |
| | 9 | 84 | | .Select(m => m.Groups[1].Value) |
| | 188 | 85 | | .ToList(); |
| | | 86 | | |
| | 321 | 87 | | return variableNames.Where(x => variableNamesInScript.Contains(x)); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private IEnumerable<string> GetInputNames(ExpressionExecutionContext context) |
| | | 91 | | { |
| | 185 | 92 | | var activityExecutionContext = context.TryGetActivityExecutionContext(out var aec) ? aec : null; |
| | | 93 | | |
| | 538 | 94 | | while (activityExecutionContext != null) |
| | | 95 | | { |
| | 353 | 96 | | if (activityExecutionContext.Activity is Workflow workflow) |
| | | 97 | | { |
| | 124 | 98 | | var inputDefinitions = workflow.Inputs; |
| | | 99 | | |
| | 282 | 100 | | foreach (var inputDefinition in inputDefinitions) |
| | 17 | 101 | | yield return inputDefinition.Name; |
| | | 102 | | } |
| | | 103 | | |
| | 742 | 104 | | foreach (var syntheticProperty in activityExecutionContext.Activity.SyntheticProperties) |
| | | 105 | | { |
| | 18 | 106 | | yield return syntheticProperty.Key; |
| | | 107 | | } |
| | | 108 | | |
| | 353 | 109 | | activityExecutionContext = activityExecutionContext.ParentActivityExecutionContext; |
| | | 110 | | } |
| | 185 | 111 | | } |
| | | 112 | | |
| | | 113 | | [GeneratedRegex(@"variables\.(\w+)(?:\.\w+)*")] |
| | | 114 | | private static partial Regex ExtractVariableNamesRegex(); |
| | | 115 | | } |