summaryrefslogtreecommitdiff
path: root/cli/npm/registry.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/npm/registry.rs')
-rw-r--r--cli/npm/registry.rs97
1 files changed, 1 insertions, 96 deletions
diff --git a/cli/npm/registry.rs b/cli/npm/registry.rs
index e04531017..3b7dd4251 100644
--- a/cli/npm/registry.rs
+++ b/cli/npm/registry.rs
@@ -23,7 +23,7 @@ use crate::fs_util;
use crate::http_cache::CACHE_PERM;
use super::cache::NpmCache;
-use super::resolution::NpmVersionMatcher;
+use super::version_req::NpmVersionReq;
// npm registry docs: https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md
@@ -320,98 +320,3 @@ impl NpmRegistryApi {
name_folder_path.join("registry.json")
}
}
-
-/// A version requirement found in an npm package's dependencies.
-pub struct NpmVersionReq {
- raw_text: String,
- comparators: Vec<semver::VersionReq>,
-}
-
-impl NpmVersionReq {
- pub fn parse(text: &str) -> Result<NpmVersionReq, AnyError> {
- // semver::VersionReq doesn't support spaces between comparators
- // and it doesn't support using || for "OR", so we pre-process
- // the version requirement in order to make this work.
- let raw_text = text.to_string();
- let part_texts = text.split("||").collect::<Vec<_>>();
- let mut comparators = Vec::with_capacity(part_texts.len());
- for part in part_texts {
- comparators.push(npm_version_req_parse_part(part)?);
- }
- Ok(NpmVersionReq {
- raw_text,
- comparators,
- })
- }
-}
-
-impl NpmVersionMatcher for NpmVersionReq {
- fn matches(&self, version: &semver::Version) -> bool {
- self.comparators.iter().any(|c| c.matches(version))
- }
-
- fn version_text(&self) -> String {
- self.raw_text.to_string()
- }
-}
-
-fn npm_version_req_parse_part(
- text: &str,
-) -> Result<semver::VersionReq, AnyError> {
- let text = text.trim();
- let text = text.strip_prefix('v').unwrap_or(text);
- let mut chars = text.chars().enumerate().peekable();
- let mut final_text = String::new();
- while chars.peek().is_some() {
- let (i, c) = chars.next().unwrap();
- let is_greater_or_less_than = c == '<' || c == '>';
- if is_greater_or_less_than || c == '=' {
- if i > 0 {
- final_text = final_text.trim().to_string();
- // add a comma to make semver::VersionReq parse this
- final_text.push(',');
- }
- final_text.push(c);
- let next_char = chars.peek().map(|(_, c)| c);
- if is_greater_or_less_than && matches!(next_char, Some('=')) {
- let c = chars.next().unwrap().1; // skip
- final_text.push(c);
- }
- } else {
- final_text.push(c);
- }
- }
- Ok(semver::VersionReq::parse(&final_text)?)
-}
-
-#[cfg(test)]
-mod test {
- use super::*;
-
- struct NpmVersionReqTester(NpmVersionReq);
-
- impl NpmVersionReqTester {
- fn matches(&self, version: &str) -> bool {
- self.0.matches(&semver::Version::parse(version).unwrap())
- }
- }
-
- #[test]
- pub fn npm_version_req_with_v() {
- assert!(NpmVersionReq::parse("v1.0.0").is_ok());
- }
-
- #[test]
- pub fn npm_version_req_ranges() {
- let tester = NpmVersionReqTester(
- NpmVersionReq::parse(">= 2.1.2 < 3.0.0 || 5.x").unwrap(),
- );
- assert!(!tester.matches("2.1.1"));
- assert!(tester.matches("2.1.2"));
- assert!(tester.matches("2.9.9"));
- assert!(!tester.matches("3.0.0"));
- assert!(tester.matches("5.0.0"));
- assert!(tester.matches("5.1.0"));
- assert!(!tester.matches("6.1.0"));
- }
-}