Skip to content

Commit

Permalink
Allow ww.Env to bootstrap from stdin.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Nov 14, 2024
1 parent c0fdccc commit a34c5ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Binary file modified examples/deliver/main.wasm
Binary file not shown.
28 changes: 28 additions & 0 deletions ww.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/fs"

"capnproto.org/go/capnp/v3"
"github.com/blang/semver/v4"
"github.com/libp2p/go-libp2p/core/host"
"github.com/tetratelabs/wazero"
Expand Down Expand Up @@ -77,13 +78,40 @@ func (env Env) CompileAndServe(ctx context.Context, bytecode []byte) error {
}
defer p.Close(ctx)

if err := env.Bootstrap(ctx, &p); err != nil {
return err
}

release := Bind(ctx, env.Host, &p)
defer release()

<-ctx.Done()
return ctx.Err()
}

func (env Env) Bootstrap(ctx context.Context, p *proc.P) error {
b, err := io.ReadAll(&io.LimitedReader{
R: env.Stdin,
N: int64(1<<32 - 1), // max u32
})
if err != nil {
return err
}

m, err := capnp.Unmarshal(b)
if err != nil {
return err
}
defer m.Release()

call, err := proc.ReadRootMethodCall(m)
if err != nil {
return err
}

return p.Deliver(ctx, call)
}

type ReleaseFunc func()

func Bind(ctx context.Context, h host.Host, p *proc.P) ReleaseFunc {
Expand Down

0 comments on commit a34c5ea

Please sign in to comment.