diff options
Diffstat (limited to 'std/uuid')
-rw-r--r-- | std/uuid/README.md | 15 | ||||
-rw-r--r-- | std/uuid/_common.ts | 63 | ||||
-rw-r--r-- | std/uuid/mod.ts | 18 | ||||
-rw-r--r-- | std/uuid/test.ts | 13 | ||||
-rw-r--r-- | std/uuid/v1.ts | 117 | ||||
-rw-r--r-- | std/uuid/v1_test.ts | 49 | ||||
-rw-r--r-- | std/uuid/v4.ts | 25 | ||||
-rw-r--r-- | std/uuid/v4_test.ts | 35 | ||||
-rw-r--r-- | std/uuid/v5.ts | 67 | ||||
-rw-r--r-- | std/uuid/v5_test.ts | 83 |
10 files changed, 0 insertions, 485 deletions
diff --git a/std/uuid/README.md b/std/uuid/README.md deleted file mode 100644 index 846b78263..000000000 --- a/std/uuid/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# UUID - -Support for version 1, 4, and 5 UUIDs. - -## Usage - -```ts -import { v4 } from "https://deno.land/std@$STD_VERSION/uuid/mod.ts"; - -// Generate a v4 uuid. -const myUUID = v4.generate(); - -// Validate a v4 uuid. -const isValid = v4.validate(myUUID); -``` diff --git a/std/uuid/_common.ts b/std/uuid/_common.ts deleted file mode 100644 index 1f3228aea..000000000 --- a/std/uuid/_common.ts +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -/** - * Converts the byte array to a UUID string - * @param bytes Used to convert Byte to Hex - */ -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, 16), - ].join(""); -} - -/** - * Converts a string to a byte array by converting the hex value to a number - * @param uuid Value that gets converted - */ -export function uuidToBytes(uuid: string): number[] { - const bytes: number[] = []; - - uuid.replace(/[a-fA-F0-9]{2}/g, (hex: string): string => { - bytes.push(parseInt(hex, 16)); - return ""; - }); - - return bytes; -} - -/** - * Converts a string to a byte array using the char code - * @param str Value that gets converted - */ -export function stringToBytes(str: string): number[] { - str = unescape(encodeURIComponent(str)); - const bytes = new Array(str.length); - for (let i = 0; i < str.length; i++) { - bytes[i] = str.charCodeAt(i); - } - return bytes; -} - -/** - * Creates a buffer for creating a SHA-1 hash - * @param content Buffer for SHA-1 hash - */ -export function createBuffer(content: number[]): ArrayBuffer { - const arrayBuffer = new ArrayBuffer(content.length); - const uint8Array = new Uint8Array(arrayBuffer); - for (let i = 0; i < content.length; i++) { - uint8Array[i] = content[i]; - } - return arrayBuffer; -} diff --git a/std/uuid/mod.ts b/std/uuid/mod.ts deleted file mode 100644 index b2580319c..000000000 --- a/std/uuid/mod.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Based on https://github.com/kelektiv/node-uuid -> https://www.ietf.org/rfc/rfc4122.txt -// Supporting Support for RFC4122 version 1, 4, and 5 UUIDs -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import * as v1 from "./v1.ts"; -import * as v4 from "./v4.ts"; -import * as v5 from "./v5.ts"; - -export const NIL_UUID = "00000000-0000-0000-0000-000000000000"; - -/** - * Checks if UUID is nil - * @param val UUID value - */ -export function isNil(val: string): boolean { - return val === NIL_UUID; -} - -export { v1, v4, v5 }; diff --git a/std/uuid/test.ts b/std/uuid/test.ts deleted file mode 100644 index 7d0feaf8d..000000000 --- a/std/uuid/test.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assert } from "../testing/asserts.ts"; -import { isNil, NIL_UUID } from "./mod.ts"; - -Deno.test({ - name: "[UUID] isNil", - fn(): void { - const nil = NIL_UUID; - const u = "582cbcff-dad6-4f28-888a-e062ae36bafc"; - assert(isNil(nil)); - assert(!isNil(u)); - }, -}); diff --git a/std/uuid/v1.ts b/std/uuid/v1.ts deleted file mode 100644 index b97c704e7..000000000 --- a/std/uuid/v1.ts +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2018-2021 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", -); - -/** - * Validates the UUID v1 - * @param id UUID value - */ -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[]; -}; - -/** - * Generates a RFC4122 v1 UUID (time-based) - * @param options Can use RFC time sequence values as overwrites - * @param buf Can allow the UUID to be written in byte-form starting at the offset - * @param offset Index to start writing on the UUID bytes in buffer - */ -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) { - // deno-lint-ignore 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/v1_test.ts b/std/uuid/v1_test.ts deleted file mode 100644 index 5971d3f4e..000000000 --- a/std/uuid/v1_test.ts +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } 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`); - }, -}); - -Deno.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"); - }, -}); - -Deno.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`); - } - }, -}); - -Deno.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/v4.ts b/std/uuid/v4.ts deleted file mode 100644 index e059cd937..000000000 --- a/std/uuid/v4.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2018-2021 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", -); - -/** - * Validates the UUID v4 - * @param id UUID value - */ -export function validate(id: string): boolean { - return UUID_RE.test(id); -} - -/** Generates a RFC4122 v4 UUID (pseudo-randomly-based) */ -export function generate(): string { - const rnds = crypto.getRandomValues(new Uint8Array(16)); - - rnds[6] = (rnds[6] & 0x0f) | 0x40; // Version 4 - rnds[8] = (rnds[8] & 0x3f) | 0x80; // Variant 10 - - return bytesToUuid(rnds); -} diff --git a/std/uuid/v4_test.ts b/std/uuid/v4_test.ts deleted file mode 100644 index 6c9cb6b4a..000000000 --- a/std/uuid/v4_test.ts +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } from "../testing/asserts.ts"; -import { generate, validate } from "./v4.ts"; - -Deno.test({ - name: "[UUID] test_uuid_v4", - fn(): void { - const u = generate(); - assertEquals(typeof u, "string", "returns a string"); - assert(u !== "", "return string is not empty"); - }, -}); - -Deno.test({ - name: "[UUID] test_uuid_v4_format", - fn(): void { - for (let i = 0; i < 10000; i++) { - const u = generate() as string; - assert(validate(u), `${u} is not a valid uuid v4`); - } - }, -}); - -Deno.test({ - name: "[UUID] is_valid_uuid_v4", - fn(): void { - const u = generate(); - const t = "84fb7824-b951-490e-8afd-0c13228a8282"; - const n = "84fb7824-b951-490g-8afd-0c13228a8282"; - - assert(validate(u), `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/v5.ts b/std/uuid/v5.ts deleted file mode 100644 index 7f6a098e0..000000000 --- a/std/uuid/v5.ts +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { - bytesToUuid, - createBuffer, - stringToBytes, - uuidToBytes, -} from "./_common.ts"; -import { Sha1 } from "../hash/sha1.ts"; -import { assert } from "../_util/assert.ts"; - -const UUID_RE = - /^[0-9a-f]{8}-[0-9a-f]{4}-[5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; - -/** - * Validates the UUID v5 - * @param id UUID value - */ -export function validate(id: string): boolean { - return UUID_RE.test(id); -} - -interface V5Options { - value: string | number[]; - namespace: string | number[]; -} - -/** - * Generates a RFC4122 v5 UUID (SHA-1 namespace-based) - * @param options Can use a namespace and value to create SHA-1 hash - * @param buf Can allow the UUID to be written in byte-form starting at the offset - * @param offset Index to start writing on the UUID bytes in buffer - */ -export function generate( - options: V5Options, - buf?: number[], - offset?: number, -): string | number[] { - const i = (buf && offset) || 0; - - let { value, namespace } = options; - if (typeof value == "string") { - value = stringToBytes(value as string); - } - - if (typeof namespace == "string") { - namespace = uuidToBytes(namespace as string); - } - - assert( - namespace.length === 16, - "namespace must be uuid string or an Array of 16 byte values", - ); - - const content = (namespace as number[]).concat(value as number[]); - const bytes = new Sha1().update(createBuffer(content)).digest(); - - bytes[6] = (bytes[6] & 0x0f) | 0x50; - bytes[8] = (bytes[8] & 0x3f) | 0x80; - - if (buf) { - for (let idx = 0; idx < 16; ++idx) { - buf[i + idx] = bytes[idx]; - } - } - - return buf || bytesToUuid(bytes); -} diff --git a/std/uuid/v5_test.ts b/std/uuid/v5_test.ts deleted file mode 100644 index 15da32aee..000000000 --- a/std/uuid/v5_test.ts +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } from "../testing/asserts.ts"; -import { generate, validate } from "./v5.ts"; - -const NAMESPACE = "1b671a64-40d5-491e-99b0-da01ff1f3341"; - -Deno.test({ - name: "[UUID] test_uuid_v5", - fn(): void { - const u = generate({ value: "", namespace: NAMESPACE }); - assertEquals(typeof u, "string", "returns a string"); - assert(u !== "", "return string is not empty"); - }, -}); - -Deno.test({ - name: "[UUID] test_uuid_v5_format", - fn(): void { - for (let i = 0; i < 10000; i++) { - const u = generate({ value: String(i), namespace: NAMESPACE }) as string; - assert(validate(u), `${u} is not a valid uuid v5`); - } - }, -}); - -Deno.test({ - name: "[UUID] test_uuid_v5_option", - fn(): void { - const v5Options = { - value: "Hello, World", - namespace: NAMESPACE, - }; - const u = generate(v5Options); - assertEquals(u, "4b4f2adc-5b27-57b5-8e3a-c4c4bcf94f05"); - }, -}); - -Deno.test({ - name: "[UUID] test_uuid_v5_buf_offset", - fn(): void { - const buf = [ - 75, - 79, - 42, - 220, - 91, - 39, - 87, - 181, - 142, - 58, - 196, - 196, - 188, - 249, - 79, - 5, - ]; - const origin = JSON.parse(JSON.stringify(buf)); - generate({ value: "Hello, World", namespace: NAMESPACE }, buf); - assertEquals(origin, buf); - - generate({ value: "Hello, World", namespace: NAMESPACE }, buf, 3); - assertEquals(origin.slice(0, 3), buf.slice(0, 3)); - assertEquals(origin, buf.slice(3)); - }, -}); - -Deno.test({ - name: "[UUID] is_valid_uuid_v5", - fn(): void { - const u = generate({ - value: "Hello, World", - namespace: "1b671a64-40d5-491e-99b0-da01ff1f3341", - }) as string; - const t = "4b4f2adc-5b27-57b5-8e3a-c4c4bcf94f05"; - const n = "4b4f2adc-5b27-17b5-8e3a-c4c4bcf94f05"; - - assert(validate(u), `generated ${u} should be valid`); - assert(validate(t), `${t} should be valid`); - assert(!validate(n), `${n} should not be valid`); - }, -}); |