2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-09-13 11:59:01 -04:00
|
|
|
|
|
|
|
mod common;
|
|
|
|
mod global;
|
2022-09-22 11:17:02 -04:00
|
|
|
mod local;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2023-04-06 18:46:44 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_ast::ModuleSpecifier;
|
2022-09-13 11:59:01 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-10-25 12:20:07 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2022-09-28 13:04:16 -04:00
|
|
|
use deno_core::serde_json;
|
2023-03-12 23:32:59 -04:00
|
|
|
use deno_core::url::Url;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::resolution::NpmResolutionSnapshot;
|
2023-04-06 21:41:19 -04:00
|
|
|
use deno_npm::resolution::PackageReqNotFoundError;
|
2023-04-13 10:47:45 -04:00
|
|
|
use deno_npm::resolution::SerializedNpmResolutionSnapshot;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::NpmPackageId;
|
2023-05-17 17:38:50 -04:00
|
|
|
use deno_npm::NpmSystemInfo;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_runtime::deno_fs::FileSystem;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_runtime::deno_node::NodePermissions;
|
2022-11-30 18:07:32 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2023-04-21 21:02:46 -04:00
|
|
|
use deno_runtime::deno_node::NpmResolver;
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_runtime::deno_node::PathClean;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageNv;
|
|
|
|
use deno_semver::npm::NpmPackageReq;
|
2022-09-13 11:59:01 -04:00
|
|
|
use global::GlobalNpmPackageResolver;
|
2022-09-28 13:04:16 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2022-11-25 17:00:28 -05:00
|
|
|
use crate::args::Lockfile;
|
2023-05-10 20:06:59 -04:00
|
|
|
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
|
2023-03-13 14:18:29 -04:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
use self::local::LocalNpmPackageResolver;
|
2023-02-22 14:15:25 -05:00
|
|
|
use super::resolution::NpmResolution;
|
2022-09-13 11:59:01 -04:00
|
|
|
use super::NpmCache;
|
|
|
|
|
2023-05-19 18:39:27 -04:00
|
|
|
pub use self::common::NpmPackageFsResolver;
|
|
|
|
|
2022-09-28 13:04:16 -04:00
|
|
|
/// State provided to the process via an environment variable.
|
2023-02-20 13:14:06 -05:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct NpmProcessState {
|
2023-04-13 10:47:45 -04:00
|
|
|
pub snapshot: SerializedNpmResolutionSnapshot,
|
2023-02-20 13:14:06 -05:00
|
|
|
pub local_node_modules_path: Option<String>,
|
2022-09-28 13:04:16 -04:00
|
|
|
}
|
|
|
|
|
2023-03-12 23:32:59 -04:00
|
|
|
/// Brings together the npm resolution with the file system.
|
2023-04-21 21:02:46 -04:00
|
|
|
pub struct CliNpmResolver {
|
2023-05-10 20:06:59 -04:00
|
|
|
fs: Arc<dyn FileSystem>,
|
2023-02-22 14:15:25 -05:00
|
|
|
fs_resolver: Arc<dyn NpmPackageFsResolver>,
|
2023-04-14 16:22:33 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2022-10-25 12:20:07 -04:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 21:02:46 -04:00
|
|
|
impl std::fmt::Debug for CliNpmResolver {
|
2022-10-21 11:20:18 -04:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("NpmPackageResolver")
|
2023-05-10 20:06:59 -04:00
|
|
|
.field("fs", &"<omitted>")
|
2023-02-22 14:15:25 -05:00
|
|
|
.field("fs_resolver", &"<omitted>")
|
|
|
|
.field("resolution", &"<omitted>")
|
|
|
|
.field("maybe_lockfile", &"<omitted>")
|
2022-10-21 11:20:18 -04:00
|
|
|
.finish()
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 21:02:46 -04:00
|
|
|
impl CliNpmResolver {
|
2023-03-12 23:32:59 -04:00
|
|
|
pub fn new(
|
2023-05-10 20:06:59 -04:00
|
|
|
fs: Arc<dyn FileSystem>,
|
2023-04-14 16:22:33 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2023-03-12 23:32:59 -04:00
|
|
|
fs_resolver: Arc<dyn NpmPackageFsResolver>,
|
2023-01-24 09:05:54 -05:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2022-09-13 11:59:01 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2023-05-10 20:06:59 -04:00
|
|
|
fs,
|
2023-02-22 14:15:25 -05:00
|
|
|
fs_resolver,
|
|
|
|
resolution,
|
2023-01-24 09:05:54 -05:00
|
|
|
maybe_lockfile,
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 13:40:22 -04:00
|
|
|
pub fn root_dir_url(&self) -> &Url {
|
|
|
|
self.fs_resolver.root_dir_url()
|
|
|
|
}
|
|
|
|
|
2023-05-10 20:06:59 -04:00
|
|
|
pub fn node_modules_path(&self) -> Option<PathBuf> {
|
|
|
|
self.fs_resolver.node_modules_path()
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:28:36 -04:00
|
|
|
/// Checks if the provided package req's folder is cached.
|
|
|
|
pub fn is_pkg_req_folder_cached(&self, req: &NpmPackageReq) -> bool {
|
|
|
|
self
|
|
|
|
.resolve_pkg_id_from_pkg_req(req)
|
|
|
|
.ok()
|
|
|
|
.and_then(|id| self.fs_resolver.package_folder(&id).ok())
|
|
|
|
.map(|folder| folder.exists())
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2023-03-12 23:32:59 -04:00
|
|
|
pub fn resolve_pkg_id_from_pkg_req(
|
|
|
|
&self,
|
|
|
|
req: &NpmPackageReq,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<NpmPackageId, PackageReqNotFoundError> {
|
2023-03-12 23:32:59 -04:00
|
|
|
self.resolution.resolve_pkg_id_from_pkg_req(req)
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
|
2023-05-10 20:06:59 -04:00
|
|
|
pub fn resolve_pkg_folder_from_pkg_id(
|
2023-02-22 14:15:25 -05:00
|
|
|
&self,
|
2023-02-22 22:45:35 -05:00
|
|
|
pkg_id: &NpmPackageId,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2023-04-14 16:22:33 -04:00
|
|
|
let path = self.fs_resolver.package_folder(pkg_id)?;
|
2023-05-10 20:06:59 -04:00
|
|
|
let path = canonicalize_path_maybe_not_exists_with_fs(&path, |path| {
|
|
|
|
self
|
|
|
|
.fs
|
|
|
|
.realpath_sync(path)
|
|
|
|
.map_err(|err| err.into_io_error())
|
|
|
|
})?;
|
2022-12-05 20:09:31 -05:00
|
|
|
log::debug!(
|
|
|
|
"Resolved package folder of {} to {}",
|
2023-02-22 22:45:35 -05:00
|
|
|
pkg_id.as_serialized(),
|
2022-12-05 20:09:31 -05:00
|
|
|
path.display()
|
|
|
|
);
|
2022-09-22 11:17:02 -04:00
|
|
|
Ok(path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve the root folder of the package the provided specifier is in.
|
|
|
|
///
|
|
|
|
/// This will error when the provided specifier is not in an npm package.
|
2022-09-22 11:17:02 -04:00
|
|
|
pub fn resolve_package_folder_from_specifier(
|
2022-09-13 11:59:01 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
|
|
|
let path = self
|
2023-02-22 14:15:25 -05:00
|
|
|
.fs_resolver
|
2022-09-22 11:17:02 -04:00
|
|
|
.resolve_package_folder_from_specifier(specifier)?;
|
2022-12-05 20:09:31 -05:00
|
|
|
log::debug!(
|
|
|
|
"Resolved package folder of {} to {}",
|
|
|
|
specifier,
|
|
|
|
path.display()
|
|
|
|
);
|
2022-09-22 11:17:02 -04:00
|
|
|
Ok(path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-07-01 21:07:57 -04:00
|
|
|
/// Resolves the package nv from the provided specifier.
|
|
|
|
pub fn resolve_package_id_from_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<NpmPackageId, AnyError> {
|
|
|
|
let cache_folder_id = self
|
|
|
|
.fs_resolver
|
|
|
|
.resolve_package_cache_folder_id_from_specifier(specifier)?;
|
|
|
|
Ok(
|
|
|
|
self
|
|
|
|
.resolution
|
|
|
|
.resolve_pkg_id_from_pkg_cache_folder_id(&cache_folder_id)?,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
/// Attempts to get the package size in bytes.
|
|
|
|
pub fn package_size(
|
|
|
|
&self,
|
2023-02-21 12:03:48 -05:00
|
|
|
package_id: &NpmPackageId,
|
2022-10-28 16:19:55 -04:00
|
|
|
) -> Result<u64, AnyError> {
|
2023-04-14 16:22:33 -04:00
|
|
|
let package_folder = self.fs_resolver.package_folder(package_id)?;
|
|
|
|
Ok(crate::util::fs::dir_size(&package_folder)?)
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
|
2022-09-13 11:59:01 -04:00
|
|
|
/// Gets if the provided specifier is in an npm package.
|
|
|
|
pub fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
|
2023-02-23 10:58:10 -05:00
|
|
|
let root_dir_url = self.fs_resolver.root_dir_url();
|
|
|
|
debug_assert!(root_dir_url.as_str().ends_with('/'));
|
|
|
|
specifier.as_ref().starts_with(root_dir_url.as_str())
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the resolver has resolved any npm packages.
|
|
|
|
pub fn has_packages(&self) -> bool {
|
2023-02-22 14:15:25 -05:00
|
|
|
self.resolution.has_packages()
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
/// Adds package requirements to the resolver and ensures everything is setup.
|
2022-09-13 11:59:01 -04:00
|
|
|
pub async fn add_package_reqs(
|
|
|
|
&self,
|
2023-05-22 16:55:04 -04:00
|
|
|
packages: &[NpmPackageReq],
|
2022-09-13 11:59:01 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-10-21 11:20:18 -04:00
|
|
|
if packages.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
self.resolution.add_package_reqs(packages).await?;
|
|
|
|
self.fs_resolver.cache_packages().await?;
|
2022-10-25 12:20:07 -04:00
|
|
|
|
|
|
|
// If there's a lock file, update it with all discovered npm packages
|
|
|
|
if let Some(lockfile_mutex) = &self.maybe_lockfile {
|
|
|
|
let mut lockfile = lockfile_mutex.lock();
|
|
|
|
self.lock(&mut lockfile)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
2022-09-28 13:04:16 -04:00
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
/// Sets package requirements to the resolver, removing old requirements and adding new ones.
|
2022-12-19 20:22:17 -05:00
|
|
|
///
|
|
|
|
/// This will retrieve and resolve package information, but not cache any package files.
|
2022-10-21 11:20:18 -04:00
|
|
|
pub async fn set_package_reqs(
|
|
|
|
&self,
|
2023-05-22 16:55:04 -04:00
|
|
|
packages: &[NpmPackageReq],
|
2022-10-21 11:20:18 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2023-02-22 14:15:25 -05:00
|
|
|
self.resolution.set_package_reqs(packages).await
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
2022-09-28 13:04:16 -04:00
|
|
|
/// Gets the state of npm for the process.
|
|
|
|
pub fn get_npm_process_state(&self) -> String {
|
|
|
|
serde_json::to_string(&NpmProcessState {
|
2023-06-08 11:48:29 -04:00
|
|
|
snapshot: self
|
|
|
|
.resolution
|
|
|
|
.serialized_valid_snapshot()
|
|
|
|
.into_serialized(),
|
2022-09-28 13:04:16 -04:00
|
|
|
local_node_modules_path: self
|
2023-03-12 23:32:59 -04:00
|
|
|
.fs_resolver
|
|
|
|
.node_modules_path()
|
2022-09-28 13:04:16 -04:00
|
|
|
.map(|p| p.to_string_lossy().to_string()),
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
2022-10-21 11:20:18 -04:00
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
pub fn snapshot(&self) -> NpmResolutionSnapshot {
|
2023-02-22 14:15:25 -05:00
|
|
|
self.resolution.snapshot()
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:20:07 -04:00
|
|
|
pub fn lock(&self, lockfile: &mut Lockfile) -> Result<(), AnyError> {
|
2023-02-22 14:15:25 -05:00
|
|
|
self.resolution.lock(lockfile)
|
2022-10-25 12:20:07 -04:00
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
|
|
|
|
pub async fn inject_synthetic_types_node_package(
|
|
|
|
&self,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
// add and ensure this isn't added to the lockfile
|
2023-04-06 21:41:19 -04:00
|
|
|
let package_reqs = vec![NpmPackageReq::from_str("@types/node").unwrap()];
|
2023-05-22 16:55:04 -04:00
|
|
|
self.resolution.add_package_reqs(&package_reqs).await?;
|
2023-02-22 14:15:25 -05:00
|
|
|
self.fs_resolver.cache_packages().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
pub async fn resolve_pending(&self) -> Result<(), AnyError> {
|
|
|
|
self.resolution.resolve_pending().await?;
|
|
|
|
self.fs_resolver.cache_packages().await?;
|
2023-01-24 09:05:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-04-21 21:02:46 -04:00
|
|
|
impl NpmResolver for CliNpmResolver {
|
2022-09-13 11:59:01 -04:00
|
|
|
fn resolve_package_folder_from_package(
|
|
|
|
&self,
|
2023-04-21 21:02:46 -04:00
|
|
|
name: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2022-11-30 18:07:32 -05:00
|
|
|
mode: NodeResolutionMode,
|
2022-09-13 11:59:01 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2023-04-21 21:02:46 -04:00
|
|
|
let path = self
|
|
|
|
.fs_resolver
|
|
|
|
.resolve_package_folder_from_package(name, referrer, mode)?;
|
|
|
|
log::debug!("Resolved {} from {} to {}", name, referrer, path.display());
|
|
|
|
Ok(path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_package_folder_from_path(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
) -> Result<PathBuf, AnyError> {
|
2022-09-22 11:17:02 -04:00
|
|
|
let specifier = path_to_specifier(path)?;
|
2023-04-21 21:02:46 -04:00
|
|
|
self.resolve_package_folder_from_specifier(&specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_package_folder_from_deno_module(
|
|
|
|
&self,
|
|
|
|
pkg_nv: &NpmPackageNv,
|
|
|
|
) -> Result<PathBuf, AnyError> {
|
|
|
|
let pkg_id = self.resolution.resolve_pkg_id_from_deno_module(pkg_nv)?;
|
2023-05-10 20:06:59 -04:00
|
|
|
self.resolve_pkg_folder_from_pkg_id(&pkg_id)
|
2023-04-21 21:02:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_pkg_id_from_pkg_req(
|
|
|
|
&self,
|
|
|
|
req: &NpmPackageReq,
|
|
|
|
) -> Result<NpmPackageId, PackageReqNotFoundError> {
|
|
|
|
self.resolution.resolve_pkg_id_from_pkg_req(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
|
2022-09-22 11:17:02 -04:00
|
|
|
self
|
2023-04-21 21:02:46 -04:00
|
|
|
.resolve_package_folder_from_specifier(specifier)
|
2022-09-22 11:17:02 -04:00
|
|
|
.is_ok()
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-01-10 08:35:44 -05:00
|
|
|
fn ensure_read_permission(
|
|
|
|
&self,
|
2023-04-24 21:07:48 -04:00
|
|
|
permissions: &dyn NodePermissions,
|
2023-01-10 08:35:44 -05:00
|
|
|
path: &Path,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-04-21 21:02:46 -04:00
|
|
|
self.fs_resolver.ensure_read_permission(permissions, path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 23:32:59 -04:00
|
|
|
pub fn create_npm_fs_resolver(
|
2023-05-10 20:06:59 -04:00
|
|
|
fs: Arc<dyn FileSystem>,
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: Arc<NpmCache>,
|
2023-03-13 14:18:29 -04:00
|
|
|
progress_bar: &ProgressBar,
|
2023-03-12 23:32:59 -04:00
|
|
|
registry_url: Url,
|
2023-04-14 16:22:33 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2023-03-12 23:32:59 -04:00
|
|
|
maybe_node_modules_path: Option<PathBuf>,
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info: NpmSystemInfo,
|
2023-03-12 23:32:59 -04:00
|
|
|
) -> Arc<dyn NpmPackageFsResolver> {
|
|
|
|
match maybe_node_modules_path {
|
|
|
|
Some(node_modules_folder) => Arc::new(LocalNpmPackageResolver::new(
|
2023-04-24 19:44:35 -04:00
|
|
|
fs,
|
2023-03-12 23:32:59 -04:00
|
|
|
cache,
|
2023-03-13 14:18:29 -04:00
|
|
|
progress_bar.clone(),
|
2023-03-12 23:32:59 -04:00
|
|
|
registry_url,
|
|
|
|
node_modules_folder,
|
|
|
|
resolution,
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info,
|
2023-03-12 23:32:59 -04:00
|
|
|
)),
|
|
|
|
None => Arc::new(GlobalNpmPackageResolver::new(
|
2023-05-10 20:06:59 -04:00
|
|
|
fs,
|
2023-03-12 23:32:59 -04:00
|
|
|
cache,
|
|
|
|
registry_url,
|
|
|
|
resolution,
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info,
|
2023-03-12 23:32:59 -04:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
fn path_to_specifier(path: &Path) -> Result<ModuleSpecifier, AnyError> {
|
2022-12-17 17:20:15 -05:00
|
|
|
match ModuleSpecifier::from_file_path(path.to_path_buf().clean()) {
|
2022-09-13 11:59:01 -04:00
|
|
|
Ok(specifier) => Ok(specifier),
|
|
|
|
Err(()) => bail!("Could not convert '{}' to url.", path.display()),
|
|
|
|
}
|
|
|
|
}
|