Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Grpc.AspNetCore" Version="2.67.0" />
<PackageVersion Include="Microsoft.ClearScript.V8" Version="7.4.5" />
<PackageVersion Include="Microsoft.ClearScript.V8.ICUData" Version="7.4.5" />
<PackageVersion Include="Microsoft.ClearScript.V8.Native.linux-x64" Version="7.4.5" />
<PackageVersion Include="Microsoft.ClearScript.V8.Native.win-x64" Version="7.4.5" />
<PackageVersion Include="Microsoft.ClearScript.V8" Version="7.5.0" />
<PackageVersion Include="Microsoft.ClearScript.V8.ICUData" Version="7.5.0" />
<PackageVersion Include="Microsoft.ClearScript.V8.Native.linux-x64" Version="7.5.0" />
<PackageVersion Include="Microsoft.ClearScript.V8.Native.win-x64" Version="7.5.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="9.0.0" />
<PackageVersion Include="Microsoft.Diagnostics.NETCore.Client" Version="0.2.553101" />
<PackageVersion Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.1.17" />
Expand All @@ -32,4 +32,4 @@
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
</Project>
</Project>
17 changes: 9 additions & 8 deletions Trell.Engine/ClearScriptWrappers/EngineWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ internal IScriptObject CreateJsStringArray(IList<string>? list) {
return arr;
}

public IArrayBuffer CreateJsBuffer(byte[] contents) {
public IArrayBuffer CreateJsBuffer(ReadOnlySpan<byte> contents) {
var buf = (IArrayBuffer)((ScriptObject)this.engine.Evaluate("ArrayBuffer")).Invoke(true, [contents.Length]);
if (contents.Length > 0) {
buf.WriteBytes(contents, 0, (ulong)contents.Length, 0);
}
return buf;
}

public ScriptObject CreateJsFile(string filename, string type, byte[] contents) {
public ScriptObject CreateJsFile(string filename, string type, ReadOnlySpan<byte> contents) {
return (ScriptObject)((ScriptObject)this.engine.Evaluate("File")).Invoke(
true,
[
Expand All @@ -220,19 +220,20 @@ public ScriptObject CreateJsFile(string filename, string type, byte[] contents)
EnableSourceLoading(work.SourceDirectory);
var limits = this.limits.RestrictBy(work.Limits);

var docInfo = new DocumentInfo {
Category = ModuleCategory.Standard
};
object module;

using (var t = Cancel(this.engine, linked, this.currentContext).After(limits.MaxStartupDuration)) {
var loadWorkerJs = $"import * as hooks from '{work.WorkerJs}'; hooks;";
module = this.engine.Evaluate(docInfo, loadWorkerJs);
var workerFileName = work.WorkerJs.ToString();
var loadWorkerJs = $"import('{workerFileName}')";
var loadEvaluation = this.engine.Evaluate(workerFileName, false, loadWorkerJs);

module = loadEvaluation is Task<object> loadTask ? await loadTask : loadEvaluation;

Log.Information("Evaluated `{Js}` to {M}", loadWorkerJs, module);

if (module is Task<object> moduleResultTask) {
throw new TrellUserException(
new TrellError(TrellErrorCode.TIMEOUT, $"worker.js took longer than {limits.MaxStartupDuration} to load"));
new TrellError(TrellErrorCode.TIMEOUT, $"{workerFileName} took longer than {limits.MaxStartupDuration} to load"));
}
}

Expand Down
6 changes: 2 additions & 4 deletions Trell/Rpc/ToEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ public static EngineWrapper.Work.RawArg ToFunctionArg(this Function fn, EngineWr
["url"] = fn.OnRequest.Url,
["method"] = fn.OnRequest.Method,
["headers"] = engine.CreateScriptObject(fn.OnRequest.Headers.ToDictionary(x => x.Key, y => (object)y.Value)),
// TODO: Replace this with Span<> equivalent once ClearScript is updated to support it:
// https://github.com/xledger/trell/issues/28
["body"] = engine.CreateJsBuffer(fn.OnRequest.Body.ToByteArray()),
["body"] = engine.CreateJsBuffer(fn.OnRequest.Body.Span),
})),
Function.ValueOneofCase.OnUpload => new EngineWrapper.Work.RawArg("file",
engine.CreateJsFile(fn.OnUpload.Filename, fn.OnUpload.Type, fn.OnUpload.Content.ToByteArray())
engine.CreateJsFile(fn.OnUpload.Filename, fn.OnUpload.Type, fn.OnUpload.Content.Span)
),
Function.ValueOneofCase.Dynamic =>
new EngineWrapper.Work.RawArg("argv", engine.CreateJsStringArray(fn.Dynamic.Arguments)),
Expand Down