diff --git a/std/uuid/_common.ts b/std/uuid/_common.ts new file mode 100644 index 00000000000000..b0cad25840c015 --- /dev/null +++ b/std/uuid/_common.ts @@ -0,0 +1,18 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +export function bytesToUuid(bytes: number[] | Uint8Array): string { + const bits: string[] = [...bytes].map((bit): string => { + const s: string = bit.toString(16); + return bit < 0x10 ? "0" + s : s; + }); + return [ + ...bits.slice(0, 4), + "-", + ...bits.slice(4, 6), + "-", + ...bits.slice(6, 8), + "-", + ...bits.slice(8, 10), + "-", + ...bits.slice(10), + ].join(""); +} diff --git a/std/uuid/mod.ts b/std/uuid/mod.ts index 8e312c5585d6a6..23d0c6410eb347 100644 --- a/std/uuid/mod.ts +++ b/std/uuid/mod.ts @@ -1,5 +1,6 @@ // Based on https://github.com/kelektiv/node-uuid // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import * as v1 from "./v1.ts"; import * as v4 from "./v4.ts"; export const NIL_UUID = "00000000-0000-0000-0000-000000000000"; @@ -17,12 +18,10 @@ const NOT_IMPLEMENTED = { }, }; -// TODO Implement -export const v1 = NOT_IMPLEMENTED; // TODO Implement export const v3 = NOT_IMPLEMENTED; -export { v4 }; +export { v1, v4 }; // TODO Implement export const v5 = NOT_IMPLEMENTED; diff --git a/std/uuid/test.ts b/std/uuid/test.ts index aeb6ed1123aece..74d3c093d69cdb 100755 --- a/std/uuid/test.ts +++ b/std/uuid/test.ts @@ -4,6 +4,10 @@ // Generic Tests import "./tests/isNil.ts"; +// V1 Tests +import "./tests/v1/validate.ts"; +import "./tests/v1/generate.ts"; + // V4 Tests import "./tests/v4/validate.ts"; import "./tests/v4/generate.ts"; diff --git a/std/uuid/tests/v1/generate.ts b/std/uuid/tests/v1/generate.ts new file mode 100644 index 00000000000000..1e60d91c1fbbb1 --- /dev/null +++ b/std/uuid/tests/v1/generate.ts @@ -0,0 +1,37 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { assert, assertEquals } from "../../../testing/asserts.ts"; +const { test } = Deno; +import { generate, validate } from "../../v1.ts"; + +test({ + name: "[UUID] test_uuid_v1", + fn(): void { + const u = generate(); + assertEquals(typeof u, "string", "returns a string"); + assert(u !== "", "return string is not empty"); + }, +}); + +test({ + name: "[UUID] test_uuid_v1_format", + fn(): void { + for (let i = 0; i < 10000; i++) { + const u = generate() as string; + assert(validate(u), `${u} is not a valid uuid v1`); + } + }, +}); + +test({ + name: "[UUID] test_uuid_v1_static", + fn(): void { + const v1options = { + node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab], + clockseq: 0x1234, + msecs: new Date("2011-11-01").getTime(), + nsecs: 5678, + }; + const u = generate(v1options); + assertEquals(u, "710b962e-041c-11e1-9234-0123456789ab"); + }, +}); diff --git a/std/uuid/tests/v1/validate.ts b/std/uuid/tests/v1/validate.ts new file mode 100644 index 00000000000000..1d066eb0226526 --- /dev/null +++ b/std/uuid/tests/v1/validate.ts @@ -0,0 +1,16 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { assert } from "../../../testing/asserts.ts"; +import { generate, validate } from "../../v1.ts"; + +Deno.test({ + name: "[UUID] is_valid_uuid_v1", + fn(): void { + const u = generate(); + const t = "63655efa-7ee6-11ea-bc55-0242ac130003"; + const n = "63655efa-7ee6-11eg-bc55-0242ac130003"; + + assert(validate(u as string), `generated ${u} should be valid`); + assert(validate(t), `${t} should be valid`); + assert(!validate(n), `${n} should not be valid`); + }, +}); diff --git a/std/uuid/v1.ts b/std/uuid/v1.ts new file mode 100644 index 00000000000000..cbfce576a57353 --- /dev/null +++ b/std/uuid/v1.ts @@ -0,0 +1,108 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { bytesToUuid } from "./_common.ts"; + +const UUID_RE = new RegExp( + "^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", + "i" +); + +export function validate(id: string): boolean { + return UUID_RE.test(id); +} + +let _nodeId: number[]; +let _clockseq: number; + +let _lastMSecs = 0; +let _lastNSecs = 0; + +type V1Options = { + node?: number[]; + clockseq?: number; + msecs?: number; + nsecs?: number; + random?: number[]; + rng?: () => number[]; +}; + +export function generate( + options?: V1Options | null, + buf?: number[], + offset?: number +): string | number[] { + let i = (buf && offset) || 0; + const b = buf || []; + + options = options || {}; + let node = options.node || _nodeId; + let clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; + + if (node == null || clockseq == null) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const seedBytes: any = + options.random || + options.rng || + crypto.getRandomValues(new Uint8Array(16)); + if (node == null) { + node = _nodeId = [ + seedBytes[0] | 0x01, + seedBytes[1], + seedBytes[2], + seedBytes[3], + seedBytes[4], + seedBytes[5], + ]; + } + if (clockseq == null) { + clockseq = _clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff; + } + } + let msecs = + options.msecs !== undefined ? options.msecs : new Date().getTime(); + + let nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; + + const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; + + if (dt < 0 && options.clockseq === undefined) { + clockseq = (clockseq + 1) & 0x3fff; + } + + if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) { + nsecs = 0; + } + + if (nsecs >= 10000) { + throw new Error("Can't create more than 10M uuids/sec"); + } + + _lastMSecs = msecs; + _lastNSecs = nsecs; + _clockseq = clockseq; + + msecs += 12219292800000; + + const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000; + b[i++] = (tl >>> 24) & 0xff; + b[i++] = (tl >>> 16) & 0xff; + b[i++] = (tl >>> 8) & 0xff; + b[i++] = tl & 0xff; + + const tmh = ((msecs / 0x100000000) * 10000) & 0xfffffff; + b[i++] = (tmh >>> 8) & 0xff; + b[i++] = tmh & 0xff; + + b[i++] = ((tmh >>> 24) & 0xf) | 0x10; + b[i++] = (tmh >>> 16) & 0xff; + + b[i++] = (clockseq >>> 8) | 0x80; + + b[i++] = clockseq & 0xff; + + for (let n = 0; n < 6; ++n) { + b[i + n] = node[n]; + } + + return buf ? buf : bytesToUuid(b); +} diff --git a/std/uuid/v4.ts b/std/uuid/v4.ts index d291b5478d5078..83fc6d86eef1df 100644 --- a/std/uuid/v4.ts +++ b/std/uuid/v4.ts @@ -1,5 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { bytesToUuid } from "./_common.ts"; + const UUID_RE = new RegExp( "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", "i" @@ -15,19 +17,5 @@ export function generate(): string { rnds[6] = (rnds[6] & 0x0f) | 0x40; // Version 4 rnds[8] = (rnds[8] & 0x3f) | 0x80; // Variant 10 - const bits: string[] = [...rnds].map((bit): string => { - const s: string = bit.toString(16); - return bit < 0x10 ? "0" + s : s; - }); - return [ - ...bits.slice(0, 4), - "-", - ...bits.slice(4, 6), - "-", - ...bits.slice(6, 8), - "-", - ...bits.slice(8, 10), - "-", - ...bits.slice(10), - ].join(""); + return bytesToUuid(rnds); }