1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00

refactor: implement NpmPackageFolderResolver in deno_resolver (#27614)

This commit is contained in:
David Sherret 2025-01-09 19:01:47 -05:00 committed by GitHub
parent 34beeb7703
commit 475793f94d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 69 additions and 65 deletions

4
Cargo.lock generated
View file

@ -7762,9 +7762,9 @@ dependencies = [
[[package]]
name = "sys_traits"
version = "0.1.6"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c12873696bde6de3aea3cd27de8e52897177c5b368a6a30987fd4926e30f85"
checksum = "5b46ac05dfbe9fd3a9703eff20e17f5b31e7b6a54daf27a421dcd56c7a27ecdd"
dependencies = [
"filetime",
"getrandom",

View file

@ -193,7 +193,7 @@ slab = "0.4"
smallvec = "1.8"
socket2 = { version = "0.5.3", features = ["all"] }
spki = "0.7.2"
sys_traits = "=0.1.6"
sys_traits = "=0.1.7"
tar = "=0.4.40"
tempfile = "3.4.0"
termcolor = "1.1.3"

View file

@ -136,7 +136,7 @@ pub enum InnerCliNpmResolverRef<'a> {
Byonm(&'a CliByonmNpmResolver),
}
pub trait CliNpmResolver: NpmPackageFolderResolver + CliNpmReqResolver {
pub trait CliNpmResolver: CliNpmReqResolver {
fn into_npm_pkg_folder_resolver(
self: Arc<Self>,
) -> Arc<dyn NpmPackageFolderResolver>;

View file

@ -400,14 +400,14 @@ impl<
}
impl<
Sys: FsCanonicalize
TSys: FsCanonicalize
+ FsMetadata
+ FsRead
+ FsReadDir
+ Send
+ Sync
+ std::fmt::Debug,
> NpmPackageFolderResolver for ByonmNpmResolver<Sys>
> NpmPackageFolderResolver for ByonmNpmResolver<TSys>
{
fn resolve_package_folder_from_package(
&self,

View file

@ -3,10 +3,9 @@
use std::path::Path;
use std::path::PathBuf;
use async_trait::async_trait;
use deno_npm::NpmPackageCacheFolderId;
use deno_npm::NpmPackageId;
use node_resolver::errors::PackageFolderResolveError;
use node_resolver::NpmPackageFolderResolver;
use url::Url;
use crate::sync::MaybeSend;
@ -22,8 +21,9 @@ pub(super) type NpmPackageFsResolverRc =
pub struct NpmPackageFsResolverPackageFolderError(deno_semver::StackString);
/// Part of the resolution that interacts with the file system.
#[async_trait(?Send)]
pub trait NpmPackageFsResolver: MaybeSend + MaybeSync {
pub trait NpmPackageFsResolver:
NpmPackageFolderResolver + MaybeSend + MaybeSync
{
/// The local node_modules folder if it is applicable to the implementation.
fn node_modules_path(&self) -> Option<&Path>;
@ -38,12 +38,6 @@ pub trait NpmPackageFsResolver: MaybeSend + MaybeSync {
})
}
fn resolve_package_folder_from_package(
&self,
name: &str,
referrer: &Url,
) -> Result<PathBuf, PackageFolderResolveError>;
fn resolve_package_cache_folder_id_from_specifier(
&self,
specifier: &Url,

View file

@ -5,7 +5,6 @@
use std::path::Path;
use std::path::PathBuf;
use async_trait::async_trait;
use deno_npm::NpmPackageCacheFolderId;
use deno_npm::NpmPackageId;
use deno_semver::package::PackageNv;
@ -14,6 +13,7 @@ use deno_semver::Version;
use node_resolver::errors::PackageFolderResolveError;
use node_resolver::errors::PackageNotFoundError;
use node_resolver::errors::ReferrerNotFoundError;
use node_resolver::NpmPackageFolderResolver;
use url::Url;
use super::resolution::NpmResolutionRc;
@ -61,25 +61,7 @@ impl GlobalNpmPackageResolver {
}
}
#[async_trait(?Send)]
impl NpmPackageFsResolver for GlobalNpmPackageResolver {
fn node_modules_path(&self) -> Option<&Path> {
None
}
fn maybe_package_folder(&self, id: &NpmPackageId) -> Option<PathBuf> {
let folder_copy_index = self
.resolution
.resolve_pkg_cache_folder_copy_index_from_pkg_id(id)?;
let registry_url = self.npm_rc.get_registry_url(&id.nv.name);
Some(self.cache.package_folder_for_id(
&id.nv.name,
&id.nv.version.to_string(),
folder_copy_index,
registry_url,
))
}
impl NpmPackageFolderResolver for GlobalNpmPackageResolver {
fn resolve_package_folder_from_package(
&self,
name: &str,
@ -138,6 +120,25 @@ impl NpmPackageFsResolver for GlobalNpmPackageResolver {
},
}
}
}
impl NpmPackageFsResolver for GlobalNpmPackageResolver {
fn node_modules_path(&self) -> Option<&Path> {
None
}
fn maybe_package_folder(&self, id: &NpmPackageId) -> Option<PathBuf> {
let folder_copy_index = self
.resolution
.resolve_pkg_cache_folder_copy_index_from_pkg_id(id)?;
let registry_url = self.npm_rc.get_registry_url(&id.nv.name);
Some(self.cache.package_folder_for_id(
&id.nv.name,
&id.nv.version.to_string(),
folder_copy_index,
registry_url,
))
}
fn resolve_package_cache_folder_id_from_specifier(
&self,

View file

@ -6,7 +6,6 @@ use std::borrow::Cow;
use std::path::Path;
use std::path::PathBuf;
use async_trait::async_trait;
use deno_npm::NpmPackageCacheFolderId;
use deno_npm::NpmPackageId;
use deno_path_util::fs::canonicalize_path_maybe_not_exists;
@ -15,6 +14,7 @@ use node_resolver::errors::PackageFolderResolveError;
use node_resolver::errors::PackageFolderResolveIoError;
use node_resolver::errors::PackageNotFoundError;
use node_resolver::errors::ReferrerNotFoundError;
use node_resolver::NpmPackageFolderResolver;
use sys_traits::FsCanonicalize;
use sys_traits::FsMetadata;
use url::Url;
@ -99,33 +99,9 @@ impl<TSys: FsCanonicalize + FsMetadata + Send + Sync>
}
}
#[async_trait(?Send)]
impl<TSys: FsCanonicalize + FsMetadata + Send + Sync> NpmPackageFsResolver
for LocalNpmPackageResolver<TSys>
impl<TSys: FsCanonicalize + FsMetadata + Send + Sync + std::fmt::Debug>
NpmPackageFolderResolver for LocalNpmPackageResolver<TSys>
{
fn node_modules_path(&self) -> Option<&Path> {
Some(self.root_node_modules_path.as_ref())
}
fn maybe_package_folder(&self, id: &NpmPackageId) -> Option<PathBuf> {
let folder_copy_index = self
.resolution
.resolve_pkg_cache_folder_copy_index_from_pkg_id(id)?;
// package is stored at:
// node_modules/.deno/<package_cache_folder_id_folder_name>/node_modules/<package_name>
Some(
self
.root_node_modules_path
.join(".deno")
.join(get_package_folder_id_folder_name_from_parts(
&id.nv,
folder_copy_index,
))
.join("node_modules")
.join(&id.nv.name),
)
}
fn resolve_package_folder_from_package(
&self,
name: &str,
@ -159,7 +135,13 @@ impl<TSys: FsCanonicalize + FsMetadata + Send + Sync> NpmPackageFsResolver
let sub_dir = join_package_name(&node_modules_folder, name);
if self.sys.fs_is_dir_no_err(&sub_dir) {
return Ok(sub_dir);
return Ok(self.sys.fs_canonicalize(&sub_dir).map_err(|err| {
PackageFolderResolveIoError {
package_name: name.to_string(),
referrer: referrer.clone(),
source: err,
}
})?);
}
if current_folder == self.root_node_modules_path {
@ -176,6 +158,33 @@ impl<TSys: FsCanonicalize + FsMetadata + Send + Sync> NpmPackageFsResolver
.into(),
)
}
}
impl<TSys: FsCanonicalize + FsMetadata + Send + Sync + std::fmt::Debug>
NpmPackageFsResolver for LocalNpmPackageResolver<TSys>
{
fn node_modules_path(&self) -> Option<&Path> {
Some(self.root_node_modules_path.as_ref())
}
fn maybe_package_folder(&self, id: &NpmPackageId) -> Option<PathBuf> {
let folder_copy_index = self
.resolution
.resolve_pkg_cache_folder_copy_index_from_pkg_id(id)?;
// package is stored at:
// node_modules/.deno/<package_cache_folder_id_folder_name>/node_modules/<package_name>
Some(
self
.root_node_modules_path
.join(".deno")
.join(get_package_folder_id_folder_name_from_parts(
&id.nv,
folder_copy_index,
))
.join("node_modules")
.join(&id.nv.name),
)
}
fn resolve_package_cache_folder_id_from_specifier(
&self,

View file

@ -25,7 +25,7 @@ use crate::NpmCacheDirRc;
use crate::ResolvedNpmRcRc;
pub fn create_npm_fs_resolver<
TSys: FsCanonicalize + FsMetadata + Send + Sync + 'static,
TSys: FsCanonicalize + FsMetadata + std::fmt::Debug + Send + Sync + 'static,
>(
npm_cache_dir: &NpmCacheDirRc,
npm_rc: &ResolvedNpmRcRc,