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]] [[package]]
name = "sys_traits" name = "sys_traits"
version = "0.1.6" version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c12873696bde6de3aea3cd27de8e52897177c5b368a6a30987fd4926e30f85" checksum = "5b46ac05dfbe9fd3a9703eff20e17f5b31e7b6a54daf27a421dcd56c7a27ecdd"
dependencies = [ dependencies = [
"filetime", "filetime",
"getrandom", "getrandom",

View file

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

View file

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

View file

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

View file

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

View file

@ -5,7 +5,6 @@
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use async_trait::async_trait;
use deno_npm::NpmPackageCacheFolderId; use deno_npm::NpmPackageCacheFolderId;
use deno_npm::NpmPackageId; use deno_npm::NpmPackageId;
use deno_semver::package::PackageNv; use deno_semver::package::PackageNv;
@ -14,6 +13,7 @@ use deno_semver::Version;
use node_resolver::errors::PackageFolderResolveError; use node_resolver::errors::PackageFolderResolveError;
use node_resolver::errors::PackageNotFoundError; use node_resolver::errors::PackageNotFoundError;
use node_resolver::errors::ReferrerNotFoundError; use node_resolver::errors::ReferrerNotFoundError;
use node_resolver::NpmPackageFolderResolver;
use url::Url; use url::Url;
use super::resolution::NpmResolutionRc; use super::resolution::NpmResolutionRc;
@ -61,25 +61,7 @@ impl GlobalNpmPackageResolver {
} }
} }
#[async_trait(?Send)] impl NpmPackageFolderResolver 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_folder_from_package( fn resolve_package_folder_from_package(
&self, &self,
name: &str, 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( fn resolve_package_cache_folder_id_from_specifier(
&self, &self,

View file

@ -6,7 +6,6 @@ use std::borrow::Cow;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use async_trait::async_trait;
use deno_npm::NpmPackageCacheFolderId; use deno_npm::NpmPackageCacheFolderId;
use deno_npm::NpmPackageId; use deno_npm::NpmPackageId;
use deno_path_util::fs::canonicalize_path_maybe_not_exists; 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::PackageFolderResolveIoError;
use node_resolver::errors::PackageNotFoundError; use node_resolver::errors::PackageNotFoundError;
use node_resolver::errors::ReferrerNotFoundError; use node_resolver::errors::ReferrerNotFoundError;
use node_resolver::NpmPackageFolderResolver;
use sys_traits::FsCanonicalize; use sys_traits::FsCanonicalize;
use sys_traits::FsMetadata; use sys_traits::FsMetadata;
use url::Url; use url::Url;
@ -99,33 +99,9 @@ impl<TSys: FsCanonicalize + FsMetadata + Send + Sync>
} }
} }
#[async_trait(?Send)] impl<TSys: FsCanonicalize + FsMetadata + Send + Sync + std::fmt::Debug>
impl<TSys: FsCanonicalize + FsMetadata + Send + Sync> NpmPackageFsResolver NpmPackageFolderResolver for LocalNpmPackageResolver<TSys>
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( fn resolve_package_folder_from_package(
&self, &self,
name: &str, name: &str,
@ -159,7 +135,13 @@ impl<TSys: FsCanonicalize + FsMetadata + Send + Sync> NpmPackageFsResolver
let sub_dir = join_package_name(&node_modules_folder, name); let sub_dir = join_package_name(&node_modules_folder, name);
if self.sys.fs_is_dir_no_err(&sub_dir) { 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 { if current_folder == self.root_node_modules_path {
@ -176,6 +158,33 @@ impl<TSys: FsCanonicalize + FsMetadata + Send + Sync> NpmPackageFsResolver
.into(), .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( fn resolve_package_cache_folder_id_from_specifier(
&self, &self,

View file

@ -25,7 +25,7 @@ use crate::NpmCacheDirRc;
use crate::ResolvedNpmRcRc; use crate::ResolvedNpmRcRc;
pub fn create_npm_fs_resolver< 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_cache_dir: &NpmCacheDirRc,
npm_rc: &ResolvedNpmRcRc, npm_rc: &ResolvedNpmRcRc,