Skip to content

feat(site): add generateUUID helper - #27661

Merged
jeremyruppel merged 3 commits into
mainfrom
jeremyruppel/generate-uuid-helper
Jul 30, 2026
Merged

feat(site): add generateUUID helper#27661
jeremyruppel merged 3 commits into
mainfrom
jeremyruppel/generate-uuid-helper

Conversation

@jeremyruppel

@jeremyruppel jeremyruppel commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Adds a generateUUID() helper to site/src/utils/uuid.ts.

It uses crypto.randomUUID() when available, and otherwise falls back to crypto.getRandomValues(), setting the version (4) and variant (RFC 4122) bits before formatting the 16 random bytes into the standard 8-4-4-4-12 UUID string.

Seriously open to any implementation here, let me know if you have a favorite!


This PR was created by Coder Agents on behalf of @jeremyruppel.

@jeremyruppel jeremyruppel changed the title feat(site/src/utils): add generateUUID helper feat(site): add generateUUID helper Jul 29, 2026
@jeremyruppel
jeremyruppel requested a review from aslilac July 29, 2026 20:28
@jeremyruppel
jeremyruppel marked this pull request as ready for review July 29, 2026 20:28
Comment thread site/src/utils/uuid.ts
return crypto.randomUUID();
}

const bytes = crypto.getRandomValues(new Uint8Array(16));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, if crypto.randomUUID is not available, is crypto.getRandomValues gonna be available? I thought the crypto library was node specific? but my browser says its there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it all has to do with the secure context. try on an http:// url (your workspace is a good one) and gatRandomValues should be there but randomUUID shouldn't

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also if this fact makes you want to yell into the void, boy do I have a slack channel for you!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow you're right, that is maddening TIL

Comment thread site/src/utils/uuid.ts
Comment on lines +22 to +23
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;

@untra untra Jul 30, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't get me wrong I love some crafty bitwise foo as much as the next person, but this is hard to understand, and not tested. Yeah you are generating a random uuid here, but no matter what you could expect that the https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.2 bits are there and set right.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call on the tests, comin' right up!

@jeremyruppel
jeremyruppel force-pushed the jeremyruppel/generate-uuid-helper branch from 42eca18 to 05c8478 Compare July 30, 2026 14:37
@jeremyruppel
jeremyruppel merged commit cf7f876 into main Jul 30, 2026
22 checks passed
@jeremyruppel
jeremyruppel deleted the jeremyruppel/generate-uuid-helper branch July 30, 2026 14:46
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 30, 2026
@aqandrew

Copy link
Copy Markdown
Contributor

Why was a new helper added when we already have uuid as a dependency? It also follows RFC 4122

import { v4 as uuidv4 } from "uuid";

@jeremyruppel

Copy link
Copy Markdown
Contributor Author

Why was a new helper added when we already have uuid as a dependency?

I checked and I coulda sworn it was a devDependency! nice catch, delete PR incoming

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants