tinymist_vfs/
browser.rs

1use std::path::Path;
2
3use typst::diag::{FileError, FileResult};
4use wasm_bindgen::prelude::*;
5
6use crate::{Bytes, PathAccessModel};
7
8/// Provides proxy access model from typst compiler to some JavaScript
9/// implementation.
10#[derive(Debug, Clone)]
11pub struct ProxyAccessModel {
12    /// The `this` value when calling the JavaScript functions
13    pub context: JsValue,
14    /// The JavaScript function to get the mtime of a file
15    pub mtime_fn: js_sys::Function,
16    /// The JavaScript function to check if a path corresponds to a file or a
17    /// directory
18    pub is_file_fn: js_sys::Function,
19    /// The JavaScript function to get the real path of a file
20    pub real_path_fn: js_sys::Function,
21    /// The JavaScript function to get the content of a file
22    pub read_all_fn: js_sys::Function,
23}
24
25impl PathAccessModel for ProxyAccessModel {
26    fn content(&self, src: &Path) -> FileResult<Bytes> {
27        let is_file = self
28            .is_file_fn
29            .call1(&self.context, &src.to_string_lossy().as_ref().into())
30            .map(|v| v.as_bool().unwrap())
31            .map_err(|e| {
32                web_sys::console::error_3(
33                    &"tinymist-vfs::ProxyAccessModel::is_file failure".into(),
34                    &src.to_string_lossy().as_ref().into(),
35                    &e,
36                );
37                FileError::AccessDenied
38            });
39
40        // todo: remove this compatibility code
41        if !is_file? {
42            return Err(FileError::IsDirectory);
43        }
44
45        let data = self
46            .read_all_fn
47            .call1(&self.context, &src.to_string_lossy().as_ref().into())
48            .map_err(|e| {
49                web_sys::console::error_3(
50                    &"tinymist-vfs::ProxyAccessModel::read_all failure".into(),
51                    &src.to_string_lossy().as_ref().into(),
52                    &e,
53                );
54                FileError::AccessDenied
55            })?;
56
57        let data = if let Some(data) = data.dyn_ref::<js_sys::Uint8Array>() {
58            Bytes::new(data.to_vec())
59        } else {
60            return Err(FileError::AccessDenied);
61        };
62
63        Ok(data)
64    }
65}
66
67// todo
68/// Safety: `ProxyAccessModel` is only used in the browser environment, and we
69/// cannot share data between workers.
70unsafe impl Send for ProxyAccessModel {}
71/// Safety: `ProxyAccessModel` is only used in the browser environment, and we
72/// cannot share data between workers.
73unsafe impl Sync for ProxyAccessModel {}