summaryrefslogtreecommitdiff
path: root/cli/npm/resolvers/common.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-09-22 11:17:02 -0400
committerGitHub <noreply@github.com>2022-09-22 11:17:02 -0400
commit716005a0d4afd1042fa75d8bdc32fd13e9ebe95f (patch)
tree4c417eb7b91d6203aacba7dcd81bee3f13c0cfd3 /cli/npm/resolvers/common.rs
parent9a216806d514b5f41c73c777010572cdf3c51eab (diff)
feat(npm): add flag for creating and resolving npm packages to a local node_modules folder (#15971)
Diffstat (limited to 'cli/npm/resolvers/common.rs')
-rw-r--r--cli/npm/resolvers/common.rs54
1 files changed, 39 insertions, 15 deletions
diff --git a/cli/npm/resolvers/common.rs b/cli/npm/resolvers/common.rs
index f0231859a..cc590e2ad 100644
--- a/cli/npm/resolvers/common.rs
+++ b/cli/npm/resolvers/common.rs
@@ -1,3 +1,6 @@
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
+use std::io::ErrorKind;
use std::path::Path;
use std::path::PathBuf;
@@ -9,34 +12,25 @@ use deno_core::futures::future::BoxFuture;
use deno_core::url::Url;
use crate::npm::NpmCache;
-use crate::npm::NpmPackageId;
use crate::npm::NpmPackageReq;
use crate::npm::NpmResolutionPackage;
-/// Information about the local npm package.
-pub struct LocalNpmPackageInfo {
- /// Unique identifier.
- pub id: NpmPackageId,
- /// Local folder path of the npm package.
- pub folder_path: PathBuf,
-}
-
pub trait InnerNpmPackageResolver: Send + Sync {
- fn resolve_package_from_deno_module(
+ fn resolve_package_folder_from_deno_module(
&self,
pkg_req: &NpmPackageReq,
- ) -> Result<LocalNpmPackageInfo, AnyError>;
+ ) -> Result<PathBuf, AnyError>;
- fn resolve_package_from_package(
+ fn resolve_package_folder_from_package(
&self,
name: &str,
referrer: &ModuleSpecifier,
- ) -> Result<LocalNpmPackageInfo, AnyError>;
+ ) -> Result<PathBuf, AnyError>;
- fn resolve_package_from_specifier(
+ fn resolve_package_folder_from_specifier(
&self,
specifier: &ModuleSpecifier,
- ) -> Result<LocalNpmPackageInfo, AnyError>;
+ ) -> Result<PathBuf, AnyError>;
fn has_packages(&self) -> bool;
@@ -87,3 +81,33 @@ pub async fn cache_packages(
}
Ok(())
}
+
+pub fn ensure_registry_read_permission(
+ registry_path: &Path,
+ path: &Path,
+) -> Result<(), AnyError> {
+ // allow reading if it's in the node_modules
+ if path.starts_with(&registry_path)
+ && path
+ .components()
+ .all(|c| !matches!(c, std::path::Component::ParentDir))
+ {
+ // todo(dsherret): cache this?
+ if let Ok(registry_path) = std::fs::canonicalize(registry_path) {
+ match std::fs::canonicalize(path) {
+ Ok(path) if path.starts_with(registry_path) => {
+ return Ok(());
+ }
+ Err(e) if e.kind() == ErrorKind::NotFound => {
+ return Ok(());
+ }
+ _ => {} // ignore
+ }
+ }
+ }
+
+ Err(deno_core::error::custom_error(
+ "PermissionDenied",
+ format!("Reading {} is not allowed", path.display()),
+ ))
+}