refactor: break up ProcState (#18707)

1. Breaks up functionality within `ProcState` into several other structs
to break out the responsibilities (`ProcState` is only a data struct
now).
2. Moves towards being able to inject dependencies more easily and have
functionality only require what it needs.
3. Exposes `Arc<T>` around the "service structs" instead of it being
embedded within them. The idea behind embedding them was to reduce the
verbosity of needing to pass around `Arc<...>`, but I don't think it was
exactly working and as we move more of these structs to be more
injectable I don't think the extra verbosity will be a big deal.
This commit is contained in:
David Sherret 2023-04-14 16:22:33 -04:00 committed by GitHub
parent a411144219
commit 136dce67ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 1506 additions and 1285 deletions

View file

@ -8,6 +8,7 @@ use std::collections::VecDeque;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use crate::util::fs::symlink_dir;
use crate::util::fs::LaxSingleProcessFsFlag;
@ -41,11 +42,11 @@ use super::common::NpmPackageFsResolver;
/// Resolver that creates a local node_modules directory
/// and resolves packages from it.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct LocalNpmPackageResolver {
cache: NpmCache,
cache: Arc<NpmCache>,
progress_bar: ProgressBar,
resolution: NpmResolution,
resolution: Arc<NpmResolution>,
registry_url: Url,
root_node_modules_path: PathBuf,
root_node_modules_url: Url,
@ -53,11 +54,11 @@ pub struct LocalNpmPackageResolver {
impl LocalNpmPackageResolver {
pub fn new(
cache: NpmCache,
cache: Arc<NpmCache>,
progress_bar: ProgressBar,
registry_url: Url,
node_modules_folder: PathBuf,
resolution: NpmResolution,
resolution: Arc<NpmResolution>,
) -> Self {
Self {
cache,
@ -103,11 +104,19 @@ impl LocalNpmPackageResolver {
// it's within the directory, so use it
specifier.to_file_path().ok()
}
}
fn get_package_id_folder(
&self,
id: &NpmPackageId,
) -> Result<PathBuf, AnyError> {
#[async_trait]
impl NpmPackageFsResolver for LocalNpmPackageResolver {
fn root_dir_url(&self) -> &Url {
&self.root_node_modules_url
}
fn node_modules_path(&self) -> Option<PathBuf> {
Some(self.root_node_modules_path.clone())
}
fn package_folder(&self, id: &NpmPackageId) -> Result<PathBuf, AnyError> {
match self.resolution.resolve_package_cache_folder_id_from_id(id) {
// package is stored at:
// node_modules/.deno/<package_cache_folder_id_folder_name>/node_modules/<package_name>
@ -125,24 +134,6 @@ impl LocalNpmPackageResolver {
),
}
}
}
#[async_trait]
impl NpmPackageFsResolver for LocalNpmPackageResolver {
fn root_dir_url(&self) -> &Url {
&self.root_node_modules_url
}
fn node_modules_path(&self) -> Option<PathBuf> {
Some(self.root_node_modules_path.clone())
}
fn resolve_package_folder_from_deno_module(
&self,
node_id: &NpmPackageId,
) -> Result<PathBuf, AnyError> {
self.get_package_id_folder(node_id)
}
fn resolve_package_folder_from_package(
&self,
@ -198,12 +189,6 @@ impl NpmPackageFsResolver for LocalNpmPackageResolver {
Ok(package_root_path)
}
fn package_size(&self, id: &NpmPackageId) -> Result<u64, AnyError> {
let package_folder_path = self.get_package_id_folder(id)?;
Ok(crate::util::fs::dir_size(&package_folder_path)?)
}
async fn cache_packages(&self) -> Result<(), AnyError> {
sync_resolution_with_fs(
&self.resolution.snapshot(),
@ -231,7 +216,7 @@ impl NpmPackageFsResolver for LocalNpmPackageResolver {
/// Creates a pnpm style folder structure.
async fn sync_resolution_with_fs(
snapshot: &NpmResolutionSnapshot,
cache: &NpmCache,
cache: &Arc<NpmCache>,
progress_bar: &ProgressBar,
registry_url: &Url,
root_node_modules_dir_path: &Path,