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