-
Notifications
You must be signed in to change notification settings - Fork 132
/
main.go
148 lines (137 loc) · 3.37 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bytes"
"context"
"errors"
"io"
"slices"
"strings"
"syscall/js"
"github.com/brimdata/zed"
"github.com/brimdata/zed/compiler"
"github.com/brimdata/zed/compiler/parser"
"github.com/brimdata/zed/pkg/storage"
"github.com/brimdata/zed/runtime"
"github.com/brimdata/zed/zbuf"
"github.com/brimdata/zed/zio"
"github.com/brimdata/zed/zio/anyio"
"github.com/teamortix/golang-wasm/wasm"
)
func main() {
wasm.Expose("zq", zq)
wasm.Expose("parse", parse)
wasm.Ready()
<-make(chan struct{})
}
type opts struct {
Program string `wasm:"program"`
Input js.Value `wasm:"input"`
InputFormat string `wasm:"inputFormat"`
OutputFormat string `wasm:"outputFormat"`
}
// chunk represents a chunk in a ReadableStream
type chunk struct {
Done bool `wasm:"done"`
Value js.Value `wasm:"value"`
}
var errInvalidInput = errors.New("only string or ReadableStream accept as input")
func zq(opts opts) wasm.Promise {
return wasm.NewPromise(func() (interface{}, error) {
flowgraph, err := compiler.Parse(opts.Program)
if err != nil {
return "", err
}
var r io.Reader
switch typ := opts.Input.Type(); typ {
case js.TypeString:
r = strings.NewReader(opts.Input.String())
case js.TypeObject:
if !opts.Input.InstanceOf(js.Global().Get("ReadableStream")) {
return nil, errInvalidInput
}
r = readableStream(opts.Input)
default:
return "", errInvalidInput
}
zctx := zed.NewContext()
zr, err := anyio.NewReaderWithOpts(zctx, r, nil, anyio.ReaderOpts{
Format: opts.InputFormat,
})
if err != nil {
return "", err
}
defer zr.Close()
var buf bytes.Buffer
zwc, err := anyio.NewWriter(zio.NopCloser(&buf), anyio.WriterOpts{Format: opts.OutputFormat})
if err != nil {
return "", err
}
defer zwc.Close()
local := storage.NewLocalEngine()
comp := compiler.NewFileSystemCompiler(local)
query, err := runtime.CompileQuery(context.Background(), zctx, comp, flowgraph, []zio.Reader{zr})
if err != nil {
return "", err
}
defer query.Pull(true)
if err := zbuf.CopyPuller(zwc, query); err != nil {
return "", err
}
if err := zwc.Close(); err != nil {
return "", err
}
return buf.String(), nil
})
}
func parse(program string) (interface{}, error) {
ast, err := parser.ParseZed(nil, program)
result := ParseResult{AST: ast}
if err != nil {
var ok bool
result.Error, ok = err.(*parser.Error)
if !ok {
return nil, err
}
}
return result, nil
}
type ParseResult struct {
AST interface{} `wasm:"ast"`
Error *parser.Error `wasm:"error"`
}
func readableStream(readable js.Value) io.Reader {
pr, pw := io.Pipe()
go func() {
reader := readable.Call("getReader")
var buf []byte
for {
var ch chunk
if err := await(reader.Call("read"), &ch); ch.Done || err != nil {
pw.CloseWithError(err)
return
}
n := ch.Value.Length()
buf = slices.Grow(buf, n)
b := buf[:n]
js.CopyBytesToGo(b, ch.Value)
pw.Write(b)
}
}()
return pr
}
func await(prom js.Value, v interface{}) error {
err := make(chan error)
prom.Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) > 0 && v != nil {
err <- wasm.FromJSValue(args[0], v)
return nil
}
err <- nil
return nil
}))
prom.Call("catch", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
err <- errors.New(args[0].Call("toString").String())
return nil
}))
return <-err
}