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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: more TS porting
broofa committed Jun 11, 2024

Verified

This commit was signed with the committer’s verified signature.
broofa Robert Kieffer
commit c248e476fd09ec953cb4bef0360ba8d75c3fd4c1
2 changes: 2 additions & 0 deletions src/_types.ts
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@ export type Version4Options = {
rng?: () => Uint8Array;
};

export type Version6Options = Version1Options;

export type Version7Options = {
random?: Uint8Array;
msecs?: number;
2 changes: 1 addition & 1 deletion src/md5.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'node:crypto';

function md5(bytes) {
function md5(bytes: Uint8Array) {
if (Array.isArray(bytes)) {
bytes = Buffer.from(bytes);
} else if (typeof bytes === 'string') {
26 changes: 8 additions & 18 deletions src/sha1-browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Adapted from Chris Veness' SHA1 code at
// http://www.movable-type.co.uk/scripts/sha1.html
function f(s, x, y, z) {
function f(s: 0 | 1 | 2 | 3, x: number, y: number, z: number) {
switch (s) {
case 0:
return (x & y) ^ (~x & z);
@@ -13,28 +13,18 @@ function f(s, x, y, z) {
}
}

function ROTL(x, n) {
function ROTL(x: number, n: number) {
return (x << n) | (x >>> (32 - n));
}

function sha1(bytes) {
function sha1(bytes: Uint8Array) {
const K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
const H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];

if (typeof bytes === 'string') {
const msg = unescape(encodeURIComponent(bytes)); // UTF8 escape

bytes = [];

for (let i = 0; i < msg.length; ++i) {
bytes.push(msg.charCodeAt(i));
}
} else if (!Array.isArray(bytes)) {
// Convert Array-like to Array
bytes = Array.prototype.slice.call(bytes);
}

bytes.push(0x80);
const newBytes = new Uint8Array(bytes.length + 1);
newBytes.set(bytes);
newBytes[bytes.length] = 0x80;
bytes = newBytes;

const l = bytes.length / 4 + 2;
const N = Math.ceil(l / 16);
@@ -76,7 +66,7 @@ function sha1(bytes) {
let e = H[4];

for (let t = 0; t < 80; ++t) {
const s = Math.floor(t / 20);
const s = Math.floor(t / 20) as 0 | 1 | 2 | 3;
const T = (ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t]) >>> 0;
e = d;
d = c;
2 changes: 1 addition & 1 deletion src/sha1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'node:crypto';

function sha1(bytes: string | Uint8Array) {
function sha1(bytes: Uint8Array) {
if (Array.isArray(bytes)) {
bytes = Buffer.from(bytes);
} else if (typeof bytes === 'string') {
14 changes: 8 additions & 6 deletions src/uuid-bin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import assert from 'assert';

import { UUIDString } from './_types.js';
import v1 from './v1.js';
import v3 from './v3.js';
import { DNS, URL } from './v35.js';
import v4 from './v4.js';
import v5 from './v5.js';
import v6 from './v6.js';
@@ -44,14 +46,14 @@ switch (version) {
assert(namespace != null, 'v3 namespace not specified');

if (namespace === 'URL') {
namespace = v3.URL;
namespace = URL;
}

if (namespace === 'DNS') {
namespace = v3.DNS;
namespace = DNS;
}

console.log(v3(name, namespace));
console.log(v3(name, namespace as UUIDString));
break;
}

@@ -67,14 +69,14 @@ switch (version) {
assert(namespace != null, 'v5 namespace not specified');

if (namespace === 'URL') {
namespace = v5.URL;
namespace = URL;
}

if (namespace === 'DNS') {
namespace = v5.DNS;
namespace = DNS;
}

console.log(v5(name, namespace));
console.log(v5(name, namespace as UUIDString));
break;
}

8 changes: 5 additions & 3 deletions src/v1.ts
Original file line number Diff line number Diff line change
@@ -14,9 +14,11 @@ let _clockseq: number;
let _lastMSecs = 0;
let _lastNSecs = 0;

function v1(options: Version1Options, buf: undefined, offset?: number): UUIDString;
function v1(options: Version1Options, buf: Uint8Array, offset?: number): Uint8Array;
function v1(options: Version1Options = {}, buf?: Uint8Array, offset?: number): UUIDTypes {
function v1(options?: Version1Options, buf?: undefined, offset?: number): UUIDString;
function v1(options?: Version1Options, buf?: Uint8Array, offset?: number): Uint8Array;
function v1(options?: Version1Options, buf?: Uint8Array, offset?: number): UUIDTypes {
options ??= {};

let i = (buf && offset) || 0;
const b = buf || new Uint8Array(16);

19 changes: 15 additions & 4 deletions src/v3.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { UUIDString, UUIDTypes } from './_types.js';
import md5 from './md5.js';
import v35, { DNS } from './v35.js';
import v35 from './v35.js';

export { DNS, URL } from './v35.js';

function v3(
value: string | Uint8Array,
namespace: UUIDTypes,
buf?: undefined,
offset?: number
): UUIDString;
function v3(
value: string | Uint8Array,
namespace: UUIDTypes,
buf?: Uint8Array,
offset?: number
): UUIDString;
function v3(value: string | Uint8Array, namespace: UUIDTypes, buf?: Uint8Array, offset?: number) {
return v35(0x30, md5, value, namespace, buf, offset);
}

v3.DNS = DNS;
v3.URL = URL;

export default v3;
3 changes: 2 additions & 1 deletion src/v35.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@ import parse from './parse.js';
import { unsafeStringify } from './stringify.js';

function stringToBytes(str: string) {
str = unescape(encodeURIComponent(str)); // UTF8 escape
// TODO: Use TextEncoder (see https://stackoverflow.com/a/48762658/109538)
str = unescape(encodeURIComponent(str));

const bytes = new Uint8Array(str.length);

8 changes: 5 additions & 3 deletions src/v4.ts
Original file line number Diff line number Diff line change
@@ -3,9 +3,11 @@ import native from './native.js';
import rng from './rng.js';
import { unsafeStringify } from './stringify.js';

function v4(options: Version4Options, buf?: undefined, offset?: number): UUIDString;
function v4(options: Version4Options, buf?: Uint8Array, offset?: number): Uint8Array;
function v4(options: Version4Options = {}, buf?: Uint8Array, offset?: number): UUIDTypes {
function v4(options?: Version4Options, buf?: undefined, offset?: number): UUIDString;
function v4(options?: Version4Options, buf?: Uint8Array, offset?: number): Uint8Array;
function v4(options?: Version4Options, buf?: Uint8Array, offset?: number): UUIDTypes {
options ??= {};

if (native.randomUUID && !buf && !options) {
return native.randomUUID();
}
9 changes: 4 additions & 5 deletions src/v5.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { UUIDString, UUIDTypes } from './_types.js';
import sha1 from './sha1.js';
import v35, { DNS, URL } from './v35.js';
import v35 from './v35.js';

export { DNS, URL } from './v35.js';

function v5(
value: string | Uint8Array,
namespace: UUIDTypes,
buf?: Uint8Array,
buf?: undefined,
offset?: number
): UUIDString;
function v5(
@@ -18,7 +20,4 @@ function v5(value: string | Uint8Array, namespace: UUIDTypes, buf?: Uint8Array,
return v35(0x50, sha1, value, namespace, buf, offset);
}

v5.DNS = DNS;
v5.URL = URL;

export default v5;
15 changes: 6 additions & 9 deletions src/v6.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Version1Options } from './_types.js';
import { UUIDString, UUIDTypes, Version6Options } from './_types.js';
import { unsafeStringify } from './stringify.js';
import v1 from './v1.js';
import v1ToV6 from './v1ToV6.js';

/**
*
* @param {object} options
* @param {Uint8Array=} buf
* @param {number=} offset
* @returns
*/
export default function v6(options: Version1Options = {}, buf?: Uint8Array, offset = 0) {
export function v6(options?: Version6Options, buf?: undefined, offset?: number): UUIDString;
export function v6(options?: Version6Options, buf?: Uint8Array, offset?: number): Uint8Array;
export default function v6(options?: Version6Options, buf?: Uint8Array, offset = 0): UUIDTypes {
options ??= {};

// v6 is v1 with different field layout, so we start with a v1 UUID, albeit
// with slightly different behavior around how the clock_seq and node fields
// are randomized, which is why we call v1 with _v6: true.
8 changes: 5 additions & 3 deletions src/v7.ts
Original file line number Diff line number Diff line change
@@ -41,9 +41,11 @@ let _seqLow: number | null = null;
let _seqHigh: number | null = null;
let _msecs = 0;

function v7(options: Version7Options, buf: undefined, offset?: number): UUIDString;
function v7(options: Version7Options, buf: Uint8Array, offset?: number): Uint8Array;
function v7(options: Version7Options = {}, buf?: Uint8Array, offset?: number): UUIDTypes {
function v7(options?: Version7Options, buf?: undefined, offset?: number): UUIDString;
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);