Skip to content

Commit

Permalink
internal/warpc: Improve the JS plugin API
Browse files Browse the repository at this point in the history
* Move the error handling into commons and make sure the error returned also returns message errors
* Make the protocol version an int so it can be more easily compared
  • Loading branch information
bep committed Sep 12, 2024
1 parent fe7e137 commit 28f621d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 42 deletions.
14 changes: 11 additions & 3 deletions internal/warpc/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,21 @@ export function readInput(handle) {
if (currentLine[i] === 10) {
const chunk = currentLine.splice(j, i + 1);
const arr = new Uint8Array(chunk);
let json;
let message;
try {
json = JSON.parse(new TextDecoder().decode(arr));
message = JSON.parse(new TextDecoder().decode(arr));
} catch (e) {
throw new Error(`Error parsing JSON '${new TextDecoder().decode(arr)}' from stdin: ${e.message}`);
}
handle(json);

try {
handle(message);
} catch (e) {
let header = message.header;
header.err = e.message;
writeOutput({ header: header });
}

j = i + 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/warpc/js/greet.bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions internal/warpc/js/renderkatex.bundle.js

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions internal/warpc/js/renderkatex.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ const render = function (input) {
const expression = data.expression;
const options = data.options;
const header = input.header;
try {
const output = katex.renderToString(expression, options);
writeOutput({ header: header, data: { output: output } });
} catch (e) {
header.err = e.message;
writeOutput({ header: header });
}
// Any error thrown here will be caught by the common.js readInput function.
const output = katex.renderToString(expression, options);
writeOutput({ header: header, data: { output: output } });
};

readInput(render);
16 changes: 12 additions & 4 deletions internal/warpc/warpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ import (
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)

const currentVersion = "v1"
const currentVersion = 1

//go:embed wasm/quickjs.wasm
var quickjsWasm []byte

// Header is in both the request and response.
type Header struct {
Version string `json:"version"`
ID uint32 `json:"id"`
// Major version of the protocol.
Version uint16 `json:"version"`

// Unique ID for the request.
// Note that this only needs to be unique within the current request set time window.
ID uint32 `json:"id"`

// Set in the response if there was an error.
Err string `json:"err"`
Expand Down Expand Up @@ -150,7 +154,11 @@ func (p *dispatcherPool[Q, R]) Execute(ctx context.Context, q Message[Q]) (Messa
return d.zero, call.err
}

return call.response, p.Err()
resp, err := call.response, p.Err()
if err == nil && resp.Header.Err != "" {
err = errors.New(resp.Header.Err)
}
return resp, err
}

func (d *dispatcher[Q, R]) newCall(q Message[Q]) (*call[Q, R], error) {
Expand Down
50 changes: 33 additions & 17 deletions internal/warpc/warpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,44 @@ func TestKatex(t *testing.T) {

defer d.Close()

ctx := context.Background()
runExpression := func(c *qt.C, id uint32, expression string) (Message[KatexOutput], error) {
c.Helper()

input := KatexInput{
Expression: "c = \\pm\\sqrt{a^2 + b^2}",
Options: KatexOptions{
Output: "html",
DisplayMode: true,
},
}
ctx := context.Background()

input := KatexInput{
Expression: expression,
Options: KatexOptions{
Output: "html",
DisplayMode: true,
ThrowOnError: true,
},
}

message := Message[KatexInput]{
Header: Header{
Version: currentVersion,
ID: uint32(id),
},
Data: input,
}

message := Message[KatexInput]{
Header: Header{
Version: currentVersion,
ID: uint32(32),
},
Data: input,
return d.Execute(ctx, message)
}

result, err := d.Execute(ctx, message)
c.Assert(err, qt.IsNil)
c.Run("Simple", func(c *qt.C) {
id := uint32(32)
result, err := runExpression(c, id, "c = \\pm\\sqrt{a^2 + b^2}")
c.Assert(err, qt.IsNil)
c.Assert(result.GetID(), qt.Equals, id)
})

c.Assert(result.GetID(), qt.Equals, message.GetID())
c.Run("Invalid expression", func(c *qt.C) {
id := uint32(32)
result, err := runExpression(c, id, "c & \\foo\\")
c.Assert(err, qt.IsNotNil)
c.Assert(result.GetID(), qt.Equals, id)
})
}

func TestGreet(t *testing.T) {
Expand Down
Binary file modified internal/warpc/wasm/greet.wasm
Binary file not shown.
Binary file modified internal/warpc/wasm/renderkatex.wasm
Binary file not shown.
5 changes: 1 addition & 4 deletions tpl/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (types.Result[temp
_, r, err := fileCache.GetOrCreate(key, func() (io.ReadCloser, error) {
message := warpc.Message[warpc.KatexInput]{
Header: warpc.Header{
Version: "v1",
Version: 1,
ID: ns.id.Add(1),
},
Data: katexInput,
Expand All @@ -249,9 +249,6 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (types.Result[temp
if err != nil {
return nil, err
}
if result.Header.Err != "" {
return nil, errors.New(result.Header.Err)
}
return hugio.NewReadSeekerNoOpCloserFromString(result.Data.Output), nil
})
if err != nil {
Expand Down

0 comments on commit 28f621d

Please sign in to comment.