Skip to main content

vertigo_cli/serve/
server_state.rs

1use parking_lot::RwLock;
2use std::{
3    collections::HashMap,
4    sync::{Arc, OnceLock},
5    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
6};
7use tokio::sync::mpsc::{error::TryRecvError, unbounded_channel};
8use vertigo::{
9    JsJson, JsJsonSerialize,
10    dev::{
11        command::{CommandForBrowser, ConsoleLogLevel, browser_response},
12        command_wire::decode_dom_commands,
13    },
14};
15use wasmtime::{Engine, InstancePre, Module};
16
17use crate::{
18    commons::{ErrorCode, spawn::SpawnOwner},
19    serve::html::FetchCache,
20};
21
22use super::{
23    html::HtmlResponse,
24    mount_path::MountConfig,
25    request_state::RequestState,
26    response_state::ResponseState,
27    timings::SsrProbe,
28    wasm::{HostState, Message, WasmInstance, build_linker},
29};
30
31#[cfg(feature = "ssr-timings")]
32use super::timings::SsrTimings;
33
34pub fn get_now() -> Duration {
35    let start = SystemTime::now();
36    match start.duration_since(UNIX_EPOCH) {
37        Ok(duration) => duration,
38        Err(err) => {
39            log::error!("Time went backwards: {err}");
40            Duration::from_secs(0)
41        }
42    }
43}
44
45pub type ServerStateMap = HashMap<String, Arc<ServerState>>;
46
47static STATE: OnceLock<Arc<RwLock<ServerStateMap>>> = OnceLock::new();
48
49#[derive(Clone)]
50pub struct ServerState {
51    engine: Engine,
52    /// The module with its imports already resolved by name, once, at startup. Holds the
53    /// [`Module`] internally, so there is no separate field for it.
54    instance_pre: InstancePre<HostState>,
55    /// What `Module::from_binary` cost at startup.
56    module_compile: Duration,
57    pub mount_config: MountConfig,
58    pub port_watch: Option<u16>,
59}
60
61impl ServerState {
62    pub fn init(mount_config: &MountConfig) -> Result<(), ErrorCode> {
63        Self::init_with_watch(mount_config, None)
64    }
65
66    pub fn init_with_watch(
67        mount_config: &MountConfig,
68        port_watch: Option<u16>,
69    ) -> Result<(), ErrorCode> {
70        let engine = Engine::default();
71
72        let (module, module_compile) = build_module_wasm(&engine, mount_config)?;
73
74        // Import resolution happens here, once - a missing or mistyped import is a startup
75        // failure naming the offending import, not a per-request one.
76        let instance_pre = build_linker(&engine)?
77            .instantiate_pre(&module)
78            .map_err(|err| {
79                log::error!("WASM import resolution failed: {err:?}");
80                ErrorCode::ServeWasmInstanceFailed
81            })?;
82
83        let mutex = STATE.get_or_init(|| Arc::new(RwLock::new(ServerStateMap::new())));
84
85        let mut guard = mutex.write();
86        guard.insert(
87            mount_config.mount_point().to_string(),
88            Arc::new(Self {
89                engine,
90                instance_pre,
91                module_compile,
92                mount_config: mount_config.clone(),
93                port_watch,
94            }),
95        );
96
97        Ok(())
98    }
99
100    /// How long compiling this mount point's wasm module took, at startup.
101    pub fn module_compile_time(&self) -> Duration {
102        self.module_compile
103    }
104
105    pub fn global(mount_point: &str) -> Arc<ServerState> {
106        let mutex = STATE.get_or_init(|| Arc::new(RwLock::new(ServerStateMap::new())));
107
108        let guard = mutex.read();
109
110        if let Some(state) = guard.get(mount_point) {
111            return state.clone();
112        }
113
114        unreachable!();
115    }
116
117    pub async fn request(&self, url: &str) -> ResponseState {
118        self.request_inner(url, None, &SsrProbe::new()).await
119    }
120
121    /// [`ServerState::request`] for a browser request that carried cookies.
122    ///
123    /// `cookie` is the request's `Cookie` header. It goes out with every SSR fetch to the
124    /// server's own origin (a relative URL, resolved against `ssr_fetch_base`), as the browser
125    /// sends it with a same-origin `fetch` - so an API behind a session answers for the
126    /// logged-in user and the page renders as it will look after hydration. Fetches to other
127    /// origins never get it.
128    ///
129    /// The app itself still can't read cookies during SSR (`get_driver().cookie_get()` returns
130    /// nothing): the header doesn't say which cookies are `HttpOnly`, and those are hidden
131    /// from the browser's `document.cookie`, so exposing them would render differently than
132    /// the browser can.
133    pub async fn request_with_cookie(&self, url: &str, cookie: Option<&str>) -> ResponseState {
134        self.request_inner(url, cookie, &SsrProbe::new()).await
135    }
136
137    /// [`ServerState::request`], with the per-phase breakdown of how the render was spent.
138    #[cfg(feature = "ssr-timings")]
139    pub async fn request_timed(&self, url: &str) -> (ResponseState, SsrTimings) {
140        let probe = SsrProbe::new();
141        let mark = probe.start();
142
143        let response = self.request_inner(url, None, &probe).await;
144
145        let timings = probe.finish(mark, response.body.len());
146        (response, timings)
147    }
148
149    async fn request_inner(
150        &self,
151        url: &str,
152        cookie: Option<&str>,
153        probe: &SsrProbe,
154    ) -> ResponseState {
155        let (sender, mut receiver) = unbounded_channel::<Message>();
156
157        let request = RequestState {
158            url: url.to_string(),
159            env: self.mount_config.env.clone(),
160        };
161
162        let fetch = FetchCache::new();
163
164        let instantiate_mark = probe.start();
165        let handle_command = Arc::new({
166            let sender = sender.clone();
167            let probe = probe.clone();
168
169            move |request: RequestState, command| match command {
170                CommandForBrowser::FetchCacheGet => {
171                    browser_response::FetchCacheGet { data: None }.to_json()
172                }
173                CommandForBrowser::FetchExec { request, callback } => {
174                    sender
175                        .send(Message::FetchRequest { callback, request })
176                        .inspect_err(|err| log::error!("Error sending FetchRequest: {err}"))
177                        .unwrap_or_default();
178
179                    JsJson::Null
180                }
181                CommandForBrowser::SetStatus { status } => {
182                    sender
183                        .send(Message::SetStatus(status))
184                        .inspect_err(|err| log::error!("Error sending FetchRequest: {err}"))
185                        .unwrap_or_default();
186
187                    JsJson::Null
188                }
189                CommandForBrowser::IsBrowser => {
190                    let response = browser_response::IsBrowser { value: false };
191
192                    response.to_json()
193                }
194                CommandForBrowser::GetDateNow => {
195                    let time = get_now().as_millis();
196
197                    let response = browser_response::GetDateNow { value: time as u64 };
198
199                    response.to_json()
200                }
201                CommandForBrowser::WebsocketRegister {
202                    host: _,
203                    callback: _,
204                } => JsJson::Null,
205                CommandForBrowser::WebsocketUnregister { callback: _ } => JsJson::Null,
206                CommandForBrowser::WebsocketSendMessage {
207                    callback: _,
208                    message: _,
209                } => JsJson::Null,
210                CommandForBrowser::TimerSet {
211                    callback,
212                    duration,
213                    kind: _,
214                } => {
215                    if duration == 0 {
216                        sender
217                            .send(Message::SetTimeoutZero { callback })
218                            .inspect_err(|err| log::error!("Error sending SetTimeoutZero: {err}"))
219                            .unwrap_or_default();
220                    }
221
222                    JsJson::Null
223                }
224                CommandForBrowser::TimerClear { callback: _ } => JsJson::Null,
225                CommandForBrowser::LocationCallback {
226                    target: _,
227                    mode: _,
228                    callback: _,
229                } => JsJson::Null,
230                CommandForBrowser::LocationSet {
231                    target: _,
232                    mode: _,
233                    value: _,
234                } => JsJson::Null,
235                CommandForBrowser::LocationGet { target: _ } => {
236                    let url = request.url.clone();
237                    browser_response::LocationGet { value: url }.to_json()
238                }
239                CommandForBrowser::CookieGet { name: _ } => {
240                    browser_response::CookieGet { value: "".into() }.to_json()
241                }
242                CommandForBrowser::CookieSet {
243                    name: _,
244                    value: _,
245                    expires_in: _,
246                } => JsJson::Null,
247                CommandForBrowser::CookieJsonGet { name: _ } => browser_response::CookieJsonGet {
248                    value: JsJson::Null,
249                }
250                .to_json(),
251                CommandForBrowser::CookieJsonSet {
252                    name: _,
253                    value: _,
254                    expires_in: _,
255                } => JsJson::Null,
256                CommandForBrowser::GetEnv { name } => {
257                    let env_value = request.env(name);
258
259                    browser_response::GetEnv { value: env_value }.to_json()
260                }
261                CommandForBrowser::Log {
262                    kind,
263                    message,
264                    arg2: _,
265                    arg3: _,
266                    arg4: _,
267                } => {
268                    if kind == ConsoleLogLevel::Error {
269                        log::warn!("{message}");
270                    } else {
271                        log::info!("{message}");
272                    }
273
274                    JsJson::Null
275                }
276                CommandForBrowser::TimezoneOffset => {
277                    browser_response::TimezoneOffset { value: 0 }.to_json()
278                }
279                CommandForBrowser::HistoryBack => JsJson::Null,
280                CommandForBrowser::GetRandom { min, max: _ } => {
281                    browser_response::GetRandom { value: min }.to_json()
282                }
283                CommandForBrowser::JsApiCall { commands: _ } => JsJson::Null,
284                CommandForBrowser::DomBulkUpdate { commands } => {
285                    // Host work, but reached from inside a wasm call - so this is
286                    // phase-3 time measured within a phase-2 region, and `SsrTimings`
287                    // subtracts it back out. See the module docs in `timings.rs`.
288                    let blob_bytes = commands.len();
289                    let decode_mark = probe.start();
290
291                    match decode_dom_commands(&commands) {
292                        Ok(list) => {
293                            // Before the send, so the channel push is not counted as
294                            // decoding.
295                            probe.decoded(decode_mark, blob_bytes, list.len());
296
297                            sender
298                                .send(Message::DomUpdate(list))
299                                .inspect_err(|err| log::error!("Error sending DomUpdate: {err}"))
300                                .unwrap_or_default();
301                        }
302                        Err(err) => log::error!("Error decoding DomBulkUpdate: {err}"),
303                    }
304
305                    JsJson::Null
306                }
307            }
308        });
309
310        let mut inst = WasmInstance::new(
311            &self.engine,
312            &self.instance_pre,
313            HostState {
314                request,
315                sender: sender.clone(),
316                probe: probe.clone(),
317                handle_command,
318            },
319        );
320        probe.instantiate(instantiate_mark);
321
322        // -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
323        //TODO - ultimately, do not call call_vertigo_entry_function if something is returned by handle_url
324        // -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
325
326        inst.call_vertigo_entry_function();
327
328        if let Some(result) = inst.handle_url(url) {
329            return result;
330        }
331
332        let spawn_resource = SpawnOwner::new({
333            let sender = sender.clone();
334
335            async move {
336                tokio::time::sleep(std::time::Duration::from_secs(10)).await;
337                let _ = sender.send(Message::TimeoutAndSendResponse);
338            }
339        });
340
341        let mut html_response = HtmlResponse::new(
342            sender.clone(),
343            &self.mount_config,
344            inst,
345            self.mount_config.env.clone(),
346            fetch,
347            probe.clone(),
348            url,
349            cookie.map(str::to_string),
350        );
351
352        loop {
353            let message = receiver.try_recv();
354
355            match message {
356                Ok(message) => {
357                    if let Some(response) = html_response.process_message(message) {
358                        return response;
359                    };
360                    continue;
361                }
362                Err(TryRecvError::Empty) => {} // continue this iteration
363                Err(TryRecvError::Disconnected) => {
364                    break; // send response to browser
365                }
366            }
367
368            if html_response.awaiting_response() {
369                // Parked on an SSR fetch: time the request spent, but not time it spent
370                // working. Kept in its own bucket so it cannot be read as either.
371                let wait_mark = probe.start();
372                let message = receiver.recv().await;
373                probe.fetch_wait(wait_mark);
374
375                if let Some(message) = message
376                    && let Some(response) = html_response.process_message(message)
377                {
378                    return response;
379                };
380            } else {
381                break; // send response to browser
382            }
383        }
384
385        spawn_resource.off();
386        html_response.build_response()
387    }
388}
389
390fn build_module_wasm(
391    engine: &Engine,
392    mount_path: &MountConfig,
393) -> Result<(Module, Duration), ErrorCode> {
394    let full_wasm_path = mount_path.get_wasm_fs_path();
395
396    log::info!("Mounting {} -> {full_wasm_path}", mount_path.mount_point());
397
398    let wasm_content = match std::fs::read(&full_wasm_path) {
399        Ok(wasm_content) => wasm_content,
400        Err(error) => {
401            log::error!("Problem reading the path: wasm_path={full_wasm_path}, error={error}");
402            return Err(ErrorCode::ServeWasmReadFailed);
403        }
404    };
405
406    let now = Instant::now();
407
408    let module = match Module::from_binary(engine, &wasm_content) {
409        Ok(module) => module,
410        Err(err) => {
411            log::error!("Wasm compilation error: error={err}");
412            return Err(ErrorCode::ServeWasmCompileFailed);
413        }
414    };
415
416    let elapsed = now.elapsed();
417    log::info!("WASM module compiled in {} ms.", elapsed.as_millis());
418    Ok((module, elapsed))
419}