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

fix!: refactor v7 internal state and options logic, fixes #764 #779

Merged
merged 38 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3d7bca6
chore: rename *.js -> *.ts
broofa Jun 10, 2024
1b933c9
chore: add tsconfig file
broofa Jun 10, 2024
694cd50
chore: port parse, stringify, v1 to TS
broofa Jun 10, 2024
a69cbd1
chore: port v1ToV6, v6ToV1 to TS
broofa Jun 10, 2024
c6d8008
chore: port v4, v6ToV1 to TS
broofa Jun 10, 2024
d910db9
chore: more TS porting
broofa Jun 10, 2024
3e3abe9
chore: more TS porting
broofa Jun 10, 2024
c248e47
chore: more TS porting
broofa Jun 11, 2024
17675e8
chore: more TS porting
broofa Jun 11, 2024
95f9d5e
chore: add tsconfig and update build script
broofa Jun 11, 2024
651a959
fix: crypto imports
broofa Jun 12, 2024
fcf627d
chore: restore browser builds
broofa Jun 12, 2024
323f598
chore: move tests to src dir
broofa Jun 14, 2024
629b3df
chore: switch to node test runner
broofa Jun 17, 2024
c2da22f
chore: update test names
broofa Jun 17, 2024
1ff5491
chore: unit tests pass, switch to node test runner
broofa Jun 17, 2024
9c1f639
chore: eslint for typescript
broofa Jun 17, 2024
4d2ffa8
chore: inline source maps
broofa Jun 18, 2024
5ea99d6
chore: update imports
broofa Jun 18, 2024
20b0864
Merge branch 'main' into ts_port
broofa Jun 18, 2024
27c15df
Merge branch 'main' into ts_port
broofa Jun 18, 2024
4b7f9e1
Merge branch 'main' into ts_port
broofa Jun 18, 2024
0b4bb1a
chore: update optionalDevDependencies
broofa Jun 19, 2024
8e287d5
chore: node:crypto -> crypto
broofa Jun 19, 2024
e5a95ef
chore: update esm-browser example
broofa Jun 19, 2024
45e0d06
chore: rm unused eslint directive
broofa Jun 28, 2024
4b2af31
chore: fix browser tests
broofa Jun 28, 2024
d659b0a
chore: rm .vscode
broofa Jun 30, 2024
1d5f7c1
chore: switch eslint config to ESM
broofa Jun 30, 2024
6c0af42
chore: add vscode configuration
broofa Jun 30, 2024
8ba0686
chore: update build comment
broofa Jun 30, 2024
9be7e05
chore: rm comment from types
broofa Jun 30, 2024
574a6be
chore: update package-lock
broofa Jun 30, 2024
f8b253a
Merge branch 'main' into ts_port
broofa Jul 15, 2024
6e540f3
fix: encapsulate v7 options behavior, fixes #764
broofa Jul 16, 2024
894b629
fix: seq-related tests
broofa Jul 16, 2024
11b7f02
Merge branch 'main' into ts_fix_768
broofa Jul 17, 2024
050a7cf
fix: reset offset
broofa Jul 18, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`] | 32-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` |
Expand Down
4 changes: 2 additions & 2 deletions README_js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`] | 32-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` |
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,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",
Expand Down
233 changes: 123 additions & 110 deletions src/test/v7.test.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,40 @@
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
);
// `option.seq` for the above RFC uuid
const RFC_SEQ = (0x0cc3 << 20) | (0x98c4dc >> 2);

const expectedBytes = Uint8Array.of(
1,
127,
34,
226,
121,
176,
124,
195,
152,
196,
220,
12,
12,
7,
57,
143
);
// `option,random` for the above RFC uuid
const RFC_RANDOM = Uint8Array.of(
0x10,
0x91,
0x56,
0xbe,
0xc4,
0xfb,
0x0c,
0xc3,
0x18,
0xc4,
0x6c,
0x0c,
0x0c,
0x07,
0x39,
0x8f
);

describe('v7', () => {
test('subsequent UUIDs are different', () => {
const id1 = v7();
const id2 = v7();
Expand All @@ -75,25 +43,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);
});
Expand All @@ -102,13 +70,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);
});

Expand All @@ -117,25 +87,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);
});

Expand All @@ -146,15 +116,15 @@ 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
// reinitialized, simulating passage of time
msecs += 1;
}

id = v7({ msecs });
id = v7({ msecs, seq: i });

if (prior !== undefined) {
assert.ok(prior < id, `${prior} < ${id}`);
Expand All @@ -164,46 +134,89 @@ 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 = [
{
title: 'new time interval',
state: { msecs: 1, seq: 123 },
now: 2,
expected: {
msecs: 2, // time interval should update
seq: 0x6c318c4, // sequence should be randomized
},
},
{
title: 'same time interval',
state: { msecs: 1, seq: 123 },
now: 1,
expected: {
msecs: 1, // timestamp unchanged
seq: 124, // sequence increments
},
},
{
title: 'same time interval (sequence rollover)',
state: { msecs: 1, seq: 0xffffffff },
now: 1,
expected: {
msecs: 2, // timestamp increments
seq: 0, // sequence rolls over
},
},
{
title: 'time regression',
state: { msecs: 2, seq: 123 },
now: 1,
expected: {
msecs: 2, // timestamp unchanged
seq: 124, // sequence increments
},
},
{
title: 'time regression (sequence rollover)',
state: { msecs: 2, seq: 0xffffffff },
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 { title, state, now, expected } of tests) {
assert.deepStrictEqual(updateV7State(state, now, RFC_RANDOM), expected, `Failed: ${title}`);
}
});

test('can supply seq', () => {
let seq = 0x12345;
let uuid = v7({
msecs: msecsFixture,
msecs: RFC_MSECS,
seq,
});

assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7000-891a-2');
assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7000-848d-1');

seq = 0x6fffffff;
uuid = v7({
msecs: msecsFixture,
msecs: RFC_MSECS,
seq,
});

assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7dff-bfff-f');
assert.strictEqual(uuid.substring(0, 25), '017f22e2-79b0-76ff-bfff-f');
});

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);
Expand All @@ -216,18 +229,18 @@ describe('v7', () => {
// convert the given number of bits (LE) to number
const asNumber = (bits: number, data: bigint) => Number(BigInt.asUintN(bits, data));

// flip the nth bit (BE) in a BigInt
// flip the nth bit (BE) in a BigInt
const flip = (data: bigint, n: number) => data ^ (1n << BigInt(127 - n));

// Extract v7 `options` from a (BigInt) UUID
const optionsFrom = (data: bigint): Version7Options => {
const ms = asNumber(48, data >> (128n - 48n));
const hi = asNumber(12, data >> (43n + 19n + 2n));
const lo = asNumber(19, data >> 43n);
const r = BigInt.asUintN(43, data);
const ms = asNumber(48, data >> 80n);
const hi = asNumber(12, data >> 64n);
const lo = asNumber(20, data >> 42n);
const r = BigInt.asUintN(42, data);
return {
msecs: ms,
seq: (hi << 19) | lo,
seq: (hi << 20) | lo,
random: Uint8Array.from([
...Array(10).fill(0),
...Array(6)
Expand All @@ -247,8 +260,8 @@ describe('v7', () => {
}
const flipped = flip(data, i);
assert.strictEqual(
asBigInt(v7(optionsFrom(flipped), buf)),
flipped,
asBigInt(v7(optionsFrom(flipped), buf)).toString(16),
flipped.toString(16),
`Unequal uuids at bit ${i}`
);
assert.notStrictEqual(stringify(buf), id);
Expand Down
Loading
Loading