2023-01-20 18:26:38 +00:00
|
|
|
|
using System.Text.Json;
|
2023-02-03 13:29:44 +00:00
|
|
|
|
using System.Text.Json.Serialization;
|
2023-03-15 11:29:07 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2023-01-20 18:26:38 +00:00
|
|
|
|
using Elsa.Expressions.Models;
|
2022-12-31 15:16:43 +00:00
|
|
|
|
using Elsa.Extensions;
|
2023-02-13 11:15:20 +00:00
|
|
|
|
using Elsa.JavaScript.Contracts;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
using Elsa.JavaScript.Notifications;
|
|
|
|
|
|
using Elsa.JavaScript.Options;
|
2023-03-03 21:47:23 +00:00
|
|
|
|
using Elsa.Mediator.Contracts;
|
2023-03-22 21:50:26 +00:00
|
|
|
|
using Elsa.Workflows.Core.Models;
|
2022-12-15 21:53:56 +00:00
|
|
|
|
using Humanizer;
|
2022-01-12 10:43:46 +00:00
|
|
|
|
using Jint;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
2023-02-03 13:29:44 +00:00
|
|
|
|
// ReSharper disable ConvertClosureToMethodGroup
|
|
|
|
|
|
|
2023-02-13 11:15:20 +00:00
|
|
|
|
namespace Elsa.JavaScript.Services;
|
2023-01-04 19:36:38 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides a JavaScript evaluator using Jint.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class JintJavaScriptEvaluator : IJavaScriptEvaluator
|
2022-01-12 10:43:46 +00:00
|
|
|
|
{
|
2023-01-04 19:36:38 +00:00
|
|
|
|
private readonly IEventPublisher _mediator;
|
|
|
|
|
|
private readonly JintOptions _jintOptions;
|
|
|
|
|
|
|
2022-11-30 20:52:41 +00:00
|
|
|
|
/// <summary>
|
2023-01-04 19:36:38 +00:00
|
|
|
|
/// Constructor.
|
2022-11-30 20:52:41 +00:00
|
|
|
|
/// </summary>
|
2023-03-20 18:55:11 +00:00
|
|
|
|
public JintJavaScriptEvaluator(IEventPublisher mediator, IOptions<JintOptions> scriptOptions)
|
2022-01-12 10:43:46 +00:00
|
|
|
|
{
|
2023-01-04 19:36:38 +00:00
|
|
|
|
_mediator = mediator;
|
|
|
|
|
|
_jintOptions = scriptOptions.Value;
|
|
|
|
|
|
}
|
2022-01-12 10:43:46 +00:00
|
|
|
|
|
2023-01-04 19:36:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public async Task<object?> EvaluateAsync(string expression,
|
|
|
|
|
|
Type returnType,
|
|
|
|
|
|
ExpressionExecutionContext context,
|
|
|
|
|
|
Action<Engine>? configureEngine = default,
|
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
var engine = await GetConfiguredEngine(configureEngine, context, cancellationToken);
|
|
|
|
|
|
var result = ExecuteExpressionAndGetResult(engine, expression);
|
2022-01-12 10:43:46 +00:00
|
|
|
|
|
2023-01-04 19:36:38 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2022-01-12 10:43:46 +00:00
|
|
|
|
|
2023-01-04 19:36:38 +00:00
|
|
|
|
private async Task<Engine> GetConfiguredEngine(Action<Engine>? configureEngine, ExpressionExecutionContext context, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
var engine = new Engine(opts =>
|
2022-01-12 10:43:46 +00:00
|
|
|
|
{
|
2023-01-04 19:36:38 +00:00
|
|
|
|
if (_jintOptions.AllowClrAccess)
|
|
|
|
|
|
opts.AllowClr();
|
|
|
|
|
|
});
|
2022-01-12 10:43:46 +00:00
|
|
|
|
|
2023-01-04 19:36:38 +00:00
|
|
|
|
configureEngine?.Invoke(engine);
|
2023-02-03 13:29:44 +00:00
|
|
|
|
|
2023-01-04 19:36:38 +00:00
|
|
|
|
// Add common functions.
|
2023-02-03 13:29:44 +00:00
|
|
|
|
engine.SetValue("getWorkflowInstanceId", (Func<string>)(() => context.GetActivityExecutionContext().WorkflowExecutionContext.Id));
|
|
|
|
|
|
engine.SetValue("setCorrelationId", (Action<string?>)(value => context.GetActivityExecutionContext().WorkflowExecutionContext.CorrelationId = value));
|
|
|
|
|
|
engine.SetValue("getCorrelationId", (Func<string?>)(() => context.GetActivityExecutionContext().WorkflowExecutionContext.CorrelationId));
|
2023-03-22 21:50:26 +00:00
|
|
|
|
engine.SetValue("setVariable", (Action<string, object>)((id, value) => context.SetVariable(id, value)));
|
|
|
|
|
|
engine.SetValue("getVariable", (Func<string, object?>)(id => context.GetVariable(id)));
|
2023-03-13 11:31:23 +00:00
|
|
|
|
engine.SetValue("getInput", (Func<string, object?>)(name => context.GetWorkflowExecutionContext().Input.GetValue(name)));
|
2023-02-03 13:29:44 +00:00
|
|
|
|
|
2023-03-24 21:25:48 +00:00
|
|
|
|
// Create variable getters and setters for each variable.
|
|
|
|
|
|
CreateVariableAccessors(engine, context);
|
2023-02-03 13:29:44 +00:00
|
|
|
|
|
|
|
|
|
|
engine.SetValue("isNullOrWhiteSpace", (Func<string, bool>)(value => string.IsNullOrWhiteSpace(value)));
|
|
|
|
|
|
engine.SetValue("isNullOrEmpty", (Func<string, bool>)(value => string.IsNullOrEmpty(value)));
|
|
|
|
|
|
engine.SetValue("toJson", (Func<object, string>)(value => Serialize(value)));
|
2023-05-08 20:38:59 +00:00
|
|
|
|
engine.SetValue("parseGuid", (Func<string, Guid>)(value => Guid.Parse(value)));
|
|
|
|
|
|
engine.SetValue("newGuid", (Func<Guid>)(() => Guid.NewGuid()));
|
|
|
|
|
|
engine.SetValue("newGuidString", (Func<string>)(() => Guid.NewGuid().ToString()));
|
|
|
|
|
|
engine.SetValue("newShortGuid", (Func<string>)(() => Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", "")));
|
2022-01-12 10:43:46 +00:00
|
|
|
|
|
2023-01-04 19:36:38 +00:00
|
|
|
|
// Add common .NET types.
|
|
|
|
|
|
engine.RegisterType<DateTime>();
|
|
|
|
|
|
engine.RegisterType<DateTimeOffset>();
|
|
|
|
|
|
engine.RegisterType<TimeSpan>();
|
2023-02-03 13:29:44 +00:00
|
|
|
|
engine.RegisterType<Guid>();
|
2022-01-12 10:43:46 +00:00
|
|
|
|
|
2023-01-04 19:36:38 +00:00
|
|
|
|
// Allow listeners invoked by the mediator to configure the engine.
|
|
|
|
|
|
await _mediator.PublishAsync(new EvaluatingJavaScript(engine, context), cancellationToken);
|
2022-01-12 10:43:46 +00:00
|
|
|
|
|
2023-01-04 19:36:38 +00:00
|
|
|
|
return engine;
|
|
|
|
|
|
}
|
2022-12-15 21:53:56 +00:00
|
|
|
|
|
2023-03-24 21:25:48 +00:00
|
|
|
|
private static void CreateVariableAccessors(Engine engine, ExpressionExecutionContext context)
|
2023-01-04 19:36:38 +00:00
|
|
|
|
{
|
2023-03-22 21:50:26 +00:00
|
|
|
|
var variableNames = GetVariableNamesInScope(context).ToList();
|
2022-12-15 21:53:56 +00:00
|
|
|
|
|
2023-03-22 21:50:26 +00:00
|
|
|
|
foreach (var variableName in variableNames)
|
2022-01-12 10:43:46 +00:00
|
|
|
|
{
|
2023-03-22 21:50:26 +00:00
|
|
|
|
var pascalName = variableName.Pascalize();
|
|
|
|
|
|
engine.SetValue($"get{pascalName}", (Func<object?>)(() => GetVariableInScope(context, variableName)));
|
|
|
|
|
|
engine.SetValue($"set{pascalName}", (Action<object?>)(value => SetVariableInScope(context, variableName, value)));
|
2022-01-12 10:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-22 21:50:26 +00:00
|
|
|
|
|
2023-03-24 21:25:48 +00:00
|
|
|
|
private static IEnumerable<string> GetVariableNamesInScope(ExpressionExecutionContext context) =>
|
|
|
|
|
|
EnumerateVariablesInScope(context)
|
|
|
|
|
|
.Select(x => x.Name)
|
|
|
|
|
|
.Where(x => !string.IsNullOrWhiteSpace(x))
|
|
|
|
|
|
.Distinct();
|
2023-03-22 21:50:26 +00:00
|
|
|
|
|
|
|
|
|
|
private static object GetVariableInScope(ExpressionExecutionContext context, string variableName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var q = from variable in EnumerateVariablesInScope(context)
|
|
|
|
|
|
where variable.Name == variableName
|
|
|
|
|
|
where variable.TryGet(context, out _)
|
|
|
|
|
|
select variable.Get(context);
|
|
|
|
|
|
|
|
|
|
|
|
var value = q.FirstOrDefault();
|
|
|
|
|
|
return value!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void SetVariableInScope(ExpressionExecutionContext context, string variableName, object? value)
|
|
|
|
|
|
{
|
|
|
|
|
|
var q = from v in EnumerateVariablesInScope(context)
|
|
|
|
|
|
where v.Name == variableName
|
|
|
|
|
|
where v.TryGet(context, out _)
|
|
|
|
|
|
select v;
|
|
|
|
|
|
|
|
|
|
|
|
var variable = q.FirstOrDefault();
|
|
|
|
|
|
variable?.Set(context, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<Variable> EnumerateVariablesInScope(ExpressionExecutionContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var currentScope = context;
|
|
|
|
|
|
|
|
|
|
|
|
while (currentScope != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!currentScope.TryGetActivityExecutionContext(out var activityExecutionContext))
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
var variables = activityExecutionContext.Variables;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var variable in variables)
|
|
|
|
|
|
yield return variable;
|
|
|
|
|
|
|
|
|
|
|
|
currentScope = currentScope.ParentContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-21 10:47:20 +00:00
|
|
|
|
private static object ExecuteExpressionAndGetResult(Engine engine, string expression)
|
2023-01-04 19:36:38 +00:00
|
|
|
|
{
|
|
|
|
|
|
var result = engine.Evaluate(expression);
|
|
|
|
|
|
return result.ToObject();
|
|
|
|
|
|
}
|
2023-02-03 13:29:44 +00:00
|
|
|
|
|
2023-02-21 10:47:20 +00:00
|
|
|
|
private static string Serialize(object value)
|
2023-02-03 13:29:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
var options = new JsonSerializerOptions();
|
|
|
|
|
|
options.Converters.Add(new JsonStringEnumConverter());
|
|
|
|
|
|
|
|
|
|
|
|
return JsonSerializer.Serialize(value, options);
|
|
|
|
|
|
}
|
2022-01-12 10:43:46 +00:00
|
|
|
|
}
|