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                CommandForBrowser::GetRandom { min, max: _ } => {
355                    browser_response::GetRandom { value: min }.to_json()
356                }
357                CommandForBrowser::JsApiCall { commands: _ } => JsJson::Null,
358                CommandForBrowser::DomBulkUpdate { commands } => {
359                    // Host work, but reached from inside a wasm call - so this is
360                    // phase-3 time measured within a phase-2 region, and `SsrTimings`
361                    // subtracts it back out. See the module docs in `timings.rs`.
362                    let blob_bytes = commands.len();
363                    let decode_mark = probe.start();
364
365                    match decode_dom_commands(&commands) {
366                        Ok(list) => {
367                            // Before the send, so the channel push is not counted as
368                            // decoding.
369                            probe.decoded(decode_mark, blob_bytes, list.len());
370
371                            sender
372                                .send(Message::DomUpdate(list))
373                                .inspect_err(|err| log::error!("Error sending DomUpdate: {err}"))
374                                .unwrap_or_default();
375                        }
376                        Err(err) => log::error!("Error decoding DomBulkUpdate: {err}"),
377                    }
378
379                    JsJson::Null
380                }
381            }
382        });
383
384        let mut inst = WasmInstance::new(
385            &self.engine,
386            &self.instance_pre,
387            HostState {
388                request,
389                sender: sender.clone(),
390                probe: probe.clone(),
391                handle_command,
392            },
393        );
394        probe.instantiate(instantiate_mark);
395
396        // -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
397        //TODO - ultimately, do not call call_vertigo_entry_function if something is returned by handle_url
398        // -- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
399
400        inst.call_vertigo_entry_function();
401
402        if let Some(result) = inst.handle_url(url) {
403            return result;
404        }
405
406        let spawn_resource = SpawnOwner::new({
407            let sender = sender.clone();
408
409            async move {
410                tokio::time::sleep(std::time::Duration::from_secs(10)).await;
411                let _ = sender.send(Message::TimeoutAndSendResponse);
412            }
413        });
414
415        let mut html_response = HtmlResponse::new(
416            sender.clone(),
417            &self.mount_config,
418            inst,
419            self.mount_config.env.clone(),
420            fetch,
421            probe.clone(),
422            url,
423            headers
424                .cookie
425                .and_then(|cookie| forwarded_cookies(cookie, &self.mount_config.ssr_fetch_cookies)),
426        );
427
428        loop {
429            let message = receiver.try_recv();
430
431            match message {
432                Ok(message) => {
433                    if let Some(response) = html_response.process_message(message) {
434                        return response;
435                    };
436                    continue;
437                }
438                Err(TryRecvError::Empty) => {} // continue this iteration
439                Err(TryRecvError::Disconnected) => {
440                    break; // send response to browser
441                }
442            }
443
444            if html_response.awaiting_response() {
445                // Parked on an SSR fetch: time the request spent, but not time it spent
446                // working. Kept in its own bucket so it cannot be read as either.
447                let wait_mark = probe.start();
448                let message = receiver.recv().await;
449                probe.fetch_wait(wait_mark);
450
451                if let Some(message) = message
452                    && let Some(response) = html_response.process_message(message)
453                {
454                    return response;
455                };
456            } else {
457                break; // send response to browser
458            }
459        }
460
461        spawn_resource.off();
462        html_response.build_response()
463    }
464}
465
466/// Tells the render that the page depends on the cookie `name`, if it's one the app may read on
467/// the server: the others read as empty, whoever asks.
468fn note_cookie_read(sender: &UnboundedSender<Message>, ssr_cookies: &[String], name: &str) {
469    if ssr_cookies.iter().any(|cookie| cookie == name) {
470        sender
471            .send(Message::CookieRead)
472            .inspect_err(|err| log::error!("Error sending CookieRead: {err}"))
473            .unwrap_or_default();
474    }
475}
476
477fn build_module_wasm(
478    engine: &Engine,
479    mount_path: &MountConfig,
480) -> Result<(Module, Duration), ErrorCode> {
481    let full_wasm_path = mount_path.get_wasm_fs_path();
482
483    log::info!("Mounting {} -> {full_wasm_path}", mount_path.mount_point());
484
485    let wasm_content = match std::fs::read(&full_wasm_path) {
486        Ok(wasm_content) => wasm_content,
487        Err(error) => {
488            log::error!("Problem reading the path: wasm_path={full_wasm_path}, error={error}");
489            return Err(ErrorCode::ServeWasmReadFailed);
490        }
491    };
492
493    let now = Instant::now();
494
495    let module = match Module::from_binary(engine, &wasm_content) {
496        Ok(module) => module,
497        Err(err) => {
498            log::error!("Wasm compilation error: error={err}");
499            return Err(ErrorCode::ServeWasmCompileFailed);
500        }
501    };
502
503    let elapsed = now.elapsed();
504    log::info!("WASM module compiled in {} ms.", elapsed.as_millis());
505    Ok((module, elapsed))
506}