Skip to content

Commit

Permalink
chore: more TS porting
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Jun 10, 2024
1 parent d910db9 commit 3e3abe9
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 69 deletions.
26 changes: 26 additions & 0 deletions src/_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copied from crypto.randomUUID()
export type UUIDString = `${string}-${string}-${string}-${string}-${string}`;

export type UUIDTypes = UUIDString | Uint8Array;

export type Version1Options = {
node?: Uint8Array;
clockseq?: number;
random?: Uint8Array;
rng?: () => Uint8Array;
msecs?: number;
nsecs?: number;
_v6?: boolean; // Internal use only!
};

export type Version4Options = {
random?: Uint8Array;
rng?: () => Uint8Array;
};

export type Version7Options = {
random?: Uint8Array;
msecs?: number;
seq?: number;
rng?: () => Uint8Array;
};
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) {
function sha1(bytes: string | Uint8Array) {
if (Array.isArray(bytes)) {
bytes = Buffer.from(bytes);
} else if (typeof bytes === 'string') {
Expand Down
8 changes: 2 additions & 6 deletions src/v1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UUIDString, Version1Options } from './_types.js';
import { UUIDString, UUIDTypes, Version1Options } from './_types.js';
import rng from './rng.js';
import { unsafeStringify } from './stringify.js';

Expand All @@ -16,11 +16,7 @@ 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
): UUIDString | Uint8Array {
function v1(options: Version1Options = {}, buf?: Uint8Array, offset?: number): UUIDTypes {
let i = (buf && offset) || 0;
const b = buf || new Uint8Array(16);

Expand Down
4 changes: 2 additions & 2 deletions src/v1ToV6.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UUIDString } from './_types.js';
import { UUIDString, UUIDTypes } from './_types.js';
import parse from './parse.js';
import { unsafeStringify } from './stringify.js';

Expand All @@ -11,7 +11,7 @@ import { unsafeStringify } from './stringify.js';
*/
export default function v1ToV6(uuid: UUIDString): UUIDString;
export default function v1ToV6(uuid: Uint8Array): Uint8Array;
export default function v1ToV6(uuid: string | Uint8Array): UUIDString | Uint8Array {
export default function v1ToV6(uuid: string | Uint8Array): UUIDTypes {
const v1Bytes = typeof uuid === 'string' ? parse(uuid) : uuid;

const v6Bytes = _v1ToV6(v1Bytes);
Expand Down
11 changes: 9 additions & 2 deletions src/v3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import v35 from './v35.js';
import { UUIDString, UUIDTypes } from './_types.js';
import md5 from './md5.js';
import v35, { DNS } from './v35.js';

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;

const v3 = v35('v3', 0x30, md5);
export default v3;
90 changes: 48 additions & 42 deletions src/v35.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { unsafeStringify } from './stringify.js';
import { UUIDString, UUIDTypes } from './_types.js';
import parse from './parse.js';
import { unsafeStringify } from './stringify.js';

function stringToBytes(str) {
function stringToBytes(str: string) {
str = unescape(encodeURIComponent(str)); // UTF8 escape

const bytes = [];
const bytes = new Uint8Array(str.length);

for (let i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
bytes[i] = str.charCodeAt(i);
}

return bytes;
Expand All @@ -16,52 +17,57 @@ function stringToBytes(str) {
export const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
export const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';

export default function v35(name, version, hashfunc) {
function generateUUID(value, namespace, buf, offset) {
if (typeof value === 'string') {
value = stringToBytes(value);
}

if (typeof namespace === 'string') {
namespace = parse(namespace);
}
type HashFunction = (bytes: Uint8Array) => Uint8Array;

type NamespaceUuidFunction<BufType, ReturnType> = (
value: string | Uint8Array,
namespace: string | Uint8Array,
buf?: BufType,
offset?: number
) => ReturnType;

export default function v35(
version: 0x30 | 0x50,
hash: HashFunction,
value: string | Uint8Array,
namespace: UUIDTypes,
buf?: Uint8Array,
offset?: number
) {
const valueBytes: Uint8Array = typeof value === 'string' ? stringToBytes(value) : value;
const namespaceBytes: Uint8Array = typeof namespace === 'string' ? parse(namespace) : namespace;
if (typeof value === 'string') {
value = stringToBytes(value);
}

if (namespace?.length !== 16) {
throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
}
if (typeof namespace === 'string') {
namespace = parse(namespace);
}

// Compute hash of namespace and value, Per 4.3
// Future: Use spread syntax when supported on all platforms, e.g. `bytes =
// hashfunc([...namespace, ... value])`
let bytes = new Uint8Array(16 + value.length);
bytes.set(namespace);
bytes.set(value, namespace.length);
bytes = hashfunc(bytes);
if (namespace?.length !== 16) {
throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
}

bytes[6] = (bytes[6] & 0x0f) | version;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
// Compute hash of namespace and value, Per 4.3
// Future: Use spread syntax when supported on all platforms, e.g. `bytes =
// hashfunc([...namespace, ... value])`
let bytes = new Uint8Array(16 + valueBytes.length);
bytes.set(namespaceBytes);
bytes.set(valueBytes, namespaceBytes.length);
bytes = hash(bytes);

if (buf) {
offset = offset || 0;
bytes[6] = (bytes[6] & 0x0f) | version;
bytes[8] = (bytes[8] & 0x3f) | 0x80;

for (let i = 0; i < 16; ++i) {
buf[offset + i] = bytes[i];
}
if (buf) {
offset = offset || 0;

return buf;
for (let i = 0; i < 16; ++i) {
buf[offset + i] = bytes[i];
}

return unsafeStringify(bytes);
return buf;
}

// Function#name is not settable on some platforms (#270)
try {
generateUUID.name = name;
} catch (err) {}

// For CommonJS default export support
generateUUID.DNS = DNS;
generateUUID.URL = URL;

return generateUUID;
return unsafeStringify(bytes);
}
8 changes: 2 additions & 6 deletions src/v4.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { UUIDString, Version4Options } from './_types.js';
import { UUIDString, UUIDTypes, Version4Options } from './_types.js';
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
): UUIDString | Uint8Array {
function v4(options: Version4Options = {}, buf?: Uint8Array, offset?: number): UUIDTypes {
if (native.randomUUID && !buf && !options) {
return native.randomUUID();
}
Expand Down
23 changes: 21 additions & 2 deletions src/v5.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import v35 from './v35.js';
import { UUIDString, UUIDTypes } from './_types.js';
import sha1 from './sha1.js';
import v35, { DNS, URL } from './v35.js';

function v5(
value: string | Uint8Array,
namespace: UUIDTypes,
buf?: Uint8Array,
offset?: number
): UUIDString;
function v5(
value: string | Uint8Array,
namespace: UUIDTypes,
buf?: Uint8Array,
offset?: number
): UUIDString;
function v5(value: string | Uint8Array, namespace: UUIDTypes, buf?: Uint8Array, offset?: number) {
return v35(0x50, sha1, value, namespace, buf, offset);
}

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

const v5 = v35('v5', 0x50, sha1);
export default v5;
4 changes: 2 additions & 2 deletions src/v6ToV1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UUIDString } from './_types.js';
import { UUIDString, UUIDTypes } from './_types.js';
import parse from './parse.js';
import { unsafeStringify } from './stringify.js';

Expand All @@ -11,7 +11,7 @@ import { unsafeStringify } from './stringify.js';
*/
export default function v6ToV1(uuid: UUIDString): UUIDString;
export default function v6ToV1(uuid: Uint8Array): Uint8Array;
export default function v6ToV1(uuid: UUIDString | Uint8Array): UUIDString | Uint8Array {
export default function v6ToV1(uuid: UUIDTypes): UUIDTypes {
const v6Bytes = typeof uuid === 'string' ? parse(uuid) : uuid;

const v1Bytes = _v6ToV1(v6Bytes);
Expand Down
8 changes: 2 additions & 6 deletions src/v7.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UUIDString, Version7Options } from './_types.js';
import { UUIDString, UUIDTypes, Version7Options } from './_types.js';
import rng from './rng.js';
import { unsafeStringify } from './stringify.js';

Expand Down Expand Up @@ -43,11 +43,7 @@ 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
): UUIDString | Uint8Array {
function v7(options: Version7Options = {}, buf?: Uint8Array, offset?: number): UUIDTypes {
// initialize buffer and pointer
let i = (buf && offset) || 0;
const b = buf || new Uint8Array(16);
Expand Down

0 comments on commit 3e3abe9

Please sign in to comment.