mirror of
https://github.com/denoland/deno.git
synced 2025-08-02 18:12:39 +00:00
refactor: move NpmCacheDir to deno_cache_dir (#25916)
Part of the ongoing work to move more of Deno's resolution out of the CLI crate (for use in Wasm and other things) Includes: * https://github.com/denoland/deno_cache_dir/pull/60
This commit is contained in:
parent
fc739dc5eb
commit
1bb47805d6
16 changed files with 267 additions and 411 deletions
5
Cargo.lock
generated
5
Cargo.lock
generated
|
@ -1348,10 +1348,11 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "deno_cache_dir"
|
name = "deno_cache_dir"
|
||||||
version = "0.11.1"
|
version = "0.12.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6df43311cb7703fa3242c282823a850e4c8d0c06b9527d8209b55bd695452ea5"
|
checksum = "87900cfcd07bdbf3597bc36b77da0c0e7b6c2e65213faa2ed43d9a1ec12bd31d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"base32",
|
||||||
"deno_media_type",
|
"deno_media_type",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -104,7 +104,7 @@ chrono = { version = "0.4", default-features = false, features = ["std", "serde"
|
||||||
console_static_text = "=0.8.1"
|
console_static_text = "=0.8.1"
|
||||||
data-encoding = "2.3.3"
|
data-encoding = "2.3.3"
|
||||||
data-url = "=0.3.0"
|
data-url = "=0.3.0"
|
||||||
deno_cache_dir = "=0.11.1"
|
deno_cache_dir = "=0.12.0"
|
||||||
deno_package_json = { version = "=0.1.1", default-features = false }
|
deno_package_json = { version = "=0.1.1", default-features = false }
|
||||||
dlopen2 = "0.6.1"
|
dlopen2 = "0.6.1"
|
||||||
ecb = "=0.1.2"
|
ecb = "=0.1.2"
|
||||||
|
|
77
cli/cache/mod.rs
vendored
77
cli/cache/mod.rs
vendored
|
@ -10,6 +10,8 @@ use crate::file_fetcher::FileFetcher;
|
||||||
use crate::file_fetcher::FileOrRedirect;
|
use crate::file_fetcher::FileOrRedirect;
|
||||||
use crate::npm::CliNpmResolver;
|
use crate::npm::CliNpmResolver;
|
||||||
use crate::util::fs::atomic_write_file_with_retries;
|
use crate::util::fs::atomic_write_file_with_retries;
|
||||||
|
use crate::util::fs::atomic_write_file_with_retries_and_fs;
|
||||||
|
use crate::util::fs::AtomicWriteFileFsAdapter;
|
||||||
use crate::util::path::specifier_has_extension;
|
use crate::util::path::specifier_has_extension;
|
||||||
|
|
||||||
use deno_ast::MediaType;
|
use deno_ast::MediaType;
|
||||||
|
@ -77,6 +79,14 @@ impl deno_cache_dir::DenoCacheEnv for RealDenoCacheEnv {
|
||||||
atomic_write_file_with_retries(path, bytes, CACHE_PERM)
|
atomic_write_file_with_retries(path, bytes, CACHE_PERM)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn canonicalize_path(&self, path: &Path) -> std::io::Result<PathBuf> {
|
||||||
|
crate::util::fs::canonicalize_path(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_dir_all(&self, path: &Path) -> std::io::Result<()> {
|
||||||
|
std::fs::create_dir_all(path)
|
||||||
|
}
|
||||||
|
|
||||||
fn remove_file(&self, path: &Path) -> std::io::Result<()> {
|
fn remove_file(&self, path: &Path) -> std::io::Result<()> {
|
||||||
std::fs::remove_file(path)
|
std::fs::remove_file(path)
|
||||||
}
|
}
|
||||||
|
@ -100,6 +110,73 @@ impl deno_cache_dir::DenoCacheEnv for RealDenoCacheEnv {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct DenoCacheEnvFsAdapter<'a>(
|
||||||
|
pub &'a dyn deno_runtime::deno_fs::FileSystem,
|
||||||
|
);
|
||||||
|
|
||||||
|
impl<'a> deno_cache_dir::DenoCacheEnv for DenoCacheEnvFsAdapter<'a> {
|
||||||
|
fn read_file_bytes(&self, path: &Path) -> std::io::Result<Vec<u8>> {
|
||||||
|
self
|
||||||
|
.0
|
||||||
|
.read_file_sync(path, None)
|
||||||
|
.map_err(|err| err.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn atomic_write_file(
|
||||||
|
&self,
|
||||||
|
path: &Path,
|
||||||
|
bytes: &[u8],
|
||||||
|
) -> std::io::Result<()> {
|
||||||
|
atomic_write_file_with_retries_and_fs(
|
||||||
|
&AtomicWriteFileFsAdapter {
|
||||||
|
fs: self.0,
|
||||||
|
write_mode: CACHE_PERM,
|
||||||
|
},
|
||||||
|
path,
|
||||||
|
bytes,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn canonicalize_path(&self, path: &Path) -> std::io::Result<PathBuf> {
|
||||||
|
self.0.realpath_sync(path).map_err(|e| e.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_dir_all(&self, path: &Path) -> std::io::Result<()> {
|
||||||
|
self
|
||||||
|
.0
|
||||||
|
.mkdir_sync(path, true, None)
|
||||||
|
.map_err(|e| e.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_file(&self, path: &Path) -> std::io::Result<()> {
|
||||||
|
self
|
||||||
|
.0
|
||||||
|
.remove_sync(path, false)
|
||||||
|
.map_err(|e| e.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn modified(&self, path: &Path) -> std::io::Result<Option<SystemTime>> {
|
||||||
|
self
|
||||||
|
.0
|
||||||
|
.stat_sync(path)
|
||||||
|
.map(|stat| {
|
||||||
|
stat
|
||||||
|
.mtime
|
||||||
|
.map(|ts| SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(ts))
|
||||||
|
})
|
||||||
|
.map_err(|e| e.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_file(&self, path: &Path) -> bool {
|
||||||
|
self.0.is_file_sync(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn time_now(&self) -> SystemTime {
|
||||||
|
SystemTime::now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub type GlobalHttpCache = deno_cache_dir::GlobalHttpCache<RealDenoCacheEnv>;
|
pub type GlobalHttpCache = deno_cache_dir::GlobalHttpCache<RealDenoCacheEnv>;
|
||||||
pub type LocalHttpCache = deno_cache_dir::LocalHttpCache<RealDenoCacheEnv>;
|
pub type LocalHttpCache = deno_cache_dir::LocalHttpCache<RealDenoCacheEnv>;
|
||||||
pub type LocalLspHttpCache =
|
pub type LocalLspHttpCache =
|
||||||
|
|
|
@ -1,295 +0,0 @@
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
||||||
|
|
||||||
use std::path::Path;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use deno_ast::ModuleSpecifier;
|
|
||||||
use deno_core::anyhow::Context;
|
|
||||||
use deno_core::error::AnyError;
|
|
||||||
use deno_core::url::Url;
|
|
||||||
use deno_npm::NpmPackageCacheFolderId;
|
|
||||||
use deno_semver::package::PackageNv;
|
|
||||||
use deno_semver::Version;
|
|
||||||
|
|
||||||
use crate::util::fs::canonicalize_path;
|
|
||||||
use crate::util::path::root_url_to_safe_local_dirname;
|
|
||||||
|
|
||||||
/// The global cache directory of npm packages.
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct NpmCacheDir {
|
|
||||||
root_dir: PathBuf,
|
|
||||||
// cached url representation of the root directory
|
|
||||||
root_dir_url: Url,
|
|
||||||
// A list of all registry that were discovered via `.npmrc` files
|
|
||||||
// turned into a safe directory names.
|
|
||||||
known_registries_dirnames: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NpmCacheDir {
|
|
||||||
pub fn new(root_dir: PathBuf, known_registries_urls: Vec<Url>) -> Self {
|
|
||||||
fn try_get_canonicalized_root_dir(
|
|
||||||
root_dir: &Path,
|
|
||||||
) -> Result<PathBuf, AnyError> {
|
|
||||||
if !root_dir.exists() {
|
|
||||||
std::fs::create_dir_all(root_dir)
|
|
||||||
.with_context(|| format!("Error creating {}", root_dir.display()))?;
|
|
||||||
}
|
|
||||||
Ok(canonicalize_path(root_dir)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
// this may fail on readonly file systems, so just ignore if so
|
|
||||||
let root_dir =
|
|
||||||
try_get_canonicalized_root_dir(&root_dir).unwrap_or(root_dir);
|
|
||||||
let root_dir_url = Url::from_directory_path(&root_dir).unwrap();
|
|
||||||
|
|
||||||
let known_registries_dirnames: Vec<_> = known_registries_urls
|
|
||||||
.into_iter()
|
|
||||||
.map(|url| {
|
|
||||||
root_url_to_safe_local_dirname(&url)
|
|
||||||
.to_string_lossy()
|
|
||||||
.replace('\\', "/")
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
root_dir,
|
|
||||||
root_dir_url,
|
|
||||||
known_registries_dirnames,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_dir(&self) -> &Path {
|
|
||||||
&self.root_dir
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn root_dir_url(&self) -> &Url {
|
|
||||||
&self.root_dir_url
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn package_folder_for_id(
|
|
||||||
&self,
|
|
||||||
folder_id: &NpmPackageCacheFolderId,
|
|
||||||
registry_url: &Url,
|
|
||||||
) -> PathBuf {
|
|
||||||
if folder_id.copy_index == 0 {
|
|
||||||
self.package_folder_for_nv(&folder_id.nv, registry_url)
|
|
||||||
} else {
|
|
||||||
self
|
|
||||||
.package_name_folder(&folder_id.nv.name, registry_url)
|
|
||||||
.join(format!("{}_{}", folder_id.nv.version, folder_id.copy_index))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn package_folder_for_nv(
|
|
||||||
&self,
|
|
||||||
package: &PackageNv,
|
|
||||||
registry_url: &Url,
|
|
||||||
) -> PathBuf {
|
|
||||||
self
|
|
||||||
.package_name_folder(&package.name, registry_url)
|
|
||||||
.join(package.version.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn package_name_folder(&self, name: &str, registry_url: &Url) -> PathBuf {
|
|
||||||
let mut dir = self.registry_folder(registry_url);
|
|
||||||
if name.to_lowercase() != name {
|
|
||||||
let encoded_name = mixed_case_package_name_encode(name);
|
|
||||||
// Using the encoded directory may have a collision with an actual package name
|
|
||||||
// so prefix it with an underscore since npm packages can't start with that
|
|
||||||
dir.join(format!("_{encoded_name}"))
|
|
||||||
} else {
|
|
||||||
// ensure backslashes are used on windows
|
|
||||||
for part in name.split('/') {
|
|
||||||
dir = dir.join(part);
|
|
||||||
}
|
|
||||||
dir
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn registry_folder(&self, registry_url: &Url) -> PathBuf {
|
|
||||||
self
|
|
||||||
.root_dir
|
|
||||||
.join(root_url_to_safe_local_dirname(registry_url))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn resolve_package_folder_id_from_specifier(
|
|
||||||
&self,
|
|
||||||
specifier: &ModuleSpecifier,
|
|
||||||
) -> Option<NpmPackageCacheFolderId> {
|
|
||||||
let mut maybe_relative_url = None;
|
|
||||||
|
|
||||||
// Iterate through known registries and try to get a match.
|
|
||||||
for registry_dirname in &self.known_registries_dirnames {
|
|
||||||
let registry_root_dir = self
|
|
||||||
.root_dir_url
|
|
||||||
.join(&format!("{}/", registry_dirname))
|
|
||||||
// this not succeeding indicates a fatal issue, so unwrap
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let Some(relative_url) = registry_root_dir.make_relative(specifier)
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
if relative_url.starts_with("../") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
maybe_relative_url = Some(relative_url);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut relative_url = maybe_relative_url?;
|
|
||||||
|
|
||||||
// base32 decode the url if it starts with an underscore
|
|
||||||
// * Ex. _{base32(package_name)}/
|
|
||||||
if let Some(end_url) = relative_url.strip_prefix('_') {
|
|
||||||
let mut parts = end_url
|
|
||||||
.split('/')
|
|
||||||
.map(ToOwned::to_owned)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
match mixed_case_package_name_decode(&parts[0]) {
|
|
||||||
Some(part) => {
|
|
||||||
parts[0] = part;
|
|
||||||
}
|
|
||||||
None => return None,
|
|
||||||
}
|
|
||||||
relative_url = parts.join("/");
|
|
||||||
}
|
|
||||||
|
|
||||||
// examples:
|
|
||||||
// * chalk/5.0.1/
|
|
||||||
// * @types/chalk/5.0.1/
|
|
||||||
// * some-package/5.0.1_1/ -- where the `_1` (/_\d+/) is a copy of the folder for peer deps
|
|
||||||
let is_scoped_package = relative_url.starts_with('@');
|
|
||||||
let mut parts = relative_url
|
|
||||||
.split('/')
|
|
||||||
.enumerate()
|
|
||||||
.take(if is_scoped_package { 3 } else { 2 })
|
|
||||||
.map(|(_, part)| part)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
if parts.len() < 2 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let version_part = parts.pop().unwrap();
|
|
||||||
let name = parts.join("/");
|
|
||||||
let (version, copy_index) =
|
|
||||||
if let Some((version, copy_count)) = version_part.split_once('_') {
|
|
||||||
(version, copy_count.parse::<u8>().ok()?)
|
|
||||||
} else {
|
|
||||||
(version_part, 0)
|
|
||||||
};
|
|
||||||
Some(NpmPackageCacheFolderId {
|
|
||||||
nv: PackageNv {
|
|
||||||
name,
|
|
||||||
version: Version::parse_from_npm(version).ok()?,
|
|
||||||
},
|
|
||||||
copy_index,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_cache_location(&self) -> PathBuf {
|
|
||||||
self.root_dir.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mixed_case_package_name_encode(name: &str) -> String {
|
|
||||||
// use base32 encoding because it's reversible and the character set
|
|
||||||
// only includes the characters within 0-9 and A-Z so it can be lower cased
|
|
||||||
base32::encode(
|
|
||||||
base32::Alphabet::Rfc4648Lower { padding: false },
|
|
||||||
name.as_bytes(),
|
|
||||||
)
|
|
||||||
.to_lowercase()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mixed_case_package_name_decode(name: &str) -> Option<String> {
|
|
||||||
base32::decode(base32::Alphabet::Rfc4648Lower { padding: false }, name)
|
|
||||||
.and_then(|b| String::from_utf8(b).ok())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use deno_core::url::Url;
|
|
||||||
use deno_semver::package::PackageNv;
|
|
||||||
use deno_semver::Version;
|
|
||||||
|
|
||||||
use super::NpmCacheDir;
|
|
||||||
use crate::npm::cache_dir::NpmPackageCacheFolderId;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn should_get_package_folder() {
|
|
||||||
let deno_dir = crate::cache::DenoDir::new(None).unwrap();
|
|
||||||
let root_dir = deno_dir.npm_folder_path();
|
|
||||||
let registry_url = Url::parse("https://registry.npmjs.org/").unwrap();
|
|
||||||
let cache = NpmCacheDir::new(root_dir.clone(), vec![registry_url.clone()]);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
cache.package_folder_for_id(
|
|
||||||
&NpmPackageCacheFolderId {
|
|
||||||
nv: PackageNv {
|
|
||||||
name: "json".to_string(),
|
|
||||||
version: Version::parse_from_npm("1.2.5").unwrap(),
|
|
||||||
},
|
|
||||||
copy_index: 0,
|
|
||||||
},
|
|
||||||
®istry_url,
|
|
||||||
),
|
|
||||||
root_dir
|
|
||||||
.join("registry.npmjs.org")
|
|
||||||
.join("json")
|
|
||||||
.join("1.2.5"),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
cache.package_folder_for_id(
|
|
||||||
&NpmPackageCacheFolderId {
|
|
||||||
nv: PackageNv {
|
|
||||||
name: "json".to_string(),
|
|
||||||
version: Version::parse_from_npm("1.2.5").unwrap(),
|
|
||||||
},
|
|
||||||
copy_index: 1,
|
|
||||||
},
|
|
||||||
®istry_url,
|
|
||||||
),
|
|
||||||
root_dir
|
|
||||||
.join("registry.npmjs.org")
|
|
||||||
.join("json")
|
|
||||||
.join("1.2.5_1"),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
cache.package_folder_for_id(
|
|
||||||
&NpmPackageCacheFolderId {
|
|
||||||
nv: PackageNv {
|
|
||||||
name: "JSON".to_string(),
|
|
||||||
version: Version::parse_from_npm("2.1.5").unwrap(),
|
|
||||||
},
|
|
||||||
copy_index: 0,
|
|
||||||
},
|
|
||||||
®istry_url,
|
|
||||||
),
|
|
||||||
root_dir
|
|
||||||
.join("registry.npmjs.org")
|
|
||||||
.join("_jjju6tq")
|
|
||||||
.join("2.1.5"),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
cache.package_folder_for_id(
|
|
||||||
&NpmPackageCacheFolderId {
|
|
||||||
nv: PackageNv {
|
|
||||||
name: "@types/JSON".to_string(),
|
|
||||||
version: Version::parse_from_npm("2.1.5").unwrap(),
|
|
||||||
},
|
|
||||||
copy_index: 0,
|
|
||||||
},
|
|
||||||
®istry_url,
|
|
||||||
),
|
|
||||||
root_dir
|
|
||||||
.join("registry.npmjs.org")
|
|
||||||
.join("_ib2hs4dfomxuuu2pjy")
|
|
||||||
.join("2.1.5"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
44
cli/npm/managed/cache/mod.rs
vendored
44
cli/npm/managed/cache/mod.rs
vendored
|
@ -8,6 +8,7 @@ use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use deno_ast::ModuleSpecifier;
|
use deno_ast::ModuleSpecifier;
|
||||||
|
use deno_cache_dir::npm::NpmCacheDir;
|
||||||
use deno_core::anyhow::bail;
|
use deno_core::anyhow::bail;
|
||||||
use deno_core::anyhow::Context;
|
use deno_core::anyhow::Context;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
|
@ -18,10 +19,10 @@ use deno_npm::npm_rc::ResolvedNpmRc;
|
||||||
use deno_npm::registry::NpmPackageInfo;
|
use deno_npm::registry::NpmPackageInfo;
|
||||||
use deno_npm::NpmPackageCacheFolderId;
|
use deno_npm::NpmPackageCacheFolderId;
|
||||||
use deno_semver::package::PackageNv;
|
use deno_semver::package::PackageNv;
|
||||||
|
use deno_semver::Version;
|
||||||
|
|
||||||
use crate::args::CacheSetting;
|
use crate::args::CacheSetting;
|
||||||
use crate::cache::CACHE_PERM;
|
use crate::cache::CACHE_PERM;
|
||||||
use crate::npm::NpmCacheDir;
|
|
||||||
use crate::util::fs::atomic_write_file_with_retries;
|
use crate::util::fs::atomic_write_file_with_retries;
|
||||||
use crate::util::fs::hard_link_dir_recursive;
|
use crate::util::fs::hard_link_dir_recursive;
|
||||||
|
|
||||||
|
@ -87,9 +88,12 @@ impl NpmCache {
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
let registry_url = self.npmrc.get_registry_url(&folder_id.nv.name);
|
let registry_url = self.npmrc.get_registry_url(&folder_id.nv.name);
|
||||||
assert_ne!(folder_id.copy_index, 0);
|
assert_ne!(folder_id.copy_index, 0);
|
||||||
let package_folder = self
|
let package_folder = self.cache_dir.package_folder_for_id(
|
||||||
.cache_dir
|
&folder_id.nv.name,
|
||||||
.package_folder_for_id(folder_id, registry_url);
|
&folder_id.nv.version.to_string(),
|
||||||
|
folder_id.copy_index,
|
||||||
|
registry_url,
|
||||||
|
);
|
||||||
|
|
||||||
if package_folder.exists()
|
if package_folder.exists()
|
||||||
// if this file exists, then the package didn't successfully initialize
|
// if this file exists, then the package didn't successfully initialize
|
||||||
|
@ -100,9 +104,12 @@ impl NpmCache {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let original_package_folder = self
|
let original_package_folder = self.cache_dir.package_folder_for_id(
|
||||||
.cache_dir
|
&folder_id.nv.name,
|
||||||
.package_folder_for_nv(&folder_id.nv, registry_url);
|
&folder_id.nv.version.to_string(),
|
||||||
|
0, // original copy index
|
||||||
|
registry_url,
|
||||||
|
);
|
||||||
|
|
||||||
// it seems Windows does an "AccessDenied" error when moving a
|
// it seems Windows does an "AccessDenied" error when moving a
|
||||||
// directory with hard links, so that's why this solution is done
|
// directory with hard links, so that's why this solution is done
|
||||||
|
@ -114,7 +121,12 @@ impl NpmCache {
|
||||||
|
|
||||||
pub fn package_folder_for_id(&self, id: &NpmPackageCacheFolderId) -> PathBuf {
|
pub fn package_folder_for_id(&self, id: &NpmPackageCacheFolderId) -> PathBuf {
|
||||||
let registry_url = self.npmrc.get_registry_url(&id.nv.name);
|
let registry_url = self.npmrc.get_registry_url(&id.nv.name);
|
||||||
self.cache_dir.package_folder_for_id(id, registry_url)
|
self.cache_dir.package_folder_for_id(
|
||||||
|
&id.nv.name,
|
||||||
|
&id.nv.version.to_string(),
|
||||||
|
id.copy_index,
|
||||||
|
registry_url,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn package_folder_for_nv(&self, package: &PackageNv) -> PathBuf {
|
pub fn package_folder_for_nv(&self, package: &PackageNv) -> PathBuf {
|
||||||
|
@ -127,7 +139,12 @@ impl NpmCache {
|
||||||
package: &PackageNv,
|
package: &PackageNv,
|
||||||
registry_url: &Url,
|
registry_url: &Url,
|
||||||
) -> PathBuf {
|
) -> PathBuf {
|
||||||
self.cache_dir.package_folder_for_nv(package, registry_url)
|
self.cache_dir.package_folder_for_id(
|
||||||
|
&package.name,
|
||||||
|
&package.version.to_string(),
|
||||||
|
0, // original copy_index
|
||||||
|
registry_url,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn package_name_folder(&self, name: &str) -> PathBuf {
|
pub fn package_name_folder(&self, name: &str) -> PathBuf {
|
||||||
|
@ -146,6 +163,15 @@ impl NpmCache {
|
||||||
self
|
self
|
||||||
.cache_dir
|
.cache_dir
|
||||||
.resolve_package_folder_id_from_specifier(specifier)
|
.resolve_package_folder_id_from_specifier(specifier)
|
||||||
|
.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,
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_package_info(
|
pub fn load_package_info(
|
||||||
|
|
|
@ -7,6 +7,7 @@ use std::sync::Arc;
|
||||||
use cache::RegistryInfoDownloader;
|
use cache::RegistryInfoDownloader;
|
||||||
use cache::TarballCache;
|
use cache::TarballCache;
|
||||||
use deno_ast::ModuleSpecifier;
|
use deno_ast::ModuleSpecifier;
|
||||||
|
use deno_cache_dir::npm::NpmCacheDir;
|
||||||
use deno_core::anyhow::Context;
|
use deno_core::anyhow::Context;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::serde_json;
|
use deno_core::serde_json;
|
||||||
|
@ -35,6 +36,7 @@ use crate::args::LifecycleScriptsConfig;
|
||||||
use crate::args::NpmInstallDepsProvider;
|
use crate::args::NpmInstallDepsProvider;
|
||||||
use crate::args::NpmProcessState;
|
use crate::args::NpmProcessState;
|
||||||
use crate::args::NpmProcessStateKind;
|
use crate::args::NpmProcessStateKind;
|
||||||
|
use crate::cache::DenoCacheEnvFsAdapter;
|
||||||
use crate::cache::FastInsecureHasher;
|
use crate::cache::FastInsecureHasher;
|
||||||
use crate::http_util::HttpClientProvider;
|
use crate::http_util::HttpClientProvider;
|
||||||
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
|
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
|
||||||
|
@ -50,7 +52,6 @@ use self::resolvers::NpmPackageFsResolver;
|
||||||
|
|
||||||
use super::CliNpmResolver;
|
use super::CliNpmResolver;
|
||||||
use super::InnerCliNpmResolverRef;
|
use super::InnerCliNpmResolverRef;
|
||||||
use super::NpmCacheDir;
|
|
||||||
|
|
||||||
mod cache;
|
mod cache;
|
||||||
mod registry;
|
mod registry;
|
||||||
|
@ -188,6 +189,7 @@ fn create_inner(
|
||||||
fn create_cache(options: &CliNpmResolverManagedCreateOptions) -> Arc<NpmCache> {
|
fn create_cache(options: &CliNpmResolverManagedCreateOptions) -> Arc<NpmCache> {
|
||||||
Arc::new(NpmCache::new(
|
Arc::new(NpmCache::new(
|
||||||
NpmCacheDir::new(
|
NpmCacheDir::new(
|
||||||
|
&DenoCacheEnvFsAdapter(options.fs.as_ref()),
|
||||||
options.npm_global_cache_dir.clone(),
|
options.npm_global_cache_dir.clone(),
|
||||||
options.npmrc.get_all_known_registries_urls(),
|
options.npmrc.get_all_known_registries_urls(),
|
||||||
),
|
),
|
||||||
|
|
|
@ -19,6 +19,8 @@ use crate::args::LifecycleScriptsConfig;
|
||||||
use crate::colors;
|
use crate::colors;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use deno_ast::ModuleSpecifier;
|
use deno_ast::ModuleSpecifier;
|
||||||
|
use deno_cache_dir::npm::mixed_case_package_name_decode;
|
||||||
|
use deno_cache_dir::npm::mixed_case_package_name_encode;
|
||||||
use deno_core::anyhow::Context;
|
use deno_core::anyhow::Context;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::futures::stream::FuturesUnordered;
|
use deno_core::futures::stream::FuturesUnordered;
|
||||||
|
@ -42,8 +44,6 @@ use serde::Serialize;
|
||||||
|
|
||||||
use crate::args::NpmInstallDepsProvider;
|
use crate::args::NpmInstallDepsProvider;
|
||||||
use crate::cache::CACHE_PERM;
|
use crate::cache::CACHE_PERM;
|
||||||
use crate::npm::cache_dir::mixed_case_package_name_decode;
|
|
||||||
use crate::npm::cache_dir::mixed_case_package_name_encode;
|
|
||||||
use crate::util::fs::atomic_write_file_with_retries;
|
use crate::util::fs::atomic_write_file_with_retries;
|
||||||
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
|
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
|
||||||
use crate::util::fs::clone_dir_recursive;
|
use crate::util::fs::clone_dir_recursive;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
mod byonm;
|
mod byonm;
|
||||||
mod cache_dir;
|
|
||||||
mod common;
|
mod common;
|
||||||
mod managed;
|
mod managed;
|
||||||
|
|
||||||
|
@ -24,7 +23,6 @@ use crate::file_fetcher::FileFetcher;
|
||||||
|
|
||||||
pub use self::byonm::ByonmCliNpmResolver;
|
pub use self::byonm::ByonmCliNpmResolver;
|
||||||
pub use self::byonm::CliNpmResolverByonmCreateOptions;
|
pub use self::byonm::CliNpmResolverByonmCreateOptions;
|
||||||
pub use self::cache_dir::NpmCacheDir;
|
|
||||||
pub use self::managed::CliNpmResolverManagedCreateOptions;
|
pub use self::managed::CliNpmResolverManagedCreateOptions;
|
||||||
pub use self::managed::CliNpmResolverManagedSnapshotOption;
|
pub use self::managed::CliNpmResolverManagedSnapshotOption;
|
||||||
pub use self::managed::ManagedCliNpmResolver;
|
pub use self::managed::ManagedCliNpmResolver;
|
||||||
|
|
|
@ -102,7 +102,7 @@ impl FileSystem for DenoCompileFileSystem {
|
||||||
&self,
|
&self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
mode: u32,
|
mode: Option<u32>,
|
||||||
) -> FsResult<()> {
|
) -> FsResult<()> {
|
||||||
self.error_if_in_vfs(path)?;
|
self.error_if_in_vfs(path)?;
|
||||||
RealFs.mkdir_sync(path, recursive, mode)
|
RealFs.mkdir_sync(path, recursive, mode)
|
||||||
|
@ -111,7 +111,7 @@ impl FileSystem for DenoCompileFileSystem {
|
||||||
&self,
|
&self,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
mode: u32,
|
mode: Option<u32>,
|
||||||
) -> FsResult<()> {
|
) -> FsResult<()> {
|
||||||
self.error_if_in_vfs(&path)?;
|
self.error_if_in_vfs(&path)?;
|
||||||
RealFs.mkdir_async(path, recursive, mode).await
|
RealFs.mkdir_async(path, recursive, mode).await
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#![allow(unused_imports)]
|
#![allow(unused_imports)]
|
||||||
|
|
||||||
use deno_ast::MediaType;
|
use deno_ast::MediaType;
|
||||||
|
use deno_cache_dir::npm::NpmCacheDir;
|
||||||
use deno_config::workspace::MappedResolution;
|
use deno_config::workspace::MappedResolution;
|
||||||
use deno_config::workspace::MappedResolutionError;
|
use deno_config::workspace::MappedResolutionError;
|
||||||
use deno_config::workspace::ResolverWorkspaceJsrPackage;
|
use deno_config::workspace::ResolverWorkspaceJsrPackage;
|
||||||
|
@ -55,6 +56,7 @@ use crate::args::StorageKeyResolver;
|
||||||
use crate::cache::Caches;
|
use crate::cache::Caches;
|
||||||
use crate::cache::DenoDirProvider;
|
use crate::cache::DenoDirProvider;
|
||||||
use crate::cache::NodeAnalysisCache;
|
use crate::cache::NodeAnalysisCache;
|
||||||
|
use crate::cache::RealDenoCacheEnv;
|
||||||
use crate::http_util::HttpClientProvider;
|
use crate::http_util::HttpClientProvider;
|
||||||
use crate::node::CliCjsCodeAnalyzer;
|
use crate::node::CliCjsCodeAnalyzer;
|
||||||
use crate::npm::create_cli_npm_resolver;
|
use crate::npm::create_cli_npm_resolver;
|
||||||
|
@ -62,7 +64,6 @@ use crate::npm::CliNpmResolverByonmCreateOptions;
|
||||||
use crate::npm::CliNpmResolverCreateOptions;
|
use crate::npm::CliNpmResolverCreateOptions;
|
||||||
use crate::npm::CliNpmResolverManagedCreateOptions;
|
use crate::npm::CliNpmResolverManagedCreateOptions;
|
||||||
use crate::npm::CliNpmResolverManagedSnapshotOption;
|
use crate::npm::CliNpmResolverManagedSnapshotOption;
|
||||||
use crate::npm::NpmCacheDir;
|
|
||||||
use crate::resolver::CjsResolutionStore;
|
use crate::resolver::CjsResolutionStore;
|
||||||
use crate::resolver::CliNodeResolver;
|
use crate::resolver::CliNodeResolver;
|
||||||
use crate::resolver::NpmModuleLoader;
|
use crate::resolver::NpmModuleLoader;
|
||||||
|
@ -464,6 +465,7 @@ pub async fn run(
|
||||||
let main_module = root_dir_url.join(&metadata.entrypoint_key).unwrap();
|
let main_module = root_dir_url.join(&metadata.entrypoint_key).unwrap();
|
||||||
let root_node_modules_path = root_path.join("node_modules");
|
let root_node_modules_path = root_path.join("node_modules");
|
||||||
let npm_cache_dir = NpmCacheDir::new(
|
let npm_cache_dir = NpmCacheDir::new(
|
||||||
|
&RealDenoCacheEnv,
|
||||||
root_node_modules_path.clone(),
|
root_node_modules_path.clone(),
|
||||||
vec![npm_registry_url.clone()],
|
vec![npm_registry_url.clone()],
|
||||||
);
|
);
|
||||||
|
|
157
cli/util/fs.rs
157
cli/util/fs.rs
|
@ -37,10 +37,98 @@ pub fn atomic_write_file_with_retries<T: AsRef<[u8]>>(
|
||||||
file_path: &Path,
|
file_path: &Path,
|
||||||
data: T,
|
data: T,
|
||||||
mode: u32,
|
mode: u32,
|
||||||
|
) -> std::io::Result<()> {
|
||||||
|
struct RealAtomicWriteFileFs {
|
||||||
|
mode: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AtomicWriteFileFs for RealAtomicWriteFileFs {
|
||||||
|
fn write_file(&self, path: &Path, bytes: &[u8]) -> std::io::Result<()> {
|
||||||
|
write_file(path, bytes, self.mode)
|
||||||
|
}
|
||||||
|
fn rename_file(&self, from: &Path, to: &Path) -> std::io::Result<()> {
|
||||||
|
std::fs::rename(from, to)
|
||||||
|
}
|
||||||
|
fn remove_file(&self, path: &Path) -> std::io::Result<()> {
|
||||||
|
std::fs::remove_file(path)
|
||||||
|
}
|
||||||
|
fn create_dir_all(&self, dir_path: &Path) -> std::io::Result<()> {
|
||||||
|
std::fs::create_dir_all(dir_path)
|
||||||
|
}
|
||||||
|
fn path_exists(&self, path: &Path) -> bool {
|
||||||
|
path.exists()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
atomic_write_file_with_retries_and_fs(
|
||||||
|
&RealAtomicWriteFileFs { mode },
|
||||||
|
file_path,
|
||||||
|
data.as_ref(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait AtomicWriteFileFs {
|
||||||
|
fn write_file(&self, path: &Path, bytes: &[u8]) -> std::io::Result<()>;
|
||||||
|
fn rename_file(&self, from: &Path, to: &Path) -> std::io::Result<()>;
|
||||||
|
fn remove_file(&self, path: &Path) -> std::io::Result<()>;
|
||||||
|
fn create_dir_all(&self, dir_path: &Path) -> std::io::Result<()>;
|
||||||
|
fn path_exists(&self, path: &Path) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AtomicWriteFileFsAdapter<'a> {
|
||||||
|
pub fs: &'a dyn FileSystem,
|
||||||
|
pub write_mode: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AtomicWriteFileFs for AtomicWriteFileFsAdapter<'a> {
|
||||||
|
fn write_file(&self, path: &Path, bytes: &[u8]) -> std::io::Result<()> {
|
||||||
|
self
|
||||||
|
.fs
|
||||||
|
.write_file_sync(
|
||||||
|
path,
|
||||||
|
deno_runtime::deno_fs::OpenOptions::write(
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
Some(self.write_mode),
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
bytes,
|
||||||
|
)
|
||||||
|
.map_err(|e| e.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rename_file(&self, from: &Path, to: &Path) -> std::io::Result<()> {
|
||||||
|
self.fs.rename_sync(from, to).map_err(|e| e.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_file(&self, path: &Path) -> std::io::Result<()> {
|
||||||
|
self
|
||||||
|
.fs
|
||||||
|
.remove_sync(path, false)
|
||||||
|
.map_err(|e| e.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_dir_all(&self, dir_path: &Path) -> std::io::Result<()> {
|
||||||
|
self
|
||||||
|
.fs
|
||||||
|
.mkdir_sync(dir_path, /* recursive */ true, None)
|
||||||
|
.map_err(|e| e.into_io_error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn path_exists(&self, path: &Path) -> bool {
|
||||||
|
self.fs.exists_sync(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn atomic_write_file_with_retries_and_fs<T: AsRef<[u8]>>(
|
||||||
|
fs: &impl AtomicWriteFileFs,
|
||||||
|
file_path: &Path,
|
||||||
|
data: T,
|
||||||
) -> std::io::Result<()> {
|
) -> std::io::Result<()> {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
loop {
|
loop {
|
||||||
match atomic_write_file(file_path, data.as_ref(), mode) {
|
match atomic_write_file(fs, file_path, data.as_ref()) {
|
||||||
Ok(()) => return Ok(()),
|
Ok(()) => return Ok(()),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if count >= 5 {
|
if count >= 5 {
|
||||||
|
@ -61,63 +149,54 @@ pub fn atomic_write_file_with_retries<T: AsRef<[u8]>>(
|
||||||
///
|
///
|
||||||
/// This also handles creating the directory if a NotFound error
|
/// This also handles creating the directory if a NotFound error
|
||||||
/// occurs.
|
/// occurs.
|
||||||
fn atomic_write_file<T: AsRef<[u8]>>(
|
fn atomic_write_file(
|
||||||
|
fs: &impl AtomicWriteFileFs,
|
||||||
file_path: &Path,
|
file_path: &Path,
|
||||||
data: T,
|
data: &[u8],
|
||||||
mode: u32,
|
|
||||||
) -> std::io::Result<()> {
|
) -> std::io::Result<()> {
|
||||||
fn atomic_write_file_raw(
|
fn atomic_write_file_raw(
|
||||||
|
fs: &impl AtomicWriteFileFs,
|
||||||
temp_file_path: &Path,
|
temp_file_path: &Path,
|
||||||
file_path: &Path,
|
file_path: &Path,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
mode: u32,
|
|
||||||
) -> std::io::Result<()> {
|
) -> std::io::Result<()> {
|
||||||
write_file(temp_file_path, data, mode)?;
|
fs.write_file(temp_file_path, data)?;
|
||||||
std::fs::rename(temp_file_path, file_path).map_err(|err| {
|
fs.rename_file(temp_file_path, file_path).map_err(|err| {
|
||||||
// clean up the created temp file on error
|
// clean up the created temp file on error
|
||||||
let _ = std::fs::remove_file(temp_file_path);
|
let _ = fs.remove_file(temp_file_path);
|
||||||
err
|
err
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inner(file_path: &Path, data: &[u8], mode: u32) -> std::io::Result<()> {
|
let temp_file_path = get_atomic_file_path(file_path);
|
||||||
let temp_file_path = get_atomic_file_path(file_path);
|
|
||||||
|
|
||||||
if let Err(write_err) =
|
if let Err(write_err) =
|
||||||
atomic_write_file_raw(&temp_file_path, file_path, data, mode)
|
atomic_write_file_raw(fs, &temp_file_path, file_path, data)
|
||||||
{
|
{
|
||||||
if write_err.kind() == ErrorKind::NotFound {
|
if write_err.kind() == ErrorKind::NotFound {
|
||||||
let parent_dir_path = file_path.parent().unwrap();
|
let parent_dir_path = file_path.parent().unwrap();
|
||||||
match std::fs::create_dir_all(parent_dir_path) {
|
match fs.create_dir_all(parent_dir_path) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
return atomic_write_file_raw(
|
return atomic_write_file_raw(fs, &temp_file_path, file_path, data)
|
||||||
&temp_file_path,
|
|
||||||
file_path,
|
|
||||||
data,
|
|
||||||
mode,
|
|
||||||
)
|
|
||||||
.map_err(|err| add_file_context_to_err(file_path, err));
|
.map_err(|err| add_file_context_to_err(file_path, err));
|
||||||
}
|
}
|
||||||
Err(create_err) => {
|
Err(create_err) => {
|
||||||
if !parent_dir_path.exists() {
|
if !fs.path_exists(parent_dir_path) {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
create_err.kind(),
|
create_err.kind(),
|
||||||
format!(
|
format!(
|
||||||
"{:#} (for '{}')\nCheck the permission of the directory.",
|
"{:#} (for '{}')\nCheck the permission of the directory.",
|
||||||
create_err,
|
create_err,
|
||||||
parent_dir_path.display()
|
parent_dir_path.display()
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Err(add_file_context_to_err(file_path, write_err));
|
|
||||||
}
|
}
|
||||||
Ok(())
|
return Err(add_file_context_to_err(file_path, write_err));
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
inner(file_path, data.as_ref(), mode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a std::fs::File handling if the parent does not exist.
|
/// Creates a std::fs::File handling if the parent does not exist.
|
||||||
|
|
|
@ -165,48 +165,6 @@ pub fn relative_path(from: &Path, to: &Path) -> Option<PathBuf> {
|
||||||
pathdiff::diff_paths(to, from)
|
pathdiff::diff_paths(to, from)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets if the provided character is not supported on all
|
|
||||||
/// kinds of file systems.
|
|
||||||
pub fn is_banned_path_char(c: char) -> bool {
|
|
||||||
matches!(c, '<' | '>' | ':' | '"' | '|' | '?' | '*')
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets a safe local directory name for the provided url.
|
|
||||||
///
|
|
||||||
/// For example:
|
|
||||||
/// https://deno.land:8080/path -> deno.land_8080/path
|
|
||||||
pub fn root_url_to_safe_local_dirname(root: &ModuleSpecifier) -> PathBuf {
|
|
||||||
fn sanitize_segment(text: &str) -> String {
|
|
||||||
text
|
|
||||||
.chars()
|
|
||||||
.map(|c| if is_banned_segment_char(c) { '_' } else { c })
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_banned_segment_char(c: char) -> bool {
|
|
||||||
matches!(c, '/' | '\\') || is_banned_path_char(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut result = String::new();
|
|
||||||
if let Some(domain) = root.domain() {
|
|
||||||
result.push_str(&sanitize_segment(domain));
|
|
||||||
}
|
|
||||||
if let Some(port) = root.port() {
|
|
||||||
if !result.is_empty() {
|
|
||||||
result.push('_');
|
|
||||||
}
|
|
||||||
result.push_str(&port.to_string());
|
|
||||||
}
|
|
||||||
let mut result = PathBuf::from(result);
|
|
||||||
if let Some(segments) = root.path_segments() {
|
|
||||||
for segment in segments.filter(|s| !s.is_empty()) {
|
|
||||||
result = result.join(sanitize_segment(segment));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Slightly different behaviour than the default matching
|
/// Slightly different behaviour than the default matching
|
||||||
/// where an exact path needs to be matched to be opted-in
|
/// where an exact path needs to be matched to be opted-in
|
||||||
/// rather than just a partial directory match.
|
/// rather than just a partial directory match.
|
||||||
|
|
|
@ -44,7 +44,7 @@ impl InMemoryFs {
|
||||||
pub fn setup_text_files(&self, files: Vec<(String, String)>) {
|
pub fn setup_text_files(&self, files: Vec<(String, String)>) {
|
||||||
for (path, text) in files {
|
for (path, text) in files {
|
||||||
let path = PathBuf::from(path);
|
let path = PathBuf::from(path);
|
||||||
self.mkdir_sync(path.parent().unwrap(), true, 0).unwrap();
|
self.mkdir_sync(path.parent().unwrap(), true, None).unwrap();
|
||||||
self
|
self
|
||||||
.write_file_sync(
|
.write_file_sync(
|
||||||
&path,
|
&path,
|
||||||
|
@ -101,7 +101,7 @@ impl FileSystem for InMemoryFs {
|
||||||
&self,
|
&self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
_mode: u32,
|
_mode: Option<u32>,
|
||||||
) -> FsResult<()> {
|
) -> FsResult<()> {
|
||||||
let path = normalize_path(path);
|
let path = normalize_path(path);
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ impl FileSystem for InMemoryFs {
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
if recursive {
|
if recursive {
|
||||||
self.mkdir_sync(parent, true, 0)?;
|
self.mkdir_sync(parent, true, None)?;
|
||||||
} else {
|
} else {
|
||||||
return Err(FsError::Io(Error::new(
|
return Err(FsError::Io(Error::new(
|
||||||
ErrorKind::NotFound,
|
ErrorKind::NotFound,
|
||||||
|
@ -149,7 +149,7 @@ impl FileSystem for InMemoryFs {
|
||||||
&self,
|
&self,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
mode: u32,
|
mode: Option<u32>,
|
||||||
) -> FsResult<()> {
|
) -> FsResult<()> {
|
||||||
self.mkdir_sync(&path, recursive, mode)
|
self.mkdir_sync(&path, recursive, mode)
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,13 +121,17 @@ pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync {
|
||||||
access_check: Option<AccessCheckCb<'a>>,
|
access_check: Option<AccessCheckCb<'a>>,
|
||||||
) -> FsResult<Rc<dyn File>>;
|
) -> FsResult<Rc<dyn File>>;
|
||||||
|
|
||||||
fn mkdir_sync(&self, path: &Path, recursive: bool, mode: u32)
|
fn mkdir_sync(
|
||||||
-> FsResult<()>;
|
&self,
|
||||||
|
path: &Path,
|
||||||
|
recursive: bool,
|
||||||
|
mode: Option<u32>,
|
||||||
|
) -> FsResult<()>;
|
||||||
async fn mkdir_async(
|
async fn mkdir_async(
|
||||||
&self,
|
&self,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
mode: u32,
|
mode: Option<u32>,
|
||||||
) -> FsResult<()>;
|
) -> FsResult<()>;
|
||||||
|
|
||||||
fn chmod_sync(&self, path: &Path, mode: u32) -> FsResult<()>;
|
fn chmod_sync(&self, path: &Path, mode: u32) -> FsResult<()>;
|
||||||
|
|
|
@ -197,7 +197,7 @@ where
|
||||||
.check_write(&path, "Deno.mkdirSync()")?;
|
.check_write(&path, "Deno.mkdirSync()")?;
|
||||||
|
|
||||||
let fs = state.borrow::<FileSystemRc>();
|
let fs = state.borrow::<FileSystemRc>();
|
||||||
fs.mkdir_sync(&path, recursive, mode)
|
fs.mkdir_sync(&path, recursive, Some(mode))
|
||||||
.context_path("mkdir", &path)?;
|
.context_path("mkdir", &path)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -221,7 +221,7 @@ where
|
||||||
(state.borrow::<FileSystemRc>().clone(), path)
|
(state.borrow::<FileSystemRc>().clone(), path)
|
||||||
};
|
};
|
||||||
|
|
||||||
fs.mkdir_async(path.clone(), recursive, mode)
|
fs.mkdir_async(path.clone(), recursive, Some(mode))
|
||||||
.await
|
.await
|
||||||
.context_path("mkdir", &path)?;
|
.context_path("mkdir", &path)?;
|
||||||
|
|
||||||
|
@ -886,7 +886,7 @@ where
|
||||||
const MAX_TRIES: u32 = 10;
|
const MAX_TRIES: u32 = 10;
|
||||||
for _ in 0..MAX_TRIES {
|
for _ in 0..MAX_TRIES {
|
||||||
let path = tmp_name(&mut rng, &dir, prefix.as_deref(), suffix.as_deref())?;
|
let path = tmp_name(&mut rng, &dir, prefix.as_deref(), suffix.as_deref())?;
|
||||||
match fs.mkdir_sync(&path, false, 0o700) {
|
match fs.mkdir_sync(&path, false, Some(0o700)) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// PERMISSIONS: ensure the absolute path is not leaked
|
// PERMISSIONS: ensure the absolute path is not leaked
|
||||||
let path = strip_dir_prefix(&dir, dir_arg.as_deref(), path)?;
|
let path = strip_dir_prefix(&dir, dir_arg.as_deref(), path)?;
|
||||||
|
@ -928,7 +928,11 @@ where
|
||||||
const MAX_TRIES: u32 = 10;
|
const MAX_TRIES: u32 = 10;
|
||||||
for _ in 0..MAX_TRIES {
|
for _ in 0..MAX_TRIES {
|
||||||
let path = tmp_name(&mut rng, &dir, prefix.as_deref(), suffix.as_deref())?;
|
let path = tmp_name(&mut rng, &dir, prefix.as_deref(), suffix.as_deref())?;
|
||||||
match fs.clone().mkdir_async(path.clone(), false, 0o700).await {
|
match fs
|
||||||
|
.clone()
|
||||||
|
.mkdir_async(path.clone(), false, Some(0o700))
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// PERMISSIONS: ensure the absolute path is not leaked
|
// PERMISSIONS: ensure the absolute path is not leaked
|
||||||
let path = strip_dir_prefix(&dir, dir_arg.as_deref(), path)?;
|
let path = strip_dir_prefix(&dir, dir_arg.as_deref(), path)?;
|
||||||
|
|
|
@ -101,7 +101,7 @@ impl FileSystem for RealFs {
|
||||||
&self,
|
&self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
mode: u32,
|
mode: Option<u32>,
|
||||||
) -> FsResult<()> {
|
) -> FsResult<()> {
|
||||||
mkdir(path, recursive, mode)
|
mkdir(path, recursive, mode)
|
||||||
}
|
}
|
||||||
|
@ -109,7 +109,7 @@ impl FileSystem for RealFs {
|
||||||
&self,
|
&self,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
mode: u32,
|
mode: Option<u32>,
|
||||||
) -> FsResult<()> {
|
) -> FsResult<()> {
|
||||||
spawn_blocking(move || mkdir(&path, recursive, mode)).await?
|
spawn_blocking(move || mkdir(&path, recursive, mode)).await?
|
||||||
}
|
}
|
||||||
|
@ -407,11 +407,11 @@ impl FileSystem for RealFs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mkdir(path: &Path, recursive: bool, mode: u32) -> FsResult<()> {
|
fn mkdir(path: &Path, recursive: bool, mode: Option<u32>) -> FsResult<()> {
|
||||||
let mut builder = fs::DirBuilder::new();
|
let mut builder = fs::DirBuilder::new();
|
||||||
builder.recursive(recursive);
|
builder.recursive(recursive);
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
{
|
if let Some(mode) = mode {
|
||||||
use std::os::unix::fs::DirBuilderExt;
|
use std::os::unix::fs::DirBuilderExt;
|
||||||
builder.mode(mode);
|
builder.mode(mode);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue