summaryrefslogtreecommitdiff
path: root/cli/npm/resolution.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-08-23 10:39:19 -0400
committerGitHub <noreply@github.com>2022-08-23 10:39:19 -0400
commite7367044d933ee3518ae583a43876a0ddab5b17e (patch)
treee557e3ea4faad632bcfafa6539f3bd0115dc6426 /cli/npm/resolution.rs
parent362af63c6f45e98948536d08d2d6195af87f729c (diff)
feat: binary npm commands (#15542)
Diffstat (limited to 'cli/npm/resolution.rs')
-rw-r--r--cli/npm/resolution.rs89
1 files changed, 56 insertions, 33 deletions
diff --git a/cli/npm/resolution.rs b/cli/npm/resolution.rs
index d92004db0..b945a1e0b 100644
--- a/cli/npm/resolution.rs
+++ b/cli/npm/resolution.rs
@@ -8,12 +8,14 @@ use deno_ast::ModuleSpecifier;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
+use deno_core::futures;
use deno_core::parking_lot::RwLock;
use super::registry::NpmPackageInfo;
use super::registry::NpmPackageVersionDistInfo;
use super::registry::NpmPackageVersionInfo;
use super::registry::NpmRegistryApi;
+use super::version_req::SpecifierVersionReq;
/// The version matcher used for npm schemed urls is more strict than
/// the one used by npm packages.
@@ -28,38 +30,6 @@ pub struct NpmPackageReference {
pub sub_path: Option<String>,
}
-#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
-pub struct NpmPackageReq {
- pub name: String,
- pub version_req: Option<semver::VersionReq>,
-}
-
-impl std::fmt::Display for NpmPackageReq {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match &self.version_req {
- Some(req) => write!(f, "{}@{}", self.name, req),
- None => write!(f, "{}", self.name),
- }
- }
-}
-
-impl NpmVersionMatcher for NpmPackageReq {
- fn matches(&self, version: &semver::Version) -> bool {
- match &self.version_req {
- Some(req) => req.matches(version),
- None => version.pre.is_empty(),
- }
- }
-
- fn version_text(&self) -> String {
- self
- .version_req
- .as_ref()
- .map(|v| format!("{}", v))
- .unwrap_or_else(|| "non-prerelease".to_string())
- }
-}
-
impl NpmPackageReference {
pub fn from_specifier(
specifier: &ModuleSpecifier,
@@ -77,7 +47,7 @@ impl NpmPackageReference {
let (name, version_req) = match specifier.rsplit_once('@') {
Some((name, version_req)) => (
name,
- match semver::VersionReq::parse(version_req) {
+ match SpecifierVersionReq::parse(version_req) {
Ok(v) => Some(v),
Err(_) => None, // not a version requirement
},
@@ -105,6 +75,38 @@ impl std::fmt::Display for NpmPackageReference {
}
}
+#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
+pub struct NpmPackageReq {
+ pub name: String,
+ pub version_req: Option<SpecifierVersionReq>,
+}
+
+impl std::fmt::Display for NpmPackageReq {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match &self.version_req {
+ Some(req) => write!(f, "{}@{}", self.name, req),
+ None => write!(f, "{}", self.name),
+ }
+ }
+}
+
+impl NpmVersionMatcher for NpmPackageReq {
+ fn matches(&self, version: &semver::Version) -> bool {
+ match &self.version_req {
+ Some(req) => req.matches(version),
+ None => version.pre.is_empty(),
+ }
+ }
+
+ fn version_text(&self) -> String {
+ self
+ .version_req
+ .as_ref()
+ .map(|v| format!("{}", v))
+ .unwrap_or_else(|| "non-prerelease".to_string())
+ }
+}
+
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct NpmPackageId {
pub name: String,
@@ -314,6 +316,27 @@ impl NpmResolution {
ordering => ordering,
});
+ // cache all the dependencies' registry infos in parallel when this env var isn't set
+ if std::env::var("DENO_UNSTABLE_NPM_SYNC_DOWNLOAD") != Ok("1".to_string())
+ {
+ let handles = deps
+ .iter()
+ .map(|dep| {
+ let name = dep.name.clone();
+ let api = self.api.clone();
+ tokio::task::spawn(async move {
+ // it's ok to call this without storing the result, because
+ // NpmRegistryApi will cache the package info in memory
+ api.package_info(&name).await
+ })
+ })
+ .collect::<Vec<_>>();
+ let results = futures::future::join_all(handles).await;
+ for result in results {
+ result??; // surface the first error
+ }
+ }
+
// now resolve them
for dep in deps {
// check if an existing dependency matches this