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
|
|
|
|
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-11-08 14:17:24 -05:00
|
|
|
use deno_core::anyhow::Context;
|
2022-09-13 11:59:01 -04:00
|
|
|
use deno_core::error::custom_error;
|
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-02-15 13:20:40 -05:00
|
|
|
use deno_graph::npm::NpmPackageReq;
|
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;
|
2022-09-22 11:17:02 -04:00
|
|
|
use deno_runtime::deno_node::PathClean;
|
2022-09-13 11:59:01 -04:00
|
|
|
use deno_runtime::deno_node::RequireNpmResolver;
|
|
|
|
use global::GlobalNpmPackageResolver;
|
2022-09-28 13:04:16 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2022-10-21 11:20:18 -04:00
|
|
|
use std::collections::HashSet;
|
2022-09-13 11:59:01 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-11-25 17:00:28 -05:00
|
|
|
use crate::args::Lockfile;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::fs::canonicalize_path_maybe_not_exists;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
|
|
|
use self::common::InnerNpmPackageResolver;
|
2022-09-22 11:17:02 -04:00
|
|
|
use self::local::LocalNpmPackageResolver;
|
2022-09-13 11:59:01 -04:00
|
|
|
use super::NpmCache;
|
2023-02-17 09:12:22 -05:00
|
|
|
use super::NpmPackageNodeId;
|
2022-10-21 11:20:18 -04:00
|
|
|
use super::NpmResolutionSnapshot;
|
2022-11-08 14:17:24 -05:00
|
|
|
use super::RealNpmRegistryApi;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
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 {
|
|
|
|
pub snapshot: NpmResolutionSnapshot,
|
|
|
|
pub local_node_modules_path: Option<String>,
|
2022-09-28 13:04:16 -04:00
|
|
|
}
|
|
|
|
|
2022-09-13 11:59:01 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct NpmPackageResolver {
|
|
|
|
no_npm: bool,
|
|
|
|
inner: Arc<dyn InnerNpmPackageResolver>,
|
2022-09-28 13:04:16 -04:00
|
|
|
local_node_modules_path: Option<PathBuf>,
|
2022-11-08 14:17:24 -05:00
|
|
|
api: RealNpmRegistryApi,
|
2022-10-21 11:20:18 -04:00
|
|
|
cache: NpmCache,
|
2022-10-25 12:20:07 -04:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for NpmPackageResolver {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("NpmPackageResolver")
|
|
|
|
.field("no_npm", &self.no_npm)
|
|
|
|
.field("inner", &"<omitted>")
|
|
|
|
.field("local_node_modules_path", &self.local_node_modules_path)
|
|
|
|
.finish()
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NpmPackageResolver {
|
2023-02-20 13:14:06 -05:00
|
|
|
pub fn new(cache: NpmCache, api: RealNpmRegistryApi) -> Self {
|
|
|
|
Self::new_inner(cache, api, false, None, None, None)
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn new_with_maybe_lockfile(
|
|
|
|
cache: NpmCache,
|
|
|
|
api: RealNpmRegistryApi,
|
|
|
|
no_npm: bool,
|
|
|
|
local_node_modules_path: Option<PathBuf>,
|
2023-02-20 13:14:06 -05:00
|
|
|
initial_snapshot: Option<NpmResolutionSnapshot>,
|
2023-01-24 09:05:54 -05:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
|
|
|
) -> Result<Self, AnyError> {
|
2023-02-20 13:14:06 -05:00
|
|
|
let mut initial_snapshot = initial_snapshot;
|
|
|
|
|
|
|
|
if initial_snapshot.is_none() {
|
|
|
|
if let Some(lockfile) = &maybe_lockfile {
|
|
|
|
if !lockfile.lock().overwrite {
|
|
|
|
initial_snapshot = Some(
|
|
|
|
NpmResolutionSnapshot::from_lockfile(lockfile.clone(), &api)
|
|
|
|
.await
|
|
|
|
.with_context(|| {
|
|
|
|
format!(
|
|
|
|
"failed reading lockfile '{}'",
|
|
|
|
lockfile.lock().filename.display()
|
|
|
|
)
|
|
|
|
})?,
|
|
|
|
)
|
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
2023-02-20 13:14:06 -05:00
|
|
|
}
|
|
|
|
|
2023-01-24 09:05:54 -05:00
|
|
|
Ok(Self::new_inner(
|
2022-10-21 11:20:18 -04:00
|
|
|
cache,
|
|
|
|
api,
|
|
|
|
no_npm,
|
|
|
|
local_node_modules_path,
|
2023-02-20 13:14:06 -05:00
|
|
|
initial_snapshot,
|
2023-01-24 09:05:54 -05:00
|
|
|
maybe_lockfile,
|
|
|
|
))
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
2023-01-24 09:05:54 -05:00
|
|
|
fn new_inner(
|
2022-10-21 11:20:18 -04:00
|
|
|
cache: NpmCache,
|
2022-11-08 14:17:24 -05:00
|
|
|
api: RealNpmRegistryApi,
|
2022-10-21 11:20:18 -04:00
|
|
|
no_npm: bool,
|
|
|
|
local_node_modules_path: Option<PathBuf>,
|
2023-02-20 13:14:06 -05:00
|
|
|
maybe_snapshot: Option<NpmResolutionSnapshot>,
|
2023-01-24 09:05:54 -05:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2022-09-13 11:59:01 -04:00
|
|
|
) -> Self {
|
2022-09-28 13:04:16 -04:00
|
|
|
let inner: Arc<dyn InnerNpmPackageResolver> = match &local_node_modules_path
|
2022-09-22 11:17:02 -04:00
|
|
|
{
|
|
|
|
Some(node_modules_folder) => Arc::new(LocalNpmPackageResolver::new(
|
2022-10-21 11:20:18 -04:00
|
|
|
cache.clone(),
|
|
|
|
api.clone(),
|
2022-09-28 13:04:16 -04:00
|
|
|
node_modules_folder.clone(),
|
|
|
|
maybe_snapshot,
|
2022-09-22 11:17:02 -04:00
|
|
|
)),
|
2022-10-21 11:20:18 -04:00
|
|
|
None => Arc::new(GlobalNpmPackageResolver::new(
|
|
|
|
cache.clone(),
|
|
|
|
api.clone(),
|
|
|
|
maybe_snapshot,
|
|
|
|
)),
|
2022-09-22 11:17:02 -04:00
|
|
|
};
|
2022-09-13 11:59:01 -04:00
|
|
|
Self {
|
|
|
|
no_npm,
|
|
|
|
inner,
|
2022-09-28 13:04:16 -04:00
|
|
|
local_node_modules_path,
|
2022-10-21 11:20:18 -04:00
|
|
|
api,
|
|
|
|
cache,
|
2023-01-24 09:05:54 -05:00
|
|
|
maybe_lockfile,
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
/// Resolves an npm package folder path from a Deno module.
|
|
|
|
pub fn resolve_package_folder_from_deno_module(
|
2022-09-13 11:59:01 -04:00
|
|
|
&self,
|
|
|
|
pkg_req: &NpmPackageReq,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
|
|
|
let path = self
|
|
|
|
.inner
|
|
|
|
.resolve_package_folder_from_deno_module(pkg_req)?;
|
2022-11-28 17:28:54 -05:00
|
|
|
let path = canonicalize_path_maybe_not_exists(&path)?;
|
2022-12-05 20:09:31 -05:00
|
|
|
log::debug!(
|
|
|
|
"Resolved package folder of {} to {}",
|
|
|
|
pkg_req,
|
|
|
|
path.display()
|
|
|
|
);
|
2022-09-22 11:17:02 -04:00
|
|
|
Ok(path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
/// Resolves an npm package folder path from an npm package referrer.
|
|
|
|
pub fn resolve_package_folder_from_package(
|
2022-09-13 11:59:01 -04:00
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2022-11-30 18:07:32 -05:00
|
|
|
mode: NodeResolutionMode,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
|
|
|
let path = self
|
|
|
|
.inner
|
2022-11-30 18:07:32 -05:00
|
|
|
.resolve_package_folder_from_package(name, referrer, mode)?;
|
2022-09-22 11:17:02 -04:00
|
|
|
log::debug!("Resolved {} from {} to {}", name, referrer, path.display());
|
|
|
|
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
|
|
|
|
.inner
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
/// Attempts to get the package size in bytes.
|
|
|
|
pub fn package_size(
|
|
|
|
&self,
|
2023-02-17 09:12:22 -05:00
|
|
|
package_id: &NpmPackageNodeId,
|
2022-10-28 16:19:55 -04:00
|
|
|
) -> Result<u64, AnyError> {
|
|
|
|
self.inner.package_size(package_id)
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-09-22 11:17:02 -04:00
|
|
|
self
|
|
|
|
.resolve_package_folder_from_specifier(specifier)
|
|
|
|
.is_ok()
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the resolver has resolved any npm packages.
|
|
|
|
pub fn has_packages(&self) -> bool {
|
|
|
|
self.inner.has_packages()
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
packages: Vec<NpmPackageReq>,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-10-21 11:20:18 -04:00
|
|
|
if packages.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
|
|
|
|
if self.no_npm {
|
|
|
|
let fmt_reqs = packages
|
2022-11-11 21:26:14 -05:00
|
|
|
.iter()
|
|
|
|
.collect::<HashSet<_>>() // prevent duplicates
|
2022-09-13 11:59:01 -04:00
|
|
|
.iter()
|
2023-01-27 10:43:16 -05:00
|
|
|
.map(|p| format!("\"{p}\""))
|
2022-09-13 11:59:01 -04:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ");
|
|
|
|
return Err(custom_error(
|
|
|
|
"NoNpm",
|
|
|
|
format!(
|
2023-01-27 10:43:16 -05:00
|
|
|
"Following npm specifiers were requested: {fmt_reqs}; but --no-npm is specified."
|
2022-09-13 11:59:01 -04:00
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:20:07 -04:00
|
|
|
self.inner.add_package_reqs(packages).await?;
|
2022-12-19 20:22:17 -05:00
|
|
|
self.inner.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,
|
|
|
|
packages: HashSet<NpmPackageReq>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self.inner.set_package_reqs(packages).await
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
snapshot: self.inner.snapshot(),
|
|
|
|
local_node_modules_path: self
|
|
|
|
.local_node_modules_path
|
|
|
|
.as_ref()
|
|
|
|
.map(|p| p.to_string_lossy().to_string()),
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
2022-10-21 11:20:18 -04:00
|
|
|
|
|
|
|
/// Gets a new resolver with a new snapshotted state.
|
|
|
|
pub fn snapshotted(&self) -> Self {
|
2023-01-24 09:05:54 -05:00
|
|
|
Self::new_inner(
|
2022-10-21 11:20:18 -04:00
|
|
|
self.cache.clone(),
|
|
|
|
self.api.clone(),
|
|
|
|
self.no_npm,
|
|
|
|
self.local_node_modules_path.clone(),
|
2022-10-28 16:19:55 -04:00
|
|
|
Some(self.snapshot()),
|
2023-01-24 09:05:54 -05:00
|
|
|
None,
|
2022-10-21 11:20:18 -04:00
|
|
|
)
|
|
|
|
}
|
2022-10-25 12:20:07 -04:00
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
pub fn snapshot(&self) -> NpmResolutionSnapshot {
|
|
|
|
self.inner.snapshot()
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:20:07 -04:00
|
|
|
pub fn lock(&self, lockfile: &mut Lockfile) -> Result<(), AnyError> {
|
|
|
|
self.inner.lock(lockfile)
|
|
|
|
}
|
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
|
|
|
|
self
|
|
|
|
.inner
|
|
|
|
.add_package_reqs(vec![NpmPackageReq::from_str("@types/node").unwrap()])
|
|
|
|
.await?;
|
|
|
|
self.inner.cache_packages().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RequireNpmResolver for NpmPackageResolver {
|
|
|
|
fn resolve_package_folder_from_package(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &std::path::Path,
|
2022-11-30 18:07:32 -05:00
|
|
|
mode: NodeResolutionMode,
|
2022-09-13 11:59:01 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2022-09-22 11:17:02 -04:00
|
|
|
let referrer = path_to_specifier(referrer)?;
|
2022-11-30 18:07:32 -05:00
|
|
|
self.resolve_package_folder_from_package(specifier, &referrer, mode)
|
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)?;
|
|
|
|
self.resolve_package_folder_from_specifier(&specifier)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn in_npm_package(&self, path: &Path) -> bool {
|
2022-09-22 11:17:02 -04:00
|
|
|
let specifier =
|
2022-12-17 17:20:15 -05:00
|
|
|
match ModuleSpecifier::from_file_path(path.to_path_buf().clean()) {
|
2022-09-22 11:17:02 -04:00
|
|
|
Ok(p) => p,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
self
|
|
|
|
.resolve_package_folder_from_specifier(&specifier)
|
|
|
|
.is_ok()
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-01-10 08:35:44 -05:00
|
|
|
fn ensure_read_permission(
|
|
|
|
&self,
|
|
|
|
permissions: &mut dyn NodePermissions,
|
|
|
|
path: &Path,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self.inner.ensure_read_permission(permissions, path)
|
2022-09-13 11:59:01 -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()),
|
|
|
|
}
|
|
|
|
}
|