Skip to content

Commit 0b58e51

Browse files
Make filesystem object safe (#9747)
1 parent 56ffb67 commit 0b58e51

File tree

9 files changed

+179
-145
lines changed

9 files changed

+179
-145
lines changed

crates/node-bindings/src/core/requests/config_request/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ mod test {
354354

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

@@ -364,7 +364,7 @@ mod test {
364364

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

crates/node-bindings/src/core/requests/entry_request/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ mod test {
215215

216216
#[test]
217217
fn test_resolve_entry_file() {
218-
let mut fs = InMemoryFileSystem::default();
218+
let fs = InMemoryFileSystem::default();
219219
fs.set_current_working_directory("/project".into());
220220
let project_root = Path::new("/project");
221221
let path = Path::new("/project/file");

crates/node-bindings/src/resolver.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ pub struct JsFileSystem {
9393
}
9494

9595
impl FileSystem for JsFileSystem {
96-
fn canonicalize<P: AsRef<Path>>(
96+
fn canonicalize(
9797
&self,
98-
path: P,
98+
path: &Path,
9999
_cache: &DashMap<PathBuf, Option<PathBuf>>,
100100
) -> std::io::Result<std::path::PathBuf> {
101101
let canonicalize = || -> napi::Result<_> {
102-
let path = path.as_ref().to_string_lossy();
102+
let path = path.to_string_lossy();
103103
let path = self.canonicalize.env.create_string(path.as_ref())?;
104104
let res: JsString = self.canonicalize.get()?.call(None, &[path])?.try_into()?;
105105
let utf8 = res.into_utf8()?;
@@ -109,9 +109,9 @@ impl FileSystem for JsFileSystem {
109109
canonicalize().map_err(|err| std::io::Error::new(std::io::ErrorKind::NotFound, err.to_string()))
110110
}
111111

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

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

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

154154
#[cfg(not(target_arch = "wasm32"))]
155155
impl<A: FileSystem, B: FileSystem> FileSystem for EitherFs<A, B> {
156-
fn canonicalize<P: AsRef<Path>>(
156+
fn canonicalize(
157157
&self,
158-
path: P,
158+
path: &Path,
159159
cache: &DashMap<PathBuf, Option<PathBuf>>,
160160
) -> std::io::Result<std::path::PathBuf> {
161161
match self {
@@ -164,21 +164,21 @@ impl<A: FileSystem, B: FileSystem> FileSystem for EitherFs<A, B> {
164164
}
165165
}
166166

167-
fn read_to_string<P: AsRef<Path>>(&self, path: P) -> std::io::Result<String> {
167+
fn read_to_string(&self, path: &Path) -> std::io::Result<String> {
168168
match self {
169169
EitherFs::A(a) => a.read_to_string(path),
170170
EitherFs::B(b) => b.read_to_string(path),
171171
}
172172
}
173173

174-
fn is_file<P: AsRef<Path>>(&self, path: P) -> bool {
174+
fn is_file(&self, path: &Path) -> bool {
175175
match self {
176176
EitherFs::A(a) => a.is_file(path),
177177
EitherFs::B(b) => b.is_file(path),
178178
}
179179
}
180180

181-
fn is_dir<P: AsRef<Path>>(&self, path: P) -> bool {
181+
fn is_dir(&self, path: &Path) -> bool {
182182
match self {
183183
EitherFs::A(a) => a.is_dir(path),
184184
EitherFs::B(b) => b.is_dir(path),

0 commit comments

Comments
 (0)