2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
//! Code for global npm cache resolution.
|
|
|
|
|
2022-09-13 11:59:01 -04:00
|
|
|
use std::path::Path;
|
2022-09-22 11:17:02 -04:00
|
|
|
use std::path::PathBuf;
|
2023-04-14 16:22:33 -04:00
|
|
|
use std::sync::Arc;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
use async_trait::async_trait;
|
2022-09-13 11:59:01 -04:00
|
|
|
use deno_ast::ModuleSpecifier;
|
2023-07-17 14:00:44 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2022-09-13 11:59:01 -04:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::url::Url;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::NpmPackageCacheFolderId;
|
|
|
|
use deno_npm::NpmPackageId;
|
2023-05-17 17:38:50 -04:00
|
|
|
use deno_npm::NpmSystemInfo;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_runtime::deno_fs::FileSystem;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_runtime::deno_node::NodePermissions;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
use super::super::cache::NpmCache;
|
2024-06-02 21:39:13 -04:00
|
|
|
use super::super::cache::TarballCache;
|
2023-09-30 12:06:38 -04:00
|
|
|
use super::super::resolution::NpmResolution;
|
|
|
|
use super::common::cache_packages;
|
2023-02-22 14:15:25 -05:00
|
|
|
use super::common::NpmPackageFsResolver;
|
2023-06-09 14:41:18 -04:00
|
|
|
use super::common::RegistryReadPermissionChecker;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
/// Resolves packages from the global npm cache.
|
2023-04-14 16:22:33 -04:00
|
|
|
#[derive(Debug)]
|
2022-09-13 11:59:01 -04:00
|
|
|
pub struct GlobalNpmPackageResolver {
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: Arc<NpmCache>,
|
2024-06-02 21:39:13 -04:00
|
|
|
tarball_cache: Arc<TarballCache>,
|
2023-04-14 16:22:33 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info: NpmSystemInfo,
|
2023-06-09 14:41:18 -04:00
|
|
|
registry_read_permission_checker: RegistryReadPermissionChecker,
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GlobalNpmPackageResolver {
|
2022-09-28 13:04:16 -04:00
|
|
|
pub fn new(
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: Arc<NpmCache>,
|
2024-06-02 21:39:13 -04:00
|
|
|
fs: Arc<dyn FileSystem>,
|
|
|
|
tarball_cache: Arc<TarballCache>,
|
2023-04-14 16:22:33 -04:00
|
|
|
resolution: Arc<NpmResolution>,
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info: NpmSystemInfo,
|
2022-09-28 13:04:16 -04:00
|
|
|
) -> Self {
|
2022-09-13 11:59:01 -04:00
|
|
|
Self {
|
2023-06-09 14:41:18 -04:00
|
|
|
registry_read_permission_checker: RegistryReadPermissionChecker::new(
|
|
|
|
fs,
|
2024-05-23 17:26:23 -04:00
|
|
|
cache.root_folder(),
|
2023-06-09 14:41:18 -04:00
|
|
|
),
|
2024-06-02 21:39:13 -04:00
|
|
|
cache,
|
|
|
|
tarball_cache,
|
|
|
|
resolution,
|
|
|
|
system_info,
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 18:37:41 -04:00
|
|
|
#[async_trait(?Send)]
|
2023-02-22 14:15:25 -05:00
|
|
|
impl NpmPackageFsResolver for GlobalNpmPackageResolver {
|
2023-02-23 10:58:10 -05:00
|
|
|
fn root_dir_url(&self) -> &Url {
|
|
|
|
self.cache.root_dir_url()
|
|
|
|
}
|
|
|
|
|
2023-11-29 09:32:23 -05:00
|
|
|
fn node_modules_path(&self) -> Option<&PathBuf> {
|
2023-03-12 23:32:59 -04:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
fn package_folder(&self, id: &NpmPackageId) -> Result<PathBuf, AnyError> {
|
|
|
|
let folder_id = self
|
|
|
|
.resolution
|
2023-07-01 21:07:57 -04:00
|
|
|
.resolve_pkg_cache_folder_id_from_pkg_id(id)
|
2023-04-14 16:22:33 -04:00
|
|
|
.unwrap();
|
2024-05-23 17:26:23 -04:00
|
|
|
Ok(self.cache.package_folder_for_id(&folder_id))
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
fn resolve_package_folder_from_package(
|
2022-09-13 11:59:01 -04:00
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2023-07-17 14:00:44 -04:00
|
|
|
let Some(referrer_pkg_id) = self
|
2022-09-13 11:59:01 -04:00
|
|
|
.cache
|
2024-05-23 17:26:23 -04:00
|
|
|
.resolve_package_folder_id_from_specifier(referrer)
|
2023-08-27 00:04:12 -04:00
|
|
|
else {
|
|
|
|
bail!("could not find npm package for '{}'", referrer);
|
|
|
|
};
|
2024-06-08 20:05:28 -04:00
|
|
|
let pkg = self
|
|
|
|
.resolution
|
|
|
|
.resolve_package_from_package(name, &referrer_pkg_id)?;
|
2023-05-24 16:23:10 -04:00
|
|
|
self.package_folder(&pkg.id)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-07-01 21:07:57 -04:00
|
|
|
fn resolve_package_cache_folder_id_from_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-07-17 14:00:44 -04:00
|
|
|
) -> Result<Option<NpmPackageCacheFolderId>, AnyError> {
|
|
|
|
Ok(
|
2024-05-23 17:26:23 -04:00
|
|
|
self
|
|
|
|
.cache
|
|
|
|
.resolve_package_folder_id_from_specifier(specifier),
|
2023-07-17 14:00:44 -04:00
|
|
|
)
|
2023-07-01 21:07:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-03 17:17:08 -04:00
|
|
|
async fn cache_packages(&self) -> Result<(), AnyError> {
|
2023-05-17 17:38:50 -04:00
|
|
|
let package_partitions = self
|
|
|
|
.resolution
|
|
|
|
.all_system_packages_partitioned(&self.system_info);
|
|
|
|
|
2024-06-03 17:17:08 -04:00
|
|
|
cache_packages(package_partitions.packages, &self.tarball_cache).await?;
|
2023-05-17 17:38:50 -04:00
|
|
|
|
|
|
|
// create the copy package folders
|
|
|
|
for copy in package_partitions.copy_packages {
|
2024-05-23 17:26:23 -04:00
|
|
|
self
|
|
|
|
.cache
|
|
|
|
.ensure_copy_package(©.get_package_cache_folder_id())?;
|
2023-05-17 17:38:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-01-10 08:35:44 -05:00
|
|
|
fn ensure_read_permission(
|
|
|
|
&self,
|
2024-06-06 23:37:53 -04:00
|
|
|
permissions: &mut dyn NodePermissions,
|
2023-01-10 08:35:44 -05:00
|
|
|
path: &Path,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-06-09 14:41:18 -04:00
|
|
|
self
|
|
|
|
.registry_read_permission_checker
|
|
|
|
.ensure_registry_read_permission(permissions, path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
}
|