2024-06-02 21:39:13 -04:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-12-02 21:10:16 -05:00
|
|
|
use anyhow::anyhow;
|
|
|
|
use anyhow::bail;
|
|
|
|
use anyhow::Context;
|
|
|
|
use anyhow::Error as AnyError;
|
2024-06-02 21:39:13 -04:00
|
|
|
use deno_npm::npm_rc::ResolvedNpmRc;
|
|
|
|
use deno_npm::registry::NpmPackageVersionDistInfo;
|
|
|
|
use deno_semver::package::PackageNv;
|
2024-12-02 21:10:16 -05:00
|
|
|
use deno_unsync::sync::MultiRuntimeAsyncValueCreator;
|
|
|
|
use futures::future::LocalBoxFuture;
|
|
|
|
use futures::FutureExt;
|
2024-07-17 19:37:31 -04:00
|
|
|
use http::StatusCode;
|
2024-12-02 21:10:16 -05:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use url::Url;
|
2024-06-02 21:39:13 -04:00
|
|
|
|
2024-12-02 21:10:16 -05:00
|
|
|
use crate::remote::maybe_auth_header_for_npm_registry;
|
|
|
|
use crate::tarball_extract::verify_and_extract_tarball;
|
|
|
|
use crate::tarball_extract::TarballExtractionMode;
|
|
|
|
use crate::NpmCache;
|
|
|
|
use crate::NpmCacheEnv;
|
|
|
|
use crate::NpmCacheSetting;
|
2024-06-02 21:39:13 -04:00
|
|
|
|
2024-06-05 15:17:35 -04:00
|
|
|
type LoadResult = Result<(), Arc<AnyError>>;
|
|
|
|
type LoadFuture = LocalBoxFuture<'static, LoadResult>;
|
|
|
|
|
2024-06-02 21:39:13 -04:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum MemoryCacheItem {
|
|
|
|
/// The cache item hasn't finished yet.
|
2024-06-05 15:17:35 -04:00
|
|
|
Pending(Arc<MultiRuntimeAsyncValueCreator<LoadResult>>),
|
2024-06-02 21:39:13 -04:00
|
|
|
/// The result errored.
|
|
|
|
Errored(Arc<AnyError>),
|
|
|
|
/// This package has already been cached.
|
|
|
|
Cached,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Coordinates caching of tarballs being loaded from
|
|
|
|
/// the npm registry.
|
|
|
|
///
|
|
|
|
/// This is shared amongst all the workers.
|
|
|
|
#[derive(Debug)]
|
2024-12-02 21:10:16 -05:00
|
|
|
pub struct TarballCache<TEnv: NpmCacheEnv> {
|
|
|
|
cache: Arc<NpmCache<TEnv>>,
|
|
|
|
env: Arc<TEnv>,
|
2024-06-02 21:39:13 -04:00
|
|
|
npmrc: Arc<ResolvedNpmRc>,
|
|
|
|
memory_cache: Mutex<HashMap<PackageNv, MemoryCacheItem>>,
|
|
|
|
}
|
|
|
|
|
2024-12-02 21:10:16 -05:00
|
|
|
impl<TEnv: NpmCacheEnv> TarballCache<TEnv> {
|
2024-06-02 21:39:13 -04:00
|
|
|
pub fn new(
|
2024-12-02 21:10:16 -05:00
|
|
|
cache: Arc<NpmCache<TEnv>>,
|
|
|
|
env: Arc<TEnv>,
|
2024-06-02 21:39:13 -04:00
|
|
|
npmrc: Arc<ResolvedNpmRc>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
cache,
|
2024-12-02 21:10:16 -05:00
|
|
|
env,
|
2024-06-02 21:39:13 -04:00
|
|
|
npmrc,
|
|
|
|
memory_cache: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn ensure_package(
|
2024-06-05 11:04:16 -04:00
|
|
|
self: &Arc<Self>,
|
2024-12-03 19:44:56 -05:00
|
|
|
package_nv: &PackageNv,
|
2024-06-02 21:39:13 -04:00
|
|
|
dist: &NpmPackageVersionDistInfo,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self
|
2024-12-03 19:44:56 -05:00
|
|
|
.ensure_package_inner(package_nv, dist)
|
2024-06-02 21:39:13 -04:00
|
|
|
.await
|
2024-12-03 19:44:56 -05:00
|
|
|
.with_context(|| format!("Failed caching npm package '{}'.", package_nv))
|
2024-06-02 21:39:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn ensure_package_inner(
|
2024-06-05 11:04:16 -04:00
|
|
|
self: &Arc<Self>,
|
2024-06-02 21:39:13 -04:00
|
|
|
package_nv: &PackageNv,
|
|
|
|
dist: &NpmPackageVersionDistInfo,
|
|
|
|
) -> Result<(), AnyError> {
|
2024-06-05 11:04:16 -04:00
|
|
|
let cache_item = {
|
2024-06-02 21:39:13 -04:00
|
|
|
let mut mem_cache = self.memory_cache.lock();
|
|
|
|
if let Some(cache_item) = mem_cache.get(package_nv) {
|
2024-06-05 11:04:16 -04:00
|
|
|
cache_item.clone()
|
2024-06-02 21:39:13 -04:00
|
|
|
} else {
|
2024-06-05 15:17:35 -04:00
|
|
|
let value_creator = MultiRuntimeAsyncValueCreator::new({
|
|
|
|
let tarball_cache = self.clone();
|
|
|
|
let package_nv = package_nv.clone();
|
|
|
|
let dist = dist.clone();
|
|
|
|
Box::new(move || {
|
|
|
|
tarball_cache.create_setup_future(package_nv.clone(), dist.clone())
|
|
|
|
})
|
|
|
|
});
|
2024-06-05 11:04:16 -04:00
|
|
|
let cache_item = MemoryCacheItem::Pending(Arc::new(value_creator));
|
2024-06-02 21:39:13 -04:00
|
|
|
mem_cache.insert(package_nv.clone(), cache_item.clone());
|
2024-06-05 11:04:16 -04:00
|
|
|
cache_item
|
2024-06-02 21:39:13 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match cache_item {
|
|
|
|
MemoryCacheItem::Cached => Ok(()),
|
2024-12-03 19:44:56 -05:00
|
|
|
MemoryCacheItem::Errored(err) => Err(anyhow!("{:#}", err)),
|
2024-06-05 11:04:16 -04:00
|
|
|
MemoryCacheItem::Pending(creator) => {
|
2024-06-05 15:17:35 -04:00
|
|
|
let result = creator.get().await;
|
2024-06-05 11:04:16 -04:00
|
|
|
match result {
|
|
|
|
Ok(_) => {
|
|
|
|
*self.memory_cache.lock().get_mut(package_nv).unwrap() =
|
|
|
|
MemoryCacheItem::Cached;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2024-12-03 19:44:56 -05:00
|
|
|
let result_err = anyhow!("{:#}", err);
|
2024-06-05 11:04:16 -04:00
|
|
|
*self.memory_cache.lock().get_mut(package_nv).unwrap() =
|
|
|
|
MemoryCacheItem::Errored(err);
|
|
|
|
Err(result_err)
|
2024-06-02 21:39:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_setup_future(
|
2024-06-05 11:04:16 -04:00
|
|
|
self: &Arc<Self>,
|
2024-06-02 21:39:13 -04:00
|
|
|
package_nv: PackageNv,
|
|
|
|
dist: NpmPackageVersionDistInfo,
|
2024-06-05 15:17:35 -04:00
|
|
|
) -> LoadFuture {
|
2024-06-05 11:04:16 -04:00
|
|
|
let tarball_cache = self.clone();
|
|
|
|
async move {
|
|
|
|
let registry_url = tarball_cache.npmrc.get_registry_url(&package_nv.name);
|
|
|
|
let package_folder =
|
|
|
|
tarball_cache.cache.package_folder_for_nv_and_url(&package_nv, registry_url);
|
|
|
|
let should_use_cache = tarball_cache.cache.should_use_cache_for_package(&package_nv);
|
2024-12-02 21:10:16 -05:00
|
|
|
let package_folder_exists = tarball_cache.env.exists(&package_folder);
|
2024-06-02 21:39:13 -04:00
|
|
|
if should_use_cache && package_folder_exists {
|
|
|
|
return Ok(());
|
2024-12-02 21:10:16 -05:00
|
|
|
} else if tarball_cache.cache.cache_setting() == &NpmCacheSetting::Only {
|
|
|
|
return Err(deno_core::error::custom_error(
|
2024-06-02 21:39:13 -04:00
|
|
|
"NotCached",
|
|
|
|
format!(
|
2024-12-03 19:44:56 -05:00
|
|
|
"npm package not found in cache: \"{}\", --cached-only is specified.",
|
2024-06-02 21:39:13 -04:00
|
|
|
&package_nv.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if dist.tarball.is_empty() {
|
|
|
|
bail!("Tarball URL was empty.");
|
|
|
|
}
|
|
|
|
|
2024-06-05 17:24:52 -04:00
|
|
|
// IMPORTANT: npm registries may specify tarball URLs at different URLS than the
|
|
|
|
// registry, so we MUST get the auth for the tarball URL and not the registry URL.
|
|
|
|
let tarball_uri = Url::parse(&dist.tarball)?;
|
|
|
|
let maybe_registry_config =
|
|
|
|
tarball_cache.npmrc.tarball_config(&tarball_uri);
|
2024-08-14 12:53:15 -04:00
|
|
|
let maybe_auth_header = maybe_registry_config.and_then(|c| maybe_auth_header_for_npm_registry(c).ok()?);
|
2024-06-02 21:39:13 -04:00
|
|
|
|
2024-12-02 21:10:16 -05:00
|
|
|
let result = tarball_cache.env
|
|
|
|
.download_with_retries_on_any_tokio_runtime(tarball_uri, maybe_auth_header)
|
2024-06-05 17:24:52 -04:00
|
|
|
.await;
|
|
|
|
let maybe_bytes = match result {
|
|
|
|
Ok(maybe_bytes) => maybe_bytes,
|
2024-12-02 21:10:16 -05:00
|
|
|
Err(err) => {
|
|
|
|
if err.status_code == Some(StatusCode::UNAUTHORIZED)
|
2024-06-05 17:24:52 -04:00
|
|
|
&& maybe_registry_config.is_none()
|
|
|
|
&& tarball_cache.npmrc.get_registry_config(&package_nv.name).auth_token.is_some()
|
|
|
|
{
|
|
|
|
bail!(
|
|
|
|
concat!(
|
|
|
|
"No auth for tarball URI, but present for scoped registry.\n\n",
|
|
|
|
"Tarball URI: {}\n",
|
|
|
|
"Scope URI: {}\n\n",
|
|
|
|
"More info here: https://github.com/npm/cli/wiki/%22No-auth-for-URI,-but-auth-present-for-scoped-registry%22"
|
|
|
|
),
|
|
|
|
dist.tarball,
|
|
|
|
registry_url,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return Err(err.into())
|
|
|
|
},
|
|
|
|
};
|
2024-06-02 21:39:13 -04:00
|
|
|
match maybe_bytes {
|
|
|
|
Some(bytes) => {
|
|
|
|
let extraction_mode = if should_use_cache || !package_folder_exists {
|
|
|
|
TarballExtractionMode::SiblingTempDir
|
|
|
|
} else {
|
|
|
|
// The user ran with `--reload`, so overwrite the package instead of
|
|
|
|
// deleting it since the package might get corrupted if a user kills
|
|
|
|
// their deno process while it's deleting a package directory
|
|
|
|
//
|
|
|
|
// We can't rename this folder and delete it because the folder
|
|
|
|
// may be in use by another process or may now contain hardlinks,
|
|
|
|
// which will cause windows to throw an "AccessDenied" error when
|
|
|
|
// renaming. So we settle for overwriting.
|
|
|
|
TarballExtractionMode::Overwrite
|
|
|
|
};
|
|
|
|
let dist = dist.clone();
|
|
|
|
let package_nv = package_nv.clone();
|
2024-12-02 21:10:16 -05:00
|
|
|
deno_unsync::spawn_blocking(move || {
|
2024-06-02 21:39:13 -04:00
|
|
|
verify_and_extract_tarball(
|
|
|
|
&package_nv,
|
|
|
|
&bytes,
|
|
|
|
&dist,
|
|
|
|
&package_folder,
|
|
|
|
extraction_mode,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bail!("Could not find npm package tarball at: {}", dist.tarball);
|
|
|
|
}
|
|
|
|
}
|
2024-06-05 15:17:35 -04:00
|
|
|
}
|
|
|
|
.map(|r| r.map_err(Arc::new))
|
|
|
|
.boxed_local()
|
2024-06-02 21:39:13 -04:00
|
|
|
}
|
|
|
|
}
|