Update checked-in dependencies

This commit is contained in:
github-actions[bot] 2025-01-06 17:12:55 +00:00
parent cf733fe86d
commit 07a86f82ca
167 changed files with 565 additions and 3893 deletions

View file

@ -1,2 +1,2 @@
declare function md5(bytes: Uint8Array): Uint8Array;
declare function md5(bytes: Uint8Array): Uint8Array<ArrayBuffer>;
export default md5;

View file

@ -1,2 +1,2 @@
declare function parse(uuid: string): Uint8Array;
declare function parse(uuid: string): Uint8Array<ArrayBuffer>;
export default parse;

View file

@ -1 +1 @@
export default function rng(): Uint8Array;
export default function rng(): Uint8Array<ArrayBuffer>;

View file

@ -1 +0,0 @@
export {};

View file

@ -1,48 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import parse from '../parse.js';
import stringify from '../stringify.js';
import uuidv4 from '../v4.js';
function splitmix32(a) {
return function () {
a |= 0;
a = (a + 0x9e3779b9) | 0;
let t = a ^ (a >>> 16);
t = Math.imul(t, 0x21f0aaad);
t = t ^ (t >>> 15);
t = Math.imul(t, 0x735a2d97);
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
};
}
const rand = splitmix32(0x12345678);
function rng(bytes = new Uint8Array(16)) {
for (let i = 0; i < 16; i++) {
bytes[i] = rand() * 256;
}
return bytes;
}
describe('parse', () => {
test('String -> bytes parsing', () => {
assert.deepStrictEqual(parse('0f5abcd1-c194-47f3-905b-2df7263a084b'), Uint8Array.from([
0x0f, 0x5a, 0xbc, 0xd1, 0xc1, 0x94, 0x47, 0xf3, 0x90, 0x5b, 0x2d, 0xf7, 0x26, 0x3a, 0x08,
0x4b,
]));
});
test('String -> bytes -> string symmetry for assorted uuids', () => {
for (let i = 0; i < 1000; i++) {
const uuid = uuidv4({ rng });
assert.equal(stringify(parse(uuid)), uuid);
}
});
test('Case neutrality', () => {
assert.deepStrictEqual(parse('0f5abcd1-c194-47f3-905b-2df7263a084b'), parse('0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()));
});
test('Null UUID case', () => {
assert.deepStrictEqual(parse('00000000-0000-0000-0000-000000000000'), Uint8Array.from(new Array(16).fill(0)));
});
test('UUID validation', () => {
assert.throws(() => parse());
assert.throws(() => parse('invalid uuid'));
assert.throws(() => parse('zyxwvuts-rqpo-nmlk-jihg-fedcba000000'));
});
});

View file

@ -1 +0,0 @@
export {};

View file

@ -1,12 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import rng from '../rng.js';
describe('rng', () => {
test('Node.js RNG', () => {
const bytes = rng();
assert.equal(bytes.length, 16);
for (let i = 0; i < bytes.length; ++i) {
assert.equal(typeof bytes[i], 'number');
}
});
});

View file

@ -1 +0,0 @@
export {};

View file

@ -1,21 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import stringify, { unsafeStringify } from '../stringify.js';
const BYTES = Uint8Array.of(0x0f, 0x5a, 0xbc, 0xd1, 0xc1, 0x94, 0x47, 0xf3, 0x90, 0x5b, 0x2d, 0xf7, 0x26, 0x3a, 0x08, 0x4b);
describe('stringify', () => {
test('Stringify Array (unsafe)', () => {
assert.equal(unsafeStringify(BYTES), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});
test('Stringify w/ offset (unsafe)', () => {
const bytes = new Uint8Array(19).fill(0);
bytes.set(BYTES, 3);
assert.equal(unsafeStringify(bytes, 3), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});
test('Stringify Array (safe)', () => {
assert.equal(stringify(BYTES), '0f5abcd1-c194-47f3-905b-2df7263a084b');
});
test('Throws on not enough values (safe)', () => {
const bytes = BYTES.slice(0, 15);
assert.throws(() => stringify(bytes));
});
});

View file

@ -1,33 +0,0 @@
export declare const TESTS: ({
value: string;
expectedValidate: boolean;
expectedVersion: number;
} | {
value: string;
expectedValidate: boolean;
expectedVersion?: undefined;
} | {
value: undefined;
expectedValidate: boolean;
expectedVersion?: undefined;
} | {
value: null;
expectedValidate: boolean;
expectedVersion?: undefined;
} | {
value: number;
expectedValidate: boolean;
expectedVersion?: undefined;
} | {
value: RegExp;
expectedValidate: boolean;
expectedVersion?: undefined;
} | {
value: Date;
expectedValidate: boolean;
expectedVersion?: undefined;
} | {
value: boolean;
expectedValidate: boolean;
expectedVersion?: undefined;
})[];

View file

@ -1,88 +0,0 @@
import MAX from '../max.js';
import NIL from '../nil.js';
export const TESTS = [
{ value: NIL, expectedValidate: true, expectedVersion: 0 },
{ value: MAX, expectedValidate: true, expectedVersion: 15 },
{ value: '00000000-0000-1000-8000-000000000000', expectedValidate: true, expectedVersion: 1 },
{ value: 'ffffffff-ffff-1fff-8fff-ffffffffffff', expectedValidate: true, expectedVersion: 1 },
{ value: '00000000-0000-2000-8000-000000000000', expectedValidate: true, expectedVersion: 2 },
{ value: 'ffffffff-ffff-2fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 2 },
{ value: '00000000-0000-3000-8000-000000000000', expectedValidate: true, expectedVersion: 3 },
{ value: 'ffffffff-ffff-3fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 3 },
{ value: '00000000-0000-4000-8000-000000000000', expectedValidate: true, expectedVersion: 4 },
{ value: 'ffffffff-ffff-4fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 4 },
{ value: '00000000-0000-5000-8000-000000000000', expectedValidate: true, expectedVersion: 5 },
{ value: 'ffffffff-ffff-5fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 5 },
{ value: '00000000-0000-6000-8000-000000000000', expectedValidate: true, expectedVersion: 6 },
{ value: 'ffffffff-ffff-6fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 6 },
{ value: '00000000-0000-7000-8000-000000000000', expectedValidate: true, expectedVersion: 7 },
{ value: 'ffffffff-ffff-7fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 7 },
{ value: '00000000-0000-8000-8000-000000000000', expectedValidate: true, expectedVersion: 8 },
{ value: 'ffffffff-ffff-8fff-bfff-ffffffffffff', expectedValidate: true, expectedVersion: 8 },
{ value: '00000000-0000-9000-8000-000000000000', expectedValidate: false },
{ value: 'ffffffff-ffff-9fff-bfff-ffffffffffff', expectedValidate: false },
{ value: '00000000-0000-a000-8000-000000000000', expectedValidate: false },
{ value: 'ffffffff-ffff-afff-bfff-ffffffffffff', expectedValidate: false },
{ value: '00000000-0000-b000-8000-000000000000', expectedValidate: false },
{ value: 'ffffffff-ffff-bfff-bfff-ffffffffffff', expectedValidate: false },
{ value: '00000000-0000-c000-8000-000000000000', expectedValidate: false },
{ value: 'ffffffff-ffff-cfff-bfff-ffffffffffff', expectedValidate: false },
{ value: '00000000-0000-d000-8000-000000000000', expectedValidate: false },
{ value: 'ffffffff-ffff-dfff-bfff-ffffffffffff', expectedValidate: false },
{ value: '00000000-0000-e000-8000-000000000000', expectedValidate: false },
{ value: 'ffffffff-ffff-efff-bfff-ffffffffffff', expectedValidate: false },
{ value: 'd9428888-122b-11e1-b85c-61cd3cbb3210', expectedValidate: true, expectedVersion: 1 },
{ value: '000003e8-2363-21ef-b200-325096b39f47', expectedValidate: true, expectedVersion: 2 },
{ value: 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec', expectedValidate: true, expectedVersion: 3 },
{ value: '109156be-c4fb-41ea-b1b4-efe1671c5836', expectedValidate: true, expectedVersion: 4 },
{ value: '90123e1c-7512-523e-bb28-76fab9f2f73d', expectedValidate: true, expectedVersion: 5 },
{ value: '1ef21d2f-1207-6660-8c4f-419efbd44d48', expectedValidate: true, expectedVersion: 6 },
{ value: '017f22e2-79b0-7cc3-98c4-dc0c0c07398f', expectedValidate: true, expectedVersion: 7 },
{ value: '0d8f23a0-697f-83ae-802e-48f3756dd581', expectedValidate: true, expectedVersion: 8 },
{ value: '00000000-0000-1000-0000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-1000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-2000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-3000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-4000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-5000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-6000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-7000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-8000-000000000000', expectedValidate: true, expectedVersion: 1 },
{ value: '00000000-0000-1000-9000-000000000000', expectedValidate: true, expectedVersion: 1 },
{ value: '00000000-0000-1000-a000-000000000000', expectedValidate: true, expectedVersion: 1 },
{ value: '00000000-0000-1000-b000-000000000000', expectedValidate: true, expectedVersion: 1 },
{ value: '00000000-0000-1000-c000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-d000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-e000-000000000000', expectedValidate: false },
{ value: '00000000-0000-1000-f000-000000000000', expectedValidate: false },
{ value: '00000000000000000000000000000000', expectedValidate: false },
{ value: '', expectedValidate: false },
{ value: 'invalid uuid string', expectedValidate: false },
{
value: '=Y00a-f*vb*-c-d#-p00f\b-g0h-#i^-j*3&-L00k-\nl---00n-fg000-00p-00r+',
expectedValidate: false,
},
{ value: undefined, expectedValidate: false },
{ value: null, expectedValidate: false },
{ value: 123, expectedValidate: false },
{ value: /regex/, expectedValidate: false },
{ value: new Date(0), expectedValidate: false },
{ value: false, expectedValidate: false },
];
for (let charIndex = 0; charIndex < 36; charIndex++) {
if (charIndex === 8 ||
charIndex === 13 ||
charIndex === 14 ||
charIndex === 18 ||
charIndex === 23) {
continue;
}
const nilChars = NIL.split('');
const maxChars = MAX.split('');
for (let i = 0; i < 4; i++) {
nilChars[charIndex] = (0x0 ^ (1 << i)).toString(16);
TESTS.push({ value: nilChars.join(''), expectedValidate: false });
maxChars[charIndex] = (0xf ^ (1 << i)).toString(16);
TESTS.push({ value: maxChars.join(''), expectedValidate: false });
}
}

View file

@ -1 +0,0 @@
export {};

View file

@ -1,125 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import parse from '../parse.js';
import v1, { updateV1State } from '../v1.js';
const TIME = 1321644961388;
const RFC_V1 = 'c232ab00-9414-11ec-b3c8-9f68deced846';
const RFC_V1_BYTES = parse(RFC_V1);
const RFC_OPTIONS = {
msecs: 0x17f22e279b0,
nsecs: 0,
clockseq: 0x33c8,
node: Uint8Array.of(0x9f, 0x68, 0xde, 0xce, 0xd8, 0x46),
};
const RFC_RANDOM = Uint8Array.of(0, 0, 0, 0, 0, 0, 0, 0, RFC_OPTIONS.clockseq >> 8, RFC_OPTIONS.clockseq & 0xff, ...RFC_OPTIONS.node);
function compareV1TimeField(a, b) {
a = a.split('-').slice(0, 3).reverse().join('');
b = b.split('-').slice(0, 3).reverse().join('');
return a < b ? -1 : a > b ? 1 : 0;
}
describe('v1', () => {
test('v1 sort order (default)', () => {
const ids = [v1(), v1(), v1(), v1(), v1()];
const sorted = [...ids].sort(compareV1TimeField);
assert.deepEqual(ids, sorted);
});
test('v1 sort order (time option)', () => {
const ids = [
v1({ msecs: TIME - 10 * 3600 * 1000 }),
v1({ msecs: TIME - 1 }),
v1({ msecs: TIME }),
v1({ msecs: TIME + 1 }),
v1({ msecs: TIME + 28 * 24 * 3600 * 1000 }),
];
const sorted = [...ids].sort(compareV1TimeField);
assert.deepEqual(ids, sorted);
});
test('v1(options)', () => {
assert.equal(v1({ msecs: RFC_OPTIONS.msecs, random: RFC_RANDOM }), RFC_V1, 'minimal options');
assert.equal(v1(RFC_OPTIONS), RFC_V1, 'full options');
});
test('v1(options) equality', () => {
assert.notEqual(v1({ msecs: TIME }), v1({ msecs: TIME }), 'UUIDs with minimal options differ');
assert.equal(v1(RFC_OPTIONS), v1(RFC_OPTIONS), 'UUIDs with full options are identical');
});
test('fills one UUID into a buffer as expected', () => {
const buffer = new Uint8Array(16);
const result = v1(RFC_OPTIONS, buffer);
assert.deepEqual(buffer, RFC_V1_BYTES);
assert.strictEqual(buffer, result);
});
test('fills two UUIDs into a buffer as expected', () => {
const buffer = new Uint8Array(32);
v1(RFC_OPTIONS, buffer, 0);
v1(RFC_OPTIONS, buffer, 16);
const expectedBuf = new Uint8Array(32);
expectedBuf.set(RFC_V1_BYTES);
expectedBuf.set(RFC_V1_BYTES, 16);
assert.deepEqual(buffer, expectedBuf);
});
test('v1() state transitions', () => {
const PRE_STATE = {
msecs: 10,
nsecs: 20,
clockseq: 0x1234,
node: Uint8Array.of(0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc),
};
const tests = [
{
title: 'initial state',
state: {},
now: 10,
expected: {
msecs: 10,
nsecs: 0,
clockseq: RFC_OPTIONS.clockseq,
node: RFC_OPTIONS.node,
},
},
{
title: 'same time interval',
state: { ...PRE_STATE },
now: PRE_STATE.msecs,
expected: {
...PRE_STATE,
nsecs: 21,
},
},
{
title: 'new time interval',
state: { ...PRE_STATE },
now: PRE_STATE.msecs + 1,
expected: {
...PRE_STATE,
msecs: PRE_STATE.msecs + 1,
nsecs: 0,
},
},
{
title: 'same time interval (nsecs overflow)',
state: { ...PRE_STATE, nsecs: 9999 },
now: PRE_STATE.msecs,
expected: {
...PRE_STATE,
nsecs: 0,
clockseq: RFC_OPTIONS.clockseq,
node: RFC_OPTIONS.node,
},
},
{
title: 'time regression',
state: { ...PRE_STATE },
now: PRE_STATE.msecs - 1,
expected: {
...PRE_STATE,
msecs: PRE_STATE.msecs - 1,
clockseq: RFC_OPTIONS.clockseq,
node: RFC_OPTIONS.node,
},
},
];
for (const { title, state, now, expected } of tests) {
assert.deepStrictEqual(updateV1State(state, now, RFC_RANDOM), expected, `Failed: ${title}`);
}
});
});

View file

@ -1 +0,0 @@
export {};

View file

@ -1,141 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import md5 from '../md5.js';
import sha1 from '../sha1.js';
import v3 from '../v3.js';
import { stringToBytes } from '../v35.js';
import v5 from '../v5.js';
describe('v35', () => {
const HASH_SAMPLES = [
{
input: stringToBytes(''),
sha1: 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
md5: 'd41d8cd98f00b204e9800998ecf8427e',
},
{
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'),
sha1: 'ca4a426a3d536f14cfd79011e79e10d64de950a0',
md5: 'e8098ec21950f841731d28749129d3ee',
},
{
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'),
sha1: 'f2753ebc390e5f637e333c2a4179644a93ae9f65',
md5: '231b309e277b6be8bb3d6c688b7f098b',
},
];
function hashToHex(hash) {
const chars = new Array(hash.length);
for (let i = 0; i < hash.length; i++) {
chars[i] = hash[i].toString(16).padStart(2, '0');
}
return chars.join('');
}
HASH_SAMPLES.forEach(function (sample, i) {
test(`sha1(node) HASH_SAMPLES[${i}]`, () => {
assert.equal(hashToHex(sha1(sample.input)), sample.sha1);
});
});
HASH_SAMPLES.forEach(function (sample, i) {
test(`md5(node) HASH_SAMPLES[${i}]`, () => {
assert.equal(hashToHex(md5(sample.input)), sample.md5);
});
});
test('v3', () => {
assert.strictEqual(v3('hello.example.com', v3.DNS), '9125a8dc-52ee-365b-a5aa-81b0b3681cf6');
assert.strictEqual(v3('http://example.com/hello', v3.URL), 'c6235813-3ba4-3801-ae84-e0a6ebb7d138');
assert.strictEqual(v3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'), 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec');
});
test('v3 namespace.toUpperCase', () => {
assert.strictEqual(v3('hello.example.com', v3.DNS.toUpperCase()), '9125a8dc-52ee-365b-a5aa-81b0b3681cf6');
assert.strictEqual(v3('http://example.com/hello', v3.URL.toUpperCase()), 'c6235813-3ba4-3801-ae84-e0a6ebb7d138');
assert.strictEqual(v3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()), 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec');
});
test('v3 namespace string validation', () => {
assert.throws(() => {
v3('hello.example.com', 'zyxwvuts-rqpo-nmlk-jihg-fedcba000000');
});
assert.throws(() => {
v3('hello.example.com', 'invalid uuid value');
});
assert.ok(v3('hello.example.com', '00000000-0000-0000-0000-000000000000'));
});
test('v3 namespace buffer validation', () => {
assert.throws(() => {
v3('hello.example.com', new Uint8Array(15));
});
assert.throws(() => {
v3('hello.example.com', new Uint8Array(17));
});
assert.ok(v3('hello.example.com', new Uint8Array(16).fill(0)));
});
test('v3 fill buffer', () => {
let buf = new Uint8Array(16);
const expectedUuid = Uint8Array.of(0x91, 0x25, 0xa8, 0xdc, 0x52, 0xee, 0x36, 0x5b, 0xa5, 0xaa, 0x81, 0xb0, 0xb3, 0x68, 0x1c, 0xf6);
const result = v3('hello.example.com', v3.DNS, buf);
assert.deepEqual(buf, expectedUuid);
assert.strictEqual(result, buf);
buf = new Uint8Array(19).fill(0xaa);
const expectedBuf = new Uint8Array(19).fill(0xaa);
expectedBuf.set(expectedUuid, 3);
v3('hello.example.com', v3.DNS, buf, 3);
assert.deepEqual(buf, expectedBuf);
});
test('v3 undefined/null', () => {
assert.throws(() => v3());
assert.throws(() => v3('hello'));
assert.throws(() => v3('hello.example.com', undefined));
assert.throws(() => v3('hello.example.com', null, new Uint8Array(16)));
});
test('v5', () => {
assert.strictEqual(v5('hello.example.com', v5.DNS), 'fdda765f-fc57-5604-a269-52a7df8164ec');
assert.strictEqual(v5('http://example.com/hello', v5.URL), '3bbcee75-cecc-5b56-8031-b6641c1ed1f1');
assert.strictEqual(v5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'), '90123e1c-7512-523e-bb28-76fab9f2f73d');
});
test('v5 namespace.toUpperCase', () => {
assert.strictEqual(v5('hello.example.com', v5.DNS.toUpperCase()), 'fdda765f-fc57-5604-a269-52a7df8164ec');
assert.strictEqual(v5('http://example.com/hello', v5.URL.toUpperCase()), '3bbcee75-cecc-5b56-8031-b6641c1ed1f1');
assert.strictEqual(v5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()), '90123e1c-7512-523e-bb28-76fab9f2f73d');
});
test('v5 namespace string validation', () => {
assert.throws(() => {
v5('hello.example.com', 'zyxwvuts-rqpo-nmlk-jihg-fedcba000000');
});
assert.throws(() => {
v5('hello.example.com', 'invalid uuid value');
});
assert.ok(v5('hello.example.com', '00000000-0000-0000-0000-000000000000'));
});
test('v5 namespace buffer validation', () => {
assert.throws(() => {
v5('hello.example.com', new Uint8Array(15));
});
assert.throws(() => {
v5('hello.example.com', new Uint8Array(17));
});
assert.ok(v5('hello.example.com', new Uint8Array(16).fill(0)));
});
test('v5 fill buffer', () => {
let buf = new Uint8Array(16);
const expectedUuid = Uint8Array.of(0xfd, 0xda, 0x76, 0x5f, 0xfc, 0x57, 0x56, 0x04, 0xa2, 0x69, 0x52, 0xa7, 0xdf, 0x81, 0x64, 0xec);
const result = v5('hello.example.com', v5.DNS, buf);
assert.deepEqual(buf, expectedUuid);
assert.strictEqual(result, buf);
buf = new Uint8Array(19).fill(0xaa);
const expectedBuf = new Uint8Array(19).fill(0xaa);
expectedBuf.set(expectedUuid, 3);
v5('hello.example.com', v5.DNS, buf, 3);
assert.deepEqual(buf, expectedBuf);
});
test('v5 undefined/null', () => {
assert.throws(() => v5());
assert.throws(() => v5('hello'));
assert.throws(() => v5('hello.example.com', undefined));
assert.throws(() => v5('hello.example.com', null, new Uint8Array(16)));
});
test('v3/v5 constants', () => {
assert.strictEqual(v3.DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
assert.strictEqual(v3.URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
assert.strictEqual(v5.DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
assert.strictEqual(v5.URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
});
});

View file

@ -1 +0,0 @@
export {};

View file

@ -1,58 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import native from '../native.js';
import v4 from '../v4.js';
const randomBytesFixture = Uint8Array.of(0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea, 0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36);
const expectedBytes = Uint8Array.of(16, 145, 86, 190, 196, 251, 65, 234, 177, 180, 239, 225, 103, 28, 88, 54);
describe('v4', () => {
test('subsequent UUIDs are different', () => {
const id1 = v4();
const id2 = v4();
assert.ok(id1 !== id2);
});
test('should uses native randomUUID() if no option is passed', async () => {
const mock = (await import('node:test')).default.mock;
if (!mock) {
return;
}
const mockRandomUUID = mock.method(native, 'randomUUID');
assert.equal(mockRandomUUID.mock.callCount(), 0);
v4();
assert.equal(mockRandomUUID.mock.callCount(), 1);
mock.restoreAll();
});
test('should not use native randomUUID() if an option is passed', async () => {
const mock = (await import('node:test')).default.mock;
if (!mock) {
return;
}
const mockRandomUUID = mock.method(native, 'randomUUID');
assert.equal(mockRandomUUID.mock.callCount(), 0);
v4({});
assert.equal(mockRandomUUID.mock.callCount(), 0);
mock.restoreAll();
});
test('explicit options.random produces expected result', () => {
const id = v4({ random: randomBytesFixture });
assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');
});
test('explicit options.rng produces expected result', () => {
const id = v4({ rng: () => randomBytesFixture });
assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');
});
test('fills one UUID into a buffer as expected', () => {
const buffer = new Uint8Array(16);
const result = v4({ random: randomBytesFixture }, buffer);
assert.deepEqual(buffer, expectedBytes);
assert.strictEqual(buffer, result);
});
test('fills two UUIDs into a buffer as expected', () => {
const buffer = new Uint8Array(32);
v4({ random: randomBytesFixture }, buffer, 0);
v4({ random: randomBytesFixture }, buffer, 16);
const expectedBuf = new Uint8Array(32);
expectedBuf.set(expectedBytes);
expectedBuf.set(expectedBytes, 16);
assert.deepEqual(buffer, expectedBuf);
});
});

View file

@ -1 +0,0 @@
export {};

View file

@ -1,54 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import v1ToV6 from '../v1ToV6.js';
import v6 from '../v6.js';
import v6ToV1 from '../v6ToV1.js';
describe('v6', () => {
const V1_ID = 'f1207660-21d2-11ef-8c4f-419efbd44d48';
const V6_ID = '1ef21d2f-1207-6660-8c4f-419efbd44d48';
const fullOptions = {
msecs: 0x133b891f705,
nsecs: 0x1538,
clockseq: 0x385c,
node: Uint8Array.of(0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10),
};
const EXPECTED_BYTES = Uint8Array.of(0x1e, 0x11, 0x22, 0xbd, 0x94, 0x28, 0x68, 0x88, 0xb8, 0x5c, 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10);
test('default behavior', () => {
const id = v6();
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');
});
test('default behavior (binary type)', () => {
const buffer = new Uint8Array(16);
const result = v6(fullOptions, buffer);
assert.deepEqual(buffer, EXPECTED_BYTES);
assert.strictEqual(buffer, result);
});
test('all options', () => {
const id = v6(fullOptions);
assert.equal(id, '1e1122bd-9428-6888-b85c-61cd3cbb3210');
});
test('sort by creation time', () => {
const ids = [];
for (let i = 0; i < 5; i++) {
ids.push(v6({ msecs: i * 1000 }));
}
assert.deepEqual(ids, ids.slice().sort());
});
test('creating at array offset', () => {
const buffer = new Uint8Array(32);
v6(fullOptions, buffer, 0);
v6(fullOptions, buffer, 16);
const expectedBuf = new Uint8Array(32);
expectedBuf.set(EXPECTED_BYTES, 0);
expectedBuf.set(EXPECTED_BYTES, 16);
assert.deepEqual(buffer, expectedBuf);
});
test('v1 -> v6 conversion', () => {
const id = v1ToV6(V1_ID);
assert.equal(id, V6_ID);
});
test('v6 -> v1 conversion', () => {
const id = v6ToV1(V6_ID);
assert.equal(id, V1_ID);
});
});

View file

@ -1 +0,0 @@
export {};

View file

@ -1,192 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import parse from '../parse.js';
import stringify from '../stringify.js';
import v7, { updateV7State } from '../v7.js';
const RFC_V7 = '017f22e2-79b0-7cc3-98c4-dc0c0c07398f';
const RFC_V7_BYTES = parse('017f22e2-79b0-7cc3-98c4-dc0c0c07398f');
const RFC_MSECS = 0x17f22e279b0;
const RFC_SEQ = (0x0cc3 << 20) | (0x98c4dc >> 2);
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();
assert.ok(id1 !== id2);
});
test('explicit options.random and options.msecs produces expected result', () => {
const id = v7({
random: RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
});
assert.strictEqual(id, RFC_V7);
});
test('explicit options.rng produces expected result', () => {
const id = v7({
rng: () => RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
});
assert.strictEqual(id, RFC_V7);
});
test('explicit options.msecs produces expected result', () => {
const id = v7({
msecs: RFC_MSECS,
});
assert.strictEqual(id.indexOf('017f22e2'), 0);
});
test('fills one UUID into a buffer as expected', () => {
const buffer = new Uint8Array(16);
const result = v7({
random: RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
}, buffer);
stringify(buffer);
assert.deepEqual(buffer, RFC_V7_BYTES);
assert.strictEqual(buffer, result);
});
test('fills two UUIDs into a buffer as expected', () => {
const buffer = new Uint8Array(32);
v7({
random: RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
}, buffer, 0);
v7({
random: RFC_RANDOM,
msecs: RFC_MSECS,
seq: RFC_SEQ,
}, buffer, 16);
const expected = new Uint8Array(32);
expected.set(RFC_V7_BYTES);
expected.set(RFC_V7_BYTES, 16);
assert.deepEqual(buffer, expected);
});
test('lexicographical sorting is preserved', () => {
let id;
let prior;
let msecs = RFC_MSECS;
for (let i = 0; i < 20000; ++i) {
if (i % 1500 === 0) {
msecs += 1;
}
id = v7({ msecs, seq: i });
if (prior !== undefined) {
assert.ok(prior < id, `${prior} < ${id}`);
}
prior = id;
}
});
test('can supply seq', () => {
let seq = 0x12345;
let uuid = v7({
msecs: RFC_MSECS,
seq,
});
assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7000-848d-1');
seq = 0x6fffffff;
uuid = v7({
msecs: RFC_MSECS,
seq,
});
assert.strictEqual(uuid.substring(0, 25), '017f22e2-79b0-76ff-bfff-f');
});
test('internal seq is reset upon timestamp change', () => {
v7({
msecs: RFC_MSECS,
seq: 0x6fffffff,
});
const uuid = v7({
msecs: RFC_MSECS + 1,
});
assert.ok(uuid.indexOf('fff') !== 15);
});
test('v7() state transitions', () => {
const tests = [
{
title: 'new time interval',
state: { msecs: 1, seq: 123 },
now: 2,
expected: {
msecs: 2,
seq: 0x6c318c4,
},
},
{
title: 'same time interval',
state: { msecs: 1, seq: 123 },
now: 1,
expected: {
msecs: 1,
seq: 124,
},
},
{
title: 'same time interval (sequence rollover)',
state: { msecs: 1, seq: 0xffffffff },
now: 1,
expected: {
msecs: 2,
seq: 0,
},
},
{
title: 'time regression',
state: { msecs: 2, seq: 123 },
now: 1,
expected: {
msecs: 2,
seq: 124,
},
},
{
title: 'time regression (sequence rollover)',
state: { msecs: 2, seq: 0xffffffff },
now: 1,
expected: {
msecs: 3,
seq: 0,
},
},
];
for (const { title, state, now, expected } of tests) {
assert.deepStrictEqual(updateV7State(state, now, RFC_RANDOM), expected, `Failed: ${title}`);
}
});
test('flipping bits changes the result', () => {
const asBigInt = (buf) => buf.reduce((acc, v) => (acc << 8n) | BigInt(v), 0n);
const asNumber = (bits, data) => Number(BigInt.asUintN(bits, data));
const flip = (data, n) => data ^ (1n << BigInt(127 - n));
const optionsFrom = (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 << 20) | lo,
random: Uint8Array.from([
...Array(10).fill(0),
...Array(6)
.fill(0)
.map((_, i) => asNumber(8, r >> (BigInt(i) * 8n)))
.reverse(),
]),
};
};
const buf = new Uint8Array(16);
const data = asBigInt(v7({}, buf));
const id = stringify(buf);
const reserved = [48, 49, 50, 51, 64, 65];
for (let i = 0; i < 128; ++i) {
if (reserved.includes(i)) {
continue;
}
const flipped = flip(data, i);
assert.strictEqual(asBigInt(v7(optionsFrom(flipped), buf)).toString(16), flipped.toString(16), `Unequal uuids at bit ${i}`);
assert.notStrictEqual(stringify(buf), id);
}
});
});

View file

@ -1 +0,0 @@
export {};

View file

@ -1,11 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import validate from '../validate.js';
import { TESTS } from './test_constants.js';
describe('validate()', () => {
test('TESTS cases', () => {
for (const { value, expectedValidate } of TESTS) {
assert.strictEqual(validate(value), expectedValidate, `validate(${value}) should be ${expectedValidate}`);
}
});
});

View file

@ -1 +0,0 @@
export {};

View file

@ -1,18 +0,0 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import version from '../version.js';
import { TESTS } from './test_constants.js';
describe('version()', () => {
test('TESTS cases', () => {
for (const { value, expectedValidate, expectedVersion } of TESTS) {
try {
const actualVersion = version(value);
assert.ok(expectedValidate, `version(${value}) should throw`);
assert.strictEqual(actualVersion, expectedVersion);
}
catch {
assert.ok(!expectedValidate, `version(${value}) threw unexpectedly`);
}
}
});
});

View file

@ -46,10 +46,18 @@ export function updateV1State(state, now, rnds) {
return state;
}
function v1Bytes(rnds, msecs, nsecs, clockseq, node, buf, offset = 0) {
if (rnds.length < 16) {
throw new Error('Random bytes length must be >= 16');
}
if (!buf) {
buf = new Uint8Array(16);
offset = 0;
}
else {
if (offset < 0 || offset + 16 > buf.length) {
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
}
}
msecs ??= Date.now();
nsecs ??= 0;
clockseq ??= ((rnds[8] << 8) | rnds[9]) & 0x3fff;

View file

@ -1,7 +1,7 @@
import { UUIDTypes } from './types.js';
export declare function stringToBytes(str: string): Uint8Array;
export declare function stringToBytes(str: string): Uint8Array<ArrayBuffer>;
export declare const DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
export declare const URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
type HashFunction = (bytes: Uint8Array) => Uint8Array;
export default function v35(version: 0x30 | 0x50, hash: HashFunction, value: string | Uint8Array, namespace: UUIDTypes, buf?: Uint8Array, offset?: number): string | Uint8Array;
export default function v35(version: 0x30 | 0x50, hash: HashFunction, value: string | Uint8Array, namespace: UUIDTypes, buf?: Uint8Array, offset?: number): string | Uint8Array<ArrayBufferLike>;
export {};

View file

@ -6,11 +6,17 @@ function v4(options, buf, offset) {
return native.randomUUID();
}
options = options || {};
const rnds = options.random || (options.rng || rng)();
const rnds = options.random ?? options.rng?.() ?? rng();
if (rnds.length < 16) {
throw new Error('Random bytes length must be >= 16');
}
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;
if (buf) {
offset = offset || 0;
if (offset < 0 || offset + 16 > buf.length) {
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
}
for (let i = 0; i < 16; ++i) {
buf[offset + i] = rnds[i];
}

View file

@ -30,10 +30,18 @@ export function updateV7State(state, now, rnds) {
return state;
}
function v7Bytes(rnds, msecs, seq, buf, offset = 0) {
if (rnds.length < 16) {
throw new Error('Random bytes length must be >= 16');
}
if (!buf) {
buf = new Uint8Array(16);
offset = 0;
}
else {
if (offset < 0 || offset + 16 > buf.length) {
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
}
}
msecs ??= Date.now();
seq ??= ((rnds[6] * 0x7f) << 24) | (rnds[7] << 16) | (rnds[8] << 8) | rnds[9];
buf[offset++] = (msecs / 0x10000000000) & 0xff;