Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: event for a resolved package nv #59

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 6 additions & 103 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;

use deno_semver::package::PackageNv;
use deno_semver::Version;
Expand All @@ -16,11 +17,15 @@ use registry::NpmPackageVersionDistInfo;
use resolution::SerializedNpmResolutionSnapshotPackage;
use serde::Deserialize;
use serde::Serialize;
use system_info::matches_os_or_cpu_vec;
use thiserror::Error;

pub mod npm_rc;
pub mod registry;
pub mod resolution;
mod system_info;

pub use system_info::NpmSystemInfo;

#[derive(Debug, Error)]
#[error("Invalid npm package id '{text}'. {message}")]
Expand Down Expand Up @@ -244,7 +249,7 @@ pub struct NpmResolutionPackage {
pub copy_index: u8,
#[serde(flatten)]
pub system: NpmResolutionPackageSystemInfo,
pub dist: NpmPackageVersionDistInfo,
pub dist: Arc<NpmPackageVersionDistInfo>,
/// Key is what the package refers to the other package as,
/// which could be different from the package name.
pub dependencies: HashMap<String, NpmPackageId>,
Expand Down Expand Up @@ -295,74 +300,6 @@ impl NpmResolutionPackage {
}
}

/// System information used to determine which optional packages
/// to download.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NpmSystemInfo {
/// `process.platform` value from Node.js
pub os: String,
/// `process.arch` value from Node.js
pub cpu: String,
}

impl Default for NpmSystemInfo {
fn default() -> Self {
Self {
os: node_js_os(std::env::consts::OS),
cpu: node_js_cpu(std::env::consts::ARCH),
}
}
}

impl NpmSystemInfo {
pub fn from_rust(os: &str, cpu: &str) -> Self {
Self {
os: node_js_os(os),
cpu: node_js_cpu(cpu),
}
}
}

fn matches_os_or_cpu_vec(items: &[String], target: &str) -> bool {
if items.is_empty() {
return true;
}
let mut had_negation = false;
for item in items {
if item.starts_with('!') {
if &item[1..] == target {
return false;
}
had_negation = true;
} else if item == target {
return true;
}
}
had_negation
}

fn node_js_cpu(rust_arch: &str) -> String {
// possible values: https://nodejs.org/api/process.html#processarch
// 'arm', 'arm64', 'ia32', 'mips','mipsel', 'ppc', 'ppc64', 's390', 's390x', and 'x64'
match rust_arch {
"x86_64" => "x64",
"aarch64" => "arm64",
value => value,
}
.to_string()
}

fn node_js_os(rust_os: &str) -> String {
// possible values: https://nodejs.org/api/process.html#processplatform
// 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32'
match rust_os {
"macos" => "darwin",
"windows" => "win32",
value => value,
}
.to_string()
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -440,38 +377,4 @@ mod test {
~",
);
}

#[test]
fn test_matches_os_or_cpu_vec() {
assert!(matches_os_or_cpu_vec(&[], "x64"));
assert!(matches_os_or_cpu_vec(&["x64".to_string()], "x64"));
assert!(!matches_os_or_cpu_vec(&["!x64".to_string()], "x64"));
assert!(matches_os_or_cpu_vec(&["!arm64".to_string()], "x64"));
assert!(matches_os_or_cpu_vec(
&["!arm64".to_string(), "!x86".to_string()],
"x64"
));
assert!(!matches_os_or_cpu_vec(
&["!arm64".to_string(), "!x86".to_string()],
"x86"
));
assert!(!matches_os_or_cpu_vec(
&[
"!arm64".to_string(),
"!x86".to_string(),
"other".to_string()
],
"x86"
));

// not explicitly excluded and there's an include, so it's considered a match
assert!(matches_os_or_cpu_vec(
&[
"!arm64".to_string(),
"!x86".to_string(),
"other".to_string()
],
"x64"
));
}
}
Loading
Loading