diff --git a/README.md b/README.md index b9079b69..ccfb59eb 100644 --- a/README.md +++ b/README.md @@ -338,10 +338,10 @@ Create an RFC version 7 (random) UUID | | | | --- | --- | | [`options`] | `Object` with one or more of the following properties: | -| [`options.msecs`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) | +| [`options.msecs`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch). Default = `Date.now()` | | [`options.random`] | `Array` of 16 random bytes (0-255) | | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) | -| [`options.seq`] | 31 bit monotonic sequence counter as `Number` between 0 - 0x7fffffff | +| [`options.seq`] | 31-bit sequence `Number` between 0 - 0xffffffff. This may be provided to help insure uniqueness for UUIDs generated within the same millisecond time interval. Default = random value. | | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` | | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` | | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` | diff --git a/README_js.md b/README_js.md index fc7a1e4c..d7a0a3fc 100644 --- a/README_js.md +++ b/README_js.md @@ -346,10 +346,10 @@ Create an RFC version 7 (random) UUID | | | | --- | --- | | [`options`] | `Object` with one or more of the following properties: | -| [`options.msecs`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) | +| [`options.msecs`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch). Default = `Date.now()` | | [`options.random`] | `Array` of 16 random bytes (0-255) | | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) | -| [`options.seq`] | 31 bit monotonic sequence counter as `Number` between 0 - 0x7fffffff | +| [`options.seq`] | 31-bit sequence `Number` between 0 - 0xffffffff. This may be provided to help insure uniqueness for UUIDs generated within the same millisecond time interval. Default = random value. | | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` | | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` | | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` | diff --git a/package.json b/package.json index f844f7fa..5d482d63 100644 --- a/package.json +++ b/package.json @@ -115,8 +115,8 @@ "test:browser": "wdio run ./wdio.conf.js", "test:node": "npm-run-all --parallel examples:node:**", "test:pack": "./scripts/testpack.sh", - "test:watch": "node --test --watch dist/esm/test", - "test": "node --test dist/esm/test" + "test:watch": "node --test --enable-source-maps --watch dist/esm/test", + "test": "node --test --enable-source-maps dist/esm/test" }, "repository": { "type": "git", diff --git a/src/test/v7.test.ts b/src/test/v7.test.ts index 98bf2bf5..ac0bcb20 100644 --- a/src/test/v7.test.ts +++ b/src/test/v7.test.ts @@ -1,72 +1,39 @@ import * as assert from 'assert'; import test, { describe } from 'node:test'; import { Version7Options } from '../_types.js'; -import v7 from '../v7.js'; +import parse from '../parse.js'; import stringify from '../stringify.js'; +import v7, { updateV7State } from '../v7.js'; -/** - * fixture bit layout: - * ref: https://www.rfc-editor.org/rfc/rfc9562.html#name-example-of-a-uuidv7-value - * - * expectedBytes was calculated using this script: - * https://gist.github.com/d5382ac3a1ce4ba9ba40a90d9da8cbf1 - * - * ------------------------------- - * field bits value - * ------------------------------- - * unix_ts_ms 48 0x17F22E279B0 - * ver 4 0x7 - * rand_a 12 0xCC3 - * var 2 b10 - * rand_b 62 b01, 0x8C4DC0C0C07398F - * ------------------------------- - * total 128 - * ------------------------------- - * final: 017f22e2-79b0-7cc3-98c4-dc0c0c07398f - */ +// Fixture values for testing with the rfc v7 UUID example: +// https://www.rfc-editor.org/rfc/rfc9562.html#name-example-of-a-uuidv7-value +const RFC_V7 = '017f22e2-79b0-7cc3-98c4-dc0c0c07398f'; +const RFC_V7_BYTES = parse('017f22e2-79b0-7cc3-98c4-dc0c0c07398f'); +const RFC_MSECS = 0x17f22e279b0; -describe('v7', () => { - const msecsFixture = 1645557742000; - const seqFixture = 0x661b189b; - - const randomBytesFixture = Uint8Array.of( - 0x10, - 0x91, - 0x56, - 0xbe, - 0xc4, - 0xfb, - 0x0c, - 0xc3, - 0x18, - 0xc4, - 0xdc, - 0x0c, - 0x0c, - 0x07, - 0x39, - 0x8f - ); - - const expectedBytes = Uint8Array.of( - 1, - 127, - 34, - 226, - 121, - 176, - 124, - 195, - 152, - 196, - 220, - 12, - 12, - 7, - 57, - 143 - ); +// `seq` and `random` values needed to create the above RFC uuid. These are +// specific to our implementation here, and are not part of the RFC. +const RFC_SEQ = 0x661b189b; +const RFC_RANDOM = Uint8Array.of( + 0x10, + 0x91, + 0x56, + 0xbe, + 0xc4, + 0xfb, + 0x0c, + 0xc3, + 0x18, + 0xc4, + 0xdc, + 0x0c, + 0x0c, + 0x07, + 0x39, + 0x8f +); +describe('v7', () => { test('subsequent UUIDs are different', () => { const id1 = v7(); const id2 = v7(); @@ -75,25 +42,25 @@ describe('v7', () => { test('explicit options.random and options.msecs produces expected result', () => { const id = v7({ - random: randomBytesFixture, - msecs: msecsFixture, - seq: seqFixture, + random: RFC_RANDOM, + msecs: RFC_MSECS, + seq: RFC_SEQ, }); - assert.strictEqual(id, '017f22e2-79b0-7cc3-98c4-dc0c0c07398f'); + assert.strictEqual(id, RFC_V7); }); test('explicit options.rng produces expected result', () => { const id = v7({ - rng: () => randomBytesFixture, - msecs: msecsFixture, - seq: seqFixture, + rng: () => RFC_RANDOM, + msecs: RFC_MSECS, + seq: RFC_SEQ, }); - assert.strictEqual(id, '017f22e2-79b0-7cc3-98c4-dc0c0c07398f'); + assert.strictEqual(id, RFC_V7); }); test('explicit options.msecs produces expected result', () => { const id = v7({ - msecs: msecsFixture, + msecs: RFC_MSECS, }); assert.strictEqual(id.indexOf('017f22e2'), 0); }); @@ -102,13 +69,15 @@ describe('v7', () => { const buffer = new Uint8Array(16); const result = v7( { - random: randomBytesFixture, - msecs: msecsFixture, - seq: seqFixture, + random: RFC_RANDOM, + msecs: RFC_MSECS, + seq: RFC_SEQ, }, buffer ); - assert.deepEqual(buffer, expectedBytes); + stringify(buffer); + + assert.deepEqual(buffer, RFC_V7_BYTES); assert.strictEqual(buffer, result); }); @@ -117,25 +86,25 @@ describe('v7', () => { v7( { - random: randomBytesFixture, - msecs: msecsFixture, - seq: seqFixture, + random: RFC_RANDOM, + msecs: RFC_MSECS, + seq: RFC_SEQ, }, buffer, 0 ); v7( { - random: randomBytesFixture, - msecs: msecsFixture, - seq: seqFixture, + random: RFC_RANDOM, + msecs: RFC_MSECS, + seq: RFC_SEQ, }, buffer, 16 ); const expected = new Uint8Array(32); - expected.set(expectedBytes); - expected.set(expectedBytes, 16); + expected.set(RFC_V7_BYTES); + expected.set(RFC_V7_BYTES, 16); assert.deepEqual(buffer, expected); }); @@ -146,7 +115,7 @@ describe('v7', () => { test('lexicographical sorting is preserved', () => { let id; let prior; - let msecs = msecsFixture; + let msecs = RFC_MSECS; for (let i = 0; i < 20000; ++i) { if (i % 1500 === 0) { // every 1500 runs increment msecs so seq is @@ -154,7 +123,7 @@ describe('v7', () => { msecs += 1; } - id = v7({ msecs }); + id = v7({ msecs, seq: i }); if (prior !== undefined) { assert.ok(prior < id, `${prior} < ${id}`); @@ -164,24 +133,67 @@ describe('v7', () => { } }); - test('handles seq rollover', () => { - const msecs = msecsFixture; - const a = v7({ - msecs, - seq: 0x7fffffff, - }); - - v7({ msecs }); - - const c = v7({ msecs }); - - assert.ok(a < c, `${a} < ${c}`); + test('internal state updates properly', () => { + const tests = [ + { + // new time interval + state: { msecs: 1, seq: 123 }, + now: 2, + expected: { + msecs: 2, // time interval should update + seq: 0x6c318c4, // sequence should be randomized + }, + }, + { + // same time interval + state: { msecs: 1, seq: 123 }, + now: 1, + expected: { + msecs: 1, // timestamp unchanged + seq: 124, // sequence increments + }, + }, + { + // same time interval (sequence rollover) + state: { msecs: 1, seq: 0x7fffffff }, + now: 1, + expected: { + msecs: 2, // timestamp increments + seq: 0, // sequence rolls over + }, + }, + { + // time regression + state: { msecs: 2, seq: 123 }, + now: 1, + expected: { + msecs: 2, // timestamp unchanged + seq: 124, // sequence increments + }, + }, + { + // time regression (sequence rollover) + state: { msecs: 2, seq: 0x7fffffff }, + now: 1, + expected: { + // timestamp increments (crazy, right? The system clock goes backwards + // but the UUID timestamp moves forward? Weird, but it's what's + // required to maintain monotonicity... and this is why we have unit + // tests!) + msecs: 3, + seq: 0, // sequence rolls over + }, + }, + ]; + for (const { state, now, expected } of tests) { + assert.deepStrictEqual(updateV7State(state, now, RFC_RANDOM), expected); + } }); test('can supply seq', () => { let seq = 0x12345; let uuid = v7({ - msecs: msecsFixture, + msecs: RFC_MSECS, seq, }); @@ -189,7 +201,7 @@ describe('v7', () => { seq = 0x6fffffff; uuid = v7({ - msecs: msecsFixture, + msecs: RFC_MSECS, seq, }); @@ -198,12 +210,12 @@ describe('v7', () => { test('internal seq is reset upon timestamp change', () => { v7({ - msecs: msecsFixture, + msecs: RFC_MSECS, seq: 0x6fffffff, }); const uuid = v7({ - msecs: msecsFixture + 1, + msecs: RFC_MSECS + 1, }); assert.ok(uuid.indexOf('fff') !== 15); diff --git a/src/v7.ts b/src/v7.ts index fdfedfce..53598c92 100644 --- a/src/v7.ts +++ b/src/v7.ts @@ -2,154 +2,116 @@ import { UUIDTypes, Version7Options } from './_types.js'; import rng from './rng.js'; import { unsafeStringify } from './stringify.js'; -/** - * UUID V7 - Unix Epoch time-based UUID - * - * The IETF has published RFC9562, introducing 3 new UUID versions (6,7,8). This - * implementation of V7 is based on the accepted, though not yet approved, - * revisions. - * - * RFC 9562:https://www.rfc-editor.org/rfc/rfc9562.html Universally Unique - * IDentifiers (UUIDs) - - * - * Sample V7 value: - * https://www.rfc-editor.org/rfc/rfc9562.html#name-example-of-a-uuidv7-value - * - * Monotonic Bit Layout: RFC rfc9562.6.2 Method 1, Dedicated Counter Bits ref: - * https://www.rfc-editor.org/rfc/rfc9562.html#section-6.2-5.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 - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | unix_ts_ms | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | unix_ts_ms | ver | seq_hi | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * |var| seq_low | rand | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | rand | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * - * seq is a 31 bit serialized counter; comprised of 12 bit seq_hi and 19 bit - * seq_low, and randomly initialized upon timestamp change. 31 bit counter size - * was selected as any bitwise operations in node are done as _signed_ 32 bit - * ints. we exclude the sign bit. - */ - -let _seqLow: number | null = null; -let _seqHigh: number | null = null; -let _msecs = 0; +type V7State = { + msecs: number; + seq: number; +}; + +const _state: V7State = { + msecs: -Infinity, // time, milliseconds + seq: 0, // sequence number (31-bits) +}; function v7(options?: Version7Options, buf?: undefined, offset?: number): string; function v7(options?: Version7Options, buf?: Uint8Array, offset?: number): Uint8Array; function v7(options?: Version7Options, buf?: Uint8Array, offset?: number): UUIDTypes { - options ??= {}; - - // initialize buffer and pointer - let i = (buf && offset) || 0; - const b = buf || new Uint8Array(16); - - // 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(); - - // 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; - } - } + let bytes: Uint8Array; + + if (options) { + // w/ options, ake a UUID independent of internal state + bytes = v7Bytes( + options.random ?? options.rng?.() ?? rng(), + options.msecs, + options.seq, + buf, + offset + ); + } else { + // No options = Use internal state + const now = Date.now(); + const rnds = rng(); - // if we have a user provided seq - if (seq !== null) { - // trim provided seq to 31 bits of value, avoiding overflow - if (seq > 0x7fffffff) { - seq = 0x7fffffff; - } + updateV7State(_state, now, rnds); - // split provided seq into high/low parts - seqHigh = (seq >>> 19) & 0xfff; - seqLow = seq & 0x7ffff; + bytes = v7Bytes(rnds, _state.msecs, _state.seq, buf, offset); } - // randomly initialize seq - if (seqHigh === null || seqLow === null) { - seqHigh = rnds[6] & 0x7f; - seqHigh = (seqHigh << 8) | rnds[7]; + return buf ? bytes : unsafeStringify(bytes); +} - seqLow = rnds[8] & 0x3f; // pad for var - seqLow = (seqLow << 8) | rnds[9]; - seqLow = (seqLow << 5) | (rnds[10] >>> 3); +// PRIVATE API. This is only exported for testing purposes and should not be +// depended upon by external code. May change without notice. +export function updateV7State(state: V7State, now: number, rnds: Uint8Array) { + if (now > state.msecs) { + // Time has moved on! Pick a new random sequence number + state.seq = (rnds[6] << 23) | (rnds[7] << 16) | (rnds[8] << 8) | rnds[9]; + state.msecs = now; + } else { + if (state.seq === 0x7fffffff) { + // Sequence rollover, increment time to maintain monotonicity. + // + // Note:Timestamp is bumped to preserve monotonicity. This is allowed by the + // RFC and under normal circumstances will self-correct as the system clock + // catches up. + state.msecs++; + state.seq = 0; + } else { + // Time hasn't moved on (or has regressed for whatever reasons), just + // increment the sequence number + // + // NOTE: We don't update the time here to avoid breaking monotonicity + state.seq = (state.seq + 1) & 0x7fffffff; + } } - // increment seq if within msecs window - if (msecs + 10000 > _msecs && seq === null) { - if (++seqLow > 0x7ffff) { - seqLow = 0; - - if (++seqHigh > 0xfff) { - seqHigh = 0; + return state; +} - // increment internal _msecs. this allows us to continue incrementing - // while staying monotonic. Note, once we hit 10k milliseconds beyond system - // clock, we will reset breaking monotonicity (after (2^31)*10000 generations) - _msecs++; - } - } - } else { - // resetting; we have advanced more than - // 10k milliseconds beyond system clock - _msecs = msecs; - } +function v7Bytes( + rnds: Uint8Array, + msecs: number | undefined, + seq: number | undefined, + buf = new Uint8Array(16), + offset = 0 +) { + // Defaults + msecs ??= Date.now(); + seq ??= ((rnds[6] * 0x7f) << 24) | (rnds[7] << 16) | (rnds[8] << 8) | rnds[9]; - _seqHigh = seqHigh; - _seqLow = seqLow; + // Mask seq to 31 bits + seq &= 0x7fffffff; // [bytes 0-5] 48 bits of local timestamp - b[i++] = (_msecs / 0x10000000000) & 0xff; - b[i++] = (_msecs / 0x100000000) & 0xff; - b[i++] = (_msecs / 0x1000000) & 0xff; - b[i++] = (_msecs / 0x10000) & 0xff; - b[i++] = (_msecs / 0x100) & 0xff; - b[i++] = _msecs & 0xff; + buf[offset++] = (msecs / 0x10000000000) & 0xff; + buf[offset++] = (msecs / 0x100000000) & 0xff; + buf[offset++] = (msecs / 0x1000000) & 0xff; + buf[offset++] = (msecs / 0x10000) & 0xff; + buf[offset++] = (msecs / 0x100) & 0xff; + buf[offset++] = msecs & 0xff; - // [byte 6] - set 4 bits of version (7) with first 4 bits seq_hi - b[i++] = ((seqHigh >>> 8) & 0x0f) | 0x70; + // [byte 6] - `version` | seq bits 31-28 + buf[offset++] = 0x70 | ((seq >>> 27) & 0x0f); - // [byte 7] remaining 8 bits of seq_hi - b[i++] = seqHigh & 0xff; + // [byte 7] seq bits 27-20 + buf[offset++] = (seq >>> 19) & 0xff; - // [byte 8] - variant (2 bits), first 6 bits seq_low - b[i++] = ((seqLow >>> 13) & 0x3f) | 0x80; + // [byte 8] - `variant` (2 bits) | seq bits 19-14 + buf[offset++] = ((seq >>> 13) & 0x3f) | 0x80; - // [byte 9] 8 bits seq_low - b[i++] = (seqLow >>> 5) & 0xff; + // [byte 9] seq bits 13-6 + buf[offset++] = (seq >>> 5) & 0xff; - // [byte 10] remaining 5 bits seq_low, 3 bits random - b[i++] = ((seqLow << 3) & 0xff) | (rnds[10] & 0x07); + // [byte 10] seq bits 5-0 | random (3 bits) + buf[offset++] = ((seq << 3) & 0xff) | (rnds[10] & 0x07); // [bytes 11-15] always random - b[i++] = rnds[11]; - b[i++] = rnds[12]; - b[i++] = rnds[13]; - b[i++] = rnds[14]; - b[i++] = rnds[15]; + buf[offset++] = rnds[11]; + buf[offset++] = rnds[12]; + buf[offset++] = rnds[13]; + buf[offset++] = rnds[14]; + buf[offset++] = rnds[15]; - return buf || unsafeStringify(b); + return buf; } export default v7;