1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05: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:
Bartek Iwańczuk 2023-03-30 03:20:31 +02:00 committed by Matt Mastracci
parent e7b80efca1
commit 8497cef156
9 changed files with 150 additions and 88 deletions

View file

@ -31,6 +31,7 @@ use deno_runtime::deno_node::NodePermissions;
use deno_runtime::deno_node::NodeResolutionMode; use deno_runtime::deno_node::NodeResolutionMode;
use deno_runtime::deno_node::PackageJson; use deno_runtime::deno_node::PackageJson;
use deno_runtime::deno_node::PathClean; use deno_runtime::deno_node::PathClean;
use deno_runtime::deno_node::RealFs;
use deno_runtime::deno_node::RequireNpmResolver; use deno_runtime::deno_node::RequireNpmResolver;
use deno_runtime::deno_node::DEFAULT_CONDITIONS; use deno_runtime::deno_node::DEFAULT_CONDITIONS;
use deno_runtime::permissions::PermissionsContainer; use deno_runtime::permissions::PermissionsContainer;
@ -227,7 +228,8 @@ pub fn node_resolve(
let path = url.to_file_path().unwrap(); let path = url.to_file_path().unwrap();
// todo(16370): the module kind is not correct here. I think we need // todo(16370): the module kind is not correct here. I think we need
// typescript to tell us if the referrer is esm or cjs // typescript to tell us if the referrer is esm or cjs
let path = match path_to_declaration_path(path, NodeModuleKind::Esm) { let path =
match path_to_declaration_path::<RealFs>(path, NodeModuleKind::Esm) {
Some(path) => path, Some(path) => path,
None => return Ok(None), None => return Ok(None),
}; };
@ -273,7 +275,8 @@ pub fn node_resolve_npm_reference(
let resolved_path = match mode { let resolved_path = match mode {
NodeResolutionMode::Execution => resolved_path, NodeResolutionMode::Execution => resolved_path,
NodeResolutionMode::Types => { NodeResolutionMode::Types => {
match path_to_declaration_path(resolved_path, node_module_kind) { match path_to_declaration_path::<RealFs>(resolved_path, node_module_kind)
{
Some(path) => path, Some(path) => path,
None => return Ok(None), None => return Ok(None),
} }
@ -312,7 +315,7 @@ pub fn node_resolve_binary_commands(
let package_folder = let package_folder =
npm_resolver.resolve_package_folder_from_deno_module(pkg_nv)?; npm_resolver.resolve_package_folder_from_deno_module(pkg_nv)?;
let package_json_path = package_folder.join("package.json"); let package_json_path = package_folder.join("package.json");
let package_json = PackageJson::load( let package_json = PackageJson::load::<RealFs>(
npm_resolver, npm_resolver,
&mut PermissionsContainer::allow_all(), &mut PermissionsContainer::allow_all(),
package_json_path, package_json_path,
@ -335,7 +338,7 @@ pub fn node_resolve_binary_export(
let package_folder = let package_folder =
npm_resolver.resolve_package_folder_from_deno_module(pkg_nv)?; npm_resolver.resolve_package_folder_from_deno_module(pkg_nv)?;
let package_json_path = package_folder.join("package.json"); let package_json_path = package_folder.join("package.json");
let package_json = PackageJson::load( let package_json = PackageJson::load::<RealFs>(
npm_resolver, npm_resolver,
&mut PermissionsContainer::allow_all(), &mut PermissionsContainer::allow_all(),
package_json_path, package_json_path,
@ -424,10 +427,13 @@ fn package_config_resolve(
) -> Result<Option<PathBuf>, AnyError> { ) -> Result<Option<PathBuf>, AnyError> {
let package_json_path = package_dir.join("package.json"); let package_json_path = package_dir.join("package.json");
let referrer = ModuleSpecifier::from_directory_path(package_dir).unwrap(); let referrer = ModuleSpecifier::from_directory_path(package_dir).unwrap();
let package_config = let package_config = PackageJson::load::<RealFs>(
PackageJson::load(npm_resolver, permissions, package_json_path.clone())?; npm_resolver,
permissions,
package_json_path.clone(),
)?;
if let Some(exports) = &package_config.exports { if let Some(exports) = &package_config.exports {
let result = package_exports_resolve( let result = package_exports_resolve::<RealFs>(
&package_json_path, &package_json_path,
package_subpath.to_string(), package_subpath.to_string(),
exports, exports,
@ -443,7 +449,7 @@ fn package_config_resolve(
Err(exports_err) => { Err(exports_err) => {
if mode.is_types() && package_subpath == "." { if mode.is_types() && package_subpath == "." {
if let Ok(Some(path)) = if let Ok(Some(path)) =
legacy_main_resolve(&package_config, referrer_kind, mode) legacy_main_resolve::<RealFs>(&package_config, referrer_kind, mode)
{ {
return Ok(Some(path)); return Ok(Some(path));
} else { } else {
@ -455,7 +461,7 @@ fn package_config_resolve(
} }
} }
if package_subpath == "." { if package_subpath == "." {
return legacy_main_resolve(&package_config, referrer_kind, mode); return legacy_main_resolve::<RealFs>(&package_config, referrer_kind, mode);
} }
Ok(Some(package_dir.join(package_subpath))) Ok(Some(package_dir.join(package_subpath)))
@ -469,7 +475,7 @@ pub fn url_to_node_resolution(
if url_str.starts_with("http") { if url_str.starts_with("http") {
Ok(NodeResolution::Esm(url)) Ok(NodeResolution::Esm(url))
} else if url_str.ends_with(".js") || url_str.ends_with(".d.ts") { } else if url_str.ends_with(".js") || url_str.ends_with(".d.ts") {
let package_config = get_closest_package_json( let package_config = get_closest_package_json::<RealFs>(
&url, &url,
npm_resolver, npm_resolver,
&mut PermissionsContainer::allow_all(), &mut PermissionsContainer::allow_all(),
@ -556,7 +562,7 @@ fn module_resolve(
// todo(dsherret): the node module kind is not correct and we // todo(dsherret): the node module kind is not correct and we
// should use the value provided by typescript instead // should use the value provided by typescript instead
let declaration_path = let declaration_path =
path_to_declaration_path(file_path, NodeModuleKind::Esm); path_to_declaration_path::<RealFs>(file_path, NodeModuleKind::Esm);
declaration_path.map(|declaration_path| { declaration_path.map(|declaration_path| {
ModuleSpecifier::from_file_path(declaration_path).unwrap() ModuleSpecifier::from_file_path(declaration_path).unwrap()
}) })
@ -565,7 +571,7 @@ fn module_resolve(
} }
} else if specifier.starts_with('#') { } else if specifier.starts_with('#') {
Some( Some(
package_imports_resolve( package_imports_resolve::<RealFs>(
specifier, specifier,
referrer, referrer,
NodeModuleKind::Esm, NodeModuleKind::Esm,
@ -579,7 +585,7 @@ fn module_resolve(
} else if let Ok(resolved) = Url::parse(specifier) { } else if let Ok(resolved) = Url::parse(specifier) {
Some(resolved) Some(resolved)
} else { } else {
package_resolve( package_resolve::<RealFs>(
specifier, specifier,
referrer, referrer,
NodeModuleKind::Esm, NodeModuleKind::Esm,
@ -821,11 +827,14 @@ fn resolve(
let package_json_path = module_dir.join("package.json"); let package_json_path = module_dir.join("package.json");
if package_json_path.exists() { if package_json_path.exists() {
let package_json = let package_json = PackageJson::load::<RealFs>(
PackageJson::load(npm_resolver, permissions, package_json_path.clone())?; npm_resolver,
permissions,
package_json_path.clone(),
)?;
if let Some(exports) = &package_json.exports { if let Some(exports) = &package_json.exports {
return package_exports_resolve( return package_exports_resolve::<deno_node::RealFs>(
&package_json_path, &package_json_path,
package_subpath, package_subpath,
exports, exports,
@ -846,8 +855,11 @@ fn resolve(
// subdir might have a package.json that specifies the entrypoint // subdir might have a package.json that specifies the entrypoint
let package_json_path = d.join("package.json"); let package_json_path = d.join("package.json");
if package_json_path.exists() { if package_json_path.exists() {
let package_json = let package_json = PackageJson::load::<RealFs>(
PackageJson::load(npm_resolver, permissions, package_json_path)?; npm_resolver,
permissions,
package_json_path,
)?;
if let Some(main) = package_json.main(NodeModuleKind::Cjs) { if let Some(main) = package_json.main(NodeModuleKind::Cjs) {
return Ok(d.join(main).clean()); return Ok(d.join(main).clean());
} }

View file

@ -159,9 +159,9 @@ impl NpmPackageFsResolver for LocalNpmPackageResolver {
if sub_dir.is_dir() { if sub_dir.is_dir() {
// if doing types resolution, only resolve the package if it specifies a types property // if doing types resolution, only resolve the package if it specifies a types property
if mode.is_types() && !name.starts_with("@types/") { if mode.is_types() && !name.starts_with("@types/") {
let package_json = PackageJson::load_skip_read_permission( let package_json = PackageJson::load_skip_read_permission::<
sub_dir.join("package.json"), deno_runtime::deno_node::RealFs,
)?; >(sub_dir.join("package.json"))?;
if package_json.types.is_some() { if package_json.types.is_some() {
return Ok(sub_dir); return Ok(sub_dir);
} }

21
ext/node/clippy.toml Normal file
View file

@ -0,0 +1,21 @@
disallowed-methods = [
{ path = "std::env::current_dir", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::path::Path::exists", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::canonicalize", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::copy", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::create_dir", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::create_dir_all", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::hard_link", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::metadata", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::read", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::read_dir", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::read_link", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::read_to_string", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::remove_dir", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::remove_dir_all", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::remove_file", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::rename", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::set_permissions", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::symlink_metadata", reason = "File system operations should be done using NodeFs trait" },
{ path = "std::fs::write", reason = "File system operations should be done using NodeFs trait" },
]

View file

@ -6,6 +6,7 @@ use deno_core::op;
use deno_core::JsRuntime; use deno_core::JsRuntime;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::collections::HashSet; use std::collections::HashSet;
use std::io;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
@ -41,18 +42,36 @@ pub use resolution::DEFAULT_CONDITIONS;
pub trait NodeEnv { pub trait NodeEnv {
type P: NodePermissions; type P: NodePermissions;
// TODO(bartlomieju): type Fs: NodeFs;
// type Fs: NodeFs;
} }
pub trait NodePermissions { pub trait NodePermissions {
fn check_read(&mut self, path: &Path) -> Result<(), AnyError>; fn check_read(&mut self, path: &Path) -> Result<(), AnyError>;
} }
// TODO(bartlomieju): pub trait NodeFs {
// pub trait NodeFs { fn current_dir() -> io::Result<PathBuf>;
// fn current_dir() -> Result<PathBuf, AnyError>; fn metadata<P: AsRef<Path>>(path: P) -> io::Result<std::fs::Metadata>;
// } fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String>;
}
pub struct RealFs;
impl NodeFs for RealFs {
fn current_dir() -> io::Result<PathBuf> {
#[allow(clippy::disallowed_methods)]
std::env::current_dir()
}
fn metadata<P: AsRef<Path>>(path: P) -> io::Result<std::fs::Metadata> {
#[allow(clippy::disallowed_methods)]
std::fs::metadata(path)
}
fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
#[allow(clippy::disallowed_methods)]
std::fs::read_to_string(path)
}
}
pub trait RequireNpmResolver { pub trait RequireNpmResolver {
fn resolve_package_folder_from_package( fn resolve_package_folder_from_package(

View file

@ -14,6 +14,7 @@ use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use crate::NodeEnv; use crate::NodeEnv;
use crate::NodeFs;
use super::resolution; use super::resolution;
use super::NodeModuleKind; use super::NodeModuleKind;
@ -98,7 +99,7 @@ where
// Guarantee that "from" is absolute. // Guarantee that "from" is absolute.
let from = deno_core::resolve_path( let from = deno_core::resolve_path(
&from, &from,
&std::env::current_dir().context("Unable to get CWD")?, &(Env::Fs::current_dir()).context("Unable to get CWD")?,
) )
.unwrap() .unwrap()
.to_file_path() .to_file_path()
@ -263,7 +264,7 @@ where
{ {
let path = PathBuf::from(path); let path = PathBuf::from(path);
ensure_read_permission::<Env::P>(state, &path)?; ensure_read_permission::<Env::P>(state, &path)?;
if let Ok(metadata) = std::fs::metadata(&path) { if let Ok(metadata) = Env::Fs::metadata(&path) {
if metadata.is_file() { if metadata.is_file() {
return Ok(0); return Ok(0);
} else { } else {
@ -352,7 +353,7 @@ where
if let Some(parent_id) = maybe_parent_id { if let Some(parent_id) = maybe_parent_id {
if parent_id == "<repl>" || parent_id == "internal/preload" { if parent_id == "<repl>" || parent_id == "internal/preload" {
if let Ok(cwd) = std::env::current_dir() { if let Ok(cwd) = Env::Fs::current_dir() {
ensure_read_permission::<Env::P>(state, &cwd)?; ensure_read_permission::<Env::P>(state, &cwd)?;
return Ok(Some(cwd.to_string_lossy().to_string())); return Ok(Some(cwd.to_string_lossy().to_string()));
} }
@ -376,7 +377,7 @@ where
let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone(); let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone();
let permissions = state.borrow_mut::<Env::P>(); let permissions = state.borrow_mut::<Env::P>();
let pkg = resolution::get_package_scope_config( let pkg = resolution::get_package_scope_config::<Env::Fs>(
&Url::from_file_path(parent_path.unwrap()).unwrap(), &Url::from_file_path(parent_path.unwrap()).unwrap(),
&*resolver, &*resolver,
permissions, permissions,
@ -407,7 +408,7 @@ where
let referrer = deno_core::url::Url::from_file_path(&pkg.path).unwrap(); let referrer = deno_core::url::Url::from_file_path(&pkg.path).unwrap();
if let Some(exports) = &pkg.exports { if let Some(exports) = &pkg.exports {
resolution::package_exports_resolve( resolution::package_exports_resolve::<Env::Fs>(
&pkg.path, &pkg.path,
expansion, expansion,
exports, exports,
@ -434,7 +435,7 @@ where
{ {
let file_path = PathBuf::from(file_path); let file_path = PathBuf::from(file_path);
ensure_read_permission::<Env::P>(state, &file_path)?; ensure_read_permission::<Env::P>(state, &file_path)?;
Ok(std::fs::read_to_string(file_path)?) Ok(Env::Fs::read_to_string(file_path)?)
} }
#[op] #[op]
@ -471,7 +472,7 @@ where
} else { } else {
path_resolve(vec![modules_path, name]) path_resolve(vec![modules_path, name])
}; };
let pkg = PackageJson::load( let pkg = PackageJson::load::<Env::Fs>(
&*resolver, &*resolver,
permissions, permissions,
PathBuf::from(&pkg_path).join("package.json"), PathBuf::from(&pkg_path).join("package.json"),
@ -479,7 +480,7 @@ where
if let Some(exports) = &pkg.exports { if let Some(exports) = &pkg.exports {
let referrer = Url::from_file_path(parent_path).unwrap(); let referrer = Url::from_file_path(parent_path).unwrap();
resolution::package_exports_resolve( resolution::package_exports_resolve::<Env::Fs>(
&pkg.path, &pkg.path,
format!(".{expansion}"), format!(".{expansion}"),
exports, exports,
@ -510,7 +511,7 @@ where
)?; )?;
let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone(); let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone();
let permissions = state.borrow_mut::<Env::P>(); let permissions = state.borrow_mut::<Env::P>();
resolution::get_closest_package_json( resolution::get_closest_package_json::<Env::Fs>(
&Url::from_file_path(filename).unwrap(), &Url::from_file_path(filename).unwrap(),
&*resolver, &*resolver,
permissions, permissions,
@ -528,7 +529,7 @@ where
let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone(); let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone();
let permissions = state.borrow_mut::<Env::P>(); let permissions = state.borrow_mut::<Env::P>();
let package_json_path = PathBuf::from(package_json_path); let package_json_path = PathBuf::from(package_json_path);
PackageJson::load(&*resolver, permissions, package_json_path).ok() PackageJson::load::<Env::Fs>(&*resolver, permissions, package_json_path).ok()
} }
#[op] #[op]
@ -544,7 +545,7 @@ where
ensure_read_permission::<Env::P>(state, &parent_path)?; ensure_read_permission::<Env::P>(state, &parent_path)?;
let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone(); let resolver = state.borrow::<Rc<dyn RequireNpmResolver>>().clone();
let permissions = state.borrow_mut::<Env::P>(); let permissions = state.borrow_mut::<Env::P>();
let pkg = PackageJson::load( let pkg = PackageJson::load::<Env::Fs>(
&*resolver, &*resolver,
permissions, permissions,
parent_path.join("package.json"), parent_path.join("package.json"),
@ -553,7 +554,7 @@ where
if pkg.imports.is_some() { if pkg.imports.is_some() {
let referrer = let referrer =
deno_core::url::Url::from_file_path(&parent_filename).unwrap(); deno_core::url::Url::from_file_path(&parent_filename).unwrap();
let r = resolution::package_imports_resolve( let r = resolution::package_imports_resolve::<Env::Fs>(
&request, &request,
&referrer, &referrer,
NodeModuleKind::Cjs, NodeModuleKind::Cjs,

View file

@ -1,5 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::NodeFs;
use crate::NodeModuleKind; use crate::NodeModuleKind;
use crate::NodePermissions; use crate::NodePermissions;
@ -61,16 +62,16 @@ impl PackageJson {
} }
} }
pub fn load( pub fn load<Fs: NodeFs>(
resolver: &dyn RequireNpmResolver, resolver: &dyn RequireNpmResolver,
permissions: &mut dyn NodePermissions, permissions: &mut dyn NodePermissions,
path: PathBuf, path: PathBuf,
) -> Result<PackageJson, AnyError> { ) -> Result<PackageJson, AnyError> {
resolver.ensure_read_permission(permissions, &path)?; 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, path: PathBuf,
) -> Result<PackageJson, AnyError> { ) -> Result<PackageJson, AnyError> {
assert!(path.is_absolute()); assert!(path.is_absolute());
@ -79,7 +80,7 @@ impl PackageJson {
return Ok(CACHE.with(|cache| cache.borrow()[&path].clone())); 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, Ok(source) => source,
Err(err) if err.kind() == ErrorKind::NotFound => { Err(err) if err.kind() == ErrorKind::NotFound => {
return Ok(PackageJson::empty(path)); return Ok(PackageJson::empty(path));

View file

@ -15,6 +15,7 @@ use regex::Regex;
use crate::errors; use crate::errors;
use crate::package_json::PackageJson; use crate::package_json::PackageJson;
use crate::path::PathClean; use crate::path::PathClean;
use crate::NodeFs;
use crate::NodePermissions; use crate::NodePermissions;
use crate::RequireNpmResolver; use crate::RequireNpmResolver;
@ -40,11 +41,11 @@ impl NodeResolutionMode {
} }
/// Checks if the resolved file has a corresponding declaration file. /// Checks if the resolved file has a corresponding declaration file.
pub fn path_to_declaration_path( pub fn path_to_declaration_path<Fs: NodeFs>(
path: PathBuf, path: PathBuf,
referrer_kind: NodeModuleKind, referrer_kind: NodeModuleKind,
) -> Option<PathBuf> { ) -> Option<PathBuf> {
fn probe_extensions( fn probe_extensions<Fs: NodeFs>(
path: &Path, path: &Path,
referrer_kind: NodeModuleKind, referrer_kind: NodeModuleKind,
) -> Option<PathBuf> { ) -> Option<PathBuf> {
@ -52,11 +53,11 @@ pub fn path_to_declaration_path(
NodeModuleKind::Cjs => with_known_extension(path, "d.cts"), NodeModuleKind::Cjs => with_known_extension(path, "d.cts"),
NodeModuleKind::Esm => with_known_extension(path, "d.mts"), NodeModuleKind::Esm => with_known_extension(path, "d.mts"),
}; };
if specific_dts_path.exists() { if Fs::metadata(&specific_dts_path).is_ok() {
return Some(specific_dts_path); return Some(specific_dts_path);
} }
let dts_path = with_known_extension(path, "d.ts"); let dts_path = with_known_extension(path, "d.ts");
if dts_path.exists() { if Fs::metadata(&dts_path).is_ok() {
Some(dts_path) Some(dts_path)
} else { } else {
None None
@ -70,11 +71,13 @@ pub fn path_to_declaration_path(
{ {
return Some(path); return Some(path);
} }
if let Some(path) = probe_extensions(&path, referrer_kind) { if let Some(path) = probe_extensions::<Fs>(&path, referrer_kind) {
return Some(path); return Some(path);
} }
if path.is_dir() { if path.is_dir() {
if let Some(path) = probe_extensions(&path.join("index"), referrer_kind) { if let Some(path) =
probe_extensions::<Fs>(&path.join("index"), referrer_kind)
{
return Some(path); return Some(path);
} }
} }
@ -182,7 +185,7 @@ fn pattern_key_compare(a: &str, b: &str) -> i32 {
0 0
} }
pub fn package_imports_resolve( pub fn package_imports_resolve<Fs: NodeFs>(
name: &str, name: &str,
referrer: &ModuleSpecifier, referrer: &ModuleSpecifier,
referrer_kind: NodeModuleKind, referrer_kind: NodeModuleKind,
@ -201,13 +204,13 @@ pub fn package_imports_resolve(
} }
let package_config = let package_config =
get_package_scope_config(referrer, npm_resolver, permissions)?; get_package_scope_config::<Fs>(referrer, npm_resolver, permissions)?;
let mut package_json_path = None; let mut package_json_path = None;
if package_config.exists { if package_config.exists {
package_json_path = Some(package_config.path.clone()); package_json_path = Some(package_config.path.clone());
if let Some(imports) = &package_config.imports { if let Some(imports) = &package_config.imports {
if imports.contains_key(name) && !name.contains('*') { if imports.contains_key(name) && !name.contains('*') {
let maybe_resolved = resolve_package_target( let maybe_resolved = resolve_package_target::<Fs>(
package_json_path.as_ref().unwrap(), package_json_path.as_ref().unwrap(),
imports.get(name).unwrap().to_owned(), imports.get(name).unwrap().to_owned(),
"".to_string(), "".to_string(),
@ -250,7 +253,7 @@ pub fn package_imports_resolve(
if !best_match.is_empty() { if !best_match.is_empty() {
let target = imports.get(best_match).unwrap().to_owned(); let target = imports.get(best_match).unwrap().to_owned();
let maybe_resolved = resolve_package_target( let maybe_resolved = resolve_package_target::<Fs>(
package_json_path.as_ref().unwrap(), package_json_path.as_ref().unwrap(),
target, target,
best_match_subpath.unwrap(), best_match_subpath.unwrap(),
@ -315,7 +318,7 @@ fn throw_invalid_subpath(
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn resolve_package_target_string( fn resolve_package_target_string<Fs: NodeFs>(
target: String, target: String,
subpath: String, subpath: String,
match_: String, match_: String,
@ -354,7 +357,7 @@ fn resolve_package_target_string(
}; };
let package_json_url = let package_json_url =
ModuleSpecifier::from_file_path(package_json_path).unwrap(); ModuleSpecifier::from_file_path(package_json_path).unwrap();
return match package_resolve( return match package_resolve::<Fs>(
&export_target, &export_target,
&package_json_url, &package_json_url,
referrer_kind, referrer_kind,
@ -425,7 +428,7 @@ fn resolve_package_target_string(
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn resolve_package_target( fn resolve_package_target<Fs: NodeFs>(
package_json_path: &Path, package_json_path: &Path,
target: Value, target: Value,
subpath: String, subpath: String,
@ -440,7 +443,7 @@ fn resolve_package_target(
permissions: &mut dyn NodePermissions, permissions: &mut dyn NodePermissions,
) -> Result<Option<PathBuf>, AnyError> { ) -> Result<Option<PathBuf>, AnyError> {
if let Some(target) = target.as_str() { if let Some(target) = target.as_str() {
return resolve_package_target_string( return resolve_package_target_string::<Fs>(
target.to_string(), target.to_string(),
subpath, subpath,
package_subpath, package_subpath,
@ -456,7 +459,7 @@ fn resolve_package_target(
) )
.map(|path| { .map(|path| {
if mode.is_types() { if mode.is_types() {
path_to_declaration_path(path, referrer_kind) path_to_declaration_path::<Fs>(path, referrer_kind)
} else { } else {
Some(path) Some(path)
} }
@ -468,7 +471,7 @@ fn resolve_package_target(
let mut last_error = None; let mut last_error = None;
for target_item in target_arr { for target_item in target_arr {
let resolved_result = resolve_package_target( let resolved_result = resolve_package_target::<Fs>(
package_json_path, package_json_path,
target_item.to_owned(), target_item.to_owned(),
subpath.clone(), subpath.clone(),
@ -518,7 +521,7 @@ fn resolve_package_target(
{ {
let condition_target = target_obj.get(key).unwrap().to_owned(); let condition_target = target_obj.get(key).unwrap().to_owned();
let resolved = resolve_package_target( let resolved = resolve_package_target::<Fs>(
package_json_path, package_json_path,
condition_target, condition_target,
subpath.clone(), subpath.clone(),
@ -566,7 +569,7 @@ fn throw_exports_not_found(
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn package_exports_resolve( pub fn package_exports_resolve<Fs: NodeFs>(
package_json_path: &Path, package_json_path: &Path,
package_subpath: String, package_subpath: String,
package_exports: &Map<String, Value>, package_exports: &Map<String, Value>,
@ -582,7 +585,7 @@ pub fn package_exports_resolve(
&& !package_subpath.ends_with('/') && !package_subpath.ends_with('/')
{ {
let target = package_exports.get(&package_subpath).unwrap().to_owned(); let target = package_exports.get(&package_subpath).unwrap().to_owned();
let resolved = resolve_package_target( let resolved = resolve_package_target::<Fs>(
package_json_path, package_json_path,
target, target,
"".to_string(), "".to_string(),
@ -642,7 +645,7 @@ pub fn package_exports_resolve(
if !best_match.is_empty() { if !best_match.is_empty() {
let target = package_exports.get(best_match).unwrap().to_owned(); let target = package_exports.get(best_match).unwrap().to_owned();
let maybe_resolved = resolve_package_target( let maybe_resolved = resolve_package_target::<Fs>(
package_json_path, package_json_path,
target, target,
best_match_subpath.unwrap(), best_match_subpath.unwrap(),
@ -725,7 +728,7 @@ fn parse_package_name(
Ok((package_name, package_subpath, is_scoped)) Ok((package_name, package_subpath, is_scoped))
} }
pub fn package_resolve( pub fn package_resolve<Fs: NodeFs>(
specifier: &str, specifier: &str,
referrer: &ModuleSpecifier, referrer: &ModuleSpecifier,
referrer_kind: NodeModuleKind, referrer_kind: NodeModuleKind,
@ -739,12 +742,12 @@ pub fn package_resolve(
// ResolveSelf // ResolveSelf
let package_config = let package_config =
get_package_scope_config(referrer, npm_resolver, permissions)?; get_package_scope_config::<Fs>(referrer, npm_resolver, permissions)?;
if package_config.exists if package_config.exists
&& package_config.name.as_ref() == Some(&package_name) && package_config.name.as_ref() == Some(&package_name)
{ {
if let Some(exports) = &package_config.exports { if let Some(exports) = &package_config.exports {
return package_exports_resolve( return package_exports_resolve::<Fs>(
&package_config.path, &package_config.path,
package_subpath, package_subpath,
exports, exports,
@ -781,9 +784,9 @@ pub fn package_resolve(
// Package match. // Package match.
let package_json = let package_json =
PackageJson::load(npm_resolver, permissions, package_json_path)?; PackageJson::load::<Fs>(npm_resolver, permissions, package_json_path)?;
if let Some(exports) = &package_json.exports { if let Some(exports) = &package_json.exports {
return package_exports_resolve( return package_exports_resolve::<Fs>(
&package_json.path, &package_json.path,
package_subpath, package_subpath,
exports, exports,
@ -797,21 +800,21 @@ pub fn package_resolve(
.map(Some); .map(Some);
} }
if package_subpath == "." { if package_subpath == "." {
return legacy_main_resolve(&package_json, referrer_kind, mode); return legacy_main_resolve::<Fs>(&package_json, referrer_kind, mode);
} }
let file_path = package_json.path.parent().unwrap().join(&package_subpath); let file_path = package_json.path.parent().unwrap().join(&package_subpath);
if mode.is_types() { if mode.is_types() {
let maybe_declaration_path = let maybe_declaration_path =
path_to_declaration_path(file_path, referrer_kind); path_to_declaration_path::<Fs>(file_path, referrer_kind);
Ok(maybe_declaration_path) Ok(maybe_declaration_path)
} else { } else {
Ok(Some(file_path)) Ok(Some(file_path))
} }
} }
pub fn get_package_scope_config( pub fn get_package_scope_config<Fs: NodeFs>(
referrer: &ModuleSpecifier, referrer: &ModuleSpecifier,
npm_resolver: &dyn RequireNpmResolver, npm_resolver: &dyn RequireNpmResolver,
permissions: &mut dyn NodePermissions, permissions: &mut dyn NodePermissions,
@ -819,26 +822,27 @@ pub fn get_package_scope_config(
let root_folder = npm_resolver let root_folder = npm_resolver
.resolve_package_folder_from_path(&referrer.to_file_path().unwrap())?; .resolve_package_folder_from_path(&referrer.to_file_path().unwrap())?;
let package_json_path = root_folder.join("package.json"); let package_json_path = root_folder.join("package.json");
PackageJson::load(npm_resolver, permissions, package_json_path) PackageJson::load::<Fs>(npm_resolver, permissions, package_json_path)
} }
pub fn get_closest_package_json( pub fn get_closest_package_json<Fs: NodeFs>(
url: &ModuleSpecifier, url: &ModuleSpecifier,
npm_resolver: &dyn RequireNpmResolver, npm_resolver: &dyn RequireNpmResolver,
permissions: &mut dyn NodePermissions, permissions: &mut dyn NodePermissions,
) -> Result<PackageJson, AnyError> { ) -> Result<PackageJson, AnyError> {
let package_json_path = get_closest_package_json_path(url, npm_resolver)?; let package_json_path =
PackageJson::load(npm_resolver, permissions, package_json_path) get_closest_package_json_path::<Fs>(url, npm_resolver)?;
PackageJson::load::<Fs>(npm_resolver, permissions, package_json_path)
} }
fn get_closest_package_json_path( fn get_closest_package_json_path<Fs: NodeFs>(
url: &ModuleSpecifier, url: &ModuleSpecifier,
npm_resolver: &dyn RequireNpmResolver, npm_resolver: &dyn RequireNpmResolver,
) -> Result<PathBuf, AnyError> { ) -> Result<PathBuf, AnyError> {
let file_path = url.to_file_path().unwrap(); let file_path = url.to_file_path().unwrap();
let mut current_dir = file_path.parent().unwrap(); let mut current_dir = file_path.parent().unwrap();
let package_json_path = current_dir.join("package.json"); let package_json_path = current_dir.join("package.json");
if package_json_path.exists() { if Fs::metadata(&package_json_path).is_ok() {
return Ok(package_json_path); return Ok(package_json_path);
} }
let root_pkg_folder = npm_resolver let root_pkg_folder = npm_resolver
@ -846,7 +850,7 @@ fn get_closest_package_json_path(
while current_dir.starts_with(&root_pkg_folder) { while current_dir.starts_with(&root_pkg_folder) {
current_dir = current_dir.parent().unwrap(); current_dir = current_dir.parent().unwrap();
let package_json_path = current_dir.join("package.json"); let package_json_path = current_dir.join("package.json");
if package_json_path.exists() { if Fs::metadata(&package_json_path).is_ok() {
return Ok(package_json_path); return Ok(package_json_path);
} }
} }
@ -854,15 +858,15 @@ fn get_closest_package_json_path(
bail!("did not find package.json in {}", root_pkg_folder.display()) bail!("did not find package.json in {}", root_pkg_folder.display())
} }
fn file_exists(path: &Path) -> bool { fn file_exists<Fs: NodeFs>(path: &Path) -> bool {
if let Ok(stats) = std::fs::metadata(path) { if let Ok(stats) = Fs::metadata(path) {
stats.is_file() stats.is_file()
} else { } else {
false false
} }
} }
pub fn legacy_main_resolve( pub fn legacy_main_resolve<Fs: NodeFs>(
package_json: &PackageJson, package_json: &PackageJson,
referrer_kind: NodeModuleKind, referrer_kind: NodeModuleKind,
mode: NodeResolutionMode, mode: NodeResolutionMode,
@ -875,7 +879,9 @@ pub fn legacy_main_resolve(
// a corresponding declaration file // a corresponding declaration file
if let Some(main) = package_json.main(referrer_kind) { if let Some(main) = package_json.main(referrer_kind) {
let main = package_json.path.parent().unwrap().join(main).clean(); let main = package_json.path.parent().unwrap().join(main).clean();
if let Some(path) = path_to_declaration_path(main, referrer_kind) { if let Some(path) =
path_to_declaration_path::<Fs>(main, referrer_kind)
{
return Ok(Some(path)); return Ok(Some(path));
} }
} }
@ -888,7 +894,7 @@ pub fn legacy_main_resolve(
if let Some(main) = maybe_main { if let Some(main) = maybe_main {
let guess = package_json.path.parent().unwrap().join(main).clean(); let guess = package_json.path.parent().unwrap().join(main).clean();
if file_exists(&guess) { if file_exists::<Fs>(&guess) {
return Ok(Some(guess)); return Ok(Some(guess));
} }
@ -917,7 +923,7 @@ pub fn legacy_main_resolve(
.unwrap() .unwrap()
.join(format!("{main}{ending}")) .join(format!("{main}{ending}"))
.clean(); .clean();
if file_exists(&guess) { if file_exists::<Fs>(&guess) {
// TODO(bartlomieju): emitLegacyIndexDeprecation() // TODO(bartlomieju): emitLegacyIndexDeprecation()
return Ok(Some(guess)); return Ok(Some(guess));
} }
@ -940,7 +946,7 @@ pub fn legacy_main_resolve(
.unwrap() .unwrap()
.join(index_file_name) .join(index_file_name)
.clean(); .clean();
if file_exists(&guess) { if file_exists::<Fs>(&guess) {
// TODO(bartlomieju): emitLegacyIndexDeprecation() // TODO(bartlomieju): emitLegacyIndexDeprecation()
return Ok(Some(guess)); return Ok(Some(guess));
} }

View file

@ -222,6 +222,7 @@ mod startup_snapshot {
impl deno_node::NodeEnv for SnapshotNodeEnv { impl deno_node::NodeEnv for SnapshotNodeEnv {
type P = Permissions; type P = Permissions;
type Fs = deno_node::RealFs;
} }
deno_core::extension!(runtime, deno_core::extension!(runtime,

View file

@ -40,4 +40,5 @@ pub use worker_bootstrap::BootstrapOptions;
pub struct RuntimeNodeEnv; pub struct RuntimeNodeEnv;
impl deno_node::NodeEnv for RuntimeNodeEnv { impl deno_node::NodeEnv for RuntimeNodeEnv {
type P = permissions::PermissionsContainer; type P = permissions::PermissionsContainer;
type Fs = deno_node::RealFs;
} }