Skip to content

Commit 28f621d

Browse files
committed
internal/warpc: Improve the JS plugin API
* 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
1 parent fe7e137 commit 28f621d

File tree

9 files changed

+67
-42
lines changed

9 files changed

+67
-42
lines changed

internal/warpc/js/common.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@ export function readInput(handle) {
4141
if (currentLine[i] === 10) {
4242
const chunk = currentLine.splice(j, i + 1);
4343
const arr = new Uint8Array(chunk);
44-
let json;
44+
let message;
4545
try {
46-
json = JSON.parse(new TextDecoder().decode(arr));
46+
message = JSON.parse(new TextDecoder().decode(arr));
4747
} catch (e) {
4848
throw new Error(`Error parsing JSON '${new TextDecoder().decode(arr)}' from stdin: ${e.message}`);
4949
}
50-
handle(json);
50+
51+
try {
52+
handle(message);
53+
} catch (e) {
54+
let header = message.header;
55+
header.err = e.message;
56+
writeOutput({ header: header });
57+
}
58+
5159
j = i + 1;
5260
}
5361
}

internal/warpc/js/greet.bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/warpc/js/renderkatex.bundle.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/warpc/js/renderkatex.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ const render = function (input) {
66
const expression = data.expression;
77
const options = data.options;
88
const header = input.header;
9-
try {
10-
const output = katex.renderToString(expression, options);
11-
writeOutput({ header: header, data: { output: output } });
12-
} catch (e) {
13-
header.err = e.message;
14-
writeOutput({ header: header });
15-
}
9+
// Any error thrown here will be caught by the common.js readInput function.
10+
const output = katex.renderToString(expression, options);
11+
writeOutput({ header: header, data: { output: output } });
1612
};
1713

1814
readInput(render);

internal/warpc/warpc.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,19 @@ import (
3535
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
3636
)
3737

38-
const currentVersion = "v1"
38+
const currentVersion = 1
3939

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

4343
// Header is in both the request and response.
4444
type Header struct {
45-
Version string `json:"version"`
46-
ID uint32 `json:"id"`
45+
// Major version of the protocol.
46+
Version uint16 `json:"version"`
47+
48+
// Unique ID for the request.
49+
// Note that this only needs to be unique within the current request set time window.
50+
ID uint32 `json:"id"`
4751

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

153-
return call.response, p.Err()
157+
resp, err := call.response, p.Err()
158+
if err == nil && resp.Header.Err != "" {
159+
err = errors.New(resp.Header.Err)
160+
}
161+
return resp, err
154162
}
155163

156164
func (d *dispatcher[Q, R]) newCall(q Message[Q]) (*call[Q, R], error) {

internal/warpc/warpc_test.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,44 @@ func TestKatex(t *testing.T) {
4545

4646
defer d.Close()
4747

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

50-
input := KatexInput{
51-
Expression: "c = \\pm\\sqrt{a^2 + b^2}",
52-
Options: KatexOptions{
53-
Output: "html",
54-
DisplayMode: true,
55-
},
56-
}
51+
ctx := context.Background()
52+
53+
input := KatexInput{
54+
Expression: expression,
55+
Options: KatexOptions{
56+
Output: "html",
57+
DisplayMode: true,
58+
ThrowOnError: true,
59+
},
60+
}
61+
62+
message := Message[KatexInput]{
63+
Header: Header{
64+
Version: currentVersion,
65+
ID: uint32(id),
66+
},
67+
Data: input,
68+
}
5769

58-
message := Message[KatexInput]{
59-
Header: Header{
60-
Version: currentVersion,
61-
ID: uint32(32),
62-
},
63-
Data: input,
70+
return d.Execute(ctx, message)
6471
}
6572

66-
result, err := d.Execute(ctx, message)
67-
c.Assert(err, qt.IsNil)
73+
c.Run("Simple", func(c *qt.C) {
74+
id := uint32(32)
75+
result, err := runExpression(c, id, "c = \\pm\\sqrt{a^2 + b^2}")
76+
c.Assert(err, qt.IsNil)
77+
c.Assert(result.GetID(), qt.Equals, id)
78+
})
6879

69-
c.Assert(result.GetID(), qt.Equals, message.GetID())
80+
c.Run("Invalid expression", func(c *qt.C) {
81+
id := uint32(32)
82+
result, err := runExpression(c, id, "c & \\foo\\")
83+
c.Assert(err, qt.IsNotNil)
84+
c.Assert(result.GetID(), qt.Equals, id)
85+
})
7086
}
7187

7288
func TestGreet(t *testing.T) {

internal/warpc/wasm/greet.wasm

135 Bytes
Binary file not shown.

internal/warpc/wasm/renderkatex.wasm

96 Bytes
Binary file not shown.

tpl/transform/transform.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (types.Result[temp
235235
_, r, err := fileCache.GetOrCreate(key, func() (io.ReadCloser, error) {
236236
message := warpc.Message[warpc.KatexInput]{
237237
Header: warpc.Header{
238-
Version: "v1",
238+
Version: 1,
239239
ID: ns.id.Add(1),
240240
},
241241
Data: katexInput,
@@ -249,9 +249,6 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (types.Result[temp
249249
if err != nil {
250250
return nil, err
251251
}
252-
if result.Header.Err != "" {
253-
return nil, errors.New(result.Header.Err)
254-
}
255252
return hugio.NewReadSeekerNoOpCloserFromString(result.Data.Output), nil
256253
})
257254
if err != nil {

0 commit comments

Comments
 (0)