Skip to content

Commit

Permalink
Minor refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Aug 4, 2024
1 parent fbc96ec commit b8bbee3
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 69 deletions.
2 changes: 1 addition & 1 deletion cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func run() cli.ActionFunc {
NS: ns,
IPFS: node,
Host: routedhost.Wrap(h, node.Routing()),
}.Build(c.Context))
}.Build())
}

return wetware.Serve(c.Context)
Expand Down
Binary file modified examples/hello-world/main.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions system/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (f FS) Open(name string) (fs.File, error) {
}
}

path, node, err := f.Resolve(name)
path, node, err := f.Resolve(f.Ctx, name)
if err != nil {
return nil, &fs.PathError{
Op: "open",
Expand All @@ -68,13 +68,13 @@ func (f FS) Open(name string) (fs.File, error) {
}, nil
}

func (f FS) Resolve(name string) (path.Path, files.Node, error) {
func (f FS) Resolve(ctx context.Context, name string) (path.Path, files.Node, error) {
joined, err := path.Join(f.Root, clean(name))
if err != nil {
return nil, nil, err
}

node, err := f.API.Get(f.Ctx, joined)
node, err := f.API.Get(ctx, joined)
return joined, node, err
}

Expand All @@ -91,7 +91,7 @@ func clean(name string) string {
// Stat returns a FileInfo describing the file.
// If there is an error, it should be of type *PathError.
func (f FS) Stat(name string) (fs.FileInfo, error) {
path, node, err := f.Resolve(name)
path, node, err := f.Resolve(f.Ctx, name)
if err != nil {
return nil, err
}
Expand Down
36 changes: 36 additions & 0 deletions system/system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package system

import (
"io"
"os"
)

type Streams struct {
Reader io.Reader
Writer io.WriteCloser
ErrWriter io.WriteCloser
}

func (std Streams) Stdin() io.Reader {
if std.Reader == nil {
return os.Stdin
}

return std.Reader
}

func (std Streams) Stdout() io.WriteCloser {
if std.Writer == nil {
return os.Stdout
}

return std.Writer
}

func (std Streams) Stderr() io.WriteCloser {
if std.ErrWriter == nil {
return os.Stdout
}

return std.ErrWriter
}
94 changes: 43 additions & 51 deletions ww.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (
"errors"
"io"
"log/slog"
"os"
"path/filepath"
"runtime"

"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
iface "github.com/ipfs/kubo/core/coreiface"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/thejerf/suture/v4"
Expand All @@ -30,46 +28,23 @@ type Resolver interface {
}

type Config struct {
NS string
IPFS iface.CoreAPI
Host host.Host
Router routing.Routing
Debug bool // debug info enabled
Stdio struct {
Reader io.Reader
Writer io.WriteCloser
ErrWriter io.WriteCloser
}
}

func (cfg Config) Build(ctx context.Context) Cluster {
return Cluster{
Config: cfg,
}
}

func (c Config) Stdin() io.Reader {
if c.Stdio.Reader == nil {
return os.Stdin
}

return c.Stdio.Reader
NS string
IPFS iface.CoreAPI
Host host.Host
IO system.Streams
Runtime wazero.RuntimeConfig
}

func (c Config) Stdout() io.WriteCloser {
if c.Stdio.Writer == nil {
return os.Stdout
func (config Config) Build() Cluster {
if config.Runtime == nil {
config.Runtime = wazero.NewRuntimeConfig().
// WithCompilationCache().
WithCloseOnContextDone(true)
}

return c.Stdio.Writer
}

func (c Config) Stderr() io.WriteCloser {
if c.Stdio.ErrWriter == nil {
return os.Stdout
return Cluster{
Config: config,
}

return c.Stdio.ErrWriter
}

type Cluster struct {
Expand All @@ -80,18 +55,18 @@ func (c Cluster) String() string {
return c.Config.NS
}

// Serve the cluster's root process
// Serve the cluster's root filesystem
func (c Cluster) Serve(ctx context.Context) error {
root, err := path.NewPath(c.NS)
fs, err := c.NewFS(ctx)
if err != nil {
return err
}

node, err := c.Resolve(ctx, root)
root, err := c.Resolve(ctx, fs.Root)
if err != nil {
return err
}
defer node.Close()
defer root.Close()

r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
WithCloseOnContextDone(true))
Expand All @@ -103,12 +78,7 @@ func (c Cluster) Serve(ctx context.Context) error {
}
defer wasi.Close(ctx)

bytecode, err := c.LoadByteCode(ctx, node)
if err != nil {
return err
}

compiled, err := r.CompileModule(ctx, bytecode)
compiled, err := c.CompileNode(ctx, r, root)
if err != nil {
return err
}
Expand All @@ -119,10 +89,10 @@ func (c Cluster) Serve(ctx context.Context) error {
WithName(c.NS).
// WithArgs().
// WithEnv().
WithStdin(c.Stdin()).
WithStderr(c.Stderr()).
WithStdout(c.Stdout()).
WithFS(system.FS{Ctx: ctx, API: c.IPFS.Unixfs(), Root: root}).
WithStdin(c.IO.Stdin()).
WithStdout(c.IO.Stdout()).
WithStderr(c.IO.Stderr()).
WithFS(fs).
WithRandSource(rand.Reader).
WithOsyield(runtime.Gosched))
if err != nil {
Expand All @@ -134,6 +104,19 @@ func (c Cluster) Serve(ctx context.Context) error {
return err
}

func (c Cluster) NewFS(ctx context.Context) (*system.FS, error) {
root, err := path.NewPath(c.NS)
if err != nil {
return nil, err
}

return &system.FS{
Ctx: ctx,
API: c.IPFS.Unixfs(),
Root: root,
}, nil
}

func (c Cluster) Resolve(ctx context.Context, root path.Path) (n files.Node, err error) {
switch ns := root.Segments()[0]; ns {
case "ipld":
Expand All @@ -152,6 +135,15 @@ func (c Cluster) Resolve(ctx context.Context, root path.Path) (n files.Node, err
return
}

func (c Cluster) CompileNode(ctx context.Context, r wazero.Runtime, node files.Node) (wazero.CompiledModule, error) {
bytecode, err := c.LoadByteCode(ctx, node)
if err != nil {
return nil, err
}

return r.CompileModule(ctx, bytecode)
}

func (c Cluster) LoadByteCode(ctx context.Context, node files.Node) (b []byte, err error) {
err = files.Walk(node, func(fpath string, node files.Node) error {
if b != nil {
Expand Down
18 changes: 5 additions & 13 deletions ww_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tetratelabs/wazero/sys"
ww "github.com/wetware/go"
"github.com/wetware/go/system"
)

func TestService(t *testing.T) {
Expand All @@ -22,23 +23,14 @@ func TestService(t *testing.T) {
ipfs, err := rpc.NewLocalApi()
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

buf := new(bytes.Buffer)
cluster := ww.Config{
NS: root.String(),
IPFS: ipfs,
// Router: ,
// Debug: ,
Stdio: struct {
Reader io.Reader
Writer io.WriteCloser
ErrWriter io.WriteCloser
}{Writer: nopCloser{buf}},
}.Build(ctx)

err = cluster.Serve(ctx)
IO: system.Streams{Writer: nopCloser{buf}},
}.Build()

err = cluster.Serve(context.Background())
status := err.(*sys.ExitError).ExitCode()
require.Zero(t, status)

Expand Down

0 comments on commit b8bbee3

Please sign in to comment.