-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
19 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,33 @@ | ||
use std::{fs::File, io::Cursor}; | ||
use std::io::Cursor; | ||
use wasi_common::pipe::{ReadPipe, WritePipe}; | ||
use wasmtime_wasi::WasiCtxBuilder; | ||
|
||
/// A library to configure the stdio of the WASI context. | ||
/// Note that currently, wws relies on stdin and stdout | ||
/// to send and read data from the worker. | ||
/// | ||
/// The stderr is configurable just to cover use cases in which | ||
/// wws is used as a library and we want to expose the logs | ||
/// | ||
/// @see https://github.com/vmware-labs/wasm-workers-server/issues/125 | ||
/// | ||
/// The stdin/stdout approach will change in the future with | ||
/// a more performant and appropiate way. | ||
/// a more performant and appropiate approach. | ||
pub struct Stdio { | ||
/// Defines the stdin ReadPipe to send the data to the module | ||
pub stdin: ReadPipe<Cursor<String>>, | ||
/// Defines the stdout to extract the data from the module | ||
pub stdout: WritePipe<Cursor<Vec<u8>>>, | ||
/// Defines the stderr to expose logs from inside the module | ||
pub stderr: Option<WritePipe<File>>, | ||
} | ||
|
||
impl Stdio { | ||
/// Initialize the stdio. The stdin will contain the input data. | ||
pub fn new(input: &str, stderr_file: Option<File>) -> Self { | ||
let stderr; | ||
|
||
if let Some(file) = stderr_file { | ||
stderr = Some(WritePipe::new(file)); | ||
} else { | ||
stderr = None | ||
} | ||
|
||
pub fn new(input: &str) -> Self { | ||
Self { | ||
stdin: ReadPipe::from(input), | ||
stdout: WritePipe::new_in_memory(), | ||
stderr, | ||
} | ||
} | ||
|
||
pub fn configure_wasi_ctx(&self, builder: WasiCtxBuilder) -> WasiCtxBuilder { | ||
let builder = builder | ||
builder | ||
.stdin(Box::new(self.stdin.clone())) | ||
.stdout(Box::new(self.stdout.clone())); | ||
|
||
// Set stderr if it was previously configured. If not, inherit | ||
// it from the environment | ||
if let Some(pipe) = self.stderr.clone() { | ||
builder.stderr(Box::new(pipe)) | ||
} else { | ||
builder.inherit_stderr() | ||
} | ||
.stdout(Box::new(self.stdout.clone())) | ||
.inherit_stderr() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters