Skip to content

JavaScript streams and globals

These globals are available inside wabe Disk JavaScript invocations.

API Contract
io.stdin Async iterable of Uint8Array chunks from the invocation’s stdin.
io.stdout.write(data) Write a string or byte array synchronously; return the number of bytes written.
io.stderr.write(data) Same behavior, routed to stderr.
io.stdout.writeChunks(source) Async copy from an iterable/async iterable; return bytes written.
io.stderr.writeChunks(source) Same behavior, routed to stderr.
print(...values) Convert values with String, join with spaces and write a newline to stdout.
console.log(...values) Same behavior as print.
console.error(...values) Stringify and write a newline to stderr.
args Frozen array of strings supplied by shell qjs; empty for direct HTTP JS invocation.
Echo stdin without collecting it
await io.stdout.writeChunks(io.stdin);

A script gets stdin when its source is supplied separately. Exec source selection explains the body, query and stored-path forms. Input is consumed as the script reads it.

bash(script) is an async iterable. It yields output events with {channel:'stdout'|'stderr', bytes:Uint8Array} and a terminal {exit:number} event.

Forward pipeline output
for await (const event of bash("printf 'beta\nalpha\n' | sort")) {
if (event.channel === 'stdout') io.stdout.write(event.bytes);
if (event.channel === 'stderr') io.stderr.write(event.bytes);
if (event.exit !== undefined && event.exit !== 0) {
throw Error(`pipeline exited ${event.exit}`);
}
}

It uses the same disk and the shell subset. Nested qjs calls are unavailable through this JS bash() binding. Exiting iteration early cancels the pipeline.

new TextEncoder().encode(text) returns UTF-8 bytes. new TextDecoder().decode(bytes) requires valid complete UTF-8 and returns a string. These helpers expose only the listed methods; the decoder has no streaming mode. A byte chunk can end inside a UTF-8 character, so preserve bytes when forwarding streams or decode a complete text value.

Use JSON.stringify(value) when printing objects as JSON. print(value) uses ordinary string conversion.

Scripts run as async function bodies with JavaScript built-ins. Native module imports, Node APIs, network calls and timers are unavailable. Returned values from direct HTTP JS execution must be JSON-serializable. Execution failures appear in stderr and the terminal exit status.