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 ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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.
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.