-
Notifications
You must be signed in to change notification settings - Fork 4
/
worker.go
79 lines (67 loc) · 1.79 KB
/
worker.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
package resvg
import (
"bytes"
"compress/gzip"
"context"
"io"
"io/fs"
"os"
"sync/atomic"
"github.com/kanrichan/resvg-go/internal"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
// Worker resvg wasm worker
// `Worker` are not goroutine-safe!
type Worker struct {
ctx context.Context
r wazero.Runtime
mod api.Module
used *atomic.Bool
}
// NewDefaultWorker initialize a resvg wasm worker by default
// `Worker` are not goroutine-safe!
func NewDefaultWorker(ctx context.Context) (*Worker, error) {
return NewWorker(ctx, wazero.NewRuntimeConfig())
}
// NewWorker initialize a resvg wasm worker with wazero.RuntimeConfig
// `Worker` are not goroutine-safe!
func NewWorker(ctx context.Context, config wazero.RuntimeConfig) (*Worker, error) {
wasmgzr, err := gzip.NewReader(bytes.NewReader(internal.WasmGZ))
if err != nil {
return nil, err
}
defer wasmgzr.Close()
wasm, err := io.ReadAll(wasmgzr)
if err != nil {
return nil, err
}
r := wazero.NewRuntimeWithConfig(ctx, config)
wasi_snapshot_preview1.MustInstantiate(ctx, r)
moduleConfig := wazero.NewModuleConfig().
WithStdout(os.Stdout).WithStderr(os.Stderr).
WithFS(vfs{})
mod, err := r.InstantiateWithConfig(ctx, wasm, moduleConfig)
if err != nil {
return nil, err
}
return &Worker{ctx, r, mod, &atomic.Bool{}}, nil
}
// Close cloes the `Worker`
func (wk *Worker) Close() error {
return wk.r.Close(wk.ctx)
}
// vfs wasm mount directory
type vfs struct{}
// Open opens file in the wasm mount directory
func (vfs vfs) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
}
f, err := os.Open(name)
if err != nil {
return nil, err // nil fs.File
}
return f, nil
}