use std::path::Path;
use std::sync::Arc;
use async_trait::async_trait;
use crate::error::Error;
use crate::middleware::MiddlewareKind;
#[derive(Debug, Clone)]
pub struct PluginExport {
pub name: String,
pub kind: MiddlewareKind,
pub stateless: bool,
pub needs_body: bool,
pub inspects: Vec<String>,
}
#[derive(Debug)]
pub struct PluginMetadata {
pub name: String,
pub version: String,
pub abi_version: String,
pub exports: Vec<PluginExport>,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ModuleId(pub Arc<str>);
pub enum ContextValue {
Text(String),
Bytes(Vec<u8>),
Int64(i64),
Uint64(u64),
Boolean(bool),
ListText(Vec<String>),
}
pub struct ContextEntry {
pub path: String,
pub value: ContextValue,
}
#[derive(Debug, Clone)]
pub struct Header {
pub name: String,
pub value: String,
}
#[derive(Debug, Clone)]
pub struct BytesView {
pub data: Vec<u8>,
pub truncated: bool,
}
pub struct L4PeekInput {
pub peek: Vec<u8>,
pub context: Vec<ContextEntry>,
}
#[derive(Debug)]
pub enum L4PeekDecision {
Continue,
Close,
}
pub struct L4BytesInput {
pub bytes: BytesView,
pub context: Vec<ContextEntry>,
}
#[derive(Debug)]
pub enum L4BytesDecision {
Continue,
Tunnel,
Close,
}
pub struct L7RequestInput {
pub method: String,
pub uri: String,
pub headers: Vec<Header>,
pub body: Option<BytesView>,
pub context: Vec<ContextEntry>,
}
#[derive(Debug, Clone)]
pub struct SynthResponse {
pub status: u16,
pub headers: Vec<Header>,
pub body: Vec<u8>,
}
#[derive(Debug)]
pub enum L7RequestDecision {
Continue,
Short(SynthResponse),
Close,
}
pub struct L7ResponseInput {
pub status: u16,
pub headers: Vec<Header>,
pub body: Option<BytesView>,
pub context: Vec<ContextEntry>,
}
#[derive(Debug, Clone)]
pub struct ModifiedResponse {
pub status: Option<u16>,
pub headers: Option<Vec<Header>>,
pub body: Option<Vec<u8>>,
}
#[derive(Debug)]
pub enum L7ResponseDecision {
Continue,
Modify(ModifiedResponse),
Abort,
}
#[derive(Debug)]
pub enum PluginError {
Plugin { code: String, message: String, on_error_hint: Option<String> },
Trap(String),
Exhausted,
}
#[async_trait]
pub trait WasmRuntime: Send + Sync {
async fn load_component(&self, path: &Path) -> Result<Arc<PluginMetadata>, Error>;
async fn invoke_l4_peek(
&self,
module_id: &ModuleId,
export_name: &str,
args_json: &str,
input: L4PeekInput,
) -> Result<L4PeekDecision, PluginError>;
async fn invoke_l4_bytes(
&self,
module_id: &ModuleId,
export_name: &str,
args_json: &str,
input: L4BytesInput,
) -> Result<L4BytesDecision, PluginError>;
async fn invoke_l7_request(
&self,
module_id: &ModuleId,
export_name: &str,
args_json: &str,
input: L7RequestInput,
) -> Result<L7RequestDecision, PluginError>;
async fn invoke_l7_response(
&self,
module_id: &ModuleId,
export_name: &str,
args_json: &str,
input: L7ResponseInput,
) -> Result<L7ResponseDecision, PluginError>;
}