2024-02-12 22:12:49 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2024-02-29 03:54:16 +00:00
|
|
|
use crate::args::jsr_api_url;
|
|
|
|
use crate::file_fetcher::FileFetcher;
|
2024-03-01 21:34:13 +00:00
|
|
|
use crate::jsr::JsrFetchResolver;
|
2024-02-14 22:48:39 +00:00
|
|
|
use dashmap::DashMap;
|
2024-02-29 03:54:16 +00:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::error::AnyError;
|
2024-02-12 22:12:49 +00:00
|
|
|
use deno_core::serde_json;
|
2024-02-29 03:54:16 +00:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2024-02-12 22:12:49 +00:00
|
|
|
use deno_semver::package::PackageNv;
|
2024-02-29 03:54:16 +00:00
|
|
|
use deno_semver::Version;
|
|
|
|
use serde::Deserialize;
|
2024-02-12 22:12:49 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-02-29 03:54:16 +00:00
|
|
|
use super::search::PackageSearchApi;
|
2024-02-15 19:49:35 +00:00
|
|
|
|
2024-03-06 13:24:15 +00:00
|
|
|
#[derive(Debug)]
|
2024-02-29 03:54:16 +00:00
|
|
|
pub struct CliJsrSearchApi {
|
|
|
|
file_fetcher: FileFetcher,
|
2024-03-06 13:24:15 +00:00
|
|
|
resolver: JsrFetchResolver,
|
|
|
|
search_cache: DashMap<String, Arc<Vec<String>>>,
|
|
|
|
versions_cache: DashMap<String, Arc<Vec<Version>>>,
|
|
|
|
exports_cache: DashMap<PackageNv, Arc<Vec<String>>>,
|
2024-02-29 03:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CliJsrSearchApi {
|
|
|
|
pub fn new(file_fetcher: FileFetcher) -> Self {
|
2024-03-06 13:24:15 +00:00
|
|
|
let resolver = JsrFetchResolver::new(file_fetcher.clone());
|
2024-02-29 03:54:16 +00:00
|
|
|
Self {
|
|
|
|
file_fetcher,
|
|
|
|
resolver,
|
|
|
|
search_cache: Default::default(),
|
|
|
|
versions_cache: Default::default(),
|
|
|
|
exports_cache: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:24:15 +00:00
|
|
|
pub fn get_resolver(&self) -> &JsrFetchResolver {
|
2024-02-29 03:54:16 +00:00
|
|
|
&self.resolver
|
|
|
|
}
|
2024-05-03 19:52:58 +00:00
|
|
|
|
|
|
|
pub fn clear_cache(&self) {
|
|
|
|
self.file_fetcher.clear_memory_files();
|
|
|
|
self.search_cache.clear();
|
|
|
|
self.versions_cache.clear();
|
|
|
|
self.exports_cache.clear();
|
|
|
|
}
|
2024-02-29 03:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl PackageSearchApi for CliJsrSearchApi {
|
|
|
|
async fn search(&self, query: &str) -> Result<Arc<Vec<String>>, AnyError> {
|
|
|
|
if let Some(names) = self.search_cache.get(query) {
|
|
|
|
return Ok(names.clone());
|
|
|
|
}
|
2024-03-06 13:24:15 +00:00
|
|
|
let mut search_url = jsr_api_url().join("packages")?;
|
2024-02-29 03:54:16 +00:00
|
|
|
search_url.query_pairs_mut().append_pair("query", query);
|
|
|
|
let file = self
|
|
|
|
.file_fetcher
|
2024-04-19 01:43:28 +00:00
|
|
|
.fetch(&search_url, &PermissionsContainer::allow_all())
|
2024-02-29 03:54:16 +00:00
|
|
|
.await?
|
|
|
|
.into_text_decoded()?;
|
|
|
|
let names = Arc::new(parse_jsr_search_response(&file.source)?);
|
|
|
|
self.search_cache.insert(query.to_string(), names.clone());
|
|
|
|
Ok(names)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn versions(&self, name: &str) -> Result<Arc<Vec<Version>>, AnyError> {
|
|
|
|
if let Some(versions) = self.versions_cache.get(name) {
|
|
|
|
return Ok(versions.clone());
|
|
|
|
}
|
2024-03-01 21:34:13 +00:00
|
|
|
let info = self
|
|
|
|
.resolver
|
|
|
|
.package_info(name)
|
|
|
|
.await
|
|
|
|
.ok_or_else(|| anyhow!("JSR package info not found: {}", name))?;
|
|
|
|
let mut versions = info.versions.keys().cloned().collect::<Vec<_>>();
|
2024-02-29 03:54:16 +00:00
|
|
|
versions.sort();
|
|
|
|
versions.reverse();
|
|
|
|
let versions = Arc::new(versions);
|
|
|
|
self
|
|
|
|
.versions_cache
|
|
|
|
.insert(name.to_string(), versions.clone());
|
|
|
|
Ok(versions)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn exports(
|
|
|
|
&self,
|
|
|
|
nv: &PackageNv,
|
|
|
|
) -> Result<Arc<Vec<String>>, AnyError> {
|
|
|
|
if let Some(exports) = self.exports_cache.get(nv) {
|
|
|
|
return Ok(exports.clone());
|
|
|
|
}
|
2024-03-01 21:34:13 +00:00
|
|
|
let info = self
|
|
|
|
.resolver
|
|
|
|
.package_version_info(nv)
|
|
|
|
.await
|
|
|
|
.ok_or_else(|| anyhow!("JSR package version info not found: {}", nv))?;
|
2024-02-29 03:54:16 +00:00
|
|
|
let mut exports = info
|
|
|
|
.exports()
|
|
|
|
.map(|(n, _)| n.to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
exports.sort();
|
|
|
|
let exports = Arc::new(exports);
|
|
|
|
self.exports_cache.insert(nv.clone(), exports.clone());
|
|
|
|
Ok(exports)
|
|
|
|
}
|
2024-02-14 22:48:39 +00:00
|
|
|
}
|
|
|
|
|
2024-02-29 03:54:16 +00:00
|
|
|
fn parse_jsr_search_response(source: &str) -> Result<Vec<String>, AnyError> {
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct Item {
|
|
|
|
scope: String,
|
|
|
|
name: String,
|
|
|
|
version_count: usize,
|
|
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct Response {
|
|
|
|
items: Vec<Item>,
|
|
|
|
}
|
|
|
|
let items = serde_json::from_str::<Response>(source)?.items;
|
|
|
|
Ok(
|
|
|
|
items
|
|
|
|
.into_iter()
|
|
|
|
.filter(|i| i.version_count > 0)
|
|
|
|
.map(|i| format!("@{}/{}", i.scope, i.name))
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|