summaryrefslogtreecommitdiff
path: root/cli/npm/managed/cache
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /cli/npm/managed/cache
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'cli/npm/managed/cache')
-rw-r--r--cli/npm/managed/cache/mod.rs14
-rw-r--r--cli/npm/managed/cache/registry_info.rs66
-rw-r--r--cli/npm/managed/cache/tarball.rs2
3 files changed, 48 insertions, 34 deletions
diff --git a/cli/npm/managed/cache/mod.rs b/cli/npm/managed/cache/mod.rs
index fa0e8c8a5..8ae99f41e 100644
--- a/cli/npm/managed/cache/mod.rs
+++ b/cli/npm/managed/cache/mod.rs
@@ -26,7 +26,7 @@ use crate::cache::CACHE_PERM;
use crate::util::fs::atomic_write_file_with_retries;
use crate::util::fs::hard_link_dir_recursive;
-mod registry_info;
+pub mod registry_info;
mod tarball;
mod tarball_extract;
@@ -36,7 +36,7 @@ pub use tarball::TarballCache;
/// Stores a single copy of npm packages in a cache.
#[derive(Debug)]
pub struct NpmCache {
- cache_dir: NpmCacheDir,
+ cache_dir: Arc<NpmCacheDir>,
cache_setting: CacheSetting,
npmrc: Arc<ResolvedNpmRc>,
/// ensures a package is only downloaded once per run
@@ -45,7 +45,7 @@ pub struct NpmCache {
impl NpmCache {
pub fn new(
- cache_dir: NpmCacheDir,
+ cache_dir: Arc<NpmCacheDir>,
cache_setting: CacheSetting,
npmrc: Arc<ResolvedNpmRc>,
) -> Self {
@@ -61,6 +61,10 @@ impl NpmCache {
&self.cache_setting
}
+ pub fn root_dir_path(&self) -> &Path {
+ self.cache_dir.root_dir()
+ }
+
pub fn root_dir_url(&self) -> &Url {
self.cache_dir.root_dir_url()
}
@@ -152,10 +156,6 @@ impl NpmCache {
self.cache_dir.package_name_folder(name, registry_url)
}
- pub fn root_folder(&self) -> PathBuf {
- self.cache_dir.root_dir().to_owned()
- }
-
pub fn resolve_package_folder_id_from_specifier(
&self,
specifier: &ModuleSpecifier,
diff --git a/cli/npm/managed/cache/registry_info.rs b/cli/npm/managed/cache/registry_info.rs
index 28b19373e..6d39d3c13 100644
--- a/cli/npm/managed/cache/registry_info.rs
+++ b/cli/npm/managed/cache/registry_info.rs
@@ -84,7 +84,7 @@ impl RegistryInfoDownloader {
self.load_package_info_inner(name).await.with_context(|| {
format!(
"Error getting response at {} for package \"{}\"",
- self.get_package_url(name),
+ get_package_url(&self.npmrc, name),
name
)
})
@@ -190,7 +190,7 @@ impl RegistryInfoDownloader {
fn create_load_future(self: &Arc<Self>, name: &str) -> LoadFuture {
let downloader = self.clone();
- let package_url = self.get_package_url(name);
+ let package_url = get_package_url(&self.npmrc, name);
let registry_config = self.npmrc.get_registry_config(name);
let maybe_auth_header =
match maybe_auth_header_for_npm_registry(registry_config) {
@@ -202,10 +202,13 @@ impl RegistryInfoDownloader {
let guard = self.progress_bar.update(package_url.as_str());
let name = name.to_string();
async move {
- let maybe_bytes = downloader
- .http_client_provider
- .get_or_create()?
- .download_with_progress(package_url, maybe_auth_header, &guard)
+ let client = downloader.http_client_provider.get_or_create()?;
+ let maybe_bytes = client
+ .download_with_progress_and_retries(
+ package_url,
+ maybe_auth_header,
+ &guard,
+ )
.await?;
match maybe_bytes {
Some(bytes) => {
@@ -236,25 +239,36 @@ impl RegistryInfoDownloader {
.map(|r| r.map_err(Arc::new))
.boxed_local()
}
+}
- fn get_package_url(&self, name: &str) -> Url {
- let registry_url = self.npmrc.get_registry_url(name);
- // list of all characters used in npm packages:
- // !, ', (, ), *, -, ., /, [0-9], @, [A-Za-z], _, ~
- const ASCII_SET: percent_encoding::AsciiSet =
- percent_encoding::NON_ALPHANUMERIC
- .remove(b'!')
- .remove(b'\'')
- .remove(b'(')
- .remove(b')')
- .remove(b'*')
- .remove(b'-')
- .remove(b'.')
- .remove(b'/')
- .remove(b'@')
- .remove(b'_')
- .remove(b'~');
- let name = percent_encoding::utf8_percent_encode(name, &ASCII_SET);
- registry_url.join(&name.to_string()).unwrap()
- }
+pub fn get_package_url(npmrc: &ResolvedNpmRc, name: &str) -> Url {
+ let registry_url = npmrc.get_registry_url(name);
+ // The '/' character in scoped package names "@scope/name" must be
+ // encoded for older third party registries. Newer registries and
+ // npm itself support both ways
+ // - encoded: https://registry.npmjs.org/@rollup%2fplugin-json
+ // - non-ecoded: https://registry.npmjs.org/@rollup/plugin-json
+ // To support as many third party registries as possible we'll
+ // always encode the '/' character.
+
+ // list of all characters used in npm packages:
+ // !, ', (, ), *, -, ., /, [0-9], @, [A-Za-z], _, ~
+ const ASCII_SET: percent_encoding::AsciiSet =
+ percent_encoding::NON_ALPHANUMERIC
+ .remove(b'!')
+ .remove(b'\'')
+ .remove(b'(')
+ .remove(b')')
+ .remove(b'*')
+ .remove(b'-')
+ .remove(b'.')
+ .remove(b'@')
+ .remove(b'_')
+ .remove(b'~');
+ let name = percent_encoding::utf8_percent_encode(name, &ASCII_SET);
+ registry_url
+ // Ensure that scoped package name percent encoding is lower cased
+ // to match npm.
+ .join(&name.to_string().replace("%2F", "%2f"))
+ .unwrap()
}
diff --git a/cli/npm/managed/cache/tarball.rs b/cli/npm/managed/cache/tarball.rs
index 4bcee38ea..7cf88d6d6 100644
--- a/cli/npm/managed/cache/tarball.rs
+++ b/cli/npm/managed/cache/tarball.rs
@@ -172,7 +172,7 @@ impl TarballCache {
let guard = tarball_cache.progress_bar.update(&dist.tarball);
let result = tarball_cache.http_client_provider
.get_or_create()?
- .download_with_progress(tarball_uri, maybe_auth_header, &guard)
+ .download_with_progress_and_retries(tarball_uri, maybe_auth_header, &guard)
.await;
let maybe_bytes = match result {
Ok(maybe_bytes) => maybe_bytes,