From 0f3e1e7aeb9a4035ad9d16deece0d770ad27b8e0 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 4 Nov 2024 15:12:15 -0800 Subject: [PATCH 1/2] feat: support enable-http-response-headers flag, minor tweaks --- call.go | 59 ++++++++++++++++++++++++++++++--------------------------- go.mod | 2 ++ 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/call.go b/call.go index 9885f18..dab2a21 100644 --- a/call.go +++ b/call.go @@ -9,29 +9,30 @@ import ( "path/filepath" "strings" - "github.com/extism/go-sdk" + extism "github.com/extism/go-sdk" "github.com/spf13/cobra" "github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero/sys" ) type callArgs struct { - args []string - input string - loop int - wasi bool - logLevel string - allowedPaths []string - allowedHosts []string - timeout uint64 - memoryMaxPages int - memoryHttpMaxBytes int - memoryVarMaxBytes int - config []string - setConfig string - manifest bool - stdin bool - link []string + args []string + input string + loop int + wasi bool + logLevel string + allowedPaths []string + allowedHosts []string + enableHttpRespHeaders bool + timeout uint64 + memoryMaxPages int + memoryHttpMaxBytes int + memoryVarMaxBytes int + config []string + setConfig string + manifest bool + stdin bool + link []string } func readStdin() []byte { @@ -76,7 +77,7 @@ func (a *callArgs) getConfig() (map[string]string, error) { err := json.Unmarshal([]byte(a.setConfig), &config) if err != nil { return config, - errors.Join(errors.New("Invalid value for --set-config flag"), err) + errors.Join(errors.New("invalid value for --set-config flag"), err) } } for _, cfg := range a.config { @@ -125,9 +126,9 @@ var globalPlugin *extism.Plugin func runCall(cmd *cobra.Command, call *callArgs) error { if len(call.args) < 1 { - return errors.New("An input file is required") + return errors.New("an input file is required") } else if len(call.args) < 2 { - return errors.New("A function name is required") + return errors.New("a function name is required") } ctx := context.Background() @@ -218,24 +219,25 @@ func runCall(cmd *cobra.Command, call *callArgs) error { var logLevel extism.LogLevel = extism.LogLevelError switch call.logLevel { - case "info": - logLevel = extism.LogLevelInfo + case "trace": + logLevel = extism.LogLevelTrace case "debug": logLevel = extism.LogLevelDebug + case "info": + logLevel = extism.LogLevelInfo case "warn": logLevel = extism.LogLevelWarn case "error": logLevel = extism.LogLevelError - case "trace": - logLevel = extism.LogLevelTrace } extism.SetLogLevel(logLevel) pluginConfig := extism.PluginConfig{ - ModuleConfig: wazero.NewModuleConfig().WithSysWalltime(), - RuntimeConfig: wazero.NewRuntimeConfig().WithCloseOnContextDone(call.timeout > 0), - EnableWasi: call.wasi, + ModuleConfig: wazero.NewModuleConfig().WithSysWalltime(), + RuntimeConfig: wazero.NewRuntimeConfig().WithCloseOnContextDone(call.timeout > 0), + EnableWasi: call.wasi, + EnableHttpResponseHeaders: call.enableHttpRespHeaders, } if call.timeout > 0 { @@ -269,7 +271,7 @@ func runCall(cmd *cobra.Command, call *callArgs) error { if exit == sys.ExitCodeDeadlineExceeded { return errors.New("timeout") } else if exit != 0 { - return errors.Join(err, errors.New(fmt.Sprintf("Returned non-zero exit code: %d", exit))) + return errors.Join(err, fmt.Errorf("returned non-zero exit code: %d", exit)) } return err @@ -303,6 +305,7 @@ func CallCmd() *cobra.Command { flags.BoolVar(&call.wasi, "wasi", false, "Enable WASI") flags.StringArrayVar(&call.allowedPaths, "allow-path", []string{}, "Allow a path to be accessed from inside the Wasm sandbox, a path can be either a plain path or a map from HOST_PATH:GUEST_PATH") flags.StringArrayVar(&call.allowedHosts, "allow-host", []string{}, "Allow access to an HTTP host, if no hosts are listed then all requests will fail. Globs may be used for wildcards") + flags.BoolVar(&call.enableHttpRespHeaders, "enable-http-response-headers", false, "Enable HTTP response headers to be read by plugins for any request to an allowed host.") flags.Uint64Var(&call.timeout, "timeout", 0, "Timeout in milliseconds") flags.IntVar(&call.memoryMaxPages, "memory-max", 0, "Maximum number of pages to allocate") flags.IntVar(&call.memoryHttpMaxBytes, "http-response-max", -1, "Maximum HTTP response size in bytes when using `extism_http_request`") diff --git a/go.mod b/go.mod index 0cb322a..1c1e240 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,8 @@ require ( golang.org/x/sys v0.24.0 ) +// replace github.com/extism/go-sdk => ../go-sdk + require ( github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c // indirect github.com/atotto/clipboard v0.1.4 // indirect From d02ac2d76d866fcd0abf6bb9462c34dbba4073f0 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 4 Nov 2024 15:29:37 -0800 Subject: [PATCH 2/2] chore: bump go-sdk dep --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 1c1e240..059afb7 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/charmbracelet/bubbletea v0.25.0 github.com/charmbracelet/lipgloss v0.10.0 github.com/ebitengine/purego v0.5.1 - github.com/extism/go-sdk v1.6.0 + github.com/extism/go-sdk v1.6.1 github.com/gobwas/glob v0.2.3 github.com/google/go-github/v55 v55.0.0 github.com/spf13/cobra v1.8.0 diff --git a/go.sum b/go.sum index 616d6e3..dc0af71 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/ebitengine/purego v0.5.1 h1:hNunhThpOf1vzKl49v6YxIsXLhl92vbBEv1/2Ez3Z github.com/ebitengine/purego v0.5.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/extism/go-sdk v1.6.0 h1:crFRMhjcPAn6R9M4eIvkjHQs7CLBs3yzPqwnj+uwzdg= github.com/extism/go-sdk v1.6.0/go.mod h1:yRolc4PvIUQ9J/BBB3QZ5EY1MtXAN2jqBGDGR3Sk54M= +github.com/extism/go-sdk v1.6.1 h1:gkbkG5KzYKrv8mLggw5ojg/JulXfEbLIRVhbw9Ot7S0= +github.com/extism/go-sdk v1.6.1/go.mod h1:yRolc4PvIUQ9J/BBB3QZ5EY1MtXAN2jqBGDGR3Sk54M= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=