Skip to main content

Module stream

Module stream 

Source
Expand description

Streaming and async iteration support

This module provides async streaming capabilities similar to JavaScript’s async iterators and stream handling in $.stream-utils.mjs.

It mirrors the JavaScript implementation’s behavior for issue #155:

  1. The stream yields an explicit OutputChunk::Exit(code) when the process exits, so consumers can observe the exit code from inside the loop.
  2. The stream does not hang forever when the process has exited but a grandchild keeps the stdio pipes open (the readers are drained with a grace period and then aborted).
  3. The process can be stopped from inside the loop via OutputStream::kill / OutputStream::kill_with, and abandoning the stream (e.g. break) also stops the process.
  4. The stop signal is configurable via StreamingRunner::kill_signal (default SIGTERM), just like the JavaScript killSignal option.

§Usage

use command_stream::{StreamingRunner, OutputChunk};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let runner = StreamingRunner::new("yes hello");

    // Stream output as it arrives
    let mut stream = runner.stream();
    let mut count = 0;
    while let Some(chunk) = stream.next().await {
        match chunk {
            OutputChunk::Stdout(data) => {
                print!("{}", String::from_utf8_lossy(&data));
                count += 1;
                if count >= 5 {
                    // Stop the process from inside the loop.
                    stream.kill();
                }
            }
            OutputChunk::Stderr(data) => {
                eprint!("{}", String::from_utf8_lossy(&data));
            }
            OutputChunk::Exit(code) => {
                println!("Process exited with code: {}", code);
                break;
            }
        }
    }

    Ok(())
}

Structs§

OutputStream
Stream of output chunks from a process
StreamingRunner
A streaming process runner that allows async iteration over output

Enums§

OutputChunk
A chunk of output from a streaming process

Traits§

AsyncIterator
Async iterator trait for output streams
IntoStream
Extension trait to convert ProcessRunner into a stream