2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2023-10-25 14:39:00 -04:00
|
|
|
mod byonm;
|
2023-09-30 12:06:38 -04:00
|
|
|
mod managed;
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
use std::borrow::Cow;
|
2024-09-30 09:33:32 -04:00
|
|
|
use std::path::Path;
|
2023-09-30 12:06:38 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-03-06 08:24:15 -05:00
|
|
|
use dashmap::DashMap;
|
2023-09-30 12:06:38 -04:00
|
|
|
use deno_core::error::AnyError;
|
2024-03-06 08:24:15 -05:00
|
|
|
use deno_core::serde_json;
|
2024-12-02 21:10:16 -05:00
|
|
|
use deno_core::url::Url;
|
2024-10-24 14:03:56 -04:00
|
|
|
use deno_npm::npm_rc::ResolvedNpmRc;
|
2024-03-06 08:24:15 -05:00
|
|
|
use deno_npm::registry::NpmPackageInfo;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_resolver::npm::ByonmInNpmPackageChecker;
|
2024-09-30 09:33:32 -04:00
|
|
|
use deno_resolver::npm::ByonmNpmResolver;
|
2024-11-14 15:24:25 -05:00
|
|
|
use deno_resolver::npm::CliNpmReqResolver;
|
|
|
|
use deno_resolver::npm::ResolvePkgFolderFromDenoReqError;
|
2024-12-02 21:10:16 -05:00
|
|
|
use deno_runtime::deno_fs::FileSystem;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_runtime::deno_node::NodePermissions;
|
2024-09-27 15:35:37 -04:00
|
|
|
use deno_runtime::ops::process::NpmProcessStateProvider;
|
2024-03-06 08:24:15 -05:00
|
|
|
use deno_semver::package::PackageNv;
|
2023-09-30 12:06:38 -04:00
|
|
|
use deno_semver::package::PackageReq;
|
2024-12-02 21:10:16 -05:00
|
|
|
use http::HeaderName;
|
|
|
|
use http::HeaderValue;
|
2024-11-01 12:27:00 -04:00
|
|
|
use managed::create_managed_in_npm_pkg_checker;
|
|
|
|
use node_resolver::InNpmPackageChecker;
|
2024-11-14 15:24:25 -05:00
|
|
|
use node_resolver::NpmPackageFolderResolver;
|
2023-10-02 17:53:55 -04:00
|
|
|
|
2024-03-06 08:24:15 -05:00
|
|
|
use crate::file_fetcher::FileFetcher;
|
2024-12-02 21:10:16 -05:00
|
|
|
use crate::http_util::HttpClientProvider;
|
|
|
|
use crate::util::fs::atomic_write_file_with_retries_and_fs;
|
|
|
|
use crate::util::fs::hard_link_dir_recursive;
|
|
|
|
use crate::util::fs::AtomicWriteFileFsAdapter;
|
|
|
|
use crate::util::progress_bar::ProgressBar;
|
2024-03-06 08:24:15 -05:00
|
|
|
|
2024-09-30 09:33:32 -04:00
|
|
|
pub use self::byonm::CliByonmNpmResolver;
|
|
|
|
pub use self::byonm::CliByonmNpmResolverCreateOptions;
|
2024-11-01 12:27:00 -04:00
|
|
|
pub use self::managed::CliManagedInNpmPkgCheckerCreateOptions;
|
|
|
|
pub use self::managed::CliManagedNpmResolverCreateOptions;
|
2023-10-02 17:53:55 -04:00
|
|
|
pub use self::managed::CliNpmResolverManagedSnapshotOption;
|
|
|
|
pub use self::managed::ManagedCliNpmResolver;
|
2023-10-25 14:39:00 -04:00
|
|
|
|
2024-12-02 21:10:16 -05:00
|
|
|
pub type CliNpmTarballCache = deno_npm_cache::TarballCache<CliNpmCacheEnv>;
|
|
|
|
pub type CliNpmCache = deno_npm_cache::NpmCache<CliNpmCacheEnv>;
|
|
|
|
pub type CliNpmRegistryInfoProvider =
|
|
|
|
deno_npm_cache::RegistryInfoProvider<CliNpmCacheEnv>;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CliNpmCacheEnv {
|
|
|
|
fs: Arc<dyn FileSystem>,
|
|
|
|
http_client_provider: Arc<HttpClientProvider>,
|
|
|
|
progress_bar: ProgressBar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CliNpmCacheEnv {
|
|
|
|
pub fn new(
|
|
|
|
fs: Arc<dyn FileSystem>,
|
|
|
|
http_client_provider: Arc<HttpClientProvider>,
|
|
|
|
progress_bar: ProgressBar,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
fs,
|
|
|
|
http_client_provider,
|
|
|
|
progress_bar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl deno_npm_cache::NpmCacheEnv for CliNpmCacheEnv {
|
|
|
|
fn exists(&self, path: &Path) -> bool {
|
|
|
|
self.fs.exists_sync(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hard_link_dir_recursive(
|
|
|
|
&self,
|
|
|
|
from: &Path,
|
|
|
|
to: &Path,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
// todo(dsherret): use self.fs here instead
|
|
|
|
hard_link_dir_recursive(from, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn atomic_write_file_with_retries(
|
|
|
|
&self,
|
|
|
|
file_path: &Path,
|
|
|
|
data: &[u8],
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
atomic_write_file_with_retries_and_fs(
|
|
|
|
&AtomicWriteFileFsAdapter {
|
|
|
|
fs: self.fs.as_ref(),
|
|
|
|
write_mode: crate::cache::CACHE_PERM,
|
|
|
|
},
|
|
|
|
file_path,
|
|
|
|
data,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn download_with_retries_on_any_tokio_runtime(
|
|
|
|
&self,
|
|
|
|
url: Url,
|
|
|
|
maybe_auth_header: Option<(HeaderName, HeaderValue)>,
|
|
|
|
) -> Result<Option<Vec<u8>>, deno_npm_cache::DownloadError> {
|
|
|
|
let guard = self.progress_bar.update(url.as_str());
|
|
|
|
let client = self.http_client_provider.get_or_create().map_err(|err| {
|
|
|
|
deno_npm_cache::DownloadError {
|
|
|
|
status_code: None,
|
|
|
|
error: err,
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
client
|
|
|
|
.download_with_progress_and_retries(url, maybe_auth_header, &guard)
|
|
|
|
.await
|
|
|
|
.map_err(|err| {
|
|
|
|
use crate::http_util::DownloadError::*;
|
|
|
|
let status_code = match &err {
|
|
|
|
Fetch { .. }
|
|
|
|
| UrlParse { .. }
|
|
|
|
| HttpParse { .. }
|
|
|
|
| Json { .. }
|
|
|
|
| ToStr { .. }
|
|
|
|
| NoRedirectHeader { .. }
|
|
|
|
| TooManyRedirects => None,
|
|
|
|
BadResponse(bad_response_error) => {
|
|
|
|
Some(bad_response_error.status_code)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
deno_npm_cache::DownloadError {
|
|
|
|
status_code,
|
|
|
|
error: err.into(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
pub enum CliNpmResolverCreateOptions {
|
2024-11-01 12:27:00 -04:00
|
|
|
Managed(CliManagedNpmResolverCreateOptions),
|
2024-09-30 09:33:32 -04:00
|
|
|
Byonm(CliByonmNpmResolverCreateOptions),
|
2023-10-02 17:53:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_cli_npm_resolver_for_lsp(
|
|
|
|
options: CliNpmResolverCreateOptions,
|
|
|
|
) -> Arc<dyn CliNpmResolver> {
|
|
|
|
use CliNpmResolverCreateOptions::*;
|
|
|
|
match options {
|
|
|
|
Managed(options) => {
|
|
|
|
managed::create_managed_npm_resolver_for_lsp(options).await
|
|
|
|
}
|
2024-09-30 09:33:32 -04:00
|
|
|
Byonm(options) => Arc::new(ByonmNpmResolver::new(options)),
|
2023-10-02 17:53:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_cli_npm_resolver(
|
|
|
|
options: CliNpmResolverCreateOptions,
|
|
|
|
) -> Result<Arc<dyn CliNpmResolver>, AnyError> {
|
|
|
|
use CliNpmResolverCreateOptions::*;
|
|
|
|
match options {
|
|
|
|
Managed(options) => managed::create_managed_npm_resolver(options).await,
|
2024-09-30 09:33:32 -04:00
|
|
|
Byonm(options) => Ok(Arc::new(ByonmNpmResolver::new(options))),
|
2023-10-02 17:53:55 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-30 12:06:38 -04:00
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
pub enum CreateInNpmPkgCheckerOptions<'a> {
|
|
|
|
Managed(CliManagedInNpmPkgCheckerCreateOptions<'a>),
|
|
|
|
Byonm,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_in_npm_pkg_checker(
|
|
|
|
options: CreateInNpmPkgCheckerOptions,
|
|
|
|
) -> Arc<dyn InNpmPackageChecker> {
|
|
|
|
match options {
|
|
|
|
CreateInNpmPkgCheckerOptions::Managed(options) => {
|
|
|
|
create_managed_in_npm_pkg_checker(options)
|
|
|
|
}
|
|
|
|
CreateInNpmPkgCheckerOptions::Byonm => Arc::new(ByonmInNpmPackageChecker),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 12:06:38 -04:00
|
|
|
pub enum InnerCliNpmResolverRef<'a> {
|
|
|
|
Managed(&'a ManagedCliNpmResolver),
|
|
|
|
#[allow(dead_code)]
|
2024-09-30 09:33:32 -04:00
|
|
|
Byonm(&'a CliByonmNpmResolver),
|
2023-09-30 12:06:38 -04:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:24:25 -05:00
|
|
|
pub trait CliNpmResolver: NpmPackageFolderResolver + CliNpmReqResolver {
|
|
|
|
fn into_npm_pkg_folder_resolver(
|
|
|
|
self: Arc<Self>,
|
|
|
|
) -> Arc<dyn NpmPackageFolderResolver>;
|
|
|
|
fn into_npm_req_resolver(self: Arc<Self>) -> Arc<dyn CliNpmReqResolver>;
|
2024-07-25 19:08:14 -04:00
|
|
|
fn into_process_state_provider(
|
|
|
|
self: Arc<Self>,
|
|
|
|
) -> Arc<dyn NpmProcessStateProvider>;
|
2024-11-14 15:24:25 -05:00
|
|
|
fn into_maybe_byonm(self: Arc<Self>) -> Option<Arc<CliByonmNpmResolver>> {
|
|
|
|
None
|
|
|
|
}
|
2023-09-30 12:06:38 -04:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver>;
|
|
|
|
|
2023-09-30 12:06:38 -04:00
|
|
|
fn as_inner(&self) -> InnerCliNpmResolverRef;
|
|
|
|
|
|
|
|
fn as_managed(&self) -> Option<&ManagedCliNpmResolver> {
|
|
|
|
match self.as_inner() {
|
|
|
|
InnerCliNpmResolverRef::Managed(inner) => Some(inner),
|
|
|
|
InnerCliNpmResolverRef::Byonm(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 09:33:32 -04:00
|
|
|
fn as_byonm(&self) -> Option<&CliByonmNpmResolver> {
|
2023-10-03 19:05:06 -04:00
|
|
|
match self.as_inner() {
|
|
|
|
InnerCliNpmResolverRef::Managed(_) => None,
|
|
|
|
InnerCliNpmResolverRef::Byonm(inner) => Some(inner),
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 12:06:38 -04:00
|
|
|
|
2024-09-30 09:33:32 -04:00
|
|
|
fn root_node_modules_path(&self) -> Option<&Path>;
|
2023-09-30 12:06:38 -04:00
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
fn ensure_read_permission<'a>(
|
|
|
|
&self,
|
|
|
|
permissions: &mut dyn NodePermissions,
|
|
|
|
path: &'a Path,
|
|
|
|
) -> Result<Cow<'a, Path>, AnyError>;
|
|
|
|
|
2023-10-03 19:05:06 -04:00
|
|
|
/// Returns a hash returning the state of the npm resolver
|
|
|
|
/// or `None` if the state currently can't be determined.
|
|
|
|
fn check_state_hash(&self) -> Option<u64>;
|
2023-09-30 12:06:38 -04:00
|
|
|
}
|
2024-03-06 08:24:15 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NpmFetchResolver {
|
|
|
|
nv_by_req: DashMap<PackageReq, Option<PackageNv>>,
|
|
|
|
info_by_name: DashMap<String, Option<Arc<NpmPackageInfo>>>,
|
2024-06-03 17:17:08 -04:00
|
|
|
file_fetcher: Arc<FileFetcher>,
|
2024-10-24 14:03:56 -04:00
|
|
|
npmrc: Arc<ResolvedNpmRc>,
|
2024-03-06 08:24:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NpmFetchResolver {
|
2024-10-24 14:03:56 -04:00
|
|
|
pub fn new(
|
|
|
|
file_fetcher: Arc<FileFetcher>,
|
|
|
|
npmrc: Arc<ResolvedNpmRc>,
|
|
|
|
) -> Self {
|
2024-03-06 08:24:15 -05:00
|
|
|
Self {
|
|
|
|
nv_by_req: Default::default(),
|
|
|
|
info_by_name: Default::default(),
|
|
|
|
file_fetcher,
|
2024-10-24 14:03:56 -04:00
|
|
|
npmrc,
|
2024-03-06 08:24:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn req_to_nv(&self, req: &PackageReq) -> Option<PackageNv> {
|
|
|
|
if let Some(nv) = self.nv_by_req.get(req) {
|
|
|
|
return nv.value().clone();
|
|
|
|
}
|
|
|
|
let maybe_get_nv = || async {
|
|
|
|
let name = req.name.clone();
|
|
|
|
let package_info = self.package_info(&name).await?;
|
2024-08-08 12:45:41 -04:00
|
|
|
if let Some(dist_tag) = req.version_req.tag() {
|
|
|
|
let version = package_info.dist_tags.get(dist_tag)?.clone();
|
|
|
|
return Some(PackageNv { name, version });
|
|
|
|
}
|
2024-03-28 15:02:07 -04:00
|
|
|
// Find the first matching version of the package.
|
2024-03-06 08:24:15 -05:00
|
|
|
let mut versions = package_info.versions.keys().collect::<Vec<_>>();
|
|
|
|
versions.sort();
|
|
|
|
let version = versions
|
|
|
|
.into_iter()
|
|
|
|
.rev()
|
|
|
|
.find(|v| req.version_req.tag().is_none() && req.version_req.matches(v))
|
|
|
|
.cloned()?;
|
|
|
|
Some(PackageNv { name, version })
|
|
|
|
};
|
|
|
|
let nv = maybe_get_nv().await;
|
|
|
|
self.nv_by_req.insert(req.clone(), nv.clone());
|
|
|
|
nv
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn package_info(&self, name: &str) -> Option<Arc<NpmPackageInfo>> {
|
|
|
|
if let Some(info) = self.info_by_name.get(name) {
|
|
|
|
return info.value().clone();
|
|
|
|
}
|
2024-12-02 21:10:16 -05:00
|
|
|
// todo(#27198): use RegistryInfoProvider instead
|
2024-03-06 08:24:15 -05:00
|
|
|
let fetch_package_info = || async {
|
2024-12-02 21:10:16 -05:00
|
|
|
let info_url = deno_npm_cache::get_package_url(&self.npmrc, name);
|
2024-06-03 17:17:08 -04:00
|
|
|
let file_fetcher = self.file_fetcher.clone();
|
2024-10-24 14:03:56 -04:00
|
|
|
let registry_config = self.npmrc.get_registry_config(name);
|
|
|
|
// TODO(bartlomieju): this should error out, not use `.ok()`.
|
|
|
|
let maybe_auth_header =
|
2024-12-02 21:10:16 -05:00
|
|
|
deno_npm_cache::maybe_auth_header_for_npm_registry(registry_config)
|
|
|
|
.ok()?;
|
2024-06-03 17:17:08 -04:00
|
|
|
// spawn due to the lsp's `Send` requirement
|
|
|
|
let file = deno_core::unsync::spawn(async move {
|
2024-10-24 14:03:56 -04:00
|
|
|
file_fetcher
|
|
|
|
.fetch_bypass_permissions_with_maybe_auth(
|
|
|
|
&info_url,
|
|
|
|
maybe_auth_header,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.ok()
|
2024-06-03 17:17:08 -04:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.ok()??;
|
2024-03-06 08:24:15 -05:00
|
|
|
serde_json::from_slice::<NpmPackageInfo>(&file.source).ok()
|
|
|
|
};
|
|
|
|
let info = fetch_package_info().await.map(Arc::new);
|
|
|
|
self.info_by_name.insert(name.to_string(), info.clone());
|
|
|
|
info
|
|
|
|
}
|
|
|
|
}
|
2024-11-01 01:19:19 -04:00
|
|
|
|
|
|
|
pub const NPM_CONFIG_USER_AGENT_ENV_VAR: &str = "npm_config_user_agent";
|
|
|
|
|
|
|
|
pub fn get_npm_config_user_agent() -> String {
|
|
|
|
format!(
|
|
|
|
"deno/{} npm/? deno/{} {} {}",
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
std::env::consts::OS,
|
|
|
|
std::env::consts::ARCH
|
|
|
|
)
|
|
|
|
}
|