Skip to content

Commit 780f6a7

Browse files
committed
fix wasm support for cuid2, add test
1 parent cbbbdd6 commit 780f6a7

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ jobs:
5050
runs-on: "ubuntu-latest"
5151
steps:
5252
- uses: "actions/checkout@v2"
53+
- uses: "jetli/[email protected]"
5354
- uses: "actions-rs/cargo@v1"
5455
with:
5556
command: "build"
5657
args: "--target ${{ matrix.target }}"
58+
- run: "cd crates/cuid2 && wasm-pack test --node"
5759

5860
lint:
5961
name: "Lint"

crates/cuid2/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ rand.workspace = true
3030
sha3 = "0.10.6"
3131

3232
[dev-dependencies]
33+
radix_fmt = "1.0.0"
34+
wasm-bindgen-test.workspace = true
35+
36+
[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
3337
criterion.workspace = true
3438
num_cpus = "1.15.0"
3539
proptest.workspace = true
36-
radix_fmt = "1.0.0"
37-
wasm-bindgen-test.workspace = true
3840

3941
[target.wasm32-unknown-unknown.dependencies]
4042
# Just specified so we can add a feature when the js feature is enabled.
4143
# This works fine on wasm targets other than unknown-unknown
4244
getrandom = { version = "0", features = ["js"] }
45+
web-time = "1.1.0"

crates/cuid2/src/lib.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ use std::{
5858
cell::RefCell,
5959
collections::hash_map::DefaultHasher,
6060
hash::{Hash, Hasher},
61-
time::{SystemTime, UNIX_EPOCH},
6261
};
6362

63+
#[cfg(not(target_family = "wasm"))]
64+
use std::time::{SystemTime, UNIX_EPOCH};
65+
#[cfg(target_family = "wasm")]
66+
use web_time::{SystemTime, UNIX_EPOCH};
67+
6468
use cuid_util::ToBase36;
6569
use num::bigint;
6670
use rand::{seq::SliceRandom, thread_rng, Rng};
@@ -85,6 +89,21 @@ const STARTING_CHARS: &str = "abcdefghijklmnopqrstuvwxyz";
8589
// - fingerprint, a hash with added entropy, derived from a random number between
8690
// 2063 and 4125, inclusive, the process ID, and the thread ID
8791

92+
fn fingerprint() -> String {
93+
hash(
94+
[
95+
thread_rng().gen::<u128>().to_be_bytes(),
96+
thread_rng().gen::<u128>().to_be_bytes(),
97+
#[cfg(not(target_family = "wasm"))]
98+
u128::from(std::process::id()).to_be_bytes(),
99+
#[cfg(target_family = "wasm")]
100+
thread_rng().gen::<u128>().to_be_bytes(),
101+
u128::from(get_thread_id()).to_be_bytes(),
102+
],
103+
BIG_LENGTH.into(),
104+
)
105+
}
106+
88107
thread_local! {
89108
/// Value used to initialize the counter. After the counter hits u64::MAX, it
90109
/// will roll back to this value.
@@ -109,15 +128,7 @@ thread_local! {
109128
///
110129
/// This is pretty non-language, non-system dependent, so it allows us to
111130
/// compile to wasm and so on.
112-
static FINGERPRINT: String = hash(
113-
[
114-
thread_rng().gen::<u128>().to_be_bytes(),
115-
thread_rng().gen::<u128>().to_be_bytes(),
116-
u128::from(std::process::id()).to_be_bytes(),
117-
u128::from(get_thread_id()).to_be_bytes(),
118-
],
119-
BIG_LENGTH.into(),
120-
);
131+
static FINGERPRINT: String = fingerprint();
121132
}
122133

123134
// Hashing
@@ -462,6 +473,12 @@ mod test {
462473
assert!(next > start);
463474
}
464475

476+
#[wasm_bindgen_test::wasm_bindgen_test]
477+
fn wasm_cuid_does_not_panic() {
478+
cuid();
479+
}
480+
481+
#[cfg(not(target_family = "wasm"))]
465482
#[test]
466483
#[ignore] // slow: run explicitly when desired
467484
fn collisions() {

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
rust-analyzer
4444
shellcheck
4545
util-linux # lspcu utility for getting info about cores
46+
wasm-pack
4647
];
4748
};
4849
});

0 commit comments

Comments
 (0)