varpulis-engine-wasm 0.10.0

Full Varpulis CEP engine compiled to WebAssembly — parse VPL, process events, get outputs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! # Varpulis Engine WASM
//!
//! Full VPL engine compiled to WebAssembly.  Parse VPL programs, process events,
//! and receive output events — all in the browser or any JS/TS runtime.
//!
//! ## Usage (JavaScript/TypeScript)
//!
//! ```javascript
//! import { WasmEngine } from '@varpulis/engine';
//!
//! const engine = new WasmEngine();
//! engine.load(`
//!   event Sensor:
//!     temperature: float
//!     zone: str
//!
//!   stream Hot = Sensor
//!     .where(temperature > 100)
//!     .emit(zone: zone, temp: temperature)
//! `);
//!
//! const outputs = engine.processEvent('{"event_type":"Sensor","temperature":150,"zone":"A"}');
//! // outputs: [{"stream":"Hot","event":{"zone":"A","temp":150},"timestamp":"..."}]
//! ```

use serde::Serialize;
use varpulis_runtime::engine::graph::program_to_graph;
use varpulis_runtime::Engine;
use wasm_bindgen::prelude::*;

/// WASM-accessible VPL engine.
///
/// Create with `new WasmEngine()`, load VPL via `load()`, process events via
/// `processEvent()` or `processBatch()`, and receive output events as JSON.
#[wasm_bindgen]
pub struct WasmEngine {
    engine: Engine,
    program_source: String,
    program_loaded: bool,
}

impl std::fmt::Debug for WasmEngine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WasmEngine")
            .field("program_loaded", &self.program_loaded)
            .finish()
    }
}

impl Default for WasmEngine {
    fn default() -> Self {
        Self {
            engine: Engine::new_sync(),
            program_source: String::new(),
            program_loaded: false,
        }
    }
}

/// JSON result for load/reload operations.
#[derive(Serialize)]
struct LoadResult {
    ok: bool,
    streams: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<String>,
}

/// JSON result for event processing.
#[derive(Serialize)]
struct OutputEvent {
    stream: String,
    event: serde_json::Value,
    timestamp: String,
}

/// JSON result for processing.
#[derive(Serialize)]
struct ProcessResult {
    ok: bool,
    outputs: Vec<OutputEvent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<String>,
}

#[wasm_bindgen]
impl WasmEngine {
    /// Create a new engine instance.
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self::default()
    }

    /// Load a VPL program.  Returns JSON: `{ ok, streams, error? }`.
    ///
    /// Replaces any previously loaded program.  Call this before processing events.
    pub fn load(&mut self, vpl: &str) -> String {
        let program = match varpulis_parser::parse(vpl) {
            Ok(p) => p,
            Err(e) => {
                return json(&LoadResult {
                    ok: false,
                    streams: vec![],
                    error: Some(format!("Parse error: {e}")),
                });
            }
        };

        match self.engine.load(&program) {
            Ok(()) => {
                self.program_source = vpl.to_string();
                self.program_loaded = true;
                let streams = self
                    .engine
                    .stream_names()
                    .into_iter()
                    .map(String::from)
                    .collect();
                json(&LoadResult {
                    ok: true,
                    streams,
                    error: None,
                })
            }
            Err(e) => json(&LoadResult {
                ok: false,
                streams: vec![],
                error: Some(format!("Load error: {e}")),
            }),
        }
    }

    /// Process a single event (JSON object).  Returns JSON: `{ ok, outputs, error? }`.
    ///
    /// The event JSON must include an `event_type` field matching a declared event type.
    /// Additional fields are the event data.
    ///
    /// ```json
    /// {"event_type": "Sensor", "temperature": 150, "zone": "A"}
    /// ```
    #[wasm_bindgen(js_name = "processEvent")]
    pub fn process_event(&mut self, event_json: &str) -> String {
        let event = match parse_event(event_json) {
            Ok(e) => e,
            Err(msg) => {
                return json(&ProcessResult {
                    ok: false,
                    outputs: vec![],
                    error: Some(msg),
                });
            }
        };

        match self.engine.process_batch_sync_collect(vec![event]) {
            Ok(outputs) => {
                let output_events = outputs.into_iter().map(event_to_output).collect();
                json(&ProcessResult {
                    ok: true,
                    outputs: output_events,
                    error: None,
                })
            }
            Err(e) => json(&ProcessResult {
                ok: false,
                outputs: vec![],
                error: Some(format!("Processing error: {e}")),
            }),
        }
    }

    /// Process a batch of events (JSON array of objects).  Returns JSON: `{ ok, outputs, error? }`.
    #[wasm_bindgen(js_name = "processBatch")]
    pub fn process_batch(&mut self, events_json: &str) -> String {
        let events: Vec<serde_json::Value> = match serde_json::from_str(events_json) {
            Ok(v) => v,
            Err(e) => {
                return json(&ProcessResult {
                    ok: false,
                    outputs: vec![],
                    error: Some(format!("Invalid JSON array: {e}")),
                });
            }
        };

        let parsed: Result<Vec<_>, _> = events
            .iter()
            .map(|v| {
                let s = serde_json::to_string(v).unwrap_or_default();
                parse_event(&s)
            })
            .collect();

        let events = match parsed {
            Ok(e) => e,
            Err(msg) => {
                return json(&ProcessResult {
                    ok: false,
                    outputs: vec![],
                    error: Some(msg),
                });
            }
        };

        match self.engine.process_batch_sync_collect(events) {
            Ok(outputs) => {
                let output_events = outputs.into_iter().map(event_to_output).collect();
                json(&ProcessResult {
                    ok: true,
                    outputs: output_events,
                    error: None,
                })
            }
            Err(e) => json(&ProcessResult {
                ok: false,
                outputs: vec![],
                error: Some(format!("Processing error: {e}")),
            }),
        }
    }

    /// Get the pipeline topology as a JSON graph.
    #[wasm_bindgen(js_name = "getTopology")]
    pub fn get_topology(&self) -> String {
        if self.program_source.is_empty() {
            return r#"{"nodes":[],"edges":[]}"#.to_string();
        }
        match varpulis_parser::parse(&self.program_source) {
            Ok(program) => {
                let graph = program_to_graph(&program);
                serde_json::to_string(&graph)
                    .unwrap_or_else(|_| r#"{"nodes":[],"edges":[]}"#.into())
            }
            Err(_) => r#"{"nodes":[],"edges":[]}"#.to_string(),
        }
    }

    /// Get loaded stream names as a JSON array.
    #[wasm_bindgen(js_name = "getStreams")]
    pub fn get_streams(&self) -> String {
        let streams: Vec<&str> = self.engine.stream_names();
        serde_json::to_string(&streams).unwrap_or_else(|_| "[]".into())
    }

    /// Enable or disable trace mode.
    #[wasm_bindgen(js_name = "setTrace")]
    pub fn set_trace(&mut self, enabled: bool) {
        self.engine.set_trace_enabled(enabled);
    }

    /// Drain trace entries as a JSON array.
    #[wasm_bindgen(js_name = "drainTrace")]
    pub fn drain_trace(&mut self) -> String {
        let entries = self.engine.drain_trace();
        let json_entries: Vec<serde_json::Value> = entries
            .into_iter()
            .map(|e| serde_json::to_value(format!("{e:?}")).unwrap_or_default())
            .collect();
        serde_json::to_string(&json_entries).unwrap_or_else(|_| "[]".into())
    }

    /// Validate VPL syntax without loading.  Returns JSON: `{ ok, error? }`.
    pub fn validate(&self, vpl: &str) -> String {
        match varpulis_parser::parse(vpl) {
            Ok(_) => r#"{"ok":true}"#.to_string(),
            Err(e) => {
                let error = format!("{e}");
                json(&serde_json::json!({"ok": false, "error": error}))
            }
        }
    }

    /// Check if a program is loaded.
    #[wasm_bindgen(js_name = "isLoaded")]
    pub fn is_loaded(&self) -> bool {
        self.program_loaded
    }
}

// =============================================================================
// Helpers
// =============================================================================

fn json<T: Serialize>(value: &T) -> String {
    serde_json::to_string(value)
        .unwrap_or_else(|_| r#"{"ok":false,"error":"serialization failed"}"#.into())
}

fn parse_event(json_str: &str) -> Result<varpulis_core::Event, String> {
    let value: serde_json::Value =
        serde_json::from_str(json_str).map_err(|e| format!("Invalid JSON: {e}"))?;

    let event_type = value
        .get("event_type")
        .and_then(|v| v.as_str())
        .ok_or_else(|| "Missing 'event_type' field".to_string())?;

    let mut event = varpulis_core::Event::new(event_type);

    if let Some(obj) = value.as_object() {
        for (key, val) in obj {
            if key == "event_type" || key == "timestamp" {
                continue;
            }
            if let Some(v) = json_value_to_core_value(val) {
                event = event.with_field(key.as_str(), v);
            }
        }
    }

    Ok(event)
}

fn json_value_to_core_value(v: &serde_json::Value) -> Option<varpulis_core::Value> {
    match v {
        serde_json::Value::Null => Some(varpulis_core::Value::Null),
        serde_json::Value::Bool(b) => Some(varpulis_core::Value::Bool(*b)),
        serde_json::Value::Number(n) => n
            .as_i64()
            .map(varpulis_core::Value::Int)
            .or_else(|| n.as_f64().map(varpulis_core::Value::Float)),
        serde_json::Value::String(s) => Some(varpulis_core::Value::str(s)),
        _ => None,
    }
}

fn event_to_output(event: varpulis_core::Event) -> OutputEvent {
    let mut fields = serde_json::Map::new();
    for (key, value) in &event.data {
        fields.insert(key.to_string(), core_value_to_json(value));
    }
    OutputEvent {
        stream: event.event_type.to_string(),
        event: serde_json::Value::Object(fields),
        timestamp: event.timestamp.to_rfc3339(),
    }
}

fn core_value_to_json(v: &varpulis_core::Value) -> serde_json::Value {
    match v {
        varpulis_core::Value::Null => serde_json::Value::Null,
        varpulis_core::Value::Bool(b) => serde_json::Value::Bool(*b),
        varpulis_core::Value::Int(i) => serde_json::json!(*i),
        varpulis_core::Value::Float(f) => serde_json::json!(*f),
        varpulis_core::Value::Str(s) => serde_json::Value::String(s.to_string()),
        _ => serde_json::Value::String(format!("{v:?}")),
    }
}

// =============================================================================
// Tests (run natively, not in WASM)
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_engine() {
        let engine = WasmEngine::new();
        assert!(!engine.is_loaded());
        assert_eq!(engine.get_streams(), "[]");
    }

    #[test]
    fn test_load_valid_vpl() {
        let mut engine = WasmEngine::new();
        let result = engine.load("event T:\n    x: int\n\nstream S = T .where(x > 10) .emit(v: x)");
        let json: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(json["ok"], true);
        assert!(json["streams"]
            .as_array()
            .unwrap()
            .contains(&serde_json::json!("S")));
        assert!(engine.is_loaded());
    }

    #[test]
    fn test_load_invalid_vpl() {
        let mut engine = WasmEngine::new();
        let result = engine.load("this is not valid vpl {{{{");
        let json: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(json["ok"], false);
        assert!(json["error"].as_str().unwrap().contains("Parse error"));
    }

    #[test]
    fn test_process_event_matching() {
        let mut engine = WasmEngine::new();
        engine.load("event T:\n    x: int\n\nstream S = T .where(x > 10) .emit(v: x)");
        let result = engine.process_event(r#"{"event_type":"T","x":42}"#);
        let json: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(json["ok"], true);
        let outputs = json["outputs"].as_array().unwrap();
        assert_eq!(outputs.len(), 1);
        assert_eq!(outputs[0]["stream"], "S");
        assert_eq!(outputs[0]["event"]["v"], 42);
    }

    #[test]
    fn test_process_event_filtered() {
        let mut engine = WasmEngine::new();
        engine.load("event T:\n    x: int\n\nstream S = T .where(x > 10) .emit(v: x)");
        let result = engine.process_event(r#"{"event_type":"T","x":5}"#);
        let json: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(json["ok"], true);
        assert!(json["outputs"].as_array().unwrap().is_empty());
    }

    #[test]
    fn test_process_batch() {
        let mut engine = WasmEngine::new();
        engine.load("event T:\n    x: int\n\nstream S = T .where(x > 10) .emit(v: x)");
        let result = engine.process_batch(
            r#"[{"event_type":"T","x":5},{"event_type":"T","x":42},{"event_type":"T","x":100}]"#,
        );
        let json: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(json["ok"], true);
        assert_eq!(json["outputs"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn test_get_topology() {
        let mut engine = WasmEngine::new();
        engine.load("event T:\n    x: int\n\nstream S = T .where(x > 10) .emit(v: x)");
        let topo = engine.get_topology();
        let json: serde_json::Value = serde_json::from_str(&topo).unwrap();
        assert!(!json["nodes"].as_array().unwrap().is_empty());
        assert!(!json["edges"].as_array().unwrap().is_empty());
    }

    #[test]
    fn test_validate() {
        let engine = WasmEngine::new();
        let valid = engine.validate("event T:\n    x: int");
        let json: serde_json::Value = serde_json::from_str(&valid).unwrap();
        assert_eq!(json["ok"], true);

        let invalid = engine.validate("not valid {{");
        let json: serde_json::Value = serde_json::from_str(&invalid).unwrap();
        assert_eq!(json["ok"], false);
    }
}