2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-10 19:23:58 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2022-11-27 18:25:08 +00:00
|
|
|
use std::collections::HashSet;
|
2022-08-10 19:23:58 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-02-21 17:03:48 +00:00
|
|
|
use async_trait::async_trait;
|
2023-04-06 22:46:44 +00:00
|
|
|
use deno_core::anyhow::anyhow;
|
2022-08-10 19:23:58 +00:00
|
|
|
use deno_core::error::AnyError;
|
2023-04-06 22:46:44 +00:00
|
|
|
use deno_core::futures::future::BoxFuture;
|
|
|
|
use deno_core::futures::future::Shared;
|
|
|
|
use deno_core::futures::FutureExt;
|
2022-08-10 19:23:58 +00:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2023-04-06 22:46:44 +00:00
|
|
|
use deno_npm::registry::NpmPackageInfo;
|
|
|
|
use deno_npm::registry::NpmRegistryApi;
|
2023-04-07 01:41:19 +00:00
|
|
|
use deno_npm::registry::NpmRegistryPackageInfoLoadError;
|
2022-08-10 19:23:58 +00:00
|
|
|
|
2022-11-28 22:28:54 +00:00
|
|
|
use crate::args::CacheSetting;
|
2023-04-11 22:10:51 +00:00
|
|
|
use crate::util::sync::AtomicFlag;
|
2022-08-10 19:23:58 +00:00
|
|
|
|
|
|
|
use super::cache::NpmCache;
|
2024-06-03 01:39:13 +00:00
|
|
|
use super::cache::RegistryInfoDownloader;
|
2022-08-10 19:23:58 +00:00
|
|
|
|
2023-04-14 20:22:33 +00:00
|
|
|
#[derive(Debug)]
|
2023-04-12 12:36:11 +00:00
|
|
|
pub struct CliNpmRegistryApi(Option<Arc<CliNpmRegistryApiInner>>);
|
2022-12-07 21:10:50 +00:00
|
|
|
|
2023-04-12 12:36:11 +00:00
|
|
|
impl CliNpmRegistryApi {
|
2022-08-22 15:35:04 +00:00
|
|
|
pub fn new(
|
2023-04-14 20:22:33 +00:00
|
|
|
cache: Arc<NpmCache>,
|
2024-06-05 15:04:16 +00:00
|
|
|
registry_info_downloader: Arc<RegistryInfoDownloader>,
|
2022-08-22 15:35:04 +00:00
|
|
|
) -> Self {
|
2023-04-12 12:36:11 +00:00
|
|
|
Self(Some(Arc::new(CliNpmRegistryApiInner {
|
2022-08-10 19:23:58 +00:00
|
|
|
cache,
|
2023-04-11 22:10:51 +00:00
|
|
|
force_reload_flag: Default::default(),
|
2022-08-10 19:23:58 +00:00
|
|
|
mem_cache: Default::default(),
|
2022-11-27 18:25:08 +00:00
|
|
|
previously_reloaded_packages: Default::default(),
|
2024-06-03 01:39:13 +00:00
|
|
|
registry_info_downloader,
|
2023-04-06 22:46:44 +00:00
|
|
|
})))
|
2022-08-10 19:23:58 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 17:03:48 +00:00
|
|
|
/// Clears the internal memory cache.
|
|
|
|
pub fn clear_memory_cache(&self) {
|
2023-04-06 22:46:44 +00:00
|
|
|
self.inner().clear_memory_cache();
|
2023-02-21 17:03:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:36:11 +00:00
|
|
|
fn inner(&self) -> &Arc<CliNpmRegistryApiInner> {
|
2023-04-06 22:46:44 +00:00
|
|
|
// this panicking indicates a bug in the code where this
|
|
|
|
// wasn't initialized
|
|
|
|
self.0.as_ref().unwrap()
|
|
|
|
}
|
2023-02-21 17:03:48 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 22:37:41 +00:00
|
|
|
#[async_trait(?Send)]
|
2023-04-12 12:36:11 +00:00
|
|
|
impl NpmRegistryApi for CliNpmRegistryApi {
|
2023-04-07 01:41:19 +00:00
|
|
|
async fn package_info(
|
2023-02-21 17:03:48 +00:00
|
|
|
&self,
|
|
|
|
name: &str,
|
2023-04-07 01:41:19 +00:00
|
|
|
) -> Result<Arc<NpmPackageInfo>, NpmRegistryPackageInfoLoadError> {
|
2023-09-14 17:51:28 +00:00
|
|
|
match self.inner().maybe_package_info(name).await {
|
2023-04-07 01:41:19 +00:00
|
|
|
Ok(Some(info)) => Ok(info),
|
|
|
|
Ok(None) => Err(NpmRegistryPackageInfoLoadError::PackageNotExists {
|
|
|
|
package_name: name.to_string(),
|
|
|
|
}),
|
|
|
|
Err(err) => {
|
|
|
|
Err(NpmRegistryPackageInfoLoadError::LoadError(Arc::new(err)))
|
|
|
|
}
|
2023-04-06 22:46:44 +00:00
|
|
|
}
|
2023-02-21 17:03:48 +00:00
|
|
|
}
|
2023-08-08 17:07:29 +00:00
|
|
|
|
|
|
|
fn mark_force_reload(&self) -> bool {
|
2024-06-05 15:04:16 +00:00
|
|
|
self.inner().mark_force_reload()
|
2023-08-08 17:07:29 +00:00
|
|
|
}
|
2023-04-06 22:46:44 +00:00
|
|
|
}
|
2023-02-21 17:03:48 +00:00
|
|
|
|
2023-04-12 12:36:11 +00:00
|
|
|
type CacheItemPendingResult =
|
|
|
|
Result<Option<Arc<NpmPackageInfo>>, Arc<AnyError>>;
|
|
|
|
|
2023-04-06 22:46:44 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum CacheItem {
|
2023-04-12 12:36:11 +00:00
|
|
|
Pending(Shared<BoxFuture<'static, CacheItemPendingResult>>),
|
2023-04-06 22:46:44 +00:00
|
|
|
Resolved(Option<Arc<NpmPackageInfo>>),
|
2022-11-08 19:17:24 +00:00
|
|
|
}
|
2022-08-10 19:23:58 +00:00
|
|
|
|
2023-02-21 17:03:48 +00:00
|
|
|
#[derive(Debug)]
|
2023-04-12 12:36:11 +00:00
|
|
|
struct CliNpmRegistryApiInner {
|
2023-04-14 20:22:33 +00:00
|
|
|
cache: Arc<NpmCache>,
|
2023-04-11 22:10:51 +00:00
|
|
|
force_reload_flag: AtomicFlag,
|
2023-04-06 22:46:44 +00:00
|
|
|
mem_cache: Mutex<HashMap<String, CacheItem>>,
|
2022-11-27 18:25:08 +00:00
|
|
|
previously_reloaded_packages: Mutex<HashSet<String>>,
|
2024-06-05 15:04:16 +00:00
|
|
|
registry_info_downloader: Arc<RegistryInfoDownloader>,
|
2022-11-08 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:36:11 +00:00
|
|
|
impl CliNpmRegistryApiInner {
|
2022-08-10 19:23:58 +00:00
|
|
|
pub async fn maybe_package_info(
|
2023-04-06 22:46:44 +00:00
|
|
|
self: &Arc<Self>,
|
2022-08-10 19:23:58 +00:00
|
|
|
name: &str,
|
2022-11-11 16:33:57 +00:00
|
|
|
) -> Result<Option<Arc<NpmPackageInfo>>, AnyError> {
|
2023-04-06 22:46:44 +00:00
|
|
|
let (created, future) = {
|
|
|
|
let mut mem_cache = self.mem_cache.lock();
|
|
|
|
match mem_cache.get(name) {
|
|
|
|
Some(CacheItem::Resolved(maybe_info)) => {
|
|
|
|
return Ok(maybe_info.clone());
|
|
|
|
}
|
|
|
|
Some(CacheItem::Pending(future)) => (false, future.clone()),
|
|
|
|
None => {
|
|
|
|
let future = {
|
|
|
|
let api = self.clone();
|
|
|
|
let name = name.to_string();
|
2023-04-12 12:36:11 +00:00
|
|
|
async move {
|
2024-06-05 15:04:16 +00:00
|
|
|
if (api.cache.cache_setting().should_use_for_npm_package(&name) && !api.force_reload_flag.is_raised())
|
2024-05-28 21:17:36 +00:00
|
|
|
// if this has been previously reloaded, then try loading from the
|
|
|
|
// file system cache
|
|
|
|
|| !api.previously_reloaded_packages.lock().insert(name.to_string())
|
|
|
|
{
|
|
|
|
// attempt to load from the file cache
|
|
|
|
if let Some(info) = api.load_file_cached_package_info(&name).await {
|
|
|
|
let result = Some(Arc::new(info));
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
}
|
2024-06-03 01:39:13 +00:00
|
|
|
api.registry_info_downloader
|
2024-06-03 21:17:08 +00:00
|
|
|
.load_package_info(&name)
|
2023-04-12 12:36:11 +00:00
|
|
|
.await
|
|
|
|
.map_err(Arc::new)
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
.shared()
|
2023-04-06 22:46:44 +00:00
|
|
|
};
|
|
|
|
mem_cache
|
|
|
|
.insert(name.to_string(), CacheItem::Pending(future.clone()));
|
|
|
|
(true, future)
|
|
|
|
}
|
2022-08-10 19:23:58 +00:00
|
|
|
}
|
2023-04-06 22:46:44 +00:00
|
|
|
};
|
2022-08-10 19:23:58 +00:00
|
|
|
|
2023-04-06 22:46:44 +00:00
|
|
|
if created {
|
|
|
|
match future.await {
|
|
|
|
Ok(maybe_info) => {
|
|
|
|
// replace the cache item to say it's resolved now
|
|
|
|
self
|
|
|
|
.mem_cache
|
|
|
|
.lock()
|
|
|
|
.insert(name.to_string(), CacheItem::Resolved(maybe_info.clone()));
|
|
|
|
Ok(maybe_info)
|
2022-08-10 19:23:58 +00:00
|
|
|
}
|
2023-04-06 22:46:44 +00:00
|
|
|
Err(err) => {
|
|
|
|
// purge the item from the cache so it loads next time
|
|
|
|
self.mem_cache.lock().remove(name);
|
2023-04-12 12:36:11 +00:00
|
|
|
Err(anyhow!("{:#}", err))
|
2023-04-06 22:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-12 12:36:11 +00:00
|
|
|
Ok(future.await.map_err(|err| anyhow!("{:#}", err))?)
|
2022-08-10 19:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:04:16 +00:00
|
|
|
fn mark_force_reload(&self) -> bool {
|
|
|
|
// never force reload the registry information if reloading
|
|
|
|
// is disabled or if we're already reloading
|
|
|
|
if matches!(
|
|
|
|
self.cache.cache_setting(),
|
|
|
|
CacheSetting::Only | CacheSetting::ReloadAll
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if self.force_reload_flag.raise() {
|
|
|
|
self.clear_memory_cache();
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2023-04-07 01:41:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:17:36 +00:00
|
|
|
async fn load_file_cached_package_info(
|
2022-08-10 19:23:58 +00:00
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
) -> Option<NpmPackageInfo> {
|
2024-06-03 01:39:13 +00:00
|
|
|
let result = deno_core::unsync::spawn_blocking({
|
|
|
|
let cache = self.cache.clone();
|
|
|
|
let name = name.to_string();
|
|
|
|
move || cache.load_package_info(&name)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
match result {
|
2022-08-22 15:35:04 +00:00
|
|
|
Ok(value) => value,
|
2022-08-10 19:23:58 +00:00
|
|
|
Err(err) => {
|
|
|
|
if cfg!(debug_assertions) {
|
2023-01-27 15:43:16 +00:00
|
|
|
panic!("error loading cached npm package info for {name}: {err:#}");
|
2022-08-10 19:23:58 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:07:29 +00:00
|
|
|
fn clear_memory_cache(&self) {
|
2023-04-06 22:46:44 +00:00
|
|
|
self.mem_cache.lock().clear();
|
2022-11-08 19:17:24 +00:00
|
|
|
}
|
2023-02-22 18:16:40 +00:00
|
|
|
}
|