diff options
Diffstat (limited to 'cli/lsp/registries.rs')
-rw-r--r-- | cli/lsp/registries.rs | 181 |
1 files changed, 115 insertions, 66 deletions
diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index a17cd1228..9a0ad6ddd 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -17,7 +17,7 @@ use crate::cache::GlobalHttpCache; use crate::cache::HttpCache; use crate::file_fetcher::FetchOptions; use crate::file_fetcher::FileFetcher; -use crate::http_util::HttpClient; +use crate::http_util::HttpClientProvider; use deno_core::anyhow::anyhow; use deno_core::error::AnyError; @@ -417,12 +417,15 @@ enum VariableItems { pub struct ModuleRegistry { origins: HashMap<String, Vec<RegistryConfiguration>>, pub location: PathBuf, - pub file_fetcher: FileFetcher, + pub file_fetcher: Arc<FileFetcher>, http_cache: Arc<GlobalHttpCache>, } impl ModuleRegistry { - pub fn new(location: PathBuf, http_client: Arc<HttpClient>) -> Self { + pub fn new( + location: PathBuf, + http_client_provider: Arc<HttpClientProvider>, + ) -> Self { // the http cache should always be the global one for registry completions let http_cache = Arc::new(GlobalHttpCache::new( location.clone(), @@ -432,7 +435,7 @@ impl ModuleRegistry { http_cache.clone(), CacheSetting::RespectHeaders, true, - http_client, + http_client_provider, Default::default(), None, ); @@ -441,7 +444,7 @@ impl ModuleRegistry { Self { origins: HashMap::new(), location, - file_fetcher, + file_fetcher: Arc::new(file_fetcher), http_cache, } } @@ -512,15 +515,21 @@ impl ModuleRegistry { &self, specifier: &ModuleSpecifier, ) -> Result<Vec<RegistryConfiguration>, AnyError> { - let fetch_result = self - .file_fetcher - .fetch_with_options(FetchOptions { - specifier, - permissions: &PermissionsContainer::allow_all(), - maybe_accept: Some("application/vnd.deno.reg.v2+json, application/vnd.deno.reg.v1+json;q=0.9, application/json;q=0.8"), - maybe_cache_setting: None, - }) - .await; + // spawn due to the lsp's `Send` requirement + let fetch_result = deno_core::unsync::spawn({ + let file_fetcher = self.file_fetcher.clone(); + let specifier = specifier.clone(); + async move { + file_fetcher + .fetch_with_options(FetchOptions { + specifier: &specifier, + permissions: &PermissionsContainer::allow_all(), + maybe_accept: Some("application/vnd.deno.reg.v2+json, application/vnd.deno.reg.v1+json;q=0.9, application/json;q=0.8"), + maybe_cache_setting: None, + }) + .await + } + }).await?; // if there is an error fetching, we will cache an empty file, so that // subsequent requests they are just an empty doc which will error without // needing to connect to the remote URL. We will cache it for 1 week. @@ -612,13 +621,20 @@ impl ModuleRegistry { None, ) .ok()?; - let file = self - .file_fetcher - .fetch(&endpoint, &PermissionsContainer::allow_all()) - .await - .ok()? - .into_text_decoded() - .ok()?; + let file_fetcher = self.file_fetcher.clone(); + // spawn due to the lsp's `Send` requirement + let file = deno_core::unsync::spawn({ + async move { + file_fetcher + .fetch(&endpoint, &PermissionsContainer::allow_all()) + .await + .ok()? + .into_text_decoded() + .ok() + } + }) + .await + .ok()??; let documentation: lsp::Documentation = serde_json::from_str(&file.source).ok()?; return match documentation { @@ -978,13 +994,18 @@ impl ModuleRegistry { url: &str, ) -> Option<lsp::Documentation> { let specifier = Url::parse(url).ok()?; - let file = self - .file_fetcher - .fetch(&specifier, &PermissionsContainer::allow_all()) - .await - .ok()? - .into_text_decoded() - .ok()?; + let file_fetcher = self.file_fetcher.clone(); + // spawn due to the lsp's `Send` requirement + let file = deno_core::unsync::spawn(async move { + file_fetcher + .fetch(&specifier, &PermissionsContainer::allow_all()) + .await + .ok()? + .into_text_decoded() + .ok() + }) + .await + .ok()??; serde_json::from_str(&file.source).ok() } @@ -1037,19 +1058,27 @@ impl ModuleRegistry { async fn get_items(&self, url: &str) -> Option<VariableItems> { let specifier = ModuleSpecifier::parse(url).ok()?; - let file = self - .file_fetcher - .fetch(&specifier, &PermissionsContainer::allow_all()) - .await - .map_err(|err| { - error!( - "Internal error fetching endpoint \"{}\". {}", - specifier, err - ); - }) - .ok()? - .into_text_decoded() - .ok()?; + // spawn due to the lsp's `Send` requirement + let file = deno_core::unsync::spawn({ + let file_fetcher = self.file_fetcher.clone(); + let specifier = specifier.clone(); + async move { + file_fetcher + .fetch(&specifier, &PermissionsContainer::allow_all()) + .await + .map_err(|err| { + error!( + "Internal error fetching endpoint \"{}\". {}", + specifier, err + ); + }) + .ok()? + .into_text_decoded() + .ok() + } + }) + .await + .ok()??; let items: VariableItems = serde_json::from_str(&file.source) .map_err(|err| { error!( @@ -1075,19 +1104,27 @@ impl ModuleRegistry { error!("Internal error mapping endpoint \"{}\". {}", url, err); }) .ok()?; - let file = self - .file_fetcher - .fetch(&specifier, &PermissionsContainer::allow_all()) - .await - .map_err(|err| { - error!( - "Internal error fetching endpoint \"{}\". {}", - specifier, err - ); - }) - .ok()? - .into_text_decoded() - .ok()?; + // spawn due to the lsp's `Send` requirement + let file = deno_core::unsync::spawn({ + let file_fetcher = self.file_fetcher.clone(); + let specifier = specifier.clone(); + async move { + file_fetcher + .fetch(&specifier, &PermissionsContainer::allow_all()) + .await + .map_err(|err| { + error!( + "Internal error fetching endpoint \"{}\". {}", + specifier, err + ); + }) + .ok()? + .into_text_decoded() + .ok() + } + }) + .await + .ok()??; let items: VariableItems = serde_json::from_str(&file.source) .map_err(|err| { error!( @@ -1264,8 +1301,10 @@ mod tests { let _g = test_util::http_server(); let temp_dir = TempDir::new(); let location = temp_dir.path().join("registries").to_path_buf(); - let mut module_registry = - ModuleRegistry::new(location, Arc::new(HttpClient::new(None, None))); + let mut module_registry = ModuleRegistry::new( + location, + Arc::new(HttpClientProvider::new(None, None)), + ); module_registry.enable("http://localhost:4545/").await; let range = lsp::Range { start: lsp::Position { @@ -1322,8 +1361,10 @@ mod tests { let _g = test_util::http_server(); let temp_dir = TempDir::new(); let location = temp_dir.path().join("registries").to_path_buf(); - let mut module_registry = - ModuleRegistry::new(location, Arc::new(HttpClient::new(None, None))); + let mut module_registry = ModuleRegistry::new( + location, + Arc::new(HttpClientProvider::new(None, None)), + ); module_registry.enable("http://localhost:4545/").await; let range = lsp::Range { start: lsp::Position { @@ -1542,8 +1583,10 @@ mod tests { let _g = test_util::http_server(); let temp_dir = TempDir::new(); let location = temp_dir.path().join("registries").to_path_buf(); - let mut module_registry = - ModuleRegistry::new(location, Arc::new(HttpClient::new(None, None))); + let mut module_registry = ModuleRegistry::new( + location, + Arc::new(HttpClientProvider::new(None, None)), + ); module_registry .enable_custom("http://localhost:4545/lsp/registries/deno-import-intellisense-key-first.json") .await @@ -1612,8 +1655,10 @@ mod tests { let _g = test_util::http_server(); let temp_dir = TempDir::new(); let location = temp_dir.path().join("registries").to_path_buf(); - let mut module_registry = - ModuleRegistry::new(location, Arc::new(HttpClient::new(None, None))); + let mut module_registry = ModuleRegistry::new( + location, + Arc::new(HttpClientProvider::new(None, None)), + ); module_registry .enable_custom("http://localhost:4545/lsp/registries/deno-import-intellisense-complex.json") .await @@ -1663,8 +1708,10 @@ mod tests { let _g = test_util::http_server(); let temp_dir = TempDir::new(); let location = temp_dir.path().join("registries").to_path_buf(); - let module_registry = - ModuleRegistry::new(location, Arc::new(HttpClient::new(None, None))); + let module_registry = ModuleRegistry::new( + location, + Arc::new(HttpClientProvider::new(None, None)), + ); let result = module_registry.check_origin("http://localhost:4545").await; assert!(result.is_ok()); } @@ -1674,8 +1721,10 @@ mod tests { let _g = test_util::http_server(); let temp_dir = TempDir::new(); let location = temp_dir.path().join("registries").to_path_buf(); - let module_registry = - ModuleRegistry::new(location, Arc::new(HttpClient::new(None, None))); + let module_registry = ModuleRegistry::new( + location, + Arc::new(HttpClientProvider::new(None, None)), + ); let result = module_registry.check_origin("https://example.com").await; assert!(result.is_err()); let err = result.unwrap_err().to_string(); |