mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add script.wasm via extism plugins
This commit is contained in:
78
internal/processor/script-wasm.go
Normal file
78
internal/processor/script-wasm.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
extism "github.com/extism/go-sdk"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ScriptWASM struct {
|
||||
config config.ProcessorConfig
|
||||
Program *extism.CompiledPlugin
|
||||
}
|
||||
|
||||
func (se *ScriptWASM) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm can only operator on byte array")
|
||||
}
|
||||
|
||||
program, err := se.Program.Instance(ctx, extism.PluginInstanceConfig{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, output, err := program.Call("process", payloadBytes)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (se *ScriptWASM) Type() string {
|
||||
return se.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "script.wasm",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
path, ok := params["path"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm requires a path parameter")
|
||||
}
|
||||
|
||||
pathString, ok := path.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm path must be a string")
|
||||
}
|
||||
|
||||
manifest := extism.Manifest{
|
||||
Wasm: []extism.Wasm{
|
||||
extism.WasmFile{
|
||||
Path: pathString,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
program, err := extism.NewCompiledPlugin(context.Background(), manifest, extism.PluginConfig{}, []extism.HostFunction{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ScriptWASM{config: config, Program: program}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user