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