2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-08-15 15:36:36 -04:00
|
|
|
use std::borrow::Cow;
|
2023-07-25 17:43:00 -04:00
|
|
|
use std::collections::HashMap;
|
2022-08-30 14:09:22 -04:00
|
|
|
use std::path::Path;
|
2022-08-20 11:31:33 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-08-30 14:09:22 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2022-08-20 11:31:33 -04:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::serde_json::Map;
|
|
|
|
use deno_core::serde_json::Value;
|
|
|
|
use deno_core::url::Url;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2023-05-08 11:02:02 -04:00
|
|
|
use deno_fs::FileSystemRc;
|
2023-04-24 19:44:35 -04:00
|
|
|
use deno_media_type::MediaType;
|
2024-07-23 17:34:46 -04:00
|
|
|
use deno_package_json::PackageJsonRc;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
|
|
|
use crate::errors;
|
2024-07-16 18:32:41 -04:00
|
|
|
use crate::errors::CanonicalizingPkgJsonDirError;
|
2024-07-09 12:15:03 -04:00
|
|
|
use crate::errors::ClosestPkgJsonError;
|
|
|
|
use crate::errors::FinalizeResolutionError;
|
|
|
|
use crate::errors::InvalidModuleSpecifierError;
|
|
|
|
use crate::errors::InvalidPackageTargetError;
|
|
|
|
use crate::errors::LegacyMainResolveError;
|
|
|
|
use crate::errors::ModuleNotFoundError;
|
2024-07-16 18:32:41 -04:00
|
|
|
use crate::errors::NodeJsErrorCode;
|
|
|
|
use crate::errors::NodeJsErrorCoded;
|
2024-07-09 12:15:03 -04:00
|
|
|
use crate::errors::NodeResolveError;
|
|
|
|
use crate::errors::PackageExportsResolveError;
|
|
|
|
use crate::errors::PackageImportNotDefinedError;
|
|
|
|
use crate::errors::PackageImportsResolveError;
|
|
|
|
use crate::errors::PackageImportsResolveErrorKind;
|
2024-07-16 18:32:41 -04:00
|
|
|
use crate::errors::PackageJsonLoadError;
|
2024-07-09 12:15:03 -04:00
|
|
|
use crate::errors::PackagePathNotExportedError;
|
|
|
|
use crate::errors::PackageResolveError;
|
|
|
|
use crate::errors::PackageSubpathResolveError;
|
|
|
|
use crate::errors::PackageSubpathResolveErrorKind;
|
|
|
|
use crate::errors::PackageTargetNotFoundError;
|
|
|
|
use crate::errors::PackageTargetResolveError;
|
|
|
|
use crate::errors::PackageTargetResolveErrorKind;
|
|
|
|
use crate::errors::PathToDeclarationUrlError;
|
|
|
|
use crate::errors::ResolveBinaryCommandsError;
|
|
|
|
use crate::errors::ResolvePkgJsonBinExportError;
|
|
|
|
use crate::errors::ResolvePkgSubpathFromDenoModuleError;
|
|
|
|
use crate::errors::TypeScriptNotSupportedInNpmError;
|
|
|
|
use crate::errors::UnsupportedDirImportError;
|
|
|
|
use crate::errors::UnsupportedEsmUrlSchemeError;
|
|
|
|
use crate::errors::UrlToNodeResolutionError;
|
2023-12-14 10:09:05 -05:00
|
|
|
use crate::is_builtin_node_module;
|
|
|
|
use crate::path::to_file_specifier;
|
|
|
|
use crate::polyfill::get_module_name_from_builtin_node_module_specifier;
|
2023-05-08 11:02:02 -04:00
|
|
|
use crate::NpmResolverRc;
|
2023-04-24 19:44:35 -04:00
|
|
|
use crate::PackageJson;
|
|
|
|
use crate::PathClean;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
|
|
|
pub static DEFAULT_CONDITIONS: &[&str] = &["deno", "node", "import"];
|
|
|
|
pub static REQUIRE_CONDITIONS: &[&str] = &["require", "node"];
|
|
|
|
|
2024-07-23 17:34:46 -04:00
|
|
|
pub type NodeModuleKind = deno_package_json::NodeModuleKind;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2022-11-30 18:07:32 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum NodeResolutionMode {
|
|
|
|
Execution,
|
|
|
|
Types,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeResolutionMode {
|
|
|
|
pub fn is_types(&self) -> bool {
|
|
|
|
matches!(self, NodeResolutionMode::Types)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum NodeResolution {
|
|
|
|
Esm(ModuleSpecifier),
|
|
|
|
CommonJs(ModuleSpecifier),
|
|
|
|
BuiltIn(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeResolution {
|
|
|
|
pub fn into_url(self) -> ModuleSpecifier {
|
|
|
|
match self {
|
|
|
|
Self::Esm(u) => u,
|
|
|
|
Self::CommonJs(u) => u,
|
|
|
|
Self::BuiltIn(specifier) => {
|
|
|
|
if specifier.starts_with("node:") {
|
|
|
|
ModuleSpecifier::parse(&specifier).unwrap()
|
|
|
|
} else {
|
|
|
|
ModuleSpecifier::parse(&format!("node:{specifier}")).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2022-11-19 10:40:01 -05:00
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_specifier_and_media_type(
|
|
|
|
resolution: Option<Self>,
|
|
|
|
) -> (ModuleSpecifier, MediaType) {
|
|
|
|
match resolution {
|
|
|
|
Some(NodeResolution::CommonJs(specifier)) => {
|
|
|
|
let media_type = MediaType::from_specifier(&specifier);
|
|
|
|
(
|
|
|
|
specifier,
|
|
|
|
match media_type {
|
|
|
|
MediaType::JavaScript | MediaType::Jsx => MediaType::Cjs,
|
|
|
|
MediaType::TypeScript | MediaType::Tsx => MediaType::Cts,
|
|
|
|
MediaType::Dts => MediaType::Dcts,
|
|
|
|
_ => media_type,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Some(NodeResolution::Esm(specifier)) => {
|
|
|
|
let media_type = MediaType::from_specifier(&specifier);
|
|
|
|
(
|
|
|
|
specifier,
|
|
|
|
match media_type {
|
|
|
|
MediaType::JavaScript | MediaType::Jsx => MediaType::Mjs,
|
|
|
|
MediaType::TypeScript | MediaType::Tsx => MediaType::Mts,
|
|
|
|
MediaType::Dts => MediaType::Dmts,
|
|
|
|
_ => media_type,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Some(resolution) => (resolution.into_url(), MediaType::Dts),
|
|
|
|
None => (
|
|
|
|
ModuleSpecifier::parse("internal:///missing_dependency.d.ts").unwrap(),
|
|
|
|
MediaType::Dts,
|
|
|
|
),
|
2022-11-19 10:40:01 -05:00
|
|
|
}
|
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2022-11-19 10:40:01 -05:00
|
|
|
|
2023-05-08 11:02:02 -04:00
|
|
|
#[allow(clippy::disallowed_types)]
|
|
|
|
pub type NodeResolverRc = deno_fs::sync::MaybeArc<NodeResolver>;
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NodeResolver {
|
2023-05-08 11:02:02 -04:00
|
|
|
fs: FileSystemRc,
|
|
|
|
npm_resolver: NpmResolverRc,
|
2023-07-25 17:43:00 -04:00
|
|
|
in_npm_package_cache: deno_fs::sync::MaybeArcMutex<HashMap<String, bool>>,
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeResolver {
|
2023-05-08 11:02:02 -04:00
|
|
|
pub fn new(fs: FileSystemRc, npm_resolver: NpmResolverRc) -> Self {
|
2023-07-25 17:43:00 -04:00
|
|
|
Self {
|
|
|
|
fs,
|
|
|
|
npm_resolver,
|
|
|
|
in_npm_package_cache: deno_fs::sync::MaybeArcMutex::new(HashMap::new()),
|
|
|
|
}
|
2022-11-16 14:41:27 -05:00
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
|
|
|
|
pub fn in_npm_package(&self, specifier: &ModuleSpecifier) -> bool {
|
|
|
|
self.npm_resolver.in_npm_package(specifier)
|
2022-11-19 10:40:01 -05:00
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
|
2023-08-15 15:36:36 -04:00
|
|
|
pub fn in_npm_package_with_cache(&self, specifier: Cow<str>) -> bool {
|
2023-07-25 17:43:00 -04:00
|
|
|
let mut cache = self.in_npm_package_cache.lock();
|
|
|
|
|
2023-08-15 15:36:36 -04:00
|
|
|
if let Some(result) = cache.get(specifier.as_ref()) {
|
2023-07-25 17:43:00 -04:00
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
let result =
|
|
|
|
if let Ok(specifier) = deno_core::ModuleSpecifier::parse(&specifier) {
|
|
|
|
self.npm_resolver.in_npm_package(&specifier)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2023-08-15 15:36:36 -04:00
|
|
|
cache.insert(specifier.into_owned(), result);
|
2023-07-25 17:43:00 -04:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
/// This function is an implementation of `defaultResolve` in
|
|
|
|
/// `lib/internal/modules/esm/resolve.js` from Node.
|
|
|
|
pub fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2024-07-10 14:46:25 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
2023-04-24 19:44:35 -04:00
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<NodeResolution>, NodeResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
// Note: if we are here, then the referrer is an esm module
|
|
|
|
// TODO(bartlomieju): skipped "policy" part as we don't plan to support it
|
|
|
|
|
|
|
|
if crate::is_builtin_node_module(specifier) {
|
|
|
|
return Ok(Some(NodeResolution::BuiltIn(specifier.to_string())));
|
2022-11-19 10:40:01 -05:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if let Ok(url) = Url::parse(specifier) {
|
|
|
|
if url.scheme() == "data" {
|
|
|
|
return Ok(Some(NodeResolution::Esm(url)));
|
|
|
|
}
|
2022-11-19 10:40:01 -05:00
|
|
|
|
2023-12-14 10:09:05 -05:00
|
|
|
if let Some(module_name) =
|
|
|
|
get_module_name_from_builtin_node_module_specifier(&url)
|
|
|
|
{
|
|
|
|
return Ok(Some(NodeResolution::BuiltIn(module_name.to_string())));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
2023-12-14 10:09:05 -05:00
|
|
|
let protocol = url.scheme();
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if protocol != "file" && protocol != "data" {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
UnsupportedEsmUrlSchemeError {
|
|
|
|
url_scheme: protocol.to_string(),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo(dsherret): this seems wrong
|
|
|
|
if referrer.scheme() == "data" {
|
2024-07-09 12:15:03 -04:00
|
|
|
let url = referrer.join(specifier).map_err(|source| {
|
|
|
|
NodeResolveError::DataUrlReferrerFailed { source }
|
|
|
|
})?;
|
2023-04-24 19:44:35 -04:00
|
|
|
return Ok(Some(NodeResolution::Esm(url)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-10 14:46:25 -04:00
|
|
|
let maybe_url = self.module_resolve(
|
|
|
|
specifier,
|
|
|
|
referrer,
|
|
|
|
referrer_kind,
|
|
|
|
match referrer_kind {
|
|
|
|
NodeModuleKind::Esm => DEFAULT_CONDITIONS,
|
|
|
|
NodeModuleKind::Cjs => REQUIRE_CONDITIONS,
|
|
|
|
},
|
|
|
|
mode,
|
|
|
|
)?;
|
2024-07-09 12:15:03 -04:00
|
|
|
let url = match maybe_url {
|
2023-04-24 19:44:35 -04:00
|
|
|
Some(url) => url,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let resolve_response = self.url_to_node_resolution(url)?;
|
|
|
|
// TODO(bartlomieju): skipped checking errors for commonJS resolution and
|
|
|
|
// "preserveSymlinksMain"/"preserveSymlinks" options.
|
|
|
|
Ok(Some(resolve_response))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn module_resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2024-07-10 14:46:25 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
2023-04-24 19:44:35 -04:00
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, NodeResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
// note: if we're here, the referrer is an esm module
|
2024-07-09 12:15:03 -04:00
|
|
|
let maybe_url = if should_be_treated_as_relative_or_absolute_path(specifier)
|
|
|
|
{
|
|
|
|
Some(referrer.join(specifier).map_err(|err| {
|
|
|
|
NodeResolveError::RelativeJoinError {
|
|
|
|
path: specifier.to_string(),
|
|
|
|
base: referrer.clone(),
|
|
|
|
source: err,
|
|
|
|
}
|
|
|
|
})?)
|
2023-04-24 19:44:35 -04:00
|
|
|
} else if specifier.starts_with('#') {
|
2024-07-09 12:15:03 -04:00
|
|
|
let pkg_config = self
|
|
|
|
.get_closest_package_json(referrer)
|
|
|
|
.map_err(PackageImportsResolveErrorKind::ClosestPkgJson)
|
|
|
|
.map_err(|err| PackageImportsResolveError(Box::new(err)))?;
|
2023-12-14 10:09:05 -05:00
|
|
|
Some(self.package_imports_resolve(
|
|
|
|
specifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
Some(referrer),
|
2024-07-10 14:46:25 -04:00
|
|
|
referrer_kind,
|
2024-04-01 09:10:04 -04:00
|
|
|
pkg_config.as_deref(),
|
2023-12-14 10:09:05 -05:00
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)?)
|
2023-04-24 19:44:35 -04:00
|
|
|
} else if let Ok(resolved) = Url::parse(specifier) {
|
|
|
|
Some(resolved)
|
2022-11-16 14:41:27 -05:00
|
|
|
} else {
|
2023-12-14 10:09:05 -05:00
|
|
|
self.package_resolve(
|
|
|
|
specifier,
|
|
|
|
referrer,
|
2024-07-10 14:46:25 -04:00
|
|
|
referrer_kind,
|
2023-12-14 10:09:05 -05:00
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)?
|
2023-04-24 19:44:35 -04:00
|
|
|
};
|
2024-07-09 12:15:03 -04:00
|
|
|
|
|
|
|
let Some(url) = maybe_url else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
|
|
|
let maybe_url = if mode.is_types() {
|
|
|
|
let file_path = to_file_path(&url);
|
2024-07-16 18:32:41 -04:00
|
|
|
self.path_to_declaration_url(&file_path, Some(referrer), referrer_kind)?
|
2024-07-09 12:15:03 -04:00
|
|
|
} else {
|
|
|
|
Some(url)
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(match maybe_url {
|
|
|
|
Some(url) => Some(self.finalize_resolution(url, Some(referrer))?),
|
2023-04-24 19:44:35 -04:00
|
|
|
None => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finalize_resolution(
|
|
|
|
&self,
|
|
|
|
resolved: ModuleSpecifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> Result<ModuleSpecifier, FinalizeResolutionError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
let encoded_sep_re = lazy_regex::regex!(r"%2F|%2C");
|
|
|
|
|
|
|
|
if encoded_sep_re.is_match(resolved.path()) {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
errors::InvalidModuleSpecifierError {
|
|
|
|
request: resolved.to_string(),
|
|
|
|
reason: Cow::Borrowed(
|
|
|
|
"must not include encoded \"/\" or \"\\\\\" characters",
|
|
|
|
),
|
|
|
|
maybe_referrer: maybe_referrer.map(to_file_path_string),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2022-11-16 14:41:27 -05:00
|
|
|
}
|
2022-11-19 10:40:01 -05:00
|
|
|
|
2023-12-14 10:09:05 -05:00
|
|
|
if resolved.scheme() == "node" {
|
|
|
|
return Ok(resolved);
|
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
let path = to_file_path(&resolved);
|
2022-11-16 14:41:27 -05:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
// TODO(bartlomieju): currently not supported
|
|
|
|
// if (getOptionValue('--experimental-specifier-resolution') === 'node') {
|
|
|
|
// ...
|
|
|
|
// }
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
let p_str = path.to_str().unwrap();
|
|
|
|
let p = if p_str.ends_with('/') {
|
|
|
|
p_str[p_str.len() - 1..].to_string()
|
|
|
|
} else {
|
|
|
|
p_str.to_string()
|
|
|
|
};
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-05-05 12:44:24 -04:00
|
|
|
let (is_dir, is_file) = if let Ok(stats) = self.fs.stat_sync(Path::new(&p))
|
|
|
|
{
|
|
|
|
(stats.is_directory, stats.is_file)
|
2023-04-24 19:44:35 -04:00
|
|
|
} else {
|
|
|
|
(false, false)
|
|
|
|
};
|
|
|
|
if is_dir {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
UnsupportedDirImportError {
|
|
|
|
dir_url: resolved.clone(),
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
} else if !is_file {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
ModuleNotFoundError {
|
|
|
|
specifier: resolved,
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
typ: "module",
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
Ok(resolved)
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-10-05 16:18:29 -04:00
|
|
|
pub fn resolve_package_subpath_from_deno_module(
|
2023-04-24 19:44:35 -04:00
|
|
|
&self,
|
2023-10-04 23:05:12 -04:00
|
|
|
package_dir: &Path,
|
|
|
|
package_subpath: Option<&str>,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-04-24 19:44:35 -04:00
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<NodeResolution>, ResolvePkgSubpathFromDenoModuleError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
let node_module_kind = NodeModuleKind::Esm;
|
2023-10-04 23:05:12 -04:00
|
|
|
let package_subpath = package_subpath
|
|
|
|
.map(|s| format!("./{s}"))
|
|
|
|
.unwrap_or_else(|| ".".to_string());
|
2024-06-26 17:24:10 -04:00
|
|
|
let maybe_resolved_url = self.resolve_package_dir_subpath(
|
|
|
|
package_dir,
|
|
|
|
&package_subpath,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2024-06-26 17:24:10 -04:00
|
|
|
node_module_kind,
|
|
|
|
DEFAULT_CONDITIONS,
|
|
|
|
mode,
|
|
|
|
)?;
|
2023-12-14 10:09:05 -05:00
|
|
|
let resolved_url = match maybe_resolved_url {
|
2024-07-09 12:15:03 -04:00
|
|
|
Some(resolved_url) => resolved_url,
|
2023-04-24 19:44:35 -04:00
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2023-12-14 10:09:05 -05:00
|
|
|
let resolve_response = self.url_to_node_resolution(resolved_url)?;
|
2023-04-24 19:44:35 -04:00
|
|
|
// TODO(bartlomieju): skipped checking errors for commonJS resolution and
|
|
|
|
// "preserveSymlinksMain"/"preserveSymlinks" options.
|
|
|
|
Ok(Some(resolve_response))
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
pub fn resolve_binary_commands(
|
|
|
|
&self,
|
2023-09-28 16:43:45 -04:00
|
|
|
package_folder: &Path,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Vec<String>, ResolveBinaryCommandsError> {
|
|
|
|
let pkg_json_path = package_folder.join("package.json");
|
|
|
|
let Some(package_json) = self.load_package_json(&pkg_json_path)? else {
|
2024-06-26 17:24:10 -04:00
|
|
|
return Ok(Vec::new());
|
|
|
|
};
|
2023-04-24 19:44:35 -04:00
|
|
|
|
2024-04-01 09:10:04 -04:00
|
|
|
Ok(match &package_json.bin {
|
2023-09-28 16:43:45 -04:00
|
|
|
Some(Value::String(_)) => {
|
|
|
|
let Some(name) = &package_json.name else {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(ResolveBinaryCommandsError::MissingPkgJsonName {
|
|
|
|
pkg_json_path,
|
|
|
|
});
|
2023-09-28 16:43:45 -04:00
|
|
|
};
|
|
|
|
vec![name.to_string()]
|
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
Some(Value::Object(o)) => {
|
2024-04-01 09:10:04 -04:00
|
|
|
o.iter().map(|(key, _)| key.clone()).collect::<Vec<_>>()
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
_ => Vec::new(),
|
|
|
|
})
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
pub fn resolve_binary_export(
|
|
|
|
&self,
|
2023-09-28 16:43:45 -04:00
|
|
|
package_folder: &Path,
|
|
|
|
sub_path: Option<&str>,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<NodeResolution, ResolvePkgJsonBinExportError> {
|
|
|
|
let pkg_json_path = package_folder.join("package.json");
|
|
|
|
let Some(package_json) = self.load_package_json(&pkg_json_path)? else {
|
|
|
|
return Err(ResolvePkgJsonBinExportError::MissingPkgJson {
|
|
|
|
pkg_json_path,
|
|
|
|
});
|
2024-06-26 17:24:10 -04:00
|
|
|
};
|
2024-07-09 12:15:03 -04:00
|
|
|
let bin_entry =
|
|
|
|
resolve_bin_entry_value(&package_json, sub_path).map_err(|err| {
|
|
|
|
ResolvePkgJsonBinExportError::InvalidBinProperty {
|
|
|
|
message: err.to_string(),
|
|
|
|
}
|
|
|
|
})?;
|
2023-12-14 10:09:05 -05:00
|
|
|
let url = to_file_specifier(&package_folder.join(bin_entry));
|
2023-04-24 19:44:35 -04:00
|
|
|
|
|
|
|
let resolve_response = self.url_to_node_resolution(url)?;
|
|
|
|
// TODO(bartlomieju): skipped checking errors for commonJS resolution and
|
|
|
|
// "preserveSymlinksMain"/"preserveSymlinks" options.
|
|
|
|
Ok(resolve_response)
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
pub fn url_to_node_resolution(
|
|
|
|
&self,
|
|
|
|
url: ModuleSpecifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<NodeResolution, UrlToNodeResolutionError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
let url_str = url.as_str().to_lowercase();
|
2023-08-01 19:20:08 -04:00
|
|
|
if url_str.starts_with("http") || url_str.ends_with(".json") {
|
2023-04-24 19:44:35 -04:00
|
|
|
Ok(NodeResolution::Esm(url))
|
|
|
|
} else if url_str.ends_with(".js") || url_str.ends_with(".d.ts") {
|
2024-06-26 17:24:10 -04:00
|
|
|
let maybe_package_config = self.get_closest_package_json(&url)?;
|
2023-07-17 14:00:44 -04:00
|
|
|
match maybe_package_config {
|
|
|
|
Some(c) if c.typ == "module" => Ok(NodeResolution::Esm(url)),
|
|
|
|
Some(_) => Ok(NodeResolution::CommonJs(url)),
|
|
|
|
None => Ok(NodeResolution::Esm(url)),
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
} else if url_str.ends_with(".mjs") || url_str.ends_with(".d.mts") {
|
|
|
|
Ok(NodeResolution::Esm(url))
|
2023-07-17 17:17:58 -04:00
|
|
|
} else if url_str.ends_with(".ts") || url_str.ends_with(".mts") {
|
|
|
|
if self.in_npm_package(&url) {
|
2024-07-09 12:15:03 -04:00
|
|
|
Err(TypeScriptNotSupportedInNpmError { specifier: url }.into())
|
2023-07-17 17:17:58 -04:00
|
|
|
} else {
|
|
|
|
Ok(NodeResolution::Esm(url))
|
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
} else {
|
|
|
|
Ok(NodeResolution::CommonJs(url))
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
/// Checks if the resolved file has a corresponding declaration file.
|
2024-03-13 22:37:56 -04:00
|
|
|
fn path_to_declaration_url(
|
2023-04-24 19:44:35 -04:00
|
|
|
&self,
|
2024-07-16 18:32:41 -04:00
|
|
|
path: &Path,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PathToDeclarationUrlError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
fn probe_extensions(
|
2023-05-05 12:44:24 -04:00
|
|
|
fs: &dyn deno_fs::FileSystem,
|
2023-04-24 19:44:35 -04:00
|
|
|
path: &Path,
|
2023-07-28 11:24:22 -04:00
|
|
|
lowercase_path: &str,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
) -> Option<PathBuf> {
|
2023-07-28 11:24:22 -04:00
|
|
|
let mut searched_for_d_mts = false;
|
|
|
|
let mut searched_for_d_cts = false;
|
|
|
|
if lowercase_path.ends_with(".mjs") {
|
|
|
|
let d_mts_path = with_known_extension(path, "d.mts");
|
2023-08-08 16:28:18 -04:00
|
|
|
if fs.exists_sync(&d_mts_path) {
|
2023-07-28 11:24:22 -04:00
|
|
|
return Some(d_mts_path);
|
|
|
|
}
|
|
|
|
searched_for_d_mts = true;
|
|
|
|
} else if lowercase_path.ends_with(".cjs") {
|
|
|
|
let d_cts_path = with_known_extension(path, "d.cts");
|
2023-08-08 16:28:18 -04:00
|
|
|
if fs.exists_sync(&d_cts_path) {
|
2023-07-28 11:24:22 -04:00
|
|
|
return Some(d_cts_path);
|
|
|
|
}
|
|
|
|
searched_for_d_cts = true;
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2023-07-28 11:24:22 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
let dts_path = with_known_extension(path, "d.ts");
|
2023-08-08 16:28:18 -04:00
|
|
|
if fs.exists_sync(&dts_path) {
|
2023-07-28 11:24:22 -04:00
|
|
|
return Some(dts_path);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2023-07-28 11:24:22 -04:00
|
|
|
|
|
|
|
let specific_dts_path = match referrer_kind {
|
|
|
|
NodeModuleKind::Cjs if !searched_for_d_cts => {
|
|
|
|
Some(with_known_extension(path, "d.cts"))
|
|
|
|
}
|
|
|
|
NodeModuleKind::Esm if !searched_for_d_mts => {
|
|
|
|
Some(with_known_extension(path, "d.mts"))
|
|
|
|
}
|
|
|
|
_ => None, // already searched above
|
|
|
|
};
|
|
|
|
if let Some(specific_dts_path) = specific_dts_path {
|
2023-08-08 16:28:18 -04:00
|
|
|
if fs.exists_sync(&specific_dts_path) {
|
2023-07-28 11:24:22 -04:00
|
|
|
return Some(specific_dts_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
let lowercase_path = path.to_string_lossy().to_lowercase();
|
|
|
|
if lowercase_path.ends_with(".d.ts")
|
|
|
|
|| lowercase_path.ends_with(".d.cts")
|
2023-07-28 11:24:22 -04:00
|
|
|
|| lowercase_path.ends_with(".d.mts")
|
2023-04-24 19:44:35 -04:00
|
|
|
{
|
2024-07-16 18:32:41 -04:00
|
|
|
return Ok(Some(to_file_specifier(path)));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2023-07-28 11:24:22 -04:00
|
|
|
if let Some(path) =
|
2024-07-16 18:32:41 -04:00
|
|
|
probe_extensions(&*self.fs, path, &lowercase_path, referrer_kind)
|
2023-07-28 11:24:22 -04:00
|
|
|
{
|
2024-03-13 22:37:56 -04:00
|
|
|
return Ok(Some(to_file_specifier(&path)));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2024-07-16 18:32:41 -04:00
|
|
|
if self.fs.is_dir_sync(path) {
|
2024-06-26 17:24:10 -04:00
|
|
|
let maybe_resolution = self.resolve_package_dir_subpath(
|
2024-07-16 18:32:41 -04:00
|
|
|
path,
|
2024-06-26 17:24:10 -04:00
|
|
|
/* sub path */ ".",
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2024-06-26 17:24:10 -04:00
|
|
|
referrer_kind,
|
|
|
|
match referrer_kind {
|
|
|
|
NodeModuleKind::Esm => DEFAULT_CONDITIONS,
|
|
|
|
NodeModuleKind::Cjs => REQUIRE_CONDITIONS,
|
|
|
|
},
|
|
|
|
NodeResolutionMode::Types,
|
|
|
|
)?;
|
|
|
|
if let Some(resolution) = maybe_resolution {
|
|
|
|
return Ok(Some(resolution));
|
2024-03-13 22:37:56 -04:00
|
|
|
}
|
2023-07-28 11:24:22 -04:00
|
|
|
let index_path = path.join("index.js");
|
|
|
|
if let Some(path) = probe_extensions(
|
|
|
|
&*self.fs,
|
|
|
|
&index_path,
|
|
|
|
&index_path.to_string_lossy().to_lowercase(),
|
|
|
|
referrer_kind,
|
|
|
|
) {
|
2024-03-13 22:37:56 -04:00
|
|
|
return Ok(Some(to_file_specifier(&path)));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
2024-03-08 14:14:28 -05:00
|
|
|
// allow resolving .css files for types resolution
|
|
|
|
if lowercase_path.ends_with(".css") {
|
2024-07-16 18:32:41 -04:00
|
|
|
return Ok(Some(to_file_specifier(path)));
|
2024-03-08 14:14:28 -05:00
|
|
|
}
|
2024-03-13 22:37:56 -04:00
|
|
|
Ok(None)
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
2024-03-09 10:21:31 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-04-24 19:44:35 -04:00
|
|
|
pub(super) fn package_imports_resolve(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
2024-03-09 10:21:31 -05:00
|
|
|
referrer_pkg_json: Option<&PackageJson>,
|
2023-04-24 19:44:35 -04:00
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<ModuleSpecifier, PackageImportsResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
if name == "#" || name.starts_with("#/") || name.ends_with('/') {
|
|
|
|
let reason = "is not a valid internal imports specifier name";
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
errors::InvalidModuleSpecifierError {
|
|
|
|
request: name.to_string(),
|
|
|
|
reason: Cow::Borrowed(reason),
|
|
|
|
maybe_referrer: maybe_referrer.map(to_specifier_display_string),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut package_json_path = None;
|
2024-03-09 10:21:31 -05:00
|
|
|
if let Some(pkg_json) = &referrer_pkg_json {
|
2024-06-26 17:24:10 -04:00
|
|
|
package_json_path = Some(pkg_json.path.clone());
|
|
|
|
if let Some(imports) = &pkg_json.imports {
|
|
|
|
if imports.contains_key(name) && !name.contains('*') {
|
|
|
|
let target = imports.get(name).unwrap();
|
|
|
|
let maybe_resolved = self.resolve_package_target(
|
|
|
|
package_json_path.as_ref().unwrap(),
|
|
|
|
target,
|
|
|
|
"",
|
|
|
|
name,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2024-06-26 17:24:10 -04:00
|
|
|
referrer_kind,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)?;
|
|
|
|
if let Some(resolved) = maybe_resolved {
|
|
|
|
return Ok(resolved);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut best_match = "";
|
|
|
|
let mut best_match_subpath = None;
|
|
|
|
for key in imports.keys() {
|
|
|
|
let pattern_index = key.find('*');
|
|
|
|
if let Some(pattern_index) = pattern_index {
|
|
|
|
let key_sub = &key[0..=pattern_index];
|
|
|
|
if name.starts_with(key_sub) {
|
|
|
|
let pattern_trailer = &key[pattern_index + 1..];
|
|
|
|
if name.len() > key.len()
|
|
|
|
&& name.ends_with(&pattern_trailer)
|
|
|
|
&& pattern_key_compare(best_match, key) == 1
|
|
|
|
&& key.rfind('*') == Some(pattern_index)
|
|
|
|
{
|
|
|
|
best_match = key;
|
|
|
|
best_match_subpath = Some(
|
|
|
|
name[pattern_index..=(name.len() - pattern_trailer.len())]
|
|
|
|
.to_string(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !best_match.is_empty() {
|
|
|
|
let target = imports.get(best_match).unwrap();
|
2023-04-24 19:44:35 -04:00
|
|
|
let maybe_resolved = self.resolve_package_target(
|
|
|
|
package_json_path.as_ref().unwrap(),
|
2023-12-14 10:09:05 -05:00
|
|
|
target,
|
2024-06-26 17:24:10 -04:00
|
|
|
&best_match_subpath.unwrap(),
|
|
|
|
best_match,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind,
|
2024-06-26 17:24:10 -04:00
|
|
|
true,
|
2023-04-24 19:44:35 -04:00
|
|
|
true,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)?;
|
|
|
|
if let Some(resolved) = maybe_resolved {
|
|
|
|
return Ok(resolved);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
Err(
|
|
|
|
PackageImportNotDefinedError {
|
|
|
|
name: name.to_string(),
|
|
|
|
package_json_path,
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
)
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn resolve_package_target_string(
|
|
|
|
&self,
|
2023-12-14 10:09:05 -05:00
|
|
|
target: &str,
|
2023-10-04 23:05:12 -04:00
|
|
|
subpath: &str,
|
|
|
|
match_: &str,
|
2023-04-24 19:44:35 -04:00
|
|
|
package_json_path: &Path,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
pattern: bool,
|
|
|
|
internal: bool,
|
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<ModuleSpecifier, PackageTargetResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
if !subpath.is_empty() && !pattern && !target.ends_with('/') {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
InvalidPackageTargetError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
sub_path: match_.to_string(),
|
|
|
|
target: target.to_string(),
|
|
|
|
is_import: internal,
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
let invalid_segment_re =
|
|
|
|
lazy_regex::regex!(r"(^|\\|/)(\.\.?|node_modules)(\\|/|$)");
|
|
|
|
let pattern_re = lazy_regex::regex!(r"\*");
|
|
|
|
if !target.starts_with("./") {
|
|
|
|
if internal && !target.starts_with("../") && !target.starts_with('/') {
|
2023-12-14 10:09:05 -05:00
|
|
|
let target_url = Url::parse(target);
|
|
|
|
match target_url {
|
|
|
|
Ok(url) => {
|
|
|
|
if get_module_name_from_builtin_node_module_specifier(&url)
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
return Ok(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
let export_target = if pattern {
|
|
|
|
pattern_re
|
|
|
|
.replace(target, |_caps: ®ex::Captures| subpath)
|
|
|
|
.to_string()
|
|
|
|
} else {
|
|
|
|
format!("{target}{subpath}")
|
|
|
|
};
|
|
|
|
let package_json_url = to_file_specifier(package_json_path);
|
|
|
|
let result = match self.package_resolve(
|
|
|
|
&export_target,
|
|
|
|
&package_json_url,
|
|
|
|
referrer_kind,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
) {
|
|
|
|
Ok(Some(url)) => Ok(url),
|
2024-07-09 12:15:03 -04:00
|
|
|
Ok(None) => Err(
|
|
|
|
PackageTargetResolveErrorKind::NotFound(
|
|
|
|
PackageTargetNotFoundError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
target: export_target.to_string(),
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
referrer_kind,
|
|
|
|
mode,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
Err(err) => {
|
|
|
|
Err(PackageTargetResolveErrorKind::PackageResolve(err).into())
|
|
|
|
}
|
2023-12-14 10:09:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return match result {
|
|
|
|
Ok(url) => Ok(url),
|
|
|
|
Err(err) => {
|
|
|
|
if is_builtin_node_module(target) {
|
|
|
|
Ok(
|
|
|
|
ModuleSpecifier::parse(&format!("node:{}", target))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
InvalidPackageTargetError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
sub_path: match_.to_string(),
|
|
|
|
target: target.to_string(),
|
|
|
|
is_import: internal,
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
if invalid_segment_re.is_match(&target[2..]) {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
InvalidPackageTargetError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
sub_path: match_.to_string(),
|
|
|
|
target: target.to_string(),
|
|
|
|
is_import: internal,
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
let package_path = package_json_path.parent().unwrap();
|
2023-12-14 10:09:05 -05:00
|
|
|
let resolved_path = package_path.join(target).clean();
|
2023-04-24 19:44:35 -04:00
|
|
|
if !resolved_path.starts_with(package_path) {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
InvalidPackageTargetError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
sub_path: match_.to_string(),
|
|
|
|
target: target.to_string(),
|
|
|
|
is_import: internal,
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
if subpath.is_empty() {
|
2023-12-14 10:09:05 -05:00
|
|
|
return Ok(to_file_specifier(&resolved_path));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2023-10-04 23:05:12 -04:00
|
|
|
if invalid_segment_re.is_match(subpath) {
|
2023-04-24 19:44:35 -04:00
|
|
|
let request = if pattern {
|
2023-10-04 23:05:12 -04:00
|
|
|
match_.replace('*', subpath)
|
2023-04-24 19:44:35 -04:00
|
|
|
} else {
|
|
|
|
format!("{match_}{subpath}")
|
|
|
|
};
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
throw_invalid_subpath(
|
|
|
|
request,
|
|
|
|
package_json_path,
|
|
|
|
internal,
|
|
|
|
maybe_referrer,
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
if pattern {
|
|
|
|
let resolved_path_str = resolved_path.to_string_lossy();
|
|
|
|
let replaced = pattern_re
|
2023-10-05 14:49:09 -04:00
|
|
|
.replace(&resolved_path_str, |_caps: ®ex::Captures| subpath);
|
2023-12-14 10:09:05 -05:00
|
|
|
return Ok(to_file_specifier(&PathBuf::from(replaced.to_string())));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2023-12-14 10:09:05 -05:00
|
|
|
Ok(to_file_specifier(&resolved_path.join(subpath).clean()))
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn resolve_package_target(
|
|
|
|
&self,
|
|
|
|
package_json_path: &Path,
|
2023-12-14 10:09:05 -05:00
|
|
|
target: &Value,
|
2023-10-04 23:05:12 -04:00
|
|
|
subpath: &str,
|
|
|
|
package_subpath: &str,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
pattern: bool,
|
|
|
|
internal: bool,
|
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PackageTargetResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
if let Some(target) = target.as_str() {
|
2024-03-13 22:37:56 -04:00
|
|
|
let url = self.resolve_package_target_string(
|
|
|
|
target,
|
|
|
|
subpath,
|
|
|
|
package_subpath,
|
|
|
|
package_json_path,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2024-03-13 22:37:56 -04:00
|
|
|
referrer_kind,
|
|
|
|
pattern,
|
|
|
|
internal,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)?;
|
|
|
|
if mode.is_types() && url.scheme() == "file" {
|
|
|
|
let path = url.to_file_path().unwrap();
|
2024-07-09 12:15:03 -04:00
|
|
|
return Ok(self.path_to_declaration_url(
|
2024-07-16 18:32:41 -04:00
|
|
|
&path,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
|
|
|
referrer_kind,
|
|
|
|
)?);
|
2024-03-13 22:37:56 -04:00
|
|
|
} else {
|
|
|
|
return Ok(Some(url));
|
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
} else if let Some(target_arr) = target.as_array() {
|
|
|
|
if target_arr.is_empty() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut last_error = None;
|
|
|
|
for target_item in target_arr {
|
|
|
|
let resolved_result = self.resolve_package_target(
|
|
|
|
package_json_path,
|
2023-12-14 10:09:05 -05:00
|
|
|
target_item,
|
2023-10-05 14:49:09 -04:00
|
|
|
subpath,
|
|
|
|
package_subpath,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind,
|
|
|
|
pattern,
|
|
|
|
internal,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
);
|
|
|
|
|
|
|
|
match resolved_result {
|
|
|
|
Ok(Some(resolved)) => return Ok(Some(resolved)),
|
|
|
|
Ok(None) => {
|
|
|
|
last_error = None;
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-11 16:32:52 -04:00
|
|
|
Err(e) => {
|
2024-07-16 18:32:41 -04:00
|
|
|
if e.code() == NodeJsErrorCode::ERR_INVALID_PACKAGE_TARGET {
|
2024-07-09 12:15:03 -04:00
|
|
|
last_error = Some(e);
|
2023-04-24 19:44:35 -04:00
|
|
|
continue;
|
2024-07-11 16:32:52 -04:00
|
|
|
} else {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(e);
|
|
|
|
}
|
2024-07-11 16:32:52 -04:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
if last_error.is_none() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
return Err(last_error.unwrap());
|
|
|
|
} else if let Some(target_obj) = target.as_object() {
|
|
|
|
for key in target_obj.keys() {
|
|
|
|
// TODO(bartlomieju): verify that keys are not numeric
|
|
|
|
// return Err(errors::err_invalid_package_config(
|
|
|
|
// to_file_path_string(package_json_url),
|
|
|
|
// Some(base.as_str().to_string()),
|
|
|
|
// Some("\"exports\" cannot contain numeric property keys.".to_string()),
|
|
|
|
// ));
|
|
|
|
|
|
|
|
if key == "default"
|
|
|
|
|| conditions.contains(&key.as_str())
|
|
|
|
|| mode.is_types() && key.as_str() == "types"
|
|
|
|
{
|
2023-12-14 10:09:05 -05:00
|
|
|
let condition_target = target_obj.get(key).unwrap();
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
let resolved = self.resolve_package_target(
|
|
|
|
package_json_path,
|
|
|
|
condition_target,
|
2023-10-05 14:49:09 -04:00
|
|
|
subpath,
|
|
|
|
package_subpath,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2022-08-30 14:09:22 -04:00
|
|
|
referrer_kind,
|
2023-04-24 19:44:35 -04:00
|
|
|
pattern,
|
|
|
|
internal,
|
2022-08-20 11:31:33 -04:00
|
|
|
conditions,
|
2022-11-30 18:07:32 -05:00
|
|
|
mode,
|
2022-08-20 11:31:33 -04:00
|
|
|
)?;
|
2023-04-24 19:44:35 -04:00
|
|
|
match resolved {
|
|
|
|
Some(resolved) => return Ok(Some(resolved)),
|
|
|
|
None => {
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
} else if target.is_null() {
|
|
|
|
return Ok(None);
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
Err(
|
|
|
|
InvalidPackageTargetError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
sub_path: package_subpath.to_string(),
|
|
|
|
target: target.to_string(),
|
|
|
|
is_import: internal,
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
)
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub fn package_exports_resolve(
|
|
|
|
&self,
|
|
|
|
package_json_path: &Path,
|
2023-10-04 23:05:12 -04:00
|
|
|
package_subpath: &str,
|
2023-04-24 19:44:35 -04:00
|
|
|
package_exports: &Map<String, Value>,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<ModuleSpecifier, PackageExportsResolveError> {
|
2023-10-04 23:05:12 -04:00
|
|
|
if package_exports.contains_key(package_subpath)
|
2023-04-24 19:44:35 -04:00
|
|
|
&& package_subpath.find('*').is_none()
|
|
|
|
&& !package_subpath.ends_with('/')
|
|
|
|
{
|
2023-12-14 10:09:05 -05:00
|
|
|
let target = package_exports.get(package_subpath).unwrap();
|
2023-04-24 19:44:35 -04:00
|
|
|
let resolved = self.resolve_package_target(
|
|
|
|
package_json_path,
|
|
|
|
target,
|
2023-10-04 23:05:12 -04:00
|
|
|
"",
|
|
|
|
package_subpath,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)?;
|
2023-12-14 10:09:05 -05:00
|
|
|
return match resolved {
|
|
|
|
Some(resolved) => Ok(resolved),
|
2024-07-09 12:15:03 -04:00
|
|
|
None => Err(
|
|
|
|
PackagePathNotExportedError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
subpath: package_subpath.to_string(),
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
mode,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
),
|
2023-12-14 10:09:05 -05:00
|
|
|
};
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut best_match = "";
|
|
|
|
let mut best_match_subpath = None;
|
|
|
|
for key in package_exports.keys() {
|
|
|
|
let pattern_index = key.find('*');
|
|
|
|
if let Some(pattern_index) = pattern_index {
|
|
|
|
let key_sub = &key[0..pattern_index];
|
|
|
|
if package_subpath.starts_with(key_sub) {
|
|
|
|
// When this reaches EOL, this can throw at the top of the whole function:
|
|
|
|
//
|
|
|
|
// if (StringPrototypeEndsWith(packageSubpath, '/'))
|
|
|
|
// throwInvalidSubpath(packageSubpath)
|
|
|
|
//
|
|
|
|
// To match "imports" and the spec.
|
|
|
|
if package_subpath.ends_with('/') {
|
|
|
|
// TODO(bartlomieju):
|
|
|
|
// emitTrailingSlashPatternDeprecation();
|
|
|
|
}
|
|
|
|
let pattern_trailer = &key[pattern_index + 1..];
|
2023-05-10 17:55:48 -04:00
|
|
|
if package_subpath.len() >= key.len()
|
2023-04-24 19:44:35 -04:00
|
|
|
&& package_subpath.ends_with(&pattern_trailer)
|
|
|
|
&& pattern_key_compare(best_match, key) == 1
|
|
|
|
&& key.rfind('*') == Some(pattern_index)
|
|
|
|
{
|
|
|
|
best_match = key;
|
|
|
|
best_match_subpath = Some(
|
|
|
|
package_subpath[pattern_index
|
|
|
|
..(package_subpath.len() - pattern_trailer.len())]
|
|
|
|
.to_string(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !best_match.is_empty() {
|
2023-12-14 10:09:05 -05:00
|
|
|
let target = package_exports.get(best_match).unwrap();
|
2023-04-24 19:44:35 -04:00
|
|
|
let maybe_resolved = self.resolve_package_target(
|
|
|
|
package_json_path,
|
|
|
|
target,
|
2023-10-04 23:05:12 -04:00
|
|
|
&best_match_subpath.unwrap(),
|
|
|
|
best_match,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)?;
|
|
|
|
if let Some(resolved) = maybe_resolved {
|
|
|
|
return Ok(resolved);
|
|
|
|
} else {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
PackagePathNotExportedError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
subpath: package_subpath.to_string(),
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
mode,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
Err(
|
|
|
|
PackagePathNotExportedError {
|
|
|
|
pkg_json_path: package_json_path.to_path_buf(),
|
|
|
|
subpath: package_subpath.to_string(),
|
|
|
|
maybe_referrer: maybe_referrer.map(ToOwned::to_owned),
|
|
|
|
mode,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
)
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn package_resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PackageResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
let (package_name, package_subpath, _is_scoped) =
|
2023-10-26 21:22:15 -04:00
|
|
|
parse_npm_pkg_name(specifier, referrer)?;
|
2023-04-24 19:44:35 -04:00
|
|
|
|
2024-06-26 17:24:10 -04:00
|
|
|
let Some(package_config) = self.get_closest_package_json(referrer)? else {
|
2023-08-27 00:04:12 -04:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-11-04 12:41:51 -04:00
|
|
|
// ResolveSelf
|
2024-06-26 17:24:10 -04:00
|
|
|
if package_config.name.as_ref() == Some(&package_name) {
|
2023-04-24 19:44:35 -04:00
|
|
|
if let Some(exports) = &package_config.exports {
|
|
|
|
return self
|
|
|
|
.package_exports_resolve(
|
|
|
|
&package_config.path,
|
2023-10-04 23:05:12 -04:00
|
|
|
&package_subpath,
|
2023-04-24 19:44:35 -04:00
|
|
|
exports,
|
2024-07-09 12:15:03 -04:00
|
|
|
Some(referrer),
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)
|
2024-07-09 12:15:03 -04:00
|
|
|
.map(Some)
|
|
|
|
.map_err(|err| err.into());
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
self
|
|
|
|
.resolve_package_subpath_for_package(
|
|
|
|
&package_name,
|
|
|
|
&package_subpath,
|
|
|
|
referrer,
|
|
|
|
referrer_kind,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)
|
|
|
|
.map_err(|err| err.into())
|
2024-06-26 17:24:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn resolve_package_subpath_for_package(
|
|
|
|
&self,
|
|
|
|
package_name: &str,
|
|
|
|
package_subpath: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PackageSubpathResolveError> {
|
2024-06-26 17:24:10 -04:00
|
|
|
let result = self.resolve_package_subpath_for_package_inner(
|
|
|
|
package_name,
|
|
|
|
package_subpath,
|
|
|
|
referrer,
|
|
|
|
referrer_kind,
|
|
|
|
conditions,
|
|
|
|
mode,
|
2024-06-08 20:05:28 -04:00
|
|
|
);
|
|
|
|
if mode.is_types() && !matches!(result, Ok(Some(_))) {
|
2024-06-26 17:24:10 -04:00
|
|
|
// try to resolve with the @types package
|
|
|
|
let package_name = types_package_name(package_name);
|
|
|
|
if let Ok(Some(result)) = self.resolve_package_subpath_for_package_inner(
|
2024-06-08 20:05:28 -04:00
|
|
|
&package_name,
|
2024-06-26 17:24:10 -04:00
|
|
|
package_subpath,
|
2024-06-08 20:05:28 -04:00
|
|
|
referrer,
|
|
|
|
referrer_kind,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
) {
|
|
|
|
return Ok(Some(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
2024-06-26 17:24:10 -04:00
|
|
|
fn resolve_package_subpath_for_package_inner(
|
2024-06-08 20:05:28 -04:00
|
|
|
&self,
|
|
|
|
package_name: &str,
|
|
|
|
package_subpath: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PackageSubpathResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
let package_dir_path = self
|
|
|
|
.npm_resolver
|
2024-06-08 20:05:28 -04:00
|
|
|
.resolve_package_folder_from_package(package_name, referrer)?;
|
2023-04-24 19:44:35 -04:00
|
|
|
|
|
|
|
// todo: error with this instead when can't find package
|
|
|
|
// Err(errors::err_module_not_found(
|
|
|
|
// &package_json_url
|
|
|
|
// .join(".")
|
|
|
|
// .unwrap()
|
|
|
|
// .to_file_path()
|
|
|
|
// .unwrap()
|
|
|
|
// .display()
|
|
|
|
// .to_string(),
|
|
|
|
// &to_file_path_string(referrer),
|
|
|
|
// "package",
|
|
|
|
// ))
|
|
|
|
|
|
|
|
// Package match.
|
2024-06-26 17:24:10 -04:00
|
|
|
self.resolve_package_dir_subpath(
|
|
|
|
&package_dir_path,
|
2024-06-08 20:05:28 -04:00
|
|
|
package_subpath,
|
2024-07-09 12:15:03 -04:00
|
|
|
Some(referrer),
|
2023-10-04 23:05:12 -04:00
|
|
|
referrer_kind,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-26 17:24:10 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn resolve_package_dir_subpath(
|
|
|
|
&self,
|
|
|
|
package_dir_path: &Path,
|
|
|
|
package_subpath: &str,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2024-06-26 17:24:10 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PackageSubpathResolveError> {
|
2024-06-26 17:24:10 -04:00
|
|
|
let package_json_path = package_dir_path.join("package.json");
|
|
|
|
match self.load_package_json(&package_json_path)? {
|
|
|
|
Some(pkg_json) => self.resolve_package_subpath(
|
|
|
|
&pkg_json,
|
|
|
|
package_subpath,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer,
|
2024-06-26 17:24:10 -04:00
|
|
|
referrer_kind,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
),
|
2024-07-09 12:15:03 -04:00
|
|
|
None => self
|
|
|
|
.resolve_package_subpath_no_pkg_json(
|
|
|
|
package_dir_path,
|
|
|
|
package_subpath,
|
|
|
|
maybe_referrer,
|
|
|
|
referrer_kind,
|
|
|
|
mode,
|
|
|
|
)
|
|
|
|
.map_err(|err| PackageSubpathResolveErrorKind::LegacyExact(err).into()),
|
2024-06-26 17:24:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 23:05:12 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn resolve_package_subpath(
|
|
|
|
&self,
|
|
|
|
package_json: &PackageJson,
|
|
|
|
package_subpath: &str,
|
2024-07-09 12:15:03 -04:00
|
|
|
referrer: Option<&ModuleSpecifier>,
|
2023-10-04 23:05:12 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
conditions: &[&str],
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PackageSubpathResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
if let Some(exports) = &package_json.exports {
|
2023-10-04 23:05:12 -04:00
|
|
|
let result = self.package_exports_resolve(
|
|
|
|
&package_json.path,
|
|
|
|
package_subpath,
|
|
|
|
exports,
|
|
|
|
referrer,
|
|
|
|
referrer_kind,
|
|
|
|
conditions,
|
|
|
|
mode,
|
|
|
|
);
|
|
|
|
match result {
|
|
|
|
Ok(found) => return Ok(Some(found)),
|
|
|
|
Err(exports_err) => {
|
|
|
|
if mode.is_types() && package_subpath == "." {
|
2024-07-09 12:15:03 -04:00
|
|
|
return self
|
|
|
|
.legacy_main_resolve(package_json, referrer, referrer_kind, mode)
|
|
|
|
.map_err(|err| {
|
|
|
|
PackageSubpathResolveErrorKind::LegacyMain(err).into()
|
|
|
|
});
|
2023-10-04 23:05:12 -04:00
|
|
|
}
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(
|
|
|
|
PackageSubpathResolveErrorKind::Exports(exports_err).into(),
|
|
|
|
);
|
2023-10-04 23:05:12 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2024-06-26 17:24:10 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if package_subpath == "." {
|
2024-07-09 12:15:03 -04:00
|
|
|
return self
|
|
|
|
.legacy_main_resolve(package_json, referrer, referrer_kind, mode)
|
|
|
|
.map_err(|err| PackageSubpathResolveErrorKind::LegacyMain(err).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
.resolve_subpath_exact(
|
|
|
|
package_json.path.parent().unwrap(),
|
|
|
|
package_subpath,
|
2024-03-13 22:37:56 -04:00
|
|
|
referrer,
|
|
|
|
referrer_kind,
|
|
|
|
mode,
|
2024-07-09 12:15:03 -04:00
|
|
|
)
|
|
|
|
.map_err(|err| PackageSubpathResolveErrorKind::LegacyExact(err).into())
|
2024-06-26 17:24:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_subpath_exact(
|
|
|
|
&self,
|
|
|
|
directory: &Path,
|
|
|
|
package_subpath: &str,
|
2024-07-09 12:15:03 -04:00
|
|
|
referrer: Option<&ModuleSpecifier>,
|
2024-06-26 17:24:10 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PathToDeclarationUrlError> {
|
2024-06-26 17:24:10 -04:00
|
|
|
assert_ne!(package_subpath, ".");
|
|
|
|
let file_path = directory.join(package_subpath);
|
2023-04-24 19:44:35 -04:00
|
|
|
if mode.is_types() {
|
2024-07-16 18:32:41 -04:00
|
|
|
Ok(self.path_to_declaration_url(&file_path, referrer, referrer_kind)?)
|
2023-04-24 19:44:35 -04:00
|
|
|
} else {
|
2023-12-14 10:09:05 -05:00
|
|
|
Ok(Some(to_file_specifier(&file_path)))
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 17:24:10 -04:00
|
|
|
fn resolve_package_subpath_no_pkg_json(
|
|
|
|
&self,
|
|
|
|
directory: &Path,
|
|
|
|
package_subpath: &str,
|
2024-07-09 12:15:03 -04:00
|
|
|
referrer: Option<&ModuleSpecifier>,
|
2024-06-26 17:24:10 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, PathToDeclarationUrlError> {
|
2024-06-26 17:24:10 -04:00
|
|
|
if package_subpath == "." {
|
2024-07-09 12:15:03 -04:00
|
|
|
Ok(self.legacy_index_resolve(directory, referrer_kind, mode))
|
2024-06-26 17:24:10 -04:00
|
|
|
} else {
|
|
|
|
self.resolve_subpath_exact(
|
|
|
|
directory,
|
|
|
|
package_subpath,
|
|
|
|
referrer,
|
|
|
|
referrer_kind,
|
|
|
|
mode,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-04 12:41:51 -04:00
|
|
|
pub fn get_closest_package_json(
|
2023-04-24 19:44:35 -04:00
|
|
|
&self,
|
|
|
|
url: &ModuleSpecifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<PackageJsonRc>, ClosestPkgJsonError> {
|
2024-03-09 10:21:31 -05:00
|
|
|
let Ok(file_path) = url.to_file_path() else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
2024-06-26 17:24:10 -04:00
|
|
|
self.get_closest_package_json_from_path(&file_path)
|
2024-03-09 10:21:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_closest_package_json_from_path(
|
|
|
|
&self,
|
|
|
|
file_path: &Path,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<PackageJsonRc>, ClosestPkgJsonError> {
|
|
|
|
let parent_dir = file_path.parent().unwrap();
|
|
|
|
let current_dir =
|
|
|
|
deno_core::strip_unc_prefix(self.fs.realpath_sync(parent_dir).map_err(
|
2024-07-16 18:32:41 -04:00
|
|
|
|source| CanonicalizingPkgJsonDirError {
|
2024-07-09 12:15:03 -04:00
|
|
|
dir_path: parent_dir.to_path_buf(),
|
|
|
|
source: source.into_io_error(),
|
|
|
|
},
|
|
|
|
)?);
|
2024-07-10 14:46:25 -04:00
|
|
|
for current_dir in current_dir.ancestors() {
|
2023-04-24 19:44:35 -04:00
|
|
|
let package_json_path = current_dir.join("package.json");
|
2024-06-26 17:24:10 -04:00
|
|
|
if let Some(pkg_json) = self.load_package_json(&package_json_path)? {
|
|
|
|
return Ok(Some(pkg_json));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 14:00:44 -04:00
|
|
|
Ok(None)
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn load_package_json(
|
|
|
|
&self,
|
2024-06-26 17:24:10 -04:00
|
|
|
package_json_path: &Path,
|
2024-07-16 18:32:41 -04:00
|
|
|
) -> Result<Option<PackageJsonRc>, PackageJsonLoadError> {
|
2024-06-26 17:24:10 -04:00
|
|
|
crate::package_json::load_pkg_json(&*self.fs, package_json_path)
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn legacy_main_resolve(
|
|
|
|
&self,
|
|
|
|
package_json: &PackageJson,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-04-24 19:44:35 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, LegacyMainResolveError> {
|
2023-04-24 19:44:35 -04:00
|
|
|
let maybe_main = if mode.is_types() {
|
|
|
|
match package_json.types.as_ref() {
|
2024-04-01 02:07:11 -04:00
|
|
|
Some(types) => Some(types.as_str()),
|
2023-04-24 19:44:35 -04:00
|
|
|
None => {
|
|
|
|
// fallback to checking the main entrypoint for
|
|
|
|
// a corresponding declaration file
|
|
|
|
if let Some(main) = package_json.main(referrer_kind) {
|
|
|
|
let main = package_json.path.parent().unwrap().join(main).clean();
|
2024-07-09 12:15:03 -04:00
|
|
|
let maybe_decl_url = self
|
2024-07-16 18:32:41 -04:00
|
|
|
.path_to_declaration_url(&main, maybe_referrer, referrer_kind)
|
2024-07-09 12:15:03 -04:00
|
|
|
.map_err(LegacyMainResolveError::PathToDeclarationUrl)?;
|
2024-03-13 22:37:56 -04:00
|
|
|
if let Some(path) = maybe_decl_url {
|
2023-04-24 19:44:35 -04:00
|
|
|
return Ok(Some(path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
package_json.main(referrer_kind)
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(main) = maybe_main {
|
|
|
|
let guess = package_json.path.parent().unwrap().join(main).clean();
|
2023-08-08 16:28:18 -04:00
|
|
|
if self.fs.is_file_sync(&guess) {
|
2024-03-13 22:37:56 -04:00
|
|
|
return Ok(Some(to_file_specifier(&guess)));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo(dsherret): investigate exactly how node and typescript handles this
|
|
|
|
let endings = if mode.is_types() {
|
|
|
|
match referrer_kind {
|
|
|
|
NodeModuleKind::Cjs => {
|
|
|
|
vec![".d.ts", ".d.cts", "/index.d.ts", "/index.d.cts"]
|
|
|
|
}
|
|
|
|
NodeModuleKind::Esm => vec![
|
|
|
|
".d.ts",
|
|
|
|
".d.mts",
|
|
|
|
"/index.d.ts",
|
|
|
|
"/index.d.mts",
|
|
|
|
".d.cts",
|
|
|
|
"/index.d.cts",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vec![".js", "/index.js"]
|
|
|
|
};
|
|
|
|
for ending in endings {
|
|
|
|
let guess = package_json
|
|
|
|
.path
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join(format!("{main}{ending}"))
|
|
|
|
.clean();
|
2023-08-08 16:28:18 -04:00
|
|
|
if self.fs.is_file_sync(&guess) {
|
2023-04-24 19:44:35 -04:00
|
|
|
// TODO(bartlomieju): emitLegacyIndexDeprecation()
|
2024-03-13 22:37:56 -04:00
|
|
|
return Ok(Some(to_file_specifier(&guess)));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
Ok(self.legacy_index_resolve(
|
2024-06-26 17:24:10 -04:00
|
|
|
package_json.path.parent().unwrap(),
|
|
|
|
referrer_kind,
|
|
|
|
mode,
|
2024-07-09 12:15:03 -04:00
|
|
|
))
|
2024-06-26 17:24:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn legacy_index_resolve(
|
|
|
|
&self,
|
|
|
|
directory: &Path,
|
|
|
|
referrer_kind: NodeModuleKind,
|
|
|
|
mode: NodeResolutionMode,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Option<ModuleSpecifier> {
|
2023-04-24 19:44:35 -04:00
|
|
|
let index_file_names = if mode.is_types() {
|
|
|
|
// todo(dsherret): investigate exactly how typescript does this
|
|
|
|
match referrer_kind {
|
|
|
|
NodeModuleKind::Cjs => vec!["index.d.ts", "index.d.cts"],
|
|
|
|
NodeModuleKind::Esm => vec!["index.d.ts", "index.d.mts", "index.d.cts"],
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vec!["index.js"]
|
|
|
|
};
|
|
|
|
for index_file_name in index_file_names {
|
2024-06-26 17:24:10 -04:00
|
|
|
let guess = directory.join(index_file_name).clean();
|
2023-08-08 16:28:18 -04:00
|
|
|
if self.fs.is_file_sync(&guess) {
|
2023-04-24 19:44:35 -04:00
|
|
|
// TODO(bartlomieju): emitLegacyIndexDeprecation()
|
2024-07-09 12:15:03 -04:00
|
|
|
return Some(to_file_specifier(&guess));
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-09 12:15:03 -04:00
|
|
|
None
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_bin_entry_value<'a>(
|
2023-09-28 16:43:45 -04:00
|
|
|
package_json: &'a PackageJson,
|
2023-04-24 19:44:35 -04:00
|
|
|
bin_name: Option<&str>,
|
|
|
|
) -> Result<&'a str, AnyError> {
|
2023-09-28 16:43:45 -04:00
|
|
|
let bin = match &package_json.bin {
|
|
|
|
Some(bin) => bin,
|
|
|
|
None => bail!(
|
|
|
|
"'{}' did not have a bin property",
|
|
|
|
package_json.path.display(),
|
|
|
|
),
|
|
|
|
};
|
2023-04-24 19:44:35 -04:00
|
|
|
let bin_entry = match bin {
|
|
|
|
Value::String(_) => {
|
2024-07-09 23:06:08 -04:00
|
|
|
if bin_name.is_some()
|
|
|
|
&& bin_name
|
|
|
|
!= package_json
|
|
|
|
.name
|
|
|
|
.as_deref()
|
|
|
|
.map(|name| name.rsplit_once('/').map_or(name, |(_, name)| name))
|
|
|
|
{
|
2023-04-24 19:44:35 -04:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(bin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Object(o) => {
|
|
|
|
if let Some(bin_name) = bin_name {
|
|
|
|
o.get(bin_name)
|
2023-09-28 16:43:45 -04:00
|
|
|
} else if o.len() == 1
|
|
|
|
|| o.len() > 1 && o.values().all(|v| v == o.values().next().unwrap())
|
|
|
|
{
|
2023-04-24 19:44:35 -04:00
|
|
|
o.values().next()
|
|
|
|
} else {
|
2023-09-28 16:43:45 -04:00
|
|
|
package_json.name.as_ref().and_then(|n| o.get(n))
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2023-09-28 16:43:45 -04:00
|
|
|
}
|
|
|
|
_ => bail!(
|
|
|
|
"'{}' did not have a bin property with a string or object value",
|
|
|
|
package_json.path.display()
|
|
|
|
),
|
2023-04-24 19:44:35 -04:00
|
|
|
};
|
|
|
|
let bin_entry = match bin_entry {
|
|
|
|
Some(e) => e,
|
|
|
|
None => {
|
2023-09-28 16:43:45 -04:00
|
|
|
let prefix = package_json
|
|
|
|
.name
|
|
|
|
.as_ref()
|
|
|
|
.map(|n| {
|
|
|
|
let mut prefix = format!("npm:{}", n);
|
|
|
|
if let Some(version) = &package_json.version {
|
|
|
|
prefix.push('@');
|
|
|
|
prefix.push_str(version);
|
|
|
|
}
|
|
|
|
prefix.push('/');
|
|
|
|
prefix
|
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2023-04-24 19:44:35 -04:00
|
|
|
let keys = bin
|
|
|
|
.as_object()
|
|
|
|
.map(|o| {
|
|
|
|
o.keys()
|
2023-09-28 16:43:45 -04:00
|
|
|
.map(|k| format!(" * {prefix}{k}"))
|
2023-04-24 19:44:35 -04:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
|
|
|
bail!(
|
2023-09-28 16:43:45 -04:00
|
|
|
"'{}' did not have a bin entry{}{}",
|
|
|
|
package_json.path.display(),
|
|
|
|
bin_name
|
|
|
|
.or(package_json.name.as_deref())
|
|
|
|
.map(|name| format!(" for '{}'", name))
|
|
|
|
.unwrap_or_default(),
|
2023-04-24 19:44:35 -04:00
|
|
|
if keys.is_empty() {
|
|
|
|
"".to_string()
|
|
|
|
} else {
|
|
|
|
format!("\n\nPossibilities:\n{}", keys.join("\n"))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match bin_entry {
|
|
|
|
Value::String(s) => Ok(s),
|
|
|
|
_ => bail!(
|
2023-09-28 16:43:45 -04:00
|
|
|
"'{}' had a non-string sub property of bin",
|
|
|
|
package_json.path.display(),
|
2023-04-24 19:44:35 -04:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_file_path(url: &ModuleSpecifier) -> PathBuf {
|
|
|
|
url
|
|
|
|
.to_file_path()
|
|
|
|
.unwrap_or_else(|_| panic!("Provided URL was not file:// URL: {url}"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_file_path_string(url: &ModuleSpecifier) -> String {
|
|
|
|
to_file_path(url).display().to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_be_treated_as_relative_or_absolute_path(specifier: &str) -> bool {
|
|
|
|
if specifier.is_empty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if specifier.starts_with('/') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_relative_specifier(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ry) We very likely have this utility function elsewhere in Deno.
|
|
|
|
fn is_relative_specifier(specifier: &str) -> bool {
|
|
|
|
let specifier_len = specifier.len();
|
|
|
|
let specifier_chars: Vec<_> = specifier.chars().collect();
|
|
|
|
|
|
|
|
if !specifier_chars.is_empty() && specifier_chars[0] == '.' {
|
|
|
|
if specifier_len == 1 || specifier_chars[1] == '/' {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if specifier_chars[1] == '.'
|
|
|
|
&& (specifier_len == 2 || specifier_chars[2] == '/')
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Alternate `PathBuf::with_extension` that will handle known extensions
|
|
|
|
/// more intelligently.
|
|
|
|
fn with_known_extension(path: &Path, ext: &str) -> PathBuf {
|
2023-10-16 11:26:38 -04:00
|
|
|
const NON_DECL_EXTS: &[&str] = &[
|
|
|
|
"cjs", "js", "json", "jsx", "mjs", "tsx", /* ex. types.d */ "d",
|
|
|
|
];
|
2023-04-24 19:44:35 -04:00
|
|
|
const DECL_EXTS: &[&str] = &["cts", "mts", "ts"];
|
|
|
|
|
|
|
|
let file_name = match path.file_name() {
|
|
|
|
Some(value) => value.to_string_lossy(),
|
|
|
|
None => return path.to_path_buf(),
|
|
|
|
};
|
|
|
|
let lowercase_file_name = file_name.to_lowercase();
|
|
|
|
let period_index = lowercase_file_name.rfind('.').and_then(|period_index| {
|
|
|
|
let ext = &lowercase_file_name[period_index + 1..];
|
|
|
|
if DECL_EXTS.contains(&ext) {
|
|
|
|
if let Some(next_period_index) =
|
|
|
|
lowercase_file_name[..period_index].rfind('.')
|
|
|
|
{
|
|
|
|
if &lowercase_file_name[next_period_index + 1..period_index] == "d" {
|
|
|
|
Some(next_period_index)
|
|
|
|
} else {
|
|
|
|
Some(period_index)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some(period_index)
|
|
|
|
}
|
|
|
|
} else if NON_DECL_EXTS.contains(&ext) {
|
|
|
|
Some(period_index)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let file_name = match period_index {
|
|
|
|
Some(period_index) => &file_name[..period_index],
|
|
|
|
None => &file_name,
|
|
|
|
};
|
|
|
|
path.with_file_name(format!("{file_name}.{ext}"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_specifier_display_string(url: &ModuleSpecifier) -> String {
|
|
|
|
if let Ok(path) = url.to_file_path() {
|
|
|
|
path.display().to_string()
|
|
|
|
} else {
|
|
|
|
url.to_string()
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2023-04-24 19:44:35 -04:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
|
|
|
fn throw_invalid_subpath(
|
|
|
|
subpath: String,
|
2022-08-30 14:09:22 -04:00
|
|
|
package_json_path: &Path,
|
2022-08-20 11:31:33 -04:00
|
|
|
internal: bool,
|
2024-07-09 12:15:03 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> InvalidModuleSpecifierError {
|
2022-08-20 11:31:33 -04:00
|
|
|
let ie = if internal { "imports" } else { "exports" };
|
|
|
|
let reason = format!(
|
|
|
|
"request is not a valid subpath for the \"{}\" resolution of {}",
|
|
|
|
ie,
|
2022-08-30 14:09:22 -04:00
|
|
|
package_json_path.display(),
|
2022-08-20 11:31:33 -04:00
|
|
|
);
|
2024-07-09 12:15:03 -04:00
|
|
|
InvalidModuleSpecifierError {
|
|
|
|
request: subpath,
|
|
|
|
reason: Cow::Owned(reason),
|
|
|
|
maybe_referrer: maybe_referrer.map(to_specifier_display_string),
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-10-26 21:22:15 -04:00
|
|
|
pub fn parse_npm_pkg_name(
|
2022-08-20 11:31:33 -04:00
|
|
|
specifier: &str,
|
2022-08-30 14:09:22 -04:00
|
|
|
referrer: &ModuleSpecifier,
|
2024-07-09 12:15:03 -04:00
|
|
|
) -> Result<(String, String, bool), InvalidModuleSpecifierError> {
|
2022-08-20 11:31:33 -04:00
|
|
|
let mut separator_index = specifier.find('/');
|
|
|
|
let mut valid_package_name = true;
|
|
|
|
let mut is_scoped = false;
|
|
|
|
if specifier.is_empty() {
|
|
|
|
valid_package_name = false;
|
|
|
|
} else if specifier.starts_with('@') {
|
|
|
|
is_scoped = true;
|
|
|
|
if let Some(index) = separator_index {
|
2022-09-06 06:56:34 -04:00
|
|
|
separator_index = specifier[index + 1..]
|
|
|
|
.find('/')
|
|
|
|
.map(|new_index| index + 1 + new_index);
|
2022-08-20 11:31:33 -04:00
|
|
|
} else {
|
|
|
|
valid_package_name = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let package_name = if let Some(index) = separator_index {
|
|
|
|
specifier[0..index].to_string()
|
|
|
|
} else {
|
|
|
|
specifier.to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Package name cannot have leading . and cannot have percent-encoding or separators.
|
|
|
|
for ch in package_name.chars() {
|
|
|
|
if ch == '%' || ch == '\\' {
|
|
|
|
valid_package_name = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid_package_name {
|
2024-07-09 12:15:03 -04:00
|
|
|
return Err(errors::InvalidModuleSpecifierError {
|
|
|
|
request: specifier.to_string(),
|
|
|
|
reason: Cow::Borrowed("is not a valid package name"),
|
|
|
|
maybe_referrer: Some(to_specifier_display_string(referrer)),
|
|
|
|
});
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let package_subpath = if let Some(index) = separator_index {
|
|
|
|
format!(".{}", specifier.chars().skip(index).collect::<String>())
|
|
|
|
} else {
|
|
|
|
".".to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((package_name, package_subpath, is_scoped))
|
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
fn pattern_key_compare(a: &str, b: &str) -> i32 {
|
|
|
|
let a_pattern_index = a.find('*');
|
|
|
|
let b_pattern_index = b.find('*');
|
2022-11-16 14:41:27 -05:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
let base_len_a = if let Some(index) = a_pattern_index {
|
|
|
|
index + 1
|
2022-11-16 14:41:27 -05:00
|
|
|
} else {
|
2023-04-24 19:44:35 -04:00
|
|
|
a.len()
|
|
|
|
};
|
|
|
|
let base_len_b = if let Some(index) = b_pattern_index {
|
|
|
|
index + 1
|
|
|
|
} else {
|
|
|
|
b.len()
|
|
|
|
};
|
2022-08-30 14:09:22 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if base_len_a > base_len_b {
|
|
|
|
return -1;
|
2022-08-30 14:09:22 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if base_len_b > base_len_a {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-08-30 14:09:22 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if a_pattern_index.is_none() {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if b_pattern_index.is_none() {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if a.len() > b.len() {
|
|
|
|
return -1;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
if b.len() > a.len() {
|
|
|
|
return 1;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
0
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2022-09-06 06:56:34 -04:00
|
|
|
|
2024-06-08 20:05:28 -04:00
|
|
|
/// Gets the corresponding @types package for the provided package name.
|
|
|
|
fn types_package_name(package_name: &str) -> String {
|
|
|
|
debug_assert!(!package_name.starts_with("@types/"));
|
|
|
|
// Scoped packages will get two underscores for each slash
|
|
|
|
// https://github.com/DefinitelyTyped/DefinitelyTyped/tree/15f1ece08f7b498f4b9a2147c2a46e94416ca777#what-about-scoped-packages
|
|
|
|
format!("@types/{}", package_name.replace('/', "__"))
|
|
|
|
}
|
|
|
|
|
2022-09-06 06:56:34 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-04-24 19:44:35 -04:00
|
|
|
use deno_core::serde_json::json;
|
|
|
|
|
2022-09-06 06:56:34 -04:00
|
|
|
use super::*;
|
|
|
|
|
2023-09-28 16:43:45 -04:00
|
|
|
fn build_package_json(json: Value) -> PackageJson {
|
2024-06-26 17:24:10 -04:00
|
|
|
PackageJson::load_from_value(PathBuf::from("/package.json"), json)
|
2023-09-28 16:43:45 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:44:35 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_bin_entry_value() {
|
|
|
|
// should resolve the specified value
|
2023-09-28 16:43:45 -04:00
|
|
|
let pkg_json = build_package_json(json!({
|
|
|
|
"name": "pkg",
|
|
|
|
"version": "1.1.1",
|
|
|
|
"bin": {
|
|
|
|
"bin1": "./value1",
|
|
|
|
"bin2": "./value2",
|
|
|
|
"pkg": "./value3",
|
|
|
|
}
|
|
|
|
}));
|
2023-04-24 19:44:35 -04:00
|
|
|
assert_eq!(
|
2023-09-28 16:43:45 -04:00
|
|
|
resolve_bin_entry_value(&pkg_json, Some("bin1")).unwrap(),
|
2023-04-24 19:44:35 -04:00
|
|
|
"./value1"
|
|
|
|
);
|
|
|
|
|
|
|
|
// should resolve the value with the same name when not specified
|
|
|
|
assert_eq!(
|
2023-09-28 16:43:45 -04:00
|
|
|
resolve_bin_entry_value(&pkg_json, None).unwrap(),
|
2023-04-24 19:44:35 -04:00
|
|
|
"./value3"
|
|
|
|
);
|
|
|
|
|
|
|
|
// should not resolve when specified value does not exist
|
|
|
|
assert_eq!(
|
2023-09-28 16:43:45 -04:00
|
|
|
resolve_bin_entry_value(&pkg_json, Some("other"),)
|
|
|
|
.err()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
2023-04-24 19:44:35 -04:00
|
|
|
concat!(
|
2023-09-28 16:43:45 -04:00
|
|
|
"'/package.json' did not have a bin entry for 'other'\n",
|
2023-04-24 19:44:35 -04:00
|
|
|
"\n",
|
|
|
|
"Possibilities:\n",
|
2023-09-28 16:43:45 -04:00
|
|
|
" * npm:pkg@1.1.1/bin1\n",
|
|
|
|
" * npm:pkg@1.1.1/bin2\n",
|
|
|
|
" * npm:pkg@1.1.1/pkg"
|
2023-04-24 19:44:35 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// should not resolve when default value can't be determined
|
2023-09-28 16:43:45 -04:00
|
|
|
let pkg_json = build_package_json(json!({
|
|
|
|
"name": "pkg",
|
|
|
|
"version": "1.1.1",
|
|
|
|
"bin": {
|
|
|
|
"bin": "./value1",
|
|
|
|
"bin2": "./value2",
|
|
|
|
}
|
|
|
|
}));
|
2023-04-24 19:44:35 -04:00
|
|
|
assert_eq!(
|
2023-09-28 16:43:45 -04:00
|
|
|
resolve_bin_entry_value(&pkg_json, None)
|
|
|
|
.err()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
2023-04-24 19:44:35 -04:00
|
|
|
concat!(
|
2023-09-28 16:43:45 -04:00
|
|
|
"'/package.json' did not have a bin entry for 'pkg'\n",
|
2023-04-24 19:44:35 -04:00
|
|
|
"\n",
|
|
|
|
"Possibilities:\n",
|
2023-09-28 16:43:45 -04:00
|
|
|
" * npm:pkg@1.1.1/bin\n",
|
|
|
|
" * npm:pkg@1.1.1/bin2",
|
2023-04-24 19:44:35 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// should resolve since all the values are the same
|
2023-09-28 16:43:45 -04:00
|
|
|
let pkg_json = build_package_json(json!({
|
|
|
|
"name": "pkg",
|
|
|
|
"version": "1.2.3",
|
|
|
|
"bin": {
|
|
|
|
"bin1": "./value",
|
|
|
|
"bin2": "./value",
|
|
|
|
}
|
|
|
|
}));
|
2023-04-24 19:44:35 -04:00
|
|
|
assert_eq!(
|
2023-09-28 16:43:45 -04:00
|
|
|
resolve_bin_entry_value(&pkg_json, None,).unwrap(),
|
2023-04-24 19:44:35 -04:00
|
|
|
"./value"
|
|
|
|
);
|
|
|
|
|
|
|
|
// should not resolve when specified and is a string
|
2023-09-28 16:43:45 -04:00
|
|
|
let pkg_json = build_package_json(json!({
|
|
|
|
"name": "pkg",
|
|
|
|
"version": "1.2.3",
|
|
|
|
"bin": "./value",
|
|
|
|
}));
|
|
|
|
assert_eq!(
|
|
|
|
resolve_bin_entry_value(&pkg_json, Some("path"),)
|
|
|
|
.err()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
|
|
|
"'/package.json' did not have a bin entry for 'path'"
|
|
|
|
);
|
|
|
|
|
|
|
|
// no version in the package.json
|
|
|
|
let pkg_json = build_package_json(json!({
|
|
|
|
"name": "pkg",
|
|
|
|
"bin": {
|
|
|
|
"bin1": "./value1",
|
|
|
|
"bin2": "./value2",
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
assert_eq!(
|
|
|
|
resolve_bin_entry_value(&pkg_json, None)
|
|
|
|
.err()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
|
|
|
concat!(
|
|
|
|
"'/package.json' did not have a bin entry for 'pkg'\n",
|
|
|
|
"\n",
|
|
|
|
"Possibilities:\n",
|
|
|
|
" * npm:pkg/bin1\n",
|
|
|
|
" * npm:pkg/bin2",
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// no name or version in the package.json
|
|
|
|
let pkg_json = build_package_json(json!({
|
|
|
|
"bin": {
|
|
|
|
"bin1": "./value1",
|
|
|
|
"bin2": "./value2",
|
|
|
|
}
|
|
|
|
}));
|
2023-04-24 19:44:35 -04:00
|
|
|
assert_eq!(
|
2023-09-28 16:43:45 -04:00
|
|
|
resolve_bin_entry_value(&pkg_json, None)
|
|
|
|
.err()
|
|
|
|
.unwrap()
|
|
|
|
.to_string(),
|
|
|
|
concat!(
|
|
|
|
"'/package.json' did not have a bin entry\n",
|
|
|
|
"\n",
|
|
|
|
"Possibilities:\n",
|
|
|
|
" * bin1\n",
|
|
|
|
" * bin2",
|
2023-04-24 19:44:35 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-06 06:56:34 -04:00
|
|
|
#[test]
|
|
|
|
fn test_parse_package_name() {
|
|
|
|
let dummy_referrer = Url::parse("http://example.com").unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
2023-10-26 21:22:15 -04:00
|
|
|
parse_npm_pkg_name("fetch-blob", &dummy_referrer).unwrap(),
|
2022-09-06 06:56:34 -04:00
|
|
|
("fetch-blob".to_string(), ".".to_string(), false)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-26 21:22:15 -04:00
|
|
|
parse_npm_pkg_name("@vue/plugin-vue", &dummy_referrer).unwrap(),
|
2022-09-06 06:56:34 -04:00
|
|
|
("@vue/plugin-vue".to_string(), ".".to_string(), true)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-26 21:22:15 -04:00
|
|
|
parse_npm_pkg_name("@astrojs/prism/dist/highlighter", &dummy_referrer)
|
2022-09-06 06:56:34 -04:00
|
|
|
.unwrap(),
|
|
|
|
(
|
|
|
|
"@astrojs/prism".to_string(),
|
|
|
|
"./dist/highlighter".to_string(),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-11-19 10:40:01 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_known_extension() {
|
|
|
|
let cases = &[
|
|
|
|
("test", "d.ts", "test.d.ts"),
|
|
|
|
("test.d.ts", "ts", "test.ts"),
|
|
|
|
("test.worker", "d.ts", "test.worker.d.ts"),
|
|
|
|
("test.d.mts", "js", "test.js"),
|
|
|
|
];
|
|
|
|
for (path, ext, expected) in cases {
|
|
|
|
let actual = with_known_extension(&PathBuf::from(path), ext);
|
|
|
|
assert_eq!(actual.to_string_lossy(), *expected);
|
|
|
|
}
|
|
|
|
}
|
2024-06-08 20:05:28 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_types_package_name() {
|
|
|
|
assert_eq!(types_package_name("name"), "@types/name");
|
|
|
|
assert_eq!(
|
|
|
|
types_package_name("@scoped/package"),
|
|
|
|
"@types/@scoped__package"
|
|
|
|
);
|
|
|
|
}
|
2022-09-06 06:56:34 -04:00
|
|
|
}
|