vertigo-cli 0.2.0-alpha

Reactive Real-DOM library for Rust - packaging tool
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
use std::sync::Arc;
use std::collections::HashMap;
use std::hash::Hash;
use wasmtime::{
    Caller,
    Engine,
    Func,
    Instance,
    Module,
    Store,
};
use tokio::sync::mpsc::UnboundedSender;

use crate::serve::{
    html::RequestBody,
    request_state::RequestState,
    js_value::{JsValue, JsJson, from_json},
    wasm::data_context::DataContext,
};

use super::get_now;
use super::js_value_match::{Match};

#[derive(Debug)]
pub enum Message {
    TimeoutAndSendResponse,
    DomUpdate(JsJson),
    Panic(Option<String>),
    SetTimeoutZero {
        callback_id: u64
    },
    FetchRequest {
        callback_id: u64,
        request: FetchRequest,
    },
    FetchResponse {
        request: Arc<FetchRequest>,
        response: FetchResponse
    },
}

#[derive(Debug, Eq)]
pub struct FetchRequest {
    pub method: String,
    pub url: String,
    pub headers: HashMap<String, String>,
    pub body: Option<RequestBody>,
}

impl PartialEq for  FetchRequest {
    fn eq(&self, other: &Self) -> bool {
        self.method == other.method &&
        self.url == other.url &&
        self.headers == other.headers  &&
        self.body == other.body
    }
}
impl Hash for FetchRequest {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.method.hash(state);
        self.url.hash(state);
        for (key, value) in self.headers.iter() {
            key.hash(state);
            value.hash(state);
        }
        self.body.hash(state);
    }
}

fn convert_value_to_body(body: JsValue) -> Result<Option<RequestBody>, String> {
    match body {
        JsValue::Json(json) => Ok(Some(RequestBody::Json(json))),
        JsValue::String(text) => Ok(Some(RequestBody::Text(text))),
        JsValue::Vec(buffer) => Ok(Some(RequestBody::Binary(buffer))),
        JsValue::Undefined => Ok(None),
        other => {
            let typename = other.typename();
            let message = format!("expected JsValue::Json or JsValue::Text or JsValue::Binary, received JsValue::{typename}");
            Err(message)
        }
    }
}

fn convert_body_to_value(body: RequestBody) -> JsValue {
    match body {
        RequestBody::Json(json) => JsValue::Json(json),
        RequestBody::Text(text) => JsValue::String(text),
        RequestBody::Binary(buffer) => JsValue::Vec(buffer),
    }
}

#[derive(Clone, Debug)]
pub struct FetchResponse {
    pub success: bool,
    pub status: u32,
    pub body: RequestBody,
}

fn match_is_browser(arg: &JsValue) -> Result<(), ()> {
    let matcher = Match::new(arg)?;
    let matcher = matcher.test_list(&["api"])?;
    let _ = matcher.test_list(&["call", "isBrowser"])?;

    Ok(())
}

fn match_cookie_command(arg: &JsValue) -> Result<(), ()> {
    let matcher = Match::new(arg)?;
    let matcher = matcher.test_list(&["api"])?;
    let _ = matcher.test_list(&["get", "cookie"])?;

    Ok(())
}

fn match_history_router(arg: &JsValue) -> Result<(), ()> {
    let matcher = Match::new(arg)?;
    let matcher = matcher.test_list(&["api"])?;
    let matcher = matcher.test_list(&["get", "historyLocation"])?;
    let matcher = matcher.test_list(&["call", "get"])?;
    matcher.end()?;

    Ok(())
}

fn match_history_router_callback(arg: &JsValue) -> Result<(), ()> {
    let matcher = Match::new(arg)?;
    let matcher = matcher.test_list(&["api"])?;
    let matcher = matcher.test_list(&["get", "historyLocation"])?;
    let (matcher, _) = matcher.test_list_with_fn(|matcher: Match| -> Result<u64, ()> {
        let matcher = matcher.str("call")?;
        let matcher = matcher.str("add")?;
        let (matcher, callback_id) = matcher.u64()?;
        matcher.end()?;

        Ok(callback_id)
    })?;
    matcher.end()?;

    Ok(())
}

fn match_dom_bulk_update(arg: &JsValue) -> Result<JsJson, ()> {
    let matcher = Match::new(arg)?;
    let matcher = matcher.test_list(&["api"])?;
    let matcher = matcher.test_list(&["get", "dom"])?;
    let (matcher, data) = matcher.test_list_with_fn(|matcher: Match| -> Result<JsJson, ()> {
        let matcher = matcher.str("call")?;
        let matcher = matcher.str("dom_bulk_update")?;
        let (matcher, data) = matcher.json()?;
        matcher.end()?;

        Ok(data)
    })?;
    matcher.end()?;

    Ok(data)
}

fn match_log(arg: &JsValue) -> Result<(String, String), ()> {
    let matcher = Match::new(arg)?;

    let matcher = matcher.test_list(&["root", "window"])?;
    let matcher = matcher.test_list(&["get", "console"])?;
    let (matcher, (log_type, log_message)) = matcher.test_list_with_fn(|matcher: Match| -> Result<(String, String), ()> {

        let matcher = matcher.str("call")?;
        let (matcher, log_type) = matcher.string()?;
        let (matcher, log_message) = matcher.string()?;
        let (matcher, _) = matcher.string()?;
        let (matcher, _) = matcher.string()?;
        let (matcher, _) = matcher.string()?;
        matcher.end()?;

        Ok((log_type, log_message))
    })?;

    matcher.end()?;

    Ok((log_type, log_message))
}

fn match_date_now(arg: &JsValue) -> Result<JsValue, ()> {
    let matcher = Match::new(arg)?;

    let matcher = matcher.test_list(&["root", "window"])?;
    let matcher = matcher.test_list(&["get", "Date"])?;
    let matcher = matcher.test_list(&["call", "now"])?;
    matcher.end()?;

    let time = get_now().as_millis();
    Ok(JsValue::I64(time as i64))
}

fn match_websocket(arg: &JsValue) -> Result<(), ()> {
    let matcher = Match::new(arg)?;

    let matcher = matcher.test_list(&["api"])?;
    let _ = matcher.test_list(&["get", "websocket"])?;

    Ok(())
}

enum CallWebsocketResult {
    TimeoutSet {
        time: u32,
        callback_id: u64,
    },
    NoResult,
}

fn match_interval(arg: &JsValue) -> Result<CallWebsocketResult, ()> {
    let matcher = Match::new(arg)?;

    let matcher = matcher.test_list(&["api"])?;
    let matcher = matcher.test_list(&["get", "interval"])?;

    let (matcher, result) = matcher.test_list_with_fn(|matcher| {

        let matcher = matcher.str("call")?;
        if let Ok(matcher) = matcher.str("timeout_set") {
            let (matcher, time) = matcher.u32()?;
            let (_, callback_id) = matcher.u64()?;

            return Ok(CallWebsocketResult::TimeoutSet { time, callback_id });
        }

        Ok(CallWebsocketResult::NoResult)
    })?;

    matcher.end()?;

    Ok(result)
}

fn match_fetch(arg: &JsValue) -> Result<(u64, FetchRequest), ()> {
    let matcher = Match::new(arg)?;

    let matcher = matcher.test_list(&["api"])?;
    let matcher = matcher.test_list(&["get", "fetch"])?;

    let (matcher, result) = matcher.test_list_with_fn(|matcher| {

        let matcher = matcher.str("call")?;
        let matcher = matcher.str("fetch_send_request")?;
        let (matcher, callback_id) = matcher.u64()?;
        let (matcher, method) = matcher.string()?;
        let (matcher, url) = matcher.string()?;
        let (matcher, headers) = matcher.json()?;
        let (matcher, body) = matcher.get_any()?;
        matcher.end()?;

        let headers = from_json::<HashMap<String, String>>(headers).map_err(|error| {
            log::error!("error decode headers: {error}");
        })?;

        let body = convert_value_to_body(body).map_err(|error| {
            log::error!("error decode body: {error}");
        })?;

        Ok((callback_id, FetchRequest {
            method,
            url,
            headers,
            body,
        }))
    })?;

    matcher.end()?;

    Ok(result)
}

pub struct WasmInstance {
    instance: Instance,
    store: Store<RequestState>,
}

impl WasmInstance {
    pub fn new(sender: UnboundedSender<Message>, engine: &Engine, module: &Module, request: RequestState) -> Self {
        let url = request.url.clone();
        let mut store = Store::new(engine, request);

        let import_panic_message = Func::wrap(&mut store, {
            let sender = sender.clone();

            move |caller: Caller<'_, RequestState>, ptr: u32, offset: u32| {
                let mut data_context = DataContext::from_caller(caller);

                let message = data_context.get_string_from(ptr, offset);
                log::error!("panic: {message:?}");

                sender.send(Message::Panic(message)).unwrap();
            }
        });

        let import_dom_access = {
            Func::wrap(
                &mut store,
                move |caller: Caller<'_, RequestState>, ptr: u32, offset: u32| -> u32 {
                    let mut data_context = DataContext::from_caller(caller);

                    let value = data_context.get_value(ptr, offset);

                    // Ignore cookie operations
                    if let Ok(()) = match_cookie_command(&value) {
                        return 0;
                    }

                    //get history router location
                    if let Ok(()) = match_history_router(&value) {
                        let result = JsValue::str(url.clone());
                        return data_context.save_value(result);
                    }

                    //adding callback for hashrouter
                    if match_history_router_callback(&value).is_ok() {
                        return 0;
                    }

                    if let Ok(data) = match_dom_bulk_update(&value) {
                        sender.send(Message::DomUpdate(data)).unwrap();
                        return 0;
                    }

                    if let Ok((log_type, log_message)) = match_log(&value) {
                        if log_type == "error" {
                            log::warn!("{log_message}");
                        } else {
                            log::info!("{log_message}");
                        }
                        return 0;
                    }

                    if let Ok(current_time) = match_date_now(&value) {
                        return data_context.save_value(current_time);
                    }

                    if let Ok(result) = match_interval(&value) {
                        match result {
                            CallWebsocketResult::TimeoutSet { time, callback_id } => {
                                if time == 0 {
                                    sender.send(Message::SetTimeoutZero { callback_id }).unwrap();
                                }

                                let result = JsValue::I32(0); // fake timerId
                                return data_context.save_value(result);
                            },
                            CallWebsocketResult::NoResult => {
                                return 0;
                            }
                        }
                    }

                    if let Ok(()) = match_websocket(&value) {
                        return 0;
                    }

                    if let Ok((callback_id, request)) = match_fetch(&value) {
                        sender.send(Message::FetchRequest { callback_id, request }).unwrap();
                        return 0;
                    }

                    if let Ok(()) = match_is_browser(&value) {
                        let result = JsValue::bool(false);
                        return data_context.save_value(result);
                    }

                    log::error!("unsupported message: {value:#?}");
                    0
                }
            )
        };

        let imports = [
            import_panic_message.into(),
            import_dom_access.into()
        ];
        let instance = Instance::new(&mut store, module, &imports).unwrap();

        WasmInstance {
            instance,
            store
        }
    }

    fn call_function<
        Params: wasmtime::WasmParams,
        Results: wasmtime::WasmResults
    >(&mut self, name: &'static str, params: Params) -> Result<Results, String> {
        let start_application = {
            self.instance.get_typed_func::<Params, Results>(&mut self.store, name).unwrap()
        };

        start_application.call(&mut self.store, params).map_err(|error| {
            format!("{error}")
        })
    }

    pub fn call_start_application(&mut self) {
        self.call_function::<(), ()>("start_application", ()).unwrap();
    }

    pub fn wasm_callback(&mut self, callback_id: u64, params: JsValue) -> JsValue {
        let mut data_context = DataContext::from_store(&mut self.store, self.instance);
        let params_ptr = data_context.save_value(params);

        let result = self.call_function::<(u64, u32), u64>("wasm_callback", (callback_id, params_ptr)).unwrap();

        if result == 0 {
            JsValue::Undefined
        } else {
            //TODO - to implement
            todo!()
        }
    }

    pub fn send_fetch_response(&mut self, callback_id: u64, response: FetchResponse) {
        let params = JsValue::List(vec!(
            JsValue::bool(response.success),
            JsValue::U32(response.status),
            convert_body_to_value(response.body),
        ));

        let result = self.wasm_callback(callback_id, params);
        assert_eq!(result, JsValue::Undefined);
    }
}