2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::url::Url;
|
2023-04-06 21:41:19 -04:00
|
|
|
use deno_npm::resolution::PackageNotFoundFromReferrerError;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_npm::NpmPackageCacheFolderId;
|
|
|
|
use deno_npm::NpmPackageId;
|
|
|
|
use deno_npm::NpmResolutionPackage;
|
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-11-30 18:07:32 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
|
|
|
use crate::npm::resolution::NpmResolution;
|
|
|
|
use crate::npm::resolvers::common::cache_packages;
|
|
|
|
use crate::npm::NpmCache;
|
|
|
|
|
2022-11-15 21:48:29 -05:00
|
|
|
use super::common::types_package_name;
|
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>,
|
|
|
|
resolution: Arc<NpmResolution>,
|
2022-09-13 11:59:01 -04:00
|
|
|
registry_url: Url,
|
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-05-10 20:06:59 -04:00
|
|
|
fs: Arc<dyn FileSystem>,
|
2023-04-14 16:22:33 -04:00
|
|
|
cache: Arc<NpmCache>,
|
2023-02-22 14:15:25 -05:00
|
|
|
registry_url: Url,
|
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
|
|
|
cache: cache.clone(),
|
2022-09-13 11:59:01 -04:00
|
|
|
resolution,
|
2023-06-09 14:41:18 -04:00
|
|
|
registry_url: registry_url.clone(),
|
2023-05-17 17:38:50 -04:00
|
|
|
system_info,
|
2023-06-09 14:41:18 -04:00
|
|
|
registry_read_permission_checker: RegistryReadPermissionChecker::new(
|
|
|
|
fs,
|
|
|
|
cache.registry_folder(®istry_url),
|
|
|
|
),
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 20:09:31 -05:00
|
|
|
fn resolve_types_package(
|
|
|
|
&self,
|
|
|
|
package_name: &str,
|
|
|
|
referrer_pkg_id: &NpmPackageCacheFolderId,
|
2023-04-06 21:41:19 -04:00
|
|
|
) -> Result<NpmResolutionPackage, Box<PackageNotFoundFromReferrerError>> {
|
2022-12-05 20:09:31 -05:00
|
|
|
let types_name = types_package_name(package_name);
|
|
|
|
self
|
|
|
|
.resolution
|
|
|
|
.resolve_package_from_package(&types_name, referrer_pkg_id)
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
#[async_trait]
|
|
|
|
impl NpmPackageFsResolver for GlobalNpmPackageResolver {
|
2023-02-23 10:58:10 -05:00
|
|
|
fn root_dir_url(&self) -> &Url {
|
|
|
|
self.cache.root_dir_url()
|
|
|
|
}
|
|
|
|
|
2023-03-12 23:32:59 -04:00
|
|
|
fn node_modules_path(&self) -> Option<PathBuf> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
fn package_folder(&self, id: &NpmPackageId) -> Result<PathBuf, AnyError> {
|
|
|
|
let folder_id = self
|
|
|
|
.resolution
|
|
|
|
.resolve_package_cache_folder_id_from_id(id)
|
|
|
|
.unwrap();
|
|
|
|
Ok(
|
|
|
|
self
|
|
|
|
.cache
|
|
|
|
.package_folder_for_id(&folder_id, &self.registry_url),
|
|
|
|
)
|
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-11-30 18:07:32 -05:00
|
|
|
mode: NodeResolutionMode,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2022-09-13 11:59:01 -04:00
|
|
|
let referrer_pkg_id = self
|
|
|
|
.cache
|
2022-11-08 14:17:24 -05:00
|
|
|
.resolve_package_folder_id_from_specifier(referrer, &self.registry_url)?;
|
2022-12-05 20:09:31 -05:00
|
|
|
let pkg = if mode.is_types() && !name.starts_with("@types/") {
|
|
|
|
// attempt to resolve the types package first, then fallback to the regular package
|
|
|
|
match self.resolve_types_package(name, &referrer_pkg_id) {
|
|
|
|
Ok(pkg) => pkg,
|
|
|
|
Err(_) => self
|
|
|
|
.resolution
|
|
|
|
.resolve_package_from_package(name, &referrer_pkg_id)?,
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-05 20:09:31 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
fn resolve_package_folder_from_specifier(
|
2022-09-13 11:59:01 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2022-11-08 14:17:24 -05:00
|
|
|
let pkg_folder_id = self.cache.resolve_package_folder_id_from_specifier(
|
|
|
|
specifier,
|
|
|
|
&self.registry_url,
|
|
|
|
)?;
|
|
|
|
Ok(
|
|
|
|
self
|
|
|
|
.cache
|
|
|
|
.package_folder_for_id(&pkg_folder_id, &self.registry_url),
|
|
|
|
)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05: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);
|
|
|
|
|
|
|
|
cache_packages(
|
|
|
|
package_partitions.packages,
|
|
|
|
&self.cache,
|
|
|
|
&self.registry_url,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// create the copy package folders
|
|
|
|
for copy in package_partitions.copy_packages {
|
|
|
|
self.cache.ensure_copy_package(
|
|
|
|
©.get_package_cache_folder_id(),
|
|
|
|
&self.registry_url,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
|
2023-01-10 08:35:44 -05:00
|
|
|
fn ensure_read_permission(
|
|
|
|
&self,
|
2023-04-24 21:07:48 -04:00
|
|
|
permissions: &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
|
|
|
}
|
|
|
|
}
|