-
Notifications
You must be signed in to change notification settings - Fork 657
feat(uuid/unstable): implement support for UUID V6 - refs #6414 #6415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,183 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { bytesToUuid } from "./_common.ts"; | ||
|
||
const UUID_RE = | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||
|
||
/** | ||
* Determines whether a string is a valid | ||
* {@link https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-6 | UUIDv6}. | ||
* | ||
* @param id UUID value. | ||
* | ||
* @returns `true` if the string is a valid UUIDv6, otherwise `false`. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { validate } from "@std/uuid/unstable-v6"; | ||
* import { assert, assertFalse } from "@std/assert"; | ||
* | ||
* assert(validate("1efed67d-d966-6490-8b9a-755015853480")); | ||
* assertFalse(validate("1efed67d-d966-1490-8b9a-755015853480")); | ||
* ``` | ||
*/ | ||
export function validate(id: string): boolean { | ||
return UUID_RE.test(id); | ||
} | ||
|
||
let _lastMSecs = 0; | ||
let _lastNSecs = 0; | ||
|
||
/** | ||
* Options for {@linkcode generate}. | ||
*/ | ||
export interface GenerateOptions { | ||
/** | ||
* An array of 6 bytes that represents the node bits for the UUID. | ||
* | ||
* If not set, a random value will be generated. | ||
* | ||
* @see {@link https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-6} | ||
*/ | ||
node?: number[]; | ||
/** | ||
* A 14-bit value used to avoid duplicates that could arise when the clock is | ||
* set backwards in time or if the node ID changes (0 - 16383). | ||
* | ||
* If not set, a random value will be generated. | ||
* | ||
* @see {@link https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-6} | ||
*/ | ||
clockseq?: number; | ||
/** | ||
* The number of milliseconds since the Unix epoch (January 1, 1970). | ||
* | ||
* @see {@link https://www.rfc-editor.org/rfc/rfc9562.html#name-timestamp-considerations} | ||
*/ | ||
msecs?: number; | ||
/** | ||
* The number of nanoseconds to add to {@linkcode GenerateOptions.msecs} | ||
* (0 - 10,000). | ||
* | ||
* @see {@link https://www.rfc-editor.org/rfc/rfc9562.html#name-timestamp-considerations} | ||
*/ | ||
nsecs?: number; | ||
/** | ||
* An array of 8 random bytes (0 - 255) to be used for node and clock sequence | ||
* bits (unless they are set). | ||
*/ | ||
random?: number[]; | ||
/** | ||
* A function that returns an array of 8 random bytes (0 - 255). | ||
* Alternative to {@linkcode GenerateOptions.random}. | ||
*/ | ||
rng?: () => number[]; | ||
} | ||
|
||
/** | ||
* Generates a | ||
* {@link https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-6 | UUIDv6}. | ||
* | ||
* @param options Can use RFC time sequence values as overwrites. | ||
* | ||
* @returns Returns a UUIDv6 string. | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { generate, validate } from "@std/uuid/unstable-v6"; | ||
* import { assert } from "@std/assert"; | ||
* | ||
* const options = { | ||
* node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab], | ||
* clockseq: 0x1234, | ||
* msecs: new Date("2011-11-01").getTime(), | ||
* nsecs: 5678, | ||
* }; | ||
* | ||
* const uuid = generate(options); | ||
* assert(validate(uuid as string)); | ||
* ``` | ||
*/ | ||
export function generate(options: GenerateOptions = {}): string { | ||
let i = 0; | ||
const b: number[] = []; | ||
|
||
let { node, clockseq } = options; | ||
|
||
if (node === undefined || clockseq === undefined) { | ||
// deno-lint-ignore no-explicit-any | ||
const seedBytes: any = options.random ?? | ||
(options.rng ? options.rng() : undefined) ?? | ||
crypto.getRandomValues(new Uint8Array(8)); | ||
|
||
if (node === undefined) { | ||
node = [ | ||
seedBytes[0], | ||
seedBytes[1], | ||
seedBytes[2], | ||
seedBytes[3], | ||
seedBytes[4], | ||
seedBytes[5], | ||
]; | ||
} | ||
|
||
if (clockseq === undefined) { | ||
clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff; | ||
} | ||
} | ||
|
||
let { msecs = new Date().getTime(), nsecs = _lastNSecs + 1 } = options; | ||
|
||
const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; | ||
|
||
if (dt < 0 && options.clockseq === undefined) { | ||
clockseq = (clockseq + 1) & 0x3fff; | ||
} | ||
|
||
if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) { | ||
nsecs = 0; | ||
} | ||
|
||
if (nsecs > 10000) { | ||
throw new Error("Can't create more than 10M uuids/sec"); | ||
} | ||
|
||
if (node.length !== 6) { | ||
throw new Error( | ||
"Cannot create UUID: the node option must be an array of 6 bytes", | ||
); | ||
} | ||
|
||
_lastMSecs = msecs; | ||
_lastNSecs = nsecs; | ||
|
||
// We have to add this value because "msecs" here is the number of | ||
// milliseconds since January 1, 1970, not since October 15, 1582. | ||
// This is also the milliseconds from October 15, 1582 to January 1, 1970. | ||
msecs += 12219292800000; | ||
|
||
const th = ((msecs / 0x10000000) * 10000) & 0xffffffff; | ||
b[i++] = (th >>> 24) & 0xff; | ||
b[i++] = (th >>> 16) & 0xff; | ||
b[i++] = (th >>> 8) & 0xff; | ||
b[i++] = th & 0xff; | ||
|
||
const tml = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x10000000; | ||
b[i++] = (tml >>> 20) & 0xff; | ||
b[i++] = (tml >>> 12) & 0xff; | ||
|
||
b[i++] = (tml >>> 8) & 0xf | 0x60; | ||
b[i++] = tml & 0xff; | ||
|
||
b[i++] = (clockseq >>> 8) | 0x80; | ||
|
||
b[i++] = clockseq & 0xff; | ||
|
||
for (let n = 0; n < 6; ++n) { | ||
b[i + n] = node[n]!; | ||
} | ||
|
||
return bytesToUuid(b); | ||
} |
This file contains hidden or 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,109 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
import { assert, assertEquals, assertThrows } from "@std/assert"; | ||
import { generate, type GenerateOptions, validate } from "./unstable_v6.ts"; | ||
|
||
Deno.test("validate() checks if a string is a valid v6 UUID", () => { | ||
const u = generate(); | ||
const t = "1efed67d-d966-6490-8b9a-755015853480"; | ||
const n = "1efed67d-d966-1490-8b9a-755015853480"; | ||
|
||
assert(validate(u as string), `generated ${u} should be valid`); | ||
assert(validate(t), `${t} should be valid`); | ||
assert(!validate(n), `${n} should not be valid`); | ||
}); | ||
|
||
Deno.test("generate() generates a non-empty string", () => { | ||
const u1 = generate(); | ||
const u2 = generate({ | ||
msecs: new Date("2011-11-01").getTime(), | ||
nsecs: 10000, | ||
}); | ||
|
||
assertEquals(typeof u1, "string", "returns a string"); | ||
assert(u1 !== "", "return string is not empty"); | ||
assertEquals(typeof u2, "string", "returns a string"); | ||
assert(u2 !== "", "return string is not empty"); | ||
}); | ||
|
||
Deno.test("generate() generates UUIDs in version 6 format", () => { | ||
for (let i = 0; i < 10000; i++) { | ||
const u = generate() as string; | ||
assert(validate(u), `${u} is not a valid uuid v6`); | ||
} | ||
}); | ||
|
||
Deno.test("generate() can generate a static v6 UUID", () => { | ||
const v6options = { | ||
node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab], | ||
clockseq: 0x385c, | ||
msecs: new Date("2011-11-18T21:25:33.573+00:00").getTime(), | ||
nsecs: 5442, | ||
}; | ||
const u = generate(v6options); | ||
assertEquals(u, "1e1122bd-9428-6892-b85c-0123456789ab"); | ||
}); | ||
|
||
Deno.test("generate() throws when node is passed with less than 6 numbers", () => { | ||
assertThrows( | ||
() => { | ||
generate({ node: [0x01, 0x23, 0x45, 0x67, 0x89] }); | ||
}, | ||
Error, | ||
"Cannot create UUID: the node option must be an array of 6 bytes", | ||
); | ||
}); | ||
|
||
Deno.test("generate() throws when node is passed with more than 6 numbers", () => { | ||
assertThrows( | ||
() => { | ||
generate({ node: [0x01, 0x23, 0x45, 0x67, 0x89, 0x89, 0x89] }); | ||
}, | ||
Error, | ||
"Cannot create UUID: the node option must be an array of 6 bytes", | ||
); | ||
}); | ||
|
||
Deno.test("generate() throws when create more than 10M uuids/sec", () => { | ||
assertThrows( | ||
() => { | ||
generate({ nsecs: 10001 }); | ||
}, | ||
Error, | ||
"Can't create more than 10M uuids/sec", | ||
); | ||
}); | ||
|
||
Deno.test("generate() uses provided rng function", () => { | ||
const v6options: GenerateOptions = { | ||
msecs: new Date("2011-11-18T21:25:33.573+00:00").getTime(), | ||
nsecs: 5442, | ||
rng: () => [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0x2d, 0xef], | ||
}; | ||
const u = generate(v6options); | ||
assertEquals(u, "1e1122bd-9428-6892-adef-0123456789ab"); | ||
}); | ||
|
||
Deno.test("generate() uses provided random data", () => { | ||
const v6options: GenerateOptions = { | ||
msecs: new Date("2011-11-18T21:25:33.573+00:00").getTime(), | ||
nsecs: 5442, | ||
random: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0x2d, 0xef], | ||
}; | ||
const u = generate(v6options); | ||
assertEquals(u, "1e1122bd-9428-6892-adef-0123456789ab"); | ||
}); | ||
|
||
Deno.test("generate() defaults to random node and clock sequence", () => { | ||
const numGenerate = 1_000; | ||
const v6options: GenerateOptions = { | ||
msecs: new Date("2011-11-18T21:25:33.573+00:00").getTime(), | ||
nsecs: 5342, | ||
}; | ||
const uuids: string[] = []; | ||
|
||
for (let i = 0; i < numGenerate; i++) { | ||
const u = generate(v6options); | ||
assert(!uuids.includes(u), "Duplicate random data detected"); | ||
uuids.push(u); | ||
} | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍