uuid 11.0.3 → 11.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -16
- package/dist/cjs/md5.d.ts +1 -1
- package/dist/cjs/parse.d.ts +1 -1
- package/dist/cjs/rng.d.ts +1 -1
- package/dist/cjs/sha1.d.ts +1 -1
- package/dist/cjs/v1.js +8 -0
- package/dist/cjs/v35.d.ts +2 -2
- package/dist/cjs/v4.js +7 -1
- package/dist/cjs/v7.js +8 -0
- package/dist/cjs-browser/md5.d.ts +1 -1
- package/dist/cjs-browser/parse.d.ts +1 -1
- package/dist/cjs-browser/rng.d.ts +1 -1
- package/dist/cjs-browser/v1.js +8 -0
- package/dist/cjs-browser/v35.d.ts +2 -2
- package/dist/cjs-browser/v4.js +7 -1
- package/dist/cjs-browser/v7.js +8 -0
- package/dist/esm/md5.d.ts +1 -1
- package/dist/esm/parse.d.ts +1 -1
- package/dist/esm/rng.d.ts +1 -1
- package/dist/esm/sha1.d.ts +1 -1
- package/dist/esm/v1.js +8 -0
- package/dist/esm/v35.d.ts +2 -2
- package/dist/esm/v4.js +7 -1
- package/dist/esm/v7.js +8 -0
- package/dist/esm-browser/md5.d.ts +1 -1
- package/dist/esm-browser/parse.d.ts +1 -1
- package/dist/esm-browser/rng.d.ts +1 -1
- package/dist/esm-browser/v1.js +8 -0
- package/dist/esm-browser/v35.d.ts +2 -2
- package/dist/esm-browser/v4.js +7 -1
- package/dist/esm-browser/v7.js +8 -0
- package/package.json +17 -20
- package/CHANGELOG.md +0 -335
- package/CONTRIBUTING.md +0 -18
- package/dist/cjs/test/parse.test.d.ts +0 -1
- package/dist/cjs/test/parse.test.js +0 -50
- package/dist/cjs/test/rng.test.d.ts +0 -1
- package/dist/cjs/test/rng.test.js +0 -14
- package/dist/cjs/test/stringify.test.d.ts +0 -1
- package/dist/cjs/test/stringify.test.js +0 -23
- package/dist/cjs/test/test_constants.d.ts +0 -33
- package/dist/cjs/test/test_constants.js +0 -91
- package/dist/cjs/test/v1.test.d.ts +0 -1
- package/dist/cjs/test/v1.test.js +0 -127
- package/dist/cjs/test/v35.test.d.ts +0 -1
- package/dist/cjs/test/v35.test.js +0 -143
- package/dist/cjs/test/v4.test.d.ts +0 -1
- package/dist/cjs/test/v4.test.js +0 -60
- package/dist/cjs/test/v6.test.d.ts +0 -1
- package/dist/cjs/test/v6.test.js +0 -56
- package/dist/cjs/test/v7.test.d.ts +0 -1
- package/dist/cjs/test/v7.test.js +0 -194
- package/dist/cjs/test/validate.test.d.ts +0 -1
- package/dist/cjs/test/validate.test.js +0 -13
- package/dist/cjs/test/version.test.d.ts +0 -1
- package/dist/cjs/test/version.test.js +0 -20
- package/dist/cjs-browser/test/parse.test.d.ts +0 -1
- package/dist/cjs-browser/test/parse.test.js +0 -50
- package/dist/cjs-browser/test/rng.test.d.ts +0 -1
- package/dist/cjs-browser/test/rng.test.js +0 -14
- package/dist/cjs-browser/test/stringify.test.d.ts +0 -1
- package/dist/cjs-browser/test/stringify.test.js +0 -23
- package/dist/cjs-browser/test/test_constants.d.ts +0 -33
- package/dist/cjs-browser/test/test_constants.js +0 -91
- package/dist/cjs-browser/test/v1.test.d.ts +0 -1
- package/dist/cjs-browser/test/v1.test.js +0 -127
- package/dist/cjs-browser/test/v35.test.d.ts +0 -1
- package/dist/cjs-browser/test/v35.test.js +0 -143
- package/dist/cjs-browser/test/v4.test.d.ts +0 -1
- package/dist/cjs-browser/test/v4.test.js +0 -60
- package/dist/cjs-browser/test/v6.test.d.ts +0 -1
- package/dist/cjs-browser/test/v6.test.js +0 -56
- package/dist/cjs-browser/test/v7.test.d.ts +0 -1
- package/dist/cjs-browser/test/v7.test.js +0 -194
- package/dist/cjs-browser/test/validate.test.d.ts +0 -1
- package/dist/cjs-browser/test/validate.test.js +0 -13
- package/dist/cjs-browser/test/version.test.d.ts +0 -1
- package/dist/cjs-browser/test/version.test.js +0 -20
- package/dist/esm/test/parse.test.d.ts +0 -1
- package/dist/esm/test/parse.test.js +0 -48
- package/dist/esm/test/rng.test.d.ts +0 -1
- package/dist/esm/test/rng.test.js +0 -12
- package/dist/esm/test/stringify.test.d.ts +0 -1
- package/dist/esm/test/stringify.test.js +0 -21
- package/dist/esm/test/test_constants.d.ts +0 -33
- package/dist/esm/test/test_constants.js +0 -88
- package/dist/esm/test/v1.test.d.ts +0 -1
- package/dist/esm/test/v1.test.js +0 -125
- package/dist/esm/test/v35.test.d.ts +0 -1
- package/dist/esm/test/v35.test.js +0 -141
- package/dist/esm/test/v4.test.d.ts +0 -1
- package/dist/esm/test/v4.test.js +0 -58
- package/dist/esm/test/v6.test.d.ts +0 -1
- package/dist/esm/test/v6.test.js +0 -54
- package/dist/esm/test/v7.test.d.ts +0 -1
- package/dist/esm/test/v7.test.js +0 -192
- package/dist/esm/test/validate.test.d.ts +0 -1
- package/dist/esm/test/validate.test.js +0 -11
- package/dist/esm/test/version.test.d.ts +0 -1
- package/dist/esm/test/version.test.js +0 -18
- package/dist/esm-browser/test/parse.test.d.ts +0 -1
- package/dist/esm-browser/test/parse.test.js +0 -48
- package/dist/esm-browser/test/rng.test.d.ts +0 -1
- package/dist/esm-browser/test/rng.test.js +0 -12
- package/dist/esm-browser/test/stringify.test.d.ts +0 -1
- package/dist/esm-browser/test/stringify.test.js +0 -21
- package/dist/esm-browser/test/test_constants.d.ts +0 -33
- package/dist/esm-browser/test/test_constants.js +0 -88
- package/dist/esm-browser/test/v1.test.d.ts +0 -1
- package/dist/esm-browser/test/v1.test.js +0 -125
- package/dist/esm-browser/test/v35.test.d.ts +0 -1
- package/dist/esm-browser/test/v35.test.js +0 -141
- package/dist/esm-browser/test/v4.test.d.ts +0 -1
- package/dist/esm-browser/test/v4.test.js +0 -58
- package/dist/esm-browser/test/v6.test.d.ts +0 -1
- package/dist/esm-browser/test/v6.test.js +0 -54
- package/dist/esm-browser/test/v7.test.d.ts +0 -1
- package/dist/esm-browser/test/v7.test.js +0 -192
- package/dist/esm-browser/test/validate.test.d.ts +0 -1
- package/dist/esm-browser/test/validate.test.js +0 -11
- package/dist/esm-browser/test/version.test.d.ts +0 -1
- package/dist/esm-browser/test/version.test.js +0 -18
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import MAX from '../max.js';
|
|
2
|
-
import NIL from '../nil.js';
|
|
3
|
-
export const TESTS = [
|
|
4
|
-
{ value: NIL, expectedValidate: true, expectedVersion: 0 },
|
|
5
|
-
{ value: MAX, expectedValidate: true, expectedVersion: 15 },
|
|
6
|
-
{ value: '00000000-0000-1000-8000-000000000000', expectedValidate: true, expectedVersion: 1 },
|
|
7
|
-
{ value: 'ffffffff-ffff-1fff-8fff-ffffffffffff', expectedValidate: true, expectedVersion: 1 },
|
|
8
|
-
{ value: '00000000-0000-2000-8000-000000000000', expectedValidate: true, expectedVersion: 2 },
|
|
9
|
-
{ value: 'ffffffff-ffff-2fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 2 },
|
|
10
|
-
{ value: '00000000-0000-3000-8000-000000000000', expectedValidate: true, expectedVersion: 3 },
|
|
11
|
-
{ value: 'ffffffff-ffff-3fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 3 },
|
|
12
|
-
{ value: '00000000-0000-4000-8000-000000000000', expectedValidate: true, expectedVersion: 4 },
|
|
13
|
-
{ value: 'ffffffff-ffff-4fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 4 },
|
|
14
|
-
{ value: '00000000-0000-5000-8000-000000000000', expectedValidate: true, expectedVersion: 5 },
|
|
15
|
-
{ value: 'ffffffff-ffff-5fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 5 },
|
|
16
|
-
{ value: '00000000-0000-6000-8000-000000000000', expectedValidate: true, expectedVersion: 6 },
|
|
17
|
-
{ value: 'ffffffff-ffff-6fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 6 },
|
|
18
|
-
{ value: '00000000-0000-7000-8000-000000000000', expectedValidate: true, expectedVersion: 7 },
|
|
19
|
-
{ value: 'ffffffff-ffff-7fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 7 },
|
|
20
|
-
{ value: '00000000-0000-8000-8000-000000000000', expectedValidate: true, expectedVersion: 8 },
|
|
21
|
-
{ value: 'ffffffff-ffff-8fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 8 },
|
|
22
|
-
{ value: '00000000-0000-9000-8000-000000000000', expectedValidate: false },
|
|
23
|
-
{ value: 'ffffffff-ffff-9fff-bfff-ffffffffffff', expectedValidate: false },
|
|
24
|
-
{ value: '00000000-0000-a000-8000-000000000000', expectedValidate: false },
|
|
25
|
-
{ value: 'ffffffff-ffff-afff-bfff-ffffffffffff', expectedValidate: false },
|
|
26
|
-
{ value: '00000000-0000-b000-8000-000000000000', expectedValidate: false },
|
|
27
|
-
{ value: 'ffffffff-ffff-bfff-bfff-ffffffffffff', expectedValidate: false },
|
|
28
|
-
{ value: '00000000-0000-c000-8000-000000000000', expectedValidate: false },
|
|
29
|
-
{ value: 'ffffffff-ffff-cfff-bfff-ffffffffffff', expectedValidate: false },
|
|
30
|
-
{ value: '00000000-0000-d000-8000-000000000000', expectedValidate: false },
|
|
31
|
-
{ value: 'ffffffff-ffff-dfff-bfff-ffffffffffff', expectedValidate: false },
|
|
32
|
-
{ value: '00000000-0000-e000-8000-000000000000', expectedValidate: false },
|
|
33
|
-
{ value: 'ffffffff-ffff-efff-bfff-ffffffffffff', expectedValidate: false },
|
|
34
|
-
{ value: 'd9428888-122b-11e1-b85c-61cd3cbb3210', expectedValidate: true, expectedVersion: 1 },
|
|
35
|
-
{ value: '000003e8-2363-21ef-b200-325096b39f47', expectedValidate: true, expectedVersion: 2 },
|
|
36
|
-
{ value: 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec', expectedValidate: true, expectedVersion: 3 },
|
|
37
|
-
{ value: '109156be-c4fb-41ea-b1b4-efe1671c5836', expectedValidate: true, expectedVersion: 4 },
|
|
38
|
-
{ value: '90123e1c-7512-523e-bb28-76fab9f2f73d', expectedValidate: true, expectedVersion: 5 },
|
|
39
|
-
{ value: '1ef21d2f-1207-6660-8c4f-419efbd44d48', expectedValidate: true, expectedVersion: 6 },
|
|
40
|
-
{ value: '017f22e2-79b0-7cc3-98c4-dc0c0c07398f', expectedValidate: true, expectedVersion: 7 },
|
|
41
|
-
{ value: '0d8f23a0-697f-83ae-802e-48f3756dd581', expectedValidate: true, expectedVersion: 8 },
|
|
42
|
-
{ value: '00000000-0000-1000-0000-000000000000', expectedValidate: false },
|
|
43
|
-
{ value: '00000000-0000-1000-1000-000000000000', expectedValidate: false },
|
|
44
|
-
{ value: '00000000-0000-1000-2000-000000000000', expectedValidate: false },
|
|
45
|
-
{ value: '00000000-0000-1000-3000-000000000000', expectedValidate: false },
|
|
46
|
-
{ value: '00000000-0000-1000-4000-000000000000', expectedValidate: false },
|
|
47
|
-
{ value: '00000000-0000-1000-5000-000000000000', expectedValidate: false },
|
|
48
|
-
{ value: '00000000-0000-1000-6000-000000000000', expectedValidate: false },
|
|
49
|
-
{ value: '00000000-0000-1000-7000-000000000000', expectedValidate: false },
|
|
50
|
-
{ value: '00000000-0000-1000-8000-000000000000', expectedValidate: true, expectedVersion: 1 },
|
|
51
|
-
{ value: '00000000-0000-1000-9000-000000000000', expectedValidate: true, expectedVersion: 1 },
|
|
52
|
-
{ value: '00000000-0000-1000-a000-000000000000', expectedValidate: true, expectedVersion: 1 },
|
|
53
|
-
{ value: '00000000-0000-1000-b000-000000000000', expectedValidate: true, expectedVersion: 1 },
|
|
54
|
-
{ value: '00000000-0000-1000-c000-000000000000', expectedValidate: false },
|
|
55
|
-
{ value: '00000000-0000-1000-d000-000000000000', expectedValidate: false },
|
|
56
|
-
{ value: '00000000-0000-1000-e000-000000000000', expectedValidate: false },
|
|
57
|
-
{ value: '00000000-0000-1000-f000-000000000000', expectedValidate: false },
|
|
58
|
-
{ value: '00000000000000000000000000000000', expectedValidate: false },
|
|
59
|
-
{ value: '', expectedValidate: false },
|
|
60
|
-
{ value: 'invalid uuid string', expectedValidate: false },
|
|
61
|
-
{
|
|
62
|
-
value: '=Y00a-f*vb*-c-d#-p00f\b-g0h-#i^-j*3&-L00k-\nl---00n-fg000-00p-00r+',
|
|
63
|
-
expectedValidate: false,
|
|
64
|
-
},
|
|
65
|
-
{ value: undefined, expectedValidate: false },
|
|
66
|
-
{ value: null, expectedValidate: false },
|
|
67
|
-
{ value: 123, expectedValidate: false },
|
|
68
|
-
{ value: /regex/, expectedValidate: false },
|
|
69
|
-
{ value: new Date(0), expectedValidate: false },
|
|
70
|
-
{ value: false, expectedValidate: false },
|
|
71
|
-
];
|
|
72
|
-
for (let charIndex = 0; charIndex < 36; charIndex++) {
|
|
73
|
-
if (charIndex === 8 ||
|
|
74
|
-
charIndex === 13 ||
|
|
75
|
-
charIndex === 14 ||
|
|
76
|
-
charIndex === 18 ||
|
|
77
|
-
charIndex === 23) {
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
const nilChars = NIL.split('');
|
|
81
|
-
const maxChars = MAX.split('');
|
|
82
|
-
for (let i = 0; i < 4; i++) {
|
|
83
|
-
nilChars[charIndex] = (0x0 ^ (1 << i)).toString(16);
|
|
84
|
-
TESTS.push({ value: nilChars.join(''), expectedValidate: false });
|
|
85
|
-
maxChars[charIndex] = (0xf ^ (1 << i)).toString(16);
|
|
86
|
-
TESTS.push({ value: maxChars.join(''), expectedValidate: false });
|
|
87
|
-
}
|
|
88
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import * as assert from 'assert';
|
|
2
|
-
import test, { describe } from 'node:test';
|
|
3
|
-
import parse from '../parse.js';
|
|
4
|
-
import v1, { updateV1State } from '../v1.js';
|
|
5
|
-
const TIME = 1321644961388;
|
|
6
|
-
const RFC_V1 = 'c232ab00-9414-11ec-b3c8-9f68deced846';
|
|
7
|
-
const RFC_V1_BYTES = parse(RFC_V1);
|
|
8
|
-
const RFC_OPTIONS = {
|
|
9
|
-
msecs: 0x17f22e279b0,
|
|
10
|
-
nsecs: 0,
|
|
11
|
-
clockseq: 0x33c8,
|
|
12
|
-
node: Uint8Array.of(0x9f, 0x68, 0xde, 0xce, 0xd8, 0x46),
|
|
13
|
-
};
|
|
14
|
-
const RFC_RANDOM = Uint8Array.of(0, 0, 0, 0, 0, 0, 0, 0, RFC_OPTIONS.clockseq >> 8, RFC_OPTIONS.clockseq & 0xff, ...RFC_OPTIONS.node);
|
|
15
|
-
function compareV1TimeField(a, b) {
|
|
16
|
-
a = a.split('-').slice(0, 3).reverse().join('');
|
|
17
|
-
b = b.split('-').slice(0, 3).reverse().join('');
|
|
18
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
19
|
-
}
|
|
20
|
-
describe('v1', () => {
|
|
21
|
-
test('v1 sort order (default)', () => {
|
|
22
|
-
const ids = [v1(), v1(), v1(), v1(), v1()];
|
|
23
|
-
const sorted = [...ids].sort(compareV1TimeField);
|
|
24
|
-
assert.deepEqual(ids, sorted);
|
|
25
|
-
});
|
|
26
|
-
test('v1 sort order (time option)', () => {
|
|
27
|
-
const ids = [
|
|
28
|
-
v1({ msecs: TIME - 10 * 3600 * 1000 }),
|
|
29
|
-
v1({ msecs: TIME - 1 }),
|
|
30
|
-
v1({ msecs: TIME }),
|
|
31
|
-
v1({ msecs: TIME + 1 }),
|
|
32
|
-
v1({ msecs: TIME + 28 * 24 * 3600 * 1000 }),
|
|
33
|
-
];
|
|
34
|
-
const sorted = [...ids].sort(compareV1TimeField);
|
|
35
|
-
assert.deepEqual(ids, sorted);
|
|
36
|
-
});
|
|
37
|
-
test('v1(options)', () => {
|
|
38
|
-
assert.equal(v1({ msecs: RFC_OPTIONS.msecs, random: RFC_RANDOM }), RFC_V1, 'minimal options');
|
|
39
|
-
assert.equal(v1(RFC_OPTIONS), RFC_V1, 'full options');
|
|
40
|
-
});
|
|
41
|
-
test('v1(options) equality', () => {
|
|
42
|
-
assert.notEqual(v1({ msecs: TIME }), v1({ msecs: TIME }), 'UUIDs with minimal options differ');
|
|
43
|
-
assert.equal(v1(RFC_OPTIONS), v1(RFC_OPTIONS), 'UUIDs with full options are identical');
|
|
44
|
-
});
|
|
45
|
-
test('fills one UUID into a buffer as expected', () => {
|
|
46
|
-
const buffer = new Uint8Array(16);
|
|
47
|
-
const result = v1(RFC_OPTIONS, buffer);
|
|
48
|
-
assert.deepEqual(buffer, RFC_V1_BYTES);
|
|
49
|
-
assert.strictEqual(buffer, result);
|
|
50
|
-
});
|
|
51
|
-
test('fills two UUIDs into a buffer as expected', () => {
|
|
52
|
-
const buffer = new Uint8Array(32);
|
|
53
|
-
v1(RFC_OPTIONS, buffer, 0);
|
|
54
|
-
v1(RFC_OPTIONS, buffer, 16);
|
|
55
|
-
const expectedBuf = new Uint8Array(32);
|
|
56
|
-
expectedBuf.set(RFC_V1_BYTES);
|
|
57
|
-
expectedBuf.set(RFC_V1_BYTES, 16);
|
|
58
|
-
assert.deepEqual(buffer, expectedBuf);
|
|
59
|
-
});
|
|
60
|
-
test('v1() state transitions', () => {
|
|
61
|
-
const PRE_STATE = {
|
|
62
|
-
msecs: 10,
|
|
63
|
-
nsecs: 20,
|
|
64
|
-
clockseq: 0x1234,
|
|
65
|
-
node: Uint8Array.of(0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc),
|
|
66
|
-
};
|
|
67
|
-
const tests = [
|
|
68
|
-
{
|
|
69
|
-
title: 'initial state',
|
|
70
|
-
state: {},
|
|
71
|
-
now: 10,
|
|
72
|
-
expected: {
|
|
73
|
-
msecs: 10,
|
|
74
|
-
nsecs: 0,
|
|
75
|
-
clockseq: RFC_OPTIONS.clockseq,
|
|
76
|
-
node: RFC_OPTIONS.node,
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
title: 'same time interval',
|
|
81
|
-
state: { ...PRE_STATE },
|
|
82
|
-
now: PRE_STATE.msecs,
|
|
83
|
-
expected: {
|
|
84
|
-
...PRE_STATE,
|
|
85
|
-
nsecs: 21,
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
title: 'new time interval',
|
|
90
|
-
state: { ...PRE_STATE },
|
|
91
|
-
now: PRE_STATE.msecs + 1,
|
|
92
|
-
expected: {
|
|
93
|
-
...PRE_STATE,
|
|
94
|
-
msecs: PRE_STATE.msecs + 1,
|
|
95
|
-
nsecs: 0,
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
{
|
|
99
|
-
title: 'same time interval (nsecs overflow)',
|
|
100
|
-
state: { ...PRE_STATE, nsecs: 9999 },
|
|
101
|
-
now: PRE_STATE.msecs,
|
|
102
|
-
expected: {
|
|
103
|
-
...PRE_STATE,
|
|
104
|
-
nsecs: 0,
|
|
105
|
-
clockseq: RFC_OPTIONS.clockseq,
|
|
106
|
-
node: RFC_OPTIONS.node,
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
title: 'time regression',
|
|
111
|
-
state: { ...PRE_STATE },
|
|
112
|
-
now: PRE_STATE.msecs - 1,
|
|
113
|
-
expected: {
|
|
114
|
-
...PRE_STATE,
|
|
115
|
-
msecs: PRE_STATE.msecs - 1,
|
|
116
|
-
clockseq: RFC_OPTIONS.clockseq,
|
|
117
|
-
node: RFC_OPTIONS.node,
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
];
|
|
121
|
-
for (const { title, state, now, expected } of tests) {
|
|
122
|
-
assert.deepStrictEqual(updateV1State(state, now, RFC_RANDOM), expected, `Failed: ${title}`);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import * as assert from 'assert';
|
|
2
|
-
import test, { describe } from 'node:test';
|
|
3
|
-
import md5 from '../md5.js';
|
|
4
|
-
import sha1 from '../sha1.js';
|
|
5
|
-
import v3 from '../v3.js';
|
|
6
|
-
import { stringToBytes } from '../v35.js';
|
|
7
|
-
import v5 from '../v5.js';
|
|
8
|
-
describe('v35', () => {
|
|
9
|
-
const HASH_SAMPLES = [
|
|
10
|
-
{
|
|
11
|
-
input: stringToBytes(''),
|
|
12
|
-
sha1: 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
|
|
13
|
-
md5: 'd41d8cd98f00b204e9800998ecf8427e',
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
input: stringToBytes('\t\b\f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6\u00A7\u00A8\u00A9\u00AA\u00AB\u00AC\u00AE\u00AF\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7\u00B8\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D7\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF'),
|
|
17
|
-
sha1: 'ca4a426a3d536f14cfd79011e79e10d64de950a0',
|
|
18
|
-
md5: 'e8098ec21950f841731d28749129d3ee',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
input: stringToBytes('\u00A5\u0104\u018F\u0256\u02B1o\u0315\u038E\u0409\u0500\u0531\u05E1\u05B6\u0920\u0903\u09A4\u0983\u0A20\u0A02\u0AA0\u0A83\u0B06\u0C05\u0C03\u1401\u16A0'),
|
|
22
|
-
sha1: 'f2753ebc390e5f637e333c2a4179644a93ae9f65',
|
|
23
|
-
md5: '231b309e277b6be8bb3d6c688b7f098b',
|
|
24
|
-
},
|
|
25
|
-
];
|
|
26
|
-
function hashToHex(hash) {
|
|
27
|
-
const chars = new Array(hash.length);
|
|
28
|
-
for (let i = 0; i < hash.length; i++) {
|
|
29
|
-
chars[i] = hash[i].toString(16).padStart(2, '0');
|
|
30
|
-
}
|
|
31
|
-
return chars.join('');
|
|
32
|
-
}
|
|
33
|
-
HASH_SAMPLES.forEach(function (sample, i) {
|
|
34
|
-
test(`sha1(node) HASH_SAMPLES[${i}]`, () => {
|
|
35
|
-
assert.equal(hashToHex(sha1(sample.input)), sample.sha1);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
HASH_SAMPLES.forEach(function (sample, i) {
|
|
39
|
-
test(`md5(node) HASH_SAMPLES[${i}]`, () => {
|
|
40
|
-
assert.equal(hashToHex(md5(sample.input)), sample.md5);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
test('v3', () => {
|
|
44
|
-
assert.strictEqual(v3('hello.example.com', v3.DNS), '9125a8dc-52ee-365b-a5aa-81b0b3681cf6');
|
|
45
|
-
assert.strictEqual(v3('http://example.com/hello', v3.URL), 'c6235813-3ba4-3801-ae84-e0a6ebb7d138');
|
|
46
|
-
assert.strictEqual(v3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'), 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec');
|
|
47
|
-
});
|
|
48
|
-
test('v3 namespace.toUpperCase', () => {
|
|
49
|
-
assert.strictEqual(v3('hello.example.com', v3.DNS.toUpperCase()), '9125a8dc-52ee-365b-a5aa-81b0b3681cf6');
|
|
50
|
-
assert.strictEqual(v3('http://example.com/hello', v3.URL.toUpperCase()), 'c6235813-3ba4-3801-ae84-e0a6ebb7d138');
|
|
51
|
-
assert.strictEqual(v3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()), 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec');
|
|
52
|
-
});
|
|
53
|
-
test('v3 namespace string validation', () => {
|
|
54
|
-
assert.throws(() => {
|
|
55
|
-
v3('hello.example.com', 'zyxwvuts-rqpo-nmlk-jihg-fedcba000000');
|
|
56
|
-
});
|
|
57
|
-
assert.throws(() => {
|
|
58
|
-
v3('hello.example.com', 'invalid uuid value');
|
|
59
|
-
});
|
|
60
|
-
assert.ok(v3('hello.example.com', '00000000-0000-0000-0000-000000000000'));
|
|
61
|
-
});
|
|
62
|
-
test('v3 namespace buffer validation', () => {
|
|
63
|
-
assert.throws(() => {
|
|
64
|
-
v3('hello.example.com', new Uint8Array(15));
|
|
65
|
-
});
|
|
66
|
-
assert.throws(() => {
|
|
67
|
-
v3('hello.example.com', new Uint8Array(17));
|
|
68
|
-
});
|
|
69
|
-
assert.ok(v3('hello.example.com', new Uint8Array(16).fill(0)));
|
|
70
|
-
});
|
|
71
|
-
test('v3 fill buffer', () => {
|
|
72
|
-
let buf = new Uint8Array(16);
|
|
73
|
-
const expectedUuid = Uint8Array.of(0x91, 0x25, 0xa8, 0xdc, 0x52, 0xee, 0x36, 0x5b, 0xa5, 0xaa, 0x81, 0xb0, 0xb3, 0x68, 0x1c, 0xf6);
|
|
74
|
-
const result = v3('hello.example.com', v3.DNS, buf);
|
|
75
|
-
assert.deepEqual(buf, expectedUuid);
|
|
76
|
-
assert.strictEqual(result, buf);
|
|
77
|
-
buf = new Uint8Array(19).fill(0xaa);
|
|
78
|
-
const expectedBuf = new Uint8Array(19).fill(0xaa);
|
|
79
|
-
expectedBuf.set(expectedUuid, 3);
|
|
80
|
-
v3('hello.example.com', v3.DNS, buf, 3);
|
|
81
|
-
assert.deepEqual(buf, expectedBuf);
|
|
82
|
-
});
|
|
83
|
-
test('v3 undefined/null', () => {
|
|
84
|
-
assert.throws(() => v3());
|
|
85
|
-
assert.throws(() => v3('hello'));
|
|
86
|
-
assert.throws(() => v3('hello.example.com', undefined));
|
|
87
|
-
assert.throws(() => v3('hello.example.com', null, new Uint8Array(16)));
|
|
88
|
-
});
|
|
89
|
-
test('v5', () => {
|
|
90
|
-
assert.strictEqual(v5('hello.example.com', v5.DNS), 'fdda765f-fc57-5604-a269-52a7df8164ec');
|
|
91
|
-
assert.strictEqual(v5('http://example.com/hello', v5.URL), '3bbcee75-cecc-5b56-8031-b6641c1ed1f1');
|
|
92
|
-
assert.strictEqual(v5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'), '90123e1c-7512-523e-bb28-76fab9f2f73d');
|
|
93
|
-
});
|
|
94
|
-
test('v5 namespace.toUpperCase', () => {
|
|
95
|
-
assert.strictEqual(v5('hello.example.com', v5.DNS.toUpperCase()), 'fdda765f-fc57-5604-a269-52a7df8164ec');
|
|
96
|
-
assert.strictEqual(v5('http://example.com/hello', v5.URL.toUpperCase()), '3bbcee75-cecc-5b56-8031-b6641c1ed1f1');
|
|
97
|
-
assert.strictEqual(v5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()), '90123e1c-7512-523e-bb28-76fab9f2f73d');
|
|
98
|
-
});
|
|
99
|
-
test('v5 namespace string validation', () => {
|
|
100
|
-
assert.throws(() => {
|
|
101
|
-
v5('hello.example.com', 'zyxwvuts-rqpo-nmlk-jihg-fedcba000000');
|
|
102
|
-
});
|
|
103
|
-
assert.throws(() => {
|
|
104
|
-
v5('hello.example.com', 'invalid uuid value');
|
|
105
|
-
});
|
|
106
|
-
assert.ok(v5('hello.example.com', '00000000-0000-0000-0000-000000000000'));
|
|
107
|
-
});
|
|
108
|
-
test('v5 namespace buffer validation', () => {
|
|
109
|
-
assert.throws(() => {
|
|
110
|
-
v5('hello.example.com', new Uint8Array(15));
|
|
111
|
-
});
|
|
112
|
-
assert.throws(() => {
|
|
113
|
-
v5('hello.example.com', new Uint8Array(17));
|
|
114
|
-
});
|
|
115
|
-
assert.ok(v5('hello.example.com', new Uint8Array(16).fill(0)));
|
|
116
|
-
});
|
|
117
|
-
test('v5 fill buffer', () => {
|
|
118
|
-
let buf = new Uint8Array(16);
|
|
119
|
-
const expectedUuid = Uint8Array.of(0xfd, 0xda, 0x76, 0x5f, 0xfc, 0x57, 0x56, 0x04, 0xa2, 0x69, 0x52, 0xa7, 0xdf, 0x81, 0x64, 0xec);
|
|
120
|
-
const result = v5('hello.example.com', v5.DNS, buf);
|
|
121
|
-
assert.deepEqual(buf, expectedUuid);
|
|
122
|
-
assert.strictEqual(result, buf);
|
|
123
|
-
buf = new Uint8Array(19).fill(0xaa);
|
|
124
|
-
const expectedBuf = new Uint8Array(19).fill(0xaa);
|
|
125
|
-
expectedBuf.set(expectedUuid, 3);
|
|
126
|
-
v5('hello.example.com', v5.DNS, buf, 3);
|
|
127
|
-
assert.deepEqual(buf, expectedBuf);
|
|
128
|
-
});
|
|
129
|
-
test('v5 undefined/null', () => {
|
|
130
|
-
assert.throws(() => v5());
|
|
131
|
-
assert.throws(() => v5('hello'));
|
|
132
|
-
assert.throws(() => v5('hello.example.com', undefined));
|
|
133
|
-
assert.throws(() => v5('hello.example.com', null, new Uint8Array(16)));
|
|
134
|
-
});
|
|
135
|
-
test('v3/v5 constants', () => {
|
|
136
|
-
assert.strictEqual(v3.DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
|
|
137
|
-
assert.strictEqual(v3.URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
|
|
138
|
-
assert.strictEqual(v5.DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
|
|
139
|
-
assert.strictEqual(v5.URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
|
|
140
|
-
});
|
|
141
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import * as assert from 'assert';
|
|
2
|
-
import test, { describe } from 'node:test';
|
|
3
|
-
import native from '../native.js';
|
|
4
|
-
import v4 from '../v4.js';
|
|
5
|
-
const randomBytesFixture = Uint8Array.of(0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea, 0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36);
|
|
6
|
-
const expectedBytes = Uint8Array.of(16, 145, 86, 190, 196, 251, 65, 234, 177, 180, 239, 225, 103, 28, 88, 54);
|
|
7
|
-
describe('v4', () => {
|
|
8
|
-
test('subsequent UUIDs are different', () => {
|
|
9
|
-
const id1 = v4();
|
|
10
|
-
const id2 = v4();
|
|
11
|
-
assert.ok(id1 !== id2);
|
|
12
|
-
});
|
|
13
|
-
test('should uses native randomUUID() if no option is passed', async () => {
|
|
14
|
-
const mock = (await import('node:test')).default.mock;
|
|
15
|
-
if (!mock) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
const mockRandomUUID = mock.method(native, 'randomUUID');
|
|
19
|
-
assert.equal(mockRandomUUID.mock.callCount(), 0);
|
|
20
|
-
v4();
|
|
21
|
-
assert.equal(mockRandomUUID.mock.callCount(), 1);
|
|
22
|
-
mock.restoreAll();
|
|
23
|
-
});
|
|
24
|
-
test('should not use native randomUUID() if an option is passed', async () => {
|
|
25
|
-
const mock = (await import('node:test')).default.mock;
|
|
26
|
-
if (!mock) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const mockRandomUUID = mock.method(native, 'randomUUID');
|
|
30
|
-
assert.equal(mockRandomUUID.mock.callCount(), 0);
|
|
31
|
-
v4({});
|
|
32
|
-
assert.equal(mockRandomUUID.mock.callCount(), 0);
|
|
33
|
-
mock.restoreAll();
|
|
34
|
-
});
|
|
35
|
-
test('explicit options.random produces expected result', () => {
|
|
36
|
-
const id = v4({ random: randomBytesFixture });
|
|
37
|
-
assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');
|
|
38
|
-
});
|
|
39
|
-
test('explicit options.rng produces expected result', () => {
|
|
40
|
-
const id = v4({ rng: () => randomBytesFixture });
|
|
41
|
-
assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');
|
|
42
|
-
});
|
|
43
|
-
test('fills one UUID into a buffer as expected', () => {
|
|
44
|
-
const buffer = new Uint8Array(16);
|
|
45
|
-
const result = v4({ random: randomBytesFixture }, buffer);
|
|
46
|
-
assert.deepEqual(buffer, expectedBytes);
|
|
47
|
-
assert.strictEqual(buffer, result);
|
|
48
|
-
});
|
|
49
|
-
test('fills two UUIDs into a buffer as expected', () => {
|
|
50
|
-
const buffer = new Uint8Array(32);
|
|
51
|
-
v4({ random: randomBytesFixture }, buffer, 0);
|
|
52
|
-
v4({ random: randomBytesFixture }, buffer, 16);
|
|
53
|
-
const expectedBuf = new Uint8Array(32);
|
|
54
|
-
expectedBuf.set(expectedBytes);
|
|
55
|
-
expectedBuf.set(expectedBytes, 16);
|
|
56
|
-
assert.deepEqual(buffer, expectedBuf);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import * as assert from 'assert';
|
|
2
|
-
import test, { describe } from 'node:test';
|
|
3
|
-
import v1ToV6 from '../v1ToV6.js';
|
|
4
|
-
import v6 from '../v6.js';
|
|
5
|
-
import v6ToV1 from '../v6ToV1.js';
|
|
6
|
-
describe('v6', () => {
|
|
7
|
-
const V1_ID = 'f1207660-21d2-11ef-8c4f-419efbd44d48';
|
|
8
|
-
const V6_ID = '1ef21d2f-1207-6660-8c4f-419efbd44d48';
|
|
9
|
-
const fullOptions = {
|
|
10
|
-
msecs: 0x133b891f705,
|
|
11
|
-
nsecs: 0x1538,
|
|
12
|
-
clockseq: 0x385c,
|
|
13
|
-
node: Uint8Array.of(0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10),
|
|
14
|
-
};
|
|
15
|
-
const EXPECTED_BYTES = Uint8Array.of(0x1e, 0x11, 0x22, 0xbd, 0x94, 0x28, 0x68, 0x88, 0xb8, 0x5c, 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10);
|
|
16
|
-
test('default behavior', () => {
|
|
17
|
-
const id = v6();
|
|
18
|
-
assert.ok(/[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/.test(id), 'id is valid v6 UUID');
|
|
19
|
-
});
|
|
20
|
-
test('default behavior (binary type)', () => {
|
|
21
|
-
const buffer = new Uint8Array(16);
|
|
22
|
-
const result = v6(fullOptions, buffer);
|
|
23
|
-
assert.deepEqual(buffer, EXPECTED_BYTES);
|
|
24
|
-
assert.strictEqual(buffer, result);
|
|
25
|
-
});
|
|
26
|
-
test('all options', () => {
|
|
27
|
-
const id = v6(fullOptions);
|
|
28
|
-
assert.equal(id, '1e1122bd-9428-6888-b85c-61cd3cbb3210');
|
|
29
|
-
});
|
|
30
|
-
test('sort by creation time', () => {
|
|
31
|
-
const ids = [];
|
|
32
|
-
for (let i = 0; i < 5; i++) {
|
|
33
|
-
ids.push(v6({ msecs: i * 1000 }));
|
|
34
|
-
}
|
|
35
|
-
assert.deepEqual(ids, ids.slice().sort());
|
|
36
|
-
});
|
|
37
|
-
test('creating at array offset', () => {
|
|
38
|
-
const buffer = new Uint8Array(32);
|
|
39
|
-
v6(fullOptions, buffer, 0);
|
|
40
|
-
v6(fullOptions, buffer, 16);
|
|
41
|
-
const expectedBuf = new Uint8Array(32);
|
|
42
|
-
expectedBuf.set(EXPECTED_BYTES, 0);
|
|
43
|
-
expectedBuf.set(EXPECTED_BYTES, 16);
|
|
44
|
-
assert.deepEqual(buffer, expectedBuf);
|
|
45
|
-
});
|
|
46
|
-
test('v1 -> v6 conversion', () => {
|
|
47
|
-
const id = v1ToV6(V1_ID);
|
|
48
|
-
assert.equal(id, V6_ID);
|
|
49
|
-
});
|
|
50
|
-
test('v6 -> v1 conversion', () => {
|
|
51
|
-
const id = v6ToV1(V6_ID);
|
|
52
|
-
assert.equal(id, V1_ID);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import * as assert from 'assert';
|
|
2
|
-
import test, { describe } from 'node:test';
|
|
3
|
-
import parse from '../parse.js';
|
|
4
|
-
import stringify from '../stringify.js';
|
|
5
|
-
import v7, { updateV7State } from '../v7.js';
|
|
6
|
-
const RFC_V7 = '017f22e2-79b0-7cc3-98c4-dc0c0c07398f';
|
|
7
|
-
const RFC_V7_BYTES = parse('017f22e2-79b0-7cc3-98c4-dc0c0c07398f');
|
|
8
|
-
const RFC_MSECS = 0x17f22e279b0;
|
|
9
|
-
const RFC_SEQ = (0x0cc3 << 20) | (0x98c4dc >> 2);
|
|
10
|
-
const RFC_RANDOM = Uint8Array.of(0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0x0c, 0xc3, 0x18, 0xc4, 0x6c, 0x0c, 0x0c, 0x07, 0x39, 0x8f);
|
|
11
|
-
describe('v7', () => {
|
|
12
|
-
test('subsequent UUIDs are different', () => {
|
|
13
|
-
const id1 = v7();
|
|
14
|
-
const id2 = v7();
|
|
15
|
-
assert.ok(id1 !== id2);
|
|
16
|
-
});
|
|
17
|
-
test('explicit options.random and options.msecs produces expected result', () => {
|
|
18
|
-
const id = v7({
|
|
19
|
-
random: RFC_RANDOM,
|
|
20
|
-
msecs: RFC_MSECS,
|
|
21
|
-
seq: RFC_SEQ,
|
|
22
|
-
});
|
|
23
|
-
assert.strictEqual(id, RFC_V7);
|
|
24
|
-
});
|
|
25
|
-
test('explicit options.rng produces expected result', () => {
|
|
26
|
-
const id = v7({
|
|
27
|
-
rng: () => RFC_RANDOM,
|
|
28
|
-
msecs: RFC_MSECS,
|
|
29
|
-
seq: RFC_SEQ,
|
|
30
|
-
});
|
|
31
|
-
assert.strictEqual(id, RFC_V7);
|
|
32
|
-
});
|
|
33
|
-
test('explicit options.msecs produces expected result', () => {
|
|
34
|
-
const id = v7({
|
|
35
|
-
msecs: RFC_MSECS,
|
|
36
|
-
});
|
|
37
|
-
assert.strictEqual(id.indexOf('017f22e2'), 0);
|
|
38
|
-
});
|
|
39
|
-
test('fills one UUID into a buffer as expected', () => {
|
|
40
|
-
const buffer = new Uint8Array(16);
|
|
41
|
-
const result = v7({
|
|
42
|
-
random: RFC_RANDOM,
|
|
43
|
-
msecs: RFC_MSECS,
|
|
44
|
-
seq: RFC_SEQ,
|
|
45
|
-
}, buffer);
|
|
46
|
-
stringify(buffer);
|
|
47
|
-
assert.deepEqual(buffer, RFC_V7_BYTES);
|
|
48
|
-
assert.strictEqual(buffer, result);
|
|
49
|
-
});
|
|
50
|
-
test('fills two UUIDs into a buffer as expected', () => {
|
|
51
|
-
const buffer = new Uint8Array(32);
|
|
52
|
-
v7({
|
|
53
|
-
random: RFC_RANDOM,
|
|
54
|
-
msecs: RFC_MSECS,
|
|
55
|
-
seq: RFC_SEQ,
|
|
56
|
-
}, buffer, 0);
|
|
57
|
-
v7({
|
|
58
|
-
random: RFC_RANDOM,
|
|
59
|
-
msecs: RFC_MSECS,
|
|
60
|
-
seq: RFC_SEQ,
|
|
61
|
-
}, buffer, 16);
|
|
62
|
-
const expected = new Uint8Array(32);
|
|
63
|
-
expected.set(RFC_V7_BYTES);
|
|
64
|
-
expected.set(RFC_V7_BYTES, 16);
|
|
65
|
-
assert.deepEqual(buffer, expected);
|
|
66
|
-
});
|
|
67
|
-
test('lexicographical sorting is preserved', () => {
|
|
68
|
-
let id;
|
|
69
|
-
let prior;
|
|
70
|
-
let msecs = RFC_MSECS;
|
|
71
|
-
for (let i = 0; i < 20000; ++i) {
|
|
72
|
-
if (i % 1500 === 0) {
|
|
73
|
-
msecs += 1;
|
|
74
|
-
}
|
|
75
|
-
id = v7({ msecs, seq: i });
|
|
76
|
-
if (prior !== undefined) {
|
|
77
|
-
assert.ok(prior < id, `${prior} < ${id}`);
|
|
78
|
-
}
|
|
79
|
-
prior = id;
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
test('can supply seq', () => {
|
|
83
|
-
let seq = 0x12345;
|
|
84
|
-
let uuid = v7({
|
|
85
|
-
msecs: RFC_MSECS,
|
|
86
|
-
seq,
|
|
87
|
-
});
|
|
88
|
-
assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7000-848d-1');
|
|
89
|
-
seq = 0x6fffffff;
|
|
90
|
-
uuid = v7({
|
|
91
|
-
msecs: RFC_MSECS,
|
|
92
|
-
seq,
|
|
93
|
-
});
|
|
94
|
-
assert.strictEqual(uuid.substring(0, 25), '017f22e2-79b0-76ff-bfff-f');
|
|
95
|
-
});
|
|
96
|
-
test('internal seq is reset upon timestamp change', () => {
|
|
97
|
-
v7({
|
|
98
|
-
msecs: RFC_MSECS,
|
|
99
|
-
seq: 0x6fffffff,
|
|
100
|
-
});
|
|
101
|
-
const uuid = v7({
|
|
102
|
-
msecs: RFC_MSECS + 1,
|
|
103
|
-
});
|
|
104
|
-
assert.ok(uuid.indexOf('fff') !== 15);
|
|
105
|
-
});
|
|
106
|
-
test('v7() state transitions', () => {
|
|
107
|
-
const tests = [
|
|
108
|
-
{
|
|
109
|
-
title: 'new time interval',
|
|
110
|
-
state: { msecs: 1, seq: 123 },
|
|
111
|
-
now: 2,
|
|
112
|
-
expected: {
|
|
113
|
-
msecs: 2,
|
|
114
|
-
seq: 0x6c318c4,
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
title: 'same time interval',
|
|
119
|
-
state: { msecs: 1, seq: 123 },
|
|
120
|
-
now: 1,
|
|
121
|
-
expected: {
|
|
122
|
-
msecs: 1,
|
|
123
|
-
seq: 124,
|
|
124
|
-
},
|
|
125
|
-
},
|
|
126
|
-
{
|
|
127
|
-
title: 'same time interval (sequence rollover)',
|
|
128
|
-
state: { msecs: 1, seq: 0xffffffff },
|
|
129
|
-
now: 1,
|
|
130
|
-
expected: {
|
|
131
|
-
msecs: 2,
|
|
132
|
-
seq: 0,
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
{
|
|
136
|
-
title: 'time regression',
|
|
137
|
-
state: { msecs: 2, seq: 123 },
|
|
138
|
-
now: 1,
|
|
139
|
-
expected: {
|
|
140
|
-
msecs: 2,
|
|
141
|
-
seq: 124,
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
{
|
|
145
|
-
title: 'time regression (sequence rollover)',
|
|
146
|
-
state: { msecs: 2, seq: 0xffffffff },
|
|
147
|
-
now: 1,
|
|
148
|
-
expected: {
|
|
149
|
-
msecs: 3,
|
|
150
|
-
seq: 0,
|
|
151
|
-
},
|
|
152
|
-
},
|
|
153
|
-
];
|
|
154
|
-
for (const { title, state, now, expected } of tests) {
|
|
155
|
-
assert.deepStrictEqual(updateV7State(state, now, RFC_RANDOM), expected, `Failed: ${title}`);
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
test('flipping bits changes the result', () => {
|
|
159
|
-
const asBigInt = (buf) => buf.reduce((acc, v) => (acc << 8n) | BigInt(v), 0n);
|
|
160
|
-
const asNumber = (bits, data) => Number(BigInt.asUintN(bits, data));
|
|
161
|
-
const flip = (data, n) => data ^ (1n << BigInt(127 - n));
|
|
162
|
-
const optionsFrom = (data) => {
|
|
163
|
-
const ms = asNumber(48, data >> 80n);
|
|
164
|
-
const hi = asNumber(12, data >> 64n);
|
|
165
|
-
const lo = asNumber(20, data >> 42n);
|
|
166
|
-
const r = BigInt.asUintN(42, data);
|
|
167
|
-
return {
|
|
168
|
-
msecs: ms,
|
|
169
|
-
seq: (hi << 20) | lo,
|
|
170
|
-
random: Uint8Array.from([
|
|
171
|
-
...Array(10).fill(0),
|
|
172
|
-
...Array(6)
|
|
173
|
-
.fill(0)
|
|
174
|
-
.map((_, i) => asNumber(8, r >> (BigInt(i) * 8n)))
|
|
175
|
-
.reverse(),
|
|
176
|
-
]),
|
|
177
|
-
};
|
|
178
|
-
};
|
|
179
|
-
const buf = new Uint8Array(16);
|
|
180
|
-
const data = asBigInt(v7({}, buf));
|
|
181
|
-
const id = stringify(buf);
|
|
182
|
-
const reserved = [48, 49, 50, 51, 64, 65];
|
|
183
|
-
for (let i = 0; i < 128; ++i) {
|
|
184
|
-
if (reserved.includes(i)) {
|
|
185
|
-
continue;
|
|
186
|
-
}
|
|
187
|
-
const flipped = flip(data, i);
|
|
188
|
-
assert.strictEqual(asBigInt(v7(optionsFrom(flipped), buf)).toString(16), flipped.toString(16), `Unequal uuids at bit ${i}`);
|
|
189
|
-
assert.notStrictEqual(stringify(buf), id);
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
});
|