WebAssembly runs code at near-native speed in browsers, edge runtimes (Cloudflare Workers, Fastly Compute), and standalone runtimes (Wasmtime, WasmEdge). UUID v7 generation in WASM requires two things: a secure random source and wall-clock time. WASI provides both. See UUID in Edge Functions for the edge deployment context.
Rust UUID v7 compiled to WASM
# Cargo.toml
[package]
name = "uuid-wasm"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
uuid = { version = "1", features = ["v7"] }
wasm-bindgen = "0.2"
getrandom = { version = "0.2", features = ["js"] } # For browser WASM
// src/lib.rs
use uuid::Uuid;
use wasm_bindgen::prelude::*;
/// Generate a UUID v7 string from WebAssembly.
#[wasm_bindgen]
pub fn generate_uuid_v7() -> String {
Uuid::now_v7().to_string()
}
/// Extract creation timestamp (ms) from a UUID v7 string.
#[wasm_bindgen]
pub fn extract_timestamp_ms(uuid_str: &str) -> Result<f64, JsValue> {
let uuid = Uuid::parse_str(uuid_str)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
// Top 48 bits of UUID v7 = milliseconds since Unix epoch
let bytes = uuid.as_bytes();
let ms = ((bytes[0] as u64) << 40)
| ((bytes[1] as u64) << 32)
| ((bytes[2] as u64) << 24)
| ((bytes[3] as u64) << 16)
| ((bytes[4] as u64) << 8)
| (bytes[5] as u64);
Ok(ms as f64)
}
Build for browser:
wasm-pack build --target web
JavaScript usage
import init, { generate_uuid_v7, extract_timestamp_ms } from './pkg/uuid_wasm.js';
await init(); // Initialize the WASM module
const id = generate_uuid_v7();
console.log(id); // '018fbe3a-4c5d-7b12-8f3e-1a2b3c4d5e6f'
const createdAt = extract_timestamp_ms(id);
console.log(new Date(createdAt)); // Creation time
WASI target (Wasmtime / WasmEdge)
# Cargo.toml — no wasm-bindgen needed for WASI
[dependencies]
uuid = { version = "1", features = ["v7"] }
# getrandom uses WASI random_get automatically for wasm32-wasip1
// src/main.rs — standalone WASI binary
use uuid::Uuid;
fn main() {
// Generate UUID v7 using WASI clock_time_get + random_get
let id = Uuid::now_v7();
println!("{}", id);
}
# Build for WASI
cargo build --target wasm32-wasip1 --release
# Run with Wasmtime
wasmtime target/wasm32-wasip1/release/uuid-wasm.wasm
# 018fbe3a-4c5d-7b12-8f3e-1a2b3c4d5e6f
Cloudflare Workers with WASM UUID v7
// worker.js — load pre-compiled WASM bundle
import wasm from './uuid_wasm_bg.wasm';
import { generate_uuid_v7 } from './uuid_wasm.js';
export default {
async fetch(request, env) {
// UUID v7 generated inside the WASM module
const traceId = generate_uuid_v7();
return new Response(JSON.stringify({ traceId }), {
headers: {
'Content-Type': 'application/json',
'X-Trace-ID': traceId,
},
});
},
};
Component Model (WASM future)
The WebAssembly Component Model (WASIp2) will standardize UUID generation as a composable interface. Until then, the pattern above (WASI random_get + clock_time_get) is the correct approach for UUID v7 in WASM modules.
Further reading
- UUID in Edge Functions — Cloudflare Workers, Vercel, Deno
- UUID in Rust — uuid Crate Guide
- CSPRNG — Secure Random Number Generator for UUIDs
- UUID v7 in Deno KV
- UUID v7 in Hono — Edge-First APIs
External references
Frequently asked questions
Can WebAssembly generate UUID v7?
Yes. WASI (WebAssembly System Interface) provides random_get for secure random bytes and clock_time_get for wall-clock time — both needed for UUID v7. Rust's uuid crate compiled to wasm32-wasip1 uses these WASI imports automatically. In the browser (non-WASI), use crypto.getRandomValues() via a JavaScript import. See CSPRNG — Secure Random Number Generator for UUIDs.
How does Rust's uuid crate work when compiled to WebAssembly?
The Rust uuid crate uses the getrandom crate for random bytes. When targeting wasm32-wasip1, getrandom calls WASI's random_get. When targeting wasm32-unknown-unknown (browser), you must enable getrandom's js feature to use the Web Crypto API. Enable with: getrandom = { version = "0.2", features = ["js"] } in Cargo.toml.
How do I call a UUID v7 WASM module from JavaScript?
Compile your Rust module to wasm32-wasip1 and use wasm-bindgen or wasm-pack to generate JavaScript bindings. Export a function returning a string (the UUID). In JavaScript: const { generate_uuid_v7 } = await import('./pkg/mymodule.js'); const id = generate_uuid_v7();. UUID strings are passed as UTF-8 across the WASM boundary.