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
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2022-11-27 13:25:08 -05:00
|
|
|
use std::collections::HashSet;
|
2022-08-10 15:23:58 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
use async_trait::async_trait;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_core::anyhow::anyhow;
|
2022-08-10 15:23:58 -04:00
|
|
|
use deno_core::error::AnyError;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_core::futures::future::BoxFuture;
|
|
|
|
use deno_core::futures::future::Shared;
|
|
|
|
use deno_core::futures::FutureExt;
|
2022-08-10 15:23:58 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::registry::NpmPackageInfo;
|
|
|
|
use deno_npm::registry::NpmRegistryApi;
|
2023-04-06 21:41:19 -04:00
|
|
|
use deno_npm::registry::NpmRegistryPackageInfoLoadError;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::args::CacheSetting;
|
2023-04-11 18:10:51 -04:00
|
|
|
use crate::util::sync::AtomicFlag;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
|
|
|
use super::cache::NpmCache;
|
2024-06-02 21:39:13 -04:00
|
|
|
use super::cache::RegistryInfoDownloader;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
#[derive(Debug)]
|
2023-04-12 08:36:11 -04:00
|
|
|
pub struct CliNpmRegistryApi(Option<Arc<CliNpmRegistryApiInner>>);
|
2022-12-07 16:10:50 -05:00
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
impl CliNpmRegistryApi {
|
2022-08-22 11:35:04 -04:00
|
|
|
pub fn new(
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: Arc<NpmCache>,
|
2024-06-05 11:04:16 -04:00
|
|
|
registry_info_downloader: Arc<RegistryInfoDownloader>,
|
2022-08-22 11:35:04 -04:00
|
|
|
) -> Self {
|
2023-04-12 08:36:11 -04:00
|
|
|
Self(Some(Arc::new(CliNpmRegistryApiInner {
|
2022-08-10 15:23:58 -04:00
|
|
|
cache,
|
2023-04-11 18:10:51 -04:00
|
|
|
force_reload_flag: Default::default(),
|
2022-08-10 15:23:58 -04:00
|
|
|
mem_cache: Default::default(),
|
2022-11-27 13:25:08 -05:00
|
|
|
previously_reloaded_packages: Default::default(),
|
2024-06-02 21:39:13 -04:00
|
|
|
registry_info_downloader,
|
2023-04-06 18:46:44 -04:00
|
|
|
})))
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
/// Clears the internal memory cache.
|
|
|
|
pub fn clear_memory_cache(&self) {
|
2023-04-06 18:46:44 -04:00
|
|
|
self.inner().clear_memory_cache();
|
2023-02-21 12:03:48 -05:00
|
|
|
}
|
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
fn inner(&self) -> &Arc<CliNpmRegistryApiInner> {
|
2023-04-06 18:46:44 -04:00
|
|
|
// this panicking indicates a bug in the code where this
|
|
|
|
// wasn't initialized
|
|
|
|
self.0.as_ref().unwrap()
|
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
}
|
|
|
|
|
2024-06-06 18:37:41 -04:00
|
|
|
#[async_trait(?Send)]
|
2023-04-12 08:36:11 -04:00
|
|
|
impl NpmRegistryApi for CliNpmRegistryApi {
|
2023-04-06 21:41:19 -04:00
|
|
|
async fn package_info(
|
2023-02-21 12:03:48 -05:00
|
|
|
&self,
|
|
|
|
name: &str,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<Arc<NpmPackageInfo>, NpmRegistryPackageInfoLoadError> {
|
2023-09-14 13:51:28 -04:00
|
|
|
match self.inner().maybe_package_info(name).await {
|
2023-04-06 21:41:19 -04: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 18:46:44 -04:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
}
|
2023-08-08 13:07:29 -04:00
|
|
|
|
|
|
|
fn mark_force_reload(&self) -> bool {
|
2024-06-05 11:04:16 -04:00
|
|
|
self.inner().mark_force_reload()
|
2023-08-08 13:07:29 -04:00
|
|
|
}
|
2023-04-06 18:46:44 -04:00
|
|
|
}
|
2023-02-21 12:03:48 -05:00
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
type CacheItemPendingResult =
|
|
|
|
Result<Option<Arc<NpmPackageInfo>>, Arc<AnyError>>;
|
|
|
|
|
2023-04-06 18:46:44 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum CacheItem {
|
2023-04-12 08:36:11 -04:00
|
|
|
Pending(Shared<BoxFuture<'static, CacheItemPendingResult>>),
|
2023-04-06 18:46:44 -04:00
|
|
|
Resolved(Option<Arc<NpmPackageInfo>>),
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2023-02-21 12:03:48 -05:00
|
|
|
#[derive(Debug)]
|
2023-04-12 08:36:11 -04:00
|
|
|
struct CliNpmRegistryApiInner {
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: Arc<NpmCache>,
|
2023-04-11 18:10:51 -04:00
|
|
|
force_reload_flag: AtomicFlag,
|
2023-04-06 18:46:44 -04:00
|
|
|
mem_cache: Mutex<HashMap<String, CacheItem>>,
|
2022-11-27 13:25:08 -05:00
|
|
|
previously_reloaded_packages: Mutex<HashSet<String>>,
|
2024-06-05 11:04:16 -04:00
|
|
|
registry_info_downloader: Arc<RegistryInfoDownloader>,
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2023-04-12 08:36:11 -04:00
|
|
|
impl CliNpmRegistryApiInner {
|
2022-08-10 15:23:58 -04:00
|
|
|
pub async fn maybe_package_info(
|
2023-04-06 18:46:44 -04:00
|
|
|
self: &Arc<Self>,
|
2022-08-10 15:23:58 -04:00
|
|
|
name: &str,
|
2022-11-11 11:33:57 -05:00
|
|
|
) -> Result<Option<Arc<NpmPackageInfo>>, AnyError> {
|
2023-04-06 18:46:44 -04: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 08:36:11 -04:00
|
|
|
async move {
|
2024-06-05 11:04:16 -04:00
|
|
|
if (api.cache.cache_setting().should_use_for_npm_package(&name) && !api.force_reload_flag.is_raised())
|
2024-05-28 17:17:36 -04: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-02 21:39:13 -04:00
|
|
|
api.registry_info_downloader
|
2024-06-03 17:17:08 -04:00
|
|
|
.load_package_info(&name)
|
2023-04-12 08:36:11 -04:00
|
|
|
.await
|
|
|
|
.map_err(Arc::new)
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
.shared()
|
2023-04-06 18:46:44 -04:00
|
|
|
};
|
|
|
|
mem_cache
|
|
|
|
.insert(name.to_string(), CacheItem::Pending(future.clone()));
|
|
|
|
(true, future)
|
|
|
|
}
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
2023-04-06 18:46:44 -04:00
|
|
|
};
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2023-04-06 18:46:44 -04: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 15:23:58 -04:00
|
|
|
}
|
2023-04-06 18:46:44 -04:00
|
|
|
Err(err) => {
|
|
|
|
// purge the item from the cache so it loads next time
|
|
|
|
self.mem_cache.lock().remove(name);
|
2023-04-12 08:36:11 -04:00
|
|
|
Err(anyhow!("{:#}", err))
|
2023-04-06 18:46:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-12 08:36:11 -04:00
|
|
|
Ok(future.await.map_err(|err| anyhow!("{:#}", err))?)
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 11:04:16 -04: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-06 21:41:19 -04:00
|
|
|
}
|
|
|
|
|
2024-05-28 17:17:36 -04:00
|
|
|
async fn load_file_cached_package_info(
|
2022-08-10 15:23:58 -04:00
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
) -> Option<NpmPackageInfo> {
|
2024-06-02 21:39:13 -04: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 11:35:04 -04:00
|
|
|
Ok(value) => value,
|
2022-08-10 15:23:58 -04:00
|
|
|
Err(err) => {
|
|
|
|
if cfg!(debug_assertions) {
|
2023-01-27 10:43:16 -05:00
|
|
|
panic!("error loading cached npm package info for {name}: {err:#}");
|
2022-08-10 15:23:58 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 13:07:29 -04:00
|
|
|
fn clear_memory_cache(&self) {
|
2023-04-06 18:46:44 -04:00
|
|
|
self.mem_cache.lock().clear();
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
2023-02-22 13:16:40 -05:00
|
|
|
}
|