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
|
|
|
|
2022-11-27 13:25:08 -05:00
|
|
|
use std::collections::HashSet;
|
2022-08-10 15:23:58 -04:00
|
|
|
use std::fs;
|
2024-06-02 21:39:13 -04:00
|
|
|
use std::io::ErrorKind;
|
2022-09-01 12:37:14 -04:00
|
|
|
use std::path::Path;
|
2022-08-10 15:23:58 -04:00
|
|
|
use std::path::PathBuf;
|
2023-05-01 16:42:05 -04:00
|
|
|
use std::sync::Arc;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
|
|
|
use deno_ast::ModuleSpecifier;
|
2024-09-28 08:50:16 -04:00
|
|
|
use deno_cache_dir::npm::NpmCacheDir;
|
2022-08-10 15:23:58 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2022-08-20 11:31:33 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2022-08-10 15:23:58 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-11-27 13:25:08 -05:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2024-06-02 21:39:13 -04:00
|
|
|
use deno_core::serde_json;
|
2022-08-10 15:23:58 -04:00
|
|
|
use deno_core::url::Url;
|
2024-05-23 17:26:23 -04:00
|
|
|
use deno_npm::npm_rc::ResolvedNpmRc;
|
2024-06-02 21:39:13 -04:00
|
|
|
use deno_npm::registry::NpmPackageInfo;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::NpmPackageCacheFolderId;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
2024-09-28 08:50:16 -04:00
|
|
|
use deno_semver::Version;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::args::CacheSetting;
|
2024-06-02 21:39:13 -04:00
|
|
|
use crate::cache::CACHE_PERM;
|
|
|
|
use crate::util::fs::atomic_write_file_with_retries;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::fs::hard_link_dir_recursive;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2024-06-02 21:39:13 -04:00
|
|
|
mod registry_info;
|
|
|
|
mod tarball;
|
|
|
|
mod tarball_extract;
|
|
|
|
|
|
|
|
pub use registry_info::RegistryInfoDownloader;
|
|
|
|
pub use tarball::TarballCache;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
|
|
|
/// Stores a single copy of npm packages in a cache.
|
2023-04-14 16:22:33 -04:00
|
|
|
#[derive(Debug)]
|
2022-08-22 11:35:04 -04:00
|
|
|
pub struct NpmCache {
|
2023-06-08 11:48:29 -04:00
|
|
|
cache_dir: NpmCacheDir,
|
2022-08-22 11:35:04 -04:00
|
|
|
cache_setting: CacheSetting,
|
2024-06-02 21:39:13 -04:00
|
|
|
npmrc: Arc<ResolvedNpmRc>,
|
2022-11-27 13:25:08 -05:00
|
|
|
/// ensures a package is only downloaded once per run
|
2023-08-21 05:53:52 -04:00
|
|
|
previously_reloaded_packages: Mutex<HashSet<PackageNv>>,
|
2022-08-22 11:35:04 -04:00
|
|
|
}
|
2022-08-10 15:23:58 -04:00
|
|
|
|
|
|
|
impl NpmCache {
|
2023-04-26 13:07:15 -04:00
|
|
|
pub fn new(
|
2023-06-08 11:48:29 -04:00
|
|
|
cache_dir: NpmCacheDir,
|
2022-09-09 15:57:39 -04:00
|
|
|
cache_setting: CacheSetting,
|
2024-05-23 17:26:23 -04:00
|
|
|
npmrc: Arc<ResolvedNpmRc>,
|
2022-09-09 15:57:39 -04:00
|
|
|
) -> Self {
|
2022-09-01 12:37:14 -04:00
|
|
|
Self {
|
2023-06-08 11:48:29 -04:00
|
|
|
cache_dir,
|
2022-08-22 11:35:04 -04:00
|
|
|
cache_setting,
|
2022-11-27 13:25:08 -05:00
|
|
|
previously_reloaded_packages: Default::default(),
|
2024-05-23 17:26:23 -04:00
|
|
|
npmrc,
|
2022-09-01 12:37:14 -04:00
|
|
|
}
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
|
2022-11-27 13:25:08 -05:00
|
|
|
pub fn cache_setting(&self) -> &CacheSetting {
|
|
|
|
&self.cache_setting
|
|
|
|
}
|
|
|
|
|
2023-02-23 10:58:10 -05:00
|
|
|
pub fn root_dir_url(&self) -> &Url {
|
2023-06-08 11:48:29 -04:00
|
|
|
self.cache_dir.root_dir_url()
|
2023-02-23 10:58:10 -05:00
|
|
|
}
|
|
|
|
|
2022-11-27 13:25:08 -05:00
|
|
|
/// Checks if the cache should be used for the provided name and version.
|
|
|
|
/// NOTE: Subsequent calls for the same package will always return `true`
|
|
|
|
/// to ensure a package is only downloaded once per run of the CLI. This
|
|
|
|
/// prevents downloads from re-occurring when someone has `--reload` and
|
|
|
|
/// and imports a dynamic import that imports the same package again for example.
|
2024-06-02 21:39:13 -04:00
|
|
|
pub fn should_use_cache_for_package(&self, package: &PackageNv) -> bool {
|
2023-02-22 14:15:25 -05:00
|
|
|
self.cache_setting.should_use_for_npm_package(&package.name)
|
2022-11-27 13:25:08 -05:00
|
|
|
|| !self
|
|
|
|
.previously_reloaded_packages
|
|
|
|
.lock()
|
2023-02-22 14:15:25 -05:00
|
|
|
.insert(package.clone())
|
2022-11-27 13:25:08 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
/// Ensures a copy of the package exists in the global cache.
|
|
|
|
///
|
|
|
|
/// This assumes that the original package folder being hard linked
|
|
|
|
/// from exists before this is called.
|
|
|
|
pub fn ensure_copy_package(
|
2022-08-10 15:23:58 -04:00
|
|
|
&self,
|
2023-02-22 14:15:25 -05:00
|
|
|
folder_id: &NpmPackageCacheFolderId,
|
2022-11-08 14:17:24 -05:00
|
|
|
) -> Result<(), AnyError> {
|
2024-05-23 17:26:23 -04:00
|
|
|
let registry_url = self.npmrc.get_registry_url(&folder_id.nv.name);
|
2023-02-22 14:15:25 -05:00
|
|
|
assert_ne!(folder_id.copy_index, 0);
|
2024-09-28 08:50:16 -04:00
|
|
|
let package_folder = self.cache_dir.package_folder_for_id(
|
|
|
|
&folder_id.nv.name,
|
|
|
|
&folder_id.nv.version.to_string(),
|
|
|
|
folder_id.copy_index,
|
|
|
|
registry_url,
|
|
|
|
);
|
2022-11-08 14:17:24 -05:00
|
|
|
|
|
|
|
if package_folder.exists()
|
2024-05-14 14:26:48 -04:00
|
|
|
// if this file exists, then the package didn't successfully initialize
|
2022-11-08 14:17:24 -05:00
|
|
|
// the first time, or another process is currently extracting the zip file
|
|
|
|
&& !package_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME).exists()
|
2023-02-22 14:15:25 -05:00
|
|
|
&& self.cache_setting.should_use_for_npm_package(&folder_id.nv.name)
|
2022-11-08 14:17:24 -05:00
|
|
|
{
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2024-09-28 08:50:16 -04:00
|
|
|
let original_package_folder = self.cache_dir.package_folder_for_id(
|
|
|
|
&folder_id.nv.name,
|
|
|
|
&folder_id.nv.version.to_string(),
|
|
|
|
0, // original copy index
|
|
|
|
registry_url,
|
|
|
|
);
|
2024-05-14 14:26:48 -04:00
|
|
|
|
|
|
|
// it seems Windows does an "AccessDenied" error when moving a
|
|
|
|
// directory with hard links, so that's why this solution is done
|
2023-02-22 14:15:25 -05:00
|
|
|
with_folder_sync_lock(&folder_id.nv, &package_folder, || {
|
|
|
|
hard_link_dir_recursive(&original_package_folder, &package_folder)
|
|
|
|
})?;
|
2022-11-08 14:17:24 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-05-23 17:26:23 -04:00
|
|
|
pub fn package_folder_for_id(&self, id: &NpmPackageCacheFolderId) -> PathBuf {
|
|
|
|
let registry_url = self.npmrc.get_registry_url(&id.nv.name);
|
2024-09-28 08:50:16 -04:00
|
|
|
self.cache_dir.package_folder_for_id(
|
|
|
|
&id.nv.name,
|
|
|
|
&id.nv.version.to_string(),
|
|
|
|
id.copy_index,
|
|
|
|
registry_url,
|
|
|
|
)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2024-06-02 21:39:13 -04:00
|
|
|
pub fn package_folder_for_nv(&self, package: &PackageNv) -> PathBuf {
|
|
|
|
let registry_url = self.npmrc.get_registry_url(&package.name);
|
|
|
|
self.package_folder_for_nv_and_url(package, registry_url)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn package_folder_for_nv_and_url(
|
2022-11-08 14:17:24 -05:00
|
|
|
&self,
|
2023-08-21 05:53:52 -04:00
|
|
|
package: &PackageNv,
|
2024-06-02 21:39:13 -04:00
|
|
|
registry_url: &Url,
|
2022-08-10 15:23:58 -04:00
|
|
|
) -> PathBuf {
|
2024-09-28 08:50:16 -04:00
|
|
|
self.cache_dir.package_folder_for_id(
|
|
|
|
&package.name,
|
|
|
|
&package.version.to_string(),
|
|
|
|
0, // original copy_index
|
|
|
|
registry_url,
|
|
|
|
)
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
|
2024-05-23 17:26:23 -04:00
|
|
|
pub fn package_name_folder(&self, name: &str) -> PathBuf {
|
|
|
|
let registry_url = self.npmrc.get_registry_url(name);
|
2023-06-08 11:48:29 -04:00
|
|
|
self.cache_dir.package_name_folder(name, registry_url)
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
|
2024-05-23 17:26:23 -04:00
|
|
|
pub fn root_folder(&self) -> PathBuf {
|
|
|
|
self.cache_dir.root_dir().to_owned()
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2022-11-08 14:17:24 -05:00
|
|
|
pub fn resolve_package_folder_id_from_specifier(
|
2022-08-10 15:23:58 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-07-17 14:00:44 -04:00
|
|
|
) -> Option<NpmPackageCacheFolderId> {
|
2022-08-10 15:23:58 -04:00
|
|
|
self
|
2023-06-08 11:48:29 -04:00
|
|
|
.cache_dir
|
2024-05-23 17:26:23 -04:00
|
|
|
.resolve_package_folder_id_from_specifier(specifier)
|
2024-09-28 08:50:16 -04:00
|
|
|
.and_then(|cache_id| {
|
|
|
|
Some(NpmPackageCacheFolderId {
|
|
|
|
nv: PackageNv {
|
|
|
|
name: cache_id.name,
|
|
|
|
version: Version::parse_from_npm(&cache_id.version).ok()?,
|
|
|
|
},
|
|
|
|
copy_index: cache_id.copy_index,
|
|
|
|
})
|
|
|
|
})
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
2024-06-02 21:39:13 -04:00
|
|
|
|
|
|
|
pub fn load_package_info(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
) -> Result<Option<NpmPackageInfo>, AnyError> {
|
|
|
|
let file_cache_path = self.get_registry_package_info_file_cache_path(name);
|
|
|
|
|
|
|
|
let file_text = match fs::read_to_string(file_cache_path) {
|
|
|
|
Ok(file_text) => file_text,
|
|
|
|
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(None),
|
|
|
|
Err(err) => return Err(err.into()),
|
|
|
|
};
|
|
|
|
Ok(serde_json::from_str(&file_text)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_package_info(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
package_info: &NpmPackageInfo,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let file_cache_path = self.get_registry_package_info_file_cache_path(name);
|
|
|
|
let file_text = serde_json::to_string(&package_info)?;
|
|
|
|
atomic_write_file_with_retries(&file_cache_path, file_text, CACHE_PERM)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_registry_package_info_file_cache_path(&self, name: &str) -> PathBuf {
|
|
|
|
let name_folder_path = self.package_name_folder(name);
|
|
|
|
name_folder_path.join("registry.json")
|
|
|
|
}
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
2022-08-14 09:09:16 -04:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
const NPM_PACKAGE_SYNC_LOCK_FILENAME: &str = ".deno_sync_lock";
|
2022-08-14 09:09:16 -04:00
|
|
|
|
2024-05-14 14:26:48 -04:00
|
|
|
fn with_folder_sync_lock(
|
2023-10-02 17:53:55 -04:00
|
|
|
package: &PackageNv,
|
|
|
|
output_folder: &Path,
|
|
|
|
action: impl FnOnce() -> Result<(), AnyError>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
fn inner(
|
|
|
|
output_folder: &Path,
|
|
|
|
action: impl FnOnce() -> Result<(), AnyError>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
fs::create_dir_all(output_folder).with_context(|| {
|
|
|
|
format!("Error creating '{}'.", output_folder.display())
|
|
|
|
})?;
|
2022-11-16 13:44:31 -05:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
// This sync lock file is a way to ensure that partially created
|
|
|
|
// npm package directories aren't considered valid. This could maybe
|
|
|
|
// be a bit smarter in the future to not bother extracting here
|
|
|
|
// if another process has taken the lock in the past X seconds and
|
|
|
|
// wait for the other process to finish (it could try to create the
|
|
|
|
// file with `create_new(true)` then if it exists, check the metadata
|
|
|
|
// then wait until the other process finishes with a timeout), but
|
|
|
|
// for now this is good enough.
|
|
|
|
let sync_lock_path = output_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME);
|
|
|
|
match fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
2024-04-10 18:08:23 -04:00
|
|
|
.truncate(false)
|
2023-10-02 17:53:55 -04:00
|
|
|
.open(&sync_lock_path)
|
|
|
|
{
|
|
|
|
Ok(_) => {
|
|
|
|
action()?;
|
|
|
|
// extraction succeeded, so only now delete this file
|
|
|
|
let _ignore = std::fs::remove_file(&sync_lock_path);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
bail!(
|
|
|
|
concat!(
|
|
|
|
"Error creating package sync lock file at '{}'. ",
|
|
|
|
"Maybe try manually deleting this folder.\n\n{:#}",
|
|
|
|
),
|
|
|
|
output_folder.display(),
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 13:44:31 -05:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
match inner(output_folder, action) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(err) => {
|
|
|
|
if let Err(remove_err) = fs::remove_dir_all(output_folder) {
|
|
|
|
if remove_err.kind() != std::io::ErrorKind::NotFound {
|
|
|
|
bail!(
|
|
|
|
concat!(
|
|
|
|
"Failed setting up package cache directory for {}, then ",
|
|
|
|
"failed cleaning it up.\n\nOriginal error:\n\n{}\n\n",
|
|
|
|
"Remove error:\n\n{}\n\nPlease manually ",
|
|
|
|
"delete this folder or you will run into issues using this ",
|
|
|
|
"package in the future:\n\n{}"
|
|
|
|
),
|
|
|
|
package,
|
|
|
|
err,
|
|
|
|
remove_err,
|
|
|
|
output_folder.display(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err)
|
|
|
|
}
|
2022-08-14 09:09:16 -04:00
|
|
|
}
|
|
|
|
}
|