wasmer_wasix/runners/wcgi/
callbacks.rs

1use std::{collections::HashMap, sync::Arc};
2
3use virtual_fs::Pipe;
4use wasmer::{Memory, Module, Store};
5
6use super::{create_env::default_recycle_env, handler::SetupBuilder, *};
7use crate::WasiEnv;
8use wasmer_types::ModuleHash;
9
10/// Configuration used for creating a new environment
11pub struct CreateEnvConfig {
12    pub env: HashMap<String, String>,
13    pub program_name: String,
14    pub module: Module,
15    pub module_hash: ModuleHash,
16    pub runtime: Arc<dyn crate::runtime::Runtime + Send + Sync>,
17    pub setup_builder: SetupBuilder,
18}
19
20/// Result of a create operation on a new environment
21pub struct CreateEnvResult {
22    pub env: WasiEnv,
23    pub memory: Option<(Memory, Store)>,
24    pub body_sender: Pipe,
25    pub body_receiver: Pipe,
26    pub stderr_receiver: Pipe,
27}
28
29/// Configuration used for reusing an new environment
30pub struct RecycleEnvConfig {
31    pub env: WasiEnv,
32    pub memory: Memory,
33    pub store: Store,
34}
35
36/// Callbacks that are triggered at various points in the lifecycle of a runner
37/// and any WebAssembly instances it may start.
38#[async_trait::async_trait]
39pub trait Callbacks: std::fmt::Debug + Send + Sync + 'static {
40    /// A callback that is called whenever the server starts.
41    fn started(&self, _abort: AbortHandle) {}
42
43    /// Data was written to stderr by an instance.
44    fn on_stderr(&self, _stderr: &[u8]) {}
45
46    /// Reading from stderr failed.
47    fn on_stderr_error(&self, _error: std::io::Error) {}
48
49    /// Recycle the WASI environment
50    async fn recycle_env(&self, conf: RecycleEnvConfig) {
51        default_recycle_env(conf).await
52    }
53
54    /// Create the WASI environment
55    async fn create_env(&self, conf: CreateEnvConfig) -> anyhow::Result<CreateEnvResult> {
56        default_create_env(conf).await
57    }
58}
59
60#[derive(Debug)]
61pub struct NoOpWcgiCallbacks;
62
63impl Callbacks for NoOpWcgiCallbacks {}