2024-04-26 16:39:33 -04:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2024-05-23 17:26:23 -04:00
|
|
|
use crate::args::create_default_npmrc;
|
2024-04-26 16:39:33 -04:00
|
|
|
use crate::args::package_json;
|
|
|
|
use crate::args::CacheSetting;
|
2024-05-06 22:20:15 -04:00
|
|
|
use crate::graph_util::CliJsrUrlProvider;
|
2024-06-03 17:17:08 -04:00
|
|
|
use crate::http_util::HttpClientProvider;
|
2024-05-06 22:20:15 -04:00
|
|
|
use crate::jsr::JsrCacheResolver;
|
2024-04-26 16:39:33 -04:00
|
|
|
use crate::lsp::config::Config;
|
|
|
|
use crate::lsp::config::ConfigData;
|
|
|
|
use crate::npm::create_cli_npm_resolver_for_lsp;
|
|
|
|
use crate::npm::CliNpmResolver;
|
|
|
|
use crate::npm::CliNpmResolverByonmCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedPackageJsonInstallerOption;
|
|
|
|
use crate::npm::CliNpmResolverManagedSnapshotOption;
|
|
|
|
use crate::npm::ManagedCliNpmResolver;
|
|
|
|
use crate::resolver::CliGraphResolver;
|
|
|
|
use crate::resolver::CliGraphResolverOptions;
|
|
|
|
use crate::resolver::CliNodeResolver;
|
2024-05-09 10:17:31 -04:00
|
|
|
use crate::resolver::SloppyImportsResolver;
|
2024-04-26 16:39:33 -04:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
|
|
|
use crate::util::progress_bar::ProgressBarStyle;
|
2024-05-09 15:22:27 -04:00
|
|
|
use dashmap::DashMap;
|
2024-05-13 12:55:31 -04:00
|
|
|
use deno_ast::MediaType;
|
2024-05-06 22:20:15 -04:00
|
|
|
use deno_cache_dir::HttpCache;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_core::error::AnyError;
|
2024-05-09 15:22:27 -04:00
|
|
|
use deno_core::url::Url;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_graph::source::NpmResolver;
|
|
|
|
use deno_graph::source::Resolver;
|
2024-05-06 22:20:15 -04:00
|
|
|
use deno_graph::GraphImport;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_graph::ModuleSpecifier;
|
|
|
|
use deno_npm::NpmSystemInfo;
|
|
|
|
use deno_runtime::deno_fs;
|
|
|
|
use deno_runtime::deno_node::NodeResolution;
|
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
|
|
|
use deno_runtime::deno_node::PackageJson;
|
|
|
|
use deno_runtime::fs_util::specifier_to_file_path;
|
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2024-05-06 22:20:15 -04:00
|
|
|
use deno_semver::jsr::JsrPackageReqReference;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2024-05-06 22:20:15 -04:00
|
|
|
use deno_semver::package::PackageNv;
|
2024-04-26 16:39:33 -04:00
|
|
|
use deno_semver::package::PackageReq;
|
2024-05-06 22:20:15 -04:00
|
|
|
use indexmap::IndexMap;
|
2024-04-26 16:39:33 -04:00
|
|
|
use package_json::PackageJsonDepsProvider;
|
2024-05-09 15:22:27 -04:00
|
|
|
use std::borrow::Cow;
|
2024-05-06 22:20:15 -04:00
|
|
|
use std::collections::HashMap;
|
2024-05-09 15:22:27 -04:00
|
|
|
use std::collections::HashSet;
|
2024-04-26 16:39:33 -04:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
use super::cache::LspCache;
|
|
|
|
|
2024-04-26 16:39:33 -04:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct LspResolver {
|
|
|
|
graph_resolver: Arc<CliGraphResolver>,
|
2024-05-06 22:20:15 -04:00
|
|
|
jsr_resolver: Option<Arc<JsrCacheResolver>>,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver: Option<Arc<dyn CliNpmResolver>>,
|
|
|
|
node_resolver: Option<Arc<CliNodeResolver>>,
|
2024-05-06 22:20:15 -04:00
|
|
|
redirect_resolver: Option<Arc<RedirectResolver>>,
|
|
|
|
graph_imports: Arc<IndexMap<ModuleSpecifier, GraphImport>>,
|
2024-04-26 16:39:33 -04:00
|
|
|
config: Arc<Config>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LspResolver {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-05-06 22:20:15 -04:00
|
|
|
graph_resolver: create_graph_resolver(None, None, None),
|
|
|
|
jsr_resolver: None,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver: None,
|
|
|
|
node_resolver: None,
|
2024-05-06 22:20:15 -04:00
|
|
|
redirect_resolver: None,
|
|
|
|
graph_imports: Default::default(),
|
2024-04-26 16:39:33 -04:00
|
|
|
config: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LspResolver {
|
2024-05-28 20:26:43 -04:00
|
|
|
pub async fn from_config(
|
2024-04-26 16:39:33 -04:00
|
|
|
config: &Config,
|
2024-05-09 15:22:27 -04:00
|
|
|
cache: &LspCache,
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: Option<&Arc<HttpClientProvider>>,
|
2024-05-28 20:26:43 -04:00
|
|
|
) -> Self {
|
2024-05-06 22:20:15 -04:00
|
|
|
let config_data = config.tree.root_data();
|
2024-04-26 16:39:33 -04:00
|
|
|
let mut npm_resolver = None;
|
|
|
|
let mut node_resolver = None;
|
2024-06-03 17:17:08 -04:00
|
|
|
if let (Some(http_client), Some(config_data)) =
|
|
|
|
(http_client_provider, config_data)
|
|
|
|
{
|
2024-05-28 20:26:43 -04:00
|
|
|
npm_resolver = create_npm_resolver(config_data, cache, http_client).await;
|
|
|
|
node_resolver = create_node_resolver(npm_resolver.as_ref());
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
let graph_resolver = create_graph_resolver(
|
2024-05-06 22:20:15 -04:00
|
|
|
config_data,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver.as_ref(),
|
|
|
|
node_resolver.as_ref(),
|
|
|
|
);
|
2024-05-06 22:20:15 -04:00
|
|
|
let jsr_resolver = Some(Arc::new(JsrCacheResolver::new(
|
2024-05-09 15:22:27 -04:00
|
|
|
cache.root_vendor_or_global(),
|
2024-05-06 22:20:15 -04:00
|
|
|
config_data.and_then(|d| d.lockfile.clone()),
|
|
|
|
)));
|
2024-05-09 15:22:27 -04:00
|
|
|
let redirect_resolver = Some(Arc::new(RedirectResolver::new(
|
|
|
|
cache.root_vendor_or_global(),
|
|
|
|
)));
|
2024-05-06 22:20:15 -04:00
|
|
|
let graph_imports = config_data
|
|
|
|
.and_then(|d| d.config_file.as_ref())
|
|
|
|
.and_then(|cf| cf.to_maybe_imports().ok())
|
|
|
|
.map(|imports| {
|
|
|
|
Arc::new(
|
|
|
|
imports
|
|
|
|
.into_iter()
|
|
|
|
.map(|(referrer, imports)| {
|
|
|
|
let graph_import = GraphImport::new(
|
|
|
|
&referrer,
|
|
|
|
imports,
|
|
|
|
&CliJsrUrlProvider,
|
|
|
|
Some(graph_resolver.as_ref()),
|
|
|
|
Some(graph_resolver.as_ref()),
|
|
|
|
);
|
|
|
|
(referrer, graph_import)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2024-05-28 20:26:43 -04:00
|
|
|
Self {
|
2024-04-26 16:39:33 -04:00
|
|
|
graph_resolver,
|
2024-05-06 22:20:15 -04:00
|
|
|
jsr_resolver,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver,
|
|
|
|
node_resolver,
|
2024-05-06 22:20:15 -04:00
|
|
|
redirect_resolver,
|
|
|
|
graph_imports,
|
2024-04-26 16:39:33 -04:00
|
|
|
config: Arc::new(config.clone()),
|
2024-05-28 20:26:43 -04:00
|
|
|
}
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn snapshot(&self) -> Arc<Self> {
|
|
|
|
let npm_resolver =
|
|
|
|
self.npm_resolver.as_ref().map(|r| r.clone_snapshotted());
|
|
|
|
let node_resolver = create_node_resolver(npm_resolver.as_ref());
|
|
|
|
let graph_resolver = create_graph_resolver(
|
2024-05-06 22:20:15 -04:00
|
|
|
self.config.tree.root_data(),
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver.as_ref(),
|
|
|
|
node_resolver.as_ref(),
|
|
|
|
);
|
|
|
|
Arc::new(Self {
|
|
|
|
graph_resolver,
|
2024-05-06 22:20:15 -04:00
|
|
|
jsr_resolver: self.jsr_resolver.clone(),
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver,
|
|
|
|
node_resolver,
|
2024-05-06 22:20:15 -04:00
|
|
|
redirect_resolver: self.redirect_resolver.clone(),
|
|
|
|
graph_imports: self.graph_imports.clone(),
|
2024-04-26 16:39:33 -04:00
|
|
|
config: self.config.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-06 22:20:15 -04:00
|
|
|
pub fn did_cache(&self) {
|
|
|
|
self.jsr_resolver.as_ref().inspect(|r| r.did_cache());
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:39:33 -04:00
|
|
|
pub async fn set_npm_package_reqs(
|
|
|
|
&self,
|
|
|
|
reqs: &[PackageReq],
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if let Some(npm_resolver) = self.npm_resolver.as_ref() {
|
|
|
|
if let Some(npm_resolver) = npm_resolver.as_managed() {
|
|
|
|
return npm_resolver.set_package_reqs(reqs).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn as_graph_resolver(
|
|
|
|
&self,
|
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> &dyn Resolver {
|
2024-05-13 12:55:31 -04:00
|
|
|
self.graph_resolver.as_ref()
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn as_graph_npm_resolver(
|
|
|
|
&self,
|
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> &dyn NpmResolver {
|
2024-04-26 16:39:33 -04:00
|
|
|
self.graph_resolver.as_ref()
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn maybe_managed_npm_resolver(
|
|
|
|
&self,
|
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> Option<&ManagedCliNpmResolver> {
|
2024-05-13 12:55:31 -04:00
|
|
|
self.npm_resolver.as_ref().and_then(|r| r.as_managed())
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn graph_imports_by_referrer(
|
2024-05-13 12:55:31 -04:00
|
|
|
&self,
|
2024-05-23 12:31:56 -04:00
|
|
|
) -> IndexMap<&ModuleSpecifier, Vec<&ModuleSpecifier>> {
|
2024-05-13 12:55:31 -04:00
|
|
|
self
|
|
|
|
.graph_imports
|
2024-05-23 12:31:56 -04:00
|
|
|
.iter()
|
|
|
|
.map(|(s, i)| {
|
|
|
|
(
|
|
|
|
s,
|
|
|
|
i.dependencies
|
|
|
|
.values()
|
|
|
|
.flat_map(|d| d.get_type().or_else(|| d.get_code()))
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect()
|
2024-05-13 12:55:31 -04:00
|
|
|
}
|
|
|
|
|
2024-05-06 22:20:15 -04:00
|
|
|
pub fn jsr_to_registry_url(
|
|
|
|
&self,
|
|
|
|
req_ref: &JsrPackageReqReference,
|
2024-05-23 12:31:56 -04:00
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
2024-05-06 22:20:15 -04:00
|
|
|
) -> Option<ModuleSpecifier> {
|
|
|
|
self.jsr_resolver.as_ref()?.jsr_to_registry_url(req_ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn jsr_lookup_export_for_path(
|
|
|
|
&self,
|
|
|
|
nv: &PackageNv,
|
|
|
|
path: &str,
|
2024-05-23 12:31:56 -04:00
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
2024-05-06 22:20:15 -04:00
|
|
|
) -> Option<String> {
|
|
|
|
self.jsr_resolver.as_ref()?.lookup_export_for_path(nv, path)
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:31:56 -04:00
|
|
|
pub fn jsr_lookup_req_for_nv(
|
|
|
|
&self,
|
|
|
|
nv: &PackageNv,
|
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> Option<PackageReq> {
|
2024-05-06 22:20:15 -04:00
|
|
|
self.jsr_resolver.as_ref()?.lookup_req_for_nv(nv)
|
|
|
|
}
|
|
|
|
|
2024-05-13 12:55:31 -04:00
|
|
|
pub fn npm_to_file_url(
|
2024-05-06 22:20:15 -04:00
|
|
|
&self,
|
2024-05-13 12:55:31 -04:00
|
|
|
req_ref: &NpmPackageReqReference,
|
|
|
|
referrer: &ModuleSpecifier,
|
2024-05-23 12:31:56 -04:00
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
2024-05-13 12:55:31 -04:00
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
|
|
|
let node_resolver = self.node_resolver.as_ref()?;
|
|
|
|
Some(NodeResolution::into_specifier_and_media_type(
|
|
|
|
node_resolver
|
|
|
|
.resolve_req_reference(
|
|
|
|
req_ref,
|
|
|
|
&PermissionsContainer::allow_all(),
|
|
|
|
referrer,
|
2024-05-21 12:54:15 -04:00
|
|
|
NodeResolutionMode::Types,
|
2024-05-13 12:55:31 -04:00
|
|
|
)
|
|
|
|
.ok(),
|
|
|
|
))
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
|
2024-05-13 12:55:31 -04:00
|
|
|
pub fn in_node_modules(&self, specifier: &ModuleSpecifier) -> bool {
|
2024-04-26 16:39:33 -04:00
|
|
|
if let Some(npm_resolver) = &self.npm_resolver {
|
|
|
|
return npm_resolver.in_npm_package(specifier);
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2024-05-13 12:55:31 -04:00
|
|
|
pub fn node_media_type(
|
2024-04-26 16:39:33 -04:00
|
|
|
&self,
|
2024-05-13 12:55:31 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<MediaType> {
|
|
|
|
let node_resolver = self.node_resolver.as_ref()?;
|
|
|
|
let resolution = node_resolver
|
|
|
|
.url_to_node_resolution(specifier.clone())
|
|
|
|
.ok()?;
|
|
|
|
Some(NodeResolution::into_specifier_and_media_type(Some(resolution)).1)
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_closest_package_json(
|
|
|
|
&self,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
) -> Result<Option<Rc<PackageJson>>, AnyError> {
|
|
|
|
let Some(node_resolver) = self.node_resolver.as_ref() else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
node_resolver
|
|
|
|
.get_closest_package_json(referrer, &PermissionsContainer::allow_all())
|
|
|
|
}
|
2024-05-06 22:20:15 -04:00
|
|
|
|
|
|
|
pub fn resolve_redirects(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2024-05-23 12:31:56 -04:00
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
2024-05-06 22:20:15 -04:00
|
|
|
) -> Option<ModuleSpecifier> {
|
|
|
|
let Some(redirect_resolver) = self.redirect_resolver.as_ref() else {
|
|
|
|
return Some(specifier.clone());
|
|
|
|
};
|
|
|
|
redirect_resolver.resolve(specifier)
|
|
|
|
}
|
2024-05-09 15:22:27 -04:00
|
|
|
|
|
|
|
pub fn redirect_chain_headers(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2024-05-23 12:31:56 -04:00
|
|
|
_file_referrer: Option<&ModuleSpecifier>,
|
2024-05-09 15:22:27 -04:00
|
|
|
) -> Vec<(ModuleSpecifier, Arc<HashMap<String, String>>)> {
|
|
|
|
let Some(redirect_resolver) = self.redirect_resolver.as_ref() else {
|
|
|
|
return vec![];
|
|
|
|
};
|
|
|
|
redirect_resolver
|
|
|
|
.chain(specifier)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(s, e)| (s, e.headers.clone()))
|
|
|
|
.collect()
|
|
|
|
}
|
2024-04-26 16:39:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_npm_resolver(
|
|
|
|
config_data: &ConfigData,
|
2024-05-09 15:22:27 -04:00
|
|
|
cache: &LspCache,
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: &Arc<HttpClientProvider>,
|
2024-04-26 16:39:33 -04:00
|
|
|
) -> Option<Arc<dyn CliNpmResolver>> {
|
|
|
|
let node_modules_dir = config_data
|
|
|
|
.node_modules_dir
|
|
|
|
.clone()
|
|
|
|
.or_else(|| specifier_to_file_path(&config_data.scope).ok())?;
|
|
|
|
let options = if config_data.byonm {
|
|
|
|
CliNpmResolverCreateOptions::Byonm(CliNpmResolverByonmCreateOptions {
|
|
|
|
fs: Arc::new(deno_fs::RealFs),
|
2024-05-28 20:26:43 -04:00
|
|
|
root_node_modules_dir: node_modules_dir.clone(),
|
2024-04-26 16:39:33 -04:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
CliNpmResolverCreateOptions::Managed(CliNpmResolverManagedCreateOptions {
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: http_client_provider.clone(),
|
2024-04-26 16:39:33 -04:00
|
|
|
snapshot: match config_data.lockfile.as_ref() {
|
|
|
|
Some(lockfile) => {
|
|
|
|
CliNpmResolverManagedSnapshotOption::ResolveFromLockfile(
|
|
|
|
lockfile.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => CliNpmResolverManagedSnapshotOption::Specified(None),
|
|
|
|
},
|
|
|
|
// Don't provide the lockfile. We don't want these resolvers
|
|
|
|
// updating it. Only the cache request should update the lockfile.
|
|
|
|
maybe_lockfile: None,
|
|
|
|
fs: Arc::new(deno_fs::RealFs),
|
2024-05-09 15:22:27 -04:00
|
|
|
npm_global_cache_dir: cache.deno_dir().npm_folder_path(),
|
2024-04-26 16:39:33 -04:00
|
|
|
// Use an "only" cache setting in order to make the
|
|
|
|
// user do an explicit "cache" command and prevent
|
|
|
|
// the cache from being filled with lots of packages while
|
|
|
|
// the user is typing.
|
|
|
|
cache_setting: CacheSetting::Only,
|
|
|
|
text_only_progress_bar: ProgressBar::new(ProgressBarStyle::TextOnly),
|
|
|
|
maybe_node_modules_path: config_data.node_modules_dir.clone(),
|
|
|
|
// do not install while resolving in the lsp—leave that to the cache command
|
|
|
|
package_json_installer:
|
|
|
|
CliNpmResolverManagedPackageJsonInstallerOption::NoInstall,
|
2024-05-23 17:26:23 -04:00
|
|
|
npmrc: config_data
|
|
|
|
.npmrc
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(create_default_npmrc),
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_system_info: NpmSystemInfo::default(),
|
|
|
|
})
|
|
|
|
};
|
|
|
|
Some(create_cli_npm_resolver_for_lsp(options).await)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_node_resolver(
|
|
|
|
npm_resolver: Option<&Arc<dyn CliNpmResolver>>,
|
|
|
|
) -> Option<Arc<CliNodeResolver>> {
|
|
|
|
let npm_resolver = npm_resolver?;
|
|
|
|
let fs = Arc::new(deno_fs::RealFs);
|
|
|
|
let node_resolver_inner = Arc::new(NodeResolver::new(
|
|
|
|
fs.clone(),
|
|
|
|
npm_resolver.clone().into_npm_resolver(),
|
|
|
|
));
|
|
|
|
Some(Arc::new(CliNodeResolver::new(
|
|
|
|
None,
|
|
|
|
fs,
|
|
|
|
node_resolver_inner,
|
|
|
|
npm_resolver.clone(),
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_graph_resolver(
|
2024-05-06 22:20:15 -04:00
|
|
|
config_data: Option<&ConfigData>,
|
2024-04-26 16:39:33 -04:00
|
|
|
npm_resolver: Option<&Arc<dyn CliNpmResolver>>,
|
|
|
|
node_resolver: Option<&Arc<CliNodeResolver>>,
|
|
|
|
) -> Arc<CliGraphResolver> {
|
|
|
|
let config_file = config_data.and_then(|d| d.config_file.as_deref());
|
2024-05-13 12:55:31 -04:00
|
|
|
let unstable_sloppy_imports =
|
|
|
|
config_file.is_some_and(|cf| cf.has_unstable("sloppy-imports"));
|
2024-04-26 16:39:33 -04:00
|
|
|
Arc::new(CliGraphResolver::new(CliGraphResolverOptions {
|
|
|
|
node_resolver: node_resolver.cloned(),
|
|
|
|
npm_resolver: npm_resolver.cloned(),
|
|
|
|
package_json_deps_provider: Arc::new(PackageJsonDepsProvider::new(
|
|
|
|
config_data
|
|
|
|
.and_then(|d| d.package_json.as_ref())
|
|
|
|
.map(|package_json| {
|
|
|
|
package_json::get_local_package_json_version_reqs(package_json)
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
maybe_jsx_import_source_config: config_file
|
|
|
|
.and_then(|cf| cf.to_maybe_jsx_import_source_config().ok().flatten()),
|
|
|
|
maybe_import_map: config_data.and_then(|d| d.import_map.clone()),
|
|
|
|
maybe_vendor_dir: config_data.and_then(|d| d.vendor_dir.as_ref()),
|
|
|
|
bare_node_builtins_enabled: config_file
|
2024-05-06 22:20:15 -04:00
|
|
|
.map(|cf| cf.has_unstable("bare-node-builtins"))
|
2024-04-26 16:39:33 -04:00
|
|
|
.unwrap_or(false),
|
2024-05-13 12:55:31 -04:00
|
|
|
sloppy_imports_resolver: unstable_sloppy_imports.then(|| {
|
|
|
|
SloppyImportsResolver::new_without_stat_cache(Arc::new(deno_fs::RealFs))
|
|
|
|
}),
|
2024-04-26 16:39:33 -04:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
struct RedirectEntry {
|
|
|
|
headers: Arc<HashMap<String, String>>,
|
|
|
|
target: Url,
|
|
|
|
destination: Option<Url>,
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetHeadersFn =
|
|
|
|
Box<dyn Fn(&Url) -> Option<HashMap<String, String>> + Send + Sync>;
|
|
|
|
|
2024-05-06 22:20:15 -04:00
|
|
|
struct RedirectResolver {
|
2024-05-09 15:22:27 -04:00
|
|
|
get_headers: GetHeadersFn,
|
|
|
|
entries: DashMap<Url, Option<Arc<RedirectEntry>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for RedirectResolver {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("RedirectResolver")
|
|
|
|
.field("get_headers", &"Box(|_| { ... })")
|
|
|
|
.field("entries", &self.entries)
|
|
|
|
.finish()
|
|
|
|
}
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RedirectResolver {
|
2024-05-09 15:22:27 -04:00
|
|
|
fn new(cache: Arc<dyn HttpCache>) -> Self {
|
2024-05-06 22:20:15 -04:00
|
|
|
Self {
|
2024-05-09 15:22:27 -04:00
|
|
|
get_headers: Box::new(move |specifier| {
|
|
|
|
let cache_key = cache.cache_item_key(specifier).ok()?;
|
|
|
|
cache.read_headers(&cache_key).ok().flatten()
|
|
|
|
}),
|
|
|
|
entries: Default::default(),
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
fn mock(get_headers: GetHeadersFn) -> Self {
|
|
|
|
Self {
|
|
|
|
get_headers,
|
|
|
|
entries: Default::default(),
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
fn resolve(&self, specifier: &Url) -> Option<Url> {
|
|
|
|
if !matches!(specifier.scheme(), "http" | "https") {
|
|
|
|
return Some(specifier.clone());
|
|
|
|
}
|
|
|
|
let mut current = specifier.clone();
|
|
|
|
let mut chain = vec![];
|
|
|
|
let destination = loop {
|
|
|
|
if let Some(maybe_entry) = self.entries.get(¤t) {
|
|
|
|
break match maybe_entry.as_ref() {
|
|
|
|
Some(entry) => entry.destination.clone(),
|
|
|
|
None => Some(current),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let Some(headers) = (self.get_headers)(¤t) else {
|
|
|
|
break None;
|
|
|
|
};
|
|
|
|
let headers = Arc::new(headers);
|
2024-05-06 22:20:15 -04:00
|
|
|
if let Some(location) = headers.get("location") {
|
2024-05-09 15:22:27 -04:00
|
|
|
if chain.len() > 10 {
|
|
|
|
break None;
|
|
|
|
}
|
|
|
|
let Ok(target) =
|
|
|
|
deno_core::resolve_import(location, specifier.as_str())
|
|
|
|
else {
|
|
|
|
break None;
|
|
|
|
};
|
|
|
|
chain.push((
|
|
|
|
current.clone(),
|
|
|
|
RedirectEntry {
|
|
|
|
headers,
|
|
|
|
target: target.clone(),
|
|
|
|
destination: None,
|
|
|
|
},
|
|
|
|
));
|
|
|
|
current = target;
|
2024-05-06 22:20:15 -04:00
|
|
|
} else {
|
2024-05-09 15:22:27 -04:00
|
|
|
self.entries.insert(current.clone(), None);
|
|
|
|
break Some(current);
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
2024-05-09 15:22:27 -04:00
|
|
|
};
|
|
|
|
for (specifier, mut entry) in chain {
|
|
|
|
entry.destination = destination.clone();
|
|
|
|
self.entries.insert(specifier, Some(Arc::new(entry)));
|
|
|
|
}
|
|
|
|
destination
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chain(&self, specifier: &Url) -> Vec<(Url, Arc<RedirectEntry>)> {
|
|
|
|
self.resolve(specifier);
|
|
|
|
let mut result = vec![];
|
|
|
|
let mut seen = HashSet::new();
|
|
|
|
let mut current = Cow::Borrowed(specifier);
|
|
|
|
loop {
|
|
|
|
let Some(maybe_entry) = self.entries.get(¤t) else {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
let Some(entry) = maybe_entry.as_ref() else {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
result.push((current.as_ref().clone(), entry.clone()));
|
|
|
|
seen.insert(current.as_ref().clone());
|
|
|
|
if seen.contains(&entry.target) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current = Cow::Owned(entry.target.clone())
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
2024-05-09 15:22:27 -04:00
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_redirect_resolver() {
|
|
|
|
let redirect_resolver =
|
|
|
|
RedirectResolver::mock(Box::new(|specifier| match specifier.as_str() {
|
|
|
|
"https://foo/redirect_2.js" => Some(
|
|
|
|
[("location".to_string(), "./redirect_1.js".to_string())]
|
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
"https://foo/redirect_1.js" => Some(
|
|
|
|
[("location".to_string(), "./file.js".to_string())]
|
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
"https://foo/file.js" => Some([].into_iter().collect()),
|
|
|
|
_ => None,
|
|
|
|
}));
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver.resolve(&Url::parse("https://foo/file.js").unwrap()),
|
|
|
|
Some(Url::parse("https://foo/file.js").unwrap())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver
|
|
|
|
.resolve(&Url::parse("https://foo/redirect_1.js").unwrap()),
|
|
|
|
Some(Url::parse("https://foo/file.js").unwrap())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver
|
|
|
|
.resolve(&Url::parse("https://foo/redirect_2.js").unwrap()),
|
|
|
|
Some(Url::parse("https://foo/file.js").unwrap())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver.resolve(&Url::parse("https://foo/unknown").unwrap()),
|
|
|
|
None
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
redirect_resolver
|
|
|
|
.chain(&Url::parse("https://foo/redirect_2.js").unwrap()),
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
Url::parse("https://foo/redirect_2.js").unwrap(),
|
|
|
|
Arc::new(RedirectEntry {
|
|
|
|
headers: Arc::new(
|
|
|
|
[("location".to_string(), "./redirect_1.js".to_string())]
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
|
|
|
),
|
|
|
|
target: Url::parse("https://foo/redirect_1.js").unwrap(),
|
|
|
|
destination: Some(Url::parse("https://foo/file.js").unwrap()),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Url::parse("https://foo/redirect_1.js").unwrap(),
|
|
|
|
Arc::new(RedirectEntry {
|
|
|
|
headers: Arc::new(
|
|
|
|
[("location".to_string(), "./file.js".to_string())]
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
|
|
|
),
|
|
|
|
target: Url::parse("https://foo/file.js").unwrap(),
|
|
|
|
destination: Some(Url::parse("https://foo/file.js").unwrap()),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
2024-05-06 22:20:15 -04:00
|
|
|
}
|
|
|
|
}
|