diff --git a/Cargo.toml b/Cargo.toml index 2172161..8557d6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,12 +13,14 @@ exclude = [".github/"] [dependencies] crc32fast = "^1" -hostname = "^0.3" md5 = "^0.7" once_cell = "^1" rand = "^0.8" thiserror = "^1" +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +hostname = "^0.3" + [target.'cfg(target_os = "macos")'.dependencies] sysctl = "^0.4" diff --git a/src/machine_id.rs b/src/machine_id.rs index 0202599..7db1ee5 100644 --- a/src/machine_id.rs +++ b/src/machine_id.rs @@ -6,9 +6,7 @@ use sysctl::{Sysctl, SysctlError}; pub fn get() -> [u8; 3] { let id = match machine_id().unwrap_or_default() { x if !x.is_empty() => x, - _ => hostname::get() - .map(|s| s.into_string().unwrap_or_default()) - .unwrap_or_default(), + _ => hostname_fallback(), }; let mut bytes = [0_u8; 3]; @@ -21,6 +19,19 @@ pub fn get() -> [u8; 3] { bytes } +#[cfg(not(target_arch = "wasm32"))] +fn hostname_fallback() -> String { + hostname::get() + .map(|s| s.into_string().unwrap_or_default()) + .unwrap_or_default() +} + +// `hostname` crate doesn't support wasm, so use an empty string to generate random bytes. +#[cfg(target_arch = "wasm32")] +fn hostname_fallback() -> String { + "".to_owned() +} + // https://github.com/rs/xid/blob/efa678f304ab65d6d57eedcb086798381ae22206/hostid_linux.go // Not checking "/sys/class/dmi/id/product_uuid" because normal users can't read it. #[cfg(target_os = "linux")]