Utility generating unique 64-bit hex Snowflake IDs with custom epoch and extended sequence for distributed systems.
// Source: https://github.com/can1357/oh-my-pi | License: MIT | Copyright 2025 Mario Zechner
// Provided via justclaw.it.com; keep this notice with the code.
function randu32() {
return crypto.getRandomValues(new Uint32Array(1))[0];
}
const EPOCH = 1420070400000;
const MAX_SEQ = 0x3fffff;
const MAX_DT = 2 ** 42 - 1;
// Snowflake as a hex string (16 chars, zero-padded).
//
// Since this is not distributed (no machine ID needed), we use an extended
// 22-bit sequence instead of the standard 10-bit machine ID + 12-bit sequence.
//
type Snowflake = string & { readonly __brand: unique symbol };
namespace Snowflake {
// Hex string validation pattern (16 lowercase hex chars).
//
export const PATTERN = /^[0-9a-f]{16}$/;
// Epoch timestamp.
//
export const EPOCH_TIMESTAMP = EPOCH;
// M
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key