Skip to content

Commit

Permalink
Support wasm target
Browse files Browse the repository at this point in the history
  • Loading branch information
kazk committed Oct 3, 2022
1 parent 6e55e98 commit 8038e5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
17 changes: 14 additions & 3 deletions src/machine_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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")]
Expand Down

0 comments on commit 8038e5c

Please sign in to comment.