2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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;
|
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;
|
|
|
|
use deno_core::anyhow::bail;
|
2022-08-20 11:31:33 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2022-08-22 11:35:04 -04:00
|
|
|
use deno_core::error::custom_error;
|
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;
|
2022-08-10 15:23:58 -04:00
|
|
|
use deno_core::url::Url;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::registry::NpmPackageVersionDistInfo;
|
|
|
|
use deno_npm::NpmPackageCacheFolderId;
|
2023-06-08 11:48:29 -04:00
|
|
|
use deno_runtime::deno_fs;
|
2023-08-21 05:53:52 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::args::CacheSetting;
|
2022-11-18 17:28:14 -05:00
|
|
|
use crate::http_util::HttpClient;
|
2023-10-02 17:53:55 -04:00
|
|
|
use crate::npm::NpmCacheDir;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::fs::hard_link_dir_recursive;
|
|
|
|
use crate::util::progress_bar::ProgressBar;
|
2022-08-10 15:23:58 -04:00
|
|
|
|
|
|
|
use super::tarball::verify_and_extract_tarball;
|
|
|
|
|
|
|
|
/// 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,
|
2023-06-08 11:48:29 -04:00
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2023-05-01 16:42:05 -04:00
|
|
|
http_client: Arc<HttpClient>,
|
2022-09-09 15:57:39 -04:00
|
|
|
progress_bar: ProgressBar,
|
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,
|
2023-06-08 11:48:29 -04:00
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2023-05-01 16:42:05 -04:00
|
|
|
http_client: Arc<HttpClient>,
|
2022-09-09 15:57:39 -04:00
|
|
|
progress_bar: ProgressBar,
|
|
|
|
) -> 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,
|
2023-06-08 11:48:29 -04:00
|
|
|
fs,
|
2022-11-18 17:28:14 -05:00
|
|
|
http_client,
|
2022-09-09 15:57:39 -04:00
|
|
|
progress_bar,
|
2022-11-27 13:25:08 -05:00
|
|
|
previously_reloaded_packages: Default::default(),
|
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.
|
2023-08-21 05:53:52 -04:00
|
|
|
fn should_use_global_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-08-10 15:23:58 -04:00
|
|
|
pub async fn ensure_package(
|
|
|
|
&self,
|
2023-08-21 05:53:52 -04:00
|
|
|
package: &PackageNv,
|
2022-08-10 15:23:58 -04:00
|
|
|
dist: &NpmPackageVersionDistInfo,
|
|
|
|
registry_url: &Url,
|
2022-09-23 17:35:48 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self
|
2022-11-08 14:17:24 -05:00
|
|
|
.ensure_package_inner(package, dist, registry_url)
|
2022-09-23 17:35:48 -04:00
|
|
|
.await
|
2023-02-22 14:15:25 -05:00
|
|
|
.with_context(|| format!("Failed caching npm package '{package}'."))
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
2022-09-23 17:35:48 -04:00
|
|
|
async fn ensure_package_inner(
|
|
|
|
&self,
|
2023-08-21 05:53:52 -04:00
|
|
|
package: &PackageNv,
|
2022-09-23 17:35:48 -04:00
|
|
|
dist: &NpmPackageVersionDistInfo,
|
|
|
|
registry_url: &Url,
|
2022-08-10 15:23:58 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2023-02-22 14:15:25 -05:00
|
|
|
let package_folder = self
|
2023-06-08 11:48:29 -04:00
|
|
|
.cache_dir
|
2023-02-22 14:15:25 -05:00
|
|
|
.package_folder_for_name_and_version(package, registry_url);
|
2022-11-27 13:25:08 -05:00
|
|
|
if self.should_use_global_cache_for_package(package)
|
2023-08-08 16:28:18 -04:00
|
|
|
&& self.fs.exists_sync(&package_folder)
|
2022-08-10 15:23:58 -04:00
|
|
|
// if this file exists, then the package didn't successfully extract
|
|
|
|
// the first time, or another process is currently extracting the zip file
|
2023-08-08 16:28:18 -04:00
|
|
|
&& !self.fs.exists_sync(&package_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME))
|
2022-08-10 15:23:58 -04:00
|
|
|
{
|
|
|
|
return Ok(());
|
2022-08-22 11:35:04 -04:00
|
|
|
} else if self.cache_setting == CacheSetting::Only {
|
|
|
|
return Err(custom_error(
|
|
|
|
"NotCached",
|
|
|
|
format!(
|
|
|
|
"An npm specifier not found in cache: \"{}\", --cached-only is specified.",
|
2023-02-22 14:15:25 -05:00
|
|
|
&package.name
|
2022-08-22 11:35:04 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
|
2023-06-08 11:48:29 -04:00
|
|
|
if dist.tarball.is_empty() {
|
|
|
|
bail!("Tarball URL was empty.");
|
|
|
|
}
|
|
|
|
|
2022-12-12 20:52:10 -05:00
|
|
|
let guard = self.progress_bar.update(&dist.tarball);
|
|
|
|
let maybe_bytes = self
|
|
|
|
.http_client
|
|
|
|
.download_with_progress(&dist.tarball, &guard)
|
|
|
|
.await?;
|
|
|
|
match maybe_bytes {
|
|
|
|
Some(bytes) => {
|
|
|
|
verify_and_extract_tarball(package, &bytes, dist, &package_folder)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bail!("Could not find npm package tarball at: {}", dist.tarball);
|
|
|
|
}
|
2022-08-10 15:23:58 -04: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
|
|
|
registry_url: &Url,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-02-22 14:15:25 -05:00
|
|
|
assert_ne!(folder_id.copy_index, 0);
|
2023-06-08 11:48:29 -04:00
|
|
|
let package_folder = self
|
|
|
|
.cache_dir
|
|
|
|
.package_folder_for_id(folder_id, registry_url);
|
2022-11-08 14:17:24 -05:00
|
|
|
|
|
|
|
if package_folder.exists()
|
|
|
|
// if this file exists, then the package didn't successfully extract
|
|
|
|
// 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(());
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
let original_package_folder = self
|
2023-06-08 11:48:29 -04:00
|
|
|
.cache_dir
|
2023-02-22 14:15:25 -05:00
|
|
|
.package_folder_for_name_and_version(&folder_id.nv, registry_url);
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn package_folder_for_id(
|
|
|
|
&self,
|
|
|
|
id: &NpmPackageCacheFolderId,
|
|
|
|
registry_url: &Url,
|
|
|
|
) -> PathBuf {
|
2023-06-08 11:48:29 -04:00
|
|
|
self.cache_dir.package_folder_for_id(id, registry_url)
|
2022-11-08 14:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn package_folder_for_name_and_version(
|
|
|
|
&self,
|
2023-08-21 05:53:52 -04:00
|
|
|
package: &PackageNv,
|
2022-08-10 15:23:58 -04:00
|
|
|
registry_url: &Url,
|
|
|
|
) -> PathBuf {
|
2023-02-22 14:15:25 -05:00
|
|
|
self
|
2023-06-08 11:48:29 -04:00
|
|
|
.cache_dir
|
2023-02-22 14:15:25 -05:00
|
|
|
.package_folder_for_name_and_version(package, registry_url)
|
2022-08-10 15:23:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn package_name_folder(&self, name: &str, registry_url: &Url) -> PathBuf {
|
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
|
|
|
}
|
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
pub fn registry_folder(&self, registry_url: &Url) -> PathBuf {
|
2023-06-08 11:48:29 -04:00
|
|
|
self.cache_dir.registry_folder(registry_url)
|
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,
|
|
|
|
registry_url: &Url,
|
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
|
2022-11-08 14:17:24 -05:00
|
|
|
.resolve_package_folder_id_from_specifier(specifier, registry_url)
|
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
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
pub fn with_folder_sync_lock(
|
|
|
|
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)
|
|
|
|
.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
|
|
|
}
|
|
|
|
}
|