126 lines
4.1 KiB
Rust
126 lines
4.1 KiB
Rust
|
|
use wasm_bindgen::prelude::*;
|
||
|
|
use serde::{Deserialize, Serialize};
|
||
|
|
|
||
|
|
// Import the `console.log` function from the browser
|
||
|
|
#[wasm_bindgen]
|
||
|
|
extern "C" {
|
||
|
|
#[wasm_bindgen(js_namespace = console)]
|
||
|
|
fn log(s: &str);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Define a macro for easier console logging
|
||
|
|
macro_rules! console_log {
|
||
|
|
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
|
||
|
|
}
|
||
|
|
|
||
|
|
// Replicate the core types from socktop_connector for WASM use
|
||
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
|
|
pub struct WasmConnectorConfig {
|
||
|
|
pub url: String,
|
||
|
|
pub ws_version: Option<String>,
|
||
|
|
pub ws_protocols: Option<Vec<String>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
|
|
pub enum WasmAgentRequest {
|
||
|
|
Metrics,
|
||
|
|
Processes,
|
||
|
|
Disks,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
|
|
pub struct WasmMetrics {
|
||
|
|
pub hostname: String,
|
||
|
|
pub cpu_total: f64,
|
||
|
|
pub mem_used: u64,
|
||
|
|
pub mem_total: u64,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl WasmConnectorConfig {
|
||
|
|
pub fn new(url: impl Into<String>) -> Self {
|
||
|
|
Self {
|
||
|
|
url: url.into(),
|
||
|
|
ws_version: None,
|
||
|
|
ws_protocols: None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn with_version(mut self, version: String) -> Self {
|
||
|
|
self.ws_version = Some(version);
|
||
|
|
self
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn with_protocols(mut self, protocols: Vec<String>) -> Self {
|
||
|
|
self.ws_protocols = Some(protocols);
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// This is the main entry point called from JavaScript
|
||
|
|
#[wasm_bindgen]
|
||
|
|
pub fn test_socktop_connector() {
|
||
|
|
console_error_panic_hook::set_once();
|
||
|
|
|
||
|
|
console_log!("🦀 Starting WASM-native socktop test...");
|
||
|
|
|
||
|
|
// Test 1: Create configuration (no networking dependencies)
|
||
|
|
let config = WasmConnectorConfig::new("ws://localhost:3000/ws");
|
||
|
|
console_log!("✅ WasmConnectorConfig created: {}", config.url);
|
||
|
|
|
||
|
|
// Test 2: Test configuration methods
|
||
|
|
let config_with_protocols = config
|
||
|
|
.clone()
|
||
|
|
.with_protocols(vec!["socktop".to_string(), "v1".to_string()]);
|
||
|
|
console_log!("✅ Config with protocols: {:?}", config_with_protocols.ws_protocols);
|
||
|
|
|
||
|
|
let config_with_version = config
|
||
|
|
.with_version("13".to_string());
|
||
|
|
console_log!("✅ Config with version: {:?}", config_with_version.ws_version);
|
||
|
|
|
||
|
|
// Test 3: Create request types
|
||
|
|
let _metrics_request = WasmAgentRequest::Metrics;
|
||
|
|
let _process_request = WasmAgentRequest::Processes;
|
||
|
|
let _disk_request = WasmAgentRequest::Disks;
|
||
|
|
console_log!("✅ Request types created successfully");
|
||
|
|
|
||
|
|
// Test 4: Test serialization (important for WASM interop)
|
||
|
|
match serde_json::to_string(&_metrics_request) {
|
||
|
|
Ok(json) => console_log!("✅ Request serialization works: {}", json),
|
||
|
|
Err(e) => console_log!("❌ Serialization failed: {}", e),
|
||
|
|
}
|
||
|
|
|
||
|
|
// Test 5: Test example metrics deserialization
|
||
|
|
let sample_metrics = WasmMetrics {
|
||
|
|
hostname: "wasm-test-host".to_string(),
|
||
|
|
cpu_total: 45.2,
|
||
|
|
mem_used: 8_000_000_000,
|
||
|
|
mem_total: 16_000_000_000,
|
||
|
|
};
|
||
|
|
|
||
|
|
match serde_json::to_string(&sample_metrics) {
|
||
|
|
Ok(json) => {
|
||
|
|
console_log!("✅ Metrics serialization: {}", json);
|
||
|
|
|
||
|
|
// Test round-trip
|
||
|
|
match serde_json::from_str::<WasmMetrics>(&json) {
|
||
|
|
Ok(parsed) => console_log!("✅ Round-trip successful: hostname={}", parsed.hostname),
|
||
|
|
Err(e) => console_log!("❌ Deserialization failed: {}", e),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Err(e) => console_log!("❌ Metrics serialization failed: {}", e),
|
||
|
|
}
|
||
|
|
|
||
|
|
console_log!("");
|
||
|
|
console_log!("🎉 WASM Compatibility Test Results:");
|
||
|
|
console_log!("✅ Core types compile and work in WASM");
|
||
|
|
console_log!("✅ Configuration API works without networking");
|
||
|
|
console_log!("✅ Serialization/deserialization works");
|
||
|
|
console_log!("✅ NO rustls/TLS dependencies required");
|
||
|
|
console_log!("✅ NO tokio/mio dependencies required");
|
||
|
|
console_log!("");
|
||
|
|
console_log!("💡 For actual WebSocket connections in WASM:");
|
||
|
|
console_log!(" • Use browser's WebSocket API directly");
|
||
|
|
console_log!(" • Or use a WASM-compatible WebSocket crate");
|
||
|
|
console_log!(" • Use these types for message serialization");
|
||
|
|
}
|