Skip to content

Commit

Permalink
refactor(uuid): prepare for noUncheckedIndexedAccess (#4445)
Browse files Browse the repository at this point in the history
* refactor(uuid): prepare for `noUncheckedIndexedAccess`

* added a test and updated the error message

* changed to !== 6 and added another test

* update error message to be clearer
  • Loading branch information
syhol committed Mar 10, 2024
1 parent 623d7d0 commit 5c23dcb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion uuid/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 7 additions & 1 deletion uuid/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions uuid/v1_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
() => {
Expand Down
4 changes: 2 additions & 2 deletions uuid/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions uuid/v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 5c23dcb

Please sign in to comment.