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::{UnboundedSender, 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, forwarded_cookies, 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. With any cookies named with
144    /// [`MountConfigBuilder::ssr_fetch_cookie`](super::MountConfigBuilder::ssr_fetch_cookie), only
145    /// those go.
146    ///
147    /// The app itself can read during SSR only the cookies named with
148    /// [`MountConfigBuilder::ssr_cookie`](super::MountConfigBuilder::ssr_cookie)
149    /// (`get_driver().cookie_get()` returns nothing for the others): the header doesn't say which
150    /// cookies are `HttpOnly`, and those are hidden from the browser's `document.cookie`, so
151    /// exposing them would render differently than the browser can.
152    pub async fn request_with_cookie(&self, url: &str, cookie: Option<&str>) -> ResponseState {
153        let headers = RequestHeaders {
154            cookie,
155            ..RequestHeaders::default()
156        };
157        self.request_with_headers(url, headers).await
158    }
159
160    /// [`ServerState::request`] for a browser request, with the headers that change the page:
161    /// the cookies, as in [`ServerState::request_with_cookie`], and the languages the browser
162    /// prefers, which `get_driver().languages()` tells the app.
163    pub async fn request_with_headers(
164        &self,
165        url: &str,
166        headers: RequestHeaders<'_>,
167    ) -> ResponseState {
168        self.request_inner(url, headers, &SsrProbe::new()).await
169    }
170
171    /// [`ServerState::request`], with the per-phase breakdown of how the render was spent.
172    #[cfg(feature = "ssr-timings")]
173    pub async fn request_timed(&self, url: &str) -> (ResponseState, SsrTimings) {
174        let probe = SsrProbe::new();
175        let mark = probe.start();
176
177        let response = self
178            .request_inner(url, RequestHeaders::default(), &probe)
179            .await;
180
181        let timings = probe.finish(mark, response.body.len());
182        (response, timings)
183    }
184
185    async fn request_inner(
186        &self,
187        url: &str,
188        headers: RequestHeaders<'_>,
189        probe: &SsrProbe,
190    ) -> ResponseState {
191        let (sender, mut receiver) = unbounded_channel::<Message>();
192
193        let request = RequestState {
194            url: url.to_string(),
195            env: self.mount_config.env.clone(),
196            languages: Arc::new(
197                headers
198                    .accept_language
199                    .map(accept_languages)
200                    .unwrap_or_default(),
201            ),
202            cookies: Arc::new(
203                headers
204                    .cookie
205                    .map(|cookie| readable_cookies(cookie, &self.mount_config.ssr_cookies))
206                    .unwrap_or_default(),
207            ),
208        };
209
210        let fetch = FetchCache::new();
211
212        let instantiate_mark = probe.start();
213        let handle_command = Arc::new({
214            let sender = sender.clone();
215            let probe = probe.clone();
216            let ssr_cookies = self.mount_config.ssr_cookies.clone();
217
218            move |request: RequestState, command| match command {
219                CommandForBrowser::FetchCacheGet => {
220                    browser_response::FetchCacheGet { data: None }.to_json()
221                }
222                CommandForBrowser::FetchExec { request, callback } => {
223                    sender
224                        .send(Message::FetchRequest { callback, request })
225                        .inspect_err(|err| log::error!("Error sending FetchRequest: {err}"))
226                        .unwrap_or_default();
227
228                    JsJson::Null
229                }
230                CommandForBrowser::SetStatus { status } => {
231                    sender
232                        .send(Message::SetStatus(status))
233                        .inspect_err(|err| log::error!("Error sending FetchRequest: {err}"))
234                        .unwrap_or_default();
235
236                    JsJson::Null
237                }
238                CommandForBrowser::IsBrowser => {
239                    let response = browser_response::IsBrowser { value: false };
240
241                    response.to_json()
242                }
243                CommandForBrowser::GetDateNow => {
244                    let time = get_now().as_millis();
245
246                    let response = browser_response::GetDateNow { value: time as u64 };
247
248                    response.to_json()
249                }
250                CommandForBrowser::WebsocketRegister {
251                    host: _,
252                    callback: _,
253                    connect_timeout: _,
254                } => JsJson::Null,
255                CommandForBrowser::WebsocketUnregister { callback: _ } => JsJson::Null,
256                CommandForBrowser::WebsocketSendMessage {
257                    callback: _,
258                    message: _,
259                } => JsJson::Null,
260                CommandForBrowser::TimerSet {
261                    callback,
262                    duration,
263                    kind: _,
264                } => {
265                    if duration == 0 {
266                        sender
267                            .send(Message::SetTimeoutZero { callback })
268                            .inspect_err(|err| log::error!("Error sending SetTimeoutZero: {err}"))
269                            .unwrap_or_default();
270                    }
271
272                    JsJson::Null
273                }
274                CommandForBrowser::TimerClear { callback: _ } => JsJson::Null,
275                // No frames are drawn on the server.
276                CommandForBrowser::AnimationFrameStart | CommandForBrowser::AnimationFrameStop => {
277                    JsJson::Null
278                }
279                CommandForBrowser::LocationCallback {
280                    target: _,
281                    mode: _,
282                    callback: _,
283                } => JsJson::Null,
284                CommandForBrowser::LocationSet {
285                    target: _,
286                    mode: _,
287                    value: _,
288                } => JsJson::Null,
289                CommandForBrowser::LocationGet { target: _ } => {
290                    let url = request.url.clone();
291                    browser_response::LocationGet { value: url }.to_json()
292                }
293                CommandForBrowser::CookieGet { name } => {
294                    note_cookie_read(&sender, &ssr_cookies, &name);
295
296                    browser_response::CookieGet {
297                        value: request.cookie(&name),
298                    }
299                    .to_json()
300                }
301                CommandForBrowser::CookieSet {
302                    name: _,
303                    value: _,
304                    expires_in: _,
305                } => JsJson::Null,
306                CommandForBrowser::CookieJsonGet { name } => {
307                    note_cookie_read(&sender, &ssr_cookies, &name);
308
309                    browser_response::CookieJsonGet {
310                        value: request.cookie_json(&name),
311                    }
312                    .to_json()
313                }
314                CommandForBrowser::CookieJsonSet {
315                    name: _,
316                    value: _,
317                    expires_in: _,
318                } => JsJson::Null,
319                CommandForBrowser::GetEnv { name } => {
320                    let env_value = request.env(name);
321
322                    browser_response::GetEnv { value: env_value }.to_json()
323                }
324                CommandForBrowser::Log {
325                    kind,
326                    message,
327                    arg2: _,
328                    arg3: _,
329                    arg4: _,
330                } => {
331                    if kind == ConsoleLogLevel::Error {
332                        log::warn!("{message}");
333                    } else {
334                        log::info!("{message}");
335                    }
336
337                    JsJson::Null
338                }
339                CommandForBrowser::TimezoneOffset => {
340                    browser_response::TimezoneOffset { value: 0 }.to_json()
341                }
342                CommandForBrowser::Languages => {
343                    sender
344                        .send(Message::LanguagesRead)
345                        .inspect_err(|err| log::error!("Error sending LanguagesRead: {err}"))
346                        .unwrap_or_default();
347
348                    browser_response::Languages {
349                        value: request.languages.to_vec(),
350                    }
351                    .to_json()
352                }
353                CommandForBrowser::HistoryBack => JsJson::Null,
354                // Images are loaded by the browser, for its canvases.
355                CommandForBrowser::ImagePreload { url: _ } => JsJson::Null,
356                CommandForBrowser::GetRandom { min, max: _ } => {
357                    browser_response::GetRandom { value: min }.to_json()
358                }
359                CommandForBrowser::JsApiCall { commands: _ } => JsJson::Null,
360                CommandForBrowser::DomBulkUpdate { commands } => {
361                    // Host work, but reached from inside a wasm call - so this is
362                    // phase-3 time measured within a phase-2 region, and `SsrTimings`
363                    // subtracts it back out. See the module docs in `timings.rs`.
364                    let blob_bytes = commands.len();
365                    let decode_mark = probe.start();
366
367                    match decode_dom_commands(&commands) {
368                        Ok(list) => {
369                            // Before the send, so the channel push is not counted as
370                            // decoding.
371                            probe.decoded(decode_mark, blob_bytes, list.len());
372
373                            sender
374                                .send(Message::DomUpdate(list))
375                                .inspect_err(|err| log::error!("Error sending DomUpdate: {err}"))
376                                .unwrap_or_default();
377                        }
378                        Err(err) => log::error!("Error decoding DomBulkUpdate: {err}"),
379                    }
380
381                    JsJson::Null
382                }
383            }
384        });
385
386        let mut inst = WasmInstance::new(
387            &self.engine,
388            &self.instance_pre,
389            HostState {
390                request,
391                sender: sender.clone(),
392                probe: probe.clone(),
393                handle_command,
394            },
395        );
396        probe.instantiate(instantiate_mark);
397
398        // -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
399        //TODO - ultimately, do not call call_vertigo_entry_function if something is returned by handle_url
400        // -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
401
402        inst.call_vertigo_entry_function();
403
404        if let Some(result) = inst.handle_url(url) {
405            return result;
406        }
407
408        let spawn_resource = SpawnOwner::new({
409            let sender = sender.clone();
410
411            async move {
412                tokio::time::sleep(std::time::Duration::from_secs(10)).await;
413                let _ = sender.send(Message::TimeoutAndSendResponse);
414            }
415        });
416
417        let mut html_response = HtmlResponse::new(
418            sender.clone(),
419            &self.mount_config,
420            inst,
421            self.mount_config.env.clone(),
422            fetch,
423            probe.clone(),
424            url,
425            headers
426                .cookie
427                .and_then(|cookie| forwarded_cookies(cookie, &self.mount_config.ssr_fetch_cookies)),
428        );
429
430        loop {
431            let message = receiver.try_recv();
432
433            match message {
434                Ok(message) => {
435                    if let Some(response) = html_response.process_message(message) {
436                        return response;
437                    };
438                    continue;
439                }
440                Err(TryRecvError::Empty) => {} // continue this iteration
441                Err(TryRecvError::Disconnected) => {
442                    break; // send response to browser
443                }
444            }
445
446            if html_response.awaiting_response() {
447                // Parked on an SSR fetch: time the request spent, but not time it spent
448                // working. Kept in its own bucket so it cannot be read as either.
449                let wait_mark = probe.start();
450                let message = receiver.recv().await;
451                probe.fetch_wait(wait_mark);
452
453                if let Some(message) = message
454                    && let Some(response) = html_response.process_message(message)
455                {
456                    return response;
457                };
458            } else {
459                break; // send response to browser
460            }
461        }
462
463        spawn_resource.off();
464        html_response.build_response()
465    }
466}
467
468/// Tells the render that the page depends on the cookie `name`, if it's one the app may read on
469/// the server: the others read as empty, whoever asks.
470fn note_cookie_read(sender: &UnboundedSender<Message>, ssr_cookies: &[String], name: &str) {
471    if ssr_cookies.iter().any(|cookie| cookie == name) {
472        sender
473            .send(Message::CookieRead)
474            .inspect_err(|err| log::error!("Error sending CookieRead: {err}"))
475            .unwrap_or_default();
476    }
477}
478
479fn build_module_wasm(
480    engine: &Engine,
481    mount_path: &MountConfig,
482) -> Result<(Module, Duration), ErrorCode> {
483    let full_wasm_path = mount_path.get_wasm_fs_path();
484
485    log::info!("Mounting {} -> {full_wasm_path}", mount_path.mount_point());
486
487    let wasm_content = match std::fs::read(&full_wasm_path) {
488        Ok(wasm_content) => wasm_content,
489        Err(error) => {
490            log::error!("Problem reading the path: wasm_path={full_wasm_path}, error={error}");
491            return Err(ErrorCode::ServeWasmReadFailed);
492        }
493    };
494
495    let now = Instant::now();
496
497    let module = match Module::from_binary(engine, &wasm_content) {
498        Ok(module) => module,
499        Err(err) => {
500            log::error!("Wasm compilation error: error={err}");
501            return Err(ErrorCode::ServeWasmCompileFailed);
502        }
503    };
504
505    let elapsed = now.elapsed();
506    log::info!("WASM module compiled in {} ms.", elapsed.as_millis());
507    Ok((module, elapsed))
508}