KSUID
KSUIDs, Segment's K-Sortable Unique IDs: 27 base62 characters sorted by the second they were made, with 128 random bits; made, read back and checked.
Version 1.0.0 · 4 functions · 1 crate
Functions
What use ksuid.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use ksuid, ksuid_at from ksuid (Quillo 0.6).
ksuid() → strA new KSUID, such as "2W1wBpjVQS0EOmrbRBvyGrKkZ3Y": base62, safe in URLs, later ones sorting after earlier ones (to the second).
ksuid_at(time_ms: int) → stror an errorA KSUID for a time in milliseconds since 1970 (as now() gives; KSUIDs keep whole seconds, from 2014-05-13 on), the rest random: for old rows, or the first id of a time range.
ksuid_parse(id: str) → anyor an errorWhat a KSUID holds: { time_ms, payload }, time_ms when it was made (to the second) in milliseconds since 1970, payload its 16 random bytes in hex. An error when it is not a KSUID.
ksuid_valid(id: str) → boolWhether id is a KSUID: 27 base62 characters within the format's range.
Get it
$ quillo install ksuid # ksuid.qwer into this folder$ quillo install ksuid --example # and its example, ksuid.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/ksuid.qwer$ curl -O https://quillolang.org/plugins/files/ksuid.qweThen, in your program, all of it or only what you call:
use ksuid.qweruse ksuid, ksuid_at from ksuid // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
svix-ksuid = "0.10"Example
// Event ids for a webhook log: sortable by time, unique without a database sequence.//// quillo run ksuid.qwe then open http://localhost:8080/eventsuse "ksuid.qwer"route GET /events { let old = ksuid_at(1767225600000)? // 2026-01-01 let fresh = ksuid() { fresh, old, sorted: sort([fresh, old]) == [old, fresh], old_time: ksuid_parse(old)?.time_ms, valid: [ksuid_valid(fresh), ksuid_valid("nope")] }}route GET /read ? { id: str } = ksuid_parse(id)?$ quillo run ksuid.qwe$ curl 'localhost:8080/events'$ curl 'localhost:8080/read?id=0ujtsYcgvSTl8PAuAdqWYSMnLOv'Source
//! KSUIDs (K-Sortable Unique IDs, Segment's format): 27 characters, sorted by the second they were made, with 128 random bits so they never clash; made, read and checked.//! version: 1.0.0//!//! [dependencies]//! svix-ksuid = "0.10"use serde_json::{json, Value};use std::str::FromStr;use svix_ksuid::{Ksuid, KsuidLike};/// A new KSUID, such as "2W1wBpjVQS0EOmrbRBvyGrKkZ3Y": base62, safe in URLs, later ones sorting after earlier ones (to the second).pub fn ksuid() -> String { let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs() as i64); Ksuid::from_seconds(Some(secs), None).to_string()}/// A KSUID for a time in milliseconds since 1970 (as `now()` gives; KSUIDs keep whole seconds, from 2014-05-13 on), the rest random: for old rows, or the first id of a time range.pub fn ksuid_at(time_ms: i64) -> Result<String, String> { let secs = time_ms.div_euclid(1000); if !(1_400_000_000..1_400_000_000 + u32::MAX as i64).contains(&secs) { return Err("KSUID times run from 2014-05-13 to 2150".into()); } Ok(Ksuid::from_seconds(Some(secs), None).to_string())}/// What a KSUID holds: `{ time_ms, payload }`, `time_ms` when it was made (to the second) in milliseconds since 1970, `payload` its 16 random bytes in hex. An error when it is not a KSUID.pub fn ksuid_parse(id: &str) -> Result<Value, String> { let k = Ksuid::from_str(id.trim()).map_err(|e| format!("{:?} is not a KSUID: {}", id, e))?; let hex: String = k.payload().iter().map(|b| format!("{:02x}", b)).collect(); Ok(json!({ "time_ms": k.timestamp_seconds() * 1000, "payload": hex }))}/// Whether `id` is a KSUID: 27 base62 characters within the format's range.pub fn ksuid_valid(id: &str) -> bool { let t = id.trim(); t.len() == 27 && Ksuid::from_str(t).is_ok()}Related
short_ids.qwer · 1.0.0Short ids for URLs made from your database's numbers and read back (Sqids): your own alphabet, a minimum length, no rude words, and typos never reaching another row.6 views · 8 downloadsSnowflake ids snowflake.qwer · 1.0.0Snowflake ids: 64-bit numbers that sort by time, unique across 1,024 servers with no coordination, with their time, server and sequence read back and the first id of a time for range queries.7 views · 7 downloadsTypeID typeid.qwer · 1.0.0TypeIDs such as user_01h455vb4pex5vsknk084sn02q: a type prefix and a UUIDv7 in base32, made, read back to their UUID and time, and checked against the type a URL expects.6 views · 7 downloadsUUID tools uuid_tools.qwer · 1.0.0UUIDs beyond the random uuid(): version 7 sorted by time for database keys, versions 5 and 3 that give the same id for the same name, and any UUID read with its version and time.7 views · 6 downloadsCUID2 cuid2.qwer · 1.0.0CUID2 ids: secure, collision-resistant, starting with a letter, of the length you choose, that give away neither the time nor a count; and a check that a string is one.7 views · 7 downloadsISO durations iso_durations.qwer · 1.0.0ISO 8601 durations such as P1DT2H and PT30M read, written, turned into seconds and added to dates and times, months moving the calendar.7 views · 9 downloadsThread 0 posts
On KSUID: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.