Skip to content

Commit

Permalink
refactor: Moving things around
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Fontes <[email protected]>
  • Loading branch information
lxfontes committed Sep 13, 2024
1 parent 4687df4 commit c3684a1
Show file tree
Hide file tree
Showing 20 changed files with 324 additions and 101 deletions.
3 changes: 3 additions & 0 deletions gen/wasmcloud/secrets/reveal/empty.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file exists for testing this package without WebAssembly,
// allowing empty function bodies with a //go:wasmimport directive.
// See https://pkg.go.dev/cmd/compile for more information.
27 changes: 27 additions & 0 deletions gen/wasmcloud/secrets/reveal/reveal.wit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions gen/wasmcloud/secrets/store/abi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions gen/wasmcloud/secrets/store/empty.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file exists for testing this package without WebAssembly,
// allowing empty function bodies with a //go:wasmimport directive.
// See https://pkg.go.dev/cmd/compile for more information.
144 changes: 144 additions & 0 deletions gen/wasmcloud/secrets/store/store.wit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions log/wasilog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"go.wasmcloud.dev/component/gen/wasi/logging/logging"
)

var DefaultLogger = slog.New(DefaultOptions().NewHandler())

func ContextLogger(wasiContext string) *slog.Logger {
return DefaultLogger.With(ContextAttr(wasiContext))
}

type contextKey string

func (k contextKey) String() string {
Expand Down
49 changes: 6 additions & 43 deletions log/wasilog/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,49 +121,6 @@ func TestContextLift(t *testing.T) {
})
}

func TestGroups(t *testing.T) {
tt := map[string]struct {
cb func(logger *slog.Logger)
expected string
}{
"inline": {
cb: func(logger *slog.Logger) {
logger.Info("test", slog.Group("top", slog.String("bottom", "simple")))
},
expected: `top.bottom="simple" test`,
},
"nested": {
cb: func(logger *slog.Logger) {
g := logger.With("bottom", "nested").WithGroup("top")
g.Info("test")
},
expected: `top.bottom="nested" test`,
},
}

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
options := DefaultOptions()
options.LoggerFunc = func(_ logging.Level, _ string, msg string) {
if want, got := tc.expected, msg; got != want {
t.Errorf("expected: %v, got: %v", want, got)
}
}

logger := slog.New(options.NewHandler())
tc.cb(logger)
})
}

logger := slog.Default()
logger.Info("test", slog.Group("top", slog.String("bottom", "simple")))
g := logger.With("bottom", "nested").WithGroup("top")
g.Info("test")
logger = logger.With("id", "123")
parserLogger := logger.WithGroup("parser")
parserLogger.Info("parsing", "file", "test.txt")
}

type Token string

// LogValue implements slog.LogValuer.
Expand All @@ -183,3 +140,9 @@ func TestLogValueMask(t *testing.T) {
logger := slog.New(options.NewHandler())
logger.Info("test", "token", Token("launch-the-nukes-code"))
}

// stub wasi:logging
//
//go:linkname wasmimport_Log go.wasmcloud.dev/component/gen/wasi/logging/logging.wasmimport_Log
func wasmimport_Log(level0 uint32, context0 *uint8, context1 uint32, message0 *uint8, message1 uint32) {
}
8 changes: 4 additions & 4 deletions net/wasihttp/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"go.wasmcloud.dev/component/gen/wasi/io/streams"
)

var _ http.ResponseWriter = &responseOutparamWriter{}
var _ http.ResponseWriter = (*responseOutparamWriter)(nil)

type IncomingRequest = types.IncomingRequest

Expand All @@ -34,6 +34,7 @@ func (row *responseOutparamWriter) Header() http.Header {
}

func (row *responseOutparamWriter) Write(buf []byte) (int, error) {
// NOTE(lxf): If this is the first write, make sure we set the headers/statuscode
row.headerOnce.Do(row.reconcile)
if row.headerErr != nil {
return 0, row.headerErr
Expand Down Expand Up @@ -74,7 +75,7 @@ func (row *responseOutparamWriter) reconcileHeaders() error {
}
}

// from now on these are trailers
// NOTE(lxf): once headers are written we clear them out so they can emit http trailers
row.httpHeaders = http.Header{}

return nil
Expand Down Expand Up @@ -164,8 +165,7 @@ func NewHttpRequest(ir IncomingRequest) (req *http.Request, err error) {
pathWithQuery = *p.Some()
}

trailers := http.Header{}
body, err := NewIncomingBodyTrailer(ir, trailers)
body, trailers, err := NewIncomingBodyTrailer(ir)
if err != nil {
return nil, fmt.Errorf("failed to consume incoming request %s", err)
}
Expand Down
11 changes: 5 additions & 6 deletions net/wasihttp/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var _ http.RoundTripper = (*Transport)(nil)

var (
DefaultTransport = &Transport{
// NOTE(lxf): Same as stdlib http.Transport
ConnectTimeout: 30 * time.Second,
}
DefaultClient = &http.Client{Transport: DefaultTransport}
Expand Down Expand Up @@ -71,7 +72,7 @@ func (r *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
types.OutgoingBodyFinish(body, cm.Some(trailers))
}

top := handleResp.OK()
top := *handleResp.OK()
// wait until resp is returned
subscription := top.Subscribe()
subscription.Block()
Expand All @@ -92,16 +93,14 @@ func (r *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("%v", resultOption.Err())
}

result := resultOption.OK()
trailers := http.Header{}

respBody, err := NewIncomingBodyTrailer(result, trailers)
incomingBodyTrailer := *resultOption.OK()
respBody, trailers, err := NewIncomingBodyTrailer(incomingBodyTrailer)
if err != nil {
return nil, fmt.Errorf("failed to consume incoming request %s", err)
}

resp := &http.Response{
StatusCode: int(result.Status()),
StatusCode: int(incomingBodyTrailer.Status()),
Body: respBody,
Trailer: trailers,
}
Expand Down
14 changes: 8 additions & 6 deletions net/wasihttp/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type BodyConsumer interface {
Consume() (result cm.Result[types.IncomingBody, types.IncomingBody, struct{}])
Headers() (result types.Fields)
}

type inputStreamReader struct {
consumer BodyConsumer
body types.IncomingBody
Expand Down Expand Up @@ -85,22 +86,23 @@ func (r *inputStreamReader) Read(p []byte) (n int, err error) {
return int(readList.Len()), nil
}

func NewIncomingBodyTrailer(consumer BodyConsumer, trailers http.Header) (io.ReadCloser, error) {
func NewIncomingBodyTrailer(consumer BodyConsumer) (io.ReadCloser, http.Header, error) {
trailers := http.Header{}
consumeResult := consumer.Consume()
if consumeResult.IsErr() {
return nil, fmt.Errorf("failed to consume incoming request %s", *consumeResult.Err())
return nil, nil, fmt.Errorf("failed to consume incoming request %s", *consumeResult.Err())
}
body := consumeResult.OK()
body := *consumeResult.OK()
streamResult := body.Stream()
if streamResult.IsErr() {
return nil, fmt.Errorf("failed to consume incoming requests's stream %s", streamResult.Err())
return nil, nil, fmt.Errorf("failed to consume incoming requests's stream %s", streamResult.Err())
}
return &inputStreamReader{
consumer: consumer,
trailers: trailers,
body: *body,
body: body,
stream: *streamResult.OK(),
}, nil
}, trailers, nil
}

type outputStreamReader struct {
Expand Down
13 changes: 0 additions & 13 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,7 @@ package component

import (
"embed"
"log/slog"

"go.wasmcloud.dev/component/log/wasilog"
"go.wasmcloud.dev/component/net"
)

//go:embed wit/*
var Wit embed.FS

var DefaultLogger = slog.New(wasilog.DefaultOptions().NewHandler())

func ContextLogger(wasiContext string) *slog.Logger {
return DefaultLogger.With(wasilog.ContextAttr(wasiContext))
}

// exposed as public variable so components can mock calls
var EnableSockets = net.EnableSockets
Loading

0 comments on commit c3684a1

Please sign in to comment.