Skip to content
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

uuid v7 #681

Merged
merged 23 commits into from
Jun 3, 2024
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0b8f680
feat: implement uuid7 (#580)
pmccarren Jan 22, 2023
a112083
fix: add v7.js to .local (#580)
pmccarren Jan 22, 2023
5a89a92
fix: add v7 to uuid-bin
pmccarren Jan 22, 2023
92bc63a
chore: fix readme anchor
pmccarren Jan 22, 2023
ba6d9cc
chore: use generated readme, remove timestamp arg from uuid-bin v7
pmccarren Jan 22, 2023
3a201c3
fix: typo in uuid regex, add negative test cases
pmccarren Jan 22, 2023
b710c41
fix: do not mutate provided rnds, add v7 unit tests
pmccarren Jan 22, 2023
1a61942
fix: validation test should not pass version 0
pmccarren Jan 22, 2023
bf11212
chore: update package.json description
pmccarren Jan 22, 2023
a17479a
Update src/v7.js
pmccarren Feb 4, 2023
ced5fdc
include uuid v6 and v8 in validation regex
pmccarren Feb 4, 2023
7264c2c
chore: add test:matching script to package.json
pmccarren Feb 4, 2023
2263096
fix: v7 monotonicity and lexicographical sorting
pmccarren Feb 5, 2023
4022ec2
refactor: v7 seq reinitialization
pmccarren Feb 6, 2023
1d5c88a
chore: update v7 README
pmccarren Feb 6, 2023
2626fe2
Merge branch 'uuidjs:main' into uuid7
pmccarren Feb 16, 2023
3cb9acd
chore: fix README_js.md prettier
pmccarren Feb 16, 2023
7f1ceaf
chore: render README.md
pmccarren Feb 16, 2023
33be2ef
Merge branch 'main' into pr/681
broofa Jun 2, 2024
a8dd224
chore: forgot to add updated prettier ignore file
broofa Jun 2, 2024
b9e6efa
docs: update rfc links
broofa Jun 3, 2024
874af5c
chore: regenerate README
broofa Jun 3, 2024
d587e5b
docs: update links
broofa Jun 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: v7 seq reinitialization
pmccarren committed Feb 6, 2023

Verified

This commit was signed with the committer’s verified signature.
pmccarren Patrick McCarren
commit 4022ec24a5f7206b0e2289822fbede3d6907f4da
46 changes: 23 additions & 23 deletions src/v7.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import { unsafeStringify } from './stringify.js';
* Monotonic Bit Layout:
* RFC 4122.6.2 Method 1, Dedicated Counter Bits
* ref: https://ietf-wg-uuidrev.github.io/rfc4122bis/draft-00/draft-ietf-uuidrev-rfc4122bis.html#section-6.2-5.2.1
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -44,18 +45,30 @@ function v7(options, buf, offset) {
let i = (buf && offset) || 0;
const b = buf || new Uint8Array(16);

let seqHigh = _seqHigh;
let seqLow = _seqLow;
// rnds is Uint8Array(16) filled with random bytes
const rnds = options.random || (options.rng || rng)();

// milliseconds since unix epoch, 1970-01-01 00:00
const msecs = options.msecs !== undefined ? options.msecs : Date.now();

// rnds is Uint8Array(16) filled with random bytes
const rnds = options.random || (options.rng || rng)();

// seq is user provided seq
// seq is user provided 31 bit counter
let seq = options.seq !== undefined ? options.seq : null;

// initialize local seq high/low parts
let seqHigh = _seqHigh;
let seqLow = _seqLow;

// check if clock has advanced and user has not provided msecs
if (msecs > _msecs && options.msecs === undefined) {
_msecs = msecs;

// unless user provided seq, reset seq parts
if (seq !== null) {
seqHigh = null;
seqLow = null;
}
}

// if we have a user provided seq
if (seq !== null) {
// trim provided seq to 31 bits of value, avoiding overflow
@@ -67,33 +80,20 @@ function v7(options, buf, offset) {
seqHigh = (seq >>> 19) & 0xfff;
seqLow = seq & 0x7ffff;
}
// else check if clock has advanced
else if (msecs !== _msecs) {
// reinitialize seq
seqHigh = null;
seqLow = null;
}

// randomly initialize seq if empty
if (seqHigh === null) {
// randomly initialize seq
if (seqHigh === null || seqLow === null) {
seqHigh = rnds[6] & 0x7f;
seqHigh = (seqHigh << 8) | rnds[7];
}

if (seqLow === null) {
seqLow = rnds[8] & 0x3f; // pad for var
seqLow = (seqLow << 8) | rnds[9];
seqLow = (seqLow << 5) | (rnds[10] >>> 3);
}

// check if clock has advanced or user provided seq
if (msecs > _msecs || seq !== null) {
_msecs = msecs;
}
// increment seq if within msecs window
else if (msecs + 10000 > _msecs) {
seqLow += 1; // increment our seq
if (seqLow > 0x7ffff) {
if (msecs + 10000 > _msecs && seq === null) {
if (++seqLow > 0x7ffff) {
seqLow = 0;

if (++seqHigh > 0xfff) {