Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(uuid): prepare for noUncheckedIndexedAccess #4445

Merged
merged 4 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
}
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]!;
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
}

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;
iuioiua marked this conversation as resolved.
Show resolved Hide resolved

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;
iuioiua marked this conversation as resolved.
Show resolved Hide resolved

return bytesToUuid(bytes);
}