Skip to content

Commit

Permalink
Increased JavaScript Engine timeout to 25 seconds
Browse files Browse the repository at this point in the history
Extended JavaScript API for writing text files
  • Loading branch information
sandreas committed Aug 9, 2022
1 parent d3ba046 commit 0cd6cb1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tone/Program.cs
Expand Up @@ -103,7 +103,7 @@
{
var cts = sp.GetRequiredService<CancellationTokenSource>();
options.LimitMemory(4_000_000);
options.TimeoutInterval(TimeSpan.FromSeconds(5));
options.TimeoutInterval(TimeSpan.FromSeconds(25));
options.MaxStatements(1000);
options.CancellationToken(cts.Token);
});
Expand Down
47 changes: 47 additions & 0 deletions 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;
Expand Down Expand Up @@ -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
};
}
3 changes: 3 additions & 0 deletions tone/doc/release/release-notes-v0.0.8.md
Expand Up @@ -3,13 +3,16 @@
## 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

- `dump` now contains technical information about the audio frames and metadata formats

## Added

- JavaScriptApi now contains new methods `tone.WriteTextFile`, `tone.AppendTextFile`, and `tone.LimitByteLength`

## known issues

Expand Down

0 comments on commit 0cd6cb1

Please sign in to comment.