2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-08-29 11:22:05 -04:00
|
|
|
|
2024-02-28 22:54:16 -05:00
|
|
|
use dashmap::DashMap;
|
2023-08-29 11:22:05 -04:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_npm::registry::NpmPackageInfo;
|
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2024-02-28 22:54:16 -05:00
|
|
|
use deno_semver::package::PackageNv;
|
|
|
|
use deno_semver::Version;
|
2023-08-29 11:22:05 -04:00
|
|
|
use serde::Deserialize;
|
2024-02-28 22:54:16 -05:00
|
|
|
use std::sync::Arc;
|
2023-08-29 11:22:05 -04:00
|
|
|
|
2023-10-02 17:53:55 -04:00
|
|
|
use crate::args::npm_registry_default_url;
|
2023-08-29 11:22:05 -04:00
|
|
|
use crate::file_fetcher::FileFetcher;
|
|
|
|
|
2024-02-28 22:54:16 -05:00
|
|
|
use super::search::PackageSearchApi;
|
2023-08-29 11:22:05 -04:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct CliNpmSearchApi {
|
|
|
|
file_fetcher: FileFetcher,
|
2024-02-28 22:54:16 -05:00
|
|
|
search_cache: Arc<DashMap<String, Arc<Vec<String>>>>,
|
|
|
|
versions_cache: Arc<DashMap<String, Arc<Vec<Version>>>>,
|
2023-08-29 11:22:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CliNpmSearchApi {
|
2024-02-28 22:54:16 -05:00
|
|
|
pub fn new(file_fetcher: FileFetcher) -> Self {
|
2023-08-29 11:22:05 -04:00
|
|
|
Self {
|
|
|
|
file_fetcher,
|
|
|
|
search_cache: Default::default(),
|
2024-02-28 22:54:16 -05:00
|
|
|
versions_cache: Default::default(),
|
2023-08-29 11:22:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2024-02-28 22:54:16 -05:00
|
|
|
impl PackageSearchApi for CliNpmSearchApi {
|
2023-08-29 11:22:05 -04:00
|
|
|
async fn search(&self, query: &str) -> Result<Arc<Vec<String>>, AnyError> {
|
2024-02-28 22:54:16 -05:00
|
|
|
if let Some(names) = self.search_cache.get(query) {
|
2023-08-29 11:22:05 -04:00
|
|
|
return Ok(names.clone());
|
|
|
|
}
|
2024-02-28 22:54:16 -05:00
|
|
|
let mut search_url = npm_registry_default_url().clone();
|
2023-08-29 11:22:05 -04:00
|
|
|
search_url
|
|
|
|
.path_segments_mut()
|
|
|
|
.map_err(|_| anyhow!("Custom npm registry URL cannot be a base."))?
|
|
|
|
.pop_if_empty()
|
|
|
|
.extend("-/v1/search".split('/'));
|
|
|
|
search_url
|
|
|
|
.query_pairs_mut()
|
|
|
|
.append_pair("text", &format!("{} boost-exact:false", query));
|
|
|
|
let file = self
|
|
|
|
.file_fetcher
|
|
|
|
.fetch(&search_url, PermissionsContainer::allow_all())
|
2024-01-31 22:15:22 -05:00
|
|
|
.await?
|
|
|
|
.into_text_decoded()?;
|
2023-08-29 11:22:05 -04:00
|
|
|
let names = Arc::new(parse_npm_search_response(&file.source)?);
|
2024-02-28 22:54:16 -05:00
|
|
|
self.search_cache.insert(query.to_string(), names.clone());
|
2023-08-29 11:22:05 -04:00
|
|
|
Ok(names)
|
|
|
|
}
|
|
|
|
|
2024-02-28 22:54:16 -05:00
|
|
|
async fn versions(&self, name: &str) -> Result<Arc<Vec<Version>>, AnyError> {
|
|
|
|
if let Some(versions) = self.versions_cache.get(name) {
|
|
|
|
return Ok(versions.clone());
|
2023-08-29 11:22:05 -04:00
|
|
|
}
|
2024-02-28 22:54:16 -05:00
|
|
|
let mut info_url = npm_registry_default_url().clone();
|
2023-08-29 11:22:05 -04:00
|
|
|
info_url
|
|
|
|
.path_segments_mut()
|
|
|
|
.map_err(|_| anyhow!("Custom npm registry URL cannot be a base."))?
|
|
|
|
.pop_if_empty()
|
|
|
|
.push(name);
|
|
|
|
let file = self
|
|
|
|
.file_fetcher
|
|
|
|
.fetch(&info_url, PermissionsContainer::allow_all())
|
|
|
|
.await?;
|
2024-02-28 22:54:16 -05:00
|
|
|
let info = serde_json::from_slice::<NpmPackageInfo>(&file.source)?;
|
|
|
|
let mut versions = info.versions.into_keys().collect::<Vec<_>>();
|
|
|
|
versions.sort();
|
|
|
|
versions.reverse();
|
|
|
|
let versions = Arc::new(versions);
|
2023-08-29 11:22:05 -04:00
|
|
|
self
|
2024-02-28 22:54:16 -05:00
|
|
|
.versions_cache
|
|
|
|
.insert(name.to_string(), versions.clone());
|
|
|
|
Ok(versions)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn exports(
|
|
|
|
&self,
|
|
|
|
_nv: &PackageNv,
|
|
|
|
) -> Result<Arc<Vec<String>>, AnyError> {
|
|
|
|
Ok(Default::default())
|
2023-08-29 11:22:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_npm_search_response(source: &str) -> Result<Vec<String>, AnyError> {
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct Package {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct Object {
|
|
|
|
package: Package,
|
|
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct Response {
|
|
|
|
objects: Vec<Object>,
|
|
|
|
}
|
|
|
|
let objects = serde_json::from_str::<Response>(source)?.objects;
|
|
|
|
Ok(objects.into_iter().map(|o| o.package.name).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_npm_search_response() {
|
|
|
|
// This is a subset of a realistic response only containing data currently
|
|
|
|
// used by our parser. It's enough to catch regressions.
|
|
|
|
let names = parse_npm_search_response(r#"{"objects":[{"package":{"name":"puppeteer"}},{"package":{"name":"puppeteer-core"}},{"package":{"name":"puppeteer-extra-plugin-stealth"}},{"package":{"name":"puppeteer-extra-plugin"}}]}"#).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
names,
|
|
|
|
vec![
|
|
|
|
"puppeteer".to_string(),
|
|
|
|
"puppeteer-core".to_string(),
|
|
|
|
"puppeteer-extra-plugin-stealth".to_string(),
|
|
|
|
"puppeteer-extra-plugin".to_string()
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|