-
Notifications
You must be signed in to change notification settings - Fork 10
/
encode.go
66 lines (60 loc) · 1.45 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"bytes"
"syscall/js"
"go.sia.tech/core/rhp/v4"
)
func encodeRPCRequest(data js.Value, req rhp.Request) result {
if data.Type() != js.TypeUndefined {
if err := unmarshalStruct(data, &req); err != nil {
return resultErr(err)
}
}
buf := bytes.NewBuffer(nil)
if err := rhp.WriteRequest(buf, req); err != nil {
return resultErr(err)
}
return resultRPC(marshalUint8Array(buf.Bytes()))
}
func decodeRPCRequest(rpcJsData js.Value, res rhp.Request) result {
rpcData, err := unmarshalUint8Array(rpcJsData)
if err != nil {
return resultErr(err)
}
err = rhp.ReadRequest(bytes.NewReader(rpcData), res)
if err != nil {
return resultErr(err)
}
d, err := marshalStruct(res)
if err != nil {
return resultErr(err)
}
return resultData(d)
}
func encodeRPCResponse(data js.Value, req rhp.Object) result {
if data.Type() != js.TypeUndefined {
if err := unmarshalStruct(data, &req); err != nil {
return resultErr(err)
}
}
buf := bytes.NewBuffer(nil)
if err := rhp.WriteResponse(buf, req); err != nil {
return resultErr(err)
}
return resultRPC(marshalUint8Array(buf.Bytes()))
}
func decodeRPCResponse(rpcJsData js.Value, res rhp.Object) result {
rpcData, err := unmarshalUint8Array(rpcJsData)
if err != nil {
return resultErr(err)
}
err = rhp.ReadResponse(bytes.NewReader(rpcData), res)
if err != nil {
return resultErr(err)
}
d, err := marshalStruct(res)
if err != nil {
return resultErr(err)
}
return resultData(d)
}