summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/fs/interface.rs25
-rw-r--r--ext/fs/ops.rs25
-rw-r--r--ext/node/ops/require.rs2
-rw-r--r--ext/node/package_json.rs2
4 files changed, 26 insertions, 28 deletions
diff --git a/ext/fs/interface.rs b/ext/fs/interface.rs
index 70f9fdf63..5031dc134 100644
--- a/ext/fs/interface.rs
+++ b/ext/fs/interface.rs
@@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+use std::borrow::Cow;
use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
@@ -284,24 +285,32 @@ pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync {
self.stat_sync(path).is_ok()
}
- fn read_text_file_sync(
+ fn read_text_file_lossy_sync(
&self,
path: &Path,
access_check: Option<AccessCheckCb>,
) -> FsResult<String> {
let buf = self.read_file_sync(path, access_check)?;
- String::from_utf8(buf).map_err(|err| {
- std::io::Error::new(std::io::ErrorKind::InvalidData, err).into()
- })
+ Ok(string_from_utf8_lossy(buf))
}
- async fn read_text_file_async<'a>(
+ async fn read_text_file_lossy_async<'a>(
&'a self,
path: PathBuf,
access_check: Option<AccessCheckCb<'a>>,
) -> FsResult<String> {
let buf = self.read_file_async(path, access_check).await?;
- String::from_utf8(buf).map_err(|err| {
- std::io::Error::new(std::io::ErrorKind::InvalidData, err).into()
- })
+ Ok(string_from_utf8_lossy(buf))
+ }
+}
+
+// Like String::from_utf8_lossy but operates on owned values
+#[inline(always)]
+fn string_from_utf8_lossy(buf: Vec<u8>) -> String {
+ match String::from_utf8_lossy(&buf) {
+ // buf contained non-utf8 chars than have been patched
+ Cow::Owned(s) => s,
+ // SAFETY: if Borrowed then the buf only contains utf8 chars,
+ // we do this instead of .into_owned() to avoid copying the input buf
+ Cow::Borrowed(_) => unsafe { String::from_utf8_unchecked(buf) },
}
}
diff --git a/ext/fs/ops.rs b/ext/fs/ops.rs
index 8e715d825..57b0e0b9e 100644
--- a/ext/fs/ops.rs
+++ b/ext/fs/ops.rs
@@ -1,6 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-use std::borrow::Cow;
use std::cell::RefCell;
use std::io;
use std::io::SeekFrom;
@@ -1333,11 +1332,11 @@ where
let fs = state.borrow::<FileSystemRc>().clone();
let mut access_check =
sync_permission_check::<P>(state.borrow_mut(), "Deno.readFileSync()");
- let buf = fs
- .read_file_sync(&path, Some(&mut access_check))
+ let str = fs
+ .read_text_file_lossy_sync(&path, Some(&mut access_check))
.map_err(|error| map_permission_error("readfile", error, &path))?;
- Ok(string_from_utf8_lossy(buf))
+ Ok(str)
}
#[op2(async)]
@@ -1361,9 +1360,10 @@ where
(state.borrow::<FileSystemRc>().clone(), cancel_handle)
};
- let fut = fs.read_file_async(path.clone(), Some(&mut access_check));
+ let fut =
+ fs.read_text_file_lossy_async(path.clone(), Some(&mut access_check));
- let buf = if let Some(cancel_handle) = cancel_handle {
+ let str = if let Some(cancel_handle) = cancel_handle {
let res = fut.or_cancel(cancel_handle).await;
if let Some(cancel_rid) = cancel_rid {
@@ -1379,18 +1379,7 @@ where
.map_err(|error| map_permission_error("readfile", error, &path))?
};
- Ok(string_from_utf8_lossy(buf))
-}
-
-// Like String::from_utf8_lossy but operates on owned values
-fn string_from_utf8_lossy(buf: Vec<u8>) -> String {
- match String::from_utf8_lossy(&buf) {
- // buf contained non-utf8 chars than have been patched
- Cow::Owned(s) => s,
- // SAFETY: if Borrowed then the buf only contains utf8 chars,
- // we do this instead of .into_owned() to avoid copying the input buf
- Cow::Borrowed(_) => unsafe { String::from_utf8_unchecked(buf) },
- }
+ Ok(str)
}
fn to_seek_from(offset: i64, whence: i32) -> Result<SeekFrom, AnyError> {
diff --git a/ext/node/ops/require.rs b/ext/node/ops/require.rs
index de2687001..3e1f1c6ae 100644
--- a/ext/node/ops/require.rs
+++ b/ext/node/ops/require.rs
@@ -451,7 +451,7 @@ where
let file_path = PathBuf::from(file_path);
ensure_read_permission::<P>(state, &file_path)?;
let fs = state.borrow::<FileSystemRc>();
- Ok(fs.read_text_file_sync(&file_path, None)?)
+ Ok(fs.read_text_file_lossy_sync(&file_path, None)?)
}
#[op2]
diff --git a/ext/node/package_json.rs b/ext/node/package_json.rs
index adae7d634..a19a2d64d 100644
--- a/ext/node/package_json.rs
+++ b/ext/node/package_json.rs
@@ -82,7 +82,7 @@ impl PackageJson {
return Ok(CACHE.with(|cache| cache.borrow()[&path].clone()));
}
- let source = match fs.read_text_file_sync(&path, None) {
+ let source = match fs.read_text_file_lossy_sync(&path, None) {
Ok(source) => source,
Err(err) if err.kind() == ErrorKind::NotFound => {
return Ok(Rc::new(PackageJson::empty(path)));