1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Host platform detection and the two naming vocabularies it maps
//! into:
//!
//! - **lockfile vocabulary** (pnpm / Node `process.platform`):
//! `darwin` / `linux` / `win32` / `freebsd`, `x64` / `arm64`,
//! `libc: musl`;
//! - **dist-file vocabulary** (nodejs.org artifact names):
//! `node-v{V}-darwin-arm64.tar.gz`, `node-v{V}-linux-x64-musl.tar.gz`,
//! `node-v{V}-win-x64.zip`.
use crate::error::Error;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Platform {
/// `process.platform` vocabulary: `darwin` / `linux` / `win32`.
pub os: String,
/// `process.arch` vocabulary: `x64` / `arm64` (others passed
/// through as-is from Rust's target arch mapping).
pub cpu: String,
/// `Some("musl")` on musl-libc Linux hosts.
pub libc: Option<String>,
}
impl Platform {
/// Detect the host platform.
///
/// musl detection is a *runtime* check: aube ships static-musl
/// Linux binaries, so `cfg!(target_env = "musl")` is true even on
/// glibc hosts and cannot be trusted. The presence of musl's
/// dynamic loader (`/lib/ld-musl-<arch>.so.1`) is the signal mise
/// uses for the same decision.
pub fn current() -> Result<Platform, Error> {
let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux",
"windows" => "win32",
// FreeBSD reports Node's `process.platform` value verbatim.
// nodejs.org ships no FreeBSD artifacts, so the download
// path can't satisfy a request here — but resolution reaches
// it only after the zero-network fast paths (PATH probe,
// installed scan, mise delegation) miss, and a missing dist
// then surfaces the normal `NoMatchingVersion` /
// `UnsupportedPlatform` diagnostic. Detecting the platform
// (rather than erroring outright) is what lets those local
// paths, `os`-field package filtering, and multi-platform
// lockfile pins behave correctly on FreeBSD.
"freebsd" => "freebsd",
other => {
return Err(Error::UnsupportedPlatform {
platform: format!("{other}-{}", std::env::consts::ARCH),
});
}
};
let cpu = match std::env::consts::ARCH {
"x86_64" => "x64",
"aarch64" => "arm64",
"x86" => "x86",
"powerpc64" => "ppc64",
"s390x" => "s390x",
other => other,
};
let libc = (os == "linux" && detect_musl()).then(|| "musl".to_string());
Ok(Platform {
os: os.to_string(),
cpu: cpu.to_string(),
libc,
})
}
/// The platform segment of a dist artifact name:
/// `darwin-arm64`, `linux-x64-musl`, `win-x64`.
pub fn dist_slug(&self) -> String {
let os = if self.os == "win32" { "win" } else { &self.os };
let musl = if self.libc.as_deref() == Some("musl") {
"-musl"
} else {
""
};
format!("{os}-{}{musl}", self.cpu)
}
/// The token nodejs.org's `index.json` `files[]` array uses for
/// this platform. macOS entries use the legacy `osx-*` prefix
/// with a `-tar` suffix; Windows uses `win-<arch>-zip`.
///
/// musl builds never appear in the official index (they live on
/// unofficial-builds.nodejs.org, whose index has the same shape
/// but plain `linux-x64` tokens), so musl maps to the bare linux
/// token for `files[]` gating purposes.
pub fn index_files_token(&self) -> String {
match self.os.as_str() {
"darwin" => format!("osx-{}-tar", self.cpu),
"win32" => format!("win-{}-zip", self.cpu),
_ => format!("linux-{}", self.cpu),
}
}
/// Archive extension for this platform's dist artifact.
pub fn archive_ext(&self) -> &'static str {
if self.os == "win32" { "zip" } else { "tar.gz" }
}
/// pnpm's `archive:` vocabulary for this platform.
pub fn archive_kind(&self) -> &'static str {
if self.os == "win32" { "zip" } else { "tarball" }
}
/// Human-readable label for error messages.
pub fn label(&self) -> String {
match &self.libc {
Some(libc) => format!("{}-{} ({libc})", self.os, self.cpu),
None => format!("{}-{}", self.os, self.cpu),
}
}
}
#[cfg(target_os = "linux")]
fn detect_musl() -> bool {
// Rust's arch names match musl's loader names for every
// architecture Node ships (x86_64, aarch64), so the constant is
// used verbatim.
std::path::Path::new(&format!("/lib/ld-musl-{}.so.1", std::env::consts::ARCH)).exists()
}
#[cfg(not(target_os = "linux"))]
fn detect_musl() -> bool {
false
}
/// The artifact filename for `version` on `platform`:
/// `node-v22.1.0-darwin-arm64.tar.gz`.
pub fn artifact_filename(version: &node_semver::Version, platform: &Platform) -> String {
format!(
"node-v{version}-{}.{}",
platform.dist_slug(),
platform.archive_ext()
)
}
/// The top-level directory inside an artifact:
/// `node-v22.1.0-darwin-arm64`.
pub fn artifact_top_dir(version: &node_semver::Version, platform: &Platform) -> String {
format!("node-v{version}-{}", platform.dist_slug())
}
#[cfg(test)]
mod tests {
use super::*;
fn plat(os: &str, cpu: &str, libc: Option<&str>) -> Platform {
Platform {
os: os.into(),
cpu: cpu.into(),
libc: libc.map(String::from),
}
}
#[test]
fn dist_slugs() {
assert_eq!(plat("darwin", "arm64", None).dist_slug(), "darwin-arm64");
assert_eq!(plat("linux", "x64", None).dist_slug(), "linux-x64");
assert_eq!(
plat("linux", "x64", Some("musl")).dist_slug(),
"linux-x64-musl"
);
assert_eq!(plat("win32", "x64", None).dist_slug(), "win-x64");
assert_eq!(plat("freebsd", "x64", None).dist_slug(), "freebsd-x64");
}
#[test]
fn freebsd_token_falls_back_to_linux() {
// nodejs.org has no FreeBSD `files[]` token, so availability
// gating reuses the linux token. Download still fails later at
// the SHASUMS lookup (no `node-v*-freebsd-*` artifact), which is
// the intended graceful outcome — FreeBSD relies on system/mise
// Node, not aube's self-download.
assert_eq!(
plat("freebsd", "x64", None).index_files_token(),
"linux-x64"
);
assert_eq!(plat("freebsd", "x64", None).archive_ext(), "tar.gz");
}
#[test]
fn index_tokens() {
assert_eq!(
plat("darwin", "arm64", None).index_files_token(),
"osx-arm64-tar"
);
assert_eq!(
plat("win32", "x64", None).index_files_token(),
"win-x64-zip"
);
assert_eq!(
plat("linux", "arm64", None).index_files_token(),
"linux-arm64"
);
assert_eq!(
plat("linux", "x64", Some("musl")).index_files_token(),
"linux-x64"
);
}
#[test]
fn artifact_names() {
let v: node_semver::Version = "22.1.0".parse().unwrap();
assert_eq!(
artifact_filename(&v, &plat("win32", "x64", None)),
"node-v22.1.0-win-x64.zip"
);
assert_eq!(
artifact_top_dir(&v, &plat("darwin", "arm64", None)),
"node-v22.1.0-darwin-arm64"
);
}
}