diff --git a/uuid/mod.ts b/uuid/mod.ts index c3ab445504fe..2d676a9dd113 100644 --- a/uuid/mod.ts +++ b/uuid/mod.ts @@ -76,5 +76,5 @@ export function version(uuid: string): number { throw new TypeError("Invalid UUID"); } - return parseInt(uuid[14], 16); + return parseInt(uuid[14]!, 16); } diff --git a/uuid/v1.ts b/uuid/v1.ts index 55a928bf229b..d9d34d40deb0 100644 --- a/uuid/v1.ts +++ b/uuid/v1.ts @@ -136,6 +136,12 @@ export function generate( throw new Error("Can't create more than 10M uuids/sec"); } + if (node.length !== 6) { + throw new Error( + "Cannot create UUID. The node option must be an array of 6 bytes", + ); + } + _lastMSecs = msecs; _lastNSecs = nsecs; _clockseq = clockseq; @@ -163,7 +169,7 @@ export function generate( b[i++] = clockseq & 0xff; for (let n = 0; n < 6; ++n) { - b[i + n] = node[n]; + b[i + n] = node[n]!; } return buf ?? bytesToUuid(b); diff --git a/uuid/v1_test.ts b/uuid/v1_test.ts index 818498d9207b..c10600e19019 100644 --- a/uuid/v1_test.ts +++ b/uuid/v1_test.ts @@ -59,6 +59,26 @@ Deno.test("generate() can fill the UUID into a buffer", () => { assertEquals(buf, uuid); }); +Deno.test("generate() throws when node is passed with less than 6 numbers", () => { + assertThrows( + () => { + generate({ node: [0x01, 0x23, 0x45, 0x67, 0x89] }); + }, + Error, + "Cannot create UUID. The node option must be an array of 6 bytes", + ); +}); + +Deno.test("generate() throws when node is passed with more than 6 numbers", () => { + assertThrows( + () => { + generate({ node: [0x01, 0x23, 0x45, 0x67, 0x89, 0x89, 0x89] }); + }, + Error, + "Cannot create UUID. The node option must be an array of 6 bytes", + ); +}); + Deno.test("generate() throws when create more than 10M uuids/sec", () => { assertThrows( () => { diff --git a/uuid/v3.ts b/uuid/v3.ts index 466314f1bb12..8733dc3f7b10 100644 --- a/uuid/v3.ts +++ b/uuid/v3.ts @@ -54,8 +54,8 @@ export async function generate( const buffer = await crypto.subtle.digest("MD5", toHash); const bytes = new Uint8Array(buffer); - bytes[6] = (bytes[6] & 0x0f) | 0x30; - bytes[8] = (bytes[8] & 0x3f) | 0x80; + bytes[6] = (bytes[6]! & 0x0f) | 0x30; + bytes[8] = (bytes[8]! & 0x3f) | 0x80; return bytesToUuid(bytes); } diff --git a/uuid/v5.ts b/uuid/v5.ts index 539580cbb4c6..8ad85ce083f9 100644 --- a/uuid/v5.ts +++ b/uuid/v5.ts @@ -53,8 +53,8 @@ export async function generate( const buffer = await crypto.subtle.digest("sha-1", toHash); const bytes = new Uint8Array(buffer); - bytes[6] = (bytes[6] & 0x0f) | 0x50; - bytes[8] = (bytes[8] & 0x3f) | 0x80; + bytes[6] = (bytes[6]! & 0x0f) | 0x50; + bytes[8] = (bytes[8]! & 0x3f) | 0x80; return bytesToUuid(bytes); }