feat(site): add generateUUID helper - #27661
Conversation
| return crypto.randomUUID(); | ||
| } | ||
|
|
||
| const bytes = crypto.getRandomValues(new Uint8Array(16)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
also if this fact makes you want to yell into the void, boy do I have a slack channel for you!
There was a problem hiding this comment.
wow you're right, that is maddening TIL
| bytes[6] = (bytes[6] & 0x0f) | 0x40; | ||
| bytes[8] = (bytes[8] & 0x3f) | 0x80; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
good call on the tests, comin' right up!
42eca18 to
05c8478
Compare
|
Why was a new helper added when we already have uuid as a dependency? It also follows RFC 4122 |
I checked and I coulda sworn it was a |
Adds a
generateUUID()helper tosite/src/utils/uuid.ts.It uses
crypto.randomUUID()when available, and otherwise falls back tocrypto.getRandomValues(), setting the version (4) and variant (RFC 4122) bits before formatting the 16 random bytes into the standard8-4-4-4-12UUID 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.