1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Source: ~/claudecode/openclaudecode/src/utils/crypto.ts
//! Indirection point for the package.json "browser" field. When bun builds
//! browser-sdk.js with --target browser, this file is swapped for
//! crypto.browser.ts — avoiding a ~500KB crypto-browserify polyfill that Bun
//! would otherwise inline for `import ... from 'crypto'`. Node/bun builds use
//! this file unchanged.
//!
//! NOTE: `export { randomUUID } from 'crypto'` (re-export syntax) breaks under
//! bun-internal's bytecode compilation — the generated bytecode shows the
//! import but the binding doesn't link (`ReferenceError: randomUUID is not
//! defined`). The explicit import-then-export below produces a correct live
//! binding.
#![allow(dead_code)]
pub use uuid::Uuid;
/// Generate a random UUID (v4).
pub fn random_uuid() -> String {
Uuid::new_v4().to_string()
}
/// Generate a random UUID and return it as a hyphenated string.
pub fn random_uuid_hyphenated() -> String {
Uuid::new_v4().hyphenated().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_random_uuid_format() {
let uuid = random_uuid();
// UUID v4 format: 8-4-4-4-12 hex chars
let parts: Vec<&str> = uuid.split('-').collect();
assert_eq!(parts.len(), 5);
assert_eq!(parts[0].len(), 8);
assert_eq!(parts[1].len(), 4);
assert_eq!(parts[2].len(), 4);
assert_eq!(parts[3].len(), 4);
assert_eq!(parts[4].len(), 12);
}
#[test]
fn test_random_uuid_unique() {
let uuid1 = random_uuid();
let uuid2 = random_uuid();
assert_ne!(uuid1, uuid2);
}
}