Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

WIT Interface Reference

This is the complete reference for all Torvyn WIT interfaces. All interfaces are defined in the torvyn:streaming@0.1.0 package unless otherwise noted.

Core Types (torvyn:streaming/types)

resource buffer

A host-managed immutable byte buffer. Buffers exist in host memory, not in component linear memory. Components interact with buffers through opaque handles.

MethodSignatureOwnershipDescription
sizefunc() -> u64Read-only (borrow)Returns the byte length of the buffer contents.
content-typefunc() -> stringRead-only (borrow)Returns a content-type hint (e.g., "application/json"). Empty string if unset.
readfunc(offset: u64, len: u64) -> list<u8>Read-only (borrow). Triggers a measured copy from host to component memory.Read up to len bytes starting at offset. Returns fewer bytes if buffer is shorter than offset+len.
read-allfunc() -> list<u8>Read-only (borrow). Triggers a measured copy.Read the entire buffer contents. Equivalent to read(0, self.size()).

Performance note: read and read-all copy data from host memory into component linear memory. The resource manager records this as a PayloadRead copy event. Components that only need metadata should use size() and content-type() instead.

resource mutable-buffer

A writable buffer obtained from the host. Single-owner. Must be frozen into an immutable buffer before returning to the host.

MethodSignatureOwnershipDescription
writefunc(offset: u64, bytes: list<u8>) -> result<_, buffer-error>Write (own). Triggers a measured copy from component to host memory.Write bytes at offset. Extends buffer if necessary up to capacity.
appendfunc(bytes: list<u8>) -> result<_, buffer-error>Write (own). Triggers a measured copy.Append bytes to the end of current content.
sizefunc() -> u64Read-only (own)Current byte length of written content.
capacityfunc() -> u64Read-only (own)Maximum capacity of this buffer.
set-content-typefunc(content-type: string)Write (own)Set the content-type hint.
freezefunc() -> bufferConsumes the mutable-buffer handle. Returns an owned immutable buffer.Finalize into an immutable buffer. After this call, the mutable-buffer handle is invalid.

resource flow-context

Carries trace correlation, deadline, and pipeline-scoped metadata. Created by the runtime, passed to components with each stream element.

MethodSignatureDescription
trace-idfunc() -> stringW3C Trace ID (hex-encoded, 32 chars). Empty if tracing disabled.
span-idfunc() -> stringCurrent span ID (hex-encoded, 16 chars). Empty if tracing disabled.
deadline-nsfunc() -> u64Remaining deadline in nanoseconds. 0 means no deadline set.
flow-idfunc() -> stringUnique flow identifier (opaque string).

record element-meta

FieldTypeDescription
sequenceu64Monotonic sequence number within the flow. Assigned by the runtime.
timestamp-nsu64Wall-clock timestamp (ns since Unix epoch). Assigned by the runtime.
content-typestringContent type of the payload.

record stream-element

The fundamental unit of data flow. Passed to processor.process() and sink.push().

FieldTypeOwnershipDescription
metaelement-metaCopied (small record)Element metadata.
payloadborrow<buffer>Borrowed. Must not be stored beyond the function call.Reference to the payload buffer.
contextborrow<flow-context>Borrowed.Reference to the flow context.

record output-element

Produced by components that create new data. Returned from processor.process() and source.pull().

FieldTypeOwnershipDescription
metaelement-metaCopied. sequence and timestamp-ns are advisory — the runtime may overwrite them.Output metadata.
payloadbufferOwned. Ownership transfers from the component to the runtime.Output payload buffer.

variant process-result

CasePayloadDescription
emitoutput-elementThe component produced output. The buffer in output-element is owned by the runtime after the call returns.
drop(none)The component consumed the input but produced no output. Not an error — used for filtering, deduplication, aggregation.

variant process-error

CasePayloadRuntime Behavior
invalid-inputstringThe input element was malformed. Error policy applies (skip, retry, terminate).
unavailablestringA required resource or service was unavailable. May trigger circuit-breaker logic.
internalstringUnexpected internal error. Use sparingly.
deadline-exceeded(none)The processing deadline has passed. Feeds into timeout accounting.
fatalstringThe component is permanently unable to process further elements. Triggers teardown.

variant buffer-error

CaseDescription
capacity-exceededWrite would exceed the buffer’s capacity limit.
out-of-boundsOffset is beyond current bounds.
allocation-failedstring — Host-side allocation failure.

enum backpressure-signal

CaseDescription
readyConsumer is ready to accept more data.
pauseConsumer requests the producer to pause.

Core Interfaces

interface buffer-allocator

Imported by components that produce output (processors, sources).

FunctionSignatureDescription
allocatefunc(capacity-hint: u64) -> result<mutable-buffer, buffer-error>Request a new mutable buffer. The host may allocate larger than requested but never smaller. Returns error if memory budget is exceeded.
clone-into-mutablefunc(source: borrow<buffer>) -> result<mutable-buffer, buffer-error>Request a mutable buffer initialized with a copy of an existing buffer’s contents. The source buffer is borrowed, not consumed.

interface processor

Exported by transform components.

FunctionSignatureDescription
processfunc(input: stream-element) -> result<process-result, process-error>Process a single stream element. Input is borrowed; output (if emit) is owned by the runtime. Called once per element, not concurrently.

interface source

Exported by data-producing components.

FunctionSignatureDescription
pullfunc() -> result<option<output-element>, process-error>Pull the next element. ok(some(element)): data available. ok(none): source exhausted. err(error): production error. Output buffer is owned by the runtime.
notify-backpressurefunc(signal: backpressure-signal)Receive a backpressure signal from the downstream pipeline. Called by the runtime between pull() invocations.

interface sink

Exported by data-consuming components.

FunctionSignatureDescription
pushfunc(element: stream-element) -> result<backpressure-signal, process-error>Push an element into the sink. Returns ready (accept more) or pause (slow down). Input is borrowed — sink must copy payload bytes during this call if it needs to buffer them.
completefunc() -> result<_, process-error>Signal that no more elements will arrive. Sink should flush any buffered data.

interface lifecycle

Optional. Exported by components that need initialization or cleanup.

FunctionSignatureDescription
initfunc(config: string) -> result<_, process-error>Called once after instantiation, before stream processing. Configuration string is component-specific (JSON recommended). Error prevents pipeline startup.
teardownfunc()Called once during shutdown. Best-effort — the runtime may skip this on forced termination.

Extension Interfaces

interface filter (torvyn:filtering@0.1.0)

FunctionSignatureDescription
evaluatefunc(element: stream-element) -> result<bool, process-error>Accept (true) or reject (false) an element. Input is borrowed. No output buffer allocation — filters are extremely cheap.

interface router (torvyn:filtering@0.1.0)

FunctionSignatureDescription
routefunc(element: stream-element) -> result<list<string>, process-error>Return output port names for this element. Empty list = drop. Multiple names = fan-out. Port names must match topology configuration.

interface aggregator (torvyn:aggregation@0.1.0)

FunctionSignatureDescription
ingestfunc(element: stream-element) -> result<option<output-element>, process-error>Absorb an element into internal state. Optionally emit an aggregated result.
flushfunc() -> result<list<output-element>, process-error>Emit any remaining buffered results. Called when the upstream flow completes.

Standard Worlds

WorldImportsExportsUse Case
transformtypes, buffer-allocatorprocessorStateless stream processor
managed-transformtypes, buffer-allocatorprocessor, lifecycleProcessor with init/teardown
data-sourcetypes, buffer-allocatorsourceData producer
managed-sourcetypes, buffer-allocatorsource, lifecycleSource with init/teardown
data-sinktypessinkData consumer
managed-sinktypessink, lifecycleSink with init/teardown
content-filtertypesfilterAccept/reject filter
content-routertypesrouterMulti-port router
stream-aggregatortypes, buffer-allocatoraggregator, lifecycleStateful aggregator