mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 18:38:33 +00:00
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.
This commit is contained in:
parent
89bbbd102c
commit
913e2875c1
9 changed files with 150 additions and 88 deletions
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue