| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Security.Cryptography; |
| | | 3 | | using System.Text; |
| | | 4 | | using Acornima.Ast; |
| | | 5 | | using Elsa.Expressions.Helpers; |
| | | 6 | | using Elsa.Expressions.Models; |
| | | 7 | | using Elsa.Expressions.JavaScript.Contracts; |
| | | 8 | | using Elsa.Expressions.JavaScript.Helpers; |
| | | 9 | | using Elsa.Expressions.JavaScript.Notifications; |
| | | 10 | | using Elsa.Expressions.JavaScript.ObjectConverters; |
| | | 11 | | using Elsa.Expressions.JavaScript.Options; |
| | | 12 | | using Elsa.Mediator.Contracts; |
| | | 13 | | using Jint; |
| | | 14 | | using Jint.Runtime.Interop; |
| | | 15 | | using Microsoft.Extensions.Caching.Memory; |
| | | 16 | | using Microsoft.Extensions.Configuration; |
| | | 17 | | using Microsoft.Extensions.Options; |
| | | 18 | | |
| | | 19 | | // ReSharper disable ConvertClosureToMethodGroup |
| | | 20 | | namespace Elsa.Expressions.JavaScript.Services; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Provides a JavaScript evaluator using Jint. |
| | | 24 | | /// </summary> |
| | 113 | 25 | | public class JintJavaScriptEvaluator(IConfiguration configuration, INotificationSender mediator, IOptions<JintOptions> s |
| | | 26 | | : IJavaScriptEvaluator |
| | | 27 | | { |
| | 113 | 28 | | private readonly JintOptions _jintOptions = scriptOptions.Value; |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | [RequiresUnreferencedCode("The Jint library uses reflection and can't be statically analyzed.")] |
| | | 32 | | public async Task<object?> EvaluateAsync(string expression, |
| | | 33 | | Type returnType, |
| | | 34 | | ExpressionExecutionContext context, |
| | | 35 | | ExpressionEvaluatorOptions? options = null, |
| | | 36 | | Action<Engine>? configureEngine = null, |
| | | 37 | | CancellationToken cancellationToken = default) |
| | | 38 | | { |
| | 188 | 39 | | var engine = await GetConfiguredEngine(configureEngine, context, options, cancellationToken); |
| | 188 | 40 | | await mediator.SendAsync(new EvaluatingJavaScript(engine, context, expression), cancellationToken); |
| | 188 | 41 | | var result = ExecuteExpressionAndGetResult(engine, expression); |
| | 185 | 42 | | await mediator.SendAsync(new EvaluatedJavaScript(engine, context, expression, result), cancellationToken); |
| | | 43 | | |
| | 185 | 44 | | return result.ConvertTo(returnType); |
| | 185 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task<Engine> GetConfiguredEngine(Action<Engine>? configureEngine, ExpressionExecutionContext context, |
| | | 48 | | { |
| | 188 | 49 | | options ??= new(); |
| | | 50 | | |
| | 188 | 51 | | var engineOptions = new Jint.Options |
| | 188 | 52 | | { |
| | 188 | 53 | | ExperimentalFeatures = ExperimentalFeature.TaskInterop |
| | 188 | 54 | | }; |
| | | 55 | | |
| | 188 | 56 | | ConfigureClrAccess(engineOptions); |
| | 188 | 57 | | ConfigureObjectWrapper(engineOptions); |
| | 188 | 58 | | ConfigureObjectConverters(engineOptions); |
| | | 59 | | |
| | 188 | 60 | | await mediator.SendAsync(new CreatingJavaScriptEngine(engineOptions, context), cancellationToken); |
| | 188 | 61 | | _jintOptions.ConfigureEngineOptionsCallback(engineOptions, context); |
| | | 62 | | |
| | 188 | 63 | | var engine = new Engine(engineOptions); |
| | | 64 | | |
| | 188 | 65 | | configureEngine?.Invoke(engine); |
| | 188 | 66 | | ConfigureArgumentGetters(engine, options); |
| | 188 | 67 | | ConfigureConfigurationAccess(engine); |
| | 188 | 68 | | _jintOptions.ConfigureEngineCallback(engine, context); |
| | | 69 | | |
| | 188 | 70 | | return engine; |
| | 188 | 71 | | } |
| | | 72 | | |
| | | 73 | | private void ConfigureClrAccess(Jint.Options options) |
| | | 74 | | { |
| | 188 | 75 | | if (_jintOptions.AllowClrAccess) |
| | 17 | 76 | | options.AllowClr(); |
| | 188 | 77 | | } |
| | | 78 | | |
| | | 79 | | private void ConfigureObjectWrapper(Jint.Options options) |
| | | 80 | | { |
| | 188 | 81 | | options.SetWrapObjectHandler((engine, target, type) => |
| | 188 | 82 | | { |
| | 340 | 83 | | var instance = ObjectWrapper.Create(engine, target); |
| | 188 | 84 | | |
| | 340 | 85 | | if (ObjectArrayHelper.DetermineIfObjectIsArrayLikeClrCollection(target.GetType())) |
| | 322 | 86 | | instance.Prototype = engine.Intrinsics.Array.PrototypeObject; |
| | 188 | 87 | | |
| | 340 | 88 | | return instance; |
| | 188 | 89 | | }); |
| | 188 | 90 | | } |
| | | 91 | | |
| | | 92 | | private void ConfigureObjectConverters(Jint.Options options) |
| | | 93 | | { |
| | 188 | 94 | | options.Interop.ObjectConverters.AddRange([new ByteArrayConverter(), new EnumToStringConverter(), new JsonElemen |
| | 188 | 95 | | } |
| | | 96 | | |
| | | 97 | | private void ConfigureArgumentGetters(Engine engine, ExpressionEvaluatorOptions options) |
| | | 98 | | { |
| | 382 | 99 | | foreach (var argument in options.Arguments) |
| | 6 | 100 | | engine.SetValue($"get{argument.Key}", (Func<object?>)(() => argument.Value)); |
| | 188 | 101 | | } |
| | | 102 | | |
| | | 103 | | private void ConfigureConfigurationAccess(Engine engine) |
| | | 104 | | { |
| | 188 | 105 | | if (_jintOptions.AllowConfigurationAccess) |
| | 0 | 106 | | engine.SetValue("getConfig", (Func<string, object?>)(name => configuration.GetSection(name).Value)); |
| | 188 | 107 | | } |
| | | 108 | | |
| | | 109 | | private object? ExecuteExpressionAndGetResult(Engine engine, string expression) |
| | | 110 | | { |
| | 188 | 111 | | var preparedScript = GetOrCreatePrepareScript(expression); |
| | 186 | 112 | | var result = engine.Evaluate(preparedScript); |
| | 185 | 113 | | return result.UnwrapIfPromise().ToObject(); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private Prepared<Script> GetOrCreatePrepareScript(string expression) |
| | | 117 | | { |
| | 188 | 118 | | var cacheKey = "jint:script:" + Hash(expression); |
| | | 119 | | |
| | 188 | 120 | | return memoryCache.GetOrCreate(cacheKey, entry => |
| | 188 | 121 | | { |
| | 137 | 122 | | if (_jintOptions.ScriptCacheTimeout.HasValue) |
| | 137 | 123 | | entry.SetSlidingExpiration(_jintOptions.ScriptCacheTimeout.Value); |
| | 188 | 124 | | |
| | 137 | 125 | | return PrepareScript(expression); |
| | 188 | 126 | | })!; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | private Prepared<Script> PrepareScript(string expression) |
| | | 130 | | { |
| | 137 | 131 | | var prepareOptions = new ScriptPreparationOptions |
| | 137 | 132 | | { |
| | 137 | 133 | | ParsingOptions = new() |
| | 137 | 134 | | { |
| | 137 | 135 | | AllowReturnOutsideFunction = true |
| | 137 | 136 | | } |
| | 137 | 137 | | }; |
| | 137 | 138 | | return Engine.PrepareScript(expression, options: prepareOptions); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | private string Hash(string input) |
| | | 142 | | { |
| | 188 | 143 | | var bytes = Encoding.UTF8.GetBytes(input); |
| | 188 | 144 | | var hash = SHA256.HashData(bytes); |
| | 188 | 145 | | return Convert.ToBase64String(hash); |
| | | 146 | | } |
| | | 147 | | } |