Skip to content

Commit

Permalink
fix wasm support for cuid2, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mplanchard committed Jan 8, 2025
1 parent cbbbdd6 commit 780f6a7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ jobs:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v2"
- uses: "jetli/[email protected]"
- uses: "actions-rs/cargo@v1"
with:
command: "build"
args: "--target ${{ matrix.target }}"
- run: "cd crates/cuid2 && wasm-pack test --node"

lint:
name: "Lint"
Expand Down
7 changes: 5 additions & 2 deletions crates/cuid2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ rand.workspace = true
sha3 = "0.10.6"

[dev-dependencies]
radix_fmt = "1.0.0"
wasm-bindgen-test.workspace = true

[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
criterion.workspace = true
num_cpus = "1.15.0"
proptest.workspace = true
radix_fmt = "1.0.0"
wasm-bindgen-test.workspace = true

[target.wasm32-unknown-unknown.dependencies]
# Just specified so we can add a feature when the js feature is enabled.
# This works fine on wasm targets other than unknown-unknown
getrandom = { version = "0", features = ["js"] }
web-time = "1.1.0"
37 changes: 27 additions & 10 deletions crates/cuid2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ use std::{
cell::RefCell,
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
time::{SystemTime, UNIX_EPOCH},
};

#[cfg(not(target_family = "wasm"))]
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(target_family = "wasm")]
use web_time::{SystemTime, UNIX_EPOCH};

Check failure on line 66 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / WASM Builds (wasm32-wasi)

unresolved import `web_time`

use cuid_util::ToBase36;
use num::bigint;
use rand::{seq::SliceRandom, thread_rng, Rng};
Expand All @@ -85,6 +89,21 @@ const STARTING_CHARS: &str = "abcdefghijklmnopqrstuvwxyz";
// - fingerprint, a hash with added entropy, derived from a random number between
// 2063 and 4125, inclusive, the process ID, and the thread ID

fn fingerprint() -> String {
hash(
[
thread_rng().gen::<u128>().to_be_bytes(),
thread_rng().gen::<u128>().to_be_bytes(),
#[cfg(not(target_family = "wasm"))]
u128::from(std::process::id()).to_be_bytes(),
#[cfg(target_family = "wasm")]
thread_rng().gen::<u128>().to_be_bytes(),
u128::from(get_thread_id()).to_be_bytes(),
],
BIG_LENGTH.into(),
)
}

thread_local! {
/// Value used to initialize the counter. After the counter hits u64::MAX, it
/// will roll back to this value.
Expand All @@ -109,15 +128,7 @@ thread_local! {
///
/// This is pretty non-language, non-system dependent, so it allows us to
/// compile to wasm and so on.
static FINGERPRINT: String = hash(
[
thread_rng().gen::<u128>().to_be_bytes(),
thread_rng().gen::<u128>().to_be_bytes(),
u128::from(std::process::id()).to_be_bytes(),
u128::from(get_thread_id()).to_be_bytes(),
],
BIG_LENGTH.into(),
);
static FINGERPRINT: String = fingerprint();
}

// Hashing
Expand Down Expand Up @@ -462,6 +473,12 @@ mod test {
assert!(next > start);
}

#[wasm_bindgen_test::wasm_bindgen_test]
fn wasm_cuid_does_not_panic() {

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, beta, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, nightly, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, beta, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, stable, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, nightly, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, beta, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, stable, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, nightly, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, beta, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, beta, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, nightly, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, nightly, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable, -p cuid2)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, beta, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, nightly, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable, -p cuid2 -- --ignored test::distribution)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, nightly, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, stable, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, beta, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, beta, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, nightly, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, beta, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, nightly, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used

Check warning on line 477 in crates/cuid2/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable, -p cuid2 -- --ignored test::collisions)

function `wasm_cuid_does_not_panic` is never used
cuid();
}

#[cfg(not(target_family = "wasm"))]
#[test]
#[ignore] // slow: run explicitly when desired
fn collisions() {
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
rust-analyzer
shellcheck
util-linux # lspcu utility for getting info about cores
wasm-pack
];
};
});
Expand Down

0 comments on commit 780f6a7

Please sign in to comment.