Skip to main content

camel_component_wasm/
endpoint.rs

1use std::path::PathBuf;
2use std::sync::Arc;
3
4use camel_api::CamelError;
5use camel_component_api::{BoxProcessor, Endpoint, ProducerContext};
6use camel_core::Registry;
7
8use crate::config::WasmConfig;
9
10pub struct WasmEndpoint {
11    uri: String,
12    module_path: PathBuf,
13    registry: Arc<std::sync::Mutex<Registry>>,
14    config: WasmConfig,
15}
16
17impl WasmEndpoint {
18    pub fn new(
19        uri: String,
20        module_path: PathBuf,
21        registry: Arc<std::sync::Mutex<Registry>>,
22        config: WasmConfig,
23    ) -> Self {
24        Self {
25            uri,
26            module_path,
27            registry,
28            config,
29        }
30    }
31
32    pub fn config(&self) -> &WasmConfig {
33        &self.config
34    }
35}
36
37impl Endpoint for WasmEndpoint {
38    fn uri(&self) -> &str {
39        &self.uri
40    }
41
42    fn create_consumer(&self) -> Result<Box<dyn camel_component_api::Consumer>, CamelError> {
43        Err(CamelError::EndpointCreationFailed(
44            "WASM consumer (from: wasm:...) is not supported in v1".to_string(),
45        ))
46    }
47
48    fn create_producer(&self, _ctx: &ProducerContext) -> Result<BoxProcessor, CamelError> {
49        let producer = crate::producer::WasmProducer::new(
50            self.module_path.clone(),
51            self.registry.clone(),
52            self.config.clone(),
53        );
54        Ok(BoxProcessor::new(producer))
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use std::sync::Mutex;
62
63    #[test]
64    fn test_wasm_endpoint_stores_config() {
65        let config = WasmConfig {
66            timeout_secs: 10,
67            max_memory_bytes: 1024 * 1024,
68        };
69        let endpoint = WasmEndpoint::new(
70            "wasm:test.wasm?timeout=10&max-memory=1048576".to_string(),
71            PathBuf::from("test.wasm"),
72            Arc::new(Mutex::new(Registry::new())),
73            config.clone(),
74        );
75        assert_eq!(endpoint.config().timeout_secs, 10);
76        assert_eq!(endpoint.config().max_memory_bytes, 1024 * 1024);
77    }
78}