use std::sync::Arc;
use rkyv::util::AlignedVec;
use crate::cache::{
call_filter_hook, call_observe_hook, call_shape_hook, call_transform_response_hook,
call_transform_sse_event_hook,
};
use crate::cell::{LoadedPluginSlot, PluginCell};
use crate::error::WasmtimeRuntimeError;
use crate::policy::{PluginWireBounds, ShapeOriginPolicy};
use crate::{HotEngineConfig, SlotKind};
pub struct WasmPluginWireDispatch {
pub(crate) cell: Arc<PluginCell>,
pub(crate) kind: SlotKind,
pub(crate) config: Arc<HotEngineConfig>,
}
impl WasmPluginWireDispatch {
pub fn from_slot(slot: Arc<LoadedPluginSlot>, config: Arc<HotEngineConfig>) -> Self {
let cell = slot.current.load_full();
let kind = slot.kind;
Self { cell, kind, config }
}
pub fn kind(&self) -> SlotKind {
self.kind
}
pub fn config(&self) -> &Arc<HotEngineConfig> {
&self.config
}
pub fn wire_bounds(&self) -> &PluginWireBounds {
&self.config.wire_bounds
}
pub fn shape_origin_policy(&self) -> ShapeOriginPolicy {
self.config.shape_origin_policy
}
pub fn cookie_redaction(&self) -> bool {
self.config.cookie_redaction
}
pub fn call_filter(&self, input: &[u8]) -> Result<Vec<u8>, WasmtimeRuntimeError> {
call_filter_hook(&self.cell, input)
}
pub fn call_shape(&self, input: &[u8]) -> Result<Vec<u8>, WasmtimeRuntimeError> {
call_shape_hook(&self.cell, input)
}
pub fn call_observe(&self, input: &[u8]) -> Result<Vec<u8>, WasmtimeRuntimeError> {
call_observe_hook(&self.cell, input)
}
pub fn call_transform_response(
&self,
input: &[u8],
) -> Result<AlignedVec<16>, WasmtimeRuntimeError> {
call_transform_response_hook(&self.cell, input)
}
pub fn call_transform_sse_event(
&self,
input: &[u8],
) -> Result<AlignedVec<16>, WasmtimeRuntimeError> {
call_transform_sse_event_hook(&self.cell, input)
}
pub fn filter_wire_version(&self) -> Option<cc_lb_plugin_wire::schema::WireVersion> {
self.cell
.metadata
.hooks
.get(cc_lb_plugin_wire::schema::HookKind::Filter.as_str())
.and_then(|m| cc_lb_plugin_wire::schema::WireVersion::from_u8(m.wire_version))
}
pub fn shape_wire_version(&self) -> Option<cc_lb_plugin_wire::schema::WireVersion> {
self.cell
.metadata
.hooks
.get(cc_lb_plugin_wire::schema::HookKind::Shape.as_str())
.and_then(|m| cc_lb_plugin_wire::schema::WireVersion::from_u8(m.wire_version))
}
pub fn observe_wire_version(&self) -> Option<cc_lb_plugin_wire::schema::WireVersion> {
self.cell
.metadata
.hooks
.get(cc_lb_plugin_wire::schema::HookKind::Observe.as_str())
.and_then(|m| cc_lb_plugin_wire::schema::WireVersion::from_u8(m.wire_version))
}
pub fn has_response_transform(&self) -> bool {
self.cell
.metadata
.hooks
.get(cc_lb_plugin_wire::schema::HookKind::TransformResponse.as_str())
.is_some_and(|m| !m.mode.is_noop())
}
pub fn has_sse_event_transform(&self) -> bool {
self.cell
.metadata
.hooks
.get(cc_lb_plugin_wire::schema::HookKind::TransformSseEvent.as_str())
.is_some_and(|m| !m.mode.is_noop())
}
}