diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-03-30 03:20:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-30 03:20:31 +0200 |
commit | 913e2875c1c31d5ffbc9c0c9ed0e8c63f6143024 (patch) | |
tree | 439fa7eef067893fde75dfe386d801a090e6c057 /ext/node/package_json.rs | |
parent | 89bbbd102c6050763105be39a49494a5ffdce35b (diff) |
refactor(ext/node): add NodeEnv::Fs associated type (#18484)
This commit adds associated type to "NodeEnv" trait, called "Fs".
The "Fs" type has a trait bound on "NodeFs", which specifies APIs
required for all ops and resolution APIs to function.
A "RealFs" implementation of "NodeFs" is exported from the "deno_node"
crate, that provides a default implementation for the trait.
All code in "deno_node" extension was changed to use the "NodeFs" trait
to handle file system operations, instead of relying on APIs from the
standard library.
Diffstat (limited to 'ext/node/package_json.rs')
-rw-r--r-- | ext/node/package_json.rs | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/ext/node/package_json.rs b/ext/node/package_json.rs index b0816dd85..60f50ad78 100644 --- a/ext/node/package_json.rs +++ b/ext/node/package_json.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use crate::NodeFs; use crate::NodeModuleKind; use crate::NodePermissions; @@ -61,16 +62,16 @@ impl PackageJson { } } - pub fn load( + pub fn load<Fs: NodeFs>( resolver: &dyn RequireNpmResolver, permissions: &mut dyn NodePermissions, path: PathBuf, ) -> Result<PackageJson, AnyError> { resolver.ensure_read_permission(permissions, &path)?; - Self::load_skip_read_permission(path) + Self::load_skip_read_permission::<Fs>(path) } - pub fn load_skip_read_permission( + pub fn load_skip_read_permission<Fs: NodeFs>( path: PathBuf, ) -> Result<PackageJson, AnyError> { assert!(path.is_absolute()); @@ -79,7 +80,7 @@ impl PackageJson { return Ok(CACHE.with(|cache| cache.borrow()[&path].clone())); } - let source = match std::fs::read_to_string(&path) { + let source = match Fs::read_to_string(&path) { Ok(source) => source, Err(err) if err.kind() == ErrorKind::NotFound => { return Ok(PackageJson::empty(path)); |