-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clearer custom timestamp generation logic
- Loading branch information
Showing
3 changed files
with
110 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { addHyphens, genRandParts } from "./utils"; | ||
|
||
export class TimestampUUIDv7 { | ||
#lastTimestamp: number = -1; | ||
#lastRandA: number; | ||
#lastRandB: bigint; | ||
|
||
/** | ||
* Generates a new UUIDv7 with custom timestamp. | ||
* | ||
* @param timestamp Custom timestamp in milliseconds | ||
* @returns | ||
*/ | ||
gen(timestamp: number) { | ||
if (timestamp < 0 || timestamp > 2 ** 48 - 1) { | ||
throw new Error("uuidv7 gen error: custom timestamp must be between 0 and 2 ** 48 - 1"); | ||
} | ||
|
||
let uuid = 0n; | ||
let randA: number; | ||
let randB: bigint; | ||
|
||
if (timestamp !== this.#lastTimestamp) { | ||
const parts = genRandParts(); | ||
randA = parts.randA; | ||
randB = parts.randB; | ||
} else { | ||
// Keep the same [rand_a] part by default. | ||
randA = this.#lastRandA; | ||
|
||
// Random increment value is between 1 and 2 ** 32 (4,294,967,296). | ||
randB = this.#lastRandB + BigInt(crypto.getRandomValues(new Uint32Array(1))[0]! + 1); | ||
|
||
// In the rare case that [rand_b] overflows its 62 bits after the increment, | ||
if (randB > 2n ** 62n - 1n) { | ||
const newParts = genRandParts(); | ||
// When [rand_b] overflows its 62 bits, always generate a new random part for it. | ||
randB = newParts.randB; | ||
|
||
// this will use [rand_a] part as an additional counter, incrementing it by 1. | ||
randA = randA + 1; | ||
|
||
// If the [rand_a] part overflows its 12 bits, use a new value for it. | ||
if (randA > 2 ** 12 - 1) { | ||
randA = newParts.randA; | ||
} | ||
} | ||
} | ||
|
||
// [unix_ts_ms] timestamp in milliseconds - 48 bits | ||
uuid = BigInt(timestamp) << 80n; | ||
|
||
// [ver] version "7" - 4 bits | ||
uuid = uuid | (0b0111n << 76n); | ||
|
||
// [rand_a] secondary randomly seeded counter - 12 bits | ||
uuid = uuid | (BigInt(randA) << 64n); | ||
|
||
// [var] variant 0b10 - 2 bits | ||
uuid = uuid | (0b10n << 62n); | ||
|
||
// [rand_b] primary randomly seeded counter - 62 bits | ||
uuid = uuid | randB; | ||
|
||
this.#lastRandA = randA; | ||
this.#lastRandB = randB; | ||
this.#lastTimestamp = timestamp; | ||
|
||
return addHyphens(uuid.toString(16).padStart(32, "0")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export function addHyphens(id: string) { | ||
return ( | ||
id.substring(0, 8) + | ||
"-" + | ||
id.substring(8, 12) + | ||
"-" + | ||
id.substring(12, 16) + | ||
"-" + | ||
id.substring(16, 20) + | ||
"-" + | ||
id.substring(20) | ||
); | ||
} | ||
|
||
// Generates the 12 [rand_a] and 62 bits [rand_b] parts in number and bigint formats. | ||
export function genRandParts() { | ||
const v4 = crypto.randomUUID(); | ||
|
||
return { | ||
randA: parseInt(v4.slice(15, 18), 16), | ||
randB: BigInt("0x" + v4.replace(/-/g, "")) & ((1n << 62n) - 1n), | ||
}; | ||
} |