-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.go
98 lines (80 loc) · 2.93 KB
/
go.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
package wazemmes
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/http-wasm/http-wasm-host-go/handler"
wasm "github.com/http-wasm/http-wasm-host-go/handler/nethttp"
"github.com/juliens/wasm-goexport/host"
"github.com/stealthrocket/wasi-go/imports"
wazergo_wasip1 "github.com/stealthrocket/wasi-go/imports/wasi_snapshot_preview1"
"github.com/stealthrocket/wazergo"
"github.com/tetratelabs/wazero"
wazero_wasip1 "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"go.uber.org/zap"
)
func hostInstanciation(ctx context.Context, runtime wazero.Runtime, mod wazero.CompiledModule) (func(ctx context.Context) context.Context, error) {
if extension := imports.DetectSocketsExtension(mod); extension != nil {
envs := []string{}
builder := imports.NewBuilder().WithSocketsExtension("auto", mod)
if len(envs) > 0 {
builder.WithEnv(envs...)
}
ctx, sys, err := builder.Instantiate(ctx, runtime)
if err != nil {
return nil, err
}
inst, err := wazergo.Instantiate(ctx, runtime, wazergo_wasip1.NewHostModule(*extension), wazergo_wasip1.WithWASI(sys))
if err != nil {
return nil, fmt.Errorf("wazergo instantiation: %w", err)
}
return func(ctx context.Context) context.Context {
return wazergo.WithModuleInstance(ctx, inst)
}, nil
}
_, err := wazero_wasip1.Instantiate(ctx, runtime)
if err != nil {
return nil, fmt.Errorf("wazero instantiation: %w", err)
}
return func(ctx context.Context) context.Context {
return ctx
}, nil
}
func NewWasmHandlerGo(modulepath string, moduleConfig any, poolConfiguration map[string]interface{}, logger *zap.Logger) (*WasmHandler, error) {
cache := wazero.NewCompilationCache()
ctx := context.Background()
wa0Rt := host.NewRuntime(wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().WithCompilationCache(cache).WithCloseOnContextDone(true)))
code, err := os.ReadFile(modulepath)
if err != nil {
logger.Sugar().Infof("impossible to read the custom module: %w", err)
return nil, err
}
customModule, err := wa0Rt.CompileModule(ctx, code)
if err != nil {
logger.Sugar().Infof("impossible to compile the custom module: %w", err)
return nil, err
}
applyCtx, err := hostInstanciation(ctx, wa0Rt, customModule)
if err != nil {
logger.Sugar().Infof("instantiating host module: %w", err)
return nil, err
}
config := wazero.NewModuleConfig().WithSysWalltime().WithStartFunctions("_start", "_initialize").WithStdout(os.Stdout).WithStderr(os.Stderr)
opts := []handler.Option{
handler.ModuleConfig(config),
handler.Logger(NewLogger(logger.Sugar())),
}
data, err := json.Marshal(moduleConfig)
if err != nil {
logger.Sugar().Infof("marshaling config: %w", err)
return nil, err
}
opts = append(opts, handler.GuestConfig(data))
mw, err := wasm.NewMiddleware(applyCtx(ctx), code, opts...)
if err != nil {
logger.Sugar().Infof("creating middleware: %w", err)
return nil, err
}
return NewWasmHandlerInstance(mw.NewHandler, poolConfiguration, logger)
}