From 3c65be6954025ac1908e00d57874b68d4f160f18 Mon Sep 17 00:00:00 2001 From: Zack Schwartz Date: Sun, 29 Sep 2024 16:44:32 -0500 Subject: [PATCH 1/3] Fixes crash on return #211 --- .../RaythaFunctionScriptEngine.cs | 135 +++++++++--------- 1 file changed, 67 insertions(+), 68 deletions(-) diff --git a/src/Raytha.Infrastructure/RaythaFunctions/RaythaFunctionScriptEngine.cs b/src/Raytha.Infrastructure/RaythaFunctions/RaythaFunctionScriptEngine.cs index 1c1b203d..44ad869d 100644 --- a/src/Raytha.Infrastructure/RaythaFunctions/RaythaFunctionScriptEngine.cs +++ b/src/Raytha.Infrastructure/RaythaFunctions/RaythaFunctionScriptEngine.cs @@ -31,85 +31,84 @@ public RaythaFunctionScriptEngine(IRaythaFunctionApi_V1 raythaFunctionApiV1, public async Task Evaluate(string code, string method, TimeSpan executeTimeout, CancellationToken cancellationToken) { - using (var _engine = new V8ScriptEngine()) - { - _engine.AddHostObject("API_V1", _raythaFunctionApiV1); - _engine.AddHostObject("CurrentOrganization", _currentOrganization); - _engine.AddHostObject("CurrentUser", _currentUser); - _engine.AddHostObject("Emailer", _emailer); - _engine.AddHostObject("HttpClient", _httpClient); - _engine.AddHostObject("host", new HostFunctions()); - _engine.AddHostObject("clr", new HostTypeCollection("mscorlib", "System", "System.Core", "System.Linq", "System.Collections")); - _engine.AddHostType(typeof(JavaScriptExtensions)); - _engine.AddHostType(typeof(Enumerable)); - _engine.AddHostType(typeof(ShortGuid)); - _engine.AddHostType(typeof(Guid)); - _engine.AddHostType(typeof(Convert)); - _engine.AddHostType(typeof(EmailMessage)); - _engine.Execute("var System = clr.System;"); - _engine.Execute(@" - class JsonResult { - constructor(obj) { - this.body = obj; - this.contentType = 'application/json'; - } - } + var _engine = new V8ScriptEngine(); + _engine.AddHostObject("API_V1", _raythaFunctionApiV1); + _engine.AddHostObject("CurrentOrganization", _currentOrganization); + _engine.AddHostObject("CurrentUser", _currentUser); + _engine.AddHostObject("Emailer", _emailer); + _engine.AddHostObject("HttpClient", _httpClient); + _engine.AddHostObject("host", new HostFunctions()); + _engine.AddHostObject("clr", new HostTypeCollection("mscorlib", "System", "System.Core", "System.Linq", "System.Collections")); + _engine.AddHostType(typeof(JavaScriptExtensions)); + _engine.AddHostType(typeof(Enumerable)); + _engine.AddHostType(typeof(ShortGuid)); + _engine.AddHostType(typeof(Guid)); + _engine.AddHostType(typeof(Convert)); + _engine.AddHostType(typeof(EmailMessage)); + _engine.Execute("var System = clr.System;"); + _engine.Execute(@" + class JsonResult { + constructor(obj) { + this.body = obj; + this.contentType = 'application/json'; + } + } - class HtmlResult { - constructor(html) { - this.body = html; - this.contentType = 'text/html'; - } - } + class HtmlResult { + constructor(html) { + this.body = html; + this.contentType = 'text/html'; + } + } - class RedirectResult { - constructor(url) { - this.body = url; - this.contentType = 'redirectToUrl'; - } - } + class RedirectResult { + constructor(url) { + this.body = url; + this.contentType = 'redirectToUrl'; + } + } - class StatusCodeResult { - constructor(statusCode, error) { - this.statusCode = statusCode; - this.body = error; - this.contentType = 'statusCode'; - } - }"); + class StatusCodeResult { + constructor(statusCode, error) { + this.statusCode = statusCode; + this.body = error; + this.contentType = 'statusCode'; + } + }"); - try + try + { + _engine.Execute(code); + return await Task.Run(async () => { - _engine.Execute(code); - return await Task.Run(async () => - { - var result = _engine.Evaluate(method); + var result = _engine.Evaluate(method); - // The script can be synchronous or asynchronous, so this simple solution is used to convert the result - // Source: https://github.com/microsoft/ClearScript/issues/366 - try - { - return await result.ToTask(); - } - catch (ArgumentException) - { - return result; - } - }, cancellationToken).WaitAsync(executeTimeout, cancellationToken); - } - catch (TimeoutException) - { - throw new RaythaFunctionExecuteTimeoutException("The function execution time has exceeded the timeout"); - } - catch (ScriptEngineException exception) - { - throw new RaythaFunctionScriptException(exception.ErrorDetails); - } + // The script can be synchronous or asynchronous, so this simple solution is used to convert the result + // Source: https://github.com/microsoft/ClearScript/issues/366 + try + { + return await result.ToTask(); + } + catch (ArgumentException) + { + return result; + } + }, cancellationToken).WaitAsync(executeTimeout, cancellationToken); + } + catch (TimeoutException) + { + throw new RaythaFunctionExecuteTimeoutException("The function execution time has exceeded the timeout"); + } + catch (ScriptEngineException exception) + { + throw new RaythaFunctionScriptException(exception.ErrorDetails); } } public async Task EvaluateGet(string code,string query, TimeSpan executeTimeout, CancellationToken cancellationToken) { - return await Evaluate(code, $"get({query})", executeTimeout, cancellationToken); + var result = await Evaluate(code, $"get({query})", executeTimeout, cancellationToken); + return result; } public async Task EvaluatePost(string code, string payload, string query, TimeSpan executeTimeout, CancellationToken cancellationToken) From 72b83a3a046a769e8a8afdc57899a596ed236054 Mon Sep 17 00:00:00 2001 From: Zack Schwartz Date: Sun, 29 Sep 2024 17:02:56 -0500 Subject: [PATCH 2/3] Regex should not use back refs #210 --- src/Raytha.Application/Common/Utils/WebTemplateExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Raytha.Application/Common/Utils/WebTemplateExtensions.cs b/src/Raytha.Application/Common/Utils/WebTemplateExtensions.cs index 165aeb7f..49ffc583 100644 --- a/src/Raytha.Application/Common/Utils/WebTemplateExtensions.cs +++ b/src/Raytha.Application/Common/Utils/WebTemplateExtensions.cs @@ -9,7 +9,7 @@ namespace Raytha.Application.Common.Utils; public static class WebTemplateExtensions { - public const string RENDERBODY_REGEX = @"{%\s*\b(renderbody)\s*%}"; + public const string RENDERBODY_REGEX = @"{%\s*\b(?:renderbody)\s*%}"; public static bool HasRenderBodyTag(string s) { From 36ace1c9ec61601d9afd347504901cd079de15df Mon Sep 17 00:00:00 2001 From: Zack Schwartz Date: Sun, 29 Sep 2024 17:04:19 -0500 Subject: [PATCH 3/3] Update version number --- src/Raytha.Web/Services/CurrentVersion.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Raytha.Web/Services/CurrentVersion.cs b/src/Raytha.Web/Services/CurrentVersion.cs index 0ec42d83..7a2453bc 100644 --- a/src/Raytha.Web/Services/CurrentVersion.cs +++ b/src/Raytha.Web/Services/CurrentVersion.cs @@ -3,5 +3,5 @@ namespace Raytha.Web.Services; public class CurrentVersion : ICurrentVersion { - public string Version => "1.3.1"; + public string Version => "1.3.2"; }