Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm: support http(s) fetch of Wasm files #3005

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions bindings/wasm/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package wasm
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -63,9 +64,10 @@ func NewWasmOutput(logger logger.Logger) bindings.OutputBinding {
}

func (out *outputBinding) Init(ctx context.Context, metadata bindings.Metadata) (err error) {
if out.meta, err = wasm.GetInitMetadata(metadata.Base); err != nil {
if out.meta, err = wasm.GetInitMetadata(ctx, metadata.Base); err != nil {
return fmt.Errorf("wasm: failed to parse metadata: %w", err)
}
// ctx = context.WithValue(ctx, experimental.FunctionListenerFactoryKey{}, logging.NewHostLoggingListenerFactory(os.Stderr, logging.LogScopeAll))
ItalyPaleAle marked this conversation as resolved.
Show resolved Hide resolved

// Create the runtime, which when closed releases any resources associated with it.
out.runtime = wazero.NewRuntimeWithConfig(ctx, out.runtimeConfig)
Expand Down Expand Up @@ -115,7 +117,13 @@ func (out *outputBinding) Invoke(ctx context.Context, req *bindings.InvokeReques

// Get any remaining args from configuration
if args := req.Metadata["args"]; args != "" {
argsSlice = append(argsSlice, strings.Split(args, ",")...)
parser := csv.NewReader(strings.NewReader(args))
parser.Comma = ' '
records, err := parser.ReadAll()
if err != nil {
return nil, err
}
argsSlice = append(argsSlice, records[0]...)
}
moduleConfig = moduleConfig.WithArgs(argsSlice...)

Expand Down
6 changes: 4 additions & 2 deletions bindings/wasm/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
)

const (
ruby = "https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/ruby%2F3.2.0%2B20230215-1349da9/ruby-3.2.0-slim.wasm"
urlArgsFile = "file://testdata/args/main.wasm"
urlExampleFile = "file://testdata/example/main.wasm"
urlLoopFile = "file://testdata/loop/main.wasm"
Expand Down Expand Up @@ -116,10 +117,11 @@ func Test_Invoke(t *testing.T) {
},
{
name: "args",
url: urlArgsFile,
url: ruby,
request: &bindings.InvokeRequest{
Metadata: map[string]string{"args": "1,2"},
Metadata: map[string]string{"args": "-ne 'print \"Hello \";print '"},
ItalyPaleAle marked this conversation as resolved.
Show resolved Hide resolved
Operation: ExecuteOperation,
Data: []byte("salaboy"),
},
expectedData: "main\n1\n2",
},
Expand Down
10 changes: 7 additions & 3 deletions internal/wasm/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ func newHTTPCLient(transport http.RoundTripper) *httpClient {
// fetch returns a byte slice of the wasm module found at the given URL, or an error otherwise.
func (f *httpClient) get(ctx context.Context, u *url.URL) ([]byte, error) {
h := http.Header{}
// Clear default user agent.
h.Set("User-Agent", "")
req := &http.Request{Method: http.MethodGet, URL: u, Header: h}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req.Header = h
resp, err := f.c.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
evacchi marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("received %v status code from %q", resp.StatusCode, u)
}
Expand All @@ -58,6 +61,7 @@ func (f *httpClient) get(ctx context.Context, u *url.URL) ([]byte, error) {
if err != nil {
return nil, err
}
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
return bytes, nil
}
4 changes: 2 additions & 2 deletions internal/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type InitMetadata struct {
}

// GetInitMetadata returns InitMetadata from the input metadata.
func GetInitMetadata(md metadata.Base) (*InitMetadata, error) {
func GetInitMetadata(ctx context.Context, md metadata.Base) (*InitMetadata, error) {
// Note: the ctx will be used for other schemes such as HTTP and OCI.

var m InitMetadata
Expand Down Expand Up @@ -95,7 +95,7 @@ func GetInitMetadata(md metadata.Base) (*InitMetadata, error) {
return nil, err
}
c := newHTTPCLient(http.DefaultTransport)
ItalyPaleAle marked this conversation as resolved.
Show resolved Hide resolved
m.Guest, err = c.get(context.Background(), u)
m.Guest, err = c.get(ctx, u)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion internal/wasm/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (
var binArgs []byte

func TestGetInitMetadata(t *testing.T) {
testCtx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

type testCase struct {
name string
metadata metadata.Base
Expand Down Expand Up @@ -137,7 +140,7 @@ func TestGetInitMetadata(t *testing.T) {
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
md, err := GetInitMetadata(tc.metadata)
md, err := GetInitMetadata(testCtx, tc.metadata)
if tc.expectedErr == "" {
require.NoError(t, err)
require.Equal(t, tc.expected, md)
Expand Down
2 changes: 1 addition & 1 deletion middleware/http/wasm/httpwasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (m *middleware) GetHandler(ctx context.Context, metadata dapr.Metadata) (fu

// getHandler is extracted for unit testing.
func (m *middleware) getHandler(ctx context.Context, metadata dapr.Metadata) (*requestHandler, error) {
meta, err := wasm.GetInitMetadata(metadata.Base)
meta, err := wasm.GetInitMetadata(ctx, metadata.Base)
if err != nil {
return nil, fmt.Errorf("wasm: failed to parse metadata: %w", err)
}
Expand Down