Skip to content

Commit

Permalink
feat: support enable-http-response-headers flag, minor tweaks (#106)
Browse files Browse the repository at this point in the history
* feat: support enable-http-response-headers flag, minor tweaks

* chore: bump go-sdk dep
  • Loading branch information
nilslice authored Nov 4, 2024
1 parent a3b9192 commit 490e40c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
59 changes: 31 additions & 28 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`")
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ 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
github.com/tetratelabs/wazero v1.8.1
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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 490e40c

Please sign in to comment.