use crate::version::Version;
const OFFICIAL_MIRROR: &str = "https://nodejs.org/dist";
const MUSL_MIRROR: &str = "https://unofficial-builds.nodejs.org/download/release";
pub fn os() -> &'static str {
match std::env::consts::OS {
"windows" => "win",
"macos" => "darwin",
other => other,
}
}
const KNOWN_ARCHES: [&str; 7] = ["x64", "arm64", "x86", "armv7l", "ppc64le", "s390x", "riscv64"];
fn native_arch() -> &'static str {
match std::env::consts::ARCH {
"x86_64" => "x64",
"aarch64" => "arm64",
"x86" => "x86",
"arm" => "armv7l",
"powerpc64" => "ppc64le",
other => other,
}
}
pub fn arch_is_chosen() -> bool {
std::env::var_os("SNE_ARCH").is_some_and(|v| !v.is_empty())
}
pub fn arch() -> anyhow::Result<&'static str> {
let Ok(wanted) = std::env::var("SNE_ARCH") else {
return Ok(native_arch());
};
let wanted = wanted.trim();
if wanted.is_empty() {
return Ok(native_arch());
}
KNOWN_ARCHES
.into_iter()
.find(|known| known.eq_ignore_ascii_case(wanted))
.ok_or_else(|| {
anyhow::anyhow!(
"SNE_ARCH is `{wanted}`, which is not a processor type of Node.js. Use one of: {}",
KNOWN_ARCHES.join(", ")
)
})
}
pub fn emulated_arch(os: &str, arch: &str) -> Option<&'static str> {
match (os, arch) {
("darwin" | "win", "arm64") => Some("x64"),
_ => None,
}
}
pub fn is_musl() -> bool {
if !cfg!(target_os = "linux") {
return false;
}
let has = |dir: &str, prefix: &str| {
std::fs::read_dir(dir).is_ok_and(|entries| {
entries
.flatten()
.any(|e| e.file_name().to_string_lossy().starts_with(prefix))
})
};
has("/lib", "ld-musl-") && !has("/lib", "ld-linux") && !has("/lib64", "ld-linux")
}
pub fn archive_name(os: &str, arch: &str, musl: bool, v: Version) -> String {
match os {
"win" => format!("node-{v}-win-{arch}.zip"),
"linux" if musl => format!("node-{v}-linux-{arch}-musl.tar.xz"),
_ => format!("node-{v}-{os}-{arch}.tar.xz"),
}
}
pub fn index_key(os: &str, arch: &str, musl: bool) -> String {
match os {
"win" => format!("win-{arch}-zip"),
"darwin" => format!("osx-{arch}-tar"),
"linux" if musl => format!("linux-{arch}-musl"),
_ => format!("{os}-{arch}"),
}
}
pub fn mirror() -> String {
match std::env::var("SNE_NODE_MIRROR") {
Ok(url) if !url.trim().is_empty() => url.trim().trim_end_matches('/').to_string(),
_ if is_musl() => MUSL_MIRROR.to_string(),
_ => OFFICIAL_MIRROR.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn archive_names_match_nodejs_dist() {
let v = Version::parse("22.11.0").unwrap();
assert_eq!(archive_name("win", "x64", false, v), "node-v22.11.0-win-x64.zip");
assert_eq!(
archive_name("darwin", "arm64", false, v),
"node-v22.11.0-darwin-arm64.tar.xz"
);
assert_eq!(
archive_name("linux", "x64", false, v),
"node-v22.11.0-linux-x64.tar.xz"
);
assert_eq!(
archive_name("linux", "x64", true, v),
"node-v22.11.0-linux-x64-musl.tar.xz"
);
}
#[test]
fn emulation_only_where_the_system_provides_it() {
assert_eq!(emulated_arch("darwin", "arm64"), Some("x64"));
assert_eq!(emulated_arch("win", "arm64"), Some("x64"));
assert_eq!(emulated_arch("linux", "arm64"), None);
assert_eq!(emulated_arch("darwin", "x64"), None);
}
#[test]
fn index_keys_match_nodejs_dist() {
assert_eq!(index_key("win", "arm64", false), "win-arm64-zip");
assert_eq!(index_key("darwin", "x64", false), "osx-x64-tar");
assert_eq!(index_key("linux", "arm64", false), "linux-arm64");
assert_eq!(index_key("linux", "x64", true), "linux-x64-musl");
}
}