Skip to content

Commit

Permalink
Make filesystem object safe (#9747)
Browse files Browse the repository at this point in the history
  • Loading branch information
MonicaOlejniczak authored May 29, 2024
1 parent 56ffb67 commit 0b58e51
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 145 deletions.
4 changes: 2 additions & 2 deletions crates/node-bindings/src/core/requests/config_request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ mod test {

#[test]
fn test_read_json_config() {
let mut file_system = InMemoryFileSystem::default();
let file_system = InMemoryFileSystem::default();
let config_path = Path::new("/config.json");
file_system.write_file(config_path, String::from(r#"{"key": "value"}"#));

Expand All @@ -364,7 +364,7 @@ mod test {

#[test]
fn test_read_toml_config() {
let mut file_system = InMemoryFileSystem::default();
let file_system = InMemoryFileSystem::default();
let config_path = Path::new("/config.toml");
file_system.write_file(config_path, String::from(r#"key = "value""#));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ mod test {

#[test]
fn test_resolve_entry_file() {
let mut fs = InMemoryFileSystem::default();
let fs = InMemoryFileSystem::default();
fs.set_current_working_directory("/project".into());
let project_root = Path::new("/project");
let path = Path::new("/project/file");
Expand Down
28 changes: 14 additions & 14 deletions crates/node-bindings/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ pub struct JsFileSystem {
}

impl FileSystem for JsFileSystem {
fn canonicalize<P: AsRef<Path>>(
fn canonicalize(
&self,
path: P,
path: &Path,
_cache: &DashMap<PathBuf, Option<PathBuf>>,
) -> std::io::Result<std::path::PathBuf> {
let canonicalize = || -> napi::Result<_> {
let path = path.as_ref().to_string_lossy();
let path = path.to_string_lossy();
let path = self.canonicalize.env.create_string(path.as_ref())?;
let res: JsString = self.canonicalize.get()?.call(None, &[path])?.try_into()?;
let utf8 = res.into_utf8()?;
Expand All @@ -109,9 +109,9 @@ impl FileSystem for JsFileSystem {
canonicalize().map_err(|err| std::io::Error::new(std::io::ErrorKind::NotFound, err.to_string()))
}

fn read_to_string<P: AsRef<Path>>(&self, path: P) -> std::io::Result<String> {
fn read_to_string(&self, path: &Path) -> std::io::Result<String> {
let read = || -> napi::Result<_> {
let path = path.as_ref().to_string_lossy();
let path = path.to_string_lossy();
let path = self.read.env.create_string(path.as_ref())?;
let res: JsBuffer = self.read.get()?.call(None, &[path])?.try_into()?;
let value = res.into_value()?;
Expand All @@ -121,9 +121,9 @@ impl FileSystem for JsFileSystem {
read().map_err(|err| std::io::Error::new(std::io::ErrorKind::NotFound, err.to_string()))
}

fn is_file<P: AsRef<Path>>(&self, path: P) -> bool {
fn is_file(&self, path: &Path) -> bool {
let is_file = || -> napi::Result<_> {
let path = path.as_ref().to_string_lossy();
let path = path.to_string_lossy();
let p = self.is_file.env.create_string(path.as_ref())?;
let res: JsBoolean = self.is_file.get()?.call(None, &[p])?.try_into()?;
res.get_value()
Expand All @@ -132,9 +132,9 @@ impl FileSystem for JsFileSystem {
is_file().unwrap_or(false)
}

fn is_dir<P: AsRef<Path>>(&self, path: P) -> bool {
fn is_dir(&self, path: &Path) -> bool {
let is_dir = || -> napi::Result<_> {
let path = path.as_ref().to_string_lossy();
let path = path.to_string_lossy();
let path = self.is_dir.env.create_string(path.as_ref())?;
let res: JsBoolean = self.is_dir.get()?.call(None, &[path])?.try_into()?;
res.get_value()
Expand All @@ -153,9 +153,9 @@ enum EitherFs<A, B> {

#[cfg(not(target_arch = "wasm32"))]
impl<A: FileSystem, B: FileSystem> FileSystem for EitherFs<A, B> {
fn canonicalize<P: AsRef<Path>>(
fn canonicalize(
&self,
path: P,
path: &Path,
cache: &DashMap<PathBuf, Option<PathBuf>>,
) -> std::io::Result<std::path::PathBuf> {
match self {
Expand All @@ -164,21 +164,21 @@ impl<A: FileSystem, B: FileSystem> FileSystem for EitherFs<A, B> {
}
}

fn read_to_string<P: AsRef<Path>>(&self, path: P) -> std::io::Result<String> {
fn read_to_string(&self, path: &Path) -> std::io::Result<String> {
match self {
EitherFs::A(a) => a.read_to_string(path),
EitherFs::B(b) => b.read_to_string(path),
}
}

fn is_file<P: AsRef<Path>>(&self, path: P) -> bool {
fn is_file(&self, path: &Path) -> bool {
match self {
EitherFs::A(a) => a.is_file(path),
EitherFs::B(b) => b.is_file(path),
}
}

fn is_dir<P: AsRef<Path>>(&self, path: P) -> bool {
fn is_dir(&self, path: &Path) -> bool {
match self {
EitherFs::A(a) => a.is_dir(path),
EitherFs::B(b) => b.is_dir(path),
Expand Down
Loading

0 comments on commit 0b58e51

Please sign in to comment.