@@ -17,29 +17,19 @@ pub trait FileSystem: Send + Sync {
1717 #[ cfg( not( feature = "yarn_pnp" ) ) ]
1818 fn new ( ) -> Self ;
1919
20- /// See [std::fs::read_to_string ]
20+ /// See [std::fs::read ]
2121 ///
2222 /// # Errors
2323 ///
24- /// * See [std::fs::read_to_string]
25- /// ## Warning
26- /// Use `&Path` instead of a generic `P: AsRef<Path>` here,
27- /// because object safety requirements, it is especially useful, when
28- /// you want to store multiple `dyn FileSystem` in a `Vec` or use a `ResolverGeneric<Fs>` in
29- /// napi env.
30- fn read_to_string ( & self , path : & Path ) -> io:: Result < String > ;
24+ /// * See [std::fs::read]
25+ fn read ( & self , path : & Path ) -> io:: Result < Vec < u8 > > ;
3126
32- /// Reads a file while bypassing the system cache.
33- ///
34- /// This is useful in scenarios where the file content is already cached in memory
35- /// and you want to avoid the overhead of using the system cache.
27+ /// See [std::fs::read_to_string]
3628 ///
3729 /// # Errors
3830 ///
3931 /// * See [std::fs::read_to_string]
40- fn read_to_string_bypass_system_cache ( & self , path : & Path ) -> io:: Result < String > {
41- self . read_to_string ( path)
42- }
32+ fn read_to_string ( & self , path : & Path ) -> io:: Result < String > ;
4333
4434 /// See [std::fs::metadata]
4535 ///
@@ -236,51 +226,26 @@ impl FileSystem for FileSystemOs {
236226 Self
237227 }
238228
239- fn read_to_string ( & self , path : & Path ) -> io:: Result < String > {
229+ fn read ( & self , path : & Path ) -> io:: Result < Vec < u8 > > {
240230 cfg_if ! {
241231 if #[ cfg( feature = "yarn_pnp" ) ] {
242232 if self . yarn_pnp {
243233 return match VPath :: from( path) ? {
244234 VPath :: Zip ( info) => {
245- self . pnp_lru. read_to_string ( info. physical_base_path( ) , info. zip_path)
235+ self . pnp_lru. read ( info. physical_base_path( ) , info. zip_path)
246236 }
247- VPath :: Virtual ( info) => Self :: read_to_string ( & info. physical_base_path( ) ) ,
248- VPath :: Native ( path) => Self :: read_to_string ( & path) ,
237+ VPath :: Virtual ( info) => fs :: read ( info. physical_base_path( ) ) ,
238+ VPath :: Native ( path) => fs :: read ( path) ,
249239 }
250240 }
251241 }
252242 }
253- Self :: read_to_string ( path)
243+ fs :: read ( path)
254244 }
255245
256- #[ allow( clippy:: items_after_statements) ]
257- fn read_to_string_bypass_system_cache ( & self , path : & Path ) -> io:: Result < String > {
258- #[ cfg( feature = "yarn_pnp" ) ]
259- if self . yarn_pnp {
260- return match VPath :: from ( path) ? {
261- VPath :: Zip ( info) => {
262- self . pnp_lru . read_to_string ( info. physical_base_path ( ) , info. zip_path )
263- }
264- VPath :: Virtual ( info) => Self :: read_to_string ( & info. physical_base_path ( ) ) ,
265- VPath :: Native ( path) => Self :: read_to_string ( & path) ,
266- } ;
267- }
268-
269- cfg_if ! {
270- if #[ cfg( target_os = "macos" ) ] {
271- use std:: io:: Read ;
272- let mut fd = fs:: OpenOptions :: new( ) . read( true ) . open( path) ?;
273- // Apply F_NOCACHE to bypass filesystem cache
274- rustix:: fs:: fcntl_nocache( & fd, true ) ?;
275- let meta = fd. metadata( ) ?;
276- #[ allow( clippy:: cast_possible_truncation) ]
277- let mut buffer = Vec :: with_capacity( meta. len( ) as usize ) ;
278- fd. read_to_end( & mut buffer) ?;
279- Self :: validate_string( buffer)
280- } else {
281- Self :: read_to_string( path)
282- }
283- }
246+ fn read_to_string ( & self , path : & Path ) -> io:: Result < String > {
247+ let bytes = self . read ( path) ?;
248+ Self :: validate_string ( bytes)
284249 }
285250
286251 fn metadata ( & self , path : & Path ) -> io:: Result < FileMetadata > {
0 commit comments