vertigo-cli 0.13.2

Reactive Real-DOM library with SSR for Rust - packaging/serving 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
use parking_lot::RwLock;
use std::{
    collections::HashMap,
    sync::{Arc, OnceLock},
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};
use tokio::sync::mpsc::{error::TryRecvError, unbounded_channel};
use vertigo::{
    JsJson, JsJsonSerialize,
    dev::{
        command::{CommandForBrowser, ConsoleLogLevel, browser_response},
        command_wire::decode_dom_commands,
    },
};
use wasmtime::{Engine, InstancePre, Module};

use crate::{
    commons::{ErrorCode, spawn::SpawnOwner},
    serve::html::FetchCache,
};

use super::{
    html::HtmlResponse,
    mount_path::MountConfig,
    request_state::RequestState,
    response_state::ResponseState,
    timings::SsrProbe,
    wasm::{HostState, Message, WasmInstance, build_linker},
};

#[cfg(feature = "ssr-timings")]
use super::timings::SsrTimings;

pub fn get_now() -> Duration {
    let start = SystemTime::now();
    match start.duration_since(UNIX_EPOCH) {
        Ok(duration) => duration,
        Err(err) => {
            log::error!("Time went backwards: {err}");
            Duration::from_secs(0)
        }
    }
}

pub type ServerStateMap = HashMap<String, Arc<ServerState>>;

static STATE: OnceLock<Arc<RwLock<ServerStateMap>>> = OnceLock::new();

#[derive(Clone)]
pub struct ServerState {
    engine: Engine,
    /// The module with its imports already resolved by name, once, at startup. Holds the
    /// [`Module`] internally, so there is no separate field for it.
    instance_pre: InstancePre<HostState>,
    /// What `Module::from_binary` cost at startup.
    module_compile: Duration,
    pub mount_config: MountConfig,
    pub port_watch: Option<u16>,
}

impl ServerState {
    pub fn init(mount_config: &MountConfig) -> Result<(), ErrorCode> {
        Self::init_with_watch(mount_config, None)
    }

    pub fn init_with_watch(
        mount_config: &MountConfig,
        port_watch: Option<u16>,
    ) -> Result<(), ErrorCode> {
        let engine = Engine::default();

        let (module, module_compile) = build_module_wasm(&engine, mount_config)?;

        // Import resolution happens here, once - a missing or mistyped import is a startup
        // failure naming the offending import, not a per-request one.
        let instance_pre = build_linker(&engine)?
            .instantiate_pre(&module)
            .map_err(|err| {
                log::error!("WASM import resolution failed: {err:?}");
                ErrorCode::ServeWasmInstanceFailed
            })?;

        let mutex = STATE.get_or_init(|| Arc::new(RwLock::new(ServerStateMap::new())));

        let mut guard = mutex.write();
        guard.insert(
            mount_config.mount_point().to_string(),
            Arc::new(Self {
                engine,
                instance_pre,
                module_compile,
                mount_config: mount_config.clone(),
                port_watch,
            }),
        );

        Ok(())
    }

    /// How long compiling this mount point's wasm module took, at startup.
    pub fn module_compile_time(&self) -> Duration {
        self.module_compile
    }

    pub fn global(mount_point: &str) -> Arc<ServerState> {
        let mutex = STATE.get_or_init(|| Arc::new(RwLock::new(ServerStateMap::new())));

        let guard = mutex.read();

        if let Some(state) = guard.get(mount_point) {
            return state.clone();
        }

        unreachable!();
    }

    pub async fn request(&self, url: &str) -> ResponseState {
        self.request_inner(url, None, &SsrProbe::new()).await
    }

    /// [`ServerState::request`] for a browser request that carried cookies.
    ///
    /// `cookie` is the request's `Cookie` header. It goes out with every SSR fetch to the
    /// server's own origin (a relative URL, resolved against `ssr_fetch_base`), as the browser
    /// sends it with a same-origin `fetch` - so an API behind a session answers for the
    /// logged-in user and the page renders as it will look after hydration. Fetches to other
    /// origins never get it.
    ///
    /// The app itself still can't read cookies during SSR (`get_driver().cookie_get()` returns
    /// nothing): the header doesn't say which cookies are `HttpOnly`, and those are hidden
    /// from the browser's `document.cookie`, so exposing them would render differently than
    /// the browser can.
    pub async fn request_with_cookie(&self, url: &str, cookie: Option<&str>) -> ResponseState {
        self.request_inner(url, cookie, &SsrProbe::new()).await
    }

    /// [`ServerState::request`], with the per-phase breakdown of how the render was spent.
    #[cfg(feature = "ssr-timings")]
    pub async fn request_timed(&self, url: &str) -> (ResponseState, SsrTimings) {
        let probe = SsrProbe::new();
        let mark = probe.start();

        let response = self.request_inner(url, None, &probe).await;

        let timings = probe.finish(mark, response.body.len());
        (response, timings)
    }

    async fn request_inner(
        &self,
        url: &str,
        cookie: Option<&str>,
        probe: &SsrProbe,
    ) -> ResponseState {
        let (sender, mut receiver) = unbounded_channel::<Message>();

        let request = RequestState {
            url: url.to_string(),
            env: self.mount_config.env.clone(),
        };

        let fetch = FetchCache::new();

        let instantiate_mark = probe.start();
        let handle_command = Arc::new({
            let sender = sender.clone();
            let probe = probe.clone();

            move |request: RequestState, command| match command {
                CommandForBrowser::FetchCacheGet => {
                    browser_response::FetchCacheGet { data: None }.to_json()
                }
                CommandForBrowser::FetchExec { request, callback } => {
                    sender
                        .send(Message::FetchRequest { callback, request })
                        .inspect_err(|err| log::error!("Error sending FetchRequest: {err}"))
                        .unwrap_or_default();

                    JsJson::Null
                }
                CommandForBrowser::SetStatus { status } => {
                    sender
                        .send(Message::SetStatus(status))
                        .inspect_err(|err| log::error!("Error sending FetchRequest: {err}"))
                        .unwrap_or_default();

                    JsJson::Null
                }
                CommandForBrowser::IsBrowser => {
                    let response = browser_response::IsBrowser { value: false };

                    response.to_json()
                }
                CommandForBrowser::GetDateNow => {
                    let time = get_now().as_millis();

                    let response = browser_response::GetDateNow { value: time as u64 };

                    response.to_json()
                }
                CommandForBrowser::WebsocketRegister {
                    host: _,
                    callback: _,
                } => JsJson::Null,
                CommandForBrowser::WebsocketUnregister { callback: _ } => JsJson::Null,
                CommandForBrowser::WebsocketSendMessage {
                    callback: _,
                    message: _,
                } => JsJson::Null,
                CommandForBrowser::TimerSet {
                    callback,
                    duration,
                    kind: _,
                } => {
                    if duration == 0 {
                        sender
                            .send(Message::SetTimeoutZero { callback })
                            .inspect_err(|err| log::error!("Error sending SetTimeoutZero: {err}"))
                            .unwrap_or_default();
                    }

                    JsJson::Null
                }
                CommandForBrowser::TimerClear { callback: _ } => JsJson::Null,
                CommandForBrowser::LocationCallback {
                    target: _,
                    mode: _,
                    callback: _,
                } => JsJson::Null,
                CommandForBrowser::LocationSet {
                    target: _,
                    mode: _,
                    value: _,
                } => JsJson::Null,
                CommandForBrowser::LocationGet { target: _ } => {
                    let url = request.url.clone();
                    browser_response::LocationGet { value: url }.to_json()
                }
                CommandForBrowser::CookieGet { name: _ } => {
                    browser_response::CookieGet { value: "".into() }.to_json()
                }
                CommandForBrowser::CookieSet {
                    name: _,
                    value: _,
                    expires_in: _,
                } => JsJson::Null,
                CommandForBrowser::CookieJsonGet { name: _ } => browser_response::CookieJsonGet {
                    value: JsJson::Null,
                }
                .to_json(),
                CommandForBrowser::CookieJsonSet {
                    name: _,
                    value: _,
                    expires_in: _,
                } => JsJson::Null,
                CommandForBrowser::GetEnv { name } => {
                    let env_value = request.env(name);

                    browser_response::GetEnv { value: env_value }.to_json()
                }
                CommandForBrowser::Log {
                    kind,
                    message,
                    arg2: _,
                    arg3: _,
                    arg4: _,
                } => {
                    if kind == ConsoleLogLevel::Error {
                        log::warn!("{message}");
                    } else {
                        log::info!("{message}");
                    }

                    JsJson::Null
                }
                CommandForBrowser::TimezoneOffset => {
                    browser_response::TimezoneOffset { value: 0 }.to_json()
                }
                CommandForBrowser::HistoryBack => JsJson::Null,
                CommandForBrowser::GetRandom { min, max: _ } => {
                    browser_response::GetRandom { value: min }.to_json()
                }
                CommandForBrowser::JsApiCall { commands: _ } => JsJson::Null,
                CommandForBrowser::DomBulkUpdate { commands } => {
                    // Host work, but reached from inside a wasm call - so this is
                    // phase-3 time measured within a phase-2 region, and `SsrTimings`
                    // subtracts it back out. See the module docs in `timings.rs`.
                    let blob_bytes = commands.len();
                    let decode_mark = probe.start();

                    match decode_dom_commands(&commands) {
                        Ok(list) => {
                            // Before the send, so the channel push is not counted as
                            // decoding.
                            probe.decoded(decode_mark, blob_bytes, list.len());

                            sender
                                .send(Message::DomUpdate(list))
                                .inspect_err(|err| log::error!("Error sending DomUpdate: {err}"))
                                .unwrap_or_default();
                        }
                        Err(err) => log::error!("Error decoding DomBulkUpdate: {err}"),
                    }

                    JsJson::Null
                }
            }
        });

        let mut inst = WasmInstance::new(
            &self.engine,
            &self.instance_pre,
            HostState {
                request,
                sender: sender.clone(),
                probe: probe.clone(),
                handle_command,
            },
        );
        probe.instantiate(instantiate_mark);

        // -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //TODO - ultimately, do not call call_vertigo_entry_function if something is returned by handle_url
        // -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        inst.call_vertigo_entry_function();

        if let Some(result) = inst.handle_url(url) {
            return result;
        }

        let spawn_resource = SpawnOwner::new({
            let sender = sender.clone();

            async move {
                tokio::time::sleep(std::time::Duration::from_secs(10)).await;
                let _ = sender.send(Message::TimeoutAndSendResponse);
            }
        });

        let mut html_response = HtmlResponse::new(
            sender.clone(),
            &self.mount_config,
            inst,
            self.mount_config.env.clone(),
            fetch,
            probe.clone(),
            url,
            cookie.map(str::to_string),
        );

        loop {
            let message = receiver.try_recv();

            match message {
                Ok(message) => {
                    if let Some(response) = html_response.process_message(message) {
                        return response;
                    };
                    continue;
                }
                Err(TryRecvError::Empty) => {} // continue this iteration
                Err(TryRecvError::Disconnected) => {
                    break; // send response to browser
                }
            }

            if html_response.awaiting_response() {
                // Parked on an SSR fetch: time the request spent, but not time it spent
                // working. Kept in its own bucket so it cannot be read as either.
                let wait_mark = probe.start();
                let message = receiver.recv().await;
                probe.fetch_wait(wait_mark);

                if let Some(message) = message
                    && let Some(response) = html_response.process_message(message)
                {
                    return response;
                };
            } else {
                break; // send response to browser
            }
        }

        spawn_resource.off();
        html_response.build_response()
    }
}

fn build_module_wasm(
    engine: &Engine,
    mount_path: &MountConfig,
) -> Result<(Module, Duration), ErrorCode> {
    let full_wasm_path = mount_path.get_wasm_fs_path();

    log::info!("Mounting {} -> {full_wasm_path}", mount_path.mount_point());

    let wasm_content = match std::fs::read(&full_wasm_path) {
        Ok(wasm_content) => wasm_content,
        Err(error) => {
            log::error!("Problem reading the path: wasm_path={full_wasm_path}, error={error}");
            return Err(ErrorCode::ServeWasmReadFailed);
        }
    };

    let now = Instant::now();

    let module = match Module::from_binary(engine, &wasm_content) {
        Ok(module) => module,
        Err(err) => {
            log::error!("Wasm compilation error: error={err}");
            return Err(ErrorCode::ServeWasmCompileFailed);
        }
    };

    let elapsed = now.elapsed();
    log::info!("WASM module compiled in {} ms.", elapsed.as_millis());
    Ok((module, elapsed))
}