wasmrpc

package module
v0.0.0-...-6c78b00 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 7 Imported by: 0

README

WASM-RPC


This module provides a convenient and simple way for WASM modules to send RPC requests to the host, without necessarily generating bindings for every possible client language. Clients only need to implement a single function linked to the WASM environment module and a JSON (TODO) or MSGP serializer.

Runtime setup

The host must provide a set of receivers similar to the stdlib RPC services. Receivers are functions exposed to client modules and are responsible for defining their own arguments and responses.

// ArithmeticEnv is an example of a host environment used to store a persistent state between invocations of any receiver
type ArithmeticEnv struct {
    LastCaller string
}

// AdderReceiver is an example of a receiver that simply adds two numbers.
type AdderReceiver struct {
    Args AddArgs
    Resp AddResp
}

// AddArgs are an example of parameters for an addition receiver
type AddArgs struct {
    A, B int
}

// AddResp is an example of the result of an addition receiver
type AddResp struct {
    C int
}

// Binding implements the wasmrpc.Receiver interface
func (a *AdderReceiver) Binding() (codecs.RpcArgs, codecs.RpcResp) {
    return &a.Args, &a.Resp
}

// Execute implements the wasmrpc.Receiver interface
func (a *AdderReceiver) Execute(caller string, env *ArithmeticEnv, _ any) (err error) {
    env.LastCaller = caller
    a.Resp.C = a.Args.A + a.Args.B
    return
}

Receivers are bundled into a wasmrpc.Host with a host environment to store persistent data available to all receivers. The wasmrpc.Host is then used to instantiate a new RPC runtime where client modules have access to the host and it's receivers.

func main() {
    env := new(mock.ArithmeticEnv)
    host := wasmrpc.NewHost(env)
    host.RegisterReceiver("add", new(AdderReceiver))

	rt, runtimeContext, err := wasmrpc.NewRpcRuntime(host, wazero.NewRuntimeConfig())
	// Error checking...
}

After that use the wazero.Runtime like usual and make sure to always pass the runtimeContext (or a child context) to all wazero function calls (like instantiations or module function calls). This is required to provide modules with a link to the RPC host.

An example of the receiver implementation can be seen in the mock package and an extended example of the runtime usage can be seen in the test.

Client setup

On the client side define a function with the following signature and make sure that the function is imported by the linker from the env module as WasmRPC.

func (param i32 i32 i32) (result i32))

To invoke an RPC function the client needs to serialize the request into an allocated buffer and call WasmRPC where the parameters in turn are:

  1. The memory pointer to the start of the buffer
  2. The length of the request
  3. The maximum capacity of the buffer

The function call will block until execution on the host is complete. The host will then write the result into the same buffer and return the length of the response. In case that the response would exceed the allocated buffer capacity no data will be written and the returned response size is zero.

Examples for different languages and compilation instructions can be found in the examples folder (TODO).

RPC spec

For MessagePack the MessagePack-RPC Specification must be used for encoding end decoding messages. The msgprpc package provides a tinygo friendly implementation ready to use.

For JSON the JSON-RPC 2.0 Specification must be used.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRpcRuntime

func NewRpcRuntime[E any](srv Host[E], cfg wazero.RuntimeConfig) (runtime wazero.Runtime, ctx context.Context, err error)

NewRpcRuntime will instantiate a new wazero runtime with the given config. Additionally, the RPC Host and all its receivers will be exposed to all modules in this runtime.

IMPORTANT: Pass the returned context.Context (or a child context) to all runtime function calls. This context is required to link modules to the RPC Host.

Types

type Host

type Host[E any] struct {
	// contains filtered or unexported fields
}

func NewHost

func NewHost[E any](env E) Host[E]

func (*Host[E]) RegisterMiddleware

func (h *Host[E]) RegisterMiddleware(middleware Middleware)

RegisterMiddleware will add a preprocessing step to every client function call. Use this to handle permissions or provide context to downstream receivers. Only one Middleware can be registered and subsequent calls will override it.

func (*Host[E]) RegisterReceiver

func (h *Host[E]) RegisterReceiver(name string, rcvr Receiver[E])

RegisterReceiver exposes a named function to clients of this Host.

type Middleware

type Middleware func(caller, function string) (info any, err error)

A Middleware can preprocess client function calls. Using the callers module name and called function a Middleware can return either an error or info.

  • If an error is returned, no receivers will be executed and the error is retuned to the client.
  • When no error is returned, the info is passed to the receiver invocation.

type Receiver

type Receiver[E any] interface {
	// Binding returns a pointer to an allocated input and output type.
	// Later, wasmrpc will write parameters to the input type and read the result from the output type.
	Binding() (codecs.RpcArgs, codecs.RpcResp)
	// Execute is called by wasmrpc to handle calls to this Receiver.
	// For context, the user defined environment, the module name of the caller and the middleware info is passed to the Receiver.
	Execute(caller string, env E, info any) (err error)
}

A Receiver exposes a function to clients.

Source Files

  • rpc.go
  • runtime.go

Directories

Path Synopsis
Package codecs implements available encoding formats used for client<->host communication.
Package codecs implements available encoding formats used for client<->host communication.
Package msgprpc provides a static en-/decoder for requests and responses of the [MessagePack-RPC Specification](https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md).
Package msgprpc provides a static en-/decoder for requests and responses of the [MessagePack-RPC Specification](https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md).
test
Package wasmguest is a go client implementation compatible with the wasmrpc main module.
Package wasmguest is a go client implementation compatible with the wasmrpc main module.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL