mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 20:25:12 -05:00
679902a108
Extracted out of https://github.com/denoland/deno/pull/27838/files Reduces some allocations by accepting either a pathbuf or url for the referrer for resolution and returning either a pathbuf or url at the end, which the caller can then convert into to their preferred state. This is about 4% faster when still converting the final result to a url and 6% faster when keeping the result as a path in a benchmark I ran.
42 lines
1.1 KiB
Rust
42 lines
1.1 KiB
Rust
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
|
|
use deno_path_util::url_from_directory_path;
|
|
use deno_path_util::url_from_file_path;
|
|
use url::Url;
|
|
|
|
use crate::errors;
|
|
use crate::path::PathClean;
|
|
use crate::path::UrlOrPathRef;
|
|
|
|
pub trait NpmPackageFolderResolver {
|
|
/// Resolves an npm package folder path from the specified referrer.
|
|
fn resolve_package_folder_from_package(
|
|
&self,
|
|
specifier: &str,
|
|
referrer: &UrlOrPathRef,
|
|
) -> Result<PathBuf, errors::PackageFolderResolveError>;
|
|
}
|
|
|
|
/// Checks if a provided specifier is in an npm package.
|
|
pub trait InNpmPackageChecker {
|
|
fn in_npm_package(&self, specifier: &Url) -> bool;
|
|
|
|
fn in_npm_package_at_dir_path(&self, path: &Path) -> bool {
|
|
let specifier = match url_from_directory_path(&path.to_path_buf().clean()) {
|
|
Ok(p) => p,
|
|
Err(_) => return false,
|
|
};
|
|
self.in_npm_package(&specifier)
|
|
}
|
|
|
|
fn in_npm_package_at_file_path(&self, path: &Path) -> bool {
|
|
let specifier = match url_from_file_path(&path.to_path_buf().clean()) {
|
|
Ok(p) => p,
|
|
Err(_) => return false,
|
|
};
|
|
self.in_npm_package(&specifier)
|
|
}
|
|
}
|