| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.Encodings.Web; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | using System.Text.RegularExpressions; |
| | | 6 | | using System.Text.Unicode; |
| | | 7 | | using Elsa.Extensions; |
| | | 8 | | using Elsa.Expressions.JavaScript.Helpers; |
| | | 9 | | using Elsa.Expressions.JavaScript.Notifications; |
| | | 10 | | using Elsa.Expressions.JavaScript.Options; |
| | | 11 | | using Elsa.Mediator.Contracts; |
| | | 12 | | using JetBrains.Annotations; |
| | | 13 | | using Microsoft.Extensions.Options; |
| | | 14 | | |
| | | 15 | | namespace Elsa.Expressions.JavaScript.Handlers; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// A handler that configures the Jint engine with common functions. |
| | | 19 | | /// </summary> |
| | | 20 | | [UsedImplicitly] |
| | 520 | 21 | | public class ConfigureEngineWithCommonFunctions(IOptions<JintOptions> options) : INotificationHandler<EvaluatingJavaScri |
| | | 22 | | { |
| | 520 | 23 | | private readonly JsonSerializerOptions _jsonSerializerOptions = CreateJsonSerializerOptions(); |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public Task HandleAsync(EvaluatingJavaScript notification, CancellationToken cancellationToken) |
| | | 27 | | { |
| | 188 | 28 | | var engine = notification.Engine; |
| | 188 | 29 | | var context = notification.Context; |
| | | 30 | | |
| | | 31 | | // Add common functions. |
| | 189 | 32 | | engine.SetValue("getWorkflowDefinitionId", (Func<string>)(() => context.GetWorkflowExecutionContext().Workflow.I |
| | 189 | 33 | | engine.SetValue("getWorkflowDefinitionVersionId", (Func<string>)(() => context.GetWorkflowExecutionContext().Wor |
| | 189 | 34 | | engine.SetValue("getWorkflowDefinitionVersion", (Func<int>)(() => context.GetWorkflowExecutionContext().Workflow |
| | 189 | 35 | | engine.SetValue("getWorkflowInstanceId", (Func<string>)(() => context.GetActivityExecutionContext().WorkflowExec |
| | 189 | 36 | | engine.SetValue("setCorrelationId", (Action<string?>)(value => context.GetActivityExecutionContext().WorkflowExe |
| | 190 | 37 | | engine.SetValue("getCorrelationId", (Func<string?>)(() => context.GetActivityExecutionContext().WorkflowExecutio |
| | 189 | 38 | | engine.SetValue("setWorkflowInstanceName", (Action<string?>)(value => context.GetWorkflowExecutionContext().Name |
| | 190 | 39 | | engine.SetValue("getWorkflowInstanceName", (Func<string?>)(() => context.GetWorkflowExecutionContext().Name)); |
| | 188 | 40 | | engine.SetValue("setVariable", (Action<string, object>)((name, value) => |
| | 188 | 41 | | { |
| | 2 | 42 | | engine.SyncVariablesContainer(options, name, value); |
| | 2 | 43 | | context.SetVariableInScope(name, value); |
| | 190 | 44 | | })); |
| | 245 | 45 | | engine.SetValue("getVariable", (Func<string, object?>)(name => context.GetVariableInScope(name))); |
| | 188 | 46 | | engine.SetValue("getInput", (Func<string, object?>)(name => context.GetInput(name))); |
| | 188 | 47 | | engine.SetValue("getOutputFrom", (Func<string, string?, object?>)((activityIdName, outputName) => context.GetOut |
| | 188 | 48 | | engine.SetValue("getLastResult", (Func<object?>)(() => context.GetLastResult())); |
| | 193 | 49 | | engine.SetValue("isNullOrWhiteSpace", (Func<string, bool>)(value => string.IsNullOrWhiteSpace(value))); |
| | 193 | 50 | | engine.SetValue("isNullOrEmpty", (Func<string, bool>)(value => string.IsNullOrEmpty(value))); |
| | 193 | 51 | | engine.SetValue("toJson", (Func<object, string>)(value => Serialize(value))); |
| | 189 | 52 | | engine.SetValue("parseGuid", (Func<string, Guid>)(value => Guid.Parse(value))); |
| | 190 | 53 | | engine.SetValue("newGuid", (Func<Guid>)(() => Guid.NewGuid())); |
| | 190 | 54 | | engine.SetValue("newGuidString", (Func<string>)(() => Guid.NewGuid().ToString())); |
| | 190 | 55 | | engine.SetValue("newShortGuid", (Func<string>)(() => Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteA |
| | 191 | 56 | | engine.SetValue("bytesToString", (Func<byte[], string>)(value => Encoding.UTF8.GetString(value))); |
| | 190 | 57 | | engine.SetValue("bytesFromString", (Func<string, byte[]>)(value => Encoding.UTF8.GetBytes(value))); |
| | 191 | 58 | | engine.SetValue("bytesToBase64", (Func<byte[], string>)(value => Convert.ToBase64String(value))); |
| | 190 | 59 | | engine.SetValue("bytesFromBase64", (Func<string, byte[]>)(value => Convert.FromBase64String(value))); |
| | 191 | 60 | | engine.SetValue("stringToBase64", (Func<string, string>)(value => Convert.ToBase64String(Encoding.UTF8.GetBytes |
| | 190 | 61 | | engine.SetValue("stringFromBase64", (Func<string, string>)(value => Encoding.UTF8.GetString(Convert.FromBase64St |
| | 188 | 62 | | engine.SetValue("streamToBytes", (Func<Stream, byte[]>)(value => StreamToBytes(value))); |
| | 188 | 63 | | engine.SetValue("streamToBase64", (Func<Stream, string>)(value => Convert.ToBase64String(StreamToBytes(value)))) |
| | | 64 | | |
| | | 65 | | // Deprecated, use newGuidString instead. |
| | 189 | 66 | | engine.SetValue("getGuidString", (Func<string>)(() => Guid.NewGuid().ToString())); |
| | | 67 | | |
| | | 68 | | // Deprecated, use newShortGuid instead. |
| | 189 | 69 | | engine.SetValue("getShortGuid", (Func<string>)(() => Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteA |
| | 188 | 70 | | return Task.CompletedTask; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private string Serialize(object value) |
| | | 74 | | { |
| | 5 | 75 | | return JsonSerializer.Serialize(value, _jsonSerializerOptions); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | private static JsonSerializerOptions CreateJsonSerializerOptions() |
| | | 79 | | { |
| | 520 | 80 | | var options = new JsonSerializerOptions |
| | 520 | 81 | | { |
| | 520 | 82 | | Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) |
| | 520 | 83 | | }; |
| | 520 | 84 | | options.Converters.Add(new JsonStringEnumConverter()); |
| | 520 | 85 | | return options; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private byte[] StreamToBytes(Stream stream) |
| | | 89 | | { |
| | 0 | 90 | | using var memoryStream = new MemoryStream(); |
| | 0 | 91 | | stream.CopyTo(memoryStream); |
| | 0 | 92 | | return memoryStream.ToArray(); |
| | 0 | 93 | | } |
| | | 94 | | } |