Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/NativeHost/
OSProperties.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Wire method: `nativeHost:getOSProperties` (cross-platform).
4//! Returns Electron-shaped `{ type, release, arch, platform, cpus }` tuple.
5//! Cached for the process lifetime - OS type, version, arch, and CPU topology
6//! are stable; spawning `sw_vers`/`uname` on every call wastes ~10 ms each.
7
8use std::sync::OnceLock;
9
10use serde_json::{Value, json};
11
12static OS_PROPERTIES_CACHE:OnceLock<Value> = OnceLock::new();
13
14pub async fn NativeOSProperties() -> Result<Value, String> {
15	if let Some(Cached) = OS_PROPERTIES_CACHE.get() {
16		return Ok(Cached.clone());
17	}
18	let Result = compute_os_properties();
19	let _ = OS_PROPERTIES_CACHE.set(Result.clone());
20	Ok(Result)
21}
22
23fn compute_os_properties() -> Value {
24	use sysinfo::System;
25
26	let OsType = match std::env::consts::OS {
27		"macos" => "Darwin",
28
29		"windows" => "Windows_NT",
30
31		"linux" => "Linux",
32
33		_ => std::env::consts::OS,
34	};
35
36	let Release = {
37		#[cfg(target_os = "macos")]
38		{
39			std::process::Command::new("sw_vers")
40				.arg("-productVersion")
41				.output()
42				.ok()
43				.map(|O| String::from_utf8_lossy(&O.stdout).trim().to_string())
44				.unwrap_or_else(|| "14.0".to_string())
45		}
46
47		#[cfg(target_os = "windows")]
48		{
49			std::process::Command::new("cmd")
50				.args(["/c", "ver"])
51				.output()
52				.ok()
53				.map(|O| {
54					let Output = String::from_utf8_lossy(&O.stdout);
55					Output
56						.split('[')
57						.nth(1)
58						.and_then(|S| S.split(']').next())
59						.and_then(|S| S.strip_prefix("Version "))
60						.unwrap_or("10.0.0")
61						.to_string()
62				})
63				.unwrap_or_else(|| "10.0.0".to_string())
64		}
65
66		#[cfg(target_os = "linux")]
67		{
68			std::process::Command::new("uname")
69				.arg("-r")
70				.output()
71				.ok()
72				.map(|O| String::from_utf8_lossy(&O.stdout).trim().to_string())
73				.unwrap_or_else(|| "6.1.0".to_string())
74		}
75
76		#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
77		{
78			"0.0.0".to_string()
79		}
80	};
81
82	let mut Sys = System::new();
83
84	Sys.refresh_cpu_all();
85
86	let Cpus:Vec<Value> = Sys
87		.cpus()
88		.iter()
89		.map(|Cpu| {
90			json!({
91				"model": Cpu.brand(),
92				"speed": Cpu.frequency()
93			})
94		})
95		.collect();
96
97	json!({
98		"type": OsType,
99		"release": Release,
100		"arch": std::env::consts::ARCH,
101		"platform": std::env::consts::OS,
102		"cpus": Cpus
103	})
104}