diff --git a/tone/Program.cs b/tone/Program.cs index ee67131..a7cebdc 100644 --- a/tone/Program.cs +++ b/tone/Program.cs @@ -103,7 +103,7 @@ { var cts = sp.GetRequiredService(); options.LimitMemory(4_000_000); - options.TimeoutInterval(TimeSpan.FromSeconds(5)); + options.TimeoutInterval(TimeSpan.FromSeconds(25)); options.MaxStatements(1000); options.CancellationToken(cts.Token); }); diff --git a/tone/Services/JavaScriptApi.cs b/tone/Services/JavaScriptApi.cs index 34f5ee2..2f91ee5 100644 --- a/tone/Services/JavaScriptApi.cs +++ b/tone/Services/JavaScriptApi.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO.Abstractions; using System.Net.Http; using System.Net.Http.Headers; +using System.Text; using System.Threading.Tasks; using ATL; using Jint; @@ -171,4 +173,49 @@ public PictureInfo CreatePicture(string path, PictureInfo.PIC_TYPE type=PictureI pic.ComputePicHash(); return pic; } + + public void WriteTextFile(string path, string content) + { + _fs.File.WriteAllText(path, content); + } + + public void AppendTextFile(string path, string content){ + if(!_fs.File.Exists(path)){ + WriteTextFile(path, content); + return; + } + _fs.File.AppendAllText(path, content); + } + + // this https://stackoverflow.com/questions/8707755/how-to-know-the-size-of-the-string-in-bytes + // maybe optimize: https://stackoverflow.com/questions/8707755/how-to-know-the-size-of-the-string-in-bytes + public static string LimitByteLength(string message, int maxLength, string encodingAsString="utf8") + { + var encoding = EncodingStringToEncoding(encodingAsString); + + if (string.IsNullOrEmpty(message) || encoding.GetByteCount(message) <= maxLength) + { + return message; + } + + var enumerator = StringInfo.GetTextElementEnumerator(message); + var result = new StringBuilder(); + var lengthBytes = 0; + while (enumerator.MoveNext()) + { + lengthBytes += encoding.GetByteCount(enumerator.GetTextElement()); + if (lengthBytes <= maxLength) + { + result.Append(enumerator.GetTextElement()); + } + } + + return result.ToString(); + } + + private static Encoding EncodingStringToEncoding(string encodingAsString) => encodingAsString switch + { + "utf8" => Encoding.UTF8, + _ => Encoding.Default + }; } \ No newline at end of file diff --git a/tone/doc/release/release-notes-v0.0.8.md b/tone/doc/release/release-notes-v0.0.8.md index 7b17248..228b200 100644 --- a/tone/doc/release/release-notes-v0.0.8.md +++ b/tone/doc/release/release-notes-v0.0.8.md @@ -3,6 +3,8 @@ ## Fixed - `dump` command now skips empty properties when using `--format=json` +- only one file was processed under specific circumstances +- script taggers were always applied ignoring the `--taggers` option ## Changed @@ -10,6 +12,7 @@ ## Added +- JavaScriptApi now contains new methods `tone.WriteTextFile`, `tone.AppendTextFile`, and `tone.LimitByteLength` ## known issues