2022-09-13 11:59:01 -04:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
//! Code for global npm cache resolution.
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
use std::collections::HashSet;
|
2022-09-13 11:59:01 -04:00
|
|
|
use std::path::Path;
|
2022-09-22 11:17:02 -04:00
|
|
|
use std::path::PathBuf;
|
2022-09-13 11:59:01 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::future::BoxFuture;
|
|
|
|
use deno_core::futures::FutureExt;
|
|
|
|
use deno_core::url::Url;
|
2022-11-30 18:07:32 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2022-11-25 17:00:28 -05:00
|
|
|
use crate::args::Lockfile;
|
2022-12-05 20:09:31 -05:00
|
|
|
use crate::npm::cache::NpmPackageCacheFolderId;
|
2022-09-13 11:59:01 -04:00
|
|
|
use crate::npm::resolution::NpmResolution;
|
2022-09-28 13:04:16 -04:00
|
|
|
use crate::npm::resolution::NpmResolutionSnapshot;
|
2022-09-13 11:59:01 -04:00
|
|
|
use crate::npm::resolvers::common::cache_packages;
|
|
|
|
use crate::npm::NpmCache;
|
|
|
|
use crate::npm::NpmPackageId;
|
|
|
|
use crate::npm::NpmPackageReq;
|
2022-12-05 20:09:31 -05:00
|
|
|
use crate::npm::NpmResolutionPackage;
|
2022-11-08 14:17:24 -05:00
|
|
|
use crate::npm::RealNpmRegistryApi;
|
2022-09-13 11:59:01 -04:00
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
use super::common::ensure_registry_read_permission;
|
2022-11-15 21:48:29 -05:00
|
|
|
use super::common::types_package_name;
|
2022-09-13 11:59:01 -04:00
|
|
|
use super::common::InnerNpmPackageResolver;
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
/// Resolves packages from the global npm cache.
|
2022-09-13 11:59:01 -04:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct GlobalNpmPackageResolver {
|
|
|
|
cache: NpmCache,
|
|
|
|
resolution: Arc<NpmResolution>,
|
|
|
|
registry_url: Url,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GlobalNpmPackageResolver {
|
2022-09-28 13:04:16 -04:00
|
|
|
pub fn new(
|
|
|
|
cache: NpmCache,
|
2022-11-08 14:17:24 -05:00
|
|
|
api: RealNpmRegistryApi,
|
2022-09-28 13:04:16 -04:00
|
|
|
initial_snapshot: Option<NpmResolutionSnapshot>,
|
|
|
|
) -> Self {
|
2022-09-13 11:59:01 -04:00
|
|
|
let registry_url = api.base_url().to_owned();
|
2022-09-28 13:04:16 -04:00
|
|
|
let resolution = Arc::new(NpmResolution::new(api, initial_snapshot));
|
2022-09-13 11:59:01 -04:00
|
|
|
|
|
|
|
Self {
|
|
|
|
cache,
|
|
|
|
resolution,
|
|
|
|
registry_url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 11:17:02 -04:00
|
|
|
fn package_folder(&self, id: &NpmPackageId) -> PathBuf {
|
2022-11-08 14:17:24 -05:00
|
|
|
let folder_id = self
|
|
|
|
.resolution
|
|
|
|
.resolve_package_cache_folder_id_from_id(id)
|
|
|
|
.unwrap();
|
|
|
|
self
|
|
|
|
.cache
|
|
|
|
.package_folder_for_id(&folder_id, &self.registry_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,
|
|
|
|
) -> Result<NpmResolutionPackage, AnyError> {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl InnerNpmPackageResolver for GlobalNpmPackageResolver {
|
2022-09-22 11:17:02 -04:00
|
|
|
fn resolve_package_folder_from_deno_module(
|
2022-09-13 11:59:01 -04:00
|
|
|
&self,
|
|
|
|
pkg_req: &NpmPackageReq,
|
2022-09-22 11:17:02 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2022-09-13 11:59:01 -04:00
|
|
|
let pkg = self.resolution.resolve_package_from_deno_module(pkg_req)?;
|
2022-09-22 11:17:02 -04:00
|
|
|
Ok(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_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)?
|
|
|
|
};
|
|
|
|
Ok(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
|
|
|
}
|
|
|
|
|
2022-10-28 16:19:55 -04:00
|
|
|
fn package_size(&self, package_id: &NpmPackageId) -> Result<u64, AnyError> {
|
|
|
|
let package_folder = self.package_folder(package_id);
|
2022-11-28 17:28:54 -05:00
|
|
|
Ok(crate::util::fs::dir_size(&package_folder)?)
|
2022-10-28 16:19:55 -04:00
|
|
|
}
|
|
|
|
|
2022-09-13 11:59:01 -04:00
|
|
|
fn has_packages(&self) -> bool {
|
|
|
|
self.resolution.has_packages()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_package_reqs(
|
|
|
|
&self,
|
|
|
|
packages: Vec<NpmPackageReq>,
|
|
|
|
) -> BoxFuture<'static, Result<(), AnyError>> {
|
|
|
|
let resolver = self.clone();
|
|
|
|
async move {
|
|
|
|
resolver.resolution.add_package_reqs(packages).await?;
|
2022-10-21 11:20:18 -04:00
|
|
|
cache_packages_in_resolver(&resolver).await
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_package_reqs(
|
|
|
|
&self,
|
|
|
|
packages: HashSet<NpmPackageReq>,
|
|
|
|
) -> BoxFuture<'static, Result<(), AnyError>> {
|
|
|
|
let resolver = self.clone();
|
|
|
|
async move {
|
|
|
|
resolver.resolution.set_package_reqs(packages).await?;
|
|
|
|
cache_packages_in_resolver(&resolver).await
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_read_permission(&self, path: &Path) -> Result<(), AnyError> {
|
|
|
|
let registry_path = self.cache.registry_folder(&self.registry_url);
|
2022-09-22 11:17:02 -04:00
|
|
|
ensure_registry_read_permission(®istry_path, path)
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
2022-09-28 13:04:16 -04:00
|
|
|
|
|
|
|
fn snapshot(&self) -> NpmResolutionSnapshot {
|
|
|
|
self.resolution.snapshot()
|
|
|
|
}
|
2022-10-25 12:20:07 -04:00
|
|
|
|
|
|
|
fn lock(&self, lockfile: &mut Lockfile) -> Result<(), AnyError> {
|
|
|
|
let snapshot = self.resolution.snapshot();
|
|
|
|
self.resolution.lock(lockfile, &snapshot)
|
|
|
|
}
|
2022-09-13 11:59:01 -04:00
|
|
|
}
|
2022-10-21 11:20:18 -04:00
|
|
|
|
|
|
|
async fn cache_packages_in_resolver(
|
|
|
|
resolver: &GlobalNpmPackageResolver,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-11-08 14:17:24 -05:00
|
|
|
let package_partitions = resolver.resolution.all_packages_partitioned();
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
cache_packages(
|
2022-11-08 14:17:24 -05:00
|
|
|
package_partitions.packages,
|
2022-10-21 11:20:18 -04:00
|
|
|
&resolver.cache,
|
|
|
|
&resolver.registry_url,
|
|
|
|
)
|
2022-11-08 14:17:24 -05:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
// create the copy package folders
|
|
|
|
for copy in package_partitions.copy_packages {
|
|
|
|
resolver.cache.ensure_copy_package(
|
|
|
|
©.get_package_cache_folder_id(),
|
|
|
|
&resolver.registry_url,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|