wasmhost

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wasmhost runs WebAssembly guests as flow nodes.

This is the real sandbox. goja is a good boundary — it has no host bindings unless you add them — but it cannot bound memory: a JavaScript function that allocates in a loop grows the Go heap until the pod is OOM-killed, and the only defence is a wall-clock timeout. WebAssembly has a linear memory with a declared maximum, so a guest that allocates past its ceiling gets a trap and the host is unharmed.

It also means a node can be written in Rust, TinyGo, Zig or AssemblyScript rather than JavaScript, which matters for the signal processing an OT flow actually wants to do.

wazero is a pure-Go runtime with no cgo, which is what keeps CGO_ENABLED=0 and the distroless image true.

Index

Constants

View Source
const (
	// ExportProcess is the entry point:
	//   emberwire_process(ptr i32, len i32) -> i64
	// The result packs an offset in the high 32 bits and a length in the low 32,
	// pointing at a JSON response in the guest's own memory.
	ExportProcess = "emberwire_process"

	// ExportAlloc lets the host place the input inside the guest's allocator
	// rather than writing over memory the guest believes it owns:
	//   emberwire_alloc(size i32) -> i32
	ExportAlloc = "emberwire_alloc"

	// ExportFree returns a buffer. Optional: a guest with an arena or a bump
	// allocator has nothing to do here.
	//   emberwire_free(ptr i32, size i32)
	ExportFree = "emberwire_free"
)

The ABI a guest implements.

Deliberately tiny: three exports, two of them allocator hooks. A wide ABI is a wide attack surface, and every entry is something a guest author has to get right. Everything travels as JSON in linear memory because a guest in any language can produce it without a shared schema compiler.

View Source
const (
	DefaultMaxMemoryBytes = 64 << 20 // 64 MiB
	DefaultTimeout        = 5 * time.Second
	DefaultMaxOutputBytes = 8 << 20 // 8 MiB

)

Defaults. Small on purpose: this runs on edge hardware alongside the flows that matter, and a node that needs more should say so explicitly.

Variables

View Source
var (
	ErrTimeout      = errors.New("wasm guest timed out")
	ErrOutOfMemory  = errors.New("wasm guest exceeded its memory limit")
	ErrGuestTrapped = errors.New("wasm guest trapped")
)

Errors a caller can distinguish.

Functions

func PackResult

func PackResult(ptr, length uint32) uint64

PackResult builds the i64 a guest must return: offset in the high 32 bits, length in the low 32. Exported so a Go guest and the host cannot disagree about the layout.

func UnpackResult

func UnpackResult(packed uint64) (ptr, length uint32)

UnpackResult is the inverse, for tests and for guest authors.

Types

type Limits

type Limits struct {
	// MaxMemoryBytes is the hard ceiling on the guest's linear memory. This is
	// the guarantee goja cannot give: past it, the guest's allocation traps and
	// the host keeps running.
	MaxMemoryBytes uint64

	// Timeout bounds one call.
	Timeout time.Duration

	// MaxOutputBytes bounds the response a guest may return, so a guest cannot
	// make the host allocate without limit by reporting a huge length.
	MaxOutputBytes uint32
}

Limits bound one guest instance.

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module is a compiled guest, shared across invocations of one node.

func Compile

func Compile(ctx context.Context, name string, wasm []byte, limits Limits) (*Module, error)

Compile prepares a guest.

The runtime is configured with no filesystem, no clock beyond what WASI needs, no network and no environment. A guest can compute and return; it cannot reach out.

func (*Module) Call

func (m *Module) Call(ctx context.Context, req Request) (*Response, error)

Call runs the guest against one message.

func (*Module) Close

func (m *Module) Close(ctx context.Context) error

Close releases the runtime and every pooled instance.

type Request

type Request struct {
	Msg    map[string]any `json:"msg"`
	Config map[string]any `json:"config,omitempty"`
}

Request is what a guest receives.

type Response

type Response struct {
	// Messages by output port. A nil entry sends nothing on that port.
	Send [][]map[string]any `json:"send,omitempty"`
	// Status badge, if the guest set one.
	Status *Status `json:"status,omitempty"`
	// Error, if the guest wants to fail the message rather than trap.
	Error string `json:"error,omitempty"`
	// Log lines.
	Logs []string `json:"logs,omitempty"`
}

Response is what a guest returns.

type Status

type Status struct {
	Fill  string `json:"fill,omitempty"`
	Shape string `json:"shape,omitempty"`
	Text  string `json:"text,omitempty"`
}

Status is a node badge.

Jump to

Keyboard shortcuts

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