* Add handlers and improve JavaScript engine configuration Added several new handlers for configuring the JavaScript engine with common types, functions, variable accessors, and input/output accessors. Refactored the JintJavaScriptEvaluator for better clarity and modularity by breaking down configuration steps into separate methods. Also replaced `IsInsideCompositeActivity` with `IsContainedWithinCompositeActivity` for better semantic consistency. * Refactor to use primary constructor syntax for TypeDefinitionProviders Updated CommonTypeDefinitionProvider, VariableTypeDefinitionProvider, and ActivityOutputFunctionsDefinitionProvider to use primary constructors for dependency injection. This change reduces redundancy and simplifies the code structure for better readability and maintainability. * Add byte conversion functions to JavaScript engine Introduced functions for converting bytes to/from strings and Base64. Updated CommonFunctionsDefinitionProvider and ConfigureEngineWithCommonFunctions to incorporate these new functions. This enhances the JavaScript engine's ability to handle byte array manipulations. * Add tests for JavaScript byte and string conversions. Introduce integration tests to verify byte array to string, string to byte array, byte array to Base64, and Base64 to byte array conversions using JavaScript functions. Ensure accurate transformation of data within different encoding scenarios. * Add meaningful summaries to Jint engine configuration handlers Updated comment summaries in four handler classes to provide clear and concise descriptions of their purpose. This helps improve code readability and understanding for future developers. * Rename and merge variable and input/output handlers Merged the variable accessor logic into the input/output handler and renamed the class to reflect its broader functionality. This consolidation ensures the accessors are registered in the right order. * Add string base64 conversion functions Introduced `stringToBase64` and `stringFromBase64` functions to handle base64 encoding and decoding of strings. Updated corresponding provider, handler, and added tests to ensure functionality.
39 lines
1.7 KiB
C#
39 lines
1.7 KiB
C#
using Elsa.Common.Models;
|
|
using Elsa.JavaScript.Contracts;
|
|
using Elsa.JavaScript.TypeDefinitions.Abstractions;
|
|
using Elsa.JavaScript.TypeDefinitions.Models;
|
|
using Elsa.Workflows.Management;
|
|
using Elsa.Workflows.Management.Contracts;
|
|
using Elsa.Workflows.Management.Entities;
|
|
using Humanizer;
|
|
using JetBrains.Annotations;
|
|
|
|
namespace Elsa.JavaScript.Providers;
|
|
|
|
/// Produces <see cref="FunctionDefinition"/>s for common functions.
|
|
[UsedImplicitly]
|
|
internal class InputFunctionsDefinitionProvider(ITypeAliasRegistry typeAliasRegistry, IWorkflowDefinitionService workflowDefinitionService)
|
|
: FunctionDefinitionProvider
|
|
{
|
|
protected override async ValueTask<IEnumerable<FunctionDefinition>> GetFunctionDefinitionsAsync(TypeDefinitionContext context)
|
|
{
|
|
var cancellationToken = context.CancellationToken;
|
|
var workflow = context.Workflow;
|
|
var workflowDefinition = await workflowDefinitionService.FindWorkflowDefinitionAsync(workflow.Identity.DefinitionId, VersionOptions.SpecificVersion(workflow.Identity.Version), cancellationToken);
|
|
return workflowDefinition == null ? Array.Empty<FunctionDefinition>() : GetFunctionDefinitionsAsync(workflowDefinition);
|
|
}
|
|
|
|
private IEnumerable<FunctionDefinition> GetFunctionDefinitionsAsync(WorkflowDefinition workflowDefinition)
|
|
{
|
|
// Input argument getters.
|
|
foreach (var input in workflowDefinition.Inputs)
|
|
{
|
|
var pascalName = input.Name.Pascalize();
|
|
var variableType = input.Type;
|
|
var typeAlias = typeAliasRegistry.TryGetAlias(variableType, out var alias) ? alias : "any";
|
|
|
|
// get{Input}.
|
|
yield return CreateFunctionDefinition(builder => builder.Name($"get{pascalName}").ReturnType(typeAlias));
|
|
}
|
|
}
|
|
} |